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 (especially for reading/record)
25 * + better implement non waiting command (without the MCI_WAIT flag).
26 * + implement the recording features
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(mcimidi);
45 #define MIDI_NOTEOFF 0x80
46 #define MIDI_NOTEON 0x90
49 DWORD dwFirst; /* offset in file of track */
50 DWORD dwLast; /* number of bytes in file of track */
51 DWORD dwIndex; /* current index in file (dwFirst <= dwIndex < dwLast) */
52 DWORD dwLength; /* number of pulses in this track */
53 DWORD dwEventPulse; /* current pulse # (event) pointed by dwIndex */
54 DWORD dwEventData; /* current data (event) pointed by dwIndex */
55 WORD wEventLength; /* current length (event) pointed by dwIndex */
56 WORD wStatus : 1, /* 1 : playing, 0 : done */
58 wLastCommand : 8; /* last MIDI command on track */
61 typedef struct tagWINE_MCIMIDI {
62 UINT wDevID; /* the MCI one */
64 int nUseCount; /* Incremented for each shared open */
65 WORD wPort; /* the WINMM device unit */
66 WORD wNotifyDeviceID; /* MCI device ID with a pending notification */
67 HANDLE hCallback; /* Callback handle for pending notification */
68 HMMIO hFile; /* mmio file handle open as Element */
69 LPWSTR lpstrElementName; /* Name of file (if any) */
70 LPWSTR lpstrCopyright;
72 WORD dwStatus; /* one from MCI_MODE_xxxx */
73 DWORD dwMciTimeFormat; /* One of the supported MCI_FORMAT_xxxx */
74 WORD wFormat; /* Format of MIDI hFile (0, 1 or 2) */
75 WORD nTracks; /* Number of tracks in hFile */
76 WORD nDivision; /* Number of division in hFile PPQN or SMPTE */
78 DWORD dwTempo; /* Tempo (# of 1/4 note per second */
79 MCI_MIDITRACK* tracks; /* Content of each track */
85 /* ===================================================================
86 * ===================================================================
87 * FIXME: should be using the new mmThreadXXXX functions from WINMM
89 * it would require to add a wine internal flag to mmThreadCreate
90 * in order to pass a 32 bit function instead of a 16 bit
91 * ===================================================================
92 * =================================================================== */
101 /* EPP DWORD WINAPI mciSendCommandA(UINT wDevID, UINT wMsg, DWORD dwParam1, DWORD dwParam2); */
103 /**************************************************************************
104 * MCI_SCAStarter [internal]
106 static DWORD CALLBACK MCI_SCAStarter(LPVOID arg)
108 struct SCA* sca = arg;
111 TRACE("In thread before async command (%08x,%u,%08lx,%08lx)\n",
112 sca->wDevID, sca->wMsg, sca->dwParam1, sca->dwParam2);
113 ret = mciSendCommandA(sca->wDevID, sca->wMsg, sca->dwParam1 | MCI_WAIT, sca->dwParam2);
114 TRACE("In thread after async command (%08x,%u,%08lx,%08lx)\n",
115 sca->wDevID, sca->wMsg, sca->dwParam1, sca->dwParam2);
116 HeapFree(GetProcessHeap(), 0, sca);
120 /**************************************************************************
121 * MCI_SendCommandAsync [internal]
123 static DWORD MCI_SendCommandAsync(UINT wDevID, UINT wMsg, DWORD_PTR dwParam1,
124 DWORD_PTR dwParam2, UINT size)
127 struct SCA* sca = HeapAlloc(GetProcessHeap(), 0, sizeof(struct SCA) + size);
130 return MCIERR_OUT_OF_MEMORY;
132 sca->wDevID = wDevID;
134 sca->dwParam1 = dwParam1;
136 if (size && dwParam2) {
137 sca->dwParam2 = (DWORD_PTR)sca + sizeof(struct SCA);
138 /* copy structure passed by program in dwParam2 to be sure
139 * we can still use it whatever the program does
141 memcpy((LPVOID)sca->dwParam2, (LPVOID)dwParam2, size);
143 sca->dwParam2 = dwParam2;
146 if ((handle = CreateThread(NULL, 0, MCI_SCAStarter, sca, 0, NULL)) == 0) {
147 WARN("Couldn't allocate thread for async command handling, sending synchronously\n");
148 return MCI_SCAStarter(sca);
150 SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL);
155 /*======================================================================*
156 * MCI MIDI implementation *
157 *======================================================================*/
159 static DWORD MIDI_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms);
161 /**************************************************************************
162 * MIDI_drvOpen [internal]
164 static DWORD MIDI_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
168 if (!modp) return 0xFFFFFFFF;
170 wmm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MCIMIDI));
175 wmm->wDevID = modp->wDeviceID;
176 mciSetDriverData(wmm->wDevID, (DWORD_PTR)wmm);
177 modp->wCustomCommandTable = MCI_NO_COMMAND_TABLE;
178 modp->wType = MCI_DEVTYPE_SEQUENCER;
179 return modp->wDeviceID;
182 /**************************************************************************
183 * MCIMIDI_drvClose [internal]
185 static DWORD MIDI_drvClose(DWORD dwDevID)
187 WINE_MCIMIDI* wmm = (WINE_MCIMIDI*)mciGetDriverData(dwDevID);
190 HeapFree(GetProcessHeap(), 0, wmm);
191 mciSetDriverData(dwDevID, 0);
194 return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
197 /**************************************************************************
198 * MIDI_mciGetOpenDev [internal]
200 static WINE_MCIMIDI* MIDI_mciGetOpenDev(UINT wDevID)
202 WINE_MCIMIDI* wmm = (WINE_MCIMIDI*)mciGetDriverData(wDevID);
204 if (wmm == NULL || wmm->nUseCount == 0) {
205 WARN("Invalid wDevID=%u\n", wDevID);
211 /**************************************************************************
212 * MIDI_mciReadByte [internal]
214 static DWORD MIDI_mciReadByte(WINE_MCIMIDI* wmm, BYTE *lpbyt)
219 mmioRead(wmm->hFile, (HPSTR)lpbyt, sizeof(BYTE)) != (long)sizeof(BYTE)) {
220 WARN("Error reading wmm=%p\n", wmm);
221 ret = MCIERR_INVALID_FILE;
227 /**************************************************************************
228 * MIDI_mciReadWord [internal]
230 static DWORD MIDI_mciReadWord(WINE_MCIMIDI* wmm, LPWORD lpw)
233 DWORD ret = MCIERR_INVALID_FILE;
236 MIDI_mciReadByte(wmm, &hibyte) == 0 &&
237 MIDI_mciReadByte(wmm, &lobyte) == 0) {
238 *lpw = ((WORD)hibyte << 8) + lobyte;
244 /**************************************************************************
245 * MIDI_mciReadLong [internal]
247 static DWORD MIDI_mciReadLong(WINE_MCIMIDI* wmm, LPDWORD lpdw)
250 DWORD ret = MCIERR_INVALID_FILE;
253 MIDI_mciReadWord(wmm, &hiword) == 0 &&
254 MIDI_mciReadWord(wmm, &loword) == 0) {
255 *lpdw = MAKELONG(loword, hiword);
261 /**************************************************************************
262 * MIDI_mciReadVaryLen [internal]
264 static WORD MIDI_mciReadVaryLen(WINE_MCIMIDI* wmm, LPDWORD lpdw)
271 ret = MCIERR_INVALID_FILE;
274 if (MIDI_mciReadByte(wmm, &byte) != 0) {
277 value = (value << 7) + (byte & 0x7F);
279 } while (byte & 0x80);
282 TRACE("val=%08X\n", value);
288 /**************************************************************************
289 * MIDI_mciReadNextEvent [internal]
291 static DWORD MIDI_mciReadNextEvent(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
299 if (mmioSeek(wmm->hFile, mmt->dwIndex, SEEK_SET) != mmt->dwIndex) {
300 WARN("Can't seek at %08X\n", mmt->dwIndex);
301 return MCIERR_INVALID_FILE;
303 evtLength = MIDI_mciReadVaryLen(wmm, &evtPulse) + 1; /* > 0 */
304 MIDI_mciReadByte(wmm, &b1);
308 evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
312 MIDI_mciReadByte(wmm, &b2); evtLength++;
314 evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
315 if (evtLength >= 0x10000u) {
316 /* this limitation shouldn't be a problem */
317 WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
320 hw = LOWORD(evtLength);
325 if (b1 & 0x80) { /* use running status ? */
326 mmt->wLastCommand = b1;
327 MIDI_mciReadByte(wmm, &b2); evtLength++;
330 b1 = mmt->wLastCommand;
332 switch ((b1 >> 4) & 0x07) {
333 case 0: case 1: case 2: case 3: case 6:
334 MIDI_mciReadByte(wmm, &b3); evtLength++;
340 WARN("Strange indeed b1=0x%02x\n", b1);
344 if (mmt->dwIndex + evtLength > mmt->dwLast)
345 return MCIERR_INTERNAL;
347 mmt->dwEventPulse += evtPulse;
348 mmt->dwEventData = (hw << 16) + (b2 << 8) + b1;
349 mmt->wEventLength = evtLength;
352 TRACE("[%u] => pulse=%08x(%08x), data=%08x, length=%u\n",
353 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
354 mmt->dwEventData, mmt->wEventLength);
359 /**************************************************************************
360 * MIDI_mciReadMTrk [internal]
362 static DWORD MIDI_mciReadMTrk(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
367 if (mmioRead(wmm->hFile, (HPSTR)&fourcc, (long)sizeof(FOURCC)) !=
368 (long)sizeof(FOURCC)) {
369 return MCIERR_INVALID_FILE;
372 if (fourcc != mmioFOURCC('M', 'T', 'r', 'k')) {
373 WARN("Can't synchronize on 'MTrk' !\n");
374 return MCIERR_INVALID_FILE;
377 if (MIDI_mciReadLong(wmm, &toberead) != 0) {
378 return MCIERR_INVALID_FILE;
380 mmt->dwFirst = mmioSeek(wmm->hFile, 0, SEEK_CUR); /* >= 0 */
381 mmt->dwLast = mmt->dwFirst + toberead;
383 /* compute # of pulses in this track */
384 mmt->dwIndex = mmt->dwFirst;
385 mmt->dwEventPulse = 0;
387 while (MIDI_mciReadNextEvent(wmm, mmt) == 0 && LOWORD(mmt->dwEventData) != 0x2FFF) {
391 mmt->dwIndex += mmt->wEventLength;
393 switch (LOWORD(mmt->dwEventData)) {
396 /* position after meta data header */
397 mmioSeek(wmm->hFile, mmt->dwIndex + HIWORD(mmt->dwEventData), SEEK_SET);
398 len = mmt->wEventLength - HIWORD(mmt->dwEventData);
400 if (len >= sizeof(buf)) {
401 WARN("Buffer for text is too small (%u are needed)\n", len);
402 len = sizeof(buf) - 1;
404 if (mmioRead(wmm->hFile, buf, len) == len) {
405 buf[len] = 0; /* end string in case */
406 switch (HIBYTE(LOWORD(mmt->dwEventData))) {
408 if (wmm->lpstrCopyright) {
409 WARN("Two copyright notices (%s|%s)\n", debugstr_w(wmm->lpstrCopyright), buf);
411 len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
412 wmm->lpstrCopyright = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
413 MultiByteToWideChar( CP_ACP, 0, buf, -1, wmm->lpstrCopyright, len );
417 if (wmm->lpstrName) {
418 WARN("Two names (%s|%s)\n", debugstr_w(wmm->lpstrName), buf);
420 len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
421 wmm->lpstrName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
422 MultiByteToWideChar( CP_ACP, 0, buf, -1, wmm->lpstrName, len );
430 mmt->dwLength = mmt->dwEventPulse;
432 TRACE("Track %u has %u bytes and %u pulses\n", mmt->wTrackNr, toberead, mmt->dwLength);
434 /* reset track data */
435 mmt->wStatus = 1; /* ok, playing */
436 mmt->dwIndex = mmt->dwFirst;
437 mmt->dwEventPulse = 0;
439 if (mmioSeek(wmm->hFile, 0, SEEK_CUR) != mmt->dwLast) {
440 WARN("Ouch, out of sync seek=%u track=%u\n",
441 mmioSeek(wmm->hFile, 0, SEEK_CUR), mmt->dwLast);
442 /* position at end of this track, to be ready to read next track */
443 mmioSeek(wmm->hFile, mmt->dwLast, SEEK_SET);
449 /**************************************************************************
450 * MIDI_mciReadMThd [internal]
452 static DWORD MIDI_mciReadMThd(WINE_MCIMIDI* wmm, DWORD dwOffset)
458 TRACE("(%p, %08X);\n", wmm, dwOffset);
460 if (mmioSeek(wmm->hFile, dwOffset, SEEK_SET) != dwOffset) {
461 WARN("Can't seek at %08X begin of 'MThd'\n", dwOffset);
462 return MCIERR_INVALID_FILE;
464 if (mmioRead(wmm->hFile, (HPSTR)&fourcc,
465 (long) sizeof(FOURCC)) != (long) sizeof(FOURCC))
466 return MCIERR_INVALID_FILE;
468 if (fourcc != mmioFOURCC('M', 'T', 'h', 'd')) {
469 WARN("Can't synchronize on 'MThd' !\n");
470 return MCIERR_INVALID_FILE;
473 if (MIDI_mciReadLong(wmm, &toberead) != 0 || toberead < 3 * sizeof(WORD))
474 return MCIERR_INVALID_FILE;
476 if (MIDI_mciReadWord(wmm, &wmm->wFormat) != 0 ||
477 MIDI_mciReadWord(wmm, &wmm->nTracks) != 0 ||
478 MIDI_mciReadWord(wmm, &wmm->nDivision) != 0) {
479 return MCIERR_INVALID_FILE;
482 TRACE("toberead=0x%08X, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
483 toberead, wmm->wFormat, wmm->nTracks, wmm->nDivision);
485 /* MS doc says that the MIDI MCI time format must be put by default to the format
486 * stored in the MIDI file...
488 if (wmm->nDivision > 0x8000) {
489 /* eric.pouech@lemel.fr 98/11
490 * I did not check this very code (pulses are expressed as SMPTE sub-frames).
491 * In about 40 MB of MIDI files I have, none was SMPTE based...
492 * I'm just wondering if this is widely used :-). So, if someone has one of
493 * these files, I'd like to know about it.
495 FIXME("Handling SMPTE time in MIDI files has not been tested\n"
496 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
498 switch (HIBYTE(wmm->nDivision)) {
499 case 0xE8: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24; break; /* -24 */
500 case 0xE7: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25; break; /* -25 */
501 case 0xE3: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
502 case 0xE2: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30; break; /* -30 */
504 WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm->nDivision));
505 return MCIERR_INVALID_FILE;
507 switch (LOBYTE(wmm->nDivision)) {
508 case 4: /* MIDI Time Code */
511 case 80: /* SMPTE bit resolution */
514 WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm->nDivision));
515 return MCIERR_INVALID_FILE;
517 } else if (wmm->nDivision == 0) {
518 WARN("Number of division is 0, can't support that !!\n");
519 return MCIERR_INVALID_FILE;
521 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
524 switch (wmm->wFormat) {
526 if (wmm->nTracks != 1) {
527 WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
535 WARN("Handling MIDI files which format = %d is not (yet) supported\n"
536 "Please report with MIDI file !\n", wmm->wFormat);
537 return MCIERR_INVALID_FILE;
540 if (wmm->nTracks & 0x8000) {
541 /* this shouldn't be a problem... */
542 WARN("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
543 wmm->nTracks = 0x7FFF;
546 if ((wmm->tracks = HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK) * wmm->nTracks)) == NULL) {
547 return MCIERR_OUT_OF_MEMORY;
550 toberead -= 3 * sizeof(WORD);
552 TRACE("Size of MThd > 6, skipping %d extra bytes\n", toberead);
553 mmioSeek(wmm->hFile, toberead, SEEK_CUR);
556 for (nt = 0; nt < wmm->nTracks; nt++) {
557 wmm->tracks[nt].wTrackNr = nt;
558 if (MIDI_mciReadMTrk(wmm, &wmm->tracks[nt]) != 0) {
559 WARN("Can't read 'MTrk' header\n");
560 return MCIERR_INVALID_FILE;
564 wmm->dwTempo = 500000;
569 /**************************************************************************
570 * MIDI_ConvertPulseToMS [internal]
572 static DWORD MIDI_ConvertPulseToMS(WINE_MCIMIDI* wmm, DWORD pulse)
576 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
577 * may change during file playing
579 if (wmm->nDivision == 0) {
580 FIXME("Shouldn't happen. wmm->nDivision = 0\n");
581 } else if (wmm->nDivision > 0x8000) { /* SMPTE, unchecked FIXME? */
582 int nf = -(char)HIBYTE(wmm->nDivision); /* number of frames */
583 int nsf = LOBYTE(wmm->nDivision); /* number of sub-frames */
584 ret = (pulse * 1000) / (nf * nsf);
586 ret = (DWORD)((double)pulse * ((double)wmm->dwTempo / 1000) /
587 (double)wmm->nDivision);
591 TRACE("pulse=%u tempo=%u division=%u=0x%04x => ms=%u\n",
592 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
598 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
599 #define TIME_MS_IN_ONE_MINUTE (60*1000)
600 #define TIME_MS_IN_ONE_SECOND (1000)
602 /**************************************************************************
603 * MIDI_ConvertTimeFormatToMS [internal]
605 static DWORD MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI* wmm, DWORD val)
609 switch (wmm->dwMciTimeFormat) {
610 case MCI_FORMAT_MILLISECONDS:
613 case MCI_FORMAT_SMPTE_24:
615 (HIBYTE(HIWORD(val)) * 125) / 3 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
616 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
618 case MCI_FORMAT_SMPTE_25:
620 HIBYTE(HIWORD(val)) * 40 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
621 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
623 case MCI_FORMAT_SMPTE_30:
625 (HIBYTE(HIWORD(val)) * 100) / 3 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
626 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
629 WARN("Bad time format %u!\n", wmm->dwMciTimeFormat);
632 TRACE("val=%u=0x%08x [tf=%u] => ret=%u\n", val, val, wmm->dwMciTimeFormat, ret);
637 /**************************************************************************
638 * MIDI_ConvertMSToTimeFormat [internal]
640 static DWORD MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI* wmm, DWORD _val)
642 DWORD ret = 0, val = _val;
645 switch (wmm->dwMciTimeFormat) {
646 case MCI_FORMAT_MILLISECONDS:
649 case MCI_FORMAT_SMPTE_24:
650 case MCI_FORMAT_SMPTE_25:
651 case MCI_FORMAT_SMPTE_30:
652 h = val / TIME_MS_IN_ONE_HOUR;
653 m = (val -= h * TIME_MS_IN_ONE_HOUR) / TIME_MS_IN_ONE_MINUTE;
654 s = (val -= m * TIME_MS_IN_ONE_MINUTE) / TIME_MS_IN_ONE_SECOND;
655 switch (wmm->dwMciTimeFormat) {
656 case MCI_FORMAT_SMPTE_24:
657 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
658 f = (val * 3) / 125; val -= (f * 125) / 3;
660 case MCI_FORMAT_SMPTE_25:
661 /* one frame is 1000/25 ms long, 1000/25 == 40 */
662 f = val / 40; val -= f * 40;
664 case MCI_FORMAT_SMPTE_30:
665 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
666 f = (val * 3) / 100; val -= (f * 100) / 3;
669 FIXME("There must be some bad bad programmer\n");
672 /* val contains the number of ms which cannot make a complete frame */
673 /* FIXME: is this correct ? programs seem to be happy with that */
674 ret = (f << 24) | (s << 16) | (m << 8) | (h << 0);
677 WARN("Bad time format %u!\n", wmm->dwMciTimeFormat);
680 TRACE("val=%u [tf=%u] => ret=%u=0x%08x\n", _val, wmm->dwMciTimeFormat, ret, ret);
685 /**************************************************************************
686 * MIDI_GetMThdLengthMS [internal]
688 static DWORD MIDI_GetMThdLengthMS(WINE_MCIMIDI* wmm)
693 for (nt = 0; nt < wmm->nTracks; nt++) {
694 if (wmm->wFormat == 2) {
695 ret += wmm->tracks[nt].dwLength;
696 } else if (wmm->tracks[nt].dwLength > ret) {
697 ret = wmm->tracks[nt].dwLength;
700 /* FIXME: this is wrong if there is a tempo change inside the file */
701 return MIDI_ConvertPulseToMS(wmm, ret);
704 /**************************************************************************
705 * MIDI_mciOpen [internal]
707 static DWORD MIDI_mciOpen(UINT wDevID, DWORD dwFlags, LPMCI_OPEN_PARMSW lpParms)
711 WINE_MCIMIDI* wmm = (WINE_MCIMIDI*)mciGetDriverData(wDevID);
713 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
715 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
716 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
717 if (dwFlags & MCI_OPEN_SHAREABLE)
718 return MCIERR_HARDWARE;
720 if (wmm->nUseCount > 0) {
721 /* The driver is already opened on this channel
722 * MIDI sequencer cannot be shared
724 return MCIERR_DEVICE_OPEN;
730 wmm->wPort = MIDI_MAPPER;
731 wmm->lpstrElementName = NULL;
732 dwDeviceID = lpParms->wDeviceID;
734 TRACE("wDevID=%04X (lpParams->wDeviceID=%08X)\n", wDevID, dwDeviceID);
735 /* lpParms->wDeviceID = wDevID;*/
737 if (dwFlags & MCI_OPEN_ELEMENT) {
738 TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpParms->lpstrElementName));
739 if (lpParms->lpstrElementName && strlenW(lpParms->lpstrElementName) > 0) {
740 wmm->hFile = mmioOpenW((LPWSTR)lpParms->lpstrElementName, NULL,
741 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
742 if (wmm->hFile == 0) {
743 WARN("Can't find file %s!\n", debugstr_w(lpParms->lpstrElementName));
745 return MCIERR_FILE_NOT_FOUND;
747 wmm->lpstrElementName = HeapAlloc(GetProcessHeap(), 0,
748 (strlenW(lpParms->lpstrElementName) + 1) * sizeof(WCHAR));
749 strcpyW(wmm->lpstrElementName, lpParms->lpstrElementName);
752 TRACE("hFile=%p\n", wmm->hFile);
754 wmm->lpstrCopyright = NULL;
755 wmm->lpstrName = NULL;
757 wmm->wNotifyDeviceID = dwDeviceID;
758 wmm->dwStatus = MCI_MODE_NOT_READY; /* while loading file contents */
759 /* spec says it should be the default format from the MIDI file... */
760 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
762 if (wmm->hFile != 0) {
767 if (mmioDescend(wmm->hFile, &ckMainRIFF, NULL, 0) != 0) {
768 dwRet = MCIERR_INVALID_FILE;
770 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
771 (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, ckMainRIFF.cksize);
773 if (ckMainRIFF.ckid == FOURCC_RIFF && ckMainRIFF.fccType == mmioFOURCC('R', 'M', 'I', 'D')) {
774 mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
775 mmioSeek(wmm->hFile, ckMainRIFF.dwDataOffset + ((ckMainRIFF.cksize + 1) & ~1), SEEK_SET);
776 if (mmioDescend(wmm->hFile, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK) == 0) {
777 TRACE("... is a 'RMID' file\n");
778 dwOffset = mmckInfo.dwDataOffset;
780 dwRet = MCIERR_INVALID_FILE;
783 if (dwRet == 0 && MIDI_mciReadMThd(wmm, dwOffset) != 0) {
784 WARN("Can't read 'MThd' header\n");
785 dwRet = MCIERR_INVALID_FILE;
789 TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
797 mmioClose(wmm->hFile, 0);
800 wmm->dwPositionMS = 0;
801 wmm->dwStatus = MCI_MODE_STOP;
802 if (dwFlags & MCI_NOTIFY) {
803 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
804 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
805 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
811 /**************************************************************************
812 * MIDI_mciStop [internal]
814 static DWORD MIDI_mciStop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
816 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
818 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
820 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
822 if (wmm->dwStatus != MCI_MODE_STOP) {
823 int oldstat = wmm->dwStatus;
825 wmm->dwStatus = MCI_MODE_NOT_READY;
826 if (oldstat == MCI_MODE_PAUSE)
827 midiOutReset((HMIDIOUT)wmm->hMidi);
829 while (wmm->dwStatus != MCI_MODE_STOP)
834 wmm->dwStatus = MCI_MODE_STOP;
836 TRACE("wmm->dwStatus=%d\n", wmm->dwStatus);
838 if (lpParms && (dwFlags & MCI_NOTIFY)) {
839 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
840 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
841 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
846 /**************************************************************************
847 * MIDI_mciClose [internal]
849 static DWORD MIDI_mciClose(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
851 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
853 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
855 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
857 if (wmm->dwStatus != MCI_MODE_STOP) {
858 MIDI_mciStop(wDevID, MCI_WAIT, lpParms);
862 if (wmm->nUseCount == 0) {
863 if (wmm->hFile != 0) {
864 mmioClose(wmm->hFile, 0);
866 TRACE("hFile closed !\n");
868 HeapFree(GetProcessHeap(), 0, wmm->tracks);
869 HeapFree(GetProcessHeap(), 0, wmm->lpstrElementName);
870 HeapFree(GetProcessHeap(), 0, wmm->lpstrCopyright);
871 HeapFree(GetProcessHeap(), 0, wmm->lpstrName);
873 TRACE("Shouldn't happen... nUseCount=%d\n", wmm->nUseCount);
874 return MCIERR_INTERNAL;
877 if (lpParms && (dwFlags & MCI_NOTIFY)) {
878 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
879 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
880 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
885 /**************************************************************************
886 * MIDI_mciFindNextEvent [internal]
888 static MCI_MIDITRACK* MIDI_mciFindNextEvent(WINE_MCIMIDI* wmm, LPDWORD hiPulse)
893 *hiPulse = 0xFFFFFFFFul;
895 for (nt = 0; nt < wmm->nTracks; nt++) {
896 mmt = &wmm->tracks[nt];
898 if (mmt->wStatus == 0)
900 if (mmt->dwEventPulse < *hiPulse) {
901 *hiPulse = mmt->dwEventPulse;
905 return (cnt == 0xFFFFu) ? 0 /* no more event on all tracks */
909 /**************************************************************************
910 * MIDI_mciPlay [internal]
912 static DWORD MIDI_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
914 DWORD dwStartMS, dwEndMS;
919 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
921 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
923 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
925 if (wmm->hFile == 0) {
926 WARN("Can't play: no file %s!\n", debugstr_w(wmm->lpstrElementName));
927 return MCIERR_FILE_NOT_FOUND;
930 if (wmm->dwStatus != MCI_MODE_STOP) {
931 if (wmm->dwStatus == MCI_MODE_PAUSE) {
932 /* FIXME: parameters (start/end) in lpParams may not be used */
933 return MIDI_mciResume(wDevID, dwFlags, (LPMCI_GENERIC_PARMS)lpParms);
935 WARN("Can't play: device is not stopped !\n");
936 return MCIERR_INTERNAL;
939 if (!(dwFlags & MCI_WAIT)) {
940 /** FIXME: I'm not 100% sure that wNotifyDeviceID is the right value in all cases ??? */
941 return MCI_SendCommandAsync(wmm->wNotifyDeviceID, MCI_PLAY, dwFlags, (DWORD_PTR)lpParms, sizeof(LPMCI_PLAY_PARMS));
944 if (lpParms && (dwFlags & MCI_FROM)) {
945 dwStartMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwFrom);
947 dwStartMS = wmm->dwPositionMS;
950 if (lpParms && (dwFlags & MCI_TO)) {
951 dwEndMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
953 dwEndMS = 0xFFFFFFFFul;
956 TRACE("Playing from %u to %u\n", dwStartMS, dwEndMS);
959 for (nt = 0; nt < wmm->nTracks; nt++) {
960 mmt = &wmm->tracks[nt];
962 mmt->wStatus = 1; /* ok, playing */
963 mmt->dwIndex = mmt->dwFirst;
964 if (wmm->wFormat == 2 && nt > 0) {
965 mmt->dwEventPulse = wmm->tracks[nt - 1].dwLength;
967 mmt->dwEventPulse = 0;
969 MIDI_mciReadNextEvent(wmm, mmt); /* FIXME == 0 */
972 dwRet = midiOutOpen((LPHMIDIOUT)&wmm->hMidi, wmm->wPort, 0L, 0L, CALLBACK_NULL);
973 /* dwRet = midiInOpen(&wmm->hMidi, wmm->wPort, 0L, 0L, CALLBACK_NULL);*/
974 if (dwRet != MMSYSERR_NOERROR) {
979 wmm->dwTempo = 500000;
980 wmm->dwStatus = MCI_MODE_PLAY;
981 wmm->dwPositionMS = 0;
982 wmm->wStartedPlaying = FALSE;
984 while (wmm->dwStatus != MCI_MODE_STOP && wmm->dwStatus != MCI_MODE_NOT_READY) {
985 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
986 * too much. Tell gcc not to optimize status value using volatile.
988 while (((volatile WINE_MCIMIDI*)wmm)->dwStatus == MCI_MODE_PAUSE);
990 doPlay = (wmm->dwPositionMS >= dwStartMS && wmm->dwPositionMS <= dwEndMS);
992 TRACE("wmm->dwStatus=%d, doPlay=%c\n", wmm->dwStatus, doPlay ? 'T' : 'F');
994 if ((mmt = MIDI_mciFindNextEvent(wmm, &hiPulse)) == NULL)
995 break; /* no more event on tracks */
997 /* if starting playing, then set StartTicks to the value it would have had
998 * if play had started at position 0
1000 if (doPlay && !wmm->wStartedPlaying) {
1001 wmm->dwStartTicks = GetTickCount() - MIDI_ConvertPulseToMS(wmm, wmm->dwPulse);
1002 wmm->wStartedPlaying = TRUE;
1003 TRACE("Setting dwStartTicks to %u\n", wmm->dwStartTicks);
1006 if (hiPulse > wmm->dwPulse) {
1007 wmm->dwPositionMS += MIDI_ConvertPulseToMS(wmm, hiPulse - wmm->dwPulse);
1009 DWORD togo = wmm->dwStartTicks + wmm->dwPositionMS;
1010 DWORD tc = GetTickCount();
1012 TRACE("Pulses hi=0x%08x <> cur=0x%08x\n", hiPulse, wmm->dwPulse);
1013 TRACE("Wait until %u => %u ms\n",
1014 tc - wmm->dwStartTicks, togo - wmm->dwStartTicks);
1018 wmm->dwPulse = hiPulse;
1021 switch (LOBYTE(LOWORD(mmt->dwEventData))) {
1023 case 0xF7: /* sysex events */
1025 FIXME("Not handling SysEx events (yet)\n");
1029 /* position after meta data header */
1030 mmioSeek(wmm->hFile, mmt->dwIndex + HIWORD(mmt->dwEventData), SEEK_SET);
1031 switch (HIBYTE(LOWORD(mmt->dwEventData))) {
1032 case 0x00: /* 16-bit sequence number */
1033 if (TRACE_ON(mcimidi)) {
1036 MIDI_mciReadWord(wmm, &twd); /* == 0 */
1037 TRACE("Got sequence number %u\n", twd);
1040 case 0x01: /* any text */
1041 case 0x02: /* Copyright Message text */
1042 case 0x03: /* Sequence/Track Name text */
1043 case 0x04: /* Instrument Name text */
1044 case 0x05: /* Lyric text */
1045 case 0x06: /* Marker text */
1046 case 0x07: /* Cue-point text */
1047 if (TRACE_ON(mcimidi)) {
1049 WORD len = mmt->wEventLength - HIWORD(mmt->dwEventData);
1050 static const char* const info[8] = {"", "Text", "Copyright", "Seq/Trk name",
1051 "Instrument", "Lyric", "Marker", "Cue-point"};
1052 WORD idx = HIBYTE(LOWORD(mmt->dwEventData));
1054 if (len >= sizeof(buf)) {
1055 WARN("Buffer for text is too small (%u are needed)\n", len);
1056 len = sizeof(buf) - 1;
1058 if (mmioRead(wmm->hFile, buf, len) == len) {
1059 buf[len] = 0; /* end string in case */
1060 TRACE("%s => \"%s\"\n", (idx < 8 ) ? info[idx] : "", buf);
1062 WARN("Couldn't read data for %s\n", (idx < 8) ? info[idx] : "");
1067 /* MIDI channel (cc) */
1068 if (FIXME_ON(mcimidi)) {
1071 MIDI_mciReadByte(wmm, &bt); /* == 0 */
1072 FIXME("NIY: MIDI channel=%u, track=%u\n", bt, mmt->wTrackNr);
1076 /* MIDI port (pp) */
1077 if (FIXME_ON(mcimidi)) {
1080 MIDI_mciReadByte(wmm, &bt); /* == 0 */
1081 FIXME("NIY: MIDI port=%u, track=%u\n", bt, mmt->wTrackNr);
1084 case 0x2F: /* end of track */
1087 case 0x51:/* set tempo */
1088 /* Tempo is expressed in -seconds per midi quarter note
1089 * for format 1 MIDI files, this can only be present on track #0
1091 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
1092 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt->wTrackNr);
1097 MIDI_mciReadByte(wmm, &tbt); value = ((DWORD)tbt) << 16;
1098 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 8;
1099 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 0;
1100 TRACE("Setting tempo to %d (BPM=%d)\n", wmm->dwTempo, (value) ? (60000000 / value) : 0);
1101 wmm->dwTempo = value;
1104 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
1105 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
1106 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt->wTrackNr);
1107 } if (mmt->dwEventPulse != 0) {
1108 WARN("SMPTE track start can only be expressed at start of track (%u)\n", mmt->dwEventPulse);
1110 BYTE h, m, s, f, ff;
1112 MIDI_mciReadByte(wmm, &h);
1113 MIDI_mciReadByte(wmm, &m);
1114 MIDI_mciReadByte(wmm, &s);
1115 MIDI_mciReadByte(wmm, &f);
1116 MIDI_mciReadByte(wmm, &ff);
1117 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h, m, s, f, ff);
1120 case 0x58: /* file rhythm */
1121 if (TRACE_ON(mcimidi)) {
1122 BYTE num, den, cpmc, _32npqn;
1124 MIDI_mciReadByte(wmm, &num);
1125 MIDI_mciReadByte(wmm, &den); /* to notate e.g. 6/8 */
1126 MIDI_mciReadByte(wmm, &cpmc); /* number of MIDI clocks per metronome click */
1127 MIDI_mciReadByte(wmm, &_32npqn); /* number of notated 32nd notes per MIDI quarter note */
1129 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num, 1 << den, cpmc, _32npqn);
1132 case 0x59: /* key signature */
1133 if (TRACE_ON(mcimidi)) {
1136 MIDI_mciReadByte(wmm, &sf);
1137 MIDI_mciReadByte(wmm, &mm);
1139 if (sf >= 0x80) TRACE("%d flats\n", -(char)sf);
1140 else if (sf > 0) TRACE("%d sharps\n", (char)sf);
1141 else TRACE("Key of C\n");
1142 TRACE("Mode: %s\n", (mm == 0) ? "major" : "minor");
1146 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt->dwEventData)));
1152 dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1154 switch (LOBYTE(LOWORD(mmt->dwEventData)) & 0xF0) {
1160 dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1164 mmt->dwIndex += mmt->wEventLength;
1165 if (mmt->dwIndex < mmt->dwFirst || mmt->dwIndex >= mmt->dwLast) {
1169 MIDI_mciReadNextEvent(wmm, mmt);
1173 midiOutReset((HMIDIOUT)wmm->hMidi);
1175 dwRet = midiOutClose((HMIDIOUT)wmm->hMidi);
1176 /* to restart playing at beginning when it's over */
1177 wmm->dwPositionMS = 0;
1179 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1180 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1181 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1182 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1185 wmm->dwStatus = MCI_MODE_STOP;
1189 /**************************************************************************
1190 * MIDI_mciRecord [internal]
1192 static DWORD MIDI_mciRecord(UINT wDevID, DWORD dwFlags, LPMCI_RECORD_PARMS lpParms)
1196 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1198 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1200 if (wmm == 0) return MCIERR_INVALID_DEVICE_ID;
1202 if (wmm->hFile == 0) {
1203 WARN("Can't find file=%s!\n", debugstr_w(wmm->lpstrElementName));
1204 return MCIERR_FILE_NOT_FOUND;
1206 start = 1; end = 99999;
1207 if (lpParms && (dwFlags & MCI_FROM)) {
1208 start = lpParms->dwFrom;
1209 TRACE("MCI_FROM=%d\n", start);
1211 if (lpParms && (dwFlags & MCI_TO)) {
1212 end = lpParms->dwTo;
1213 TRACE("MCI_TO=%d\n", end);
1215 midiHdr.lpData = HeapAlloc(GetProcessHeap(), 0, 1200);
1216 if (!midiHdr.lpData)
1217 return MCIERR_OUT_OF_MEMORY;
1218 midiHdr.dwBufferLength = 1024;
1219 midiHdr.dwUser = 0L;
1220 midiHdr.dwFlags = 0L;
1221 midiInPrepareHeader((HMIDIIN)wmm->hMidi, &midiHdr, sizeof(MIDIHDR));
1222 TRACE("After MIDM_PREPARE\n");
1223 wmm->dwStatus = MCI_MODE_RECORD;
1224 /* FIXME: there is no buffer added */
1225 while (wmm->dwStatus != MCI_MODE_STOP) {
1226 TRACE("wmm->dwStatus=%p %d\n",
1227 &wmm->dwStatus, wmm->dwStatus);
1228 midiHdr.dwBytesRecorded = 0;
1229 midiInStart((HMIDIIN)wmm->hMidi);
1230 TRACE("midiInStart => dwBytesRecorded=%u\n", midiHdr.dwBytesRecorded);
1231 if (midiHdr.dwBytesRecorded == 0) break;
1233 TRACE("Before MIDM_UNPREPARE\n");
1234 midiInUnprepareHeader((HMIDIIN)wmm->hMidi, &midiHdr, sizeof(MIDIHDR));
1235 TRACE("After MIDM_UNPREPARE\n");
1236 HeapFree(GetProcessHeap(), 0, midiHdr.lpData);
1237 midiHdr.lpData = NULL;
1238 wmm->dwStatus = MCI_MODE_STOP;
1239 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1240 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1241 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1242 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1247 /**************************************************************************
1248 * MIDI_mciPause [internal]
1250 static DWORD MIDI_mciPause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1252 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1254 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1256 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1258 if (wmm->dwStatus == MCI_MODE_PLAY) {
1259 /* stop all notes */
1261 for (chn = 0; chn < 16; chn++)
1262 midiOutShortMsg((HMIDIOUT)(wmm->hMidi), 0x78B0 | chn);
1263 wmm->dwStatus = MCI_MODE_PAUSE;
1265 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1266 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1267 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1268 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1274 /**************************************************************************
1275 * MIDI_mciResume [internal]
1277 static DWORD MIDI_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1279 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1281 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1283 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1285 if (wmm->dwStatus == MCI_MODE_PAUSE) {
1286 wmm->wStartedPlaying = FALSE;
1287 wmm->dwStatus = MCI_MODE_PLAY;
1289 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1290 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1291 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1292 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1297 /**************************************************************************
1298 * MIDI_mciSet [internal]
1300 static DWORD MIDI_mciSet(UINT wDevID, DWORD dwFlags, LPMCI_SEQ_SET_PARMS lpParms)
1302 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1304 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1306 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1307 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1309 if (dwFlags & MCI_SET_TIME_FORMAT) {
1310 switch (lpParms->dwTimeFormat) {
1311 case MCI_FORMAT_MILLISECONDS:
1312 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1313 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
1315 case MCI_FORMAT_SMPTE_24:
1316 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1317 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24;
1319 case MCI_FORMAT_SMPTE_25:
1320 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1321 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25;
1323 case MCI_FORMAT_SMPTE_30:
1324 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1325 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30;
1328 WARN("Bad time format %u!\n", lpParms->dwTimeFormat);
1329 return MCIERR_BAD_TIME_FORMAT;
1332 if (dwFlags & MCI_SET_VIDEO) {
1333 TRACE("No support for video !\n");
1334 return MCIERR_UNSUPPORTED_FUNCTION;
1336 if (dwFlags & MCI_SET_DOOR_OPEN) {
1337 TRACE("No support for door open !\n");
1338 return MCIERR_UNSUPPORTED_FUNCTION;
1340 if (dwFlags & MCI_SET_DOOR_CLOSED) {
1341 TRACE("No support for door close !\n");
1342 return MCIERR_UNSUPPORTED_FUNCTION;
1344 if (dwFlags & MCI_SET_AUDIO) {
1345 if (dwFlags & MCI_SET_ON) {
1346 TRACE("MCI_SET_ON audio !\n");
1347 } else if (dwFlags & MCI_SET_OFF) {
1348 TRACE("MCI_SET_OFF audio !\n");
1350 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1351 return MCIERR_BAD_INTEGER;
1354 switch (lpParms->dwAudio)
1356 case MCI_SET_AUDIO_ALL: TRACE("MCI_SET_AUDIO_ALL !\n"); break;
1357 case MCI_SET_AUDIO_LEFT: TRACE("MCI_SET_AUDIO_LEFT !\n"); break;
1358 case MCI_SET_AUDIO_RIGHT: TRACE("MCI_SET_AUDIO_RIGHT !\n"); break;
1359 default: WARN("Unknown audio channel %u\n", lpParms->dwAudio); break;
1363 if (dwFlags & MCI_SEQ_SET_MASTER)
1364 TRACE("MCI_SEQ_SET_MASTER !\n");
1365 if (dwFlags & MCI_SEQ_SET_SLAVE)
1366 TRACE("MCI_SEQ_SET_SLAVE !\n");
1367 if (dwFlags & MCI_SEQ_SET_OFFSET)
1368 TRACE("MCI_SEQ_SET_OFFSET !\n");
1369 if (dwFlags & MCI_SEQ_SET_PORT) {
1370 TRACE("MCI_SEQ_SET_PORT = %d\n", lpParms->dwPort);
1371 if ((UINT16)lpParms->dwPort != (UINT16)MIDI_MAPPER &&
1372 (UINT16)lpParms->dwPort >= midiOutGetNumDevs())
1373 /* FIXME: input/output port distinction? */
1374 return MCIERR_SEQ_PORT_NONEXISTENT;
1375 /* FIXME: Native manages to swap the device while playing! */
1376 wmm->wPort = lpParms->dwPort;
1378 if (dwFlags & MCI_SEQ_SET_TEMPO)
1379 TRACE("MCI_SEQ_SET_TEMPO !\n");
1383 /**************************************************************************
1384 * MIDI_mciStatus [internal]
1386 static DWORD MIDI_mciStatus(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
1388 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1391 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1393 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1394 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1396 if (dwFlags & MCI_STATUS_ITEM) {
1397 switch (lpParms->dwItem) {
1398 case MCI_STATUS_CURRENT_TRACK:
1399 /* FIXME in Format 2 */
1400 lpParms->dwReturn = 1;
1401 TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms->dwReturn);
1403 case MCI_STATUS_LENGTH:
1404 if ((dwFlags & MCI_TRACK) && wmm->wFormat == 2) {
1405 if (lpParms->dwTrack >= wmm->nTracks)
1406 return MCIERR_BAD_INTEGER;
1407 /* FIXME: this is wrong if there is a tempo change inside the file */
1408 lpParms->dwReturn = MIDI_ConvertPulseToMS(wmm, wmm->tracks[lpParms->dwTrack].dwLength);
1410 lpParms->dwReturn = MIDI_GetMThdLengthMS(wmm);
1412 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm, lpParms->dwReturn);
1413 TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms->dwReturn);
1415 case MCI_STATUS_MODE:
1416 TRACE("MCI_STATUS_MODE => %u\n", wmm->dwStatus);
1417 lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwStatus, wmm->dwStatus);
1418 ret = MCI_RESOURCE_RETURNED;
1420 case MCI_STATUS_MEDIA_PRESENT:
1421 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1422 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1423 ret = MCI_RESOURCE_RETURNED;
1425 case MCI_STATUS_NUMBER_OF_TRACKS:
1426 lpParms->dwReturn = (wmm->wFormat == 2) ? wmm->nTracks : 1;
1427 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms->dwReturn);
1429 case MCI_STATUS_POSITION:
1430 /* FIXME: do I need to use MCI_TRACK ? */
1431 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm,
1432 (dwFlags & MCI_STATUS_START) ? 0 : wmm->dwPositionMS);
1433 TRACE("MCI_STATUS_POSITION %s => %lu\n",
1434 (dwFlags & MCI_STATUS_START) ? "start" : "current", lpParms->dwReturn);
1436 case MCI_STATUS_READY:
1437 lpParms->dwReturn = (wmm->dwStatus == MCI_MODE_NOT_READY) ?
1438 MAKEMCIRESOURCE(FALSE, MCI_FALSE) : MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1439 ret = MCI_RESOURCE_RETURNED;
1440 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms->dwReturn));
1442 case MCI_STATUS_TIME_FORMAT:
1443 lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwMciTimeFormat, MCI_FORMAT_RETURN_BASE + wmm->dwMciTimeFormat);
1444 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms->dwReturn));
1445 ret = MCI_RESOURCE_RETURNED;
1447 case MCI_SEQ_STATUS_DIVTYPE:
1448 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1449 if (wmm->nDivision > 0x8000) {
1450 switch (wmm->nDivision) {
1451 case 0xE8: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_24; break; /* -24 */
1452 case 0xE7: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_25; break; /* -25 */
1453 case 0xE3: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
1454 case 0xE2: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30; break; /* -30 */
1455 default: FIXME("There is a bad bad programmer\n");
1458 lpParms->dwReturn = MCI_SEQ_DIV_PPQN;
1460 lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn,lpParms->dwReturn);
1461 ret = MCI_RESOURCE_RETURNED;
1463 case MCI_SEQ_STATUS_MASTER:
1464 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1465 lpParms->dwReturn = 0;
1467 case MCI_SEQ_STATUS_SLAVE:
1468 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1469 lpParms->dwReturn = 0;
1471 case MCI_SEQ_STATUS_OFFSET:
1472 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1473 lpParms->dwReturn = 0;
1475 case MCI_SEQ_STATUS_PORT:
1476 if (wmm->wPort != (UINT16)MIDI_MAPPER)
1477 lpParms->dwReturn = wmm->wPort;
1479 lpParms->dwReturn = MAKEMCIRESOURCE(MIDI_MAPPER, MCI_SEQ_MAPPER_S);
1480 ret = MCI_RESOURCE_RETURNED;
1482 TRACE("MCI_SEQ_STATUS_PORT (%u) => %d\n", wmm->wDevID, wmm->wPort);
1484 case MCI_SEQ_STATUS_TEMPO:
1485 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1486 lpParms->dwReturn = wmm->dwTempo;
1489 FIXME("Unknown command %08X !\n", lpParms->dwItem);
1490 return MCIERR_UNRECOGNIZED_COMMAND;
1493 WARN("No Status-Item!\n");
1494 return MCIERR_UNRECOGNIZED_COMMAND;
1496 if (dwFlags & MCI_NOTIFY) {
1497 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1498 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1499 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1504 /**************************************************************************
1505 * MIDI_mciGetDevCaps [internal]
1507 static DWORD MIDI_mciGetDevCaps(UINT wDevID, DWORD dwFlags,
1508 LPMCI_GETDEVCAPS_PARMS lpParms)
1510 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1513 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1515 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1516 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1518 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
1519 switch (lpParms->dwItem) {
1520 case MCI_GETDEVCAPS_DEVICE_TYPE:
1521 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1522 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER, MCI_DEVTYPE_SEQUENCER);
1523 ret = MCI_RESOURCE_RETURNED;
1525 case MCI_GETDEVCAPS_HAS_AUDIO:
1526 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1527 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1528 ret = MCI_RESOURCE_RETURNED;
1530 case MCI_GETDEVCAPS_HAS_VIDEO:
1531 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1532 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1533 ret = MCI_RESOURCE_RETURNED;
1535 case MCI_GETDEVCAPS_USES_FILES:
1536 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1537 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1538 ret = MCI_RESOURCE_RETURNED;
1540 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
1541 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1542 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1543 ret = MCI_RESOURCE_RETURNED;
1545 case MCI_GETDEVCAPS_CAN_EJECT:
1546 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1547 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1548 ret = MCI_RESOURCE_RETURNED;
1550 case MCI_GETDEVCAPS_CAN_PLAY:
1551 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1552 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1553 ret = MCI_RESOURCE_RETURNED;
1555 case MCI_GETDEVCAPS_CAN_RECORD:
1556 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1557 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1558 ret = MCI_RESOURCE_RETURNED;
1560 case MCI_GETDEVCAPS_CAN_SAVE:
1561 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1562 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1563 ret = MCI_RESOURCE_RETURNED;
1566 FIXME("Unknown capability (%08x) !\n", lpParms->dwItem);
1567 return MCIERR_UNRECOGNIZED_COMMAND;
1570 WARN("No GetDevCaps-Item !\n");
1571 return MCIERR_UNRECOGNIZED_COMMAND;
1576 /**************************************************************************
1577 * MIDI_mciInfo [internal]
1579 static DWORD MIDI_mciInfo(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
1582 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1584 static const WCHAR wszMidiSeq[] = {'W','i','n','e','\'','s',' ','M','I','D','I',' ','s','e','q','u','e','n','c','e','r',0};
1586 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1588 if (lpParms == NULL || lpParms->lpstrReturn == NULL)
1589 return MCIERR_NULL_PARAMETER_BLOCK;
1590 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1592 TRACE("buf=%p, len=%u\n", lpParms->lpstrReturn, lpParms->dwRetSize);
1594 switch (dwFlags & ~(MCI_WAIT|MCI_NOTIFY)) {
1595 case MCI_INFO_PRODUCT: str = wszMidiSeq; break;
1596 case MCI_INFO_FILE: str = wmm->lpstrElementName; break;
1597 case MCI_INFO_COPYRIGHT: str = wmm->lpstrCopyright; break;
1598 case MCI_INFO_NAME: str = wmm->lpstrName; break;
1600 WARN("Don't know this info command (%u)\n", dwFlags);
1601 return MCIERR_UNRECOGNIZED_COMMAND;
1604 if (lpParms->dwRetSize <= strlenW(str)) {
1605 lstrcpynW(lpParms->lpstrReturn, str, lpParms->dwRetSize - 1);
1606 ret = MCIERR_PARAM_OVERFLOW;
1608 strcpyW(lpParms->lpstrReturn, str);
1611 *lpParms->lpstrReturn = 0;
1616 /**************************************************************************
1617 * MIDI_mciSeek [internal]
1619 static DWORD MIDI_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1622 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1624 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1626 if (lpParms == NULL) {
1627 ret = MCIERR_NULL_PARAMETER_BLOCK;
1628 } else if (wmm == NULL) {
1629 ret = MCIERR_INVALID_DEVICE_ID;
1631 MIDI_mciStop(wDevID, MCI_WAIT, 0);
1633 if (dwFlags & MCI_SEEK_TO_START) {
1634 wmm->dwPositionMS = 0;
1635 } else if (dwFlags & MCI_SEEK_TO_END) {
1636 wmm->dwPositionMS = 0xFFFFFFFF; /* FIXME */
1637 } else if (dwFlags & MCI_TO) {
1638 wmm->dwPositionMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
1640 WARN("dwFlag doesn't tell where to seek to...\n");
1641 return MCIERR_MISSING_PARAMETER;
1644 TRACE("Seeking to position=%u ms\n", wmm->dwPositionMS);
1646 if (dwFlags & MCI_NOTIFY) {
1647 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1648 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1649 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1655 /*======================================================================*
1656 * MIDI entry points *
1657 *======================================================================*/
1659 /**************************************************************************
1660 * DriverProc (MCISEQ.@)
1662 LRESULT CALLBACK MCIMIDI_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1663 LPARAM dwParam1, LPARAM dwParam2)
1666 case DRV_LOAD: return 1;
1667 case DRV_FREE: return 1;
1668 case DRV_ENABLE: return 1;
1669 case DRV_DISABLE: return 1;
1670 case DRV_QUERYCONFIGURE: return 1;
1671 case DRV_CONFIGURE: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK); return 1;
1672 case DRV_INSTALL: return DRVCNF_RESTART;
1673 case DRV_REMOVE: return DRVCNF_RESTART;
1674 case DRV_OPEN: return MIDI_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1675 case DRV_CLOSE: return MIDI_drvClose(dwDevID);
1678 if (dwDevID == 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION;
1681 case MCI_OPEN_DRIVER: return MIDI_mciOpen (dwDevID, dwParam1, (LPMCI_OPEN_PARMSW) dwParam2);
1682 case MCI_CLOSE_DRIVER: return MIDI_mciClose (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1683 case MCI_PLAY: return MIDI_mciPlay (dwDevID, dwParam1, (LPMCI_PLAY_PARMS) dwParam2);
1684 case MCI_RECORD: return MIDI_mciRecord (dwDevID, dwParam1, (LPMCI_RECORD_PARMS) dwParam2);
1685 case MCI_STOP: return MIDI_mciStop (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1686 case MCI_SET: return MIDI_mciSet (dwDevID, dwParam1, (LPMCI_SEQ_SET_PARMS) dwParam2);
1687 case MCI_PAUSE: return MIDI_mciPause (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1688 case MCI_RESUME: return MIDI_mciResume (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1689 case MCI_STATUS: return MIDI_mciStatus (dwDevID, dwParam1, (LPMCI_STATUS_PARMS) dwParam2);
1690 case MCI_GETDEVCAPS: return MIDI_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1691 case MCI_INFO: return MIDI_mciInfo (dwDevID, dwParam1, (LPMCI_INFO_PARMSW) dwParam2);
1692 case MCI_SEEK: return MIDI_mciSeek (dwDevID, dwParam1, (LPMCI_SEEK_PARMS) dwParam2);
1693 /* commands that should be supported */
1709 WARN("Unsupported command [%u]\n", wMsg);
1711 /* commands that should report an error */
1713 TRACE("Unsupported command [%u]\n", wMsg);
1717 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1720 TRACE("Sending msg [%u] to default driver proc\n", wMsg);
1721 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1723 return MCIERR_UNRECOGNIZED_COMMAND;