1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * Sample MIDI Wine Driver for Linux
6 * Copyright 1994 Martin Ayotte
12 * 98/7 changes for making this MIDI driver work on OSS
13 * current support is limited to MIDI ports of OSS systems
14 * 98/9 rewriting MCI code for MIDI
15 * 98/11 splitted in midi.c and mcimidi.c
26 #include "debugtools.h"
28 DEFAULT_DEBUG_CHANNEL(mcimidi
);
30 #define MIDI_NOTEOFF 0x80
31 #define MIDI_NOTEON 0x90
34 DWORD dwFirst
; /* offset in file of track */
35 DWORD dwLast
; /* number of bytes in file of track */
36 DWORD dwIndex
; /* current index in file (dwFirst <= dwIndex < dwLast) */
37 DWORD dwLength
; /* number of pulses in this track */
38 DWORD dwEventPulse
; /* current pulse # (event) pointed by dwIndex */
39 DWORD dwEventData
; /* current data (event) pointed by dwIndex */
40 WORD wEventLength
; /* current length (event) pointed by dwIndex */
41 WORD wStatus
: 1, /* 1 : playing, 0 : done */
43 wLastCommand
: 8; /* last MIDI command on track */
46 typedef struct tagWINE_MCIMIDI
{
47 UINT wDevID
; /* the MCI one */
49 int nUseCount
; /* Incremented for each shared open */
50 WORD wNotifyDeviceID
; /* MCI device ID with a pending notification */
51 HANDLE hCallback
; /* Callback handle for pending notification */
52 HMMIO hFile
; /* mmio file handle open as Element */
53 LPCSTR lpstrElementName
; /* Name of file */
54 LPCSTR lpstrCopyright
;
56 WORD dwStatus
; /* one from MCI_MODE_xxxx */
57 DWORD dwMciTimeFormat
; /* One of the supported MCI_FORMAT_xxxx */
58 WORD wFormat
; /* Format of MIDI hFile (0, 1 or 2) */
59 WORD nTracks
; /* Number of tracks in hFile */
60 WORD nDivision
; /* Number of division in hFile PPQN or SMPTE */
62 DWORD dwTempo
; /* Tempo (# of 1/4 note per second */
63 MCI_MIDITRACK
* tracks
; /* Content of each track */
69 /* ===================================================================
70 * ===================================================================
71 * FIXME: should be using the new mmThreadXXXX functions from WINMM
73 * it would require to add a wine internal flag to mmThreadCreate
74 * in order to pass a 32 bit function instead of a 16 bit
75 * ===================================================================
76 * =================================================================== */
86 /* EPP DWORD WINAPI mciSendCommandA(UINT wDevID, UINT wMsg, DWORD dwParam1, DWORD dwParam2); */
88 /**************************************************************************
89 * MCI_SCAStarter [internal]
91 static DWORD CALLBACK
MCI_SCAStarter(LPVOID arg
)
93 struct SCA
* sca
= (struct SCA
*)arg
;
96 TRACE("In thread before async command (%08x,%u,%08lx,%08lx)\n",
97 sca
->wDevID
, sca
->wMsg
, sca
->dwParam1
, sca
->dwParam2
);
98 ret
= mciSendCommandA(sca
->wDevID
, sca
->wMsg
, sca
->dwParam1
| MCI_WAIT
, sca
->dwParam2
);
99 TRACE("In thread after async command (%08x,%u,%08lx,%08lx)\n",
100 sca
->wDevID
, sca
->wMsg
, sca
->dwParam1
, sca
->dwParam2
);
101 if (sca
->allocatedCopy
)
102 HeapFree(GetProcessHeap(), 0, (LPVOID
)sca
->dwParam2
);
103 HeapFree(GetProcessHeap(), 0, sca
);
105 WARN("Should not happen ? what's wrong \n");
106 /* should not go after this point */
110 /**************************************************************************
111 * MCI_SendCommandAsync [internal]
113 static DWORD
MCI_SendCommandAsync(UINT wDevID
, UINT wMsg
, DWORD dwParam1
,
114 DWORD dwParam2
, UINT size
)
116 struct SCA
* sca
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct SCA
));
119 return MCIERR_OUT_OF_MEMORY
;
121 sca
->wDevID
= wDevID
;
123 sca
->dwParam1
= dwParam1
;
126 sca
->dwParam2
= (DWORD
)HeapAlloc(GetProcessHeap(), 0, size
);
127 if (sca
->dwParam2
== 0) {
128 HeapFree(GetProcessHeap(), 0, sca
);
129 return MCIERR_OUT_OF_MEMORY
;
131 sca
->allocatedCopy
= TRUE
;
132 /* copy structure passed by program in dwParam2 to be sure
133 * we can still use it whatever the program does
135 memcpy((LPVOID
)sca
->dwParam2
, (LPVOID
)dwParam2
, size
);
137 sca
->dwParam2
= dwParam2
;
138 sca
->allocatedCopy
= FALSE
;
141 if (CreateThread(NULL
, 0, MCI_SCAStarter
, sca
, 0, NULL
) == 0) {
142 WARN("Couldn't allocate thread for async command handling, sending synchonously\n");
143 return MCI_SCAStarter(&sca
);
148 /*======================================================================*
149 * MCI MIDI implemantation *
150 *======================================================================*/
152 static DWORD
MIDI_mciResume(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
);
154 /**************************************************************************
155 * MIDI_drvOpen [internal]
157 static DWORD
MIDI_drvOpen(LPSTR str
, LPMCI_OPEN_DRIVER_PARMSA modp
)
159 WINE_MCIMIDI
* wmm
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINE_MCIMIDI
));
164 wmm
->wDevID
= modp
->wDeviceID
;
165 mciSetDriverData(wmm
->wDevID
, (DWORD
)wmm
);
166 modp
->wCustomCommandTable
= MCI_NO_COMMAND_TABLE
;
167 modp
->wType
= MCI_DEVTYPE_SEQUENCER
;
168 return modp
->wDeviceID
;
171 /**************************************************************************
172 * MCIMIDI_drvClose [internal]
174 static DWORD
MIDI_drvClose(DWORD dwDevID
)
176 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(dwDevID
);
179 HeapFree(GetProcessHeap(), 0, wmm
);
180 mciSetDriverData(dwDevID
, 0);
186 /**************************************************************************
187 * MIDI_mciGetOpenDev [internal]
189 static WINE_MCIMIDI
* MIDI_mciGetOpenDev(UINT wDevID
)
191 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(wDevID
);
193 if (wmm
== NULL
|| wmm
->nUseCount
== 0) {
194 WARN("Invalid wDevID=%u\n", wDevID
);
200 /**************************************************************************
201 * MIDI_mciReadByte [internal]
203 static DWORD
MIDI_mciReadByte(WINE_MCIMIDI
* wmm
, BYTE
*lpbyt
)
208 mmioRead(wmm
->hFile
, (HPSTR
)lpbyt
, sizeof(BYTE
)) != (long)sizeof(BYTE
)) {
209 WARN("Error reading wmm=%p\n", wmm
);
210 ret
= MCIERR_INVALID_FILE
;
216 /**************************************************************************
217 * MIDI_mciReadWord [internal]
219 static DWORD
MIDI_mciReadWord(WINE_MCIMIDI
* wmm
, LPWORD lpw
)
222 DWORD ret
= MCIERR_INVALID_FILE
;
225 MIDI_mciReadByte(wmm
, &hibyte
) == 0 &&
226 MIDI_mciReadByte(wmm
, &lobyte
) == 0) {
227 *lpw
= ((WORD
)hibyte
<< 8) + lobyte
;
233 /**************************************************************************
234 * MIDI_mciReadLong [internal]
236 static DWORD
MIDI_mciReadLong(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
239 DWORD ret
= MCIERR_INVALID_FILE
;
242 MIDI_mciReadWord(wmm
, &hiword
) == 0 &&
243 MIDI_mciReadWord(wmm
, &loword
) == 0) {
244 *lpdw
= MAKELONG(loword
, hiword
);
250 /**************************************************************************
251 * MIDI_mciReadVaryLen [internal]
253 static WORD
MIDI_mciReadVaryLen(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
260 ret
= MCIERR_INVALID_FILE
;
263 if (MIDI_mciReadByte(wmm
, &byte
) != 0) {
266 value
= (value
<< 7) + (byte
& 0x7F);
268 } while (byte
& 0x80);
271 TRACE("val=%08lX \n", value);
277 /**************************************************************************
278 * MIDI_mciReadNextEvent [internal]
280 static DWORD
MIDI_mciReadNextEvent(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
288 if (mmioSeek(wmm
->hFile
, mmt
->dwIndex
, SEEK_SET
) != mmt
->dwIndex
) {
289 WARN("Can't seek at %08lX \n", mmt
->dwIndex
);
290 return MCIERR_INVALID_FILE
;
292 evtLength
= MIDI_mciReadVaryLen(wmm
, &evtPulse
) + 1; /* > 0 */
293 MIDI_mciReadByte(wmm
, &b1
);
297 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
301 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
303 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
304 if (evtLength
>= 0x10000u
) {
305 /* this limitation shouldn't be a problem */
306 WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
309 hw
= LOWORD(evtLength
);
314 if (b1
& 0x80) { /* use running status ? */
315 mmt
->wLastCommand
= b1
;
316 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
319 b1
= mmt
->wLastCommand
;
321 switch ((b1
>> 4) & 0x07) {
322 case 0: case 1: case 2: case 3: case 6:
323 MIDI_mciReadByte(wmm
, &b3
); evtLength
++;
329 WARN("Strange indeed b1=0x%02x\n", b1
);
333 if (mmt
->dwIndex
+ evtLength
> mmt
->dwLast
)
334 return MCIERR_INTERNAL
;
336 mmt
->dwEventPulse
+= evtPulse
;
337 mmt
->dwEventData
= (hw
<< 16) + (b2
<< 8) + b1
;
338 mmt
->wEventLength
= evtLength
;
341 TRACE("[%u] => pulse=%08lx(%08lx), data=%08lx, length=%u\n",
342 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
343 mmt->dwEventData, mmt->wEventLength);
348 /**************************************************************************
349 * MIDI_mciReadMTrk [internal]
351 static DWORD
MIDI_mciReadMTrk(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
356 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
, (long)sizeof(FOURCC
)) !=
357 (long)sizeof(FOURCC
)) {
358 return MCIERR_INVALID_FILE
;
361 if (fourcc
!= mmioFOURCC('M', 'T', 'r', 'k')) {
362 WARN("Can't synchronize on 'MTrk' !\n");
363 return MCIERR_INVALID_FILE
;
366 if (MIDI_mciReadLong(wmm
, &toberead
) != 0) {
367 return MCIERR_INVALID_FILE
;
369 mmt
->dwFirst
= mmioSeek(wmm
->hFile
, 0, SEEK_CUR
); /* >= 0 */
370 mmt
->dwLast
= mmt
->dwFirst
+ toberead
;
372 /* compute # of pulses in this track */
373 mmt
->dwIndex
= mmt
->dwFirst
;
374 mmt
->dwEventPulse
= 0;
376 while (MIDI_mciReadNextEvent(wmm
, mmt
) == 0 && LOWORD(mmt
->dwEventData
) != 0x2FFF) {
380 mmt
->dwIndex
+= mmt
->wEventLength
;
382 switch (LOWORD(mmt
->dwEventData
)) {
385 /* position after meta data header */
386 mmioSeek(wmm
->hFile
, mmt
->dwIndex
+ HIWORD(mmt
->dwEventData
), SEEK_SET
);
387 len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
389 if (len
>= sizeof(buf
)) {
390 WARN("Buffer for text is too small (%d bytes, when %u are needed)\n", sizeof(buf
) - 1, len
);
391 len
= sizeof(buf
) - 1;
393 if (mmioRead(wmm
->hFile
, (HPSTR
)buf
, len
) == len
) {
394 buf
[len
] = 0; /* end string in case */
395 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
397 if (wmm
->lpstrCopyright
) {
398 WARN("Two copyright notices (%s|%s)\n", wmm
->lpstrCopyright
, buf
);
400 wmm
->lpstrCopyright
= HEAP_strdupA(GetProcessHeap(), 0, buf
);
404 if (wmm
->lpstrCopyright
) {
405 WARN("Two names (%s|%s)\n", wmm
->lpstrName
, buf
);
407 wmm
->lpstrName
= HEAP_strdupA(GetProcessHeap(), 0, buf
);
415 mmt
->dwLength
= mmt
->dwEventPulse
;
417 TRACE("Track %u has %lu bytes and %lu pulses\n", mmt
->wTrackNr
, toberead
, mmt
->dwLength
);
419 /* reset track data */
420 mmt
->wStatus
= 1; /* ok, playing */
421 mmt
->dwIndex
= mmt
->dwFirst
;
422 mmt
->dwEventPulse
= 0;
424 if (mmioSeek(wmm
->hFile
, 0, SEEK_CUR
) != mmt
->dwLast
) {
425 WARN("Ouch, out of sync seek=%lu track=%lu\n",
426 mmioSeek(wmm
->hFile
, 0, SEEK_CUR
), mmt
->dwLast
);
427 /* position at end of this track, to be ready to read next track */
428 mmioSeek(wmm
->hFile
, mmt
->dwLast
, SEEK_SET
);
434 /**************************************************************************
435 * MIDI_mciReadMThd [internal]
437 static DWORD
MIDI_mciReadMThd(WINE_MCIMIDI
* wmm
, DWORD dwOffset
)
443 TRACE("(%p, %08lX);\n", wmm
, dwOffset
);
445 if (mmioSeek(wmm
->hFile
, dwOffset
, SEEK_SET
) != dwOffset
) {
446 WARN("Can't seek at %08lX begin of 'MThd' \n", dwOffset
);
447 return MCIERR_INVALID_FILE
;
449 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
,
450 (long) sizeof(FOURCC
)) != (long) sizeof(FOURCC
))
451 return MCIERR_INVALID_FILE
;
453 if (fourcc
!= mmioFOURCC('M', 'T', 'h', 'd')) {
454 WARN("Can't synchronize on 'MThd' !\n");
455 return MCIERR_INVALID_FILE
;
458 if (MIDI_mciReadLong(wmm
, &toberead
) != 0 || toberead
< 3 * sizeof(WORD
))
459 return MCIERR_INVALID_FILE
;
461 if (MIDI_mciReadWord(wmm
, &wmm
->wFormat
) != 0 ||
462 MIDI_mciReadWord(wmm
, &wmm
->nTracks
) != 0 ||
463 MIDI_mciReadWord(wmm
, &wmm
->nDivision
) != 0) {
464 return MCIERR_INVALID_FILE
;
467 TRACE("toberead=0x%08lX, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
468 toberead
, wmm
->wFormat
, wmm
->nTracks
, wmm
->nDivision
);
470 /* MS doc says that the MIDI MCI time format must be put by default to the format
471 * stored in the MIDI file...
473 if (wmm
->nDivision
> 0x8000) {
474 /* eric.pouech@lemel.fr 98/11
475 * In did not check this very code (pulses are expressed as SMPTE sub-frames).
476 * In about 40 MB of MIDI files I have, none was SMPTE based...
477 * I'm just wondering if this is widely used :-). So, if someone has one of
478 * these files, I'd like to know about.
480 FIXME("Handling SMPTE time in MIDI files has not been tested\n"
481 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
483 switch (HIBYTE(wmm
->nDivision
)) {
484 case 0xE8: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
; break; /* -24 */
485 case 0xE7: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
; break; /* -25 */
486 case 0xE3: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
487 case 0xE2: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
; break; /* -30 */
489 WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm
->nDivision
));
490 return MCIERR_INVALID_FILE
;
492 switch (LOBYTE(wmm
->nDivision
)) {
493 case 4: /* MIDI Time Code */
496 case 80: /* SMPTE bit resolution */
499 WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm
->nDivision
));
500 return MCIERR_INVALID_FILE
;
502 } else if (wmm
->nDivision
== 0) {
503 WARN("Number of division is 0, can't support that !!\n");
504 return MCIERR_INVALID_FILE
;
506 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
509 switch (wmm
->wFormat
) {
511 if (wmm
->nTracks
!= 1) {
512 WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
520 WARN("Handling MIDI files which format = %d is not (yet) supported\n"
521 "Please report with MIDI file !\n", wmm
->wFormat
);
522 return MCIERR_INVALID_FILE
;
525 if (wmm
->nTracks
& 0x8000) {
526 /* this shouldn't be a problem... */
527 WARN("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
528 wmm
->nTracks
= 0x7FFF;
531 if ((wmm
->tracks
= HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK
) * wmm
->nTracks
)) == NULL
) {
532 return MCIERR_OUT_OF_MEMORY
;
535 toberead
-= 3 * sizeof(WORD
);
537 TRACE("Size of MThd > 6, skipping %ld extra bytes\n", toberead
);
538 mmioSeek(wmm
->hFile
, toberead
, SEEK_CUR
);
541 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
542 wmm
->tracks
[nt
].wTrackNr
= nt
;
543 if (MIDI_mciReadMTrk(wmm
, &wmm
->tracks
[nt
]) != 0) {
544 WARN("Can't read 'MTrk' header \n");
545 return MCIERR_INVALID_FILE
;
549 wmm
->dwTempo
= 500000;
554 /**************************************************************************
555 * MIDI_ConvertPulseToMS [internal]
557 static DWORD
MIDI_ConvertPulseToMS(WINE_MCIMIDI
* wmm
, DWORD pulse
)
561 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
562 * may change during file playing
564 if (wmm
->nDivision
== 0) {
565 FIXME("Shouldn't happen. wmm->nDivision = 0\n");
566 } else if (wmm
->nDivision
> 0x8000) { /* SMPTE, unchecked FIXME? */
567 int nf
= -(char)HIBYTE(wmm
->nDivision
); /* number of frames */
568 int nsf
= LOBYTE(wmm
->nDivision
); /* number of sub-frames */
569 ret
= (pulse
* 1000) / (nf
* nsf
);
571 ret
= (DWORD
)((double)pulse
* ((double)wmm
->dwTempo
/ 1000) /
572 (double)wmm
->nDivision
);
576 TRACE("pulse=%lu tempo=%lu division=%u=0x%04x => ms=%lu\n",
577 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
583 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
584 #define TIME_MS_IN_ONE_MINUTE (60*1000)
585 #define TIME_MS_IN_ONE_SECOND (1000)
587 /**************************************************************************
588 * MIDI_ConvertTimeFormatToMS [internal]
590 static DWORD
MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI
* wmm
, DWORD val
)
594 switch (wmm
->dwMciTimeFormat
) {
595 case MCI_FORMAT_MILLISECONDS
:
598 case MCI_FORMAT_SMPTE_24
:
600 (HIBYTE(HIWORD(val
)) * 125) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
601 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
603 case MCI_FORMAT_SMPTE_25
:
605 HIBYTE(HIWORD(val
)) * 40 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
606 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
608 case MCI_FORMAT_SMPTE_30
:
610 (HIBYTE(HIWORD(val
)) * 100) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
611 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
614 WARN("Bad time format %lu!\n", wmm
->dwMciTimeFormat
);
617 TRACE("val=%lu=0x%08lx [tf=%lu] => ret=%lu\n", val, val, wmm->dwMciTimeFormat, ret);
622 /**************************************************************************
623 * MIDI_ConvertMSToTimeFormat [internal]
625 static DWORD
MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI
* wmm
, DWORD _val
)
627 DWORD ret
= 0, val
= _val
;
630 switch (wmm
->dwMciTimeFormat
) {
631 case MCI_FORMAT_MILLISECONDS
:
634 case MCI_FORMAT_SMPTE_24
:
635 case MCI_FORMAT_SMPTE_25
:
636 case MCI_FORMAT_SMPTE_30
:
637 h
= val
/ TIME_MS_IN_ONE_HOUR
;
638 m
= (val
-= h
* TIME_MS_IN_ONE_HOUR
) / TIME_MS_IN_ONE_MINUTE
;
639 s
= (val
-= m
* TIME_MS_IN_ONE_MINUTE
) / TIME_MS_IN_ONE_SECOND
;
640 switch (wmm
->dwMciTimeFormat
) {
641 case MCI_FORMAT_SMPTE_24
:
642 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
643 f
= (val
* 3) / 125; val
-= (f
* 125) / 3;
645 case MCI_FORMAT_SMPTE_25
:
646 /* one frame is 1000/25 ms long, 1000/25 == 40 */
647 f
= val
/ 40; val
-= f
* 40;
649 case MCI_FORMAT_SMPTE_30
:
650 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
651 f
= (val
* 3) / 100; val
-= (f
* 100) / 3;
654 FIXME("There must be some bad bad programmer\n");
657 /* val contains the number of ms which cannot make a complete frame */
658 /* FIXME: is this correct ? programs seem to be happy with that */
659 ret
= (f
<< 24) | (s
<< 16) | (m
<< 8) | (h
<< 0);
662 WARN("Bad time format %lu!\n", wmm
->dwMciTimeFormat
);
665 TRACE("val=%lu [tf=%lu] => ret=%lu=0x%08lx\n", _val, wmm->dwMciTimeFormat, ret, ret);
670 /**************************************************************************
671 * MIDI_GetMThdLengthMS [internal]
673 static DWORD
MIDI_GetMThdLengthMS(WINE_MCIMIDI
* wmm
)
678 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
679 if (wmm
->wFormat
== 2) {
680 ret
+= wmm
->tracks
[nt
].dwLength
;
681 } else if (wmm
->tracks
[nt
].dwLength
> ret
) {
682 ret
= wmm
->tracks
[nt
].dwLength
;
685 /* FIXME: this is wrong if there is a tempo change inside the file */
686 return MIDI_ConvertPulseToMS(wmm
, ret
);
689 /**************************************************************************
690 * MIDI_mciOpen [internal]
692 static DWORD
MIDI_mciOpen(UINT wDevID
, DWORD dwFlags
, LPMCI_OPEN_PARMSA lpParms
)
696 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(wDevID
);
698 TRACE("(%04x, %08lX, %p)\n", wDevID
, dwFlags
, lpParms
);
700 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
701 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
702 if (dwFlags
& MCI_OPEN_SHAREABLE
)
703 return MCIERR_HARDWARE
;
705 if (wmm
->nUseCount
> 0) {
706 /* The driver is already opened on this channel
707 * MIDI sequencer cannot be shared
709 return MCIERR_DEVICE_OPEN
;
715 dwDeviceID
= lpParms
->wDeviceID
;
717 TRACE("wDevID=%04X (lpParams->wDeviceID=%08lX)\n", wDevID
, dwDeviceID
);
718 /* lpParms->wDeviceID = wDevID;*/
720 if (dwFlags
& MCI_OPEN_ELEMENT
) {
721 TRACE("MCI_OPEN_ELEMENT '%s' !\n", lpParms
->lpstrElementName
);
722 if (lpParms
->lpstrElementName
&& strlen(lpParms
->lpstrElementName
) > 0) {
723 wmm
->hFile
= mmioOpenA(lpParms
->lpstrElementName
, NULL
,
724 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
725 if (wmm
->hFile
== 0) {
726 WARN("Can't find file '%s' !\n", lpParms
->lpstrElementName
);
728 return MCIERR_FILE_NOT_FOUND
;
734 TRACE("hFile=%u\n", wmm
->hFile
);
736 /* FIXME: should I get a strdup() of it instead? */
737 wmm
->lpstrElementName
= HEAP_strdupA(GetProcessHeap(), 0, lpParms
->lpstrElementName
);
738 wmm
->lpstrCopyright
= NULL
;
739 wmm
->lpstrName
= NULL
;
741 wmm
->wNotifyDeviceID
= dwDeviceID
;
742 wmm
->dwStatus
= MCI_MODE_NOT_READY
; /* while loading file contents */
743 /* spec says it should be the default format from the MIDI file... */
744 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
746 if (wmm
->hFile
!= 0) {
751 if (mmioDescend(wmm
->hFile
, &ckMainRIFF
, NULL
, 0) != 0) {
752 dwRet
= MCIERR_INVALID_FILE
;
754 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08lX \n",
755 (LPSTR
)&ckMainRIFF
.ckid
, (LPSTR
)&ckMainRIFF
.fccType
, ckMainRIFF
.cksize
);
757 if (ckMainRIFF
.ckid
== FOURCC_RIFF
&& ckMainRIFF
.fccType
== mmioFOURCC('R', 'M', 'I', 'D')) {
758 mmckInfo
.ckid
= mmioFOURCC('d', 'a', 't', 'a');
759 mmioSeek(wmm
->hFile
, ckMainRIFF
.dwDataOffset
+ ((ckMainRIFF
.cksize
+ 1) & ~1), SEEK_SET
);
760 if (mmioDescend(wmm
->hFile
, &mmckInfo
, &ckMainRIFF
, MMIO_FINDCHUNK
) == 0) {
761 TRACE("... is a 'RMID' file \n");
762 dwOffset
= mmckInfo
.dwDataOffset
;
764 dwRet
= MCIERR_INVALID_FILE
;
767 if (dwRet
== 0 && MIDI_mciReadMThd(wmm
, dwOffset
) != 0) {
768 WARN("Can't read 'MThd' header \n");
769 dwRet
= MCIERR_INVALID_FILE
;
773 TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
781 mmioClose(wmm
->hFile
, 0);
784 wmm
->dwPositionMS
= 0;
785 wmm
->dwStatus
= MCI_MODE_STOP
;
790 /**************************************************************************
791 * MIDI_mciStop [internal]
793 static DWORD
MIDI_mciStop(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
795 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
797 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
799 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
801 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
802 wmm
->dwStatus
= MCI_MODE_STOP
;
803 midiOutClose(wmm
->hMidi
);
805 TRACE("wmm->dwStatus=%d\n", wmm
->dwStatus
);
807 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
808 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
809 mciDriverNotify((HWND
)LOWORD(lpParms
->dwCallback
),
810 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
815 /**************************************************************************
816 * MIDI_mciClose [internal]
818 static DWORD
MIDI_mciClose(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
820 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
822 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
824 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
826 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
827 MIDI_mciStop(wDevID
, MCI_WAIT
, lpParms
);
830 wmm
->dwStatus
= MCI_MODE_STOP
;
832 if (wmm
->nUseCount
== 0) {
833 if (wmm
->hFile
!= 0) {
834 mmioClose(wmm
->hFile
, 0);
836 TRACE("hFile closed !\n");
838 HeapFree(GetProcessHeap(), 0, wmm
->tracks
);
839 HeapFree(GetProcessHeap(), 0, (LPSTR
)wmm
->lpstrElementName
);
840 HeapFree(GetProcessHeap(), 0, (LPSTR
)wmm
->lpstrCopyright
);
841 HeapFree(GetProcessHeap(), 0, (LPSTR
)wmm
->lpstrName
);
843 TRACE("Shouldn't happen... nUseCount=%d\n", wmm
->nUseCount
);
844 return MCIERR_INTERNAL
;
847 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
848 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
849 mciDriverNotify((HWND
)LOWORD(lpParms
->dwCallback
),
850 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
855 /**************************************************************************
856 * MIDI_mciFindNextEvent [internal]
858 static MCI_MIDITRACK
* MIDI_mciFindNextEvent(WINE_MCIMIDI
* wmm
, LPDWORD hiPulse
)
863 *hiPulse
= 0xFFFFFFFFul
;
865 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
866 mmt
= &wmm
->tracks
[nt
];
868 if (mmt
->wStatus
== 0)
870 if (mmt
->dwEventPulse
< *hiPulse
) {
871 *hiPulse
= mmt
->dwEventPulse
;
875 return (cnt
== 0xFFFFu
) ? 0 /* no more event on all tracks */
879 /**************************************************************************
880 * MIDI_mciPlay [internal]
882 static DWORD
MIDI_mciPlay(UINT wDevID
, DWORD dwFlags
, LPMCI_PLAY_PARMS lpParms
)
884 DWORD dwStartMS
, dwEndMS
, dwRet
;
888 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
890 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
892 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
894 if (wmm
->hFile
== 0) {
895 WARN("Can't play: no file '%s' !\n", wmm
->lpstrElementName
);
896 return MCIERR_FILE_NOT_FOUND
;
899 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
900 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
901 /* FIXME: parameters (start/end) in lpParams may not be used */
902 return MIDI_mciResume(wDevID
, dwFlags
, (LPMCI_GENERIC_PARMS
)lpParms
);
904 WARN("Can't play: device is not stopped !\n");
905 return MCIERR_INTERNAL
;
908 if (!(dwFlags
& MCI_WAIT
)) {
909 /** FIXME: I'm not 100% sure that wNotifyDeviceID is the right value in all cases ??? */
910 return MCI_SendCommandAsync(wmm
->wNotifyDeviceID
, MCI_PLAY
, dwFlags
, (DWORD
)lpParms
, sizeof(LPMCI_PLAY_PARMS
));
913 if (lpParms
&& (dwFlags
& MCI_FROM
)) {
914 dwStartMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwFrom
);
916 dwStartMS
= wmm
->dwPositionMS
;
919 if (lpParms
&& (dwFlags
& MCI_TO
)) {
920 dwEndMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
922 dwEndMS
= 0xFFFFFFFFul
;
925 TRACE("Playing from %lu to %lu\n", dwStartMS
, dwEndMS
);
928 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
929 mmt
= &wmm
->tracks
[nt
];
931 mmt
->wStatus
= 1; /* ok, playing */
932 mmt
->dwIndex
= mmt
->dwFirst
;
933 if (wmm
->wFormat
== 2 && nt
> 0) {
934 mmt
->dwEventPulse
= wmm
->tracks
[nt
- 1].dwLength
;
936 mmt
->dwEventPulse
= 0;
938 MIDI_mciReadNextEvent(wmm
, mmt
); /* FIXME == 0 */
942 wmm
->dwTempo
= 500000;
943 wmm
->dwStatus
= MCI_MODE_PLAY
;
944 wmm
->dwPositionMS
= 0;
945 wmm
->wStartedPlaying
= FALSE
;
947 dwRet
= midiOutOpen(&wmm
->hMidi
, MIDIMAPPER
, 0L, 0L, CALLBACK_NULL
);
948 /* dwRet = midiInOpen(&wmm->hMidi, MIDIMAPPER, 0L, 0L, CALLBACK_NULL);*/
950 while (wmm
->dwStatus
!= MCI_MODE_STOP
) {
951 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
952 * too much. Tell gcc not to optimize status value using volatile.
954 while (((volatile WINE_MCIMIDI
*)wmm
)->dwStatus
== MCI_MODE_PAUSE
);
956 doPlay
= (wmm
->dwPositionMS
>= dwStartMS
&& wmm
->dwPositionMS
<= dwEndMS
);
958 TRACE("wmm->dwStatus=%d, doPlay=%c\n", wmm
->dwStatus
, doPlay
? 'T' : 'F');
960 if ((mmt
= MIDI_mciFindNextEvent(wmm
, &hiPulse
)) == NULL
)
961 break; /* no more event on tracks */
963 /* if starting playing, then set StartTicks to the value it would have had
964 * if play had started at position 0
966 if (doPlay
&& !wmm
->wStartedPlaying
) {
967 wmm
->dwStartTicks
= GetTickCount() - MIDI_ConvertPulseToMS(wmm
, wmm
->dwPulse
);
968 wmm
->wStartedPlaying
= TRUE
;
969 TRACE("Setting dwStartTicks to %lu\n", wmm
->dwStartTicks
);
972 if (hiPulse
> wmm
->dwPulse
) {
973 wmm
->dwPositionMS
+= MIDI_ConvertPulseToMS(wmm
, hiPulse
- wmm
->dwPulse
);
975 DWORD togo
= wmm
->dwStartTicks
+ wmm
->dwPositionMS
;
976 DWORD tc
= GetTickCount();
978 TRACE("Pulses hi=0x%08lx <> cur=0x%08lx\n", hiPulse
, wmm
->dwPulse
);
979 TRACE("Wait until %lu => %lu ms\n",
980 tc
- wmm
->dwStartTicks
, togo
- wmm
->dwStartTicks
);
984 wmm
->dwPulse
= hiPulse
;
987 switch (LOBYTE(LOWORD(mmt
->dwEventData
))) {
989 case 0xF7: /* sysex events */
991 FIXME("Not handling SysEx events (yet)\n");
995 /* position after meta data header */
996 mmioSeek(wmm
->hFile
, mmt
->dwIndex
+ HIWORD(mmt
->dwEventData
), SEEK_SET
);
997 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
998 case 0x00: /* 16-bit sequence number */
999 if (TRACE_ON(mcimidi
)) {
1002 MIDI_mciReadWord(wmm
, &twd
); /* == 0 */
1003 TRACE("Got sequence number %u\n", twd
);
1006 case 0x01: /* any text */
1007 case 0x02: /* Copyright Message text */
1008 case 0x03: /* Sequence/Track Name text */
1009 case 0x04: /* Instrument Name text */
1010 case 0x05: /* Lyric text */
1011 case 0x06: /* Marker text */
1012 case 0x07: /* Cue-point text */
1013 if (TRACE_ON(mcimidi
)) {
1015 WORD len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
1016 static char* info
[8] = {"", "Text", "Copyright", "Seq/Trk name",
1017 "Instrument", "Lyric", "Marker", "Cue-point"};
1018 WORD idx
= HIBYTE(LOWORD(mmt
->dwEventData
));
1020 if (len
>= sizeof(buf
)) {
1021 WARN("Buffer for text is too small (%d bytes, when %u are needed)\n", sizeof(buf
) - 1, len
);
1022 len
= sizeof(buf
) - 1;
1024 if (mmioRead(wmm
->hFile
, (HPSTR
)buf
, len
) == len
) {
1025 buf
[len
] = 0; /* end string in case */
1026 TRACE("%s => \"%s\"\n", (idx
< 8 ) ? info
[idx
] : "", buf
);
1028 WARN("Couldn't read data for %s\n", (idx
< 8) ? info
[idx
] : "");
1033 /* MIDI channel (cc) */
1034 if (FIXME_ON(mcimidi
)) {
1037 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
1038 FIXME("NIY: MIDI channel=%u, track=%u\n", bt
, mmt
->wTrackNr
);
1042 /* MIDI port (pp) */
1043 if (FIXME_ON(mcimidi
)) {
1046 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
1047 FIXME("NIY: MIDI port=%u, track=%u\n", bt
, mmt
->wTrackNr
);
1050 case 0x2F: /* end of track */
1053 case 0x51:/* set tempo */
1054 /* Tempo is expressed in µ-seconds per midi quarter note
1055 * for format 1 MIDI files, this can only be present on track #0
1057 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
1058 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt
->wTrackNr
);
1063 MIDI_mciReadByte(wmm
, &tbt
); value
= ((DWORD
)tbt
) << 16;
1064 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 8;
1065 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 0;
1066 TRACE("Setting tempo to %ld (BPM=%ld)\n", wmm
->dwTempo
, (value
) ? (60000000l / value
) : 0);
1067 wmm
->dwTempo
= value
;
1070 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
1071 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
1072 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt
->wTrackNr
);
1073 } if (mmt
->dwEventPulse
!= 0) {
1074 WARN("SMPTE track start can only be expressed at start of track (%lu)\n", mmt
->dwEventPulse
);
1076 BYTE h
, m
, s
, f
, ff
;
1078 MIDI_mciReadByte(wmm
, &h
);
1079 MIDI_mciReadByte(wmm
, &m
);
1080 MIDI_mciReadByte(wmm
, &s
);
1081 MIDI_mciReadByte(wmm
, &f
);
1082 MIDI_mciReadByte(wmm
, &ff
);
1083 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h
, m
, s
, f
, ff
);
1086 case 0x58: /* file rythm */
1087 if (TRACE_ON(mcimidi
)) {
1088 BYTE num
, den
, cpmc
, _32npqn
;
1090 MIDI_mciReadByte(wmm
, &num
);
1091 MIDI_mciReadByte(wmm
, &den
); /* to notate e.g. 6/8 */
1092 MIDI_mciReadByte(wmm
, &cpmc
); /* number of MIDI clocks per metronome click */
1093 MIDI_mciReadByte(wmm
, &_32npqn
); /* number of notated 32nd notes per MIDI quarter note */
1095 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num
, 1 << den
, cpmc
, _32npqn
);
1098 case 0x59: /* key signature */
1099 if (TRACE_ON(mcimidi
)) {
1102 MIDI_mciReadByte(wmm
, &sf
);
1103 MIDI_mciReadByte(wmm
, &mm
);
1105 if (sf
>= 0x80) TRACE("%d flats\n", -(char)sf
);
1106 else if (sf
> 0) TRACE("%d sharps\n", (char)sf
);
1107 else TRACE("Key of C\n");
1108 TRACE("Mode: %s\n", (mm
= 0) ? "major" : "minor");
1112 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt
->dwEventData
)));
1118 dwRet
= midiOutShortMsg(wmm
->hMidi
, mmt
->dwEventData
);
1120 switch (LOBYTE(LOWORD(mmt
->dwEventData
)) & 0xF0) {
1126 dwRet
= midiOutShortMsg(wmm
->hMidi
, mmt
->dwEventData
);
1130 mmt
->dwIndex
+= mmt
->wEventLength
;
1131 if (mmt
->dwIndex
< mmt
->dwFirst
|| mmt
->dwIndex
>= mmt
->dwLast
) {
1135 MIDI_mciReadNextEvent(wmm
, mmt
);
1139 midiOutReset(wmm
->hMidi
);
1141 dwRet
= midiOutClose(wmm
->hMidi
);
1142 wmm
->dwStatus
= MCI_MODE_STOP
;
1144 /* to restart playing at beginning when it's over */
1145 wmm
->dwPositionMS
= 0;
1147 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
1148 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1149 mciDriverNotify((HWND
)LOWORD(lpParms
->dwCallback
),
1150 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1155 /**************************************************************************
1156 * MIDI_mciRecord [internal]
1158 static DWORD
MIDI_mciRecord(UINT wDevID
, DWORD dwFlags
, LPMCI_RECORD_PARMS lpParms
)
1163 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1165 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1167 if (wmm
== 0) return MCIERR_INVALID_DEVICE_ID
;
1169 if (wmm
->hFile
== 0) {
1170 WARN("Can't find file='%s' !\n", wmm
->lpstrElementName
);
1171 return MCIERR_FILE_NOT_FOUND
;
1173 start
= 1; end
= 99999;
1174 if (lpParms
&& (dwFlags
& MCI_FROM
)) {
1175 start
= lpParms
->dwFrom
;
1176 TRACE("MCI_FROM=%d \n", start
);
1178 if (lpParms
&& (dwFlags
& MCI_TO
)) {
1179 end
= lpParms
->dwTo
;
1180 TRACE("MCI_TO=%d \n", end
);
1182 midiHdr
.lpData
= (LPSTR
) HeapAlloc(GetProcessHeap(), 0, 1200);
1183 if (!midiHdr
.lpData
)
1184 return MCIERR_OUT_OF_MEMORY
;
1185 midiHdr
.dwBufferLength
= 1024;
1186 midiHdr
.dwUser
= 0L;
1187 midiHdr
.dwFlags
= 0L;
1188 dwRet
= midiInPrepareHeader(wmm
->hMidi
, &midiHdr
, sizeof(MIDIHDR
));
1189 TRACE("After MIDM_PREPARE \n");
1190 wmm
->dwStatus
= MCI_MODE_RECORD
;
1191 /* FIXME: there is no buffer added */
1192 while (wmm
->dwStatus
!= MCI_MODE_STOP
) {
1193 TRACE("wmm->dwStatus=%p %d\n",
1194 &wmm
->dwStatus
, wmm
->dwStatus
);
1195 midiHdr
.dwBytesRecorded
= 0;
1196 dwRet
= midiInStart(wmm
->hMidi
);
1197 TRACE("midiInStart => dwBytesRecorded=%lu\n", midiHdr
.dwBytesRecorded
);
1198 if (midiHdr
.dwBytesRecorded
== 0) break;
1200 TRACE("Before MIDM_UNPREPARE \n");
1201 dwRet
= midiInUnprepareHeader(wmm
->hMidi
, &midiHdr
, sizeof(MIDIHDR
));
1202 TRACE("After MIDM_UNPREPARE \n");
1203 if (midiHdr
.lpData
!= NULL
) {
1204 HeapFree(GetProcessHeap(), 0, midiHdr
.lpData
);
1205 midiHdr
.lpData
= NULL
;
1207 wmm
->dwStatus
= MCI_MODE_STOP
;
1208 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
1209 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1210 mciDriverNotify((HWND
)LOWORD(lpParms
->dwCallback
),
1211 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1216 /**************************************************************************
1217 * MIDI_mciPause [internal]
1219 static DWORD
MIDI_mciPause(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1221 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1223 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1225 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1227 if (wmm
->dwStatus
== MCI_MODE_PLAY
) {
1228 /* stop all notes */
1230 for (chn
= 0; chn
< 16; chn
++)
1231 midiOutShortMsg(wmm
->hMidi
, 0x78B0 | chn
);
1232 wmm
->dwStatus
= MCI_MODE_PAUSE
;
1234 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
1235 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1236 mciDriverNotify((HWND
)LOWORD(lpParms
->dwCallback
),
1237 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1243 /**************************************************************************
1244 * MIDI_mciResume [internal]
1246 static DWORD
MIDI_mciResume(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1248 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1250 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1252 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1254 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
1255 wmm
->wStartedPlaying
= FALSE
;
1256 wmm
->dwStatus
= MCI_MODE_PLAY
;
1258 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
1259 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1260 mciDriverNotify((HWND
)LOWORD(lpParms
->dwCallback
),
1261 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1266 /**************************************************************************
1267 * MIDI_mciSet [internal]
1269 static DWORD
MIDI_mciSet(UINT wDevID
, DWORD dwFlags
, LPMCI_SET_PARMS lpParms
)
1271 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1273 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1275 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1276 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1278 if (dwFlags
& MCI_SET_TIME_FORMAT
) {
1279 switch (lpParms
->dwTimeFormat
) {
1280 case MCI_FORMAT_MILLISECONDS
:
1281 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1282 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
1284 case MCI_FORMAT_SMPTE_24
:
1285 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1286 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
;
1288 case MCI_FORMAT_SMPTE_25
:
1289 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1290 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
;
1292 case MCI_FORMAT_SMPTE_30
:
1293 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1294 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
;
1297 WARN("Bad time format %lu!\n", lpParms
->dwTimeFormat
);
1298 return MCIERR_BAD_TIME_FORMAT
;
1301 if (dwFlags
& MCI_SET_VIDEO
) {
1302 TRACE("No support for video !\n");
1303 return MCIERR_UNSUPPORTED_FUNCTION
;
1305 if (dwFlags
& MCI_SET_DOOR_OPEN
) {
1306 TRACE("No support for door open !\n");
1307 return MCIERR_UNSUPPORTED_FUNCTION
;
1309 if (dwFlags
& MCI_SET_DOOR_CLOSED
) {
1310 TRACE("No support for door close !\n");
1311 return MCIERR_UNSUPPORTED_FUNCTION
;
1313 if (dwFlags
& MCI_SET_AUDIO
) {
1314 if (dwFlags
& MCI_SET_ON
) {
1315 TRACE("MCI_SET_ON audio !\n");
1316 } else if (dwFlags
& MCI_SET_OFF
) {
1317 TRACE("MCI_SET_OFF audio !\n");
1319 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1320 return MCIERR_BAD_INTEGER
;
1323 if (lpParms
->dwAudio
& MCI_SET_AUDIO_ALL
)
1324 TRACE("MCI_SET_AUDIO_ALL !\n");
1325 if (lpParms
->dwAudio
& MCI_SET_AUDIO_LEFT
)
1326 TRACE("MCI_SET_AUDIO_LEFT !\n");
1327 if (lpParms
->dwAudio
& MCI_SET_AUDIO_RIGHT
)
1328 TRACE("MCI_SET_AUDIO_RIGHT !\n");
1331 if (dwFlags
& MCI_SEQ_SET_MASTER
)
1332 TRACE("MCI_SEQ_SET_MASTER !\n");
1333 if (dwFlags
& MCI_SEQ_SET_SLAVE
)
1334 TRACE("MCI_SEQ_SET_SLAVE !\n");
1335 if (dwFlags
& MCI_SEQ_SET_OFFSET
)
1336 TRACE("MCI_SEQ_SET_OFFSET !\n");
1337 if (dwFlags
& MCI_SEQ_SET_PORT
)
1338 TRACE("MCI_SEQ_SET_PORT !\n");
1339 if (dwFlags
& MCI_SEQ_SET_TEMPO
)
1340 TRACE("MCI_SEQ_SET_TEMPO !\n");
1344 /**************************************************************************
1345 * MIDI_mciStatus [internal]
1347 static DWORD
MIDI_mciStatus(UINT wDevID
, DWORD dwFlags
, LPMCI_STATUS_PARMS lpParms
)
1349 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1352 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1354 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1355 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1357 if (dwFlags
& MCI_STATUS_ITEM
) {
1358 switch (lpParms
->dwItem
) {
1359 case MCI_STATUS_CURRENT_TRACK
:
1360 /* FIXME in Format 2 */
1361 lpParms
->dwReturn
= 1;
1362 TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms
->dwReturn
);
1364 case MCI_STATUS_LENGTH
:
1365 if ((dwFlags
& MCI_TRACK
) && wmm
->wFormat
== 2) {
1366 if (lpParms
->dwTrack
>= wmm
->nTracks
)
1367 return MCIERR_BAD_INTEGER
;
1368 /* FIXME: this is wrong if there is a tempo change inside the file */
1369 lpParms
->dwReturn
= MIDI_ConvertPulseToMS(wmm
, wmm
->tracks
[lpParms
->dwTrack
].dwLength
);
1371 lpParms
->dwReturn
= MIDI_GetMThdLengthMS(wmm
);
1373 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
, lpParms
->dwReturn
);
1374 TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms
->dwReturn
);
1376 case MCI_STATUS_MODE
:
1377 TRACE("MCI_STATUS_MODE => %u\n", wmm
->dwStatus
);
1378 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwStatus
, wmm
->dwStatus
);
1379 ret
= MCI_RESOURCE_RETURNED
;
1381 case MCI_STATUS_MEDIA_PRESENT
:
1382 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1383 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1384 ret
= MCI_RESOURCE_RETURNED
;
1386 case MCI_STATUS_NUMBER_OF_TRACKS
:
1387 lpParms
->dwReturn
= (wmm
->wFormat
== 2) ? wmm
->nTracks
: 1;
1388 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms
->dwReturn
);
1390 case MCI_STATUS_POSITION
:
1391 /* FIXME: do I need to use MCI_TRACK ? */
1392 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
,
1393 (dwFlags
& MCI_STATUS_START
) ? 0 : wmm
->dwPositionMS
);
1394 TRACE("MCI_STATUS_POSITION %s => %lu\n",
1395 (dwFlags
& MCI_STATUS_START
) ? "start" : "current", lpParms
->dwReturn
);
1397 case MCI_STATUS_READY
:
1398 lpParms
->dwReturn
= (wmm
->dwStatus
== MCI_MODE_NOT_READY
) ?
1399 MAKEMCIRESOURCE(FALSE
, MCI_FALSE
) : MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1400 ret
= MCI_RESOURCE_RETURNED
;
1401 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms
->dwReturn
));
1403 case MCI_STATUS_TIME_FORMAT
:
1404 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwMciTimeFormat
, wmm
->dwMciTimeFormat
);
1405 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms
->dwReturn
));
1406 ret
= MCI_RESOURCE_RETURNED
;
1408 case MCI_SEQ_STATUS_DIVTYPE
:
1409 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1410 if (wmm
->nDivision
> 0x8000) {
1411 switch (wmm
->nDivision
) {
1412 case 0xE8: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_24
; break; /* -24 */
1413 case 0xE7: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_25
; break; /* -25 */
1414 case 0xE3: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
1415 case 0xE2: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30
; break; /* -30 */
1416 default: FIXME("There is a bad bad programmer\n");
1419 lpParms
->dwReturn
= MCI_SEQ_DIV_PPQN
;
1421 lpParms
->dwReturn
= MAKEMCIRESOURCE(lpParms
->dwReturn
,lpParms
->dwReturn
);
1422 ret
= MCI_RESOURCE_RETURNED
;
1424 case MCI_SEQ_STATUS_MASTER
:
1425 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1426 lpParms
->dwReturn
= 0;
1428 case MCI_SEQ_STATUS_SLAVE
:
1429 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1430 lpParms
->dwReturn
= 0;
1432 case MCI_SEQ_STATUS_OFFSET
:
1433 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1434 lpParms
->dwReturn
= 0;
1436 case MCI_SEQ_STATUS_PORT
:
1437 TRACE("MCI_SEQ_STATUS_PORT (%u)!\n", wmm
->wDevID
);
1438 lpParms
->dwReturn
= wmm
->wDevID
;
1440 case MCI_SEQ_STATUS_TEMPO
:
1441 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1442 lpParms
->dwReturn
= wmm
->dwTempo
;
1445 FIXME("Unknowm command %08lX !\n", lpParms
->dwItem
);
1446 return MCIERR_UNRECOGNIZED_COMMAND
;
1449 WARN("No Status-Item!\n");
1450 return MCIERR_UNRECOGNIZED_COMMAND
;
1452 if (dwFlags
& MCI_NOTIFY
) {
1453 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1454 mciDriverNotify((HWND
)LOWORD(lpParms
->dwCallback
),
1455 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1460 /**************************************************************************
1461 * MIDI_mciGetDevCaps [internal]
1463 static DWORD
MIDI_mciGetDevCaps(UINT wDevID
, DWORD dwFlags
,
1464 LPMCI_GETDEVCAPS_PARMS lpParms
)
1466 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1469 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1471 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1472 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1474 if (dwFlags
& MCI_GETDEVCAPS_ITEM
) {
1475 switch (lpParms
->dwItem
) {
1476 case MCI_GETDEVCAPS_DEVICE_TYPE
:
1477 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1478 lpParms
->dwReturn
= MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER
, MCI_DEVTYPE_SEQUENCER
);
1479 ret
= MCI_RESOURCE_RETURNED
;
1481 case MCI_GETDEVCAPS_HAS_AUDIO
:
1482 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1483 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1484 ret
= MCI_RESOURCE_RETURNED
;
1486 case MCI_GETDEVCAPS_HAS_VIDEO
:
1487 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1488 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1489 ret
= MCI_RESOURCE_RETURNED
;
1491 case MCI_GETDEVCAPS_USES_FILES
:
1492 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1493 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1494 ret
= MCI_RESOURCE_RETURNED
;
1496 case MCI_GETDEVCAPS_COMPOUND_DEVICE
:
1497 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1498 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1499 ret
= MCI_RESOURCE_RETURNED
;
1501 case MCI_GETDEVCAPS_CAN_EJECT
:
1502 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1503 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1504 ret
= MCI_RESOURCE_RETURNED
;
1506 case MCI_GETDEVCAPS_CAN_PLAY
:
1507 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1508 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1509 ret
= MCI_RESOURCE_RETURNED
;
1511 case MCI_GETDEVCAPS_CAN_RECORD
:
1512 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1513 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1514 ret
= MCI_RESOURCE_RETURNED
;
1516 case MCI_GETDEVCAPS_CAN_SAVE
:
1517 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1518 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1519 ret
= MCI_RESOURCE_RETURNED
;
1522 FIXME("Unknown capability (%08lx) !\n", lpParms
->dwItem
);
1523 return MCIERR_UNRECOGNIZED_COMMAND
;
1526 WARN("No GetDevCaps-Item !\n");
1527 return MCIERR_UNRECOGNIZED_COMMAND
;
1532 /**************************************************************************
1533 * MIDI_mciInfo [internal]
1535 static DWORD
MIDI_mciInfo(UINT wDevID
, DWORD dwFlags
, LPMCI_INFO_PARMSA lpParms
)
1538 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1541 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1543 if (lpParms
== NULL
|| lpParms
->lpstrReturn
== NULL
)
1544 return MCIERR_NULL_PARAMETER_BLOCK
;
1545 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1547 TRACE("buf=%p, len=%lu\n", lpParms
->lpstrReturn
, lpParms
->dwRetSize
);
1549 switch (dwFlags
& ~(MCI_WAIT
|MCI_NOTIFY
)) {
1550 case MCI_INFO_PRODUCT
:
1551 str
= "Wine's MIDI sequencer";
1554 str
= wmm
->lpstrElementName
;
1556 case MCI_INFO_COPYRIGHT
:
1557 str
= wmm
->lpstrCopyright
;
1560 str
= wmm
->lpstrName
;
1563 WARN("Don't know this info command (%lu)\n", dwFlags
);
1564 return MCIERR_UNRECOGNIZED_COMMAND
;
1567 if (lpParms
->dwRetSize
<= strlen(str
)) {
1568 lstrcpynA(lpParms
->lpstrReturn
, str
, lpParms
->dwRetSize
- 1);
1569 ret
= MCIERR_PARAM_OVERFLOW
;
1571 strcpy(lpParms
->lpstrReturn
, str
);
1574 *lpParms
->lpstrReturn
= 0;
1579 /**************************************************************************
1580 * MIDI_mciSeek [internal]
1582 static DWORD
MIDI_mciSeek(UINT wDevID
, DWORD dwFlags
, LPMCI_SEEK_PARMS lpParms
)
1585 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1587 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1589 if (lpParms
== NULL
) {
1590 ret
= MCIERR_NULL_PARAMETER_BLOCK
;
1591 } else if (wmm
== NULL
) {
1592 ret
= MCIERR_INVALID_DEVICE_ID
;
1594 MIDI_mciStop(wDevID
, MCI_WAIT
, 0);
1596 if (dwFlags
& MCI_SEEK_TO_START
) {
1597 wmm
->dwPositionMS
= 0;
1598 } else if (dwFlags
& MCI_SEEK_TO_END
) {
1599 wmm
->dwPositionMS
= 0xFFFFFFFF; /* fixme */
1600 } else if (dwFlags
& MCI_TO
) {
1601 wmm
->dwPositionMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
1603 WARN("dwFlag doesn't tell where to seek to...\n");
1604 return MCIERR_MISSING_PARAMETER
;
1607 TRACE("Seeking to position=%lu ms\n", wmm
->dwPositionMS
);
1609 if (dwFlags
& MCI_NOTIFY
) {
1610 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1611 mciDriverNotify((HWND
)LOWORD(lpParms
->dwCallback
),
1612 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1618 /*======================================================================*
1619 * MIDI entry points *
1620 *======================================================================*/
1622 /**************************************************************************
1623 * MCIMIDI_DriverProc [sample driver]
1625 LONG CALLBACK
MCIMIDI_DriverProc(DWORD dwDevID
, HDRVR hDriv
, DWORD wMsg
,
1626 DWORD dwParam1
, DWORD dwParam2
)
1629 case DRV_LOAD
: return 1;
1630 case DRV_FREE
: return 1;
1631 case DRV_ENABLE
: return 1;
1632 case DRV_DISABLE
: return 1;
1633 case DRV_QUERYCONFIGURE
: return 1;
1634 case DRV_CONFIGURE
: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK
); return 1;
1635 case DRV_INSTALL
: return DRVCNF_RESTART
;
1636 case DRV_REMOVE
: return DRVCNF_RESTART
;
1637 case DRV_OPEN
: return MIDI_drvOpen((LPSTR
)dwParam1
, (LPMCI_OPEN_DRIVER_PARMSA
)dwParam2
);
1638 case DRV_CLOSE
: return MIDI_drvClose(dwDevID
);
1639 case MCI_OPEN_DRIVER
: return MIDI_mciOpen (dwDevID
, dwParam1
, (LPMCI_OPEN_PARMSA
) dwParam2
);
1640 case MCI_CLOSE_DRIVER
: return MIDI_mciClose (dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1641 case MCI_PLAY
: return MIDI_mciPlay (dwDevID
, dwParam1
, (LPMCI_PLAY_PARMS
) dwParam2
);
1642 case MCI_RECORD
: return MIDI_mciRecord (dwDevID
, dwParam1
, (LPMCI_RECORD_PARMS
) dwParam2
);
1643 case MCI_STOP
: return MIDI_mciStop (dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1644 case MCI_SET
: return MIDI_mciSet (dwDevID
, dwParam1
, (LPMCI_SET_PARMS
) dwParam2
);
1645 case MCI_PAUSE
: return MIDI_mciPause (dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1646 case MCI_RESUME
: return MIDI_mciResume (dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1647 case MCI_STATUS
: return MIDI_mciStatus (dwDevID
, dwParam1
, (LPMCI_STATUS_PARMS
) dwParam2
);
1648 case MCI_GETDEVCAPS
: return MIDI_mciGetDevCaps(dwDevID
, dwParam1
, (LPMCI_GETDEVCAPS_PARMS
)dwParam2
);
1649 case MCI_INFO
: return MIDI_mciInfo (dwDevID
, dwParam1
, (LPMCI_INFO_PARMSA
) dwParam2
);
1650 case MCI_SEEK
: return MIDI_mciSeek (dwDevID
, dwParam1
, (LPMCI_SEEK_PARMS
) dwParam2
);
1651 /* commands that should be supported */
1667 WARN("Unsupported command [%lu]\n", wMsg
);
1669 /* commands that should report an error */
1671 TRACE("Unsupported command [%lu]\n", wMsg
);
1675 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1678 TRACE("Sending msg [%lu] to default driver proc\n", wMsg
);
1679 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1681 return MCIERR_UNRECOGNIZED_COMMAND
;