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 wNotifyDeviceID; /* MCI device ID with a pending notification */
66 HANDLE hCallback; /* Callback handle for pending notification */
67 HMMIO hFile; /* mmio file handle open as Element */
68 LPWSTR lpstrElementName; /* Name of file (if any) */
69 LPWSTR lpstrCopyright;
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 = (struct 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);
117 WARN("Should not happen ? what's wrong\n");
118 /* should not go after this point */
122 /**************************************************************************
123 * MCI_SendCommandAsync [internal]
125 static DWORD MCI_SendCommandAsync(UINT wDevID, UINT wMsg, DWORD_PTR dwParam1,
126 DWORD_PTR dwParam2, UINT size)
129 struct SCA* sca = HeapAlloc(GetProcessHeap(), 0, sizeof(struct SCA) + size);
132 return MCIERR_OUT_OF_MEMORY;
134 sca->wDevID = wDevID;
136 sca->dwParam1 = dwParam1;
138 if (size && dwParam2) {
139 sca->dwParam2 = (DWORD_PTR)sca + sizeof(struct SCA);
140 /* copy structure passed by program in dwParam2 to be sure
141 * we can still use it whatever the program does
143 memcpy((LPVOID)sca->dwParam2, (LPVOID)dwParam2, size);
145 sca->dwParam2 = dwParam2;
148 if ((handle = CreateThread(NULL, 0, MCI_SCAStarter, sca, 0, NULL)) == 0) {
149 WARN("Couldn't allocate thread for async command handling, sending synchronously\n");
150 return MCI_SCAStarter(&sca);
152 SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL);
157 /*======================================================================*
158 * MCI MIDI implementation *
159 *======================================================================*/
161 static DWORD MIDI_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms);
163 /**************************************************************************
164 * MIDI_drvOpen [internal]
166 static DWORD MIDI_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
170 if (!modp) return 0xFFFFFFFF;
172 wmm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MCIMIDI));
177 wmm->wDevID = modp->wDeviceID;
178 mciSetDriverData(wmm->wDevID, (DWORD_PTR)wmm);
179 modp->wCustomCommandTable = MCI_NO_COMMAND_TABLE;
180 modp->wType = MCI_DEVTYPE_SEQUENCER;
181 return modp->wDeviceID;
184 /**************************************************************************
185 * MCIMIDI_drvClose [internal]
187 static DWORD MIDI_drvClose(DWORD dwDevID)
189 WINE_MCIMIDI* wmm = (WINE_MCIMIDI*)mciGetDriverData(dwDevID);
192 HeapFree(GetProcessHeap(), 0, wmm);
193 mciSetDriverData(dwDevID, 0);
196 return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
199 /**************************************************************************
200 * MIDI_mciGetOpenDev [internal]
202 static WINE_MCIMIDI* MIDI_mciGetOpenDev(UINT wDevID)
204 WINE_MCIMIDI* wmm = (WINE_MCIMIDI*)mciGetDriverData(wDevID);
206 if (wmm == NULL || wmm->nUseCount == 0) {
207 WARN("Invalid wDevID=%u\n", wDevID);
213 /**************************************************************************
214 * MIDI_mciReadByte [internal]
216 static DWORD MIDI_mciReadByte(WINE_MCIMIDI* wmm, BYTE *lpbyt)
221 mmioRead(wmm->hFile, (HPSTR)lpbyt, sizeof(BYTE)) != (long)sizeof(BYTE)) {
222 WARN("Error reading wmm=%p\n", wmm);
223 ret = MCIERR_INVALID_FILE;
229 /**************************************************************************
230 * MIDI_mciReadWord [internal]
232 static DWORD MIDI_mciReadWord(WINE_MCIMIDI* wmm, LPWORD lpw)
235 DWORD ret = MCIERR_INVALID_FILE;
238 MIDI_mciReadByte(wmm, &hibyte) == 0 &&
239 MIDI_mciReadByte(wmm, &lobyte) == 0) {
240 *lpw = ((WORD)hibyte << 8) + lobyte;
246 /**************************************************************************
247 * MIDI_mciReadLong [internal]
249 static DWORD MIDI_mciReadLong(WINE_MCIMIDI* wmm, LPDWORD lpdw)
252 DWORD ret = MCIERR_INVALID_FILE;
255 MIDI_mciReadWord(wmm, &hiword) == 0 &&
256 MIDI_mciReadWord(wmm, &loword) == 0) {
257 *lpdw = MAKELONG(loword, hiword);
263 /**************************************************************************
264 * MIDI_mciReadVaryLen [internal]
266 static WORD MIDI_mciReadVaryLen(WINE_MCIMIDI* wmm, LPDWORD lpdw)
273 ret = MCIERR_INVALID_FILE;
276 if (MIDI_mciReadByte(wmm, &byte) != 0) {
279 value = (value << 7) + (byte & 0x7F);
281 } while (byte & 0x80);
284 TRACE("val=%08X\n", value);
290 /**************************************************************************
291 * MIDI_mciReadNextEvent [internal]
293 static DWORD MIDI_mciReadNextEvent(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
301 if (mmioSeek(wmm->hFile, mmt->dwIndex, SEEK_SET) != mmt->dwIndex) {
302 WARN("Can't seek at %08X\n", mmt->dwIndex);
303 return MCIERR_INVALID_FILE;
305 evtLength = MIDI_mciReadVaryLen(wmm, &evtPulse) + 1; /* > 0 */
306 MIDI_mciReadByte(wmm, &b1);
310 evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
314 MIDI_mciReadByte(wmm, &b2); evtLength++;
316 evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
317 if (evtLength >= 0x10000u) {
318 /* this limitation shouldn't be a problem */
319 WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
322 hw = LOWORD(evtLength);
327 if (b1 & 0x80) { /* use running status ? */
328 mmt->wLastCommand = b1;
329 MIDI_mciReadByte(wmm, &b2); evtLength++;
332 b1 = mmt->wLastCommand;
334 switch ((b1 >> 4) & 0x07) {
335 case 0: case 1: case 2: case 3: case 6:
336 MIDI_mciReadByte(wmm, &b3); evtLength++;
342 WARN("Strange indeed b1=0x%02x\n", b1);
346 if (mmt->dwIndex + evtLength > mmt->dwLast)
347 return MCIERR_INTERNAL;
349 mmt->dwEventPulse += evtPulse;
350 mmt->dwEventData = (hw << 16) + (b2 << 8) + b1;
351 mmt->wEventLength = evtLength;
354 TRACE("[%u] => pulse=%08x(%08x), data=%08x, length=%u\n",
355 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
356 mmt->dwEventData, mmt->wEventLength);
361 /**************************************************************************
362 * MIDI_mciReadMTrk [internal]
364 static DWORD MIDI_mciReadMTrk(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
369 if (mmioRead(wmm->hFile, (HPSTR)&fourcc, (long)sizeof(FOURCC)) !=
370 (long)sizeof(FOURCC)) {
371 return MCIERR_INVALID_FILE;
374 if (fourcc != mmioFOURCC('M', 'T', 'r', 'k')) {
375 WARN("Can't synchronize on 'MTrk' !\n");
376 return MCIERR_INVALID_FILE;
379 if (MIDI_mciReadLong(wmm, &toberead) != 0) {
380 return MCIERR_INVALID_FILE;
382 mmt->dwFirst = mmioSeek(wmm->hFile, 0, SEEK_CUR); /* >= 0 */
383 mmt->dwLast = mmt->dwFirst + toberead;
385 /* compute # of pulses in this track */
386 mmt->dwIndex = mmt->dwFirst;
387 mmt->dwEventPulse = 0;
389 while (MIDI_mciReadNextEvent(wmm, mmt) == 0 && LOWORD(mmt->dwEventData) != 0x2FFF) {
393 mmt->dwIndex += mmt->wEventLength;
395 switch (LOWORD(mmt->dwEventData)) {
398 /* position after meta data header */
399 mmioSeek(wmm->hFile, mmt->dwIndex + HIWORD(mmt->dwEventData), SEEK_SET);
400 len = mmt->wEventLength - HIWORD(mmt->dwEventData);
402 if (len >= sizeof(buf)) {
403 WARN("Buffer for text is too small (%u are needed)\n", len);
404 len = sizeof(buf) - 1;
406 if (mmioRead(wmm->hFile, (HPSTR)buf, len) == len) {
407 buf[len] = 0; /* end string in case */
408 switch (HIBYTE(LOWORD(mmt->dwEventData))) {
410 if (wmm->lpstrCopyright) {
411 WARN("Two copyright notices (%s|%s)\n", debugstr_w(wmm->lpstrCopyright), buf);
413 len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
414 wmm->lpstrCopyright = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
415 len = MultiByteToWideChar( CP_ACP, 0, buf, -1, wmm->lpstrCopyright, len );
419 if (wmm->lpstrName) {
420 WARN("Two names (%s|%s)\n", debugstr_w(wmm->lpstrName), buf);
422 len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
423 wmm->lpstrName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
424 len = MultiByteToWideChar( CP_ACP, 0, buf, -1, wmm->lpstrName, len );
432 mmt->dwLength = mmt->dwEventPulse;
434 TRACE("Track %u has %u bytes and %u pulses\n", mmt->wTrackNr, toberead, mmt->dwLength);
436 /* reset track data */
437 mmt->wStatus = 1; /* ok, playing */
438 mmt->dwIndex = mmt->dwFirst;
439 mmt->dwEventPulse = 0;
441 if (mmioSeek(wmm->hFile, 0, SEEK_CUR) != mmt->dwLast) {
442 WARN("Ouch, out of sync seek=%u track=%u\n",
443 mmioSeek(wmm->hFile, 0, SEEK_CUR), mmt->dwLast);
444 /* position at end of this track, to be ready to read next track */
445 mmioSeek(wmm->hFile, mmt->dwLast, SEEK_SET);
451 /**************************************************************************
452 * MIDI_mciReadMThd [internal]
454 static DWORD MIDI_mciReadMThd(WINE_MCIMIDI* wmm, DWORD dwOffset)
460 TRACE("(%p, %08X);\n", wmm, dwOffset);
462 if (mmioSeek(wmm->hFile, dwOffset, SEEK_SET) != dwOffset) {
463 WARN("Can't seek at %08X begin of 'MThd'\n", dwOffset);
464 return MCIERR_INVALID_FILE;
466 if (mmioRead(wmm->hFile, (HPSTR)&fourcc,
467 (long) sizeof(FOURCC)) != (long) sizeof(FOURCC))
468 return MCIERR_INVALID_FILE;
470 if (fourcc != mmioFOURCC('M', 'T', 'h', 'd')) {
471 WARN("Can't synchronize on 'MThd' !\n");
472 return MCIERR_INVALID_FILE;
475 if (MIDI_mciReadLong(wmm, &toberead) != 0 || toberead < 3 * sizeof(WORD))
476 return MCIERR_INVALID_FILE;
478 if (MIDI_mciReadWord(wmm, &wmm->wFormat) != 0 ||
479 MIDI_mciReadWord(wmm, &wmm->nTracks) != 0 ||
480 MIDI_mciReadWord(wmm, &wmm->nDivision) != 0) {
481 return MCIERR_INVALID_FILE;
484 TRACE("toberead=0x%08X, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
485 toberead, wmm->wFormat, wmm->nTracks, wmm->nDivision);
487 /* MS doc says that the MIDI MCI time format must be put by default to the format
488 * stored in the MIDI file...
490 if (wmm->nDivision > 0x8000) {
491 /* eric.pouech@lemel.fr 98/11
492 * I did not check this very code (pulses are expressed as SMPTE sub-frames).
493 * In about 40 MB of MIDI files I have, none was SMPTE based...
494 * I'm just wondering if this is widely used :-). So, if someone has one of
495 * these files, I'd like to know about it.
497 FIXME("Handling SMPTE time in MIDI files has not been tested\n"
498 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
500 switch (HIBYTE(wmm->nDivision)) {
501 case 0xE8: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24; break; /* -24 */
502 case 0xE7: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25; break; /* -25 */
503 case 0xE3: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
504 case 0xE2: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30; break; /* -30 */
506 WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm->nDivision));
507 return MCIERR_INVALID_FILE;
509 switch (LOBYTE(wmm->nDivision)) {
510 case 4: /* MIDI Time Code */
513 case 80: /* SMPTE bit resolution */
516 WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm->nDivision));
517 return MCIERR_INVALID_FILE;
519 } else if (wmm->nDivision == 0) {
520 WARN("Number of division is 0, can't support that !!\n");
521 return MCIERR_INVALID_FILE;
523 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
526 switch (wmm->wFormat) {
528 if (wmm->nTracks != 1) {
529 WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
537 WARN("Handling MIDI files which format = %d is not (yet) supported\n"
538 "Please report with MIDI file !\n", wmm->wFormat);
539 return MCIERR_INVALID_FILE;
542 if (wmm->nTracks & 0x8000) {
543 /* this shouldn't be a problem... */
544 WARN("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
545 wmm->nTracks = 0x7FFF;
548 if ((wmm->tracks = HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK) * wmm->nTracks)) == NULL) {
549 return MCIERR_OUT_OF_MEMORY;
552 toberead -= 3 * sizeof(WORD);
554 TRACE("Size of MThd > 6, skipping %d extra bytes\n", toberead);
555 mmioSeek(wmm->hFile, toberead, SEEK_CUR);
558 for (nt = 0; nt < wmm->nTracks; nt++) {
559 wmm->tracks[nt].wTrackNr = nt;
560 if (MIDI_mciReadMTrk(wmm, &wmm->tracks[nt]) != 0) {
561 WARN("Can't read 'MTrk' header\n");
562 return MCIERR_INVALID_FILE;
566 wmm->dwTempo = 500000;
571 /**************************************************************************
572 * MIDI_ConvertPulseToMS [internal]
574 static DWORD MIDI_ConvertPulseToMS(WINE_MCIMIDI* wmm, DWORD pulse)
578 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
579 * may change during file playing
581 if (wmm->nDivision == 0) {
582 FIXME("Shouldn't happen. wmm->nDivision = 0\n");
583 } else if (wmm->nDivision > 0x8000) { /* SMPTE, unchecked FIXME? */
584 int nf = -(char)HIBYTE(wmm->nDivision); /* number of frames */
585 int nsf = LOBYTE(wmm->nDivision); /* number of sub-frames */
586 ret = (pulse * 1000) / (nf * nsf);
588 ret = (DWORD)((double)pulse * ((double)wmm->dwTempo / 1000) /
589 (double)wmm->nDivision);
593 TRACE("pulse=%u tempo=%u division=%u=0x%04x => ms=%u\n",
594 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
600 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
601 #define TIME_MS_IN_ONE_MINUTE (60*1000)
602 #define TIME_MS_IN_ONE_SECOND (1000)
604 /**************************************************************************
605 * MIDI_ConvertTimeFormatToMS [internal]
607 static DWORD MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI* wmm, DWORD val)
611 switch (wmm->dwMciTimeFormat) {
612 case MCI_FORMAT_MILLISECONDS:
615 case MCI_FORMAT_SMPTE_24:
617 (HIBYTE(HIWORD(val)) * 125) / 3 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
618 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
620 case MCI_FORMAT_SMPTE_25:
622 HIBYTE(HIWORD(val)) * 40 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
623 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
625 case MCI_FORMAT_SMPTE_30:
627 (HIBYTE(HIWORD(val)) * 100) / 3 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
628 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
631 WARN("Bad time format %u!\n", wmm->dwMciTimeFormat);
634 TRACE("val=%u=0x%08x [tf=%u] => ret=%u\n", val, val, wmm->dwMciTimeFormat, ret);
639 /**************************************************************************
640 * MIDI_ConvertMSToTimeFormat [internal]
642 static DWORD MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI* wmm, DWORD _val)
644 DWORD ret = 0, val = _val;
647 switch (wmm->dwMciTimeFormat) {
648 case MCI_FORMAT_MILLISECONDS:
651 case MCI_FORMAT_SMPTE_24:
652 case MCI_FORMAT_SMPTE_25:
653 case MCI_FORMAT_SMPTE_30:
654 h = val / TIME_MS_IN_ONE_HOUR;
655 m = (val -= h * TIME_MS_IN_ONE_HOUR) / TIME_MS_IN_ONE_MINUTE;
656 s = (val -= m * TIME_MS_IN_ONE_MINUTE) / TIME_MS_IN_ONE_SECOND;
657 switch (wmm->dwMciTimeFormat) {
658 case MCI_FORMAT_SMPTE_24:
659 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
660 f = (val * 3) / 125; val -= (f * 125) / 3;
662 case MCI_FORMAT_SMPTE_25:
663 /* one frame is 1000/25 ms long, 1000/25 == 40 */
664 f = val / 40; val -= f * 40;
666 case MCI_FORMAT_SMPTE_30:
667 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
668 f = (val * 3) / 100; val -= (f * 100) / 3;
671 FIXME("There must be some bad bad programmer\n");
674 /* val contains the number of ms which cannot make a complete frame */
675 /* FIXME: is this correct ? programs seem to be happy with that */
676 ret = (f << 24) | (s << 16) | (m << 8) | (h << 0);
679 WARN("Bad time format %u!\n", wmm->dwMciTimeFormat);
682 TRACE("val=%u [tf=%u] => ret=%u=0x%08x\n", _val, wmm->dwMciTimeFormat, ret, ret);
687 /**************************************************************************
688 * MIDI_GetMThdLengthMS [internal]
690 static DWORD MIDI_GetMThdLengthMS(WINE_MCIMIDI* wmm)
695 for (nt = 0; nt < wmm->nTracks; nt++) {
696 if (wmm->wFormat == 2) {
697 ret += wmm->tracks[nt].dwLength;
698 } else if (wmm->tracks[nt].dwLength > ret) {
699 ret = wmm->tracks[nt].dwLength;
702 /* FIXME: this is wrong if there is a tempo change inside the file */
703 return MIDI_ConvertPulseToMS(wmm, ret);
706 /**************************************************************************
707 * MIDI_mciOpen [internal]
709 static DWORD MIDI_mciOpen(UINT wDevID, DWORD dwFlags, LPMCI_OPEN_PARMSW lpParms)
713 WINE_MCIMIDI* wmm = (WINE_MCIMIDI*)mciGetDriverData(wDevID);
715 TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
717 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
718 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
719 if (dwFlags & MCI_OPEN_SHAREABLE)
720 return MCIERR_HARDWARE;
722 if (wmm->nUseCount > 0) {
723 /* The driver is already opened on this channel
724 * MIDI sequencer cannot be shared
726 return MCIERR_DEVICE_OPEN;
732 wmm->lpstrElementName = NULL;
733 dwDeviceID = lpParms->wDeviceID;
735 TRACE("wDevID=%04X (lpParams->wDeviceID=%08X)\n", wDevID, dwDeviceID);
736 /* lpParms->wDeviceID = wDevID;*/
738 if (dwFlags & MCI_OPEN_ELEMENT) {
739 TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpParms->lpstrElementName));
740 if (lpParms->lpstrElementName && strlenW(lpParms->lpstrElementName) > 0) {
741 wmm->hFile = mmioOpenW((LPWSTR)lpParms->lpstrElementName, NULL,
742 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
743 if (wmm->hFile == 0) {
744 WARN("Can't find file %s!\n", debugstr_w(lpParms->lpstrElementName));
746 return MCIERR_FILE_NOT_FOUND;
748 wmm->lpstrElementName = HeapAlloc(GetProcessHeap(), 0,
749 (strlenW(lpParms->lpstrElementName) + 1) * sizeof(WCHAR));
750 strcpyW(wmm->lpstrElementName, lpParms->lpstrElementName);
753 TRACE("hFile=%p\n", wmm->hFile);
755 wmm->lpstrCopyright = NULL;
756 wmm->lpstrName = NULL;
758 wmm->wNotifyDeviceID = dwDeviceID;
759 wmm->dwStatus = MCI_MODE_NOT_READY; /* while loading file contents */
760 /* spec says it should be the default format from the MIDI file... */
761 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
763 if (wmm->hFile != 0) {
768 if (mmioDescend(wmm->hFile, &ckMainRIFF, NULL, 0) != 0) {
769 dwRet = MCIERR_INVALID_FILE;
771 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
772 (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, ckMainRIFF.cksize);
774 if (ckMainRIFF.ckid == FOURCC_RIFF && ckMainRIFF.fccType == mmioFOURCC('R', 'M', 'I', 'D')) {
775 mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
776 mmioSeek(wmm->hFile, ckMainRIFF.dwDataOffset + ((ckMainRIFF.cksize + 1) & ~1), SEEK_SET);
777 if (mmioDescend(wmm->hFile, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK) == 0) {
778 TRACE("... is a 'RMID' file\n");
779 dwOffset = mmckInfo.dwDataOffset;
781 dwRet = MCIERR_INVALID_FILE;
784 if (dwRet == 0 && MIDI_mciReadMThd(wmm, dwOffset) != 0) {
785 WARN("Can't read 'MThd' header\n");
786 dwRet = MCIERR_INVALID_FILE;
790 TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
798 mmioClose(wmm->hFile, 0);
801 wmm->dwPositionMS = 0;
802 wmm->dwStatus = MCI_MODE_STOP;
807 /**************************************************************************
808 * MIDI_mciStop [internal]
810 static DWORD MIDI_mciStop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
812 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
815 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
817 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
819 if (wmm->dwStatus != MCI_MODE_STOP) {
820 int oldstat = wmm->dwStatus;
822 wmm->dwStatus = MCI_MODE_NOT_READY;
823 if (oldstat == MCI_MODE_PAUSE)
824 dwRet = midiOutReset((HMIDIOUT)wmm->hMidi);
826 while (wmm->dwStatus != MCI_MODE_STOP)
831 wmm->dwStatus = MCI_MODE_STOP;
833 TRACE("wmm->dwStatus=%d\n", wmm->dwStatus);
835 if (lpParms && (dwFlags & MCI_NOTIFY)) {
836 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
837 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
838 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
843 /**************************************************************************
844 * MIDI_mciClose [internal]
846 static DWORD MIDI_mciClose(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
848 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
850 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
852 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
854 if (wmm->dwStatus != MCI_MODE_STOP) {
855 MIDI_mciStop(wDevID, MCI_WAIT, lpParms);
859 if (wmm->nUseCount == 0) {
860 if (wmm->hFile != 0) {
861 mmioClose(wmm->hFile, 0);
863 TRACE("hFile closed !\n");
865 HeapFree(GetProcessHeap(), 0, wmm->tracks);
866 HeapFree(GetProcessHeap(), 0, (LPWSTR)wmm->lpstrElementName);
867 HeapFree(GetProcessHeap(), 0, (LPSTR)wmm->lpstrCopyright);
868 HeapFree(GetProcessHeap(), 0, (LPSTR)wmm->lpstrName);
870 TRACE("Shouldn't happen... nUseCount=%d\n", wmm->nUseCount);
871 return MCIERR_INTERNAL;
874 if (lpParms && (dwFlags & MCI_NOTIFY)) {
875 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
876 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
877 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
882 /**************************************************************************
883 * MIDI_mciFindNextEvent [internal]
885 static MCI_MIDITRACK* MIDI_mciFindNextEvent(WINE_MCIMIDI* wmm, LPDWORD hiPulse)
890 *hiPulse = 0xFFFFFFFFul;
892 for (nt = 0; nt < wmm->nTracks; nt++) {
893 mmt = &wmm->tracks[nt];
895 if (mmt->wStatus == 0)
897 if (mmt->dwEventPulse < *hiPulse) {
898 *hiPulse = mmt->dwEventPulse;
902 return (cnt == 0xFFFFu) ? 0 /* no more event on all tracks */
906 /**************************************************************************
907 * MIDI_mciPlay [internal]
909 static DWORD MIDI_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
911 DWORD dwStartMS, dwEndMS;
916 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
918 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
920 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
922 if (wmm->hFile == 0) {
923 WARN("Can't play: no file %s!\n", debugstr_w(wmm->lpstrElementName));
924 return MCIERR_FILE_NOT_FOUND;
927 if (wmm->dwStatus != MCI_MODE_STOP) {
928 if (wmm->dwStatus == MCI_MODE_PAUSE) {
929 /* FIXME: parameters (start/end) in lpParams may not be used */
930 return MIDI_mciResume(wDevID, dwFlags, (LPMCI_GENERIC_PARMS)lpParms);
932 WARN("Can't play: device is not stopped !\n");
933 return MCIERR_INTERNAL;
936 if (!(dwFlags & MCI_WAIT)) {
937 /** FIXME: I'm not 100% sure that wNotifyDeviceID is the right value in all cases ??? */
938 return MCI_SendCommandAsync(wmm->wNotifyDeviceID, MCI_PLAY, dwFlags, (DWORD_PTR)lpParms, sizeof(LPMCI_PLAY_PARMS));
941 if (lpParms && (dwFlags & MCI_FROM)) {
942 dwStartMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwFrom);
944 dwStartMS = wmm->dwPositionMS;
947 if (lpParms && (dwFlags & MCI_TO)) {
948 dwEndMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
950 dwEndMS = 0xFFFFFFFFul;
953 TRACE("Playing from %u to %u\n", dwStartMS, dwEndMS);
956 for (nt = 0; nt < wmm->nTracks; nt++) {
957 mmt = &wmm->tracks[nt];
959 mmt->wStatus = 1; /* ok, playing */
960 mmt->dwIndex = mmt->dwFirst;
961 if (wmm->wFormat == 2 && nt > 0) {
962 mmt->dwEventPulse = wmm->tracks[nt - 1].dwLength;
964 mmt->dwEventPulse = 0;
966 MIDI_mciReadNextEvent(wmm, mmt); /* FIXME == 0 */
969 dwRet = midiOutOpen((LPHMIDIOUT)&wmm->hMidi, MIDIMAPPER, 0L, 0L, CALLBACK_NULL);
970 /* dwRet = midiInOpen(&wmm->hMidi, MIDIMAPPER, 0L, 0L, CALLBACK_NULL);*/
971 if (dwRet != MMSYSERR_NOERROR) {
976 wmm->dwTempo = 500000;
977 wmm->dwStatus = MCI_MODE_PLAY;
978 wmm->dwPositionMS = 0;
979 wmm->wStartedPlaying = FALSE;
981 while (wmm->dwStatus != MCI_MODE_STOP && wmm->dwStatus != MCI_MODE_NOT_READY) {
982 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
983 * too much. Tell gcc not to optimize status value using volatile.
985 while (((volatile WINE_MCIMIDI*)wmm)->dwStatus == MCI_MODE_PAUSE);
987 doPlay = (wmm->dwPositionMS >= dwStartMS && wmm->dwPositionMS <= dwEndMS);
989 TRACE("wmm->dwStatus=%d, doPlay=%c\n", wmm->dwStatus, doPlay ? 'T' : 'F');
991 if ((mmt = MIDI_mciFindNextEvent(wmm, &hiPulse)) == NULL)
992 break; /* no more event on tracks */
994 /* if starting playing, then set StartTicks to the value it would have had
995 * if play had started at position 0
997 if (doPlay && !wmm->wStartedPlaying) {
998 wmm->dwStartTicks = GetTickCount() - MIDI_ConvertPulseToMS(wmm, wmm->dwPulse);
999 wmm->wStartedPlaying = TRUE;
1000 TRACE("Setting dwStartTicks to %u\n", wmm->dwStartTicks);
1003 if (hiPulse > wmm->dwPulse) {
1004 wmm->dwPositionMS += MIDI_ConvertPulseToMS(wmm, hiPulse - wmm->dwPulse);
1006 DWORD togo = wmm->dwStartTicks + wmm->dwPositionMS;
1007 DWORD tc = GetTickCount();
1009 TRACE("Pulses hi=0x%08x <> cur=0x%08x\n", hiPulse, wmm->dwPulse);
1010 TRACE("Wait until %u => %u ms\n",
1011 tc - wmm->dwStartTicks, togo - wmm->dwStartTicks);
1015 wmm->dwPulse = hiPulse;
1018 switch (LOBYTE(LOWORD(mmt->dwEventData))) {
1020 case 0xF7: /* sysex events */
1022 FIXME("Not handling SysEx events (yet)\n");
1026 /* position after meta data header */
1027 mmioSeek(wmm->hFile, mmt->dwIndex + HIWORD(mmt->dwEventData), SEEK_SET);
1028 switch (HIBYTE(LOWORD(mmt->dwEventData))) {
1029 case 0x00: /* 16-bit sequence number */
1030 if (TRACE_ON(mcimidi)) {
1033 MIDI_mciReadWord(wmm, &twd); /* == 0 */
1034 TRACE("Got sequence number %u\n", twd);
1037 case 0x01: /* any text */
1038 case 0x02: /* Copyright Message text */
1039 case 0x03: /* Sequence/Track Name text */
1040 case 0x04: /* Instrument Name text */
1041 case 0x05: /* Lyric text */
1042 case 0x06: /* Marker text */
1043 case 0x07: /* Cue-point text */
1044 if (TRACE_ON(mcimidi)) {
1046 WORD len = mmt->wEventLength - HIWORD(mmt->dwEventData);
1047 static const char* const info[8] = {"", "Text", "Copyright", "Seq/Trk name",
1048 "Instrument", "Lyric", "Marker", "Cue-point"};
1049 WORD idx = HIBYTE(LOWORD(mmt->dwEventData));
1051 if (len >= sizeof(buf)) {
1052 WARN("Buffer for text is too small (%u are needed)\n", len);
1053 len = sizeof(buf) - 1;
1055 if (mmioRead(wmm->hFile, (HPSTR)buf, len) == len) {
1056 buf[len] = 0; /* end string in case */
1057 TRACE("%s => \"%s\"\n", (idx < 8 ) ? info[idx] : "", buf);
1059 WARN("Couldn't read data for %s\n", (idx < 8) ? info[idx] : "");
1064 /* MIDI channel (cc) */
1065 if (FIXME_ON(mcimidi)) {
1068 MIDI_mciReadByte(wmm, &bt); /* == 0 */
1069 FIXME("NIY: MIDI channel=%u, track=%u\n", bt, mmt->wTrackNr);
1073 /* MIDI port (pp) */
1074 if (FIXME_ON(mcimidi)) {
1077 MIDI_mciReadByte(wmm, &bt); /* == 0 */
1078 FIXME("NIY: MIDI port=%u, track=%u\n", bt, mmt->wTrackNr);
1081 case 0x2F: /* end of track */
1084 case 0x51:/* set tempo */
1085 /* Tempo is expressed in -seconds per midi quarter note
1086 * for format 1 MIDI files, this can only be present on track #0
1088 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
1089 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt->wTrackNr);
1094 MIDI_mciReadByte(wmm, &tbt); value = ((DWORD)tbt) << 16;
1095 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 8;
1096 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 0;
1097 TRACE("Setting tempo to %d (BPM=%d)\n", wmm->dwTempo, (value) ? (60000000 / value) : 0);
1098 wmm->dwTempo = value;
1101 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
1102 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
1103 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt->wTrackNr);
1104 } if (mmt->dwEventPulse != 0) {
1105 WARN("SMPTE track start can only be expressed at start of track (%u)\n", mmt->dwEventPulse);
1107 BYTE h, m, s, f, ff;
1109 MIDI_mciReadByte(wmm, &h);
1110 MIDI_mciReadByte(wmm, &m);
1111 MIDI_mciReadByte(wmm, &s);
1112 MIDI_mciReadByte(wmm, &f);
1113 MIDI_mciReadByte(wmm, &ff);
1114 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h, m, s, f, ff);
1117 case 0x58: /* file rhythm */
1118 if (TRACE_ON(mcimidi)) {
1119 BYTE num, den, cpmc, _32npqn;
1121 MIDI_mciReadByte(wmm, &num);
1122 MIDI_mciReadByte(wmm, &den); /* to notate e.g. 6/8 */
1123 MIDI_mciReadByte(wmm, &cpmc); /* number of MIDI clocks per metronome click */
1124 MIDI_mciReadByte(wmm, &_32npqn); /* number of notated 32nd notes per MIDI quarter note */
1126 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num, 1 << den, cpmc, _32npqn);
1129 case 0x59: /* key signature */
1130 if (TRACE_ON(mcimidi)) {
1133 MIDI_mciReadByte(wmm, &sf);
1134 MIDI_mciReadByte(wmm, &mm);
1136 if (sf >= 0x80) TRACE("%d flats\n", -(char)sf);
1137 else if (sf > 0) TRACE("%d sharps\n", (char)sf);
1138 else TRACE("Key of C\n");
1139 TRACE("Mode: %s\n", (mm == 0) ? "major" : "minor");
1143 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt->dwEventData)));
1149 dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1151 switch (LOBYTE(LOWORD(mmt->dwEventData)) & 0xF0) {
1157 dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1161 mmt->dwIndex += mmt->wEventLength;
1162 if (mmt->dwIndex < mmt->dwFirst || mmt->dwIndex >= mmt->dwLast) {
1166 MIDI_mciReadNextEvent(wmm, mmt);
1170 midiOutReset((HMIDIOUT)wmm->hMidi);
1172 dwRet = midiOutClose((HMIDIOUT)wmm->hMidi);
1173 /* to restart playing at beginning when it's over */
1174 wmm->dwPositionMS = 0;
1176 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1177 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1178 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1179 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1182 wmm->dwStatus = MCI_MODE_STOP;
1186 /**************************************************************************
1187 * MIDI_mciRecord [internal]
1189 static DWORD MIDI_mciRecord(UINT wDevID, DWORD dwFlags, LPMCI_RECORD_PARMS lpParms)
1194 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1196 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1198 if (wmm == 0) return MCIERR_INVALID_DEVICE_ID;
1200 if (wmm->hFile == 0) {
1201 WARN("Can't find file=%s!\n", debugstr_w(wmm->lpstrElementName));
1202 return MCIERR_FILE_NOT_FOUND;
1204 start = 1; end = 99999;
1205 if (lpParms && (dwFlags & MCI_FROM)) {
1206 start = lpParms->dwFrom;
1207 TRACE("MCI_FROM=%d\n", start);
1209 if (lpParms && (dwFlags & MCI_TO)) {
1210 end = lpParms->dwTo;
1211 TRACE("MCI_TO=%d\n", end);
1213 midiHdr.lpData = HeapAlloc(GetProcessHeap(), 0, 1200);
1214 if (!midiHdr.lpData)
1215 return MCIERR_OUT_OF_MEMORY;
1216 midiHdr.dwBufferLength = 1024;
1217 midiHdr.dwUser = 0L;
1218 midiHdr.dwFlags = 0L;
1219 dwRet = midiInPrepareHeader((HMIDIIN)wmm->hMidi, &midiHdr, sizeof(MIDIHDR));
1220 TRACE("After MIDM_PREPARE\n");
1221 wmm->dwStatus = MCI_MODE_RECORD;
1222 /* FIXME: there is no buffer added */
1223 while (wmm->dwStatus != MCI_MODE_STOP) {
1224 TRACE("wmm->dwStatus=%p %d\n",
1225 &wmm->dwStatus, wmm->dwStatus);
1226 midiHdr.dwBytesRecorded = 0;
1227 dwRet = midiInStart((HMIDIIN)wmm->hMidi);
1228 TRACE("midiInStart => dwBytesRecorded=%u\n", midiHdr.dwBytesRecorded);
1229 if (midiHdr.dwBytesRecorded == 0) break;
1231 TRACE("Before MIDM_UNPREPARE\n");
1232 dwRet = midiInUnprepareHeader((HMIDIIN)wmm->hMidi, &midiHdr, sizeof(MIDIHDR));
1233 TRACE("After MIDM_UNPREPARE\n");
1234 HeapFree(GetProcessHeap(), 0, midiHdr.lpData);
1235 midiHdr.lpData = NULL;
1236 wmm->dwStatus = MCI_MODE_STOP;
1237 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1238 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1239 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1240 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1245 /**************************************************************************
1246 * MIDI_mciPause [internal]
1248 static DWORD MIDI_mciPause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1250 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1252 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1254 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1256 if (wmm->dwStatus == MCI_MODE_PLAY) {
1257 /* stop all notes */
1259 for (chn = 0; chn < 16; chn++)
1260 midiOutShortMsg((HMIDIOUT)(wmm->hMidi), 0x78B0 | chn);
1261 wmm->dwStatus = MCI_MODE_PAUSE;
1263 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1264 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1265 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1266 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1272 /**************************************************************************
1273 * MIDI_mciResume [internal]
1275 static DWORD MIDI_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1277 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1279 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1281 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1283 if (wmm->dwStatus == MCI_MODE_PAUSE) {
1284 wmm->wStartedPlaying = FALSE;
1285 wmm->dwStatus = MCI_MODE_PLAY;
1287 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1288 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1289 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1290 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1295 /**************************************************************************
1296 * MIDI_mciSet [internal]
1298 static DWORD MIDI_mciSet(UINT wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
1300 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1302 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1304 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1305 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1307 if (dwFlags & MCI_SET_TIME_FORMAT) {
1308 switch (lpParms->dwTimeFormat) {
1309 case MCI_FORMAT_MILLISECONDS:
1310 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1311 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
1313 case MCI_FORMAT_SMPTE_24:
1314 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1315 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24;
1317 case MCI_FORMAT_SMPTE_25:
1318 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1319 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25;
1321 case MCI_FORMAT_SMPTE_30:
1322 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1323 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30;
1326 WARN("Bad time format %u!\n", lpParms->dwTimeFormat);
1327 return MCIERR_BAD_TIME_FORMAT;
1330 if (dwFlags & MCI_SET_VIDEO) {
1331 TRACE("No support for video !\n");
1332 return MCIERR_UNSUPPORTED_FUNCTION;
1334 if (dwFlags & MCI_SET_DOOR_OPEN) {
1335 TRACE("No support for door open !\n");
1336 return MCIERR_UNSUPPORTED_FUNCTION;
1338 if (dwFlags & MCI_SET_DOOR_CLOSED) {
1339 TRACE("No support for door close !\n");
1340 return MCIERR_UNSUPPORTED_FUNCTION;
1342 if (dwFlags & MCI_SET_AUDIO) {
1343 if (dwFlags & MCI_SET_ON) {
1344 TRACE("MCI_SET_ON audio !\n");
1345 } else if (dwFlags & MCI_SET_OFF) {
1346 TRACE("MCI_SET_OFF audio !\n");
1348 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1349 return MCIERR_BAD_INTEGER;
1352 switch (lpParms->dwAudio)
1354 case MCI_SET_AUDIO_ALL: TRACE("MCI_SET_AUDIO_ALL !\n"); break;
1355 case MCI_SET_AUDIO_LEFT: TRACE("MCI_SET_AUDIO_LEFT !\n"); break;
1356 case MCI_SET_AUDIO_RIGHT: TRACE("MCI_SET_AUDIO_RIGHT !\n"); break;
1357 default: WARN("Unknown audio channel %u\n", lpParms->dwAudio); break;
1361 if (dwFlags & MCI_SEQ_SET_MASTER)
1362 TRACE("MCI_SEQ_SET_MASTER !\n");
1363 if (dwFlags & MCI_SEQ_SET_SLAVE)
1364 TRACE("MCI_SEQ_SET_SLAVE !\n");
1365 if (dwFlags & MCI_SEQ_SET_OFFSET)
1366 TRACE("MCI_SEQ_SET_OFFSET !\n");
1367 if (dwFlags & MCI_SEQ_SET_PORT)
1368 TRACE("MCI_SEQ_SET_PORT !\n");
1369 if (dwFlags & MCI_SEQ_SET_TEMPO)
1370 TRACE("MCI_SEQ_SET_TEMPO !\n");
1374 /**************************************************************************
1375 * MIDI_mciStatus [internal]
1377 static DWORD MIDI_mciStatus(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
1379 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1382 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1384 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1385 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1387 if (dwFlags & MCI_STATUS_ITEM) {
1388 switch (lpParms->dwItem) {
1389 case MCI_STATUS_CURRENT_TRACK:
1390 /* FIXME in Format 2 */
1391 lpParms->dwReturn = 1;
1392 TRACE("MCI_STATUS_CURRENT_TRACK => %u\n", lpParms->dwReturn);
1394 case MCI_STATUS_LENGTH:
1395 if ((dwFlags & MCI_TRACK) && wmm->wFormat == 2) {
1396 if (lpParms->dwTrack >= wmm->nTracks)
1397 return MCIERR_BAD_INTEGER;
1398 /* FIXME: this is wrong if there is a tempo change inside the file */
1399 lpParms->dwReturn = MIDI_ConvertPulseToMS(wmm, wmm->tracks[lpParms->dwTrack].dwLength);
1401 lpParms->dwReturn = MIDI_GetMThdLengthMS(wmm);
1403 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm, lpParms->dwReturn);
1404 TRACE("MCI_STATUS_LENGTH => %u\n", lpParms->dwReturn);
1406 case MCI_STATUS_MODE:
1407 TRACE("MCI_STATUS_MODE => %u\n", wmm->dwStatus);
1408 lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwStatus, wmm->dwStatus);
1409 ret = MCI_RESOURCE_RETURNED;
1411 case MCI_STATUS_MEDIA_PRESENT:
1412 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1413 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1414 ret = MCI_RESOURCE_RETURNED;
1416 case MCI_STATUS_NUMBER_OF_TRACKS:
1417 lpParms->dwReturn = (wmm->wFormat == 2) ? wmm->nTracks : 1;
1418 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %u\n", lpParms->dwReturn);
1420 case MCI_STATUS_POSITION:
1421 /* FIXME: do I need to use MCI_TRACK ? */
1422 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm,
1423 (dwFlags & MCI_STATUS_START) ? 0 : wmm->dwPositionMS);
1424 TRACE("MCI_STATUS_POSITION %s => %u\n",
1425 (dwFlags & MCI_STATUS_START) ? "start" : "current", lpParms->dwReturn);
1427 case MCI_STATUS_READY:
1428 lpParms->dwReturn = (wmm->dwStatus == MCI_MODE_NOT_READY) ?
1429 MAKEMCIRESOURCE(FALSE, MCI_FALSE) : MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1430 ret = MCI_RESOURCE_RETURNED;
1431 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms->dwReturn));
1433 case MCI_STATUS_TIME_FORMAT:
1434 lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwMciTimeFormat, MCI_FORMAT_RETURN_BASE + wmm->dwMciTimeFormat);
1435 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms->dwReturn));
1436 ret = MCI_RESOURCE_RETURNED;
1438 case MCI_SEQ_STATUS_DIVTYPE:
1439 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1440 if (wmm->nDivision > 0x8000) {
1441 switch (wmm->nDivision) {
1442 case 0xE8: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_24; break; /* -24 */
1443 case 0xE7: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_25; break; /* -25 */
1444 case 0xE3: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
1445 case 0xE2: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30; break; /* -30 */
1446 default: FIXME("There is a bad bad programmer\n");
1449 lpParms->dwReturn = MCI_SEQ_DIV_PPQN;
1451 lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn,lpParms->dwReturn);
1452 ret = MCI_RESOURCE_RETURNED;
1454 case MCI_SEQ_STATUS_MASTER:
1455 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1456 lpParms->dwReturn = 0;
1458 case MCI_SEQ_STATUS_SLAVE:
1459 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1460 lpParms->dwReturn = 0;
1462 case MCI_SEQ_STATUS_OFFSET:
1463 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1464 lpParms->dwReturn = 0;
1466 case MCI_SEQ_STATUS_PORT:
1467 TRACE("MCI_SEQ_STATUS_PORT (%u)!\n", wmm->wDevID);
1468 lpParms->dwReturn = MIDI_MAPPER;
1470 case MCI_SEQ_STATUS_TEMPO:
1471 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1472 lpParms->dwReturn = wmm->dwTempo;
1475 FIXME("Unknowm command %08X !\n", lpParms->dwItem);
1476 return MCIERR_UNRECOGNIZED_COMMAND;
1479 WARN("No Status-Item!\n");
1480 return MCIERR_UNRECOGNIZED_COMMAND;
1482 if (dwFlags & MCI_NOTIFY) {
1483 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1484 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1485 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1490 /**************************************************************************
1491 * MIDI_mciGetDevCaps [internal]
1493 static DWORD MIDI_mciGetDevCaps(UINT wDevID, DWORD dwFlags,
1494 LPMCI_GETDEVCAPS_PARMS lpParms)
1496 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1499 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1501 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1502 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1504 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
1505 switch (lpParms->dwItem) {
1506 case MCI_GETDEVCAPS_DEVICE_TYPE:
1507 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1508 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER, MCI_DEVTYPE_SEQUENCER);
1509 ret = MCI_RESOURCE_RETURNED;
1511 case MCI_GETDEVCAPS_HAS_AUDIO:
1512 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1513 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1514 ret = MCI_RESOURCE_RETURNED;
1516 case MCI_GETDEVCAPS_HAS_VIDEO:
1517 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1518 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1519 ret = MCI_RESOURCE_RETURNED;
1521 case MCI_GETDEVCAPS_USES_FILES:
1522 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1523 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1524 ret = MCI_RESOURCE_RETURNED;
1526 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
1527 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1528 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1529 ret = MCI_RESOURCE_RETURNED;
1531 case MCI_GETDEVCAPS_CAN_EJECT:
1532 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1533 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1534 ret = MCI_RESOURCE_RETURNED;
1536 case MCI_GETDEVCAPS_CAN_PLAY:
1537 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1538 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1539 ret = MCI_RESOURCE_RETURNED;
1541 case MCI_GETDEVCAPS_CAN_RECORD:
1542 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1543 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1544 ret = MCI_RESOURCE_RETURNED;
1546 case MCI_GETDEVCAPS_CAN_SAVE:
1547 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1548 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1549 ret = MCI_RESOURCE_RETURNED;
1552 FIXME("Unknown capability (%08x) !\n", lpParms->dwItem);
1553 return MCIERR_UNRECOGNIZED_COMMAND;
1556 WARN("No GetDevCaps-Item !\n");
1557 return MCIERR_UNRECOGNIZED_COMMAND;
1562 /**************************************************************************
1563 * MIDI_mciInfo [internal]
1565 static DWORD MIDI_mciInfo(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
1568 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1570 static const WCHAR wszMidiSeq[] = {'W','i','n','e','\'','s',' ','M','I','D','I',' ','s','e','q','u','e','n','c','e','r',0};
1572 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1574 if (lpParms == NULL || lpParms->lpstrReturn == NULL)
1575 return MCIERR_NULL_PARAMETER_BLOCK;
1576 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1578 TRACE("buf=%p, len=%u\n", lpParms->lpstrReturn, lpParms->dwRetSize);
1580 switch (dwFlags & ~(MCI_WAIT|MCI_NOTIFY)) {
1581 case MCI_INFO_PRODUCT: str = wszMidiSeq; break;
1582 case MCI_INFO_FILE: str = wmm->lpstrElementName; break;
1583 case MCI_INFO_COPYRIGHT: str = wmm->lpstrCopyright; break;
1584 case MCI_INFO_NAME: str = wmm->lpstrName; break;
1586 WARN("Don't know this info command (%u)\n", dwFlags);
1587 return MCIERR_UNRECOGNIZED_COMMAND;
1590 if (lpParms->dwRetSize <= strlenW(str)) {
1591 lstrcpynW(lpParms->lpstrReturn, str, lpParms->dwRetSize - 1);
1592 ret = MCIERR_PARAM_OVERFLOW;
1594 strcpyW(lpParms->lpstrReturn, str);
1597 *lpParms->lpstrReturn = 0;
1602 /**************************************************************************
1603 * MIDI_mciSeek [internal]
1605 static DWORD MIDI_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1608 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1610 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1612 if (lpParms == NULL) {
1613 ret = MCIERR_NULL_PARAMETER_BLOCK;
1614 } else if (wmm == NULL) {
1615 ret = MCIERR_INVALID_DEVICE_ID;
1617 MIDI_mciStop(wDevID, MCI_WAIT, 0);
1619 if (dwFlags & MCI_SEEK_TO_START) {
1620 wmm->dwPositionMS = 0;
1621 } else if (dwFlags & MCI_SEEK_TO_END) {
1622 wmm->dwPositionMS = 0xFFFFFFFF; /* FIXME */
1623 } else if (dwFlags & MCI_TO) {
1624 wmm->dwPositionMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
1626 WARN("dwFlag doesn't tell where to seek to...\n");
1627 return MCIERR_MISSING_PARAMETER;
1630 TRACE("Seeking to position=%u ms\n", wmm->dwPositionMS);
1632 if (dwFlags & MCI_NOTIFY) {
1633 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1634 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1635 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1641 /*======================================================================*
1642 * MIDI entry points *
1643 *======================================================================*/
1645 /**************************************************************************
1646 * DriverProc (MCISEQ.@)
1648 LRESULT CALLBACK MCIMIDI_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1649 LPARAM dwParam1, LPARAM dwParam2)
1652 case DRV_LOAD: return 1;
1653 case DRV_FREE: return 1;
1654 case DRV_ENABLE: return 1;
1655 case DRV_DISABLE: return 1;
1656 case DRV_QUERYCONFIGURE: return 1;
1657 case DRV_CONFIGURE: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK); return 1;
1658 case DRV_INSTALL: return DRVCNF_RESTART;
1659 case DRV_REMOVE: return DRVCNF_RESTART;
1660 case DRV_OPEN: return MIDI_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1661 case DRV_CLOSE: return MIDI_drvClose(dwDevID);
1664 if (dwDevID == 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION;
1667 case MCI_OPEN_DRIVER: return MIDI_mciOpen (dwDevID, dwParam1, (LPMCI_OPEN_PARMSW) dwParam2);
1668 case MCI_CLOSE_DRIVER: return MIDI_mciClose (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1669 case MCI_PLAY: return MIDI_mciPlay (dwDevID, dwParam1, (LPMCI_PLAY_PARMS) dwParam2);
1670 case MCI_RECORD: return MIDI_mciRecord (dwDevID, dwParam1, (LPMCI_RECORD_PARMS) dwParam2);
1671 case MCI_STOP: return MIDI_mciStop (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1672 case MCI_SET: return MIDI_mciSet (dwDevID, dwParam1, (LPMCI_SET_PARMS) dwParam2);
1673 case MCI_PAUSE: return MIDI_mciPause (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1674 case MCI_RESUME: return MIDI_mciResume (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1675 case MCI_STATUS: return MIDI_mciStatus (dwDevID, dwParam1, (LPMCI_STATUS_PARMS) dwParam2);
1676 case MCI_GETDEVCAPS: return MIDI_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1677 case MCI_INFO: return MIDI_mciInfo (dwDevID, dwParam1, (LPMCI_INFO_PARMSW) dwParam2);
1678 case MCI_SEEK: return MIDI_mciSeek (dwDevID, dwParam1, (LPMCI_SEEK_PARMS) dwParam2);
1679 /* commands that should be supported */
1695 WARN("Unsupported command [%u]\n", wMsg);
1697 /* commands that should report an error */
1699 TRACE("Unsupported command [%u]\n", wMsg);
1703 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1706 TRACE("Sending msg [%u] to default driver proc\n", wMsg);
1707 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1709 return MCIERR_UNRECOGNIZED_COMMAND;