2 * Sample MIDI Wine Driver for Linux
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1999 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * + implement it correctly
24 * + finish asynchronous commands
25 * + better implement non waiting command (without the MCI_WAIT flag).
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mcimidi);
44 #define MIDI_NOTEOFF 0x80
45 #define MIDI_NOTEON 0x90
48 DWORD dwFirst; /* offset in file of track */
49 DWORD dwLast; /* number of bytes in file of track */
50 DWORD dwIndex; /* current index in file (dwFirst <= dwIndex < dwLast) */
51 DWORD dwLength; /* number of pulses in this track */
52 DWORD dwEventPulse; /* current pulse # (event) pointed by dwIndex */
53 DWORD dwEventData; /* current data (event) pointed by dwIndex */
54 WORD wEventLength; /* current length (event) pointed by dwIndex */
55 WORD wStatus : 1, /* 1 : playing, 0 : done */
57 wLastCommand : 8; /* last MIDI command on track */
60 typedef struct tagWINE_MCIMIDI {
61 UINT wDevID; /* the MCI one */
63 int nUseCount; /* Incremented for each shared open */
64 MCIDEVICEID wNotifyDeviceID; /* MCI device ID with a pending notification */
65 HANDLE hCallback; /* Callback handle for pending notification */
66 HMMIO hFile; /* mmio file handle open as Element */
67 LPWSTR lpstrElementName; /* Name of file (if any) */
68 LPWSTR lpstrCopyright;
70 WORD wPort; /* the WINMM device unit */
71 WORD dwStatus; /* one from MCI_MODE_xxxx */
72 DWORD dwMciTimeFormat; /* One of the supported MCI_FORMAT_xxxx */
73 WORD wFormat; /* Format of MIDI hFile (0, 1 or 2) */
74 WORD nTracks; /* Number of tracks in hFile */
75 WORD nDivision; /* Number of division in hFile PPQN or SMPTE */
77 DWORD dwTempo; /* Tempo (# of 1/4 note per second */
78 MCI_MIDITRACK* tracks; /* Content of each track */
84 /* ===================================================================
85 * ===================================================================
86 * FIXME: should be using the new mmThreadXXXX functions from WINMM
88 * it would require to add a wine internal flag to mmThreadCreate
89 * in order to pass a 32 bit function instead of a 16 bit
90 * ===================================================================
91 * =================================================================== */
100 /* EPP DWORD WINAPI mciSendCommandA(UINT wDevID, UINT wMsg, DWORD dwParam1, DWORD dwParam2); */
102 /**************************************************************************
103 * MCI_SCAStarter [internal]
105 static DWORD CALLBACK MCI_SCAStarter(LPVOID arg)
107 struct SCA* sca = arg;
110 TRACE("In thread before async command (%08x,%u,%08lx,%08lx)\n",
111 sca->wDevID, sca->wMsg, sca->dwParam1, sca->dwParam2);
112 ret = mciSendCommandA(sca->wDevID, sca->wMsg, sca->dwParam1 | MCI_WAIT, sca->dwParam2);
113 TRACE("In thread after async command (%08x,%u,%08lx,%08lx)\n",
114 sca->wDevID, sca->wMsg, sca->dwParam1, sca->dwParam2);
115 HeapFree(GetProcessHeap(), 0, sca);
119 /**************************************************************************
120 * MCI_SendCommandAsync [internal]
122 static DWORD MCI_SendCommandAsync(UINT wDevID, UINT wMsg, DWORD_PTR dwParam1,
123 DWORD_PTR dwParam2, UINT size)
126 struct SCA* sca = HeapAlloc(GetProcessHeap(), 0, sizeof(struct SCA) + size);
129 return MCIERR_OUT_OF_MEMORY;
131 sca->wDevID = wDevID;
133 sca->dwParam1 = dwParam1;
135 if (size && dwParam2) {
136 sca->dwParam2 = (DWORD_PTR)sca + sizeof(struct SCA);
137 /* copy structure passed by program in dwParam2 to be sure
138 * we can still use it whatever the program does
140 memcpy((LPVOID)sca->dwParam2, (LPVOID)dwParam2, size);
142 sca->dwParam2 = dwParam2;
145 if ((handle = CreateThread(NULL, 0, MCI_SCAStarter, sca, 0, NULL)) == 0) {
146 WARN("Couldn't allocate thread for async command handling, sending synchronously\n");
147 return MCI_SCAStarter(sca);
149 SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL);
154 /*======================================================================*
155 * MCI MIDI implementation *
156 *======================================================================*/
158 static DWORD MIDI_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms);
160 /**************************************************************************
161 * MIDI_drvOpen [internal]
163 static DWORD MIDI_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
167 if (!modp) return 0xFFFFFFFF;
169 wmm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MCIMIDI));
174 wmm->wDevID = modp->wDeviceID;
175 mciSetDriverData(wmm->wDevID, (DWORD_PTR)wmm);
176 modp->wCustomCommandTable = MCI_NO_COMMAND_TABLE;
177 modp->wType = MCI_DEVTYPE_SEQUENCER;
178 return modp->wDeviceID;
181 /**************************************************************************
182 * MCIMIDI_drvClose [internal]
184 static DWORD MIDI_drvClose(DWORD dwDevID)
186 WINE_MCIMIDI* wmm = (WINE_MCIMIDI*)mciGetDriverData(dwDevID);
189 HeapFree(GetProcessHeap(), 0, wmm);
190 mciSetDriverData(dwDevID, 0);
193 return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
196 /**************************************************************************
197 * MIDI_mciGetOpenDev [internal]
199 static WINE_MCIMIDI* MIDI_mciGetOpenDev(UINT wDevID)
201 WINE_MCIMIDI* wmm = (WINE_MCIMIDI*)mciGetDriverData(wDevID);
203 if (wmm == NULL || wmm->nUseCount == 0) {
204 WARN("Invalid wDevID=%u\n", wDevID);
210 /**************************************************************************
211 * MIDI_mciNotify [internal]
213 * Notifications in MCI work like a 1-element queue.
214 * Each new notification request supersedes the previous one.
215 * This affects Play and Record; other commands are immediate.
217 static void MIDI_mciNotify(DWORD_PTR hWndCallBack, WINE_MCIMIDI* wmm, UINT wStatus)
219 /* We simply save one parameter by not passing the wDevID local
220 * to the command. They are the same (via mciGetDriverData).
222 MCIDEVICEID wDevID = wmm->wNotifyDeviceID;
223 HANDLE old = InterlockedExchangePointer(&wmm->hCallback, NULL);
224 if (old) mciDriverNotify(old, wDevID, MCI_NOTIFY_SUPERSEDED);
225 mciDriverNotify(HWND_32(LOWORD(hWndCallBack)), wDevID, wStatus);
228 /**************************************************************************
229 * MIDI_mciReadByte [internal]
231 static DWORD MIDI_mciReadByte(WINE_MCIMIDI* wmm, BYTE *lpbyt)
236 mmioRead(wmm->hFile, (HPSTR)lpbyt, sizeof(BYTE)) != (long)sizeof(BYTE)) {
237 WARN("Error reading wmm=%p\n", wmm);
238 ret = MCIERR_INVALID_FILE;
244 /**************************************************************************
245 * MIDI_mciReadWord [internal]
247 static DWORD MIDI_mciReadWord(WINE_MCIMIDI* wmm, LPWORD lpw)
250 DWORD ret = MCIERR_INVALID_FILE;
253 MIDI_mciReadByte(wmm, &hibyte) == 0 &&
254 MIDI_mciReadByte(wmm, &lobyte) == 0) {
255 *lpw = ((WORD)hibyte << 8) + lobyte;
261 /**************************************************************************
262 * MIDI_mciReadLong [internal]
264 static DWORD MIDI_mciReadLong(WINE_MCIMIDI* wmm, LPDWORD lpdw)
267 DWORD ret = MCIERR_INVALID_FILE;
270 MIDI_mciReadWord(wmm, &hiword) == 0 &&
271 MIDI_mciReadWord(wmm, &loword) == 0) {
272 *lpdw = MAKELONG(loword, hiword);
278 /**************************************************************************
279 * MIDI_mciReadVaryLen [internal]
281 static WORD MIDI_mciReadVaryLen(WINE_MCIMIDI* wmm, LPDWORD lpdw)
288 ret = MCIERR_INVALID_FILE;
291 if (MIDI_mciReadByte(wmm, &byte) != 0) {
294 value = (value << 7) + (byte & 0x7F);
296 } while (byte & 0x80);
299 TRACE("val=%08X\n", value);
305 /**************************************************************************
306 * MIDI_mciReadNextEvent [internal]
308 static DWORD MIDI_mciReadNextEvent(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
316 if (mmioSeek(wmm->hFile, mmt->dwIndex, SEEK_SET) != mmt->dwIndex) {
317 WARN("Can't seek at %08X\n", mmt->dwIndex);
318 return MCIERR_INVALID_FILE;
320 evtLength = MIDI_mciReadVaryLen(wmm, &evtPulse) + 1; /* > 0 */
321 MIDI_mciReadByte(wmm, &b1);
325 evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
329 MIDI_mciReadByte(wmm, &b2); evtLength++;
331 evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
332 if (evtLength >= 0x10000u) {
333 /* this limitation shouldn't be a problem */
334 WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
337 hw = LOWORD(evtLength);
342 if (b1 & 0x80) { /* use running status ? */
343 mmt->wLastCommand = b1;
344 MIDI_mciReadByte(wmm, &b2); evtLength++;
347 b1 = mmt->wLastCommand;
349 switch ((b1 >> 4) & 0x07) {
350 case 0: case 1: case 2: case 3: case 6:
351 MIDI_mciReadByte(wmm, &b3); evtLength++;
357 WARN("Strange indeed b1=0x%02x\n", b1);
361 if (mmt->dwIndex + evtLength > mmt->dwLast)
362 return MCIERR_INTERNAL;
364 mmt->dwEventPulse += evtPulse;
365 mmt->dwEventData = (hw << 16) + (b2 << 8) + b1;
366 mmt->wEventLength = evtLength;
369 TRACE("[%u] => pulse=%08x(%08x), data=%08x, length=%u\n",
370 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
371 mmt->dwEventData, mmt->wEventLength);
376 /**************************************************************************
377 * MIDI_mciReadMTrk [internal]
379 static DWORD MIDI_mciReadMTrk(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
384 if (mmioRead(wmm->hFile, (HPSTR)&fourcc, (long)sizeof(FOURCC)) !=
385 (long)sizeof(FOURCC)) {
386 return MCIERR_INVALID_FILE;
389 if (fourcc != mmioFOURCC('M', 'T', 'r', 'k')) {
390 WARN("Can't synchronize on 'MTrk' !\n");
391 return MCIERR_INVALID_FILE;
394 if (MIDI_mciReadLong(wmm, &toberead) != 0) {
395 return MCIERR_INVALID_FILE;
397 mmt->dwFirst = mmioSeek(wmm->hFile, 0, SEEK_CUR); /* >= 0 */
398 mmt->dwLast = mmt->dwFirst + toberead;
400 /* compute # of pulses in this track */
401 mmt->dwIndex = mmt->dwFirst;
402 mmt->dwEventPulse = 0;
404 while (MIDI_mciReadNextEvent(wmm, mmt) == 0 && LOWORD(mmt->dwEventData) != 0x2FFF) {
408 mmt->dwIndex += mmt->wEventLength;
410 switch (LOWORD(mmt->dwEventData)) {
413 /* position after meta data header */
414 mmioSeek(wmm->hFile, mmt->dwIndex + HIWORD(mmt->dwEventData), SEEK_SET);
415 len = mmt->wEventLength - HIWORD(mmt->dwEventData);
417 if (len >= sizeof(buf)) {
418 WARN("Buffer for text is too small (%u are needed)\n", len);
419 len = sizeof(buf) - 1;
421 if (mmioRead(wmm->hFile, buf, len) == len) {
422 buf[len] = 0; /* end string in case */
423 switch (HIBYTE(LOWORD(mmt->dwEventData))) {
425 if (wmm->lpstrCopyright) {
426 WARN("Two copyright notices (%s|%s)\n", debugstr_w(wmm->lpstrCopyright), buf);
428 len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
429 wmm->lpstrCopyright = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
430 MultiByteToWideChar( CP_ACP, 0, buf, -1, wmm->lpstrCopyright, len );
434 if (wmm->lpstrName) {
435 WARN("Two names (%s|%s)\n", debugstr_w(wmm->lpstrName), buf);
437 len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
438 wmm->lpstrName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
439 MultiByteToWideChar( CP_ACP, 0, buf, -1, wmm->lpstrName, len );
447 mmt->dwLength = mmt->dwEventPulse;
449 TRACE("Track %u has %u bytes and %u pulses\n", mmt->wTrackNr, toberead, mmt->dwLength);
451 /* reset track data */
452 mmt->wStatus = 1; /* ok, playing */
453 mmt->dwIndex = mmt->dwFirst;
454 mmt->dwEventPulse = 0;
456 if (mmioSeek(wmm->hFile, 0, SEEK_CUR) != mmt->dwLast) {
457 WARN("Ouch, out of sync seek=%u track=%u\n",
458 mmioSeek(wmm->hFile, 0, SEEK_CUR), mmt->dwLast);
459 /* position at end of this track, to be ready to read next track */
460 mmioSeek(wmm->hFile, mmt->dwLast, SEEK_SET);
466 /**************************************************************************
467 * MIDI_mciReadMThd [internal]
469 static DWORD MIDI_mciReadMThd(WINE_MCIMIDI* wmm, DWORD dwOffset)
475 TRACE("(%p, %08X);\n", wmm, dwOffset);
477 if (mmioSeek(wmm->hFile, dwOffset, SEEK_SET) != dwOffset) {
478 WARN("Can't seek at %08X begin of 'MThd'\n", dwOffset);
479 return MCIERR_INVALID_FILE;
481 if (mmioRead(wmm->hFile, (HPSTR)&fourcc,
482 (long) sizeof(FOURCC)) != (long) sizeof(FOURCC))
483 return MCIERR_INVALID_FILE;
485 if (fourcc != mmioFOURCC('M', 'T', 'h', 'd')) {
486 WARN("Can't synchronize on 'MThd' !\n");
487 return MCIERR_INVALID_FILE;
490 if (MIDI_mciReadLong(wmm, &toberead) != 0 || toberead < 3 * sizeof(WORD))
491 return MCIERR_INVALID_FILE;
493 if (MIDI_mciReadWord(wmm, &wmm->wFormat) != 0 ||
494 MIDI_mciReadWord(wmm, &wmm->nTracks) != 0 ||
495 MIDI_mciReadWord(wmm, &wmm->nDivision) != 0) {
496 return MCIERR_INVALID_FILE;
499 TRACE("toberead=0x%08X, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
500 toberead, wmm->wFormat, wmm->nTracks, wmm->nDivision);
502 /* MS doc says that the MIDI MCI time format must be put by default to the format
503 * stored in the MIDI file...
505 if (wmm->nDivision > 0x8000) {
506 /* eric.pouech@lemel.fr 98/11
507 * I did not check this very code (pulses are expressed as SMPTE sub-frames).
508 * In about 40 MB of MIDI files I have, none was SMPTE based...
509 * I'm just wondering if this is widely used :-). So, if someone has one of
510 * these files, I'd like to know about it.
512 FIXME("Handling SMPTE time in MIDI files has not been tested\n"
513 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
515 switch (HIBYTE(wmm->nDivision)) {
516 case 0xE8: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24; break; /* -24 */
517 case 0xE7: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25; break; /* -25 */
518 case 0xE3: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
519 case 0xE2: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30; break; /* -30 */
521 WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm->nDivision));
522 return MCIERR_INVALID_FILE;
524 switch (LOBYTE(wmm->nDivision)) {
525 case 4: /* MIDI Time Code */
528 case 80: /* SMPTE bit resolution */
531 WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm->nDivision));
532 return MCIERR_INVALID_FILE;
534 } else if (wmm->nDivision == 0) {
535 WARN("Number of division is 0, can't support that !!\n");
536 return MCIERR_INVALID_FILE;
538 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
541 switch (wmm->wFormat) {
543 if (wmm->nTracks != 1) {
544 WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
552 WARN("Handling MIDI files which format = %d is not (yet) supported\n"
553 "Please report with MIDI file !\n", wmm->wFormat);
554 return MCIERR_INVALID_FILE;
557 if (wmm->nTracks & 0x8000) {
558 /* this shouldn't be a problem... */
559 WARN("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
560 wmm->nTracks = 0x7FFF;
563 if ((wmm->tracks = HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK) * wmm->nTracks)) == NULL) {
564 return MCIERR_OUT_OF_MEMORY;
567 toberead -= 3 * sizeof(WORD);
569 TRACE("Size of MThd > 6, skipping %d extra bytes\n", toberead);
570 mmioSeek(wmm->hFile, toberead, SEEK_CUR);
573 for (nt = 0; nt < wmm->nTracks; nt++) {
574 wmm->tracks[nt].wTrackNr = nt;
575 if (MIDI_mciReadMTrk(wmm, &wmm->tracks[nt]) != 0) {
576 WARN("Can't read 'MTrk' header\n");
577 return MCIERR_INVALID_FILE;
581 wmm->dwTempo = 500000;
586 /**************************************************************************
587 * MIDI_ConvertPulseToMS [internal]
589 static DWORD MIDI_ConvertPulseToMS(WINE_MCIMIDI* wmm, DWORD pulse)
593 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
594 * may change during file playing
596 if (wmm->nDivision == 0) {
597 FIXME("Shouldn't happen. wmm->nDivision = 0\n");
598 } else if (wmm->nDivision > 0x8000) { /* SMPTE, unchecked FIXME? */
599 int nf = -(char)HIBYTE(wmm->nDivision); /* number of frames */
600 int nsf = LOBYTE(wmm->nDivision); /* number of sub-frames */
601 ret = (pulse * 1000) / (nf * nsf);
603 ret = (DWORD)((double)pulse * ((double)wmm->dwTempo / 1000) /
604 (double)wmm->nDivision);
608 TRACE("pulse=%u tempo=%u division=%u=0x%04x => ms=%u\n",
609 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
615 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
616 #define TIME_MS_IN_ONE_MINUTE (60*1000)
617 #define TIME_MS_IN_ONE_SECOND (1000)
619 /**************************************************************************
620 * MIDI_ConvertTimeFormatToMS [internal]
622 static DWORD MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI* wmm, DWORD val)
626 switch (wmm->dwMciTimeFormat) {
627 case MCI_FORMAT_MILLISECONDS:
630 case MCI_FORMAT_SMPTE_24:
632 (HIBYTE(HIWORD(val)) * 125) / 3 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
633 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
635 case MCI_FORMAT_SMPTE_25:
637 HIBYTE(HIWORD(val)) * 40 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
638 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
640 case MCI_FORMAT_SMPTE_30:
642 (HIBYTE(HIWORD(val)) * 100) / 3 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
643 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
646 WARN("Bad time format %u!\n", wmm->dwMciTimeFormat);
649 TRACE("val=%u=0x%08x [tf=%u] => ret=%u\n", val, val, wmm->dwMciTimeFormat, ret);
654 /**************************************************************************
655 * MIDI_ConvertMSToTimeFormat [internal]
657 static DWORD MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI* wmm, DWORD _val)
659 DWORD ret = 0, val = _val;
662 switch (wmm->dwMciTimeFormat) {
663 case MCI_FORMAT_MILLISECONDS:
666 case MCI_FORMAT_SMPTE_24:
667 case MCI_FORMAT_SMPTE_25:
668 case MCI_FORMAT_SMPTE_30:
669 h = val / TIME_MS_IN_ONE_HOUR;
670 m = (val -= h * TIME_MS_IN_ONE_HOUR) / TIME_MS_IN_ONE_MINUTE;
671 s = (val -= m * TIME_MS_IN_ONE_MINUTE) / TIME_MS_IN_ONE_SECOND;
672 switch (wmm->dwMciTimeFormat) {
673 case MCI_FORMAT_SMPTE_24:
674 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
675 f = (val * 3) / 125; val -= (f * 125) / 3;
677 case MCI_FORMAT_SMPTE_25:
678 /* one frame is 1000/25 ms long, 1000/25 == 40 */
679 f = val / 40; val -= f * 40;
681 case MCI_FORMAT_SMPTE_30:
682 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
683 f = (val * 3) / 100; val -= (f * 100) / 3;
686 FIXME("There must be some bad bad programmer\n");
689 /* val contains the number of ms which cannot make a complete frame */
690 /* FIXME: is this correct ? programs seem to be happy with that */
691 ret = (f << 24) | (s << 16) | (m << 8) | (h << 0);
694 WARN("Bad time format %u!\n", wmm->dwMciTimeFormat);
697 TRACE("val=%u [tf=%u] => ret=%u=0x%08x\n", _val, wmm->dwMciTimeFormat, ret, ret);
702 /**************************************************************************
703 * MIDI_GetMThdLengthMS [internal]
705 static DWORD MIDI_GetMThdLengthMS(WINE_MCIMIDI* wmm)
710 for (nt = 0; nt < wmm->nTracks; nt++) {
711 if (wmm->wFormat == 2) {
712 ret += wmm->tracks[nt].dwLength;
713 } else if (wmm->tracks[nt].dwLength > ret) {
714 ret = wmm->tracks[nt].dwLength;
717 /* FIXME: this is wrong if there is a tempo change inside the file */
718 return MIDI_ConvertPulseToMS(wmm, ret);
721 /**************************************************************************
722 * MIDI_mciOpen [internal]
724 static DWORD MIDI_mciOpen(UINT wDevID, DWORD dwFlags, LPMCI_OPEN_PARMSW lpParms)
728 WINE_MCIMIDI* wmm = (WINE_MCIMIDI*)mciGetDriverData(wDevID);
730 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
732 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
733 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
734 if (dwFlags & MCI_OPEN_SHAREABLE)
735 return MCIERR_HARDWARE;
737 if (wmm->nUseCount > 0) {
738 /* The driver is already opened on this channel
739 * MIDI sequencer cannot be shared
741 return MCIERR_DEVICE_OPEN;
747 wmm->wPort = MIDI_MAPPER;
748 wmm->lpstrElementName = NULL;
749 dwDeviceID = lpParms->wDeviceID;
751 TRACE("wDevID=%04X (lpParams->wDeviceID=%08X)\n", wDevID, dwDeviceID);
752 /* lpParms->wDeviceID = wDevID;*/
754 if (dwFlags & MCI_OPEN_ELEMENT) {
755 TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpParms->lpstrElementName));
756 if (lpParms->lpstrElementName && strlenW(lpParms->lpstrElementName) > 0) {
757 wmm->hFile = mmioOpenW((LPWSTR)lpParms->lpstrElementName, NULL,
758 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
759 if (wmm->hFile == 0) {
760 WARN("Can't find file %s!\n", debugstr_w(lpParms->lpstrElementName));
762 return MCIERR_FILE_NOT_FOUND;
764 wmm->lpstrElementName = HeapAlloc(GetProcessHeap(), 0,
765 (strlenW(lpParms->lpstrElementName) + 1) * sizeof(WCHAR));
766 strcpyW(wmm->lpstrElementName, lpParms->lpstrElementName);
769 TRACE("hFile=%p\n", wmm->hFile);
771 wmm->lpstrCopyright = NULL;
772 wmm->lpstrName = NULL;
774 wmm->wNotifyDeviceID = wDevID;
775 wmm->dwStatus = MCI_MODE_NOT_READY; /* while loading file contents */
776 /* spec says it should be the default format from the MIDI file... */
777 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
779 if (wmm->hFile != 0) {
784 if (mmioDescend(wmm->hFile, &ckMainRIFF, NULL, 0) != 0) {
785 dwRet = MCIERR_INVALID_FILE;
787 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
788 (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, ckMainRIFF.cksize);
790 if (ckMainRIFF.ckid == FOURCC_RIFF && ckMainRIFF.fccType == mmioFOURCC('R', 'M', 'I', 'D')) {
791 mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
792 mmioSeek(wmm->hFile, ckMainRIFF.dwDataOffset + ((ckMainRIFF.cksize + 1) & ~1), SEEK_SET);
793 if (mmioDescend(wmm->hFile, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK) == 0) {
794 TRACE("... is a 'RMID' file\n");
795 dwOffset = mmckInfo.dwDataOffset;
797 dwRet = MCIERR_INVALID_FILE;
800 if (dwRet == 0 && MIDI_mciReadMThd(wmm, dwOffset) != 0) {
801 WARN("Can't read 'MThd' header\n");
802 dwRet = MCIERR_INVALID_FILE;
806 TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
814 mmioClose(wmm->hFile, 0);
817 wmm->dwPositionMS = 0;
818 wmm->dwStatus = MCI_MODE_STOP;
819 if (dwFlags & MCI_NOTIFY)
820 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
825 /**************************************************************************
826 * MIDI_mciStop [internal]
828 static DWORD MIDI_mciStop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
831 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
833 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
835 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
837 if (wmm->dwStatus != MCI_MODE_STOP) {
838 HANDLE old = InterlockedExchangePointer(&wmm->hCallback, NULL);
839 if (old) mciDriverNotify(old, wDevID, MCI_NOTIFY_ABORTED);
842 if (wmm->dwStatus != MCI_MODE_STOP) {
843 int oldstat = wmm->dwStatus;
845 wmm->dwStatus = MCI_MODE_NOT_READY;
846 if (oldstat == MCI_MODE_PAUSE)
847 dwRet = midiOutReset((HMIDIOUT)wmm->hMidi);
849 while (wmm->dwStatus != MCI_MODE_STOP)
854 wmm->dwStatus = MCI_MODE_STOP;
856 if ((dwFlags & MCI_NOTIFY) && lpParms && MMSYSERR_NOERROR==dwRet)
857 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
861 /**************************************************************************
862 * MIDI_mciClose [internal]
864 static DWORD MIDI_mciClose(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
866 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
868 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
870 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
872 if (wmm->dwStatus != MCI_MODE_STOP) {
873 /* mciStop handles MCI_NOTIFY_ABORTED */
874 MIDI_mciStop(wDevID, MCI_WAIT, lpParms);
878 if (wmm->nUseCount == 0) {
879 if (wmm->hFile != 0) {
880 mmioClose(wmm->hFile, 0);
882 TRACE("hFile closed !\n");
884 HeapFree(GetProcessHeap(), 0, wmm->tracks);
885 HeapFree(GetProcessHeap(), 0, wmm->lpstrElementName);
886 HeapFree(GetProcessHeap(), 0, wmm->lpstrCopyright);
887 HeapFree(GetProcessHeap(), 0, wmm->lpstrName);
889 TRACE("Shouldn't happen... nUseCount=%d\n", wmm->nUseCount);
890 return MCIERR_INTERNAL;
893 if ((dwFlags & MCI_NOTIFY) && lpParms) {
894 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
899 /**************************************************************************
900 * MIDI_mciFindNextEvent [internal]
902 static MCI_MIDITRACK* MIDI_mciFindNextEvent(WINE_MCIMIDI* wmm, LPDWORD hiPulse)
907 *hiPulse = 0xFFFFFFFFul;
909 for (nt = 0; nt < wmm->nTracks; nt++) {
910 mmt = &wmm->tracks[nt];
912 if (mmt->wStatus == 0)
914 if (mmt->dwEventPulse < *hiPulse) {
915 *hiPulse = mmt->dwEventPulse;
919 return (cnt == 0xFFFFu) ? 0 /* no more event on all tracks */
923 /**************************************************************************
924 * MIDI_mciPlay [internal]
926 static DWORD MIDI_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
928 DWORD dwStartMS, dwEndMS;
934 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
936 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
938 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
940 if (wmm->hFile == 0) {
941 WARN("Can't play: no file %s!\n", debugstr_w(wmm->lpstrElementName));
942 return MCIERR_FILE_NOT_FOUND;
945 if (wmm->dwStatus != MCI_MODE_STOP) {
946 if (wmm->dwStatus == MCI_MODE_PAUSE) {
947 /* FIXME: parameters (start/end) in lpParams may not be used */
948 return MIDI_mciResume(wDevID, dwFlags, (LPMCI_GENERIC_PARMS)lpParms);
950 WARN("Can't play: device is not stopped !\n");
951 return MCIERR_INTERNAL;
954 if (!(dwFlags & MCI_WAIT)) {
955 return MCI_SendCommandAsync(wDevID, MCI_PLAY, dwFlags, (DWORD_PTR)lpParms, sizeof(MCI_PLAY_PARMS));
958 if (lpParms && (dwFlags & MCI_FROM)) {
959 dwStartMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwFrom);
961 dwStartMS = wmm->dwPositionMS;
964 if (lpParms && (dwFlags & MCI_TO)) {
965 dwEndMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
967 dwEndMS = 0xFFFFFFFFul;
970 TRACE("Playing from %u to %u\n", dwStartMS, dwEndMS);
972 oldcb = InterlockedExchangePointer(&wmm->hCallback,
973 (dwFlags & MCI_NOTIFY) ? HWND_32(LOWORD(lpParms->dwCallback)) : NULL);
974 if (oldcb) mciDriverNotify(oldcb, wDevID, MCI_NOTIFY_ABORTED);
978 for (nt = 0; nt < wmm->nTracks; nt++) {
979 mmt = &wmm->tracks[nt];
981 mmt->wStatus = 1; /* ok, playing */
982 mmt->dwIndex = mmt->dwFirst;
983 if (wmm->wFormat == 2 && nt > 0) {
984 mmt->dwEventPulse = wmm->tracks[nt - 1].dwLength;
986 mmt->dwEventPulse = 0;
988 MIDI_mciReadNextEvent(wmm, mmt); /* FIXME == 0 */
991 dwRet = midiOutOpen((LPHMIDIOUT)&wmm->hMidi, wmm->wPort, 0L, 0L, CALLBACK_NULL);
992 if (dwRet != MMSYSERR_NOERROR) {
997 wmm->dwTempo = 500000;
998 wmm->dwStatus = MCI_MODE_PLAY;
999 wmm->dwPositionMS = 0;
1000 wmm->wStartedPlaying = FALSE;
1002 while (wmm->dwStatus != MCI_MODE_STOP && wmm->dwStatus != MCI_MODE_NOT_READY) {
1003 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
1004 * too much. Tell gcc not to optimize status value using volatile.
1006 while (((volatile WINE_MCIMIDI*)wmm)->dwStatus == MCI_MODE_PAUSE);
1008 doPlay = (wmm->dwPositionMS >= dwStartMS && wmm->dwPositionMS <= dwEndMS);
1010 TRACE("wmm->dwStatus=%d, doPlay=%c\n", wmm->dwStatus, doPlay ? 'T' : 'F');
1012 if ((mmt = MIDI_mciFindNextEvent(wmm, &hiPulse)) == NULL)
1013 break; /* no more event on tracks */
1015 /* if starting playing, then set StartTicks to the value it would have had
1016 * if play had started at position 0
1018 if (doPlay && !wmm->wStartedPlaying) {
1019 wmm->dwStartTicks = GetTickCount() - MIDI_ConvertPulseToMS(wmm, wmm->dwPulse);
1020 wmm->wStartedPlaying = TRUE;
1021 TRACE("Setting dwStartTicks to %u\n", wmm->dwStartTicks);
1024 if (hiPulse > wmm->dwPulse) {
1025 wmm->dwPositionMS += MIDI_ConvertPulseToMS(wmm, hiPulse - wmm->dwPulse);
1027 DWORD togo = wmm->dwStartTicks + wmm->dwPositionMS;
1028 DWORD tc = GetTickCount();
1030 TRACE("Pulses hi=0x%08x <> cur=0x%08x\n", hiPulse, wmm->dwPulse);
1031 TRACE("Wait until %u => %u ms\n",
1032 tc - wmm->dwStartTicks, togo - wmm->dwStartTicks);
1036 wmm->dwPulse = hiPulse;
1039 switch (LOBYTE(LOWORD(mmt->dwEventData))) {
1041 case 0xF7: /* sysex events */
1043 FIXME("Not handling SysEx events (yet)\n");
1047 /* position after meta data header */
1048 mmioSeek(wmm->hFile, mmt->dwIndex + HIWORD(mmt->dwEventData), SEEK_SET);
1049 switch (HIBYTE(LOWORD(mmt->dwEventData))) {
1050 case 0x00: /* 16-bit sequence number */
1051 if (TRACE_ON(mcimidi)) {
1054 MIDI_mciReadWord(wmm, &twd); /* == 0 */
1055 TRACE("Got sequence number %u\n", twd);
1058 case 0x01: /* any text */
1059 case 0x02: /* Copyright Message text */
1060 case 0x03: /* Sequence/Track Name text */
1061 case 0x04: /* Instrument Name text */
1062 case 0x05: /* Lyric text */
1063 case 0x06: /* Marker text */
1064 case 0x07: /* Cue-point text */
1065 if (TRACE_ON(mcimidi)) {
1067 WORD len = mmt->wEventLength - HIWORD(mmt->dwEventData);
1068 static const char* const info[8] = {"", "Text", "Copyright", "Seq/Trk name",
1069 "Instrument", "Lyric", "Marker", "Cue-point"};
1070 WORD idx = HIBYTE(LOWORD(mmt->dwEventData));
1072 if (len >= sizeof(buf)) {
1073 WARN("Buffer for text is too small (%u are needed)\n", len);
1074 len = sizeof(buf) - 1;
1076 if (mmioRead(wmm->hFile, buf, len) == len) {
1077 buf[len] = 0; /* end string in case */
1078 TRACE("%s => \"%s\"\n", (idx < 8 ) ? info[idx] : "", buf);
1080 WARN("Couldn't read data for %s\n", (idx < 8) ? info[idx] : "");
1085 /* MIDI channel (cc) */
1086 if (FIXME_ON(mcimidi)) {
1089 MIDI_mciReadByte(wmm, &bt); /* == 0 */
1090 FIXME("NIY: MIDI channel=%u, track=%u\n", bt, mmt->wTrackNr);
1094 /* MIDI port (pp) */
1095 if (FIXME_ON(mcimidi)) {
1098 MIDI_mciReadByte(wmm, &bt); /* == 0 */
1099 FIXME("NIY: MIDI port=%u, track=%u\n", bt, mmt->wTrackNr);
1102 case 0x2F: /* end of track */
1105 case 0x51:/* set tempo */
1106 /* Tempo is expressed in -seconds per midi quarter note
1107 * for format 1 MIDI files, this can only be present on track #0
1109 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
1110 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt->wTrackNr);
1115 MIDI_mciReadByte(wmm, &tbt); value = ((DWORD)tbt) << 16;
1116 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 8;
1117 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 0;
1118 TRACE("Setting tempo to %d (BPM=%d)\n", wmm->dwTempo, (value) ? (60000000 / value) : 0);
1119 wmm->dwTempo = value;
1122 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
1123 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
1124 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt->wTrackNr);
1125 } if (mmt->dwEventPulse != 0) {
1126 WARN("SMPTE track start can only be expressed at start of track (%u)\n", mmt->dwEventPulse);
1128 BYTE h, m, s, f, ff;
1130 MIDI_mciReadByte(wmm, &h);
1131 MIDI_mciReadByte(wmm, &m);
1132 MIDI_mciReadByte(wmm, &s);
1133 MIDI_mciReadByte(wmm, &f);
1134 MIDI_mciReadByte(wmm, &ff);
1135 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h, m, s, f, ff);
1138 case 0x58: /* file rhythm */
1139 if (TRACE_ON(mcimidi)) {
1140 BYTE num, den, cpmc, _32npqn;
1142 MIDI_mciReadByte(wmm, &num);
1143 MIDI_mciReadByte(wmm, &den); /* to notate e.g. 6/8 */
1144 MIDI_mciReadByte(wmm, &cpmc); /* number of MIDI clocks per metronome click */
1145 MIDI_mciReadByte(wmm, &_32npqn); /* number of notated 32nd notes per MIDI quarter note */
1147 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num, 1 << den, cpmc, _32npqn);
1150 case 0x59: /* key signature */
1151 if (TRACE_ON(mcimidi)) {
1154 MIDI_mciReadByte(wmm, &sf);
1155 MIDI_mciReadByte(wmm, &mm);
1157 if (sf >= 0x80) TRACE("%d flats\n", -(char)sf);
1158 else if (sf > 0) TRACE("%d sharps\n", (char)sf);
1159 else TRACE("Key of C\n");
1160 TRACE("Mode: %s\n", (mm == 0) ? "major" : "minor");
1164 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt->dwEventData)));
1170 dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1172 switch (LOBYTE(LOWORD(mmt->dwEventData)) & 0xF0) {
1178 dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1182 mmt->dwIndex += mmt->wEventLength;
1183 if (mmt->dwIndex < mmt->dwFirst || mmt->dwIndex >= mmt->dwLast) {
1187 MIDI_mciReadNextEvent(wmm, mmt);
1191 midiOutReset((HMIDIOUT)wmm->hMidi);
1193 dwRet = midiOutClose((HMIDIOUT)wmm->hMidi);
1195 if (dwFlags & MCI_NOTIFY)
1196 oldcb = InterlockedExchangePointer(&wmm->hCallback, NULL);
1198 wmm->dwStatus = MCI_MODE_STOP;
1200 /* Let the potentially asynchronous commands support FAILURE notification. */
1201 if (oldcb) mciDriverNotify(oldcb, wDevID,
1202 dwRet ? MCI_NOTIFY_FAILURE : MCI_NOTIFY_SUCCESSFUL);
1206 /**************************************************************************
1207 * MIDI_mciPause [internal]
1209 static DWORD MIDI_mciPause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1211 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1213 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1215 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1217 if (wmm->dwStatus == MCI_MODE_PLAY) {
1218 /* stop all notes */
1220 for (chn = 0; chn < 16; chn++)
1221 midiOutShortMsg((HMIDIOUT)(wmm->hMidi), 0x78B0 | chn);
1222 wmm->dwStatus = MCI_MODE_PAUSE;
1224 if ((dwFlags & MCI_NOTIFY) && lpParms)
1225 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1229 /**************************************************************************
1230 * MIDI_mciResume [internal]
1232 static DWORD MIDI_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1234 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1236 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1238 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1240 if (wmm->dwStatus == MCI_MODE_PAUSE) {
1241 wmm->wStartedPlaying = FALSE;
1242 wmm->dwStatus = MCI_MODE_PLAY;
1244 if ((dwFlags & MCI_NOTIFY) && lpParms)
1245 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1249 /**************************************************************************
1250 * MIDI_mciSet [internal]
1252 static DWORD MIDI_mciSet(UINT wDevID, DWORD dwFlags, LPMCI_SEQ_SET_PARMS lpParms)
1254 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1256 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1258 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1259 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1261 if (dwFlags & MCI_SET_TIME_FORMAT) {
1262 switch (lpParms->dwTimeFormat) {
1263 case MCI_FORMAT_MILLISECONDS:
1264 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1265 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
1267 case MCI_FORMAT_SMPTE_24:
1268 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1269 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24;
1271 case MCI_FORMAT_SMPTE_25:
1272 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1273 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25;
1275 case MCI_FORMAT_SMPTE_30:
1276 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1277 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30;
1280 WARN("Bad time format %u!\n", lpParms->dwTimeFormat);
1281 return MCIERR_BAD_TIME_FORMAT;
1284 if (dwFlags & MCI_SET_VIDEO) {
1285 TRACE("No support for video !\n");
1286 return MCIERR_UNSUPPORTED_FUNCTION;
1288 if (dwFlags & MCI_SET_DOOR_OPEN) {
1289 TRACE("No support for door open !\n");
1290 return MCIERR_UNSUPPORTED_FUNCTION;
1292 if (dwFlags & MCI_SET_DOOR_CLOSED) {
1293 TRACE("No support for door close !\n");
1294 return MCIERR_UNSUPPORTED_FUNCTION;
1296 if (dwFlags & MCI_SET_AUDIO) {
1297 if (dwFlags & MCI_SET_ON) {
1298 TRACE("MCI_SET_ON audio !\n");
1299 } else if (dwFlags & MCI_SET_OFF) {
1300 TRACE("MCI_SET_OFF audio !\n");
1302 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1303 return MCIERR_BAD_INTEGER;
1306 switch (lpParms->dwAudio)
1308 case MCI_SET_AUDIO_ALL: TRACE("MCI_SET_AUDIO_ALL !\n"); break;
1309 case MCI_SET_AUDIO_LEFT: TRACE("MCI_SET_AUDIO_LEFT !\n"); break;
1310 case MCI_SET_AUDIO_RIGHT: TRACE("MCI_SET_AUDIO_RIGHT !\n"); break;
1311 default: WARN("Unknown audio channel %u\n", lpParms->dwAudio); break;
1315 if (dwFlags & MCI_SEQ_SET_MASTER)
1316 TRACE("MCI_SEQ_SET_MASTER !\n");
1317 if (dwFlags & MCI_SEQ_SET_SLAVE)
1318 TRACE("MCI_SEQ_SET_SLAVE !\n");
1319 if (dwFlags & MCI_SEQ_SET_OFFSET)
1320 TRACE("MCI_SEQ_SET_OFFSET !\n");
1321 if (dwFlags & MCI_SEQ_SET_PORT) {
1322 TRACE("MCI_SEQ_SET_PORT = %d\n", lpParms->dwPort);
1323 if ((UINT16)lpParms->dwPort != (UINT16)MIDI_MAPPER &&
1324 (UINT16)lpParms->dwPort >= midiOutGetNumDevs())
1325 /* FIXME: input/output port distinction? */
1326 return MCIERR_SEQ_PORT_NONEXISTENT;
1327 /* FIXME: Native manages to swap the device while playing! */
1328 wmm->wPort = lpParms->dwPort;
1330 if (dwFlags & MCI_SEQ_SET_TEMPO)
1331 TRACE("MCI_SEQ_SET_TEMPO !\n");
1332 if (dwFlags & MCI_NOTIFY)
1333 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1337 /**************************************************************************
1338 * MIDI_mciStatus [internal]
1340 static DWORD MIDI_mciStatus(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
1342 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1345 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1347 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1348 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1350 if (dwFlags & MCI_STATUS_ITEM) {
1351 switch (lpParms->dwItem) {
1352 case MCI_STATUS_CURRENT_TRACK:
1353 /* FIXME in Format 2 */
1354 lpParms->dwReturn = 1;
1355 TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms->dwReturn);
1357 case MCI_STATUS_LENGTH:
1358 if ((dwFlags & MCI_TRACK) && wmm->wFormat == 2) {
1359 if (lpParms->dwTrack >= wmm->nTracks)
1360 return MCIERR_BAD_INTEGER;
1361 /* FIXME: this is wrong if there is a tempo change inside the file */
1362 lpParms->dwReturn = MIDI_ConvertPulseToMS(wmm, wmm->tracks[lpParms->dwTrack].dwLength);
1364 lpParms->dwReturn = MIDI_GetMThdLengthMS(wmm);
1366 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm, lpParms->dwReturn);
1367 TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms->dwReturn);
1369 case MCI_STATUS_MODE:
1370 TRACE("MCI_STATUS_MODE => %u\n", wmm->dwStatus);
1371 lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwStatus, wmm->dwStatus);
1372 ret = MCI_RESOURCE_RETURNED;
1374 case MCI_STATUS_MEDIA_PRESENT:
1375 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1376 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1377 ret = MCI_RESOURCE_RETURNED;
1379 case MCI_STATUS_NUMBER_OF_TRACKS:
1380 lpParms->dwReturn = (wmm->wFormat == 2) ? wmm->nTracks : 1;
1381 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms->dwReturn);
1383 case MCI_STATUS_POSITION:
1384 /* FIXME: do I need to use MCI_TRACK ? */
1385 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm,
1386 (dwFlags & MCI_STATUS_START) ? 0 : wmm->dwPositionMS);
1387 TRACE("MCI_STATUS_POSITION %s => %lu\n",
1388 (dwFlags & MCI_STATUS_START) ? "start" : "current", lpParms->dwReturn);
1390 case MCI_STATUS_READY:
1391 lpParms->dwReturn = (wmm->dwStatus == MCI_MODE_NOT_READY) ?
1392 MAKEMCIRESOURCE(FALSE, MCI_FALSE) : MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1393 ret = MCI_RESOURCE_RETURNED;
1394 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms->dwReturn));
1396 case MCI_STATUS_TIME_FORMAT:
1397 lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwMciTimeFormat, MCI_FORMAT_RETURN_BASE + wmm->dwMciTimeFormat);
1398 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms->dwReturn));
1399 ret = MCI_RESOURCE_RETURNED;
1401 case MCI_SEQ_STATUS_DIVTYPE:
1402 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1403 if (wmm->nDivision > 0x8000) {
1404 switch (wmm->nDivision) {
1405 case 0xE8: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_24; break; /* -24 */
1406 case 0xE7: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_25; break; /* -25 */
1407 case 0xE3: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
1408 case 0xE2: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30; break; /* -30 */
1409 default: FIXME("There is a bad bad programmer\n");
1412 lpParms->dwReturn = MCI_SEQ_DIV_PPQN;
1414 lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn,lpParms->dwReturn);
1415 ret = MCI_RESOURCE_RETURNED;
1417 case MCI_SEQ_STATUS_MASTER:
1418 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1419 lpParms->dwReturn = 0;
1421 case MCI_SEQ_STATUS_SLAVE:
1422 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1423 lpParms->dwReturn = 0;
1425 case MCI_SEQ_STATUS_OFFSET:
1426 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1427 lpParms->dwReturn = 0;
1429 case MCI_SEQ_STATUS_PORT:
1430 if (wmm->wPort != (UINT16)MIDI_MAPPER)
1431 lpParms->dwReturn = wmm->wPort;
1433 lpParms->dwReturn = MAKEMCIRESOURCE(MIDI_MAPPER, MCI_SEQ_MAPPER_S);
1434 ret = MCI_RESOURCE_RETURNED;
1436 TRACE("MCI_SEQ_STATUS_PORT (%u) => %d\n", wmm->wDevID, wmm->wPort);
1438 case MCI_SEQ_STATUS_TEMPO:
1439 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1440 lpParms->dwReturn = wmm->dwTempo;
1443 FIXME("Unknown command %08X !\n", lpParms->dwItem);
1444 return MCIERR_UNRECOGNIZED_COMMAND;
1447 WARN("No Status-Item!\n");
1448 return MCIERR_UNRECOGNIZED_COMMAND;
1450 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
1451 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1455 /**************************************************************************
1456 * MIDI_mciGetDevCaps [internal]
1458 static DWORD MIDI_mciGetDevCaps(UINT wDevID, DWORD dwFlags,
1459 LPMCI_GETDEVCAPS_PARMS lpParms)
1461 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1464 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1466 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1467 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1469 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
1470 switch (lpParms->dwItem) {
1471 case MCI_GETDEVCAPS_DEVICE_TYPE:
1472 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1473 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER, MCI_DEVTYPE_SEQUENCER);
1474 ret = MCI_RESOURCE_RETURNED;
1476 case MCI_GETDEVCAPS_HAS_AUDIO:
1477 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1478 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1479 ret = MCI_RESOURCE_RETURNED;
1481 case MCI_GETDEVCAPS_HAS_VIDEO:
1482 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1483 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1484 ret = MCI_RESOURCE_RETURNED;
1486 case MCI_GETDEVCAPS_USES_FILES:
1487 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1488 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1489 ret = MCI_RESOURCE_RETURNED;
1491 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
1492 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1493 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1494 ret = MCI_RESOURCE_RETURNED;
1496 case MCI_GETDEVCAPS_CAN_EJECT:
1497 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1498 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1499 ret = MCI_RESOURCE_RETURNED;
1501 case MCI_GETDEVCAPS_CAN_PLAY:
1502 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1503 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1504 ret = MCI_RESOURCE_RETURNED;
1506 case MCI_GETDEVCAPS_CAN_RECORD:
1507 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1508 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1509 ret = MCI_RESOURCE_RETURNED;
1511 case MCI_GETDEVCAPS_CAN_SAVE:
1512 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1513 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1514 ret = MCI_RESOURCE_RETURNED;
1517 FIXME("Unknown capability (%08x) !\n", lpParms->dwItem);
1518 return MCIERR_UNRECOGNIZED_COMMAND;
1521 WARN("No GetDevCaps-Item !\n");
1522 return MCIERR_UNRECOGNIZED_COMMAND;
1524 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
1525 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1529 /**************************************************************************
1530 * MIDI_mciInfo [internal]
1532 static DWORD MIDI_mciInfo(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
1535 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1537 static const WCHAR wszMidiSeq[] = {'W','i','n','e','\'','s',' ','M','I','D','I',' ','s','e','q','u','e','n','c','e','r',0};
1539 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1541 if (lpParms == NULL || lpParms->lpstrReturn == NULL)
1542 return MCIERR_NULL_PARAMETER_BLOCK;
1543 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1545 TRACE("buf=%p, len=%u\n", lpParms->lpstrReturn, lpParms->dwRetSize);
1547 switch (dwFlags & ~(MCI_WAIT|MCI_NOTIFY)) {
1548 case MCI_INFO_PRODUCT: str = wszMidiSeq; break;
1549 case MCI_INFO_FILE: str = wmm->lpstrElementName; break;
1550 case MCI_INFO_COPYRIGHT: str = wmm->lpstrCopyright; break;
1551 case MCI_INFO_NAME: str = wmm->lpstrName; break;
1553 WARN("Don't know this info command (%u)\n", dwFlags);
1554 return MCIERR_UNRECOGNIZED_COMMAND;
1557 if (lpParms->dwRetSize) {
1559 /* FIXME? Since NT, mciwave, mciseq and mcicda set dwRetSize
1560 * to the number of characters written, excluding \0. */
1561 lstrcpynW(lpParms->lpstrReturn, str ? str : &zero, lpParms->dwRetSize);
1562 } else ret = MCIERR_PARAM_OVERFLOW;
1564 if (MMSYSERR_NOERROR==ret && (dwFlags & MCI_NOTIFY))
1565 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1569 /**************************************************************************
1570 * MIDI_mciSeek [internal]
1572 static DWORD MIDI_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1575 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1577 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1579 if (lpParms == NULL) {
1580 ret = MCIERR_NULL_PARAMETER_BLOCK;
1581 } else if (wmm == NULL) {
1582 ret = MCIERR_INVALID_DEVICE_ID;
1584 MIDI_mciStop(wDevID, MCI_WAIT, 0);
1586 if (dwFlags & MCI_SEEK_TO_START) {
1587 wmm->dwPositionMS = 0;
1588 } else if (dwFlags & MCI_SEEK_TO_END) {
1589 wmm->dwPositionMS = 0xFFFFFFFF; /* FIXME */
1590 } else if (dwFlags & MCI_TO) {
1591 wmm->dwPositionMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
1593 WARN("dwFlag doesn't tell where to seek to...\n");
1594 return MCIERR_MISSING_PARAMETER;
1597 TRACE("Seeking to position=%u ms\n", wmm->dwPositionMS);
1599 if (dwFlags & MCI_NOTIFY) {
1600 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1601 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1602 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1608 /*======================================================================*
1609 * MIDI entry points *
1610 *======================================================================*/
1612 /**************************************************************************
1613 * DriverProc (MCISEQ.@)
1615 LRESULT CALLBACK MCIMIDI_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1616 LPARAM dwParam1, LPARAM dwParam2)
1619 case DRV_LOAD: return 1;
1620 case DRV_FREE: return 1;
1621 case DRV_ENABLE: return 1;
1622 case DRV_DISABLE: return 1;
1623 case DRV_QUERYCONFIGURE: return 1;
1624 case DRV_CONFIGURE: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK); return 1;
1625 case DRV_INSTALL: return DRVCNF_RESTART;
1626 case DRV_REMOVE: return DRVCNF_RESTART;
1627 case DRV_OPEN: return MIDI_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1628 case DRV_CLOSE: return MIDI_drvClose(dwDevID);
1631 if (dwDevID == 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION;
1634 case MCI_OPEN_DRIVER: return MIDI_mciOpen (dwDevID, dwParam1, (LPMCI_OPEN_PARMSW) dwParam2);
1635 case MCI_CLOSE_DRIVER: return MIDI_mciClose (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1636 case MCI_PLAY: return MIDI_mciPlay (dwDevID, dwParam1, (LPMCI_PLAY_PARMS) dwParam2);
1637 case MCI_STOP: return MIDI_mciStop (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1638 case MCI_SET: return MIDI_mciSet (dwDevID, dwParam1, (LPMCI_SEQ_SET_PARMS) dwParam2);
1639 case MCI_PAUSE: return MIDI_mciPause (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1640 case MCI_RESUME: return MIDI_mciResume (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1641 case MCI_STATUS: return MIDI_mciStatus (dwDevID, dwParam1, (LPMCI_STATUS_PARMS) dwParam2);
1642 case MCI_GETDEVCAPS: return MIDI_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1643 case MCI_INFO: return MIDI_mciInfo (dwDevID, dwParam1, (LPMCI_INFO_PARMSW) dwParam2);
1644 case MCI_SEEK: return MIDI_mciSeek (dwDevID, dwParam1, (LPMCI_SEEK_PARMS) dwParam2);
1645 /* commands that should report an error */
1663 TRACE("Unsupported command [0x%x]\n", wMsg);
1667 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1670 TRACE("Sending msg [%u] to default driver proc\n", wMsg);
1671 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1673 return MCIERR_UNSUPPORTED_FUNCTION;