2 * Sample MIDI Wine Driver for Linux
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1999 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * + implement it correctly
24 * + finish asynchronous commands
25 * + better implement non waiting command (without the MCI_WAIT flag).
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mcimidi
);
44 #define MIDI_NOTEOFF 0x80
45 #define MIDI_NOTEON 0x90
48 DWORD dwFirst
; /* offset in file of track */
49 DWORD dwLast
; /* number of bytes in file of track */
50 DWORD dwIndex
; /* current index in file (dwFirst <= dwIndex < dwLast) */
51 DWORD dwLength
; /* number of pulses in this track */
52 DWORD dwEventPulse
; /* current pulse # (event) pointed by dwIndex */
53 DWORD dwEventData
; /* current data (event) pointed by dwIndex */
54 WORD wEventLength
; /* current length (event) pointed by dwIndex */
55 WORD wStatus
: 1, /* 1 : playing, 0 : done */
57 wLastCommand
: 8; /* last MIDI command on track */
60 typedef struct tagWINE_MCIMIDI
{
61 UINT wDevID
; /* the MCI one */
63 int nUseCount
; /* Incremented for each shared open */
64 HANDLE hCallback
; /* Callback handle for pending notification */
65 HMMIO hFile
; /* mmio file handle open as Element */
66 LPWSTR lpstrElementName
; /* Name of file (if any) */
67 LPWSTR lpstrCopyright
;
69 WORD wPort
; /* the WINMM device unit */
70 WORD dwStatus
; /* one from MCI_MODE_xxxx */
71 DWORD dwMciTimeFormat
; /* One of the supported MCI_FORMAT_xxxx */
72 WORD wFormat
; /* Format of MIDI hFile (0, 1 or 2) */
73 WORD nTracks
; /* Number of tracks in hFile */
74 WORD nDivision
; /* Number of division in hFile PPQN or SMPTE */
76 DWORD dwTempo
; /* Tempo (# of 1/4 note per second */
77 MCI_MIDITRACK
* tracks
; /* Content of each track */
83 /* ===================================================================
84 * ===================================================================
85 * FIXME: should be using the new mmThreadXXXX functions from WINMM
87 * it would require to add a wine internal flag to mmThreadCreate
88 * in order to pass a 32 bit function instead of a 16 bit
89 * ===================================================================
90 * =================================================================== */
99 /* EPP DWORD WINAPI mciSendCommandA(UINT wDevID, UINT wMsg, DWORD dwParam1, DWORD dwParam2); */
101 /**************************************************************************
102 * MCI_SCAStarter [internal]
104 static DWORD CALLBACK
MCI_SCAStarter(LPVOID arg
)
106 struct SCA
* sca
= arg
;
109 TRACE("In thread before async command (%08x,%u,%08lx,%08lx)\n",
110 sca
->wDevID
, sca
->wMsg
, sca
->dwParam1
, sca
->dwParam2
);
111 ret
= mciSendCommandA(sca
->wDevID
, sca
->wMsg
, sca
->dwParam1
| MCI_WAIT
, sca
->dwParam2
);
112 TRACE("In thread after async command (%08x,%u,%08lx,%08lx)\n",
113 sca
->wDevID
, sca
->wMsg
, sca
->dwParam1
, sca
->dwParam2
);
114 HeapFree(GetProcessHeap(), 0, sca
);
118 /**************************************************************************
119 * MCI_SendCommandAsync [internal]
121 static DWORD
MCI_SendCommandAsync(UINT wDevID
, UINT wMsg
, DWORD_PTR dwParam1
,
122 DWORD_PTR dwParam2
, UINT size
)
125 struct SCA
* sca
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct SCA
) + size
);
128 return MCIERR_OUT_OF_MEMORY
;
130 sca
->wDevID
= wDevID
;
132 sca
->dwParam1
= dwParam1
;
134 if (size
&& dwParam2
) {
135 sca
->dwParam2
= (DWORD_PTR
)sca
+ sizeof(struct SCA
);
136 /* copy structure passed by program in dwParam2 to be sure
137 * we can still use it whatever the program does
139 memcpy((LPVOID
)sca
->dwParam2
, (LPVOID
)dwParam2
, size
);
141 sca
->dwParam2
= dwParam2
;
144 if ((handle
= CreateThread(NULL
, 0, MCI_SCAStarter
, sca
, 0, NULL
)) == 0) {
145 WARN("Couldn't allocate thread for async command handling, sending synchronously\n");
146 return MCI_SCAStarter(sca
);
148 SetThreadPriority(handle
, THREAD_PRIORITY_TIME_CRITICAL
);
153 /*======================================================================*
154 * MCI MIDI implementation *
155 *======================================================================*/
157 static DWORD
MIDI_mciResume(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
);
159 /**************************************************************************
160 * MIDI_drvOpen [internal]
162 static DWORD
MIDI_drvOpen(LPCWSTR str
, LPMCI_OPEN_DRIVER_PARMSW modp
)
166 if (!modp
) return 0xFFFFFFFF;
168 wmm
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINE_MCIMIDI
));
173 wmm
->wDevID
= modp
->wDeviceID
;
174 mciSetDriverData(wmm
->wDevID
, (DWORD_PTR
)wmm
);
175 modp
->wCustomCommandTable
= MCI_NO_COMMAND_TABLE
;
176 modp
->wType
= MCI_DEVTYPE_SEQUENCER
;
177 return modp
->wDeviceID
;
180 /**************************************************************************
181 * MCIMIDI_drvClose [internal]
183 static DWORD
MIDI_drvClose(DWORD dwDevID
)
185 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(dwDevID
);
188 HeapFree(GetProcessHeap(), 0, wmm
);
189 mciSetDriverData(dwDevID
, 0);
192 return (dwDevID
== 0xFFFFFFFF) ? 1 : 0;
195 /**************************************************************************
196 * MIDI_mciGetOpenDev [internal]
198 static WINE_MCIMIDI
* MIDI_mciGetOpenDev(MCIDEVICEID wDevID
, UINT wMsg
)
200 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(wDevID
);
202 if (wmm
== NULL
|| ((wmm
->nUseCount
== 0) ^ (wMsg
== MCI_OPEN_DRIVER
))) {
203 WARN("Invalid wDevID=%u\n", wDevID
);
209 /**************************************************************************
210 * MIDI_mciNotify [internal]
212 * Notifications in MCI work like a 1-element queue.
213 * Each new notification request supersedes the previous one.
214 * This affects Play and Record; other commands are immediate.
216 static void MIDI_mciNotify(DWORD_PTR hWndCallBack
, WINE_MCIMIDI
* wmm
, UINT wStatus
)
218 /* We simply save one parameter by not passing the wDevID local
219 * to the command. They are the same (via mciGetDriverData).
221 MCIDEVICEID wDevID
= wmm
->wDevID
;
222 HANDLE old
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
223 if (old
) mciDriverNotify(old
, wDevID
, MCI_NOTIFY_SUPERSEDED
);
224 mciDriverNotify(HWND_32(LOWORD(hWndCallBack
)), wDevID
, wStatus
);
227 /**************************************************************************
228 * MIDI_mciReadByte [internal]
230 static DWORD
MIDI_mciReadByte(WINE_MCIMIDI
* wmm
, BYTE
*lpbyt
)
234 if (mmioRead(wmm
->hFile
, (HPSTR
)lpbyt
, sizeof(BYTE
)) != (long)sizeof(BYTE
)) {
235 WARN("Error reading wmm=%p\n", wmm
);
236 ret
= MCIERR_INVALID_FILE
;
242 /**************************************************************************
243 * MIDI_mciReadWord [internal]
245 static DWORD
MIDI_mciReadWord(WINE_MCIMIDI
* wmm
, LPWORD lpw
)
248 DWORD ret
= MCIERR_INVALID_FILE
;
250 if (MIDI_mciReadByte(wmm
, &hibyte
) == 0 &&
251 MIDI_mciReadByte(wmm
, &lobyte
) == 0) {
252 *lpw
= ((WORD
)hibyte
<< 8) + lobyte
;
258 /**************************************************************************
259 * MIDI_mciReadLong [internal]
261 static DWORD
MIDI_mciReadLong(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
264 DWORD ret
= MCIERR_INVALID_FILE
;
266 if (MIDI_mciReadWord(wmm
, &hiword
) == 0 &&
267 MIDI_mciReadWord(wmm
, &loword
) == 0) {
268 *lpdw
= MAKELONG(loword
, hiword
);
274 /**************************************************************************
275 * MIDI_mciReadVaryLen [internal]
277 static WORD
MIDI_mciReadVaryLen(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
284 if (MIDI_mciReadByte(wmm
, &byte
) != 0) {
287 value
= (value
<< 7) + (byte
& 0x7F);
289 } while (byte
& 0x80);
294 /**************************************************************************
295 * MIDI_mciReadNextEvent [internal]
297 static DWORD
MIDI_mciReadNextEvent(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
305 if (mmioSeek(wmm
->hFile
, mmt
->dwIndex
, SEEK_SET
) != mmt
->dwIndex
) {
306 WARN("Can't seek at %08X\n", mmt
->dwIndex
);
307 return MCIERR_INVALID_FILE
;
309 evtLength
= MIDI_mciReadVaryLen(wmm
, &evtPulse
) + 1; /* > 0 */
310 MIDI_mciReadByte(wmm
, &b1
);
314 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
318 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
320 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
321 if (evtLength
>= 0x10000u
) {
322 /* this limitation shouldn't be a problem */
323 WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
326 hw
= LOWORD(evtLength
);
331 if (b1
& 0x80) { /* use running status ? */
332 mmt
->wLastCommand
= b1
;
333 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
336 b1
= mmt
->wLastCommand
;
338 switch ((b1
>> 4) & 0x07) {
339 case 0: case 1: case 2: case 3: case 6:
340 MIDI_mciReadByte(wmm
, &b3
); evtLength
++;
346 WARN("Strange indeed b1=0x%02x\n", b1
);
350 if (mmt
->dwIndex
+ evtLength
> mmt
->dwLast
)
351 return MCIERR_INTERNAL
;
353 mmt
->dwEventPulse
+= evtPulse
;
354 mmt
->dwEventData
= (hw
<< 16) + (b2
<< 8) + b1
;
355 mmt
->wEventLength
= evtLength
;
358 TRACE("[%u] => pulse=%08x(%08x), data=%08x, length=%u\n",
359 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
360 mmt->dwEventData, mmt->wEventLength);
365 /**************************************************************************
366 * MIDI_mciReadMTrk [internal]
368 static DWORD
MIDI_mciReadMTrk(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
373 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
, (long)sizeof(FOURCC
)) !=
374 (long)sizeof(FOURCC
)) {
375 return MCIERR_INVALID_FILE
;
378 if (fourcc
!= mmioFOURCC('M', 'T', 'r', 'k')) {
379 WARN("Can't synchronize on 'MTrk' !\n");
380 return MCIERR_INVALID_FILE
;
383 if (MIDI_mciReadLong(wmm
, &toberead
) != 0) {
384 return MCIERR_INVALID_FILE
;
386 mmt
->dwFirst
= mmioSeek(wmm
->hFile
, 0, SEEK_CUR
); /* >= 0 */
387 mmt
->dwLast
= mmt
->dwFirst
+ toberead
;
389 /* compute # of pulses in this track */
390 mmt
->dwIndex
= mmt
->dwFirst
;
391 mmt
->dwEventPulse
= 0;
393 while (MIDI_mciReadNextEvent(wmm
, mmt
) == 0 && LOWORD(mmt
->dwEventData
) != 0x2FFF) {
397 mmt
->dwIndex
+= mmt
->wEventLength
;
399 switch (LOWORD(mmt
->dwEventData
)) {
402 /* position after meta data header */
403 mmioSeek(wmm
->hFile
, mmt
->dwIndex
+ HIWORD(mmt
->dwEventData
), SEEK_SET
);
404 len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
406 if (len
>= sizeof(buf
)) {
407 WARN("Buffer for text is too small (%u are needed)\n", len
);
408 len
= sizeof(buf
) - 1;
410 if (mmioRead(wmm
->hFile
, buf
, len
) == len
) {
411 buf
[len
] = 0; /* end string in case */
412 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
414 if (wmm
->lpstrCopyright
) {
415 WARN("Two copyright notices (%s|%s)\n", debugstr_w(wmm
->lpstrCopyright
), buf
);
417 len
= MultiByteToWideChar( CP_ACP
, 0, buf
, -1, NULL
, 0 );
418 wmm
->lpstrCopyright
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
419 MultiByteToWideChar( CP_ACP
, 0, buf
, -1, wmm
->lpstrCopyright
, len
);
423 if (wmm
->lpstrName
) {
424 WARN("Two names (%s|%s)\n", debugstr_w(wmm
->lpstrName
), buf
);
426 len
= MultiByteToWideChar( CP_ACP
, 0, buf
, -1, NULL
, 0 );
427 wmm
->lpstrName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
428 MultiByteToWideChar( CP_ACP
, 0, buf
, -1, wmm
->lpstrName
, len
);
436 mmt
->dwLength
= mmt
->dwEventPulse
;
438 TRACE("Track %u has %u bytes and %u pulses\n", mmt
->wTrackNr
, toberead
, mmt
->dwLength
);
440 /* reset track data */
441 mmt
->wStatus
= 1; /* ok, playing */
442 mmt
->dwIndex
= mmt
->dwFirst
;
443 mmt
->dwEventPulse
= 0;
445 if (mmioSeek(wmm
->hFile
, 0, SEEK_CUR
) != mmt
->dwLast
) {
446 WARN("Ouch, out of sync seek=%u track=%u\n",
447 mmioSeek(wmm
->hFile
, 0, SEEK_CUR
), mmt
->dwLast
);
448 /* position at end of this track, to be ready to read next track */
449 mmioSeek(wmm
->hFile
, mmt
->dwLast
, SEEK_SET
);
455 /**************************************************************************
456 * MIDI_mciReadMThd [internal]
458 static DWORD
MIDI_mciReadMThd(WINE_MCIMIDI
* wmm
, DWORD dwOffset
)
464 TRACE("(%p, %08X);\n", wmm
, dwOffset
);
466 if (mmioSeek(wmm
->hFile
, dwOffset
, SEEK_SET
) != dwOffset
) {
467 WARN("Can't seek at %08X begin of 'MThd'\n", dwOffset
);
468 return MCIERR_INVALID_FILE
;
470 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
,
471 (long) sizeof(FOURCC
)) != (long) sizeof(FOURCC
))
472 return MCIERR_INVALID_FILE
;
474 if (fourcc
!= mmioFOURCC('M', 'T', 'h', 'd')) {
475 WARN("Can't synchronize on 'MThd' !\n");
476 return MCIERR_INVALID_FILE
;
479 if (MIDI_mciReadLong(wmm
, &toberead
) != 0 || toberead
< 3 * sizeof(WORD
))
480 return MCIERR_INVALID_FILE
;
482 if (MIDI_mciReadWord(wmm
, &wmm
->wFormat
) != 0 ||
483 MIDI_mciReadWord(wmm
, &wmm
->nTracks
) != 0 ||
484 MIDI_mciReadWord(wmm
, &wmm
->nDivision
) != 0) {
485 return MCIERR_INVALID_FILE
;
488 TRACE("toberead=0x%08X, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
489 toberead
, wmm
->wFormat
, wmm
->nTracks
, wmm
->nDivision
);
491 /* MS doc says that the MIDI MCI time format must be put by default to the format
492 * stored in the MIDI file...
494 if (wmm
->nDivision
> 0x8000) {
495 /* eric.pouech@lemel.fr 98/11
496 * I did not check this very code (pulses are expressed as SMPTE sub-frames).
497 * In about 40 MB of MIDI files I have, none was SMPTE based...
498 * I'm just wondering if this is widely used :-). So, if someone has one of
499 * these files, I'd like to know about it.
501 FIXME("Handling SMPTE time in MIDI files has not been tested\n"
502 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
504 switch (HIBYTE(wmm
->nDivision
)) {
505 case 0xE8: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
; break; /* -24 */
506 case 0xE7: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
; break; /* -25 */
507 case 0xE3: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
508 case 0xE2: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
; break; /* -30 */
510 WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm
->nDivision
));
511 return MCIERR_INVALID_FILE
;
513 switch (LOBYTE(wmm
->nDivision
)) {
514 case 4: /* MIDI Time Code */
517 case 80: /* SMPTE bit resolution */
520 WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm
->nDivision
));
521 return MCIERR_INVALID_FILE
;
523 } else if (wmm
->nDivision
== 0) {
524 WARN("Number of division is 0, can't support that !!\n");
525 return MCIERR_INVALID_FILE
;
527 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
530 switch (wmm
->wFormat
) {
532 if (wmm
->nTracks
!= 1) {
533 WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
541 WARN("Handling MIDI files which format = %d is not (yet) supported\n"
542 "Please report with MIDI file !\n", wmm
->wFormat
);
543 return MCIERR_INVALID_FILE
;
546 if (wmm
->nTracks
& 0x8000) {
547 /* this shouldn't be a problem... */
548 WARN("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
549 wmm
->nTracks
= 0x7FFF;
552 if ((wmm
->tracks
= HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK
) * wmm
->nTracks
)) == NULL
) {
553 return MCIERR_OUT_OF_MEMORY
;
556 toberead
-= 3 * sizeof(WORD
);
558 TRACE("Size of MThd > 6, skipping %d extra bytes\n", toberead
);
559 mmioSeek(wmm
->hFile
, toberead
, SEEK_CUR
);
562 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
563 wmm
->tracks
[nt
].wTrackNr
= nt
;
564 if (MIDI_mciReadMTrk(wmm
, &wmm
->tracks
[nt
]) != 0) {
565 WARN("Can't read 'MTrk' header\n");
566 return MCIERR_INVALID_FILE
;
570 wmm
->dwTempo
= 500000;
575 /**************************************************************************
576 * MIDI_ConvertPulseToMS [internal]
578 static DWORD
MIDI_ConvertPulseToMS(WINE_MCIMIDI
* wmm
, DWORD pulse
)
582 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
583 * may change during file playing
585 if (wmm
->nDivision
== 0) {
586 FIXME("Shouldn't happen. wmm->nDivision = 0\n");
587 } else if (wmm
->nDivision
> 0x8000) { /* SMPTE, unchecked FIXME? */
588 int nf
= -(char)HIBYTE(wmm
->nDivision
); /* number of frames */
589 int nsf
= LOBYTE(wmm
->nDivision
); /* number of sub-frames */
590 ret
= (pulse
* 1000) / (nf
* nsf
);
592 ret
= (DWORD
)((double)pulse
* ((double)wmm
->dwTempo
/ 1000) /
593 (double)wmm
->nDivision
);
597 TRACE("pulse=%u tempo=%u division=%u=0x%04x => ms=%u\n",
598 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
604 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
605 #define TIME_MS_IN_ONE_MINUTE (60*1000)
606 #define TIME_MS_IN_ONE_SECOND (1000)
608 /**************************************************************************
609 * MIDI_ConvertTimeFormatToMS [internal]
611 static DWORD
MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI
* wmm
, DWORD val
)
615 switch (wmm
->dwMciTimeFormat
) {
616 case MCI_FORMAT_MILLISECONDS
:
619 case MCI_FORMAT_SMPTE_24
:
621 (HIBYTE(HIWORD(val
)) * 125) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
622 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
624 case MCI_FORMAT_SMPTE_25
:
626 HIBYTE(HIWORD(val
)) * 40 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
627 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
629 case MCI_FORMAT_SMPTE_30
:
631 (HIBYTE(HIWORD(val
)) * 100) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
632 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
635 WARN("Bad time format %u!\n", wmm
->dwMciTimeFormat
);
638 TRACE("val=%u=0x%08x [tf=%u] => ret=%u\n", val, val, wmm->dwMciTimeFormat, ret);
643 /**************************************************************************
644 * MIDI_ConvertMSToTimeFormat [internal]
646 static DWORD
MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI
* wmm
, DWORD _val
)
648 DWORD ret
= 0, val
= _val
;
651 switch (wmm
->dwMciTimeFormat
) {
652 case MCI_FORMAT_MILLISECONDS
:
655 case MCI_FORMAT_SMPTE_24
:
656 case MCI_FORMAT_SMPTE_25
:
657 case MCI_FORMAT_SMPTE_30
:
658 h
= val
/ TIME_MS_IN_ONE_HOUR
;
659 m
= (val
-= h
* TIME_MS_IN_ONE_HOUR
) / TIME_MS_IN_ONE_MINUTE
;
660 s
= (val
-= m
* TIME_MS_IN_ONE_MINUTE
) / TIME_MS_IN_ONE_SECOND
;
661 switch (wmm
->dwMciTimeFormat
) {
662 case MCI_FORMAT_SMPTE_24
:
663 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
664 f
= (val
* 3) / 125; val
-= (f
* 125) / 3;
666 case MCI_FORMAT_SMPTE_25
:
667 /* one frame is 1000/25 ms long, 1000/25 == 40 */
668 f
= val
/ 40; val
-= f
* 40;
670 case MCI_FORMAT_SMPTE_30
:
671 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
672 f
= (val
* 3) / 100; val
-= (f
* 100) / 3;
675 FIXME("There must be some bad bad programmer\n");
678 /* val contains the number of ms which cannot make a complete frame */
679 /* FIXME: is this correct ? programs seem to be happy with that */
680 ret
= (f
<< 24) | (s
<< 16) | (m
<< 8) | (h
<< 0);
683 WARN("Bad time format %u!\n", wmm
->dwMciTimeFormat
);
686 TRACE("val=%u [tf=%u] => ret=%u=0x%08x\n", _val, wmm->dwMciTimeFormat, ret, ret);
691 /**************************************************************************
692 * MIDI_GetMThdLengthMS [internal]
694 static DWORD
MIDI_GetMThdLengthMS(WINE_MCIMIDI
* wmm
)
699 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
700 if (wmm
->wFormat
== 2) {
701 ret
+= wmm
->tracks
[nt
].dwLength
;
702 } else if (wmm
->tracks
[nt
].dwLength
> ret
) {
703 ret
= wmm
->tracks
[nt
].dwLength
;
706 /* FIXME: this is wrong if there is a tempo change inside the file */
707 return MIDI_ConvertPulseToMS(wmm
, ret
);
710 /**************************************************************************
711 * MIDI_mciOpen [internal]
713 static DWORD
MIDI_mciOpen(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_OPEN_PARMSW lpParms
)
717 TRACE("(%d, %08X, %p)\n", wmm
->wDevID
, dwFlags
, lpParms
);
719 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
720 if (dwFlags
& MCI_OPEN_SHAREABLE
)
721 return MCIERR_HARDWARE
;
723 if (wmm
->nUseCount
> 0) {
724 /* The driver is already opened on this channel
725 * MIDI sequencer cannot be shared
727 return MCIERR_DEVICE_OPEN
;
733 wmm
->wPort
= MIDI_MAPPER
;
734 wmm
->lpstrElementName
= NULL
;
736 TRACE("wDevID=%d (lpParams->wDeviceID=%d)\n", wmm
->wDevID
, lpParms
->wDeviceID
);
737 /* lpParms->wDeviceID = wDevID;*/
739 if (dwFlags
& MCI_OPEN_ELEMENT
) {
740 TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpParms
->lpstrElementName
));
741 if (lpParms
->lpstrElementName
&& strlenW(lpParms
->lpstrElementName
) > 0) {
742 wmm
->hFile
= mmioOpenW((LPWSTR
)lpParms
->lpstrElementName
, NULL
,
743 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
744 if (wmm
->hFile
== 0) {
745 WARN("Can't find file %s!\n", debugstr_w(lpParms
->lpstrElementName
));
747 return MCIERR_FILE_NOT_FOUND
;
749 wmm
->lpstrElementName
= HeapAlloc(GetProcessHeap(), 0,
750 (strlenW(lpParms
->lpstrElementName
) + 1) * sizeof(WCHAR
));
751 strcpyW(wmm
->lpstrElementName
, lpParms
->lpstrElementName
);
754 TRACE("hFile=%p\n", wmm
->hFile
);
756 wmm
->lpstrCopyright
= NULL
;
757 wmm
->lpstrName
= NULL
;
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
;
803 if (dwFlags
& MCI_NOTIFY
)
804 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
809 /**************************************************************************
810 * MIDI_mciStop [internal]
812 static DWORD
MIDI_mciStop(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
816 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
818 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
819 HANDLE old
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
820 if (old
) mciDriverNotify(old
, wmm
->wDevID
, MCI_NOTIFY_ABORTED
);
823 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
824 int oldstat
= wmm
->dwStatus
;
826 wmm
->dwStatus
= MCI_MODE_NOT_READY
;
827 if (oldstat
== MCI_MODE_PAUSE
)
828 dwRet
= midiOutReset((HMIDIOUT
)wmm
->hMidi
);
830 while (wmm
->dwStatus
!= MCI_MODE_STOP
)
835 wmm
->dwStatus
= MCI_MODE_STOP
;
837 if ((dwFlags
& MCI_NOTIFY
) && lpParms
&& MMSYSERR_NOERROR
==dwRet
)
838 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
842 /**************************************************************************
843 * MIDI_mciClose [internal]
845 static DWORD
MIDI_mciClose(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
848 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
850 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
851 /* mciStop handles MCI_NOTIFY_ABORTED */
852 MIDI_mciStop(wmm
, MCI_WAIT
, lpParms
);
856 if (wmm
->nUseCount
== 0) {
857 if (wmm
->hFile
!= 0) {
858 mmioClose(wmm
->hFile
, 0);
860 TRACE("hFile closed !\n");
862 HeapFree(GetProcessHeap(), 0, wmm
->tracks
);
863 HeapFree(GetProcessHeap(), 0, wmm
->lpstrElementName
);
864 HeapFree(GetProcessHeap(), 0, wmm
->lpstrCopyright
);
865 HeapFree(GetProcessHeap(), 0, wmm
->lpstrName
);
867 TRACE("Shouldn't happen... nUseCount=%d\n", wmm
->nUseCount
);
868 return MCIERR_INTERNAL
;
871 if ((dwFlags
& MCI_NOTIFY
) && lpParms
) {
872 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
877 /**************************************************************************
878 * MIDI_mciFindNextEvent [internal]
880 static MCI_MIDITRACK
* MIDI_mciFindNextEvent(WINE_MCIMIDI
* wmm
, LPDWORD hiPulse
)
885 *hiPulse
= 0xFFFFFFFFul
;
887 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
888 mmt
= &wmm
->tracks
[nt
];
890 if (mmt
->wStatus
== 0)
892 if (mmt
->dwEventPulse
< *hiPulse
) {
893 *hiPulse
= mmt
->dwEventPulse
;
897 return (cnt
== 0xFFFFu
) ? 0 /* no more event on all tracks */
901 /**************************************************************************
902 * MIDI_mciPlay [internal]
904 static DWORD
MIDI_mciPlay(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_PLAY_PARMS lpParms
)
906 DWORD dwStartMS
, dwEndMS
;
913 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
915 if (wmm
->hFile
== 0) {
916 WARN("Can't play: no file %s!\n", debugstr_w(wmm
->lpstrElementName
));
917 return MCIERR_FILE_NOT_FOUND
;
920 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
921 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
922 /* FIXME: parameters (start/end) in lpParams may not be used */
923 return MIDI_mciResume(wmm
, dwFlags
, (LPMCI_GENERIC_PARMS
)lpParms
);
925 WARN("Can't play: device is not stopped !\n");
926 return MCIERR_INTERNAL
;
929 if (!(dwFlags
& MCI_WAIT
)) {
930 return MCI_SendCommandAsync(wmm
->wDevID
, MCI_PLAY
, dwFlags
, (DWORD_PTR
)lpParms
, sizeof(MCI_PLAY_PARMS
));
933 if (lpParms
&& (dwFlags
& MCI_FROM
)) {
934 dwStartMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwFrom
);
936 dwStartMS
= wmm
->dwPositionMS
;
939 if (lpParms
&& (dwFlags
& MCI_TO
)) {
940 dwEndMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
942 dwEndMS
= 0xFFFFFFFFul
;
945 TRACE("Playing from %u to %u\n", dwStartMS
, dwEndMS
);
947 oldcb
= InterlockedExchangePointer(&wmm
->hCallback
,
948 (dwFlags
& MCI_NOTIFY
) ? HWND_32(LOWORD(lpParms
->dwCallback
)) : NULL
);
949 if (oldcb
) mciDriverNotify(oldcb
, wmm
->wDevID
, MCI_NOTIFY_ABORTED
);
953 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
954 mmt
= &wmm
->tracks
[nt
];
956 mmt
->wStatus
= 1; /* ok, playing */
957 mmt
->dwIndex
= mmt
->dwFirst
;
958 if (wmm
->wFormat
== 2 && nt
> 0) {
959 mmt
->dwEventPulse
= wmm
->tracks
[nt
- 1].dwLength
;
961 mmt
->dwEventPulse
= 0;
963 MIDI_mciReadNextEvent(wmm
, mmt
); /* FIXME == 0 */
966 dwRet
= midiOutOpen((LPHMIDIOUT
)&wmm
->hMidi
, wmm
->wPort
, 0L, 0L, CALLBACK_NULL
);
967 if (dwRet
!= MMSYSERR_NOERROR
) {
972 wmm
->dwTempo
= 500000;
973 wmm
->dwStatus
= MCI_MODE_PLAY
;
974 wmm
->dwPositionMS
= 0;
975 wmm
->wStartedPlaying
= FALSE
;
977 while (wmm
->dwStatus
!= MCI_MODE_STOP
&& wmm
->dwStatus
!= MCI_MODE_NOT_READY
) {
978 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
979 * too much. Tell gcc not to optimize status value using volatile.
981 while (((volatile WINE_MCIMIDI
*)wmm
)->dwStatus
== MCI_MODE_PAUSE
);
983 doPlay
= (wmm
->dwPositionMS
>= dwStartMS
&& wmm
->dwPositionMS
<= dwEndMS
);
985 TRACE("wmm->dwStatus=%d, doPlay=%c\n", wmm
->dwStatus
, doPlay
? 'T' : 'F');
987 if ((mmt
= MIDI_mciFindNextEvent(wmm
, &hiPulse
)) == NULL
)
988 break; /* no more event on tracks */
990 /* if starting playing, then set StartTicks to the value it would have had
991 * if play had started at position 0
993 if (doPlay
&& !wmm
->wStartedPlaying
) {
994 wmm
->dwStartTicks
= GetTickCount() - MIDI_ConvertPulseToMS(wmm
, wmm
->dwPulse
);
995 wmm
->wStartedPlaying
= TRUE
;
996 TRACE("Setting dwStartTicks to %u\n", wmm
->dwStartTicks
);
999 if (hiPulse
> wmm
->dwPulse
) {
1000 wmm
->dwPositionMS
+= MIDI_ConvertPulseToMS(wmm
, hiPulse
- wmm
->dwPulse
);
1002 DWORD togo
= wmm
->dwStartTicks
+ wmm
->dwPositionMS
;
1003 DWORD tc
= GetTickCount();
1005 TRACE("Pulses hi=0x%08x <> cur=0x%08x\n", hiPulse
, wmm
->dwPulse
);
1006 TRACE("Wait until %u => %u ms\n",
1007 tc
- wmm
->dwStartTicks
, togo
- wmm
->dwStartTicks
);
1011 wmm
->dwPulse
= hiPulse
;
1014 switch (LOBYTE(LOWORD(mmt
->dwEventData
))) {
1016 case 0xF7: /* sysex events */
1018 FIXME("Not handling SysEx events (yet)\n");
1022 /* position after meta data header */
1023 mmioSeek(wmm
->hFile
, mmt
->dwIndex
+ HIWORD(mmt
->dwEventData
), SEEK_SET
);
1024 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
1025 case 0x00: /* 16-bit sequence number */
1026 if (TRACE_ON(mcimidi
)) {
1029 MIDI_mciReadWord(wmm
, &twd
); /* == 0 */
1030 TRACE("Got sequence number %u\n", twd
);
1033 case 0x01: /* any text */
1034 case 0x02: /* Copyright Message text */
1035 case 0x03: /* Sequence/Track Name text */
1036 case 0x04: /* Instrument Name text */
1037 case 0x05: /* Lyric text */
1038 case 0x06: /* Marker text */
1039 case 0x07: /* Cue-point text */
1040 if (TRACE_ON(mcimidi
)) {
1042 WORD len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
1043 static const char* const info
[8] = {"", "Text", "Copyright", "Seq/Trk name",
1044 "Instrument", "Lyric", "Marker", "Cue-point"};
1045 WORD idx
= HIBYTE(LOWORD(mmt
->dwEventData
));
1047 if (len
>= sizeof(buf
)) {
1048 WARN("Buffer for text is too small (%u are needed)\n", len
);
1049 len
= sizeof(buf
) - 1;
1051 if (mmioRead(wmm
->hFile
, buf
, len
) == len
) {
1052 buf
[len
] = 0; /* end string in case */
1053 TRACE("%s => \"%s\"\n", (idx
< 8 ) ? info
[idx
] : "", buf
);
1055 WARN("Couldn't read data for %s\n", (idx
< 8) ? info
[idx
] : "");
1060 /* MIDI channel (cc) */
1061 if (FIXME_ON(mcimidi
)) {
1064 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
1065 FIXME("NIY: MIDI channel=%u, track=%u\n", bt
, mmt
->wTrackNr
);
1069 /* MIDI port (pp) */
1070 if (FIXME_ON(mcimidi
)) {
1073 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
1074 FIXME("NIY: MIDI port=%u, track=%u\n", bt
, mmt
->wTrackNr
);
1077 case 0x2F: /* end of track */
1080 case 0x51:/* set tempo */
1081 /* Tempo is expressed in -seconds per midi quarter note
1082 * for format 1 MIDI files, this can only be present on track #0
1084 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
1085 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt
->wTrackNr
);
1090 MIDI_mciReadByte(wmm
, &tbt
); value
= ((DWORD
)tbt
) << 16;
1091 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 8;
1092 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 0;
1093 TRACE("Setting tempo to %d (BPM=%d)\n", wmm
->dwTempo
, (value
) ? (60000000 / value
) : 0);
1094 wmm
->dwTempo
= value
;
1097 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
1098 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
1099 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt
->wTrackNr
);
1100 } if (mmt
->dwEventPulse
!= 0) {
1101 WARN("SMPTE track start can only be expressed at start of track (%u)\n", mmt
->dwEventPulse
);
1103 BYTE h
, m
, s
, f
, ff
;
1105 MIDI_mciReadByte(wmm
, &h
);
1106 MIDI_mciReadByte(wmm
, &m
);
1107 MIDI_mciReadByte(wmm
, &s
);
1108 MIDI_mciReadByte(wmm
, &f
);
1109 MIDI_mciReadByte(wmm
, &ff
);
1110 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h
, m
, s
, f
, ff
);
1113 case 0x58: /* file rhythm */
1114 if (TRACE_ON(mcimidi
)) {
1115 BYTE num
, den
, cpmc
, _32npqn
;
1117 MIDI_mciReadByte(wmm
, &num
);
1118 MIDI_mciReadByte(wmm
, &den
); /* to notate e.g. 6/8 */
1119 MIDI_mciReadByte(wmm
, &cpmc
); /* number of MIDI clocks per metronome click */
1120 MIDI_mciReadByte(wmm
, &_32npqn
); /* number of notated 32nd notes per MIDI quarter note */
1122 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num
, 1 << den
, cpmc
, _32npqn
);
1125 case 0x59: /* key signature */
1126 if (TRACE_ON(mcimidi
)) {
1129 MIDI_mciReadByte(wmm
, &sf
);
1130 MIDI_mciReadByte(wmm
, &mm
);
1132 if (sf
>= 0x80) TRACE("%d flats\n", -(char)sf
);
1133 else if (sf
> 0) TRACE("%d sharps\n", (char)sf
);
1134 else TRACE("Key of C\n");
1135 TRACE("Mode: %s\n", (mm
== 0) ? "major" : "minor");
1139 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt
->dwEventData
)));
1145 dwRet
= midiOutShortMsg((HMIDIOUT
)wmm
->hMidi
, mmt
->dwEventData
);
1147 switch (LOBYTE(LOWORD(mmt
->dwEventData
)) & 0xF0) {
1153 dwRet
= midiOutShortMsg((HMIDIOUT
)wmm
->hMidi
, mmt
->dwEventData
);
1157 mmt
->dwIndex
+= mmt
->wEventLength
;
1158 if (mmt
->dwIndex
< mmt
->dwFirst
|| mmt
->dwIndex
>= mmt
->dwLast
) {
1162 MIDI_mciReadNextEvent(wmm
, mmt
);
1166 midiOutReset((HMIDIOUT
)wmm
->hMidi
);
1168 dwRet
= midiOutClose((HMIDIOUT
)wmm
->hMidi
);
1170 if (dwFlags
& MCI_NOTIFY
)
1171 oldcb
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
1173 wmm
->dwStatus
= MCI_MODE_STOP
;
1175 /* Let the potentially asynchronous commands support FAILURE notification. */
1176 if (oldcb
) mciDriverNotify(oldcb
, wmm
->wDevID
,
1177 dwRet
? MCI_NOTIFY_FAILURE
: MCI_NOTIFY_SUCCESSFUL
);
1181 /**************************************************************************
1182 * MIDI_mciPause [internal]
1184 static DWORD
MIDI_mciPause(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1186 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1188 if (wmm
->dwStatus
== MCI_MODE_PLAY
) {
1189 /* stop all notes */
1191 for (chn
= 0; chn
< 16; chn
++)
1192 midiOutShortMsg((HMIDIOUT
)(wmm
->hMidi
), 0x78B0 | chn
);
1193 wmm
->dwStatus
= MCI_MODE_PAUSE
;
1195 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1196 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1200 /**************************************************************************
1201 * MIDI_mciResume [internal]
1203 static DWORD
MIDI_mciResume(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1205 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1207 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
1208 wmm
->wStartedPlaying
= FALSE
;
1209 wmm
->dwStatus
= MCI_MODE_PLAY
;
1211 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1212 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1216 /**************************************************************************
1217 * MIDI_mciSet [internal]
1219 static DWORD
MIDI_mciSet(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_SEQ_SET_PARMS lpParms
)
1221 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1223 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1225 if (dwFlags
& MCI_SET_TIME_FORMAT
) {
1226 switch (lpParms
->dwTimeFormat
) {
1227 case MCI_FORMAT_MILLISECONDS
:
1228 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1229 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
1231 case MCI_FORMAT_SMPTE_24
:
1232 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1233 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
;
1235 case MCI_FORMAT_SMPTE_25
:
1236 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1237 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
;
1239 case MCI_FORMAT_SMPTE_30
:
1240 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1241 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
;
1244 WARN("Bad time format %u!\n", lpParms
->dwTimeFormat
);
1245 return MCIERR_BAD_TIME_FORMAT
;
1248 if (dwFlags
& MCI_SET_VIDEO
) {
1249 TRACE("No support for video !\n");
1250 return MCIERR_UNSUPPORTED_FUNCTION
;
1252 if (dwFlags
& MCI_SET_DOOR_OPEN
) {
1253 TRACE("No support for door open !\n");
1254 return MCIERR_UNSUPPORTED_FUNCTION
;
1256 if (dwFlags
& MCI_SET_DOOR_CLOSED
) {
1257 TRACE("No support for door close !\n");
1258 return MCIERR_UNSUPPORTED_FUNCTION
;
1260 if (dwFlags
& MCI_SET_AUDIO
) {
1261 if (dwFlags
& MCI_SET_ON
) {
1262 TRACE("MCI_SET_ON audio !\n");
1263 } else if (dwFlags
& MCI_SET_OFF
) {
1264 TRACE("MCI_SET_OFF audio !\n");
1266 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1267 return MCIERR_BAD_INTEGER
;
1270 switch (lpParms
->dwAudio
)
1272 case MCI_SET_AUDIO_ALL
: TRACE("MCI_SET_AUDIO_ALL !\n"); break;
1273 case MCI_SET_AUDIO_LEFT
: TRACE("MCI_SET_AUDIO_LEFT !\n"); break;
1274 case MCI_SET_AUDIO_RIGHT
: TRACE("MCI_SET_AUDIO_RIGHT !\n"); break;
1275 default: WARN("Unknown audio channel %u\n", lpParms
->dwAudio
); break;
1279 if (dwFlags
& MCI_SEQ_SET_MASTER
)
1280 TRACE("MCI_SEQ_SET_MASTER !\n");
1281 if (dwFlags
& MCI_SEQ_SET_SLAVE
)
1282 TRACE("MCI_SEQ_SET_SLAVE !\n");
1283 if (dwFlags
& MCI_SEQ_SET_OFFSET
)
1284 TRACE("MCI_SEQ_SET_OFFSET !\n");
1285 if (dwFlags
& MCI_SEQ_SET_PORT
) {
1286 TRACE("MCI_SEQ_SET_PORT = %d\n", lpParms
->dwPort
);
1287 if ((UINT16
)lpParms
->dwPort
!= (UINT16
)MIDI_MAPPER
&&
1288 (UINT16
)lpParms
->dwPort
>= midiOutGetNumDevs())
1289 /* FIXME: input/output port distinction? */
1290 return MCIERR_SEQ_PORT_NONEXISTENT
;
1291 /* FIXME: Native manages to swap the device while playing! */
1292 wmm
->wPort
= lpParms
->dwPort
;
1294 if (dwFlags
& MCI_SEQ_SET_TEMPO
)
1295 TRACE("MCI_SEQ_SET_TEMPO !\n");
1296 if (dwFlags
& MCI_NOTIFY
)
1297 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1301 /**************************************************************************
1302 * MIDI_mciStatus [internal]
1304 static DWORD
MIDI_mciStatus(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_STATUS_PARMS lpParms
)
1308 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1310 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1312 if (dwFlags
& MCI_STATUS_ITEM
) {
1313 switch (lpParms
->dwItem
) {
1314 case MCI_STATUS_CURRENT_TRACK
:
1315 /* FIXME in Format 2 */
1316 lpParms
->dwReturn
= 1;
1317 TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms
->dwReturn
);
1319 case MCI_STATUS_LENGTH
:
1320 if ((dwFlags
& MCI_TRACK
) && wmm
->wFormat
== 2) {
1321 if (lpParms
->dwTrack
>= wmm
->nTracks
)
1322 return MCIERR_BAD_INTEGER
;
1323 /* FIXME: this is wrong if there is a tempo change inside the file */
1324 lpParms
->dwReturn
= MIDI_ConvertPulseToMS(wmm
, wmm
->tracks
[lpParms
->dwTrack
].dwLength
);
1326 lpParms
->dwReturn
= MIDI_GetMThdLengthMS(wmm
);
1328 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
, lpParms
->dwReturn
);
1329 TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms
->dwReturn
);
1331 case MCI_STATUS_MODE
:
1332 TRACE("MCI_STATUS_MODE => %u\n", wmm
->dwStatus
);
1333 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwStatus
, wmm
->dwStatus
);
1334 ret
= MCI_RESOURCE_RETURNED
;
1336 case MCI_STATUS_MEDIA_PRESENT
:
1337 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1338 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1339 ret
= MCI_RESOURCE_RETURNED
;
1341 case MCI_STATUS_NUMBER_OF_TRACKS
:
1342 lpParms
->dwReturn
= (wmm
->wFormat
== 2) ? wmm
->nTracks
: 1;
1343 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms
->dwReturn
);
1345 case MCI_STATUS_POSITION
:
1346 /* FIXME: do I need to use MCI_TRACK ? */
1347 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
,
1348 (dwFlags
& MCI_STATUS_START
) ? 0 : wmm
->dwPositionMS
);
1349 TRACE("MCI_STATUS_POSITION %s => %lu\n",
1350 (dwFlags
& MCI_STATUS_START
) ? "start" : "current", lpParms
->dwReturn
);
1352 case MCI_STATUS_READY
:
1353 lpParms
->dwReturn
= (wmm
->dwStatus
== MCI_MODE_NOT_READY
) ?
1354 MAKEMCIRESOURCE(FALSE
, MCI_FALSE
) : MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1355 ret
= MCI_RESOURCE_RETURNED
;
1356 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms
->dwReturn
));
1358 case MCI_STATUS_TIME_FORMAT
:
1359 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwMciTimeFormat
, MCI_FORMAT_RETURN_BASE
+ wmm
->dwMciTimeFormat
);
1360 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms
->dwReturn
));
1361 ret
= MCI_RESOURCE_RETURNED
;
1363 case MCI_SEQ_STATUS_DIVTYPE
:
1364 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1365 if (wmm
->nDivision
> 0x8000) {
1366 switch (wmm
->nDivision
) {
1367 case 0xE8: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_24
; break; /* -24 */
1368 case 0xE7: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_25
; break; /* -25 */
1369 case 0xE3: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
1370 case 0xE2: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30
; break; /* -30 */
1371 default: FIXME("There is a bad bad programmer\n");
1374 lpParms
->dwReturn
= MCI_SEQ_DIV_PPQN
;
1376 lpParms
->dwReturn
= MAKEMCIRESOURCE(lpParms
->dwReturn
,lpParms
->dwReturn
);
1377 ret
= MCI_RESOURCE_RETURNED
;
1379 case MCI_SEQ_STATUS_MASTER
:
1380 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1381 lpParms
->dwReturn
= 0;
1383 case MCI_SEQ_STATUS_SLAVE
:
1384 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1385 lpParms
->dwReturn
= 0;
1387 case MCI_SEQ_STATUS_OFFSET
:
1388 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1389 lpParms
->dwReturn
= 0;
1391 case MCI_SEQ_STATUS_PORT
:
1392 if (wmm
->wPort
!= (UINT16
)MIDI_MAPPER
)
1393 lpParms
->dwReturn
= wmm
->wPort
;
1395 lpParms
->dwReturn
= MAKEMCIRESOURCE(MIDI_MAPPER
, MCI_SEQ_MAPPER_S
);
1396 ret
= MCI_RESOURCE_RETURNED
;
1398 TRACE("MCI_SEQ_STATUS_PORT (%u) => %d\n", wmm
->wDevID
, wmm
->wPort
);
1400 case MCI_SEQ_STATUS_TEMPO
:
1401 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1402 lpParms
->dwReturn
= wmm
->dwTempo
;
1405 FIXME("Unknown command %08X !\n", lpParms
->dwItem
);
1406 return MCIERR_UNRECOGNIZED_COMMAND
;
1409 WARN("No Status-Item!\n");
1410 return MCIERR_UNRECOGNIZED_COMMAND
;
1412 if ((dwFlags
& MCI_NOTIFY
) && HRESULT_CODE(ret
)==0)
1413 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1417 /**************************************************************************
1418 * MIDI_mciGetDevCaps [internal]
1420 static DWORD
MIDI_mciGetDevCaps(WINE_MCIMIDI
* wmm
, DWORD dwFlags
,
1421 LPMCI_GETDEVCAPS_PARMS lpParms
)
1425 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1427 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1429 if (dwFlags
& MCI_GETDEVCAPS_ITEM
) {
1430 switch (lpParms
->dwItem
) {
1431 case MCI_GETDEVCAPS_DEVICE_TYPE
:
1432 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1433 lpParms
->dwReturn
= MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER
, MCI_DEVTYPE_SEQUENCER
);
1434 ret
= MCI_RESOURCE_RETURNED
;
1436 case MCI_GETDEVCAPS_HAS_AUDIO
:
1437 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1438 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1439 ret
= MCI_RESOURCE_RETURNED
;
1441 case MCI_GETDEVCAPS_HAS_VIDEO
:
1442 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1443 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1444 ret
= MCI_RESOURCE_RETURNED
;
1446 case MCI_GETDEVCAPS_USES_FILES
:
1447 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1448 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1449 ret
= MCI_RESOURCE_RETURNED
;
1451 case MCI_GETDEVCAPS_COMPOUND_DEVICE
:
1452 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1453 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1454 ret
= MCI_RESOURCE_RETURNED
;
1456 case MCI_GETDEVCAPS_CAN_EJECT
:
1457 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1458 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1459 ret
= MCI_RESOURCE_RETURNED
;
1461 case MCI_GETDEVCAPS_CAN_PLAY
:
1462 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1463 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1464 ret
= MCI_RESOURCE_RETURNED
;
1466 case MCI_GETDEVCAPS_CAN_RECORD
:
1467 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1468 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1469 ret
= MCI_RESOURCE_RETURNED
;
1471 case MCI_GETDEVCAPS_CAN_SAVE
:
1472 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1473 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1474 ret
= MCI_RESOURCE_RETURNED
;
1477 FIXME("Unknown capability (%08x) !\n", lpParms
->dwItem
);
1478 return MCIERR_UNRECOGNIZED_COMMAND
;
1481 WARN("No GetDevCaps-Item !\n");
1482 return MCIERR_UNRECOGNIZED_COMMAND
;
1484 if ((dwFlags
& MCI_NOTIFY
) && HRESULT_CODE(ret
)==0)
1485 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1489 /**************************************************************************
1490 * MIDI_mciInfo [internal]
1492 static DWORD
MIDI_mciInfo(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_INFO_PARMSW lpParms
)
1496 static const WCHAR wszMidiSeq
[] = {'W','i','n','e','\'','s',' ','M','I','D','I',' ','s','e','q','u','e','n','c','e','r',0};
1498 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1500 if (lpParms
== NULL
|| lpParms
->lpstrReturn
== NULL
)
1501 return MCIERR_NULL_PARAMETER_BLOCK
;
1503 TRACE("buf=%p, len=%u\n", lpParms
->lpstrReturn
, lpParms
->dwRetSize
);
1505 switch (dwFlags
& ~(MCI_WAIT
|MCI_NOTIFY
)) {
1506 case MCI_INFO_PRODUCT
: str
= wszMidiSeq
; break;
1507 case MCI_INFO_FILE
: str
= wmm
->lpstrElementName
; break;
1508 case MCI_INFO_COPYRIGHT
: str
= wmm
->lpstrCopyright
; break;
1509 case MCI_INFO_NAME
: str
= wmm
->lpstrName
; break;
1511 WARN("Don't know this info command (%u)\n", dwFlags
);
1512 return MCIERR_UNRECOGNIZED_COMMAND
;
1515 if (lpParms
->dwRetSize
) {
1517 /* FIXME? Since NT, mciwave, mciseq and mcicda set dwRetSize
1518 * to the number of characters written, excluding \0. */
1519 lstrcpynW(lpParms
->lpstrReturn
, str
? str
: &zero
, lpParms
->dwRetSize
);
1520 } else ret
= MCIERR_PARAM_OVERFLOW
;
1522 if (MMSYSERR_NOERROR
==ret
&& (dwFlags
& MCI_NOTIFY
))
1523 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1527 /**************************************************************************
1528 * MIDI_mciSeek [internal]
1530 static DWORD
MIDI_mciSeek(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_SEEK_PARMS lpParms
)
1534 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1536 if (lpParms
== NULL
) {
1537 ret
= MCIERR_NULL_PARAMETER_BLOCK
;
1539 MIDI_mciStop(wmm
, MCI_WAIT
, 0);
1541 if (dwFlags
& MCI_SEEK_TO_START
) {
1542 wmm
->dwPositionMS
= 0;
1543 } else if (dwFlags
& MCI_SEEK_TO_END
) {
1544 wmm
->dwPositionMS
= 0xFFFFFFFF; /* FIXME */
1545 } else if (dwFlags
& MCI_TO
) {
1546 wmm
->dwPositionMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
1548 WARN("dwFlag doesn't tell where to seek to...\n");
1549 return MCIERR_MISSING_PARAMETER
;
1552 TRACE("Seeking to position=%u ms\n", wmm
->dwPositionMS
);
1554 if (dwFlags
& MCI_NOTIFY
)
1555 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1560 /*======================================================================*
1561 * MIDI entry points *
1562 *======================================================================*/
1564 /**************************************************************************
1565 * DriverProc (MCISEQ.@)
1567 LRESULT CALLBACK
MCIMIDI_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1568 LPARAM dwParam1
, LPARAM dwParam2
)
1572 case DRV_LOAD
: return 1;
1573 case DRV_FREE
: return 1;
1574 case DRV_ENABLE
: return 1;
1575 case DRV_DISABLE
: return 1;
1576 case DRV_QUERYCONFIGURE
: return 1;
1577 case DRV_CONFIGURE
: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK
); return 1;
1578 case DRV_INSTALL
: return DRVCNF_RESTART
;
1579 case DRV_REMOVE
: return DRVCNF_RESTART
;
1580 case DRV_OPEN
: return MIDI_drvOpen((LPCWSTR
)dwParam1
, (LPMCI_OPEN_DRIVER_PARMSW
)dwParam2
);
1581 case DRV_CLOSE
: return MIDI_drvClose(dwDevID
);
1583 if ((wMsg
< DRV_MCI_FIRST
) || (wMsg
> DRV_MCI_LAST
)) {
1584 TRACE("Sending msg %04x to default driver proc\n", wMsg
);
1585 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1588 wmm
= MIDI_mciGetOpenDev(dwDevID
, wMsg
);
1589 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1592 case MCI_OPEN_DRIVER
: return MIDI_mciOpen (wmm
, dwParam1
, (LPMCI_OPEN_PARMSW
) dwParam2
);
1593 case MCI_CLOSE_DRIVER
: return MIDI_mciClose (wmm
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1594 case MCI_PLAY
: return MIDI_mciPlay (wmm
, dwParam1
, (LPMCI_PLAY_PARMS
) dwParam2
);
1595 case MCI_STOP
: return MIDI_mciStop (wmm
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1596 case MCI_SET
: return MIDI_mciSet (wmm
, dwParam1
, (LPMCI_SEQ_SET_PARMS
) dwParam2
);
1597 case MCI_PAUSE
: return MIDI_mciPause (wmm
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1598 case MCI_RESUME
: return MIDI_mciResume (wmm
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1599 case MCI_STATUS
: return MIDI_mciStatus (wmm
, dwParam1
, (LPMCI_STATUS_PARMS
) dwParam2
);
1600 case MCI_GETDEVCAPS
: return MIDI_mciGetDevCaps(wmm
, dwParam1
, (LPMCI_GETDEVCAPS_PARMS
)dwParam2
);
1601 case MCI_INFO
: return MIDI_mciInfo (wmm
, dwParam1
, (LPMCI_INFO_PARMSW
) dwParam2
);
1602 case MCI_SEEK
: return MIDI_mciSeek (wmm
, dwParam1
, (LPMCI_SEEK_PARMS
) dwParam2
);
1605 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1608 TRACE("Unsupported command [0x%x]\n", wMsg
);
1609 return MCIERR_UNSUPPORTED_FUNCTION
; /* Win9x: MCIERR_UNRECOGNIZED_COMMAND */