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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 dwParam1,
126 DWORD 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)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 synchonously\n");
150 return MCI_SCAStarter(&sca);
152 SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL);
157 /*======================================================================*
158 * MCI MIDI implemantation *
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)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=%08lX\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 %08lX\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=%08lx(%08lx), data=%08lx, 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 (%d bytes, when %u are needed)\n", sizeof(buf) - 1, 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 %lu bytes and %lu 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=%lu track=%lu\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, %08lX);\n", wmm, dwOffset);
462 if (mmioSeek(wmm->hFile, dwOffset, SEEK_SET) != dwOffset) {
463 WARN("Can't seek at %08lX 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%08lX, 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 * In 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.
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 %ld 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=%lu tempo=%lu division=%u=0x%04x => ms=%lu\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 %lu!\n", wmm->dwMciTimeFormat);
634 TRACE("val=%lu=0x%08lx [tf=%lu] => ret=%lu\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 %lu!\n", wmm->dwMciTimeFormat);
682 TRACE("val=%lu [tf=%lu] => ret=%lu=0x%08lx\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, %08lX, %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=%08lX)\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=%08lX\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, %08lX, %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, %08lX, %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, %08lX, %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)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 %lu to %lu\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 %lu\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%08lx <> cur=0x%08lx\n", hiPulse, wmm->dwPulse);
1010 TRACE("Wait until %lu => %lu 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* 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 (%d bytes, when %u are needed)\n", sizeof(buf) - 1, 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 %ld (BPM=%ld)\n", wmm->dwTempo, (value) ? (60000000l / 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 (%lu)\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 rythm */
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, %08lX, %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=%lu\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 if (midiHdr.lpData != NULL) {
1235 HeapFree(GetProcessHeap(), 0, midiHdr.lpData);
1236 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, %08lX, %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, %08lX, %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_SET_PARMS lpParms)
1302 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1304 TRACE("(%04X, %08lX, %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 %lu!\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 %lu\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 !\n");
1371 if (dwFlags & MCI_SEQ_SET_TEMPO)
1372 TRACE("MCI_SEQ_SET_TEMPO !\n");
1376 /**************************************************************************
1377 * MIDI_mciStatus [internal]
1379 static DWORD MIDI_mciStatus(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
1381 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1384 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1386 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1387 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1389 if (dwFlags & MCI_STATUS_ITEM) {
1390 switch (lpParms->dwItem) {
1391 case MCI_STATUS_CURRENT_TRACK:
1392 /* FIXME in Format 2 */
1393 lpParms->dwReturn = 1;
1394 TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms->dwReturn);
1396 case MCI_STATUS_LENGTH:
1397 if ((dwFlags & MCI_TRACK) && wmm->wFormat == 2) {
1398 if (lpParms->dwTrack >= wmm->nTracks)
1399 return MCIERR_BAD_INTEGER;
1400 /* FIXME: this is wrong if there is a tempo change inside the file */
1401 lpParms->dwReturn = MIDI_ConvertPulseToMS(wmm, wmm->tracks[lpParms->dwTrack].dwLength);
1403 lpParms->dwReturn = MIDI_GetMThdLengthMS(wmm);
1405 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm, lpParms->dwReturn);
1406 TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms->dwReturn);
1408 case MCI_STATUS_MODE:
1409 TRACE("MCI_STATUS_MODE => %u\n", wmm->dwStatus);
1410 lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwStatus, wmm->dwStatus);
1411 ret = MCI_RESOURCE_RETURNED;
1413 case MCI_STATUS_MEDIA_PRESENT:
1414 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1415 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1416 ret = MCI_RESOURCE_RETURNED;
1418 case MCI_STATUS_NUMBER_OF_TRACKS:
1419 lpParms->dwReturn = (wmm->wFormat == 2) ? wmm->nTracks : 1;
1420 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms->dwReturn);
1422 case MCI_STATUS_POSITION:
1423 /* FIXME: do I need to use MCI_TRACK ? */
1424 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm,
1425 (dwFlags & MCI_STATUS_START) ? 0 : wmm->dwPositionMS);
1426 TRACE("MCI_STATUS_POSITION %s => %lu\n",
1427 (dwFlags & MCI_STATUS_START) ? "start" : "current", lpParms->dwReturn);
1429 case MCI_STATUS_READY:
1430 lpParms->dwReturn = (wmm->dwStatus == MCI_MODE_NOT_READY) ?
1431 MAKEMCIRESOURCE(FALSE, MCI_FALSE) : MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1432 ret = MCI_RESOURCE_RETURNED;
1433 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms->dwReturn));
1435 case MCI_STATUS_TIME_FORMAT:
1436 lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwMciTimeFormat, MCI_FORMAT_RETURN_BASE + wmm->dwMciTimeFormat);
1437 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms->dwReturn));
1438 ret = MCI_RESOURCE_RETURNED;
1440 case MCI_SEQ_STATUS_DIVTYPE:
1441 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1442 if (wmm->nDivision > 0x8000) {
1443 switch (wmm->nDivision) {
1444 case 0xE8: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_24; break; /* -24 */
1445 case 0xE7: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_25; break; /* -25 */
1446 case 0xE3: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
1447 case 0xE2: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30; break; /* -30 */
1448 default: FIXME("There is a bad bad programmer\n");
1451 lpParms->dwReturn = MCI_SEQ_DIV_PPQN;
1453 lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn,lpParms->dwReturn);
1454 ret = MCI_RESOURCE_RETURNED;
1456 case MCI_SEQ_STATUS_MASTER:
1457 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1458 lpParms->dwReturn = 0;
1460 case MCI_SEQ_STATUS_SLAVE:
1461 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1462 lpParms->dwReturn = 0;
1464 case MCI_SEQ_STATUS_OFFSET:
1465 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1466 lpParms->dwReturn = 0;
1468 case MCI_SEQ_STATUS_PORT:
1469 TRACE("MCI_SEQ_STATUS_PORT (%u)!\n", wmm->wDevID);
1470 lpParms->dwReturn = MIDI_MAPPER;
1472 case MCI_SEQ_STATUS_TEMPO:
1473 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1474 lpParms->dwReturn = wmm->dwTempo;
1477 FIXME("Unknowm command %08lX !\n", lpParms->dwItem);
1478 return MCIERR_UNRECOGNIZED_COMMAND;
1481 WARN("No Status-Item!\n");
1482 return MCIERR_UNRECOGNIZED_COMMAND;
1484 if (dwFlags & MCI_NOTIFY) {
1485 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1486 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1487 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1492 /**************************************************************************
1493 * MIDI_mciGetDevCaps [internal]
1495 static DWORD MIDI_mciGetDevCaps(UINT wDevID, DWORD dwFlags,
1496 LPMCI_GETDEVCAPS_PARMS lpParms)
1498 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1501 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1503 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1504 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1506 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
1507 switch (lpParms->dwItem) {
1508 case MCI_GETDEVCAPS_DEVICE_TYPE:
1509 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1510 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER, MCI_DEVTYPE_SEQUENCER);
1511 ret = MCI_RESOURCE_RETURNED;
1513 case MCI_GETDEVCAPS_HAS_AUDIO:
1514 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1515 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1516 ret = MCI_RESOURCE_RETURNED;
1518 case MCI_GETDEVCAPS_HAS_VIDEO:
1519 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1520 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1521 ret = MCI_RESOURCE_RETURNED;
1523 case MCI_GETDEVCAPS_USES_FILES:
1524 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1525 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1526 ret = MCI_RESOURCE_RETURNED;
1528 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
1529 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1530 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1531 ret = MCI_RESOURCE_RETURNED;
1533 case MCI_GETDEVCAPS_CAN_EJECT:
1534 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1535 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1536 ret = MCI_RESOURCE_RETURNED;
1538 case MCI_GETDEVCAPS_CAN_PLAY:
1539 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1540 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1541 ret = MCI_RESOURCE_RETURNED;
1543 case MCI_GETDEVCAPS_CAN_RECORD:
1544 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1545 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1546 ret = MCI_RESOURCE_RETURNED;
1548 case MCI_GETDEVCAPS_CAN_SAVE:
1549 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1550 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1551 ret = MCI_RESOURCE_RETURNED;
1554 FIXME("Unknown capability (%08lx) !\n", lpParms->dwItem);
1555 return MCIERR_UNRECOGNIZED_COMMAND;
1558 WARN("No GetDevCaps-Item !\n");
1559 return MCIERR_UNRECOGNIZED_COMMAND;
1564 /**************************************************************************
1565 * MIDI_mciInfo [internal]
1567 static DWORD MIDI_mciInfo(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
1570 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1572 static const WCHAR wszMidiSeq[] = {'W','i','n','e','\'','s',' ','M','I','D','I',' ','s','e','q','u','e','n','c','e','r',0};
1574 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1576 if (lpParms == NULL || lpParms->lpstrReturn == NULL)
1577 return MCIERR_NULL_PARAMETER_BLOCK;
1578 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1580 TRACE("buf=%p, len=%lu\n", lpParms->lpstrReturn, lpParms->dwRetSize);
1582 switch (dwFlags & ~(MCI_WAIT|MCI_NOTIFY)) {
1583 case MCI_INFO_PRODUCT: str = wszMidiSeq; break;
1584 case MCI_INFO_FILE: str = wmm->lpstrElementName; break;
1585 case MCI_INFO_COPYRIGHT: str = wmm->lpstrCopyright; break;
1586 case MCI_INFO_NAME: str = wmm->lpstrName; break;
1588 WARN("Don't know this info command (%lu)\n", dwFlags);
1589 return MCIERR_UNRECOGNIZED_COMMAND;
1592 if (lpParms->dwRetSize <= strlenW(str)) {
1593 lstrcpynW(lpParms->lpstrReturn, str, lpParms->dwRetSize - 1);
1594 ret = MCIERR_PARAM_OVERFLOW;
1596 strcpyW(lpParms->lpstrReturn, str);
1599 *lpParms->lpstrReturn = 0;
1604 /**************************************************************************
1605 * MIDI_mciSeek [internal]
1607 static DWORD MIDI_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1610 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1612 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1614 if (lpParms == NULL) {
1615 ret = MCIERR_NULL_PARAMETER_BLOCK;
1616 } else if (wmm == NULL) {
1617 ret = MCIERR_INVALID_DEVICE_ID;
1619 MIDI_mciStop(wDevID, MCI_WAIT, 0);
1621 if (dwFlags & MCI_SEEK_TO_START) {
1622 wmm->dwPositionMS = 0;
1623 } else if (dwFlags & MCI_SEEK_TO_END) {
1624 wmm->dwPositionMS = 0xFFFFFFFF; /* FIXME */
1625 } else if (dwFlags & MCI_TO) {
1626 wmm->dwPositionMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
1628 WARN("dwFlag doesn't tell where to seek to...\n");
1629 return MCIERR_MISSING_PARAMETER;
1632 TRACE("Seeking to position=%lu ms\n", wmm->dwPositionMS);
1634 if (dwFlags & MCI_NOTIFY) {
1635 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1636 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1637 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1643 /*======================================================================*
1644 * MIDI entry points *
1645 *======================================================================*/
1647 /**************************************************************************
1648 * DriverProc (MCISEQ.@)
1650 LRESULT CALLBACK MCIMIDI_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1651 LPARAM dwParam1, LPARAM dwParam2)
1654 case DRV_LOAD: return 1;
1655 case DRV_FREE: return 1;
1656 case DRV_ENABLE: return 1;
1657 case DRV_DISABLE: return 1;
1658 case DRV_QUERYCONFIGURE: return 1;
1659 case DRV_CONFIGURE: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK); return 1;
1660 case DRV_INSTALL: return DRVCNF_RESTART;
1661 case DRV_REMOVE: return DRVCNF_RESTART;
1662 case DRV_OPEN: return MIDI_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1663 case DRV_CLOSE: return MIDI_drvClose(dwDevID);
1666 if (dwDevID == 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION;
1669 case MCI_OPEN_DRIVER: return MIDI_mciOpen (dwDevID, dwParam1, (LPMCI_OPEN_PARMSW) dwParam2);
1670 case MCI_CLOSE_DRIVER: return MIDI_mciClose (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1671 case MCI_PLAY: return MIDI_mciPlay (dwDevID, dwParam1, (LPMCI_PLAY_PARMS) dwParam2);
1672 case MCI_RECORD: return MIDI_mciRecord (dwDevID, dwParam1, (LPMCI_RECORD_PARMS) dwParam2);
1673 case MCI_STOP: return MIDI_mciStop (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1674 case MCI_SET: return MIDI_mciSet (dwDevID, dwParam1, (LPMCI_SET_PARMS) dwParam2);
1675 case MCI_PAUSE: return MIDI_mciPause (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1676 case MCI_RESUME: return MIDI_mciResume (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1677 case MCI_STATUS: return MIDI_mciStatus (dwDevID, dwParam1, (LPMCI_STATUS_PARMS) dwParam2);
1678 case MCI_GETDEVCAPS: return MIDI_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1679 case MCI_INFO: return MIDI_mciInfo (dwDevID, dwParam1, (LPMCI_INFO_PARMSW) dwParam2);
1680 case MCI_SEEK: return MIDI_mciSeek (dwDevID, dwParam1, (LPMCI_SEEK_PARMS) dwParam2);
1681 /* commands that should be supported */
1697 WARN("Unsupported command [%u]\n", wMsg);
1699 /* commands that should report an error */
1701 TRACE("Unsupported command [%u]\n", wMsg);
1705 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1708 TRACE("Sending msg [%u] to default driver proc\n", wMsg);
1709 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1711 return MCIERR_UNRECOGNIZED_COMMAND;