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 WORD wPort
; /* the WINMM device unit */
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
= 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
);
119 /**************************************************************************
120 * MCI_SendCommandAsync [internal]
122 static DWORD
MCI_SendCommandAsync(UINT wDevID
, UINT wMsg
, DWORD_PTR dwParam1
,
123 DWORD_PTR dwParam2
, UINT size
)
126 struct SCA
* sca
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct SCA
) + size
);
129 return MCIERR_OUT_OF_MEMORY
;
131 sca
->wDevID
= wDevID
;
133 sca
->dwParam1
= dwParam1
;
135 if (size
&& dwParam2
) {
136 sca
->dwParam2
= (DWORD_PTR
)sca
+ sizeof(struct SCA
);
137 /* copy structure passed by program in dwParam2 to be sure
138 * we can still use it whatever the program does
140 memcpy((LPVOID
)sca
->dwParam2
, (LPVOID
)dwParam2
, size
);
142 sca
->dwParam2
= dwParam2
;
145 if ((handle
= CreateThread(NULL
, 0, MCI_SCAStarter
, sca
, 0, NULL
)) == 0) {
146 WARN("Couldn't allocate thread for async command handling, sending synchronously\n");
147 return MCI_SCAStarter(sca
);
149 SetThreadPriority(handle
, THREAD_PRIORITY_TIME_CRITICAL
);
154 /*======================================================================*
155 * MCI MIDI implementation *
156 *======================================================================*/
158 static DWORD
MIDI_mciResume(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
);
160 /**************************************************************************
161 * MIDI_drvOpen [internal]
163 static DWORD
MIDI_drvOpen(LPCWSTR str
, LPMCI_OPEN_DRIVER_PARMSW modp
)
167 if (!modp
) return 0xFFFFFFFF;
169 wmm
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINE_MCIMIDI
));
174 wmm
->wDevID
= modp
->wDeviceID
;
175 mciSetDriverData(wmm
->wDevID
, (DWORD_PTR
)wmm
);
176 modp
->wCustomCommandTable
= MCI_NO_COMMAND_TABLE
;
177 modp
->wType
= MCI_DEVTYPE_SEQUENCER
;
178 return modp
->wDeviceID
;
181 /**************************************************************************
182 * MCIMIDI_drvClose [internal]
184 static DWORD
MIDI_drvClose(DWORD dwDevID
)
186 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(dwDevID
);
189 HeapFree(GetProcessHeap(), 0, wmm
);
190 mciSetDriverData(dwDevID
, 0);
193 return (dwDevID
== 0xFFFFFFFF) ? 1 : 0;
196 /**************************************************************************
197 * MIDI_mciGetOpenDev [internal]
199 static WINE_MCIMIDI
* MIDI_mciGetOpenDev(UINT wDevID
)
201 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(wDevID
);
203 if (wmm
== NULL
|| wmm
->nUseCount
== 0) {
204 WARN("Invalid wDevID=%u\n", wDevID
);
210 /**************************************************************************
211 * MIDI_mciReadByte [internal]
213 static DWORD
MIDI_mciReadByte(WINE_MCIMIDI
* wmm
, BYTE
*lpbyt
)
218 mmioRead(wmm
->hFile
, (HPSTR
)lpbyt
, sizeof(BYTE
)) != (long)sizeof(BYTE
)) {
219 WARN("Error reading wmm=%p\n", wmm
);
220 ret
= MCIERR_INVALID_FILE
;
226 /**************************************************************************
227 * MIDI_mciReadWord [internal]
229 static DWORD
MIDI_mciReadWord(WINE_MCIMIDI
* wmm
, LPWORD lpw
)
232 DWORD ret
= MCIERR_INVALID_FILE
;
235 MIDI_mciReadByte(wmm
, &hibyte
) == 0 &&
236 MIDI_mciReadByte(wmm
, &lobyte
) == 0) {
237 *lpw
= ((WORD
)hibyte
<< 8) + lobyte
;
243 /**************************************************************************
244 * MIDI_mciReadLong [internal]
246 static DWORD
MIDI_mciReadLong(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
249 DWORD ret
= MCIERR_INVALID_FILE
;
252 MIDI_mciReadWord(wmm
, &hiword
) == 0 &&
253 MIDI_mciReadWord(wmm
, &loword
) == 0) {
254 *lpdw
= MAKELONG(loword
, hiword
);
260 /**************************************************************************
261 * MIDI_mciReadVaryLen [internal]
263 static WORD
MIDI_mciReadVaryLen(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
270 ret
= MCIERR_INVALID_FILE
;
273 if (MIDI_mciReadByte(wmm
, &byte
) != 0) {
276 value
= (value
<< 7) + (byte
& 0x7F);
278 } while (byte
& 0x80);
281 TRACE("val=%08X\n", value);
287 /**************************************************************************
288 * MIDI_mciReadNextEvent [internal]
290 static DWORD
MIDI_mciReadNextEvent(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
298 if (mmioSeek(wmm
->hFile
, mmt
->dwIndex
, SEEK_SET
) != mmt
->dwIndex
) {
299 WARN("Can't seek at %08X\n", mmt
->dwIndex
);
300 return MCIERR_INVALID_FILE
;
302 evtLength
= MIDI_mciReadVaryLen(wmm
, &evtPulse
) + 1; /* > 0 */
303 MIDI_mciReadByte(wmm
, &b1
);
307 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
311 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
313 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
314 if (evtLength
>= 0x10000u
) {
315 /* this limitation shouldn't be a problem */
316 WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
319 hw
= LOWORD(evtLength
);
324 if (b1
& 0x80) { /* use running status ? */
325 mmt
->wLastCommand
= b1
;
326 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
329 b1
= mmt
->wLastCommand
;
331 switch ((b1
>> 4) & 0x07) {
332 case 0: case 1: case 2: case 3: case 6:
333 MIDI_mciReadByte(wmm
, &b3
); evtLength
++;
339 WARN("Strange indeed b1=0x%02x\n", b1
);
343 if (mmt
->dwIndex
+ evtLength
> mmt
->dwLast
)
344 return MCIERR_INTERNAL
;
346 mmt
->dwEventPulse
+= evtPulse
;
347 mmt
->dwEventData
= (hw
<< 16) + (b2
<< 8) + b1
;
348 mmt
->wEventLength
= evtLength
;
351 TRACE("[%u] => pulse=%08x(%08x), data=%08x, length=%u\n",
352 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
353 mmt->dwEventData, mmt->wEventLength);
358 /**************************************************************************
359 * MIDI_mciReadMTrk [internal]
361 static DWORD
MIDI_mciReadMTrk(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
366 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
, (long)sizeof(FOURCC
)) !=
367 (long)sizeof(FOURCC
)) {
368 return MCIERR_INVALID_FILE
;
371 if (fourcc
!= mmioFOURCC('M', 'T', 'r', 'k')) {
372 WARN("Can't synchronize on 'MTrk' !\n");
373 return MCIERR_INVALID_FILE
;
376 if (MIDI_mciReadLong(wmm
, &toberead
) != 0) {
377 return MCIERR_INVALID_FILE
;
379 mmt
->dwFirst
= mmioSeek(wmm
->hFile
, 0, SEEK_CUR
); /* >= 0 */
380 mmt
->dwLast
= mmt
->dwFirst
+ toberead
;
382 /* compute # of pulses in this track */
383 mmt
->dwIndex
= mmt
->dwFirst
;
384 mmt
->dwEventPulse
= 0;
386 while (MIDI_mciReadNextEvent(wmm
, mmt
) == 0 && LOWORD(mmt
->dwEventData
) != 0x2FFF) {
390 mmt
->dwIndex
+= mmt
->wEventLength
;
392 switch (LOWORD(mmt
->dwEventData
)) {
395 /* position after meta data header */
396 mmioSeek(wmm
->hFile
, mmt
->dwIndex
+ HIWORD(mmt
->dwEventData
), SEEK_SET
);
397 len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
399 if (len
>= sizeof(buf
)) {
400 WARN("Buffer for text is too small (%u are needed)\n", len
);
401 len
= sizeof(buf
) - 1;
403 if (mmioRead(wmm
->hFile
, buf
, len
) == len
) {
404 buf
[len
] = 0; /* end string in case */
405 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
407 if (wmm
->lpstrCopyright
) {
408 WARN("Two copyright notices (%s|%s)\n", debugstr_w(wmm
->lpstrCopyright
), buf
);
410 len
= MultiByteToWideChar( CP_ACP
, 0, buf
, -1, NULL
, 0 );
411 wmm
->lpstrCopyright
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
412 MultiByteToWideChar( CP_ACP
, 0, buf
, -1, wmm
->lpstrCopyright
, len
);
416 if (wmm
->lpstrName
) {
417 WARN("Two names (%s|%s)\n", debugstr_w(wmm
->lpstrName
), buf
);
419 len
= MultiByteToWideChar( CP_ACP
, 0, buf
, -1, NULL
, 0 );
420 wmm
->lpstrName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
421 MultiByteToWideChar( CP_ACP
, 0, buf
, -1, wmm
->lpstrName
, len
);
429 mmt
->dwLength
= mmt
->dwEventPulse
;
431 TRACE("Track %u has %u bytes and %u pulses\n", mmt
->wTrackNr
, toberead
, mmt
->dwLength
);
433 /* reset track data */
434 mmt
->wStatus
= 1; /* ok, playing */
435 mmt
->dwIndex
= mmt
->dwFirst
;
436 mmt
->dwEventPulse
= 0;
438 if (mmioSeek(wmm
->hFile
, 0, SEEK_CUR
) != mmt
->dwLast
) {
439 WARN("Ouch, out of sync seek=%u track=%u\n",
440 mmioSeek(wmm
->hFile
, 0, SEEK_CUR
), mmt
->dwLast
);
441 /* position at end of this track, to be ready to read next track */
442 mmioSeek(wmm
->hFile
, mmt
->dwLast
, SEEK_SET
);
448 /**************************************************************************
449 * MIDI_mciReadMThd [internal]
451 static DWORD
MIDI_mciReadMThd(WINE_MCIMIDI
* wmm
, DWORD dwOffset
)
457 TRACE("(%p, %08X);\n", wmm
, dwOffset
);
459 if (mmioSeek(wmm
->hFile
, dwOffset
, SEEK_SET
) != dwOffset
) {
460 WARN("Can't seek at %08X begin of 'MThd'\n", dwOffset
);
461 return MCIERR_INVALID_FILE
;
463 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
,
464 (long) sizeof(FOURCC
)) != (long) sizeof(FOURCC
))
465 return MCIERR_INVALID_FILE
;
467 if (fourcc
!= mmioFOURCC('M', 'T', 'h', 'd')) {
468 WARN("Can't synchronize on 'MThd' !\n");
469 return MCIERR_INVALID_FILE
;
472 if (MIDI_mciReadLong(wmm
, &toberead
) != 0 || toberead
< 3 * sizeof(WORD
))
473 return MCIERR_INVALID_FILE
;
475 if (MIDI_mciReadWord(wmm
, &wmm
->wFormat
) != 0 ||
476 MIDI_mciReadWord(wmm
, &wmm
->nTracks
) != 0 ||
477 MIDI_mciReadWord(wmm
, &wmm
->nDivision
) != 0) {
478 return MCIERR_INVALID_FILE
;
481 TRACE("toberead=0x%08X, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
482 toberead
, wmm
->wFormat
, wmm
->nTracks
, wmm
->nDivision
);
484 /* MS doc says that the MIDI MCI time format must be put by default to the format
485 * stored in the MIDI file...
487 if (wmm
->nDivision
> 0x8000) {
488 /* eric.pouech@lemel.fr 98/11
489 * I did not check this very code (pulses are expressed as SMPTE sub-frames).
490 * In about 40 MB of MIDI files I have, none was SMPTE based...
491 * I'm just wondering if this is widely used :-). So, if someone has one of
492 * these files, I'd like to know about it.
494 FIXME("Handling SMPTE time in MIDI files has not been tested\n"
495 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
497 switch (HIBYTE(wmm
->nDivision
)) {
498 case 0xE8: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
; break; /* -24 */
499 case 0xE7: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
; break; /* -25 */
500 case 0xE3: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
501 case 0xE2: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
; break; /* -30 */
503 WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm
->nDivision
));
504 return MCIERR_INVALID_FILE
;
506 switch (LOBYTE(wmm
->nDivision
)) {
507 case 4: /* MIDI Time Code */
510 case 80: /* SMPTE bit resolution */
513 WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm
->nDivision
));
514 return MCIERR_INVALID_FILE
;
516 } else if (wmm
->nDivision
== 0) {
517 WARN("Number of division is 0, can't support that !!\n");
518 return MCIERR_INVALID_FILE
;
520 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
523 switch (wmm
->wFormat
) {
525 if (wmm
->nTracks
!= 1) {
526 WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
534 WARN("Handling MIDI files which format = %d is not (yet) supported\n"
535 "Please report with MIDI file !\n", wmm
->wFormat
);
536 return MCIERR_INVALID_FILE
;
539 if (wmm
->nTracks
& 0x8000) {
540 /* this shouldn't be a problem... */
541 WARN("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
542 wmm
->nTracks
= 0x7FFF;
545 if ((wmm
->tracks
= HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK
) * wmm
->nTracks
)) == NULL
) {
546 return MCIERR_OUT_OF_MEMORY
;
549 toberead
-= 3 * sizeof(WORD
);
551 TRACE("Size of MThd > 6, skipping %d extra bytes\n", toberead
);
552 mmioSeek(wmm
->hFile
, toberead
, SEEK_CUR
);
555 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
556 wmm
->tracks
[nt
].wTrackNr
= nt
;
557 if (MIDI_mciReadMTrk(wmm
, &wmm
->tracks
[nt
]) != 0) {
558 WARN("Can't read 'MTrk' header\n");
559 return MCIERR_INVALID_FILE
;
563 wmm
->dwTempo
= 500000;
568 /**************************************************************************
569 * MIDI_ConvertPulseToMS [internal]
571 static DWORD
MIDI_ConvertPulseToMS(WINE_MCIMIDI
* wmm
, DWORD pulse
)
575 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
576 * may change during file playing
578 if (wmm
->nDivision
== 0) {
579 FIXME("Shouldn't happen. wmm->nDivision = 0\n");
580 } else if (wmm
->nDivision
> 0x8000) { /* SMPTE, unchecked FIXME? */
581 int nf
= -(char)HIBYTE(wmm
->nDivision
); /* number of frames */
582 int nsf
= LOBYTE(wmm
->nDivision
); /* number of sub-frames */
583 ret
= (pulse
* 1000) / (nf
* nsf
);
585 ret
= (DWORD
)((double)pulse
* ((double)wmm
->dwTempo
/ 1000) /
586 (double)wmm
->nDivision
);
590 TRACE("pulse=%u tempo=%u division=%u=0x%04x => ms=%u\n",
591 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
597 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
598 #define TIME_MS_IN_ONE_MINUTE (60*1000)
599 #define TIME_MS_IN_ONE_SECOND (1000)
601 /**************************************************************************
602 * MIDI_ConvertTimeFormatToMS [internal]
604 static DWORD
MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI
* wmm
, DWORD val
)
608 switch (wmm
->dwMciTimeFormat
) {
609 case MCI_FORMAT_MILLISECONDS
:
612 case MCI_FORMAT_SMPTE_24
:
614 (HIBYTE(HIWORD(val
)) * 125) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
615 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
617 case MCI_FORMAT_SMPTE_25
:
619 HIBYTE(HIWORD(val
)) * 40 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
620 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
622 case MCI_FORMAT_SMPTE_30
:
624 (HIBYTE(HIWORD(val
)) * 100) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
625 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
628 WARN("Bad time format %u!\n", wmm
->dwMciTimeFormat
);
631 TRACE("val=%u=0x%08x [tf=%u] => ret=%u\n", val, val, wmm->dwMciTimeFormat, ret);
636 /**************************************************************************
637 * MIDI_ConvertMSToTimeFormat [internal]
639 static DWORD
MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI
* wmm
, DWORD _val
)
641 DWORD ret
= 0, val
= _val
;
644 switch (wmm
->dwMciTimeFormat
) {
645 case MCI_FORMAT_MILLISECONDS
:
648 case MCI_FORMAT_SMPTE_24
:
649 case MCI_FORMAT_SMPTE_25
:
650 case MCI_FORMAT_SMPTE_30
:
651 h
= val
/ TIME_MS_IN_ONE_HOUR
;
652 m
= (val
-= h
* TIME_MS_IN_ONE_HOUR
) / TIME_MS_IN_ONE_MINUTE
;
653 s
= (val
-= m
* TIME_MS_IN_ONE_MINUTE
) / TIME_MS_IN_ONE_SECOND
;
654 switch (wmm
->dwMciTimeFormat
) {
655 case MCI_FORMAT_SMPTE_24
:
656 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
657 f
= (val
* 3) / 125; val
-= (f
* 125) / 3;
659 case MCI_FORMAT_SMPTE_25
:
660 /* one frame is 1000/25 ms long, 1000/25 == 40 */
661 f
= val
/ 40; val
-= f
* 40;
663 case MCI_FORMAT_SMPTE_30
:
664 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
665 f
= (val
* 3) / 100; val
-= (f
* 100) / 3;
668 FIXME("There must be some bad bad programmer\n");
671 /* val contains the number of ms which cannot make a complete frame */
672 /* FIXME: is this correct ? programs seem to be happy with that */
673 ret
= (f
<< 24) | (s
<< 16) | (m
<< 8) | (h
<< 0);
676 WARN("Bad time format %u!\n", wmm
->dwMciTimeFormat
);
679 TRACE("val=%u [tf=%u] => ret=%u=0x%08x\n", _val, wmm->dwMciTimeFormat, ret, ret);
684 /**************************************************************************
685 * MIDI_GetMThdLengthMS [internal]
687 static DWORD
MIDI_GetMThdLengthMS(WINE_MCIMIDI
* wmm
)
692 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
693 if (wmm
->wFormat
== 2) {
694 ret
+= wmm
->tracks
[nt
].dwLength
;
695 } else if (wmm
->tracks
[nt
].dwLength
> ret
) {
696 ret
= wmm
->tracks
[nt
].dwLength
;
699 /* FIXME: this is wrong if there is a tempo change inside the file */
700 return MIDI_ConvertPulseToMS(wmm
, ret
);
703 /**************************************************************************
704 * MIDI_mciOpen [internal]
706 static DWORD
MIDI_mciOpen(UINT wDevID
, DWORD dwFlags
, LPMCI_OPEN_PARMSW lpParms
)
710 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(wDevID
);
712 TRACE("(%04x, %08X, %p)\n", wDevID
, dwFlags
, lpParms
);
714 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
715 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
716 if (dwFlags
& MCI_OPEN_SHAREABLE
)
717 return MCIERR_HARDWARE
;
719 if (wmm
->nUseCount
> 0) {
720 /* The driver is already opened on this channel
721 * MIDI sequencer cannot be shared
723 return MCIERR_DEVICE_OPEN
;
729 wmm
->wPort
= MIDI_MAPPER
;
730 wmm
->lpstrElementName
= NULL
;
731 dwDeviceID
= lpParms
->wDeviceID
;
733 TRACE("wDevID=%04X (lpParams->wDeviceID=%08X)\n", wDevID
, dwDeviceID
);
734 /* lpParms->wDeviceID = wDevID;*/
736 if (dwFlags
& MCI_OPEN_ELEMENT
) {
737 TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpParms
->lpstrElementName
));
738 if (lpParms
->lpstrElementName
&& strlenW(lpParms
->lpstrElementName
) > 0) {
739 wmm
->hFile
= mmioOpenW((LPWSTR
)lpParms
->lpstrElementName
, NULL
,
740 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
741 if (wmm
->hFile
== 0) {
742 WARN("Can't find file %s!\n", debugstr_w(lpParms
->lpstrElementName
));
744 return MCIERR_FILE_NOT_FOUND
;
746 wmm
->lpstrElementName
= HeapAlloc(GetProcessHeap(), 0,
747 (strlenW(lpParms
->lpstrElementName
) + 1) * sizeof(WCHAR
));
748 strcpyW(wmm
->lpstrElementName
, lpParms
->lpstrElementName
);
751 TRACE("hFile=%p\n", wmm
->hFile
);
753 wmm
->lpstrCopyright
= NULL
;
754 wmm
->lpstrName
= NULL
;
756 wmm
->wNotifyDeviceID
= dwDeviceID
;
757 wmm
->dwStatus
= MCI_MODE_NOT_READY
; /* while loading file contents */
758 /* spec says it should be the default format from the MIDI file... */
759 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
761 if (wmm
->hFile
!= 0) {
766 if (mmioDescend(wmm
->hFile
, &ckMainRIFF
, NULL
, 0) != 0) {
767 dwRet
= MCIERR_INVALID_FILE
;
769 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
770 (LPSTR
)&ckMainRIFF
.ckid
, (LPSTR
)&ckMainRIFF
.fccType
, ckMainRIFF
.cksize
);
772 if (ckMainRIFF
.ckid
== FOURCC_RIFF
&& ckMainRIFF
.fccType
== mmioFOURCC('R', 'M', 'I', 'D')) {
773 mmckInfo
.ckid
= mmioFOURCC('d', 'a', 't', 'a');
774 mmioSeek(wmm
->hFile
, ckMainRIFF
.dwDataOffset
+ ((ckMainRIFF
.cksize
+ 1) & ~1), SEEK_SET
);
775 if (mmioDescend(wmm
->hFile
, &mmckInfo
, &ckMainRIFF
, MMIO_FINDCHUNK
) == 0) {
776 TRACE("... is a 'RMID' file\n");
777 dwOffset
= mmckInfo
.dwDataOffset
;
779 dwRet
= MCIERR_INVALID_FILE
;
782 if (dwRet
== 0 && MIDI_mciReadMThd(wmm
, dwOffset
) != 0) {
783 WARN("Can't read 'MThd' header\n");
784 dwRet
= MCIERR_INVALID_FILE
;
788 TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
796 mmioClose(wmm
->hFile
, 0);
799 wmm
->dwPositionMS
= 0;
800 wmm
->dwStatus
= MCI_MODE_STOP
;
801 if (dwFlags
& MCI_NOTIFY
) {
802 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
803 mciDriverNotify(HWND_32(LOWORD(lpParms
->dwCallback
)),
804 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
810 /**************************************************************************
811 * MIDI_mciStop [internal]
813 static DWORD
MIDI_mciStop(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
815 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
817 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
819 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
821 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
822 int oldstat
= wmm
->dwStatus
;
824 wmm
->dwStatus
= MCI_MODE_NOT_READY
;
825 if (oldstat
== MCI_MODE_PAUSE
)
826 midiOutReset((HMIDIOUT
)wmm
->hMidi
);
828 while (wmm
->dwStatus
!= MCI_MODE_STOP
)
833 wmm
->dwStatus
= MCI_MODE_STOP
;
835 TRACE("wmm->dwStatus=%d\n", wmm
->dwStatus
);
837 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
838 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
839 mciDriverNotify(HWND_32(LOWORD(lpParms
->dwCallback
)),
840 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
845 /**************************************************************************
846 * MIDI_mciClose [internal]
848 static DWORD
MIDI_mciClose(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
850 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
852 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
854 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
856 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
857 MIDI_mciStop(wDevID
, MCI_WAIT
, lpParms
);
861 if (wmm
->nUseCount
== 0) {
862 if (wmm
->hFile
!= 0) {
863 mmioClose(wmm
->hFile
, 0);
865 TRACE("hFile closed !\n");
867 HeapFree(GetProcessHeap(), 0, wmm
->tracks
);
868 HeapFree(GetProcessHeap(), 0, wmm
->lpstrElementName
);
869 HeapFree(GetProcessHeap(), 0, wmm
->lpstrCopyright
);
870 HeapFree(GetProcessHeap(), 0, wmm
->lpstrName
);
872 TRACE("Shouldn't happen... nUseCount=%d\n", wmm
->nUseCount
);
873 return MCIERR_INTERNAL
;
876 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
877 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
878 mciDriverNotify(HWND_32(LOWORD(lpParms
->dwCallback
)),
879 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
884 /**************************************************************************
885 * MIDI_mciFindNextEvent [internal]
887 static MCI_MIDITRACK
* MIDI_mciFindNextEvent(WINE_MCIMIDI
* wmm
, LPDWORD hiPulse
)
892 *hiPulse
= 0xFFFFFFFFul
;
894 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
895 mmt
= &wmm
->tracks
[nt
];
897 if (mmt
->wStatus
== 0)
899 if (mmt
->dwEventPulse
< *hiPulse
) {
900 *hiPulse
= mmt
->dwEventPulse
;
904 return (cnt
== 0xFFFFu
) ? 0 /* no more event on all tracks */
908 /**************************************************************************
909 * MIDI_mciPlay [internal]
911 static DWORD
MIDI_mciPlay(UINT wDevID
, DWORD dwFlags
, LPMCI_PLAY_PARMS lpParms
)
913 DWORD dwStartMS
, dwEndMS
;
918 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
920 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
922 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
924 if (wmm
->hFile
== 0) {
925 WARN("Can't play: no file %s!\n", debugstr_w(wmm
->lpstrElementName
));
926 return MCIERR_FILE_NOT_FOUND
;
929 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
930 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
931 /* FIXME: parameters (start/end) in lpParams may not be used */
932 return MIDI_mciResume(wDevID
, dwFlags
, (LPMCI_GENERIC_PARMS
)lpParms
);
934 WARN("Can't play: device is not stopped !\n");
935 return MCIERR_INTERNAL
;
938 if (!(dwFlags
& MCI_WAIT
)) {
939 /** FIXME: I'm not 100% sure that wNotifyDeviceID is the right value in all cases ??? */
940 return MCI_SendCommandAsync(wmm
->wNotifyDeviceID
, MCI_PLAY
, dwFlags
, (DWORD_PTR
)lpParms
, sizeof(LPMCI_PLAY_PARMS
));
943 if (lpParms
&& (dwFlags
& MCI_FROM
)) {
944 dwStartMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwFrom
);
946 dwStartMS
= wmm
->dwPositionMS
;
949 if (lpParms
&& (dwFlags
& MCI_TO
)) {
950 dwEndMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
952 dwEndMS
= 0xFFFFFFFFul
;
955 TRACE("Playing from %u to %u\n", dwStartMS
, dwEndMS
);
958 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
959 mmt
= &wmm
->tracks
[nt
];
961 mmt
->wStatus
= 1; /* ok, playing */
962 mmt
->dwIndex
= mmt
->dwFirst
;
963 if (wmm
->wFormat
== 2 && nt
> 0) {
964 mmt
->dwEventPulse
= wmm
->tracks
[nt
- 1].dwLength
;
966 mmt
->dwEventPulse
= 0;
968 MIDI_mciReadNextEvent(wmm
, mmt
); /* FIXME == 0 */
971 dwRet
= midiOutOpen((LPHMIDIOUT
)&wmm
->hMidi
, wmm
->wPort
, 0L, 0L, CALLBACK_NULL
);
972 if (dwRet
!= MMSYSERR_NOERROR
) {
977 wmm
->dwTempo
= 500000;
978 wmm
->dwStatus
= MCI_MODE_PLAY
;
979 wmm
->dwPositionMS
= 0;
980 wmm
->wStartedPlaying
= FALSE
;
982 while (wmm
->dwStatus
!= MCI_MODE_STOP
&& wmm
->dwStatus
!= MCI_MODE_NOT_READY
) {
983 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
984 * too much. Tell gcc not to optimize status value using volatile.
986 while (((volatile WINE_MCIMIDI
*)wmm
)->dwStatus
== MCI_MODE_PAUSE
);
988 doPlay
= (wmm
->dwPositionMS
>= dwStartMS
&& wmm
->dwPositionMS
<= dwEndMS
);
990 TRACE("wmm->dwStatus=%d, doPlay=%c\n", wmm
->dwStatus
, doPlay
? 'T' : 'F');
992 if ((mmt
= MIDI_mciFindNextEvent(wmm
, &hiPulse
)) == NULL
)
993 break; /* no more event on tracks */
995 /* if starting playing, then set StartTicks to the value it would have had
996 * if play had started at position 0
998 if (doPlay
&& !wmm
->wStartedPlaying
) {
999 wmm
->dwStartTicks
= GetTickCount() - MIDI_ConvertPulseToMS(wmm
, wmm
->dwPulse
);
1000 wmm
->wStartedPlaying
= TRUE
;
1001 TRACE("Setting dwStartTicks to %u\n", wmm
->dwStartTicks
);
1004 if (hiPulse
> wmm
->dwPulse
) {
1005 wmm
->dwPositionMS
+= MIDI_ConvertPulseToMS(wmm
, hiPulse
- wmm
->dwPulse
);
1007 DWORD togo
= wmm
->dwStartTicks
+ wmm
->dwPositionMS
;
1008 DWORD tc
= GetTickCount();
1010 TRACE("Pulses hi=0x%08x <> cur=0x%08x\n", hiPulse
, wmm
->dwPulse
);
1011 TRACE("Wait until %u => %u ms\n",
1012 tc
- wmm
->dwStartTicks
, togo
- wmm
->dwStartTicks
);
1016 wmm
->dwPulse
= hiPulse
;
1019 switch (LOBYTE(LOWORD(mmt
->dwEventData
))) {
1021 case 0xF7: /* sysex events */
1023 FIXME("Not handling SysEx events (yet)\n");
1027 /* position after meta data header */
1028 mmioSeek(wmm
->hFile
, mmt
->dwIndex
+ HIWORD(mmt
->dwEventData
), SEEK_SET
);
1029 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
1030 case 0x00: /* 16-bit sequence number */
1031 if (TRACE_ON(mcimidi
)) {
1034 MIDI_mciReadWord(wmm
, &twd
); /* == 0 */
1035 TRACE("Got sequence number %u\n", twd
);
1038 case 0x01: /* any text */
1039 case 0x02: /* Copyright Message text */
1040 case 0x03: /* Sequence/Track Name text */
1041 case 0x04: /* Instrument Name text */
1042 case 0x05: /* Lyric text */
1043 case 0x06: /* Marker text */
1044 case 0x07: /* Cue-point text */
1045 if (TRACE_ON(mcimidi
)) {
1047 WORD len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
1048 static const char* const info
[8] = {"", "Text", "Copyright", "Seq/Trk name",
1049 "Instrument", "Lyric", "Marker", "Cue-point"};
1050 WORD idx
= HIBYTE(LOWORD(mmt
->dwEventData
));
1052 if (len
>= sizeof(buf
)) {
1053 WARN("Buffer for text is too small (%u are needed)\n", len
);
1054 len
= sizeof(buf
) - 1;
1056 if (mmioRead(wmm
->hFile
, buf
, len
) == len
) {
1057 buf
[len
] = 0; /* end string in case */
1058 TRACE("%s => \"%s\"\n", (idx
< 8 ) ? info
[idx
] : "", buf
);
1060 WARN("Couldn't read data for %s\n", (idx
< 8) ? info
[idx
] : "");
1065 /* MIDI channel (cc) */
1066 if (FIXME_ON(mcimidi
)) {
1069 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
1070 FIXME("NIY: MIDI channel=%u, track=%u\n", bt
, mmt
->wTrackNr
);
1074 /* MIDI port (pp) */
1075 if (FIXME_ON(mcimidi
)) {
1078 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
1079 FIXME("NIY: MIDI port=%u, track=%u\n", bt
, mmt
->wTrackNr
);
1082 case 0x2F: /* end of track */
1085 case 0x51:/* set tempo */
1086 /* Tempo is expressed in -seconds per midi quarter note
1087 * for format 1 MIDI files, this can only be present on track #0
1089 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
1090 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt
->wTrackNr
);
1095 MIDI_mciReadByte(wmm
, &tbt
); value
= ((DWORD
)tbt
) << 16;
1096 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 8;
1097 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 0;
1098 TRACE("Setting tempo to %d (BPM=%d)\n", wmm
->dwTempo
, (value
) ? (60000000 / value
) : 0);
1099 wmm
->dwTempo
= value
;
1102 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
1103 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
1104 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt
->wTrackNr
);
1105 } if (mmt
->dwEventPulse
!= 0) {
1106 WARN("SMPTE track start can only be expressed at start of track (%u)\n", mmt
->dwEventPulse
);
1108 BYTE h
, m
, s
, f
, ff
;
1110 MIDI_mciReadByte(wmm
, &h
);
1111 MIDI_mciReadByte(wmm
, &m
);
1112 MIDI_mciReadByte(wmm
, &s
);
1113 MIDI_mciReadByte(wmm
, &f
);
1114 MIDI_mciReadByte(wmm
, &ff
);
1115 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h
, m
, s
, f
, ff
);
1118 case 0x58: /* file rhythm */
1119 if (TRACE_ON(mcimidi
)) {
1120 BYTE num
, den
, cpmc
, _32npqn
;
1122 MIDI_mciReadByte(wmm
, &num
);
1123 MIDI_mciReadByte(wmm
, &den
); /* to notate e.g. 6/8 */
1124 MIDI_mciReadByte(wmm
, &cpmc
); /* number of MIDI clocks per metronome click */
1125 MIDI_mciReadByte(wmm
, &_32npqn
); /* number of notated 32nd notes per MIDI quarter note */
1127 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num
, 1 << den
, cpmc
, _32npqn
);
1130 case 0x59: /* key signature */
1131 if (TRACE_ON(mcimidi
)) {
1134 MIDI_mciReadByte(wmm
, &sf
);
1135 MIDI_mciReadByte(wmm
, &mm
);
1137 if (sf
>= 0x80) TRACE("%d flats\n", -(char)sf
);
1138 else if (sf
> 0) TRACE("%d sharps\n", (char)sf
);
1139 else TRACE("Key of C\n");
1140 TRACE("Mode: %s\n", (mm
== 0) ? "major" : "minor");
1144 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt
->dwEventData
)));
1150 dwRet
= midiOutShortMsg((HMIDIOUT
)wmm
->hMidi
, mmt
->dwEventData
);
1152 switch (LOBYTE(LOWORD(mmt
->dwEventData
)) & 0xF0) {
1158 dwRet
= midiOutShortMsg((HMIDIOUT
)wmm
->hMidi
, mmt
->dwEventData
);
1162 mmt
->dwIndex
+= mmt
->wEventLength
;
1163 if (mmt
->dwIndex
< mmt
->dwFirst
|| mmt
->dwIndex
>= mmt
->dwLast
) {
1167 MIDI_mciReadNextEvent(wmm
, mmt
);
1171 midiOutReset((HMIDIOUT
)wmm
->hMidi
);
1173 dwRet
= midiOutClose((HMIDIOUT
)wmm
->hMidi
);
1174 /* to restart playing at beginning when it's over */
1175 wmm
->dwPositionMS
= 0;
1177 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
1178 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1179 mciDriverNotify(HWND_32(LOWORD(lpParms
->dwCallback
)),
1180 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1183 wmm
->dwStatus
= MCI_MODE_STOP
;
1187 /**************************************************************************
1188 * MIDI_mciPause [internal]
1190 static DWORD
MIDI_mciPause(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1192 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1194 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1196 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1198 if (wmm
->dwStatus
== MCI_MODE_PLAY
) {
1199 /* stop all notes */
1201 for (chn
= 0; chn
< 16; chn
++)
1202 midiOutShortMsg((HMIDIOUT
)(wmm
->hMidi
), 0x78B0 | chn
);
1203 wmm
->dwStatus
= MCI_MODE_PAUSE
;
1205 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
1206 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1207 mciDriverNotify(HWND_32(LOWORD(lpParms
->dwCallback
)),
1208 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1214 /**************************************************************************
1215 * MIDI_mciResume [internal]
1217 static DWORD
MIDI_mciResume(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1219 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1221 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1223 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1225 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
1226 wmm
->wStartedPlaying
= FALSE
;
1227 wmm
->dwStatus
= MCI_MODE_PLAY
;
1229 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
1230 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1231 mciDriverNotify(HWND_32(LOWORD(lpParms
->dwCallback
)),
1232 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1237 /**************************************************************************
1238 * MIDI_mciSet [internal]
1240 static DWORD
MIDI_mciSet(UINT wDevID
, DWORD dwFlags
, LPMCI_SEQ_SET_PARMS lpParms
)
1242 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1244 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1246 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1247 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1249 if (dwFlags
& MCI_SET_TIME_FORMAT
) {
1250 switch (lpParms
->dwTimeFormat
) {
1251 case MCI_FORMAT_MILLISECONDS
:
1252 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1253 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
1255 case MCI_FORMAT_SMPTE_24
:
1256 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1257 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
;
1259 case MCI_FORMAT_SMPTE_25
:
1260 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1261 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
;
1263 case MCI_FORMAT_SMPTE_30
:
1264 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1265 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
;
1268 WARN("Bad time format %u!\n", lpParms
->dwTimeFormat
);
1269 return MCIERR_BAD_TIME_FORMAT
;
1272 if (dwFlags
& MCI_SET_VIDEO
) {
1273 TRACE("No support for video !\n");
1274 return MCIERR_UNSUPPORTED_FUNCTION
;
1276 if (dwFlags
& MCI_SET_DOOR_OPEN
) {
1277 TRACE("No support for door open !\n");
1278 return MCIERR_UNSUPPORTED_FUNCTION
;
1280 if (dwFlags
& MCI_SET_DOOR_CLOSED
) {
1281 TRACE("No support for door close !\n");
1282 return MCIERR_UNSUPPORTED_FUNCTION
;
1284 if (dwFlags
& MCI_SET_AUDIO
) {
1285 if (dwFlags
& MCI_SET_ON
) {
1286 TRACE("MCI_SET_ON audio !\n");
1287 } else if (dwFlags
& MCI_SET_OFF
) {
1288 TRACE("MCI_SET_OFF audio !\n");
1290 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1291 return MCIERR_BAD_INTEGER
;
1294 switch (lpParms
->dwAudio
)
1296 case MCI_SET_AUDIO_ALL
: TRACE("MCI_SET_AUDIO_ALL !\n"); break;
1297 case MCI_SET_AUDIO_LEFT
: TRACE("MCI_SET_AUDIO_LEFT !\n"); break;
1298 case MCI_SET_AUDIO_RIGHT
: TRACE("MCI_SET_AUDIO_RIGHT !\n"); break;
1299 default: WARN("Unknown audio channel %u\n", lpParms
->dwAudio
); break;
1303 if (dwFlags
& MCI_SEQ_SET_MASTER
)
1304 TRACE("MCI_SEQ_SET_MASTER !\n");
1305 if (dwFlags
& MCI_SEQ_SET_SLAVE
)
1306 TRACE("MCI_SEQ_SET_SLAVE !\n");
1307 if (dwFlags
& MCI_SEQ_SET_OFFSET
)
1308 TRACE("MCI_SEQ_SET_OFFSET !\n");
1309 if (dwFlags
& MCI_SEQ_SET_PORT
) {
1310 TRACE("MCI_SEQ_SET_PORT = %d\n", lpParms
->dwPort
);
1311 if ((UINT16
)lpParms
->dwPort
!= (UINT16
)MIDI_MAPPER
&&
1312 (UINT16
)lpParms
->dwPort
>= midiOutGetNumDevs())
1313 /* FIXME: input/output port distinction? */
1314 return MCIERR_SEQ_PORT_NONEXISTENT
;
1315 /* FIXME: Native manages to swap the device while playing! */
1316 wmm
->wPort
= lpParms
->dwPort
;
1318 if (dwFlags
& MCI_SEQ_SET_TEMPO
)
1319 TRACE("MCI_SEQ_SET_TEMPO !\n");
1323 /**************************************************************************
1324 * MIDI_mciStatus [internal]
1326 static DWORD
MIDI_mciStatus(UINT wDevID
, DWORD dwFlags
, LPMCI_STATUS_PARMS lpParms
)
1328 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1331 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1333 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1334 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1336 if (dwFlags
& MCI_STATUS_ITEM
) {
1337 switch (lpParms
->dwItem
) {
1338 case MCI_STATUS_CURRENT_TRACK
:
1339 /* FIXME in Format 2 */
1340 lpParms
->dwReturn
= 1;
1341 TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms
->dwReturn
);
1343 case MCI_STATUS_LENGTH
:
1344 if ((dwFlags
& MCI_TRACK
) && wmm
->wFormat
== 2) {
1345 if (lpParms
->dwTrack
>= wmm
->nTracks
)
1346 return MCIERR_BAD_INTEGER
;
1347 /* FIXME: this is wrong if there is a tempo change inside the file */
1348 lpParms
->dwReturn
= MIDI_ConvertPulseToMS(wmm
, wmm
->tracks
[lpParms
->dwTrack
].dwLength
);
1350 lpParms
->dwReturn
= MIDI_GetMThdLengthMS(wmm
);
1352 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
, lpParms
->dwReturn
);
1353 TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms
->dwReturn
);
1355 case MCI_STATUS_MODE
:
1356 TRACE("MCI_STATUS_MODE => %u\n", wmm
->dwStatus
);
1357 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwStatus
, wmm
->dwStatus
);
1358 ret
= MCI_RESOURCE_RETURNED
;
1360 case MCI_STATUS_MEDIA_PRESENT
:
1361 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1362 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1363 ret
= MCI_RESOURCE_RETURNED
;
1365 case MCI_STATUS_NUMBER_OF_TRACKS
:
1366 lpParms
->dwReturn
= (wmm
->wFormat
== 2) ? wmm
->nTracks
: 1;
1367 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms
->dwReturn
);
1369 case MCI_STATUS_POSITION
:
1370 /* FIXME: do I need to use MCI_TRACK ? */
1371 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
,
1372 (dwFlags
& MCI_STATUS_START
) ? 0 : wmm
->dwPositionMS
);
1373 TRACE("MCI_STATUS_POSITION %s => %lu\n",
1374 (dwFlags
& MCI_STATUS_START
) ? "start" : "current", lpParms
->dwReturn
);
1376 case MCI_STATUS_READY
:
1377 lpParms
->dwReturn
= (wmm
->dwStatus
== MCI_MODE_NOT_READY
) ?
1378 MAKEMCIRESOURCE(FALSE
, MCI_FALSE
) : MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1379 ret
= MCI_RESOURCE_RETURNED
;
1380 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms
->dwReturn
));
1382 case MCI_STATUS_TIME_FORMAT
:
1383 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwMciTimeFormat
, MCI_FORMAT_RETURN_BASE
+ wmm
->dwMciTimeFormat
);
1384 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms
->dwReturn
));
1385 ret
= MCI_RESOURCE_RETURNED
;
1387 case MCI_SEQ_STATUS_DIVTYPE
:
1388 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1389 if (wmm
->nDivision
> 0x8000) {
1390 switch (wmm
->nDivision
) {
1391 case 0xE8: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_24
; break; /* -24 */
1392 case 0xE7: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_25
; break; /* -25 */
1393 case 0xE3: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
1394 case 0xE2: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30
; break; /* -30 */
1395 default: FIXME("There is a bad bad programmer\n");
1398 lpParms
->dwReturn
= MCI_SEQ_DIV_PPQN
;
1400 lpParms
->dwReturn
= MAKEMCIRESOURCE(lpParms
->dwReturn
,lpParms
->dwReturn
);
1401 ret
= MCI_RESOURCE_RETURNED
;
1403 case MCI_SEQ_STATUS_MASTER
:
1404 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1405 lpParms
->dwReturn
= 0;
1407 case MCI_SEQ_STATUS_SLAVE
:
1408 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1409 lpParms
->dwReturn
= 0;
1411 case MCI_SEQ_STATUS_OFFSET
:
1412 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1413 lpParms
->dwReturn
= 0;
1415 case MCI_SEQ_STATUS_PORT
:
1416 if (wmm
->wPort
!= (UINT16
)MIDI_MAPPER
)
1417 lpParms
->dwReturn
= wmm
->wPort
;
1419 lpParms
->dwReturn
= MAKEMCIRESOURCE(MIDI_MAPPER
, MCI_SEQ_MAPPER_S
);
1420 ret
= MCI_RESOURCE_RETURNED
;
1422 TRACE("MCI_SEQ_STATUS_PORT (%u) => %d\n", wmm
->wDevID
, wmm
->wPort
);
1424 case MCI_SEQ_STATUS_TEMPO
:
1425 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1426 lpParms
->dwReturn
= wmm
->dwTempo
;
1429 FIXME("Unknown command %08X !\n", lpParms
->dwItem
);
1430 return MCIERR_UNRECOGNIZED_COMMAND
;
1433 WARN("No Status-Item!\n");
1434 return MCIERR_UNRECOGNIZED_COMMAND
;
1436 if (dwFlags
& MCI_NOTIFY
) {
1437 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1438 mciDriverNotify(HWND_32(LOWORD(lpParms
->dwCallback
)),
1439 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1444 /**************************************************************************
1445 * MIDI_mciGetDevCaps [internal]
1447 static DWORD
MIDI_mciGetDevCaps(UINT wDevID
, DWORD dwFlags
,
1448 LPMCI_GETDEVCAPS_PARMS lpParms
)
1450 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1453 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1455 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1456 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1458 if (dwFlags
& MCI_GETDEVCAPS_ITEM
) {
1459 switch (lpParms
->dwItem
) {
1460 case MCI_GETDEVCAPS_DEVICE_TYPE
:
1461 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1462 lpParms
->dwReturn
= MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER
, MCI_DEVTYPE_SEQUENCER
);
1463 ret
= MCI_RESOURCE_RETURNED
;
1465 case MCI_GETDEVCAPS_HAS_AUDIO
:
1466 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1467 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1468 ret
= MCI_RESOURCE_RETURNED
;
1470 case MCI_GETDEVCAPS_HAS_VIDEO
:
1471 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1472 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1473 ret
= MCI_RESOURCE_RETURNED
;
1475 case MCI_GETDEVCAPS_USES_FILES
:
1476 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1477 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1478 ret
= MCI_RESOURCE_RETURNED
;
1480 case MCI_GETDEVCAPS_COMPOUND_DEVICE
:
1481 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1482 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1483 ret
= MCI_RESOURCE_RETURNED
;
1485 case MCI_GETDEVCAPS_CAN_EJECT
:
1486 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1487 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1488 ret
= MCI_RESOURCE_RETURNED
;
1490 case MCI_GETDEVCAPS_CAN_PLAY
:
1491 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1492 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1493 ret
= MCI_RESOURCE_RETURNED
;
1495 case MCI_GETDEVCAPS_CAN_RECORD
:
1496 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1497 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1498 ret
= MCI_RESOURCE_RETURNED
;
1500 case MCI_GETDEVCAPS_CAN_SAVE
:
1501 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1502 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1503 ret
= MCI_RESOURCE_RETURNED
;
1506 FIXME("Unknown capability (%08x) !\n", lpParms
->dwItem
);
1507 return MCIERR_UNRECOGNIZED_COMMAND
;
1510 WARN("No GetDevCaps-Item !\n");
1511 return MCIERR_UNRECOGNIZED_COMMAND
;
1516 /**************************************************************************
1517 * MIDI_mciInfo [internal]
1519 static DWORD
MIDI_mciInfo(UINT wDevID
, DWORD dwFlags
, LPMCI_INFO_PARMSW lpParms
)
1522 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1524 static const WCHAR wszMidiSeq
[] = {'W','i','n','e','\'','s',' ','M','I','D','I',' ','s','e','q','u','e','n','c','e','r',0};
1526 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1528 if (lpParms
== NULL
|| lpParms
->lpstrReturn
== NULL
)
1529 return MCIERR_NULL_PARAMETER_BLOCK
;
1530 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1532 TRACE("buf=%p, len=%u\n", lpParms
->lpstrReturn
, lpParms
->dwRetSize
);
1534 switch (dwFlags
& ~(MCI_WAIT
|MCI_NOTIFY
)) {
1535 case MCI_INFO_PRODUCT
: str
= wszMidiSeq
; break;
1536 case MCI_INFO_FILE
: str
= wmm
->lpstrElementName
; break;
1537 case MCI_INFO_COPYRIGHT
: str
= wmm
->lpstrCopyright
; break;
1538 case MCI_INFO_NAME
: str
= wmm
->lpstrName
; break;
1540 WARN("Don't know this info command (%u)\n", dwFlags
);
1541 return MCIERR_UNRECOGNIZED_COMMAND
;
1544 if (lpParms
->dwRetSize
) {
1546 /* FIXME? Since NT, mciwave, mciseq and mcicda set dwRetSize
1547 * to the number of characters written, excluding \0. */
1548 lstrcpynW(lpParms
->lpstrReturn
, str
? str
: &zero
, lpParms
->dwRetSize
);
1549 } else ret
= MCIERR_PARAM_OVERFLOW
;
1554 /**************************************************************************
1555 * MIDI_mciSeek [internal]
1557 static DWORD
MIDI_mciSeek(UINT wDevID
, DWORD dwFlags
, LPMCI_SEEK_PARMS lpParms
)
1560 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1562 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1564 if (lpParms
== NULL
) {
1565 ret
= MCIERR_NULL_PARAMETER_BLOCK
;
1566 } else if (wmm
== NULL
) {
1567 ret
= MCIERR_INVALID_DEVICE_ID
;
1569 MIDI_mciStop(wDevID
, MCI_WAIT
, 0);
1571 if (dwFlags
& MCI_SEEK_TO_START
) {
1572 wmm
->dwPositionMS
= 0;
1573 } else if (dwFlags
& MCI_SEEK_TO_END
) {
1574 wmm
->dwPositionMS
= 0xFFFFFFFF; /* FIXME */
1575 } else if (dwFlags
& MCI_TO
) {
1576 wmm
->dwPositionMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
1578 WARN("dwFlag doesn't tell where to seek to...\n");
1579 return MCIERR_MISSING_PARAMETER
;
1582 TRACE("Seeking to position=%u ms\n", wmm
->dwPositionMS
);
1584 if (dwFlags
& MCI_NOTIFY
) {
1585 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1586 mciDriverNotify(HWND_32(LOWORD(lpParms
->dwCallback
)),
1587 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1593 /*======================================================================*
1594 * MIDI entry points *
1595 *======================================================================*/
1597 /**************************************************************************
1598 * DriverProc (MCISEQ.@)
1600 LRESULT CALLBACK
MCIMIDI_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1601 LPARAM dwParam1
, LPARAM dwParam2
)
1604 case DRV_LOAD
: return 1;
1605 case DRV_FREE
: return 1;
1606 case DRV_ENABLE
: return 1;
1607 case DRV_DISABLE
: return 1;
1608 case DRV_QUERYCONFIGURE
: return 1;
1609 case DRV_CONFIGURE
: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK
); return 1;
1610 case DRV_INSTALL
: return DRVCNF_RESTART
;
1611 case DRV_REMOVE
: return DRVCNF_RESTART
;
1612 case DRV_OPEN
: return MIDI_drvOpen((LPCWSTR
)dwParam1
, (LPMCI_OPEN_DRIVER_PARMSW
)dwParam2
);
1613 case DRV_CLOSE
: return MIDI_drvClose(dwDevID
);
1616 if (dwDevID
== 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION
;
1619 case MCI_OPEN_DRIVER
: return MIDI_mciOpen (dwDevID
, dwParam1
, (LPMCI_OPEN_PARMSW
) dwParam2
);
1620 case MCI_CLOSE_DRIVER
: return MIDI_mciClose (dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1621 case MCI_PLAY
: return MIDI_mciPlay (dwDevID
, dwParam1
, (LPMCI_PLAY_PARMS
) dwParam2
);
1622 case MCI_STOP
: return MIDI_mciStop (dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1623 case MCI_SET
: return MIDI_mciSet (dwDevID
, dwParam1
, (LPMCI_SEQ_SET_PARMS
) dwParam2
);
1624 case MCI_PAUSE
: return MIDI_mciPause (dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1625 case MCI_RESUME
: return MIDI_mciResume (dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1626 case MCI_STATUS
: return MIDI_mciStatus (dwDevID
, dwParam1
, (LPMCI_STATUS_PARMS
) dwParam2
);
1627 case MCI_GETDEVCAPS
: return MIDI_mciGetDevCaps(dwDevID
, dwParam1
, (LPMCI_GETDEVCAPS_PARMS
)dwParam2
);
1628 case MCI_INFO
: return MIDI_mciInfo (dwDevID
, dwParam1
, (LPMCI_INFO_PARMSW
) dwParam2
);
1629 case MCI_SEEK
: return MIDI_mciSeek (dwDevID
, dwParam1
, (LPMCI_SEEK_PARMS
) dwParam2
);
1630 /* commands that should report an error */
1648 TRACE("Unsupported command [0x%x]\n", wMsg
);
1652 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1655 TRACE("Sending msg [%u] to default driver proc\n", wMsg
);
1656 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1658 return MCIERR_UNSUPPORTED_FUNCTION
;