1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * Sample MIDI Wine Driver for Linux
6 * Copyright 1994 Martin Ayotte
11 * 98/7 changes for making this MIDI driver work on OSS
12 * current support is limited to MIDI ports of OSS systems
13 * 98/9 rewriting MCI code for MIDI
14 * 98/11 splitted in midi.c and mcimidi.c
23 #include <sys/ioctl.h>
25 #include "multimedia.h"
29 #include "debugtools.h"
31 DECLARE_DEBUG_CHANNEL(mcimidi)
32 DECLARE_DEBUG_CHANNEL(midi)
35 DWORD dwFirst; /* offset in file of track */
36 DWORD dwLast; /* number of bytes in file of track */
37 DWORD dwIndex; /* current index in file (dwFirst <= dwIndex < dwLast) */
38 DWORD dwLength; /* number of pulses in this track */
39 DWORD dwEventPulse; /* current pulse # (event) pointed by dwIndex */
40 DWORD dwEventData; /* current data (event) pointed by dwIndex */
41 WORD wEventLength; /* current length (event) pointed by dwIndex */
42 WORD wStatus : 1, /* 1 : playing, 0 : done */
50 int nUseCount; /* Incremented for each shared open */
51 WORD wNotifyDeviceID; /* MCI device ID with a pending notification */
52 HANDLE16 hCallback; /* Callback handle for pending notification */
53 HMMIO hFile; /* mmio file handle open as Element */
54 MCI_OPEN_PARMSA openParms;
56 WORD dwStatus; /* one from MCI_MODE_xxxx */
57 DWORD dwMciTimeFormat; /* One of the supported MCI_FORMAT_xxxx */
58 WORD wFormat; /* Format of MIDI hFile (0, 1 or 2) */
59 WORD nTracks; /* Number of tracks in hFile */
60 WORD nDivision; /* Number of division in hFile PPQN or SMPTE */
62 DWORD dwTempo; /* Tempo (# of 1/4 note per second */
63 MCI_MIDITRACK* tracks; /* Content of each track */
69 #define MAX_MCIMIDIDRV (1)
70 static WINE_MCIMIDI MCIMidiDev[MAX_MCIMIDIDRV];
72 /*======================================================================*
73 * MCI MIDI implemantation *
74 *======================================================================*/
76 #ifdef SNDCTL_MIDI_INFO
77 /**************************************************************************
78 * MIDI_drvGetDrv [internal]
80 static WINE_MCIMIDI* MIDI_drvGetDrv(UINT16 wDevID)
84 for (i = 0; i < MAX_MCIMIDIDRV; i++) {
85 if (MCIMidiDev[i].wDevID == wDevID) {
86 return &MCIMidiDev[i];
92 /**************************************************************************
93 * MIDI_drvOpen [internal]
95 static DWORD MIDI_drvOpen(LPSTR str, LPMCI_OPEN_DRIVER_PARMSA modp)
99 for (i = 0; i < MAX_MCIMIDIDRV; i++) {
100 if (MCIMidiDev[i].wDevID == 0) {
101 MCIMidiDev[i].wDevID = modp->wDeviceID;
102 modp->wCustomCommandTable = -1;
103 modp->wType = MCI_DEVTYPE_CD_AUDIO;
104 return modp->wDeviceID;
110 /**************************************************************************
111 * MCIMIDI_drvClose [internal]
113 static DWORD MIDI_drvClose(DWORD dwDevID)
115 WINE_MCIMIDI* wmm = MIDI_drvGetDrv(dwDevID);
124 /**************************************************************************
125 * MIDI_mciGetOpenDev [internal]
127 static WINE_MCIMIDI* MIDI_mciGetOpenDev(UINT16 wDevID)
129 WINE_MCIMIDI* wmm = MIDI_drvGetDrv(wDevID);
131 if (wmm == NULL || wmm->nUseCount == 0) {
132 WARN_(mcimidi)("Invalid wDevID=%u\n", wDevID);
138 /**************************************************************************
139 * MIDI_mciReadByte [internal]
141 static DWORD MIDI_mciReadByte(WINE_MCIMIDI* wmm, BYTE *lpbyt)
146 mmioRead(wmm->hFile, (HPSTR)lpbyt, (long)sizeof(BYTE)) != (long)sizeof(BYTE)) {
147 WARN_(mcimidi)("Error reading wmm=%p\n", wmm);
148 ret = MCIERR_INVALID_FILE;
154 /**************************************************************************
155 * MIDI_mciReadWord [internal]
157 static DWORD MIDI_mciReadWord(WINE_MCIMIDI* wmm, LPWORD lpw)
160 DWORD ret = MCIERR_INVALID_FILE;
163 MIDI_mciReadByte(wmm, &hibyte) == 0 &&
164 MIDI_mciReadByte(wmm, &lobyte) == 0) {
165 *lpw = ((WORD)hibyte << 8) + lobyte;
171 /**************************************************************************
172 * MIDI_mciReadLong [internal]
174 static DWORD MIDI_mciReadLong(WINE_MCIMIDI* wmm, LPDWORD lpdw)
177 DWORD ret = MCIERR_INVALID_FILE;
180 MIDI_mciReadWord(wmm, &hiword) == 0 &&
181 MIDI_mciReadWord(wmm, &loword) == 0) {
182 *lpdw = MAKELONG(loword, hiword);
188 /**************************************************************************
189 * MIDI_mciReadVaryLen [internal]
191 static WORD MIDI_mciReadVaryLen(WINE_MCIMIDI* wmm, LPDWORD lpdw)
198 ret = MCIERR_INVALID_FILE;
201 if (MIDI_mciReadByte(wmm, &byte) != 0) {
204 value = (value << 7) + (byte & 0x7F);
206 } while (byte & 0x80);
209 TRACE(mcimidi, "val=%08lX \n", value);
215 /**************************************************************************
216 * MIDI_mciReadNextEvent [internal]
218 static DWORD MIDI_mciReadNextEvent(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
226 if (mmioSeek(wmm->hFile, mmt->dwIndex, SEEK_SET) != mmt->dwIndex) {
227 WARN_(mcimidi)("Can't seek at %08lX \n", mmt->dwIndex);
228 return MCIERR_INVALID_FILE;
230 evtLength = MIDI_mciReadVaryLen(wmm, &evtPulse) + 1; /* > 0 */
231 MIDI_mciReadByte(wmm, &b1);
235 evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
239 MIDI_mciReadByte(wmm, &b2); evtLength++;
241 evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
242 if (evtLength >= 0x10000u) {
243 /* this limitation shouldn't be a problem */
244 WARN_(mcimidi)("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
247 hw = LOWORD(evtLength);
252 if (b1 & 0x80) { /* use running status ? */
253 mmt->wLastCommand = b1;
254 MIDI_mciReadByte(wmm, &b2); evtLength++;
257 b1 = mmt->wLastCommand;
259 switch ((b1 >> 4) & 0x07) {
260 case 0: case 1: case 2: case 3: case 6:
261 MIDI_mciReadByte(wmm, &b3); evtLength++;
267 WARN_(mcimidi)("Strange indeed b1=0x%02x\n", b1);
271 if (mmt->dwIndex + evtLength > mmt->dwLast)
272 return MCIERR_INTERNAL;
274 mmt->dwEventPulse += evtPulse;
275 mmt->dwEventData = (hw << 16) + (b2 << 8) + b1;
276 mmt->wEventLength = evtLength;
279 TRACE(mcimidi, "[%u] => pulse=%08lx(%08lx), data=%08lx, length=%u\n",
280 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
281 mmt->dwEventData, mmt->wEventLength);
286 /**************************************************************************
287 * MIDI_mciReadMTrk [internal]
289 static DWORD MIDI_mciReadMTrk(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
294 if (mmioRead(wmm->hFile, (HPSTR)&fourcc, (long)sizeof(FOURCC)) !=
295 (long)sizeof(FOURCC)) {
296 return MCIERR_INVALID_FILE;
299 if (fourcc != mmioFOURCC('M', 'T', 'r', 'k')) {
300 WARN_(mcimidi)("Can't synchronize on 'MTrk' !\n");
301 return MCIERR_INVALID_FILE;
304 if (MIDI_mciReadLong(wmm, &toberead) != 0) {
305 return MCIERR_INVALID_FILE;
307 mmt->dwFirst = mmioSeek(wmm->hFile, 0, SEEK_CUR); /* >= 0 */
308 mmt->dwLast = mmt->dwFirst + toberead;
310 /* compute # of pulses in this track */
311 mmt->dwIndex = mmt->dwFirst;
312 mmt->dwEventPulse = 0;
314 while (MIDI_mciReadNextEvent(wmm, mmt) == 0 &&
315 LOWORD(mmt->dwEventData) != 0x2FFF) {
316 mmt->dwIndex += mmt->wEventLength;
318 mmt->dwLength = mmt->dwEventPulse;
320 TRACE_(mcimidi)("Track %u has %lu bytes and %lu pulses\n", mmt->wTrackNr, toberead, mmt->dwLength);
322 /* reset track data */
323 mmt->wStatus = 1; /* ok, playing */
324 mmt->dwIndex = mmt->dwFirst;
325 mmt->dwEventPulse = 0;
327 if (mmioSeek(wmm->hFile, 0, SEEK_CUR) != mmt->dwLast) {
328 WARN_(mcimidi)("Ouch, out of sync seek=%lu track=%lu\n",
329 mmioSeek(wmm->hFile, 0, SEEK_CUR), mmt->dwLast);
330 /* position at end of this track, to be ready to read next track */
331 mmioSeek(wmm->hFile, mmt->dwLast, SEEK_SET);
337 /**************************************************************************
338 * MIDI_mciReadMThd [internal]
340 static DWORD MIDI_mciReadMThd(WINE_MCIMIDI* wmm, DWORD dwOffset)
346 TRACE_(mcimidi)("(%p, %08lX);\n", wmm, dwOffset);
348 if (mmioSeek(wmm->hFile, dwOffset, SEEK_SET) != dwOffset) {
349 WARN_(mcimidi)("Can't seek at %08lX begin of 'MThd' \n", dwOffset);
350 return MCIERR_INVALID_FILE;
352 if (mmioRead(wmm->hFile, (HPSTR)&fourcc,
353 (long) sizeof(FOURCC)) != (long) sizeof(FOURCC))
354 return MCIERR_INVALID_FILE;
356 if (fourcc != mmioFOURCC('M', 'T', 'h', 'd')) {
357 WARN_(mcimidi)("Can't synchronize on 'MThd' !\n");
358 return MCIERR_INVALID_FILE;
361 if (MIDI_mciReadLong(wmm, &toberead) != 0 || toberead < 3 * sizeof(WORD))
362 return MCIERR_INVALID_FILE;
364 if (MIDI_mciReadWord(wmm, &wmm->wFormat) != 0 ||
365 MIDI_mciReadWord(wmm, &wmm->nTracks) != 0 ||
366 MIDI_mciReadWord(wmm, &wmm->nDivision) != 0) {
367 return MCIERR_INVALID_FILE;
370 TRACE_(mcimidi)("toberead=0x%08lX, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
371 toberead, wmm->wFormat, wmm->nTracks, wmm->nDivision);
373 /* MS doc says that the MIDI MCI time format must be put by default to the format
374 * stored in the MIDI file...
376 if (wmm->nDivision > 0x8000) {
377 /* eric.pouech@lemel.fr 98/11
378 * In did not check this very code (pulses are expressed as SMPTE sub-frames).
379 * In about 40 MB of MIDI files I have, none was SMPTE based...
380 * I'm just wondering if this is widely used :-). So, if someone has one of
381 * these files, I'd like to know about.
383 FIXME_(mcimidi)("Handling SMPTE time in MIDI files has not been tested\n"
384 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
386 switch (HIBYTE(wmm->nDivision)) {
387 case 0xE8: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24; break; /* -24 */
388 case 0xE7: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25; break; /* -25 */
389 case 0xE3: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
390 case 0xE2: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30; break; /* -30 */
392 WARN_(mcimidi)("Unsupported number of frames %d\n", -(char)HIBYTE(wmm->nDivision));
393 return MCIERR_INVALID_FILE;
395 switch (LOBYTE(wmm->nDivision)) {
396 case 4: /* MIDI Time Code */
399 case 80: /* SMPTE bit resolution */
402 WARN_(mcimidi)("Unsupported number of sub-frames %d\n", LOBYTE(wmm->nDivision));
403 return MCIERR_INVALID_FILE;
405 } else if (wmm->nDivision == 0) {
406 WARN_(mcimidi)("Number of division is 0, can't support that !!\n");
407 return MCIERR_INVALID_FILE;
409 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
412 switch (wmm->wFormat) {
414 if (wmm->nTracks != 1) {
415 WARN_(mcimidi)("Got type 0 file whose number of track is not 1. Setting it to 1\n");
423 WARN_(mcimidi)("Handling MIDI files which format = %d is not (yet) supported\n"
424 "Please report with MIDI file !\n", wmm->wFormat);
425 return MCIERR_INVALID_FILE;
428 if (wmm->nTracks & 0x8000) {
429 /* this shouldn't be a problem... */
430 WARN_(mcimidi)("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
431 wmm->nTracks = 0x7FFF;
434 if ((wmm->tracks = malloc(sizeof(MCI_MIDITRACK) * wmm->nTracks)) == NULL) {
435 return MCIERR_OUT_OF_MEMORY;
438 toberead -= 3 * sizeof(WORD);
440 TRACE_(mcimidi)("Size of MThd > 6, skipping %ld extra bytes\n", toberead);
441 mmioSeek(wmm->hFile, toberead, SEEK_CUR);
444 for (nt = 0; nt < wmm->nTracks; nt++) {
445 wmm->tracks[nt].wTrackNr = nt;
446 if (MIDI_mciReadMTrk(wmm, &wmm->tracks[nt]) != 0) {
447 WARN_(mcimidi)("Can't read 'MTrk' header \n");
448 return MCIERR_INVALID_FILE;
452 wmm->dwTempo = 500000;
457 /**************************************************************************
458 * MIDI_ConvertPulseToMS [internal]
460 static DWORD MIDI_ConvertPulseToMS(WINE_MCIMIDI* wmm, DWORD pulse)
464 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
465 * may change during file playing
467 if (wmm->nDivision == 0) {
468 FIXME_(mcimidi)("Shouldn't happen. wmm->nDivision = 0\n");
469 } else if (wmm->nDivision > 0x8000) { /* SMPTE, unchecked FIXME? */
470 int nf = -(char)HIBYTE(wmm->nDivision); /* number of frames */
471 int nsf = LOBYTE(wmm->nDivision); /* number of sub-frames */
472 ret = (pulse * 1000) / (nf * nsf);
474 ret = (DWORD)((double)pulse * ((double)wmm->dwTempo / 1000) /
475 (double)wmm->nDivision);
479 TRACE(mcimidi, "pulse=%lu tempo=%lu division=%u=0x%04x => ms=%lu\n",
480 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
486 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
487 #define TIME_MS_IN_ONE_MINUTE (60*1000)
488 #define TIME_MS_IN_ONE_SECOND (1000)
490 /**************************************************************************
491 * MIDI_ConvertTimeFormatToMS [internal]
493 static DWORD MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI* wmm, DWORD val)
497 switch (wmm->dwMciTimeFormat) {
498 case MCI_FORMAT_MILLISECONDS:
501 case MCI_FORMAT_SMPTE_24:
503 (HIBYTE(HIWORD(val)) * 125) / 3 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
504 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
506 case MCI_FORMAT_SMPTE_25:
508 HIBYTE(HIWORD(val)) * 40 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
509 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
511 case MCI_FORMAT_SMPTE_30:
513 (HIBYTE(HIWORD(val)) * 100) / 3 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
514 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
517 WARN_(mcimidi)("Bad time format %lu!\n", wmm->dwMciTimeFormat);
520 TRACE(mcimidi, "val=%lu=0x%08lx [tf=%lu] => ret=%lu\n", val, val, wmm->dwMciTimeFormat, ret);
525 /**************************************************************************
526 * MIDI_ConvertMSToTimeFormat [internal]
528 static DWORD MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI* wmm, DWORD _val)
530 DWORD ret = 0, val = _val;
533 switch (wmm->dwMciTimeFormat) {
534 case MCI_FORMAT_MILLISECONDS:
537 case MCI_FORMAT_SMPTE_24:
538 case MCI_FORMAT_SMPTE_25:
539 case MCI_FORMAT_SMPTE_30:
540 h = val / TIME_MS_IN_ONE_HOUR;
541 m = (val -= h * TIME_MS_IN_ONE_HOUR) / TIME_MS_IN_ONE_MINUTE;
542 s = (val -= m * TIME_MS_IN_ONE_MINUTE) / TIME_MS_IN_ONE_SECOND;
543 switch (wmm->dwMciTimeFormat) {
544 case MCI_FORMAT_SMPTE_24:
545 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
546 f = (val * 3) / 125; val -= (f * 125) / 3;
548 case MCI_FORMAT_SMPTE_25:
549 /* one frame is 1000/25 ms long, 1000/25 == 40 */
550 f = val / 40; val -= f * 40;
552 case MCI_FORMAT_SMPTE_30:
553 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
554 f = (val * 3) / 100; val -= (f * 100) / 3;
557 FIXME_(mcimidi)("There must be some bad bad programmer\n");
560 /* val contains the number of ms which cannot make a complete frame */
561 /* FIXME: is this correct ? programs seem to be happy with that */
562 ret = (f << 24) | (s << 16) | (m << 8) | (h << 0);
565 WARN_(mcimidi)("Bad time format %lu!\n", wmm->dwMciTimeFormat);
568 TRACE(mcimidi, "val=%lu [tf=%lu] => ret=%lu=0x%08lx\n", _val, wmm->dwMciTimeFormat, ret, ret);
573 /**************************************************************************
574 * MIDI_GetMThdLengthMS [internal]
576 static DWORD MIDI_GetMThdLengthMS(WINE_MCIMIDI* wmm)
581 for (nt = 0; nt < wmm->nTracks; nt++) {
582 if (wmm->wFormat == 2) {
583 ret += wmm->tracks[nt].dwLength;
584 } else if (wmm->tracks[nt].dwLength > ret) {
585 ret = wmm->tracks[nt].dwLength;
588 /* FIXME: this is wrong if there is a tempo change inside the file */
589 return MIDI_ConvertPulseToMS(wmm, ret);
592 /**************************************************************************
593 * MIDI_mciOpen [internal]
595 static DWORD MIDI_mciOpen(UINT16 wDevID, DWORD dwFlags, LPMCI_OPEN_PARMSA lpParms)
597 MIDIOPENDESC midiOpenDesc;
600 WINE_MCIMIDI* wmm = MIDI_drvGetDrv(wDevID);
602 TRACE_(mcimidi)("(%04x, %08lX, %p)\n", wDevID, dwFlags, lpParms);
604 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
605 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
606 if (dwFlags & MCI_OPEN_SHAREABLE)
607 return MCIERR_HARDWARE;
609 if (wmm->nUseCount > 0) {
610 /* The driver is already opened on this channel
611 * MIDI sequencer cannot be shared
613 return MCIERR_DEVICE_OPEN;
619 dwDeviceID = lpParms->wDeviceID;
621 TRACE_(mcimidi)("wDevID=%04X (lpParams->wDeviceID=%08lX)\n", wDevID, dwDeviceID);
622 /* lpParms->wDeviceID = wDevID;*/
624 if (dwFlags & MCI_OPEN_ELEMENT) {
625 LPSTR lpstrElementName;
627 lpstrElementName = lpParms->lpstrElementName;
629 TRACE_(mcimidi)("MCI_OPEN_ELEMENT '%s' !\n", lpstrElementName);
630 if (lpstrElementName && strlen(lpstrElementName) > 0) {
631 wmm->hFile = mmioOpenA(lpstrElementName, NULL,
632 MMIO_ALLOCBUF | MMIO_READWRITE | MMIO_EXCLUSIVE);
633 if (wmm->hFile == 0) {
634 WARN_(mcimidi)("Can't find file='%s' !\n", lpstrElementName);
636 return MCIERR_FILE_NOT_FOUND;
642 TRACE_(mcimidi)("hFile=%u\n", wmm->hFile);
644 memcpy(&wmm->openParms, lpParms, sizeof(MCI_OPEN_PARMSA));
646 wmm->wNotifyDeviceID = dwDeviceID;
647 wmm->dwStatus = MCI_MODE_NOT_READY; /* while loading file contents */
648 /* spec says it should be the default format from the MIDI file... */
649 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
651 midiOpenDesc.hMidi = 0;
653 if (wmm->hFile != 0) {
658 if (mmioDescend(wmm->hFile, &ckMainRIFF, NULL, 0) != 0) {
659 dwRet = MCIERR_INVALID_FILE;
661 TRACE_(mcimidi)("ParentChunk ckid=%.4s fccType=%.4s cksize=%08lX \n",
662 (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, ckMainRIFF.cksize);
664 if (ckMainRIFF.ckid == FOURCC_RIFF && ckMainRIFF.fccType == mmioFOURCC('R', 'M', 'I', 'D')) {
665 mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
666 if (mmioDescend(wmm->hFile, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK) == 0) {
667 TRACE_(mcimidi)("... is a 'RMID' file \n");
668 dwOffset = mmckInfo.dwDataOffset;
670 dwRet = MCIERR_INVALID_FILE;
673 if (dwRet == 0 && MIDI_mciReadMThd(wmm, dwOffset) != 0) {
674 WARN_(mcimidi)("Can't read 'MThd' header \n");
675 dwRet = MCIERR_INVALID_FILE;
679 TRACE_(mcimidi)("hFile==0, setting #tracks to 0; is this correct ?\n");
687 mmioClose(wmm->hFile, 0);
690 wmm->dwPositionMS = 0;
691 wmm->dwStatus = MCI_MODE_STOP;
692 wmm->hMidiHdr = USER_HEAP_ALLOC(sizeof(MIDIHDR16));
694 dwRet = modMessage(wmm->wMidiID, MODM_OPEN, 0, (DWORD)&midiOpenDesc, CALLBACK_NULL);
695 /* dwRet = midMessage(wmm->wMidiID, MIDM_OPEN, 0, (DWORD)&midiOpenDesc, CALLBACK_NULL);*/
700 /**************************************************************************
701 * MIDI_mciStop [internal]
703 static DWORD MIDI_mciStop(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
705 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
707 TRACE_(mcimidi)("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
709 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
711 wmm->dwStatus = MCI_MODE_STOP;
712 TRACE_(mcimidi)("wmm->dwStatus=%d\n", wmm->dwStatus);
714 if (lpParms && (dwFlags & MCI_NOTIFY)) {
715 TRACE_(mcimidi)("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
716 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
717 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
722 /**************************************************************************
723 * MIDI_mciClose [internal]
725 static DWORD MIDI_mciClose(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
728 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
730 TRACE_(mcimidi)("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
732 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
734 if (wmm->dwStatus != MCI_MODE_STOP) {
735 MIDI_mciStop(wDevID, MCI_WAIT, lpParms);
738 wmm->dwStatus = MCI_MODE_STOP;
740 if (wmm->nUseCount == 0) {
741 if (wmm->hFile != 0) {
742 mmioClose(wmm->hFile, 0);
744 TRACE_(mcimidi)("hFile closed !\n");
746 USER_HEAP_FREE(wmm->hMidiHdr);
748 dwRet = modMessage(wmm->wMidiID, MODM_CLOSE, 0, 0L, 0L);
749 if (dwRet != MMSYSERR_NOERROR) return MCIERR_INTERNAL;
751 dwRet = midMessage(wmm->wMidiID, MIDM_CLOSE, 0, 0L, 0L);
752 if (dwRet != MMSYSERR_NOERROR) return MCIERR_INTERNAL;
755 TRACE_(mcimidi)("Shouldn't happen... nUseCount=%d\n", wmm->nUseCount);
756 return MCIERR_INTERNAL;
759 if (lpParms && (dwFlags & MCI_NOTIFY)) {
760 TRACE_(mcimidi)("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
761 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
762 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
767 /**************************************************************************
768 * MIDI_mciFindNextEvent [internal]
770 static MCI_MIDITRACK* MIDI_mciFindNextEvent(WINE_MCIMIDI* wmm, LPDWORD hiPulse)
775 *hiPulse = 0xFFFFFFFFul;
777 for (nt = 0; nt < wmm->nTracks; nt++) {
778 mmt = &wmm->tracks[nt];
780 if (mmt->wStatus == 0)
782 if (mmt->dwEventPulse < *hiPulse) {
783 *hiPulse = mmt->dwEventPulse;
787 return (cnt == 0xFFFFu) ? 0 /* no more event on all tracks */
791 /**************************************************************************
792 * MIDI_mciPlay [internal]
794 static DWORD MIDI_mciPlay(UINT16 wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
796 DWORD dwStartMS, dwEndMS, dwRet;
800 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
802 TRACE_(mcimidi)("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
804 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
806 if (wmm->hFile == 0) {
807 WARN_(mcimidi)("Can't play: no file '%s' !\n", wmm->openParms.lpstrElementName);
808 return MCIERR_FILE_NOT_FOUND;
811 if (wmm->dwStatus != MCI_MODE_STOP) {
812 WARN_(mcimidi)("Can't play: device is not stopped !\n");
813 return MCIERR_INTERNAL;
816 if (!(dwFlags & MCI_WAIT)) {
817 /** FIXME: I'm not 100% sure that wNotifyDeviceID is the right value in all cases ??? */
818 return MCI_SendCommandAsync(wmm->wNotifyDeviceID, MCI_PLAY, dwFlags, (DWORD)lpParms, sizeof(LPMCI_PLAY_PARMS));
821 if (lpParms && (dwFlags & MCI_FROM)) {
822 dwStartMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwFrom);
824 dwStartMS = wmm->dwPositionMS;
827 if (lpParms && (dwFlags & MCI_TO)) {
828 dwEndMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
830 dwEndMS = 0xFFFFFFFFul;
833 TRACE_(mcimidi)("Playing from %lu to %lu\n", dwStartMS, dwEndMS);
836 for (nt = 0; nt < wmm->nTracks; nt++) {
837 mmt = &wmm->tracks[nt];
839 mmt->wStatus = 1; /* ok, playing */
840 mmt->dwIndex = mmt->dwFirst;
841 if (wmm->wFormat == 2 && nt > 0) {
842 mmt->dwEventPulse = wmm->tracks[nt - 1].dwLength;
844 mmt->dwEventPulse = 0;
846 MIDI_mciReadNextEvent(wmm, mmt); /* FIXME == 0 */
850 wmm->dwTempo = 500000;
851 wmm->dwStatus = MCI_MODE_PLAY;
852 wmm->dwPositionMS = 0;
853 wmm->wStartedPlaying = FALSE;
855 while (wmm->dwStatus != MCI_MODE_STOP) {
856 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
857 * too much. Tell gcc not to optimize status value using volatile.
859 while (((volatile WINE_MCIMIDI*)wmm)->dwStatus == MCI_MODE_PAUSE);
861 doPlay = (wmm->dwPositionMS >= dwStartMS && wmm->dwPositionMS <= dwEndMS);
863 TRACE_(mcimidi)("wmm->dwStatus=%d, doPlay=%s\n", wmm->dwStatus, doPlay ? "T" : "F");
865 if ((mmt = MIDI_mciFindNextEvent(wmm, &hiPulse)) == NULL)
866 break; /* no more event on tracks */
868 /* if starting playing, then set StartTicks to the value it would have had
869 * if play had started at position 0
871 if (doPlay && !wmm->wStartedPlaying) {
872 wmm->dwStartTicks = GetTickCount() - MIDI_ConvertPulseToMS(wmm, wmm->dwPulse);
873 wmm->wStartedPlaying = TRUE;
874 TRACE_(mcimidi)("Setting dwStartTicks to %lu\n", wmm->dwStartTicks);
877 if (hiPulse > wmm->dwPulse) {
878 wmm->dwPositionMS += MIDI_ConvertPulseToMS(wmm, hiPulse - wmm->dwPulse);
880 DWORD togo = wmm->dwStartTicks + wmm->dwPositionMS;
881 DWORD tc = GetTickCount();
883 TRACE_(mcimidi)("Pulses hi=0x%08lx <> cur=0x%08lx\n", hiPulse, wmm->dwPulse);
884 TRACE_(mcimidi)("Wait until %lu => %lu ms\n",
885 tc - wmm->dwStartTicks, togo - wmm->dwStartTicks);
889 wmm->dwPulse = hiPulse;
892 switch (LOBYTE(LOWORD(mmt->dwEventData))) {
894 case 0xF7: /* sysex events */
896 FIXME_(mcimidi)("Not handling SysEx events (yet)\n");
900 /* position after meta data header */
901 mmioSeek(wmm->hFile, mmt->dwIndex + HIWORD(mmt->dwEventData), SEEK_SET);
902 switch (HIBYTE(LOWORD(mmt->dwEventData))) {
903 case 0x00: /* 16-bit sequence number */
904 if (TRACE_ON(midi)) {
907 MIDI_mciReadWord(wmm, &twd); /* == 0 */
908 TRACE_(mcimidi)("Got sequence number %u\n", twd);
911 case 0x01: /* any text */
912 case 0x02: /* Copyright Message text */
913 case 0x03: /* Sequence/Track Name text */
914 case 0x04: /* Instrument Name text */
915 case 0x05: /* Lyric text */
916 case 0x06: /* Marker text */
917 case 0x07: /* Cue-point text */
918 if (TRACE_ON(mcimidi)) {
920 WORD len = mmt->wEventLength - HIWORD(mmt->dwEventData);
921 static char* info[8] = {"", "Text", "Copyright", "Seq/Trk name",
922 "Instrument", "Lyric", "Marker", "Cue-point"};
923 WORD idx = HIBYTE(LOWORD(mmt->dwEventData));
925 if (len >= sizeof(buf)) {
926 WARN_(mcimidi)("Buffer for text is too small (%d bytes, when %u are needed)\n", sizeof(buf) - 1, len);
927 len = sizeof(buf) - 1;
929 if (mmioRead(wmm->hFile, (HPSTR)buf, len) == len) {
930 buf[len] = 0; /* end string in case */
931 TRACE_(mcimidi)("%s => \"%s\"\n", (idx < 8 ) ? info[idx] : "", buf);
933 WARN_(mcimidi)("Couldn't read data for %s\n", (idx < 8 ) ? info[idx] : "");
938 /* MIDI channel (cc) */
939 if (FIXME_ON(mcimidi)) {
942 MIDI_mciReadByte(wmm, &bt); /* == 0 */
943 FIXME_(mcimidi)("NIY: MIDI channel=%u, track=%u\n", bt, mmt->wTrackNr);
948 if (FIXME_ON(mcimidi)) {
951 MIDI_mciReadByte(wmm, &bt); /* == 0 */
952 FIXME_(mcimidi)("NIY: MIDI port=%u, track=%u\n", bt, mmt->wTrackNr);
955 case 0x2F: /* end of track */
958 case 0x51:/* set tempo */
959 /* Tempo is expressed in µ-seconds per midi quarter note
960 * for format 1 MIDI files, this can only be present on track #0
962 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
963 WARN_(mcimidi)("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt->wTrackNr);
968 MIDI_mciReadByte(wmm, &tbt); value = ((DWORD)tbt) << 16;
969 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 8;
970 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 0;
971 TRACE_(mcimidi)("Setting tempo to %ld (BPM=%ld)\n", wmm->dwTempo, (value) ? (60000000l / value) : 0);
972 wmm->dwTempo = value;
975 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
976 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
977 WARN_(mcimidi)("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt->wTrackNr);
978 } if (mmt->dwEventPulse != 0) {
979 WARN_(mcimidi)("SMPTE track start can only be expressed at start of track (%lu)\n", mmt->dwEventPulse);
983 MIDI_mciReadByte(wmm, &h);
984 MIDI_mciReadByte(wmm, &m);
985 MIDI_mciReadByte(wmm, &s);
986 MIDI_mciReadByte(wmm, &f);
987 MIDI_mciReadByte(wmm, &ff);
988 FIXME_(mcimidi)("NIY: SMPTE track start %u:%u:%u %u.%u\n", h, m, s, f, ff);
991 case 0x58: /* file rythm */
992 if (TRACE_ON(mcimidi)) {
993 BYTE num, den, cpmc, _32npqn;
995 MIDI_mciReadByte(wmm, &num);
996 MIDI_mciReadByte(wmm, &den); /* to notate e.g. 6/8 */
997 MIDI_mciReadByte(wmm, &cpmc); /* number of MIDI clocks per metronome click */
998 MIDI_mciReadByte(wmm, &_32npqn); /* number of notated 32nd notes per MIDI quarter note */
1000 TRACE_(mcimidi)("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num, 1 << den, cpmc, _32npqn);
1003 case 0x59: /* key signature */
1004 if (TRACE_ON(mcimidi)) {
1007 MIDI_mciReadByte(wmm, &sf);
1008 MIDI_mciReadByte(wmm, &mm);
1010 if (sf >= 0x80) TRACE_(mcimidi)("%d flats\n", -(char)sf);
1011 else if (sf > 0) TRACE_(mcimidi)("%d sharps\n", (char)sf);
1012 else TRACE_(mcimidi)("Key of C\n");
1013 TRACE_(mcimidi)("Mode: %s\n", (mm = 0) ? "major" : "minor");
1017 WARN_(mcimidi)("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt->dwEventData)));
1023 dwRet = modMessage(wmm->wMidiID, MODM_DATA, 0, mmt->dwEventData, 0);
1025 switch (LOBYTE(LOWORD(mmt->dwEventData)) & 0xF0) {
1031 dwRet = modMessage(wmm->wMidiID, MODM_DATA, 0, mmt->dwEventData, 0);
1035 mmt->dwIndex += mmt->wEventLength;
1036 if (mmt->dwIndex < mmt->dwFirst || mmt->dwIndex >= mmt->dwLast) {
1040 MIDI_mciReadNextEvent(wmm, mmt);
1044 /* stop all notes */
1045 /* should be (0x7800 | MIDI_CTL_CHANGE) instead of 0x78B0,
1046 * but MIDI_CTL_CHANGE is defined in OSS's soundcard.h and MCI must compile
1047 * without any reference to OSS
1049 /* FIXME: check if 0x78B0 is channel dependant or not. I coded it so that
1050 * it's channel dependent...
1054 for (chn = 0; chn < 16; chn++)
1055 modMessage(wmm->wMidiID, MODM_DATA, 0, 0x78B0 | chn, 0);
1058 wmm->dwStatus = MCI_MODE_STOP;
1060 /* to restart playing at beginning when it's over */
1061 wmm->dwPositionMS = 0;
1063 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1064 TRACE_(mcimidi)("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1065 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
1066 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1071 /**************************************************************************
1072 * MIDI_mciRecord [internal]
1074 static DWORD MIDI_mciRecord(UINT16 wDevID, DWORD dwFlags, LPMCI_RECORD_PARMS lpParms)
1077 LPMIDIHDR16 lpMidiHdr;
1079 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1081 TRACE_(mcimidi)("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1083 if (wmm == 0) return MCIERR_INVALID_DEVICE_ID;
1085 if (wmm->hFile == 0) {
1086 WARN_(mcimidi)("Can't find file='%08lx' !\n",
1087 (DWORD)wmm->openParms.lpstrElementName);
1088 return MCIERR_FILE_NOT_FOUND;
1090 start = 1; end = 99999;
1091 if (lpParms && (dwFlags & MCI_FROM)) {
1092 start = lpParms->dwFrom;
1093 TRACE_(mcimidi)("MCI_FROM=%d \n", start);
1095 if (lpParms && (dwFlags & MCI_TO)) {
1096 end = lpParms->dwTo;
1097 TRACE_(mcimidi)("MCI_TO=%d \n", end);
1099 lpMidiHdr = USER_HEAP_LIN_ADDR(wmm->hMidiHdr);
1100 lpMidiHdr->lpData = (LPSTR) malloc(1200);
1102 return MCIERR_OUT_OF_MEMORY;
1103 lpMidiHdr->dwBufferLength = 1024;
1104 lpMidiHdr->dwUser = 0L;
1105 lpMidiHdr->dwFlags = 0L;
1106 dwRet = midMessage(wmm->wMidiID, MIDM_PREPARE, 0, (DWORD)lpMidiHdr, sizeof(MIDIHDR16));
1107 TRACE_(mcimidi)("After MIDM_PREPARE \n");
1108 wmm->dwStatus = MCI_MODE_RECORD;
1109 while (wmm->dwStatus != MCI_MODE_STOP) {
1110 TRACE_(mcimidi)("wmm->dwStatus=%p %d\n",
1111 &wmm->dwStatus, wmm->dwStatus);
1112 lpMidiHdr->dwBytesRecorded = 0;
1113 dwRet = midMessage(wmm->wMidiID, MIDM_START, 0, 0L, 0L);
1114 TRACE_(mcimidi)("After MIDM_START lpMidiHdr=%p dwBytesRecorded=%lu\n",
1115 lpMidiHdr, lpMidiHdr->dwBytesRecorded);
1116 if (lpMidiHdr->dwBytesRecorded == 0) break;
1118 TRACE_(mcimidi)("Before MIDM_UNPREPARE \n");
1119 dwRet = midMessage(wmm->wMidiID, MIDM_UNPREPARE, 0, (DWORD)lpMidiHdr, sizeof(MIDIHDR16));
1120 TRACE_(mcimidi)("After MIDM_UNPREPARE \n");
1121 if (lpMidiHdr->lpData != NULL) {
1122 free(lpMidiHdr->lpData);
1123 lpMidiHdr->lpData = NULL;
1125 wmm->dwStatus = MCI_MODE_STOP;
1126 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1127 TRACE_(mcimidi)("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1128 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
1129 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1134 /**************************************************************************
1135 * MIDI_mciPause [internal]
1137 static DWORD MIDI_mciPause(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1139 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1141 TRACE_(mcimidi)("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1143 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1145 if (wmm->dwStatus == MCI_MODE_PLAY) {
1146 /* stop all notes */
1147 /* see note in MIDI_mciPlay */
1149 for (chn = 0; chn < 16; chn++)
1150 modMessage(wmm->wMidiID, MODM_DATA, 0, 0x78B0 | chn, 0);
1151 wmm->dwStatus = MCI_MODE_PAUSE;
1153 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1154 TRACE_(mcimidi)("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1155 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
1156 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1162 /**************************************************************************
1163 * MIDI_mciResume [internal]
1165 static DWORD MIDI_mciResume(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1167 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1169 TRACE_(mcimidi)("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1171 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1173 if (wmm->dwStatus == MCI_MODE_PAUSE) {
1174 wmm->wStartedPlaying = FALSE;
1175 wmm->dwStatus = MCI_MODE_PLAY;
1177 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1178 TRACE_(mcimidi)("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1179 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
1180 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1185 /**************************************************************************
1186 * MIDI_mciSet [internal]
1188 static DWORD MIDI_mciSet(UINT16 wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
1190 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1192 TRACE_(mcimidi)("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1194 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1195 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1197 if (dwFlags & MCI_SET_TIME_FORMAT) {
1198 switch (lpParms->dwTimeFormat) {
1199 case MCI_FORMAT_MILLISECONDS:
1200 TRACE_(mcimidi)("MCI_FORMAT_MILLISECONDS !\n");
1201 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
1203 case MCI_FORMAT_SMPTE_24:
1204 TRACE_(mcimidi)("MCI_FORMAT_SMPTE_24 !\n");
1205 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24;
1207 case MCI_FORMAT_SMPTE_25:
1208 TRACE_(mcimidi)("MCI_FORMAT_SMPTE_25 !\n");
1209 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25;
1211 case MCI_FORMAT_SMPTE_30:
1212 TRACE_(mcimidi)("MCI_FORMAT_SMPTE_30 !\n");
1213 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30;
1216 WARN_(mcimidi)("Bad time format %lu!\n", lpParms->dwTimeFormat);
1217 return MCIERR_BAD_TIME_FORMAT;
1220 if (dwFlags & MCI_SET_VIDEO) {
1221 TRACE_(mcimidi)("No support for video !\n");
1222 return MCIERR_UNSUPPORTED_FUNCTION;
1224 if (dwFlags & MCI_SET_DOOR_OPEN) {
1225 TRACE_(mcimidi)("No support for door open !\n");
1226 return MCIERR_UNSUPPORTED_FUNCTION;
1228 if (dwFlags & MCI_SET_DOOR_CLOSED) {
1229 TRACE_(mcimidi)("No support for door close !\n");
1230 return MCIERR_UNSUPPORTED_FUNCTION;
1232 if (dwFlags & MCI_SET_AUDIO) {
1233 if (dwFlags & MCI_SET_ON) {
1234 TRACE_(mcimidi)("MCI_SET_ON audio !\n");
1235 } else if (dwFlags & MCI_SET_OFF) {
1236 TRACE_(mcimidi)("MCI_SET_OFF audio !\n");
1238 WARN_(mcimidi)("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1239 return MCIERR_BAD_INTEGER;
1242 if (lpParms->dwAudio & MCI_SET_AUDIO_ALL)
1243 TRACE_(mcimidi)("MCI_SET_AUDIO_ALL !\n");
1244 if (lpParms->dwAudio & MCI_SET_AUDIO_LEFT)
1245 TRACE_(mcimidi)("MCI_SET_AUDIO_LEFT !\n");
1246 if (lpParms->dwAudio & MCI_SET_AUDIO_RIGHT)
1247 TRACE_(mcimidi)("MCI_SET_AUDIO_RIGHT !\n");
1250 if (dwFlags & MCI_SEQ_SET_MASTER)
1251 TRACE_(mcimidi)("MCI_SEQ_SET_MASTER !\n");
1252 if (dwFlags & MCI_SEQ_SET_SLAVE)
1253 TRACE_(mcimidi)("MCI_SEQ_SET_SLAVE !\n");
1254 if (dwFlags & MCI_SEQ_SET_OFFSET)
1255 TRACE_(mcimidi)("MCI_SEQ_SET_OFFSET !\n");
1256 if (dwFlags & MCI_SEQ_SET_PORT)
1257 TRACE_(mcimidi)("MCI_SEQ_SET_PORT !\n");
1258 if (dwFlags & MCI_SEQ_SET_TEMPO)
1259 TRACE_(mcimidi)("MCI_SEQ_SET_TEMPO !\n");
1263 /**************************************************************************
1264 * MIDI_mciStatus [internal]
1266 static DWORD MIDI_mciStatus(UINT16 wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
1268 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1270 TRACE_(mcimidi)("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1272 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1273 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1275 if (dwFlags & MCI_STATUS_ITEM) {
1276 switch (lpParms->dwItem) {
1277 case MCI_STATUS_CURRENT_TRACK:
1278 /* FIXME in Format 2 */
1279 lpParms->dwReturn = 1;
1280 TRACE_(mcimidi)("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms->dwReturn);
1282 case MCI_STATUS_LENGTH:
1283 if ((dwFlags & MCI_TRACK) && wmm->wFormat == 2) {
1284 if (lpParms->dwTrack >= wmm->nTracks)
1285 return MCIERR_BAD_INTEGER;
1286 /* FIXME: this is wrong if there is a tempo change inside the file */
1287 lpParms->dwReturn = MIDI_ConvertPulseToMS(wmm, wmm->tracks[lpParms->dwTrack].dwLength);
1289 lpParms->dwReturn = MIDI_GetMThdLengthMS(wmm);
1291 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm, lpParms->dwReturn);
1292 TRACE_(mcimidi)("MCI_STATUS_LENGTH => %lu\n", lpParms->dwReturn);
1294 case MCI_STATUS_MODE:
1295 lpParms->dwReturn = wmm->dwStatus;
1296 TRACE_(mcimidi)("MCI_STATUS_MODE => %lu\n", lpParms->dwReturn);
1298 case MCI_STATUS_MEDIA_PRESENT:
1299 TRACE_(mcimidi)("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1300 lpParms->dwReturn = TRUE;
1302 case MCI_STATUS_NUMBER_OF_TRACKS:
1303 lpParms->dwReturn = (wmm->wFormat == 2) ? wmm->nTracks : 1;
1304 TRACE_(mcimidi)("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms->dwReturn);
1306 case MCI_STATUS_POSITION:
1307 /* FIXME: do I need to use MCI_TRACK ? */
1308 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm,
1309 (dwFlags & MCI_STATUS_START) ? 0 : wmm->dwPositionMS);
1310 TRACE_(mcimidi)("MCI_STATUS_POSITION %s => %lu\n",
1311 (dwFlags & MCI_STATUS_START) ? "start" : "current", lpParms->dwReturn);
1313 case MCI_STATUS_READY:
1314 lpParms->dwReturn = (wmm->dwStatus != MCI_MODE_NOT_READY);
1315 TRACE_(mcimidi)("MCI_STATUS_READY = %lu\n", lpParms->dwReturn);
1317 case MCI_STATUS_TIME_FORMAT:
1318 lpParms->dwReturn = wmm->dwMciTimeFormat;
1319 TRACE_(mcimidi)("MCI_STATUS_TIME_FORMAT => %lu\n", lpParms->dwReturn);
1321 case MCI_SEQ_STATUS_DIVTYPE:
1322 TRACE_(mcimidi)("MCI_SEQ_STATUS_DIVTYPE !\n");
1323 if (wmm->nDivision > 0x8000) {
1324 switch (wmm->nDivision) {
1325 case 0xE8: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_24; break; /* -24 */
1326 case 0xE7: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_25; break; /* -25 */
1327 case 0xE3: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
1328 case 0xE2: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30; break; /* -30 */
1329 default: FIXME_(mcimidi)("There is a bad bad programmer\n");
1332 lpParms->dwReturn = MCI_SEQ_DIV_PPQN;
1335 case MCI_SEQ_STATUS_MASTER:
1336 TRACE_(mcimidi)("MCI_SEQ_STATUS_MASTER !\n");
1337 lpParms->dwReturn = 0;
1339 case MCI_SEQ_STATUS_SLAVE:
1340 TRACE_(mcimidi)("MCI_SEQ_STATUS_SLAVE !\n");
1341 lpParms->dwReturn = 0;
1343 case MCI_SEQ_STATUS_OFFSET:
1344 TRACE_(mcimidi)("MCI_SEQ_STATUS_OFFSET !\n");
1345 lpParms->dwReturn = 0;
1347 case MCI_SEQ_STATUS_PORT:
1348 TRACE_(mcimidi)("MCI_SEQ_STATUS_PORT !\n");
1349 lpParms->dwReturn = 0;
1351 case MCI_SEQ_STATUS_TEMPO:
1352 TRACE_(mcimidi)("MCI_SEQ_STATUS_TEMPO !\n");
1353 lpParms->dwReturn = wmm->dwTempo;
1356 WARN_(mcimidi)("Unknowm command %08lX !\n", lpParms->dwItem);
1357 return MCIERR_UNRECOGNIZED_COMMAND;
1360 WARN_(mcimidi)("No Status-Item!\n");
1361 return MCIERR_UNRECOGNIZED_COMMAND;
1363 if (dwFlags & MCI_NOTIFY) {
1364 TRACE_(mcimidi)("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1365 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
1366 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1371 /**************************************************************************
1372 * MIDI_mciGetDevCaps [internal]
1374 static DWORD MIDI_mciGetDevCaps(UINT16 wDevID, DWORD dwFlags,
1375 LPMCI_GETDEVCAPS_PARMS lpParms)
1377 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1379 TRACE_(mcimidi)("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1381 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1382 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1384 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
1385 switch (lpParms->dwItem) {
1386 case MCI_GETDEVCAPS_DEVICE_TYPE:
1387 TRACE_(mcimidi)("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1388 lpParms->dwReturn = MCI_DEVTYPE_SEQUENCER;
1390 case MCI_GETDEVCAPS_HAS_AUDIO:
1391 TRACE_(mcimidi)("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1392 lpParms->dwReturn = TRUE;
1394 case MCI_GETDEVCAPS_HAS_VIDEO:
1395 TRACE_(mcimidi)("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1396 lpParms->dwReturn = FALSE;
1398 case MCI_GETDEVCAPS_USES_FILES:
1399 TRACE_(mcimidi)("MCI_GETDEVCAPS_USES_FILES !\n");
1400 lpParms->dwReturn = TRUE;
1402 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
1403 TRACE_(mcimidi)("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1404 lpParms->dwReturn = TRUE;
1406 case MCI_GETDEVCAPS_CAN_EJECT:
1407 TRACE_(mcimidi)("MCI_GETDEVCAPS_CAN_EJECT !\n");
1408 lpParms->dwReturn = FALSE;
1410 case MCI_GETDEVCAPS_CAN_PLAY:
1411 TRACE_(mcimidi)("MCI_GETDEVCAPS_CAN_PLAY !\n");
1412 lpParms->dwReturn = TRUE;
1414 case MCI_GETDEVCAPS_CAN_RECORD:
1415 TRACE_(mcimidi)("MCI_GETDEVCAPS_CAN_RECORD !\n");
1416 lpParms->dwReturn = TRUE;
1418 case MCI_GETDEVCAPS_CAN_SAVE:
1419 TRACE_(mcimidi)("MCI_GETDEVCAPS_CAN_SAVE !\n");
1420 lpParms->dwReturn = FALSE;
1423 TRACE_(mcimidi)("Unknown capability (%08lx) !\n", lpParms->dwItem);
1424 return MCIERR_UNRECOGNIZED_COMMAND;
1427 TRACE_(mcimidi)("No GetDevCaps-Item !\n");
1428 return MCIERR_UNRECOGNIZED_COMMAND;
1433 /**************************************************************************
1434 * MIDI_mciInfo [internal]
1436 static DWORD MIDI_mciInfo(UINT16 wDevID, DWORD dwFlags, LPMCI_INFO_PARMSA lpParms)
1440 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1442 TRACE_(mcimidi)("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1444 if (lpParms == NULL || lpParms->lpstrReturn == NULL) {
1445 ret = MCIERR_NULL_PARAMETER_BLOCK;
1446 } else if (wmm == NULL) {
1447 ret = MCIERR_INVALID_DEVICE_ID;
1449 TRACE_(mcimidi)("buf=%p, len=%lu\n", lpParms->lpstrReturn, lpParms->dwRetSize);
1452 case MCI_INFO_PRODUCT:
1453 str = "Wine's MIDI sequencer";
1456 str = wmm->openParms.lpstrElementName;
1459 /* FIXME: the following manifest constants are not defined in <WINE>/include/mmsystem.h */
1460 case MCI_INFO_COPYRIGHT:
1466 WARN_(mcimidi)("Don't know this info command (%lu)\n", dwFlags);
1467 ret = MCIERR_UNRECOGNIZED_COMMAND;
1471 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, str);
1473 lpParms->lpstrReturn[0] = 0;
1479 /**************************************************************************
1480 * MIDI_mciSeek [internal]
1482 static DWORD MIDI_mciSeek(UINT16 wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1485 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1487 TRACE_(mcimidi)("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1489 if (lpParms == NULL) {
1490 ret = MCIERR_NULL_PARAMETER_BLOCK;
1491 } else if (wmm == NULL) {
1492 ret = MCIERR_INVALID_DEVICE_ID;
1494 MIDI_mciStop(wDevID, MCI_WAIT, 0);
1496 if (dwFlags & MCI_SEEK_TO_START) {
1497 wmm->dwPositionMS = 0;
1498 } else if (dwFlags & MCI_SEEK_TO_END) {
1499 wmm->dwPositionMS = 0xFFFFFFFF; /* fixme */
1500 } else if (dwFlags & MCI_TO) {
1501 wmm->dwPositionMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
1503 WARN_(mcimidi)("dwFlag doesn't tell where to seek to...\n");
1504 return MCIERR_MISSING_PARAMETER;
1507 TRACE_(mcimidi)("Seeking to position=%lu ms\n", wmm->dwPositionMS);
1509 if (dwFlags & MCI_NOTIFY) {
1510 TRACE_(mcimidi)("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1511 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
1512 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1519 /*======================================================================*
1520 * MIDI entry points *
1521 *======================================================================*/
1523 /**************************************************************************
1524 * MCIMIDI_DriverProc [sample driver]
1526 LONG MCIMIDI_DriverProc(DWORD dwDevID, HDRVR hDriv, DWORD wMsg,
1527 DWORD dwParam1, DWORD dwParam2)
1530 case DRV_LOAD: return 1;
1531 case DRV_FREE: return 1;
1532 case DRV_OPEN: return MIDI_drvOpen((LPSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSA)dwParam2);
1533 case DRV_CLOSE: return MIDI_drvClose(dwDevID);
1534 case DRV_ENABLE: return 1;
1535 case DRV_DISABLE: return 1;
1536 case DRV_QUERYCONFIGURE: return 1;
1537 case DRV_CONFIGURE: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK); return 1;
1538 case DRV_INSTALL: return DRVCNF_RESTART;
1539 case DRV_REMOVE: return DRVCNF_RESTART;
1540 #ifdef SNDCTL_MIDI_INFO
1541 case MCI_OPEN_DRIVER: return MIDI_mciOpen (dwDevID, dwParam1, (LPMCI_OPEN_PARMSA) dwParam2);
1542 case MCI_CLOSE_DRIVER: return MIDI_mciClose (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1543 case MCI_PLAY: return MIDI_mciPlay (dwDevID, dwParam1, (LPMCI_PLAY_PARMS) dwParam2);
1544 case MCI_RECORD: return MIDI_mciRecord (dwDevID, dwParam1, (LPMCI_RECORD_PARMS) dwParam2);
1545 case MCI_STOP: return MIDI_mciStop (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1546 case MCI_SET: return MIDI_mciSet (dwDevID, dwParam1, (LPMCI_SET_PARMS) dwParam2);
1547 case MCI_PAUSE: return MIDI_mciPause (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1548 case MCI_RESUME: return MIDI_mciResume (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1549 case MCI_STATUS: return MIDI_mciStatus (dwDevID, dwParam1, (LPMCI_STATUS_PARMS) dwParam2);
1550 case MCI_GETDEVCAPS: return MIDI_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1551 case MCI_INFO: return MIDI_mciInfo (dwDevID, dwParam1, (LPMCI_INFO_PARMSA) dwParam2);
1552 case MCI_SEEK: return MIDI_mciSeek (dwDevID, dwParam1, (LPMCI_SEEK_PARMS) dwParam2);
1554 case MCI_OPEN_DRIVER:
1555 case MCI_CLOSE_DRIVER:
1563 case MCI_GETDEVCAPS:
1583 WARN_(mcimidi)("Unsupported command=%s\n", MCI_CommandToString(wMsg));
1587 FIXME_(mcimidi)("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1590 TRACE_(mcimidi)("Sending msg=%s to default driver proc\n", MCI_CommandToString(wMsg));
1591 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1593 return MCIERR_UNRECOGNIZED_COMMAND;