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);
814 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
816 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
818 if (wmm->dwStatus != MCI_MODE_STOP) {
819 int oldstat = wmm->dwStatus;
821 wmm->dwStatus = MCI_MODE_NOT_READY;
822 if (oldstat == MCI_MODE_PAUSE)
823 midiOutReset((HMIDIOUT)wmm->hMidi);
825 while (wmm->dwStatus != MCI_MODE_STOP)
830 wmm->dwStatus = MCI_MODE_STOP;
832 TRACE("wmm->dwStatus=%d\n", wmm->dwStatus);
834 if (lpParms && (dwFlags & MCI_NOTIFY)) {
835 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
836 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
837 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
842 /**************************************************************************
843 * MIDI_mciClose [internal]
845 static DWORD MIDI_mciClose(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
847 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
849 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
851 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
853 if (wmm->dwStatus != MCI_MODE_STOP) {
854 MIDI_mciStop(wDevID, MCI_WAIT, lpParms);
858 if (wmm->nUseCount == 0) {
859 if (wmm->hFile != 0) {
860 mmioClose(wmm->hFile, 0);
862 TRACE("hFile closed !\n");
864 HeapFree(GetProcessHeap(), 0, wmm->tracks);
865 HeapFree(GetProcessHeap(), 0, wmm->lpstrElementName);
866 HeapFree(GetProcessHeap(), 0, wmm->lpstrCopyright);
867 HeapFree(GetProcessHeap(), 0, wmm->lpstrName);
869 TRACE("Shouldn't happen... nUseCount=%d\n", wmm->nUseCount);
870 return MCIERR_INTERNAL;
873 if (lpParms && (dwFlags & MCI_NOTIFY)) {
874 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
875 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
876 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
881 /**************************************************************************
882 * MIDI_mciFindNextEvent [internal]
884 static MCI_MIDITRACK* MIDI_mciFindNextEvent(WINE_MCIMIDI* wmm, LPDWORD hiPulse)
889 *hiPulse = 0xFFFFFFFFul;
891 for (nt = 0; nt < wmm->nTracks; nt++) {
892 mmt = &wmm->tracks[nt];
894 if (mmt->wStatus == 0)
896 if (mmt->dwEventPulse < *hiPulse) {
897 *hiPulse = mmt->dwEventPulse;
901 return (cnt == 0xFFFFu) ? 0 /* no more event on all tracks */
905 /**************************************************************************
906 * MIDI_mciPlay [internal]
908 static DWORD MIDI_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
910 DWORD dwStartMS, dwEndMS;
915 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
917 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
919 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
921 if (wmm->hFile == 0) {
922 WARN("Can't play: no file %s!\n", debugstr_w(wmm->lpstrElementName));
923 return MCIERR_FILE_NOT_FOUND;
926 if (wmm->dwStatus != MCI_MODE_STOP) {
927 if (wmm->dwStatus == MCI_MODE_PAUSE) {
928 /* FIXME: parameters (start/end) in lpParams may not be used */
929 return MIDI_mciResume(wDevID, dwFlags, (LPMCI_GENERIC_PARMS)lpParms);
931 WARN("Can't play: device is not stopped !\n");
932 return MCIERR_INTERNAL;
935 if (!(dwFlags & MCI_WAIT)) {
936 /** FIXME: I'm not 100% sure that wNotifyDeviceID is the right value in all cases ??? */
937 return MCI_SendCommandAsync(wmm->wNotifyDeviceID, MCI_PLAY, dwFlags, (DWORD_PTR)lpParms, sizeof(LPMCI_PLAY_PARMS));
940 if (lpParms && (dwFlags & MCI_FROM)) {
941 dwStartMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwFrom);
943 dwStartMS = wmm->dwPositionMS;
946 if (lpParms && (dwFlags & MCI_TO)) {
947 dwEndMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
949 dwEndMS = 0xFFFFFFFFul;
952 TRACE("Playing from %u to %u\n", dwStartMS, dwEndMS);
955 for (nt = 0; nt < wmm->nTracks; nt++) {
956 mmt = &wmm->tracks[nt];
958 mmt->wStatus = 1; /* ok, playing */
959 mmt->dwIndex = mmt->dwFirst;
960 if (wmm->wFormat == 2 && nt > 0) {
961 mmt->dwEventPulse = wmm->tracks[nt - 1].dwLength;
963 mmt->dwEventPulse = 0;
965 MIDI_mciReadNextEvent(wmm, mmt); /* FIXME == 0 */
968 dwRet = midiOutOpen((LPHMIDIOUT)&wmm->hMidi, MIDIMAPPER, 0L, 0L, CALLBACK_NULL);
969 /* dwRet = midiInOpen(&wmm->hMidi, MIDIMAPPER, 0L, 0L, CALLBACK_NULL);*/
970 if (dwRet != MMSYSERR_NOERROR) {
975 wmm->dwTempo = 500000;
976 wmm->dwStatus = MCI_MODE_PLAY;
977 wmm->dwPositionMS = 0;
978 wmm->wStartedPlaying = FALSE;
980 while (wmm->dwStatus != MCI_MODE_STOP && wmm->dwStatus != MCI_MODE_NOT_READY) {
981 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
982 * too much. Tell gcc not to optimize status value using volatile.
984 while (((volatile WINE_MCIMIDI*)wmm)->dwStatus == MCI_MODE_PAUSE);
986 doPlay = (wmm->dwPositionMS >= dwStartMS && wmm->dwPositionMS <= dwEndMS);
988 TRACE("wmm->dwStatus=%d, doPlay=%c\n", wmm->dwStatus, doPlay ? 'T' : 'F');
990 if ((mmt = MIDI_mciFindNextEvent(wmm, &hiPulse)) == NULL)
991 break; /* no more event on tracks */
993 /* if starting playing, then set StartTicks to the value it would have had
994 * if play had started at position 0
996 if (doPlay && !wmm->wStartedPlaying) {
997 wmm->dwStartTicks = GetTickCount() - MIDI_ConvertPulseToMS(wmm, wmm->dwPulse);
998 wmm->wStartedPlaying = TRUE;
999 TRACE("Setting dwStartTicks to %u\n", wmm->dwStartTicks);
1002 if (hiPulse > wmm->dwPulse) {
1003 wmm->dwPositionMS += MIDI_ConvertPulseToMS(wmm, hiPulse - wmm->dwPulse);
1005 DWORD togo = wmm->dwStartTicks + wmm->dwPositionMS;
1006 DWORD tc = GetTickCount();
1008 TRACE("Pulses hi=0x%08x <> cur=0x%08x\n", hiPulse, wmm->dwPulse);
1009 TRACE("Wait until %u => %u ms\n",
1010 tc - wmm->dwStartTicks, togo - wmm->dwStartTicks);
1014 wmm->dwPulse = hiPulse;
1017 switch (LOBYTE(LOWORD(mmt->dwEventData))) {
1019 case 0xF7: /* sysex events */
1021 FIXME("Not handling SysEx events (yet)\n");
1025 /* position after meta data header */
1026 mmioSeek(wmm->hFile, mmt->dwIndex + HIWORD(mmt->dwEventData), SEEK_SET);
1027 switch (HIBYTE(LOWORD(mmt->dwEventData))) {
1028 case 0x00: /* 16-bit sequence number */
1029 if (TRACE_ON(mcimidi)) {
1032 MIDI_mciReadWord(wmm, &twd); /* == 0 */
1033 TRACE("Got sequence number %u\n", twd);
1036 case 0x01: /* any text */
1037 case 0x02: /* Copyright Message text */
1038 case 0x03: /* Sequence/Track Name text */
1039 case 0x04: /* Instrument Name text */
1040 case 0x05: /* Lyric text */
1041 case 0x06: /* Marker text */
1042 case 0x07: /* Cue-point text */
1043 if (TRACE_ON(mcimidi)) {
1045 WORD len = mmt->wEventLength - HIWORD(mmt->dwEventData);
1046 static const char* const info[8] = {"", "Text", "Copyright", "Seq/Trk name",
1047 "Instrument", "Lyric", "Marker", "Cue-point"};
1048 WORD idx = HIBYTE(LOWORD(mmt->dwEventData));
1050 if (len >= sizeof(buf)) {
1051 WARN("Buffer for text is too small (%u are needed)\n", len);
1052 len = sizeof(buf) - 1;
1054 if (mmioRead(wmm->hFile, (HPSTR)buf, len) == len) {
1055 buf[len] = 0; /* end string in case */
1056 TRACE("%s => \"%s\"\n", (idx < 8 ) ? info[idx] : "", buf);
1058 WARN("Couldn't read data for %s\n", (idx < 8) ? info[idx] : "");
1063 /* MIDI channel (cc) */
1064 if (FIXME_ON(mcimidi)) {
1067 MIDI_mciReadByte(wmm, &bt); /* == 0 */
1068 FIXME("NIY: MIDI channel=%u, track=%u\n", bt, mmt->wTrackNr);
1072 /* MIDI port (pp) */
1073 if (FIXME_ON(mcimidi)) {
1076 MIDI_mciReadByte(wmm, &bt); /* == 0 */
1077 FIXME("NIY: MIDI port=%u, track=%u\n", bt, mmt->wTrackNr);
1080 case 0x2F: /* end of track */
1083 case 0x51:/* set tempo */
1084 /* Tempo is expressed in -seconds per midi quarter note
1085 * for format 1 MIDI files, this can only be present on track #0
1087 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
1088 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt->wTrackNr);
1093 MIDI_mciReadByte(wmm, &tbt); value = ((DWORD)tbt) << 16;
1094 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 8;
1095 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 0;
1096 TRACE("Setting tempo to %d (BPM=%d)\n", wmm->dwTempo, (value) ? (60000000 / value) : 0);
1097 wmm->dwTempo = value;
1100 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
1101 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
1102 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt->wTrackNr);
1103 } if (mmt->dwEventPulse != 0) {
1104 WARN("SMPTE track start can only be expressed at start of track (%u)\n", mmt->dwEventPulse);
1106 BYTE h, m, s, f, ff;
1108 MIDI_mciReadByte(wmm, &h);
1109 MIDI_mciReadByte(wmm, &m);
1110 MIDI_mciReadByte(wmm, &s);
1111 MIDI_mciReadByte(wmm, &f);
1112 MIDI_mciReadByte(wmm, &ff);
1113 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h, m, s, f, ff);
1116 case 0x58: /* file rhythm */
1117 if (TRACE_ON(mcimidi)) {
1118 BYTE num, den, cpmc, _32npqn;
1120 MIDI_mciReadByte(wmm, &num);
1121 MIDI_mciReadByte(wmm, &den); /* to notate e.g. 6/8 */
1122 MIDI_mciReadByte(wmm, &cpmc); /* number of MIDI clocks per metronome click */
1123 MIDI_mciReadByte(wmm, &_32npqn); /* number of notated 32nd notes per MIDI quarter note */
1125 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num, 1 << den, cpmc, _32npqn);
1128 case 0x59: /* key signature */
1129 if (TRACE_ON(mcimidi)) {
1132 MIDI_mciReadByte(wmm, &sf);
1133 MIDI_mciReadByte(wmm, &mm);
1135 if (sf >= 0x80) TRACE("%d flats\n", -(char)sf);
1136 else if (sf > 0) TRACE("%d sharps\n", (char)sf);
1137 else TRACE("Key of C\n");
1138 TRACE("Mode: %s\n", (mm == 0) ? "major" : "minor");
1142 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt->dwEventData)));
1148 dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1150 switch (LOBYTE(LOWORD(mmt->dwEventData)) & 0xF0) {
1156 dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1160 mmt->dwIndex += mmt->wEventLength;
1161 if (mmt->dwIndex < mmt->dwFirst || mmt->dwIndex >= mmt->dwLast) {
1165 MIDI_mciReadNextEvent(wmm, mmt);
1169 midiOutReset((HMIDIOUT)wmm->hMidi);
1171 dwRet = midiOutClose((HMIDIOUT)wmm->hMidi);
1172 /* to restart playing at beginning when it's over */
1173 wmm->dwPositionMS = 0;
1175 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1176 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1177 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1178 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1181 wmm->dwStatus = MCI_MODE_STOP;
1185 /**************************************************************************
1186 * MIDI_mciRecord [internal]
1188 static DWORD MIDI_mciRecord(UINT wDevID, DWORD dwFlags, LPMCI_RECORD_PARMS lpParms)
1192 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1194 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1196 if (wmm == 0) return MCIERR_INVALID_DEVICE_ID;
1198 if (wmm->hFile == 0) {
1199 WARN("Can't find file=%s!\n", debugstr_w(wmm->lpstrElementName));
1200 return MCIERR_FILE_NOT_FOUND;
1202 start = 1; end = 99999;
1203 if (lpParms && (dwFlags & MCI_FROM)) {
1204 start = lpParms->dwFrom;
1205 TRACE("MCI_FROM=%d\n", start);
1207 if (lpParms && (dwFlags & MCI_TO)) {
1208 end = lpParms->dwTo;
1209 TRACE("MCI_TO=%d\n", end);
1211 midiHdr.lpData = HeapAlloc(GetProcessHeap(), 0, 1200);
1212 if (!midiHdr.lpData)
1213 return MCIERR_OUT_OF_MEMORY;
1214 midiHdr.dwBufferLength = 1024;
1215 midiHdr.dwUser = 0L;
1216 midiHdr.dwFlags = 0L;
1217 midiInPrepareHeader((HMIDIIN)wmm->hMidi, &midiHdr, sizeof(MIDIHDR));
1218 TRACE("After MIDM_PREPARE\n");
1219 wmm->dwStatus = MCI_MODE_RECORD;
1220 /* FIXME: there is no buffer added */
1221 while (wmm->dwStatus != MCI_MODE_STOP) {
1222 TRACE("wmm->dwStatus=%p %d\n",
1223 &wmm->dwStatus, wmm->dwStatus);
1224 midiHdr.dwBytesRecorded = 0;
1225 midiInStart((HMIDIIN)wmm->hMidi);
1226 TRACE("midiInStart => dwBytesRecorded=%u\n", midiHdr.dwBytesRecorded);
1227 if (midiHdr.dwBytesRecorded == 0) break;
1229 TRACE("Before MIDM_UNPREPARE\n");
1230 midiInUnprepareHeader((HMIDIIN)wmm->hMidi, &midiHdr, sizeof(MIDIHDR));
1231 TRACE("After MIDM_UNPREPARE\n");
1232 HeapFree(GetProcessHeap(), 0, midiHdr.lpData);
1233 midiHdr.lpData = NULL;
1234 wmm->dwStatus = MCI_MODE_STOP;
1235 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1236 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1237 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1238 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1243 /**************************************************************************
1244 * MIDI_mciPause [internal]
1246 static DWORD MIDI_mciPause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1248 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1250 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1252 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1254 if (wmm->dwStatus == MCI_MODE_PLAY) {
1255 /* stop all notes */
1257 for (chn = 0; chn < 16; chn++)
1258 midiOutShortMsg((HMIDIOUT)(wmm->hMidi), 0x78B0 | chn);
1259 wmm->dwStatus = MCI_MODE_PAUSE;
1261 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1262 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1263 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1264 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1270 /**************************************************************************
1271 * MIDI_mciResume [internal]
1273 static DWORD MIDI_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1275 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1277 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1279 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1281 if (wmm->dwStatus == MCI_MODE_PAUSE) {
1282 wmm->wStartedPlaying = FALSE;
1283 wmm->dwStatus = MCI_MODE_PLAY;
1285 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1286 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1287 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1288 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1293 /**************************************************************************
1294 * MIDI_mciSet [internal]
1296 static DWORD MIDI_mciSet(UINT wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
1298 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1300 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1302 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1303 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1305 if (dwFlags & MCI_SET_TIME_FORMAT) {
1306 switch (lpParms->dwTimeFormat) {
1307 case MCI_FORMAT_MILLISECONDS:
1308 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1309 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
1311 case MCI_FORMAT_SMPTE_24:
1312 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1313 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24;
1315 case MCI_FORMAT_SMPTE_25:
1316 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1317 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25;
1319 case MCI_FORMAT_SMPTE_30:
1320 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1321 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30;
1324 WARN("Bad time format %u!\n", lpParms->dwTimeFormat);
1325 return MCIERR_BAD_TIME_FORMAT;
1328 if (dwFlags & MCI_SET_VIDEO) {
1329 TRACE("No support for video !\n");
1330 return MCIERR_UNSUPPORTED_FUNCTION;
1332 if (dwFlags & MCI_SET_DOOR_OPEN) {
1333 TRACE("No support for door open !\n");
1334 return MCIERR_UNSUPPORTED_FUNCTION;
1336 if (dwFlags & MCI_SET_DOOR_CLOSED) {
1337 TRACE("No support for door close !\n");
1338 return MCIERR_UNSUPPORTED_FUNCTION;
1340 if (dwFlags & MCI_SET_AUDIO) {
1341 if (dwFlags & MCI_SET_ON) {
1342 TRACE("MCI_SET_ON audio !\n");
1343 } else if (dwFlags & MCI_SET_OFF) {
1344 TRACE("MCI_SET_OFF audio !\n");
1346 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1347 return MCIERR_BAD_INTEGER;
1350 switch (lpParms->dwAudio)
1352 case MCI_SET_AUDIO_ALL: TRACE("MCI_SET_AUDIO_ALL !\n"); break;
1353 case MCI_SET_AUDIO_LEFT: TRACE("MCI_SET_AUDIO_LEFT !\n"); break;
1354 case MCI_SET_AUDIO_RIGHT: TRACE("MCI_SET_AUDIO_RIGHT !\n"); break;
1355 default: WARN("Unknown audio channel %u\n", lpParms->dwAudio); break;
1359 if (dwFlags & MCI_SEQ_SET_MASTER)
1360 TRACE("MCI_SEQ_SET_MASTER !\n");
1361 if (dwFlags & MCI_SEQ_SET_SLAVE)
1362 TRACE("MCI_SEQ_SET_SLAVE !\n");
1363 if (dwFlags & MCI_SEQ_SET_OFFSET)
1364 TRACE("MCI_SEQ_SET_OFFSET !\n");
1365 if (dwFlags & MCI_SEQ_SET_PORT)
1366 TRACE("MCI_SEQ_SET_PORT !\n");
1367 if (dwFlags & MCI_SEQ_SET_TEMPO)
1368 TRACE("MCI_SEQ_SET_TEMPO !\n");
1372 /**************************************************************************
1373 * MIDI_mciStatus [internal]
1375 static DWORD MIDI_mciStatus(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
1377 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1380 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1382 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1383 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1385 if (dwFlags & MCI_STATUS_ITEM) {
1386 switch (lpParms->dwItem) {
1387 case MCI_STATUS_CURRENT_TRACK:
1388 /* FIXME in Format 2 */
1389 lpParms->dwReturn = 1;
1390 TRACE("MCI_STATUS_CURRENT_TRACK => %u\n", lpParms->dwReturn);
1392 case MCI_STATUS_LENGTH:
1393 if ((dwFlags & MCI_TRACK) && wmm->wFormat == 2) {
1394 if (lpParms->dwTrack >= wmm->nTracks)
1395 return MCIERR_BAD_INTEGER;
1396 /* FIXME: this is wrong if there is a tempo change inside the file */
1397 lpParms->dwReturn = MIDI_ConvertPulseToMS(wmm, wmm->tracks[lpParms->dwTrack].dwLength);
1399 lpParms->dwReturn = MIDI_GetMThdLengthMS(wmm);
1401 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm, lpParms->dwReturn);
1402 TRACE("MCI_STATUS_LENGTH => %u\n", lpParms->dwReturn);
1404 case MCI_STATUS_MODE:
1405 TRACE("MCI_STATUS_MODE => %u\n", wmm->dwStatus);
1406 lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwStatus, wmm->dwStatus);
1407 ret = MCI_RESOURCE_RETURNED;
1409 case MCI_STATUS_MEDIA_PRESENT:
1410 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1411 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1412 ret = MCI_RESOURCE_RETURNED;
1414 case MCI_STATUS_NUMBER_OF_TRACKS:
1415 lpParms->dwReturn = (wmm->wFormat == 2) ? wmm->nTracks : 1;
1416 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %u\n", lpParms->dwReturn);
1418 case MCI_STATUS_POSITION:
1419 /* FIXME: do I need to use MCI_TRACK ? */
1420 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm,
1421 (dwFlags & MCI_STATUS_START) ? 0 : wmm->dwPositionMS);
1422 TRACE("MCI_STATUS_POSITION %s => %u\n",
1423 (dwFlags & MCI_STATUS_START) ? "start" : "current", lpParms->dwReturn);
1425 case MCI_STATUS_READY:
1426 lpParms->dwReturn = (wmm->dwStatus == MCI_MODE_NOT_READY) ?
1427 MAKEMCIRESOURCE(FALSE, MCI_FALSE) : MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1428 ret = MCI_RESOURCE_RETURNED;
1429 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms->dwReturn));
1431 case MCI_STATUS_TIME_FORMAT:
1432 lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwMciTimeFormat, MCI_FORMAT_RETURN_BASE + wmm->dwMciTimeFormat);
1433 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms->dwReturn));
1434 ret = MCI_RESOURCE_RETURNED;
1436 case MCI_SEQ_STATUS_DIVTYPE:
1437 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1438 if (wmm->nDivision > 0x8000) {
1439 switch (wmm->nDivision) {
1440 case 0xE8: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_24; break; /* -24 */
1441 case 0xE7: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_25; break; /* -25 */
1442 case 0xE3: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
1443 case 0xE2: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30; break; /* -30 */
1444 default: FIXME("There is a bad bad programmer\n");
1447 lpParms->dwReturn = MCI_SEQ_DIV_PPQN;
1449 lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn,lpParms->dwReturn);
1450 ret = MCI_RESOURCE_RETURNED;
1452 case MCI_SEQ_STATUS_MASTER:
1453 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1454 lpParms->dwReturn = 0;
1456 case MCI_SEQ_STATUS_SLAVE:
1457 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1458 lpParms->dwReturn = 0;
1460 case MCI_SEQ_STATUS_OFFSET:
1461 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1462 lpParms->dwReturn = 0;
1464 case MCI_SEQ_STATUS_PORT:
1465 TRACE("MCI_SEQ_STATUS_PORT (%u)!\n", wmm->wDevID);
1466 lpParms->dwReturn = MIDI_MAPPER;
1468 case MCI_SEQ_STATUS_TEMPO:
1469 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1470 lpParms->dwReturn = wmm->dwTempo;
1473 FIXME("Unknown command %08X !\n", lpParms->dwItem);
1474 return MCIERR_UNRECOGNIZED_COMMAND;
1477 WARN("No Status-Item!\n");
1478 return MCIERR_UNRECOGNIZED_COMMAND;
1480 if (dwFlags & MCI_NOTIFY) {
1481 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1482 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1483 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1488 /**************************************************************************
1489 * MIDI_mciGetDevCaps [internal]
1491 static DWORD MIDI_mciGetDevCaps(UINT wDevID, DWORD dwFlags,
1492 LPMCI_GETDEVCAPS_PARMS lpParms)
1494 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1497 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1499 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1500 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1502 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
1503 switch (lpParms->dwItem) {
1504 case MCI_GETDEVCAPS_DEVICE_TYPE:
1505 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1506 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER, MCI_DEVTYPE_SEQUENCER);
1507 ret = MCI_RESOURCE_RETURNED;
1509 case MCI_GETDEVCAPS_HAS_AUDIO:
1510 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1511 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1512 ret = MCI_RESOURCE_RETURNED;
1514 case MCI_GETDEVCAPS_HAS_VIDEO:
1515 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1516 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1517 ret = MCI_RESOURCE_RETURNED;
1519 case MCI_GETDEVCAPS_USES_FILES:
1520 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1521 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1522 ret = MCI_RESOURCE_RETURNED;
1524 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
1525 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1526 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1527 ret = MCI_RESOURCE_RETURNED;
1529 case MCI_GETDEVCAPS_CAN_EJECT:
1530 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1531 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1532 ret = MCI_RESOURCE_RETURNED;
1534 case MCI_GETDEVCAPS_CAN_PLAY:
1535 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1536 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1537 ret = MCI_RESOURCE_RETURNED;
1539 case MCI_GETDEVCAPS_CAN_RECORD:
1540 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1541 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1542 ret = MCI_RESOURCE_RETURNED;
1544 case MCI_GETDEVCAPS_CAN_SAVE:
1545 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1546 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1547 ret = MCI_RESOURCE_RETURNED;
1550 FIXME("Unknown capability (%08x) !\n", lpParms->dwItem);
1551 return MCIERR_UNRECOGNIZED_COMMAND;
1554 WARN("No GetDevCaps-Item !\n");
1555 return MCIERR_UNRECOGNIZED_COMMAND;
1560 /**************************************************************************
1561 * MIDI_mciInfo [internal]
1563 static DWORD MIDI_mciInfo(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
1566 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1568 static const WCHAR wszMidiSeq[] = {'W','i','n','e','\'','s',' ','M','I','D','I',' ','s','e','q','u','e','n','c','e','r',0};
1570 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1572 if (lpParms == NULL || lpParms->lpstrReturn == NULL)
1573 return MCIERR_NULL_PARAMETER_BLOCK;
1574 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1576 TRACE("buf=%p, len=%u\n", lpParms->lpstrReturn, lpParms->dwRetSize);
1578 switch (dwFlags & ~(MCI_WAIT|MCI_NOTIFY)) {
1579 case MCI_INFO_PRODUCT: str = wszMidiSeq; break;
1580 case MCI_INFO_FILE: str = wmm->lpstrElementName; break;
1581 case MCI_INFO_COPYRIGHT: str = wmm->lpstrCopyright; break;
1582 case MCI_INFO_NAME: str = wmm->lpstrName; break;
1584 WARN("Don't know this info command (%u)\n", dwFlags);
1585 return MCIERR_UNRECOGNIZED_COMMAND;
1588 if (lpParms->dwRetSize <= strlenW(str)) {
1589 lstrcpynW(lpParms->lpstrReturn, str, lpParms->dwRetSize - 1);
1590 ret = MCIERR_PARAM_OVERFLOW;
1592 strcpyW(lpParms->lpstrReturn, str);
1595 *lpParms->lpstrReturn = 0;
1600 /**************************************************************************
1601 * MIDI_mciSeek [internal]
1603 static DWORD MIDI_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1606 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1608 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1610 if (lpParms == NULL) {
1611 ret = MCIERR_NULL_PARAMETER_BLOCK;
1612 } else if (wmm == NULL) {
1613 ret = MCIERR_INVALID_DEVICE_ID;
1615 MIDI_mciStop(wDevID, MCI_WAIT, 0);
1617 if (dwFlags & MCI_SEEK_TO_START) {
1618 wmm->dwPositionMS = 0;
1619 } else if (dwFlags & MCI_SEEK_TO_END) {
1620 wmm->dwPositionMS = 0xFFFFFFFF; /* FIXME */
1621 } else if (dwFlags & MCI_TO) {
1622 wmm->dwPositionMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
1624 WARN("dwFlag doesn't tell where to seek to...\n");
1625 return MCIERR_MISSING_PARAMETER;
1628 TRACE("Seeking to position=%u ms\n", wmm->dwPositionMS);
1630 if (dwFlags & MCI_NOTIFY) {
1631 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1632 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1633 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1639 /*======================================================================*
1640 * MIDI entry points *
1641 *======================================================================*/
1643 /**************************************************************************
1644 * DriverProc (MCISEQ.@)
1646 LRESULT CALLBACK MCIMIDI_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1647 LPARAM dwParam1, LPARAM dwParam2)
1650 case DRV_LOAD: return 1;
1651 case DRV_FREE: return 1;
1652 case DRV_ENABLE: return 1;
1653 case DRV_DISABLE: return 1;
1654 case DRV_QUERYCONFIGURE: return 1;
1655 case DRV_CONFIGURE: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK); return 1;
1656 case DRV_INSTALL: return DRVCNF_RESTART;
1657 case DRV_REMOVE: return DRVCNF_RESTART;
1658 case DRV_OPEN: return MIDI_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1659 case DRV_CLOSE: return MIDI_drvClose(dwDevID);
1662 if (dwDevID == 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION;
1665 case MCI_OPEN_DRIVER: return MIDI_mciOpen (dwDevID, dwParam1, (LPMCI_OPEN_PARMSW) dwParam2);
1666 case MCI_CLOSE_DRIVER: return MIDI_mciClose (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1667 case MCI_PLAY: return MIDI_mciPlay (dwDevID, dwParam1, (LPMCI_PLAY_PARMS) dwParam2);
1668 case MCI_RECORD: return MIDI_mciRecord (dwDevID, dwParam1, (LPMCI_RECORD_PARMS) dwParam2);
1669 case MCI_STOP: return MIDI_mciStop (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1670 case MCI_SET: return MIDI_mciSet (dwDevID, dwParam1, (LPMCI_SET_PARMS) dwParam2);
1671 case MCI_PAUSE: return MIDI_mciPause (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1672 case MCI_RESUME: return MIDI_mciResume (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1673 case MCI_STATUS: return MIDI_mciStatus (dwDevID, dwParam1, (LPMCI_STATUS_PARMS) dwParam2);
1674 case MCI_GETDEVCAPS: return MIDI_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1675 case MCI_INFO: return MIDI_mciInfo (dwDevID, dwParam1, (LPMCI_INFO_PARMSW) dwParam2);
1676 case MCI_SEEK: return MIDI_mciSeek (dwDevID, dwParam1, (LPMCI_SEEK_PARMS) dwParam2);
1677 /* commands that should be supported */
1693 WARN("Unsupported command [%u]\n", wMsg);
1695 /* commands that should report an error */
1697 TRACE("Unsupported command [%u]\n", wMsg);
1701 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1704 TRACE("Sending msg [%u] to default driver proc\n", wMsg);
1705 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1707 return MCIERR_UNRECOGNIZED_COMMAND;