1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * Sample MIDI Wine Driver for Linux
6 * Copyright 1994 Martin Ayotte
11 * 98/7 changes for making this MIDI driver work on OSS
12 * current support is limited to MIDI ports of OSS systems
13 * 98/9 rewriting MCI code for MIDI
14 * 98/11 splitted in midi.c and mcimidi.c
19 #include "multimedia.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(mcimidi
)
27 #ifdef SNDCTL_MIDI_INFO
30 DWORD dwFirst
; /* offset in file of track */
31 DWORD dwLast
; /* number of bytes in file of track */
32 DWORD dwIndex
; /* current index in file (dwFirst <= dwIndex < dwLast) */
33 DWORD dwLength
; /* number of pulses in this track */
34 DWORD dwEventPulse
; /* current pulse # (event) pointed by dwIndex */
35 DWORD dwEventData
; /* current data (event) pointed by dwIndex */
36 WORD wEventLength
; /* current length (event) pointed by dwIndex */
37 WORD wStatus
: 1, /* 1 : playing, 0 : done */
39 wLastCommand
: 8; /* last MIDI command on track */
42 typedef struct tagWINE_MCIMIDI
{
45 int nUseCount
; /* Incremented for each shared open */
46 WORD wNotifyDeviceID
; /* MCI device ID with a pending notification */
47 HANDLE16 hCallback
; /* Callback handle for pending notification */
48 HMMIO hFile
; /* mmio file handle open as Element */
49 LPCSTR lpstrElementName
; /* Name of file */
51 WORD dwStatus
; /* one from MCI_MODE_xxxx */
52 DWORD dwMciTimeFormat
; /* One of the supported MCI_FORMAT_xxxx */
53 WORD wFormat
; /* Format of MIDI hFile (0, 1 or 2) */
54 WORD nTracks
; /* Number of tracks in hFile */
55 WORD nDivision
; /* Number of division in hFile PPQN or SMPTE */
57 DWORD dwTempo
; /* Tempo (# of 1/4 note per second */
58 MCI_MIDITRACK
* tracks
; /* Content of each track */
65 #endif /* defined(SNDCTL_MIDI_INFO) */
67 /*======================================================================*
68 * MCI MIDI implemantation *
69 *======================================================================*/
71 #ifdef SNDCTL_MIDI_INFO
73 static DWORD
MIDI_mciResume(UINT16 wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
);
75 /**************************************************************************
76 * MIDI_drvOpen [internal]
78 static DWORD
MIDI_drvOpen(LPSTR str
, LPMCI_OPEN_DRIVER_PARMSA modp
)
80 WINE_MCIMIDI
* wmm
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINE_MCIMIDI
));
85 wmm
->wDevID
= modp
->wDeviceID
;
86 mciSetDriverData(wmm
->wDevID
, (DWORD
)wmm
);
87 modp
->wCustomCommandTable
= MCI_NO_COMMAND_TABLE
;
88 modp
->wType
= MCI_DEVTYPE_SEQUENCER
;
89 return modp
->wDeviceID
;
92 /**************************************************************************
93 * MCIMIDI_drvClose [internal]
95 static DWORD
MIDI_drvClose(DWORD dwDevID
)
97 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(dwDevID
);
100 HeapFree(GetProcessHeap(), 0, wmm
);
101 mciSetDriverData(dwDevID
, 0);
107 /**************************************************************************
108 * MIDI_mciGetOpenDev [internal]
110 static WINE_MCIMIDI
* MIDI_mciGetOpenDev(UINT16 wDevID
)
112 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(wDevID
);
114 if (wmm
== NULL
|| wmm
->nUseCount
== 0) {
115 WARN("Invalid wDevID=%u\n", wDevID
);
121 /**************************************************************************
122 * MIDI_mciReadByte [internal]
124 static DWORD
MIDI_mciReadByte(WINE_MCIMIDI
* wmm
, BYTE
*lpbyt
)
129 mmioRead(wmm
->hFile
, (HPSTR
)lpbyt
, (long)sizeof(BYTE
)) != (long)sizeof(BYTE
)) {
130 WARN("Error reading wmm=%p\n", wmm
);
131 ret
= MCIERR_INVALID_FILE
;
137 /**************************************************************************
138 * MIDI_mciReadWord [internal]
140 static DWORD
MIDI_mciReadWord(WINE_MCIMIDI
* wmm
, LPWORD lpw
)
143 DWORD ret
= MCIERR_INVALID_FILE
;
146 MIDI_mciReadByte(wmm
, &hibyte
) == 0 &&
147 MIDI_mciReadByte(wmm
, &lobyte
) == 0) {
148 *lpw
= ((WORD
)hibyte
<< 8) + lobyte
;
154 /**************************************************************************
155 * MIDI_mciReadLong [internal]
157 static DWORD
MIDI_mciReadLong(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
160 DWORD ret
= MCIERR_INVALID_FILE
;
163 MIDI_mciReadWord(wmm
, &hiword
) == 0 &&
164 MIDI_mciReadWord(wmm
, &loword
) == 0) {
165 *lpdw
= MAKELONG(loword
, hiword
);
171 /**************************************************************************
172 * MIDI_mciReadVaryLen [internal]
174 static WORD
MIDI_mciReadVaryLen(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
181 ret
= MCIERR_INVALID_FILE
;
184 if (MIDI_mciReadByte(wmm
, &byte
) != 0) {
187 value
= (value
<< 7) + (byte
& 0x7F);
189 } while (byte
& 0x80);
192 TRACE("val=%08lX \n", value);
198 /**************************************************************************
199 * MIDI_mciReadNextEvent [internal]
201 static DWORD
MIDI_mciReadNextEvent(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
209 if (mmioSeek(wmm
->hFile
, mmt
->dwIndex
, SEEK_SET
) != mmt
->dwIndex
) {
210 WARN("Can't seek at %08lX \n", mmt
->dwIndex
);
211 return MCIERR_INVALID_FILE
;
213 evtLength
= MIDI_mciReadVaryLen(wmm
, &evtPulse
) + 1; /* > 0 */
214 MIDI_mciReadByte(wmm
, &b1
);
218 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
222 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
224 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
225 if (evtLength
>= 0x10000u
) {
226 /* this limitation shouldn't be a problem */
227 WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
230 hw
= LOWORD(evtLength
);
235 if (b1
& 0x80) { /* use running status ? */
236 mmt
->wLastCommand
= b1
;
237 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
240 b1
= mmt
->wLastCommand
;
242 switch ((b1
>> 4) & 0x07) {
243 case 0: case 1: case 2: case 3: case 6:
244 MIDI_mciReadByte(wmm
, &b3
); evtLength
++;
250 WARN("Strange indeed b1=0x%02x\n", b1
);
254 if (mmt
->dwIndex
+ evtLength
> mmt
->dwLast
)
255 return MCIERR_INTERNAL
;
257 mmt
->dwEventPulse
+= evtPulse
;
258 mmt
->dwEventData
= (hw
<< 16) + (b2
<< 8) + b1
;
259 mmt
->wEventLength
= evtLength
;
262 TRACE("[%u] => pulse=%08lx(%08lx), data=%08lx, length=%u\n",
263 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
264 mmt->dwEventData, mmt->wEventLength);
269 /**************************************************************************
270 * MIDI_mciReadMTrk [internal]
272 static DWORD
MIDI_mciReadMTrk(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
277 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
, (long)sizeof(FOURCC
)) !=
278 (long)sizeof(FOURCC
)) {
279 return MCIERR_INVALID_FILE
;
282 if (fourcc
!= mmioFOURCC('M', 'T', 'r', 'k')) {
283 WARN("Can't synchronize on 'MTrk' !\n");
284 return MCIERR_INVALID_FILE
;
287 if (MIDI_mciReadLong(wmm
, &toberead
) != 0) {
288 return MCIERR_INVALID_FILE
;
290 mmt
->dwFirst
= mmioSeek(wmm
->hFile
, 0, SEEK_CUR
); /* >= 0 */
291 mmt
->dwLast
= mmt
->dwFirst
+ toberead
;
293 /* compute # of pulses in this track */
294 mmt
->dwIndex
= mmt
->dwFirst
;
295 mmt
->dwEventPulse
= 0;
297 while (MIDI_mciReadNextEvent(wmm
, mmt
) == 0 &&
298 LOWORD(mmt
->dwEventData
) != 0x2FFF) {
299 mmt
->dwIndex
+= mmt
->wEventLength
;
301 mmt
->dwLength
= mmt
->dwEventPulse
;
303 TRACE("Track %u has %lu bytes and %lu pulses\n", mmt
->wTrackNr
, toberead
, mmt
->dwLength
);
305 /* reset track data */
306 mmt
->wStatus
= 1; /* ok, playing */
307 mmt
->dwIndex
= mmt
->dwFirst
;
308 mmt
->dwEventPulse
= 0;
310 if (mmioSeek(wmm
->hFile
, 0, SEEK_CUR
) != mmt
->dwLast
) {
311 WARN("Ouch, out of sync seek=%lu track=%lu\n",
312 mmioSeek(wmm
->hFile
, 0, SEEK_CUR
), mmt
->dwLast
);
313 /* position at end of this track, to be ready to read next track */
314 mmioSeek(wmm
->hFile
, mmt
->dwLast
, SEEK_SET
);
320 /**************************************************************************
321 * MIDI_mciReadMThd [internal]
323 static DWORD
MIDI_mciReadMThd(WINE_MCIMIDI
* wmm
, DWORD dwOffset
)
329 TRACE("(%p, %08lX);\n", wmm
, dwOffset
);
331 if (mmioSeek(wmm
->hFile
, dwOffset
, SEEK_SET
) != dwOffset
) {
332 WARN("Can't seek at %08lX begin of 'MThd' \n", dwOffset
);
333 return MCIERR_INVALID_FILE
;
335 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
,
336 (long) sizeof(FOURCC
)) != (long) sizeof(FOURCC
))
337 return MCIERR_INVALID_FILE
;
339 if (fourcc
!= mmioFOURCC('M', 'T', 'h', 'd')) {
340 WARN("Can't synchronize on 'MThd' !\n");
341 return MCIERR_INVALID_FILE
;
344 if (MIDI_mciReadLong(wmm
, &toberead
) != 0 || toberead
< 3 * sizeof(WORD
))
345 return MCIERR_INVALID_FILE
;
347 if (MIDI_mciReadWord(wmm
, &wmm
->wFormat
) != 0 ||
348 MIDI_mciReadWord(wmm
, &wmm
->nTracks
) != 0 ||
349 MIDI_mciReadWord(wmm
, &wmm
->nDivision
) != 0) {
350 return MCIERR_INVALID_FILE
;
353 TRACE("toberead=0x%08lX, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
354 toberead
, wmm
->wFormat
, wmm
->nTracks
, wmm
->nDivision
);
356 /* MS doc says that the MIDI MCI time format must be put by default to the format
357 * stored in the MIDI file...
359 if (wmm
->nDivision
> 0x8000) {
360 /* eric.pouech@lemel.fr 98/11
361 * In did not check this very code (pulses are expressed as SMPTE sub-frames).
362 * In about 40 MB of MIDI files I have, none was SMPTE based...
363 * I'm just wondering if this is widely used :-). So, if someone has one of
364 * these files, I'd like to know about.
366 FIXME("Handling SMPTE time in MIDI files has not been tested\n"
367 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
369 switch (HIBYTE(wmm
->nDivision
)) {
370 case 0xE8: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
; break; /* -24 */
371 case 0xE7: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
; break; /* -25 */
372 case 0xE3: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
373 case 0xE2: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
; break; /* -30 */
375 WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm
->nDivision
));
376 return MCIERR_INVALID_FILE
;
378 switch (LOBYTE(wmm
->nDivision
)) {
379 case 4: /* MIDI Time Code */
382 case 80: /* SMPTE bit resolution */
385 WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm
->nDivision
));
386 return MCIERR_INVALID_FILE
;
388 } else if (wmm
->nDivision
== 0) {
389 WARN("Number of division is 0, can't support that !!\n");
390 return MCIERR_INVALID_FILE
;
392 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
395 switch (wmm
->wFormat
) {
397 if (wmm
->nTracks
!= 1) {
398 WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
406 WARN("Handling MIDI files which format = %d is not (yet) supported\n"
407 "Please report with MIDI file !\n", wmm
->wFormat
);
408 return MCIERR_INVALID_FILE
;
411 if (wmm
->nTracks
& 0x8000) {
412 /* this shouldn't be a problem... */
413 WARN("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
414 wmm
->nTracks
= 0x7FFF;
417 if ((wmm
->tracks
= HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK
) * wmm
->nTracks
)) == NULL
) {
418 return MCIERR_OUT_OF_MEMORY
;
421 toberead
-= 3 * sizeof(WORD
);
423 TRACE("Size of MThd > 6, skipping %ld extra bytes\n", toberead
);
424 mmioSeek(wmm
->hFile
, toberead
, SEEK_CUR
);
427 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
428 wmm
->tracks
[nt
].wTrackNr
= nt
;
429 if (MIDI_mciReadMTrk(wmm
, &wmm
->tracks
[nt
]) != 0) {
430 WARN("Can't read 'MTrk' header \n");
431 return MCIERR_INVALID_FILE
;
435 wmm
->dwTempo
= 500000;
440 /**************************************************************************
441 * MIDI_ConvertPulseToMS [internal]
443 static DWORD
MIDI_ConvertPulseToMS(WINE_MCIMIDI
* wmm
, DWORD pulse
)
447 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
448 * may change during file playing
450 if (wmm
->nDivision
== 0) {
451 FIXME("Shouldn't happen. wmm->nDivision = 0\n");
452 } else if (wmm
->nDivision
> 0x8000) { /* SMPTE, unchecked FIXME? */
453 int nf
= -(char)HIBYTE(wmm
->nDivision
); /* number of frames */
454 int nsf
= LOBYTE(wmm
->nDivision
); /* number of sub-frames */
455 ret
= (pulse
* 1000) / (nf
* nsf
);
457 ret
= (DWORD
)((double)pulse
* ((double)wmm
->dwTempo
/ 1000) /
458 (double)wmm
->nDivision
);
462 TRACE("pulse=%lu tempo=%lu division=%u=0x%04x => ms=%lu\n",
463 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
469 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
470 #define TIME_MS_IN_ONE_MINUTE (60*1000)
471 #define TIME_MS_IN_ONE_SECOND (1000)
473 /**************************************************************************
474 * MIDI_ConvertTimeFormatToMS [internal]
476 static DWORD
MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI
* wmm
, DWORD val
)
480 switch (wmm
->dwMciTimeFormat
) {
481 case MCI_FORMAT_MILLISECONDS
:
484 case MCI_FORMAT_SMPTE_24
:
486 (HIBYTE(HIWORD(val
)) * 125) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
487 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
489 case MCI_FORMAT_SMPTE_25
:
491 HIBYTE(HIWORD(val
)) * 40 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
492 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
494 case MCI_FORMAT_SMPTE_30
:
496 (HIBYTE(HIWORD(val
)) * 100) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
497 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
500 WARN("Bad time format %lu!\n", wmm
->dwMciTimeFormat
);
503 TRACE("val=%lu=0x%08lx [tf=%lu] => ret=%lu\n", val, val, wmm->dwMciTimeFormat, ret);
508 /**************************************************************************
509 * MIDI_ConvertMSToTimeFormat [internal]
511 static DWORD
MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI
* wmm
, DWORD _val
)
513 DWORD ret
= 0, val
= _val
;
516 switch (wmm
->dwMciTimeFormat
) {
517 case MCI_FORMAT_MILLISECONDS
:
520 case MCI_FORMAT_SMPTE_24
:
521 case MCI_FORMAT_SMPTE_25
:
522 case MCI_FORMAT_SMPTE_30
:
523 h
= val
/ TIME_MS_IN_ONE_HOUR
;
524 m
= (val
-= h
* TIME_MS_IN_ONE_HOUR
) / TIME_MS_IN_ONE_MINUTE
;
525 s
= (val
-= m
* TIME_MS_IN_ONE_MINUTE
) / TIME_MS_IN_ONE_SECOND
;
526 switch (wmm
->dwMciTimeFormat
) {
527 case MCI_FORMAT_SMPTE_24
:
528 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
529 f
= (val
* 3) / 125; val
-= (f
* 125) / 3;
531 case MCI_FORMAT_SMPTE_25
:
532 /* one frame is 1000/25 ms long, 1000/25 == 40 */
533 f
= val
/ 40; val
-= f
* 40;
535 case MCI_FORMAT_SMPTE_30
:
536 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
537 f
= (val
* 3) / 100; val
-= (f
* 100) / 3;
540 FIXME("There must be some bad bad programmer\n");
543 /* val contains the number of ms which cannot make a complete frame */
544 /* FIXME: is this correct ? programs seem to be happy with that */
545 ret
= (f
<< 24) | (s
<< 16) | (m
<< 8) | (h
<< 0);
548 WARN("Bad time format %lu!\n", wmm
->dwMciTimeFormat
);
551 TRACE("val=%lu [tf=%lu] => ret=%lu=0x%08lx\n", _val, wmm->dwMciTimeFormat, ret, ret);
556 /**************************************************************************
557 * MIDI_GetMThdLengthMS [internal]
559 static DWORD
MIDI_GetMThdLengthMS(WINE_MCIMIDI
* wmm
)
564 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
565 if (wmm
->wFormat
== 2) {
566 ret
+= wmm
->tracks
[nt
].dwLength
;
567 } else if (wmm
->tracks
[nt
].dwLength
> ret
) {
568 ret
= wmm
->tracks
[nt
].dwLength
;
571 /* FIXME: this is wrong if there is a tempo change inside the file */
572 return MIDI_ConvertPulseToMS(wmm
, ret
);
575 /**************************************************************************
576 * MIDI_mciOpen [internal]
578 static DWORD
MIDI_mciOpen(UINT16 wDevID
, DWORD dwFlags
, LPMCI_OPEN_PARMSA lpParms
)
582 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(wDevID
);
584 TRACE("(%04x, %08lX, %p)\n", wDevID
, dwFlags
, lpParms
);
586 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
587 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
588 if (dwFlags
& MCI_OPEN_SHAREABLE
)
589 return MCIERR_HARDWARE
;
591 if (wmm
->nUseCount
> 0) {
592 /* The driver is already opened on this channel
593 * MIDI sequencer cannot be shared
595 return MCIERR_DEVICE_OPEN
;
601 dwDeviceID
= lpParms
->wDeviceID
;
603 TRACE("wDevID=%04X (lpParams->wDeviceID=%08lX)\n", wDevID
, dwDeviceID
);
604 /* lpParms->wDeviceID = wDevID;*/
606 if (dwFlags
& MCI_OPEN_ELEMENT
) {
607 LPSTR lpstrElementName
;
609 lpstrElementName
= lpParms
->lpstrElementName
;
611 TRACE("MCI_OPEN_ELEMENT '%s' !\n", lpstrElementName
);
612 if (lpstrElementName
&& strlen(lpstrElementName
) > 0) {
613 wmm
->hFile
= mmioOpenA(lpstrElementName
, NULL
,
614 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
615 if (wmm
->hFile
== 0) {
616 WARN("Can't find file '%s' !\n", lpstrElementName
);
618 return MCIERR_FILE_NOT_FOUND
;
624 TRACE("hFile=%u\n", wmm
->hFile
);
626 /* FIXME: should I get a strdup() of it instead? */
627 wmm
->lpstrElementName
= lpParms
->lpstrElementName
;
629 wmm
->wNotifyDeviceID
= dwDeviceID
;
630 wmm
->dwStatus
= MCI_MODE_NOT_READY
; /* while loading file contents */
631 /* spec says it should be the default format from the MIDI file... */
632 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
634 if (wmm
->hFile
!= 0) {
639 if (mmioDescend(wmm
->hFile
, &ckMainRIFF
, NULL
, 0) != 0) {
640 dwRet
= MCIERR_INVALID_FILE
;
642 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08lX \n",
643 (LPSTR
)&ckMainRIFF
.ckid
, (LPSTR
)&ckMainRIFF
.fccType
, ckMainRIFF
.cksize
);
645 if (ckMainRIFF
.ckid
== FOURCC_RIFF
&& ckMainRIFF
.fccType
== mmioFOURCC('R', 'M', 'I', 'D')) {
646 mmckInfo
.ckid
= mmioFOURCC('d', 'a', 't', 'a');
647 mmioSeek(wmm
->hFile
, ckMainRIFF
.dwDataOffset
+ ((ckMainRIFF
.cksize
+ 1) & ~1), SEEK_SET
);
648 if (mmioDescend(wmm
->hFile
, &mmckInfo
, &ckMainRIFF
, MMIO_FINDCHUNK
) == 0) {
649 TRACE("... is a 'RMID' file \n");
650 dwOffset
= mmckInfo
.dwDataOffset
;
652 dwRet
= MCIERR_INVALID_FILE
;
655 if (dwRet
== 0 && MIDI_mciReadMThd(wmm
, dwOffset
) != 0) {
656 WARN("Can't read 'MThd' header \n");
657 dwRet
= MCIERR_INVALID_FILE
;
661 TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
669 mmioClose(wmm
->hFile
, 0);
672 wmm
->dwPositionMS
= 0;
673 wmm
->dwStatus
= MCI_MODE_STOP
;
674 wmm
->hMidiHdr
= USER_HEAP_ALLOC(sizeof(MIDIHDR16
));
679 /**************************************************************************
680 * MIDI_mciStop [internal]
682 static DWORD
MIDI_mciStop(UINT16 wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
684 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
686 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
688 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
690 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
691 wmm
->dwStatus
= MCI_MODE_STOP
;
692 modMessage(wmm
->wMidiID
, MODM_CLOSE
, 0, 0L, 0L);
694 TRACE("wmm->dwStatus=%d\n", wmm
->dwStatus
);
696 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
697 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
698 mciDriverNotify16((HWND16
)LOWORD(lpParms
->dwCallback
),
699 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
704 /**************************************************************************
705 * MIDI_mciClose [internal]
707 static DWORD
MIDI_mciClose(UINT16 wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
709 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
711 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
713 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
715 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
716 MIDI_mciStop(wDevID
, MCI_WAIT
, lpParms
);
719 wmm
->dwStatus
= MCI_MODE_STOP
;
721 if (wmm
->nUseCount
== 0) {
722 if (wmm
->hFile
!= 0) {
723 mmioClose(wmm
->hFile
, 0);
725 TRACE("hFile closed !\n");
727 USER_HEAP_FREE(wmm
->hMidiHdr
);
728 HeapFree(GetProcessHeap(), 0, wmm
->tracks
);
730 TRACE("Shouldn't happen... nUseCount=%d\n", wmm
->nUseCount
);
731 return MCIERR_INTERNAL
;
734 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
735 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
736 mciDriverNotify16((HWND16
)LOWORD(lpParms
->dwCallback
),
737 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
742 /**************************************************************************
743 * MIDI_mciFindNextEvent [internal]
745 static MCI_MIDITRACK
* MIDI_mciFindNextEvent(WINE_MCIMIDI
* wmm
, LPDWORD hiPulse
)
750 *hiPulse
= 0xFFFFFFFFul
;
752 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
753 mmt
= &wmm
->tracks
[nt
];
755 if (mmt
->wStatus
== 0)
757 if (mmt
->dwEventPulse
< *hiPulse
) {
758 *hiPulse
= mmt
->dwEventPulse
;
762 return (cnt
== 0xFFFFu
) ? 0 /* no more event on all tracks */
766 /**************************************************************************
767 * MIDI_mciPlay [internal]
769 static DWORD
MIDI_mciPlay(UINT16 wDevID
, DWORD dwFlags
, LPMCI_PLAY_PARMS lpParms
)
771 DWORD dwStartMS
, dwEndMS
, dwRet
;
775 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
776 MIDIOPENDESC midiOpenDesc
;
778 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
780 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
782 if (wmm
->hFile
== 0) {
783 WARN("Can't play: no file '%s' !\n", wmm
->lpstrElementName
);
784 return MCIERR_FILE_NOT_FOUND
;
787 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
788 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
789 /* FIXME: parameters (start/end) in lpParams may not be used */
790 return MIDI_mciResume(wDevID
, dwFlags
, (LPMCI_GENERIC_PARMS
)lpParms
);
792 WARN("Can't play: device is not stopped !\n");
793 return MCIERR_INTERNAL
;
796 if (!(dwFlags
& MCI_WAIT
)) {
797 /** FIXME: I'm not 100% sure that wNotifyDeviceID is the right value in all cases ??? */
798 return MCI_SendCommandAsync(wmm
->wNotifyDeviceID
, MCI_PLAY
, dwFlags
, (DWORD
)lpParms
, sizeof(LPMCI_PLAY_PARMS
));
801 if (lpParms
&& (dwFlags
& MCI_FROM
)) {
802 dwStartMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwFrom
);
804 dwStartMS
= wmm
->dwPositionMS
;
807 if (lpParms
&& (dwFlags
& MCI_TO
)) {
808 dwEndMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
810 dwEndMS
= 0xFFFFFFFFul
;
813 TRACE("Playing from %lu to %lu\n", dwStartMS
, dwEndMS
);
816 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
817 mmt
= &wmm
->tracks
[nt
];
819 mmt
->wStatus
= 1; /* ok, playing */
820 mmt
->dwIndex
= mmt
->dwFirst
;
821 if (wmm
->wFormat
== 2 && nt
> 0) {
822 mmt
->dwEventPulse
= wmm
->tracks
[nt
- 1].dwLength
;
824 mmt
->dwEventPulse
= 0;
826 MIDI_mciReadNextEvent(wmm
, mmt
); /* FIXME == 0 */
830 wmm
->dwTempo
= 500000;
831 wmm
->dwStatus
= MCI_MODE_PLAY
;
832 wmm
->dwPositionMS
= 0;
833 wmm
->wStartedPlaying
= FALSE
;
835 midiOpenDesc
.hMidi
= 0;
836 dwRet
= modMessage(wmm
->wMidiID
, MODM_OPEN
, 0, (DWORD
)&midiOpenDesc
, CALLBACK_NULL
);
837 /* dwRet = midMessage(wmm->wMidiID, MIDM_OPEN, 0, (DWORD)&midiOpenDesc, CALLBACK_NULL);*/
839 while (wmm
->dwStatus
!= MCI_MODE_STOP
) {
840 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
841 * too much. Tell gcc not to optimize status value using volatile.
843 while (((volatile WINE_MCIMIDI
*)wmm
)->dwStatus
== MCI_MODE_PAUSE
);
845 doPlay
= (wmm
->dwPositionMS
>= dwStartMS
&& wmm
->dwPositionMS
<= dwEndMS
);
847 TRACE("wmm->dwStatus=%d, doPlay=%s\n", wmm
->dwStatus
, doPlay
? "T" : "F");
849 if ((mmt
= MIDI_mciFindNextEvent(wmm
, &hiPulse
)) == NULL
)
850 break; /* no more event on tracks */
852 /* if starting playing, then set StartTicks to the value it would have had
853 * if play had started at position 0
855 if (doPlay
&& !wmm
->wStartedPlaying
) {
856 wmm
->dwStartTicks
= GetTickCount() - MIDI_ConvertPulseToMS(wmm
, wmm
->dwPulse
);
857 wmm
->wStartedPlaying
= TRUE
;
858 TRACE("Setting dwStartTicks to %lu\n", wmm
->dwStartTicks
);
861 if (hiPulse
> wmm
->dwPulse
) {
862 wmm
->dwPositionMS
+= MIDI_ConvertPulseToMS(wmm
, hiPulse
- wmm
->dwPulse
);
864 DWORD togo
= wmm
->dwStartTicks
+ wmm
->dwPositionMS
;
865 DWORD tc
= GetTickCount();
867 TRACE("Pulses hi=0x%08lx <> cur=0x%08lx\n", hiPulse
, wmm
->dwPulse
);
868 TRACE("Wait until %lu => %lu ms\n",
869 tc
- wmm
->dwStartTicks
, togo
- wmm
->dwStartTicks
);
873 wmm
->dwPulse
= hiPulse
;
876 switch (LOBYTE(LOWORD(mmt
->dwEventData
))) {
878 case 0xF7: /* sysex events */
880 FIXME("Not handling SysEx events (yet)\n");
884 /* position after meta data header */
885 mmioSeek(wmm
->hFile
, mmt
->dwIndex
+ HIWORD(mmt
->dwEventData
), SEEK_SET
);
886 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
887 case 0x00: /* 16-bit sequence number */
888 if (TRACE_ON(mcimidi
)) {
891 MIDI_mciReadWord(wmm
, &twd
); /* == 0 */
892 TRACE("Got sequence number %u\n", twd
);
895 case 0x01: /* any text */
896 case 0x02: /* Copyright Message text */
897 case 0x03: /* Sequence/Track Name text */
898 case 0x04: /* Instrument Name text */
899 case 0x05: /* Lyric text */
900 case 0x06: /* Marker text */
901 case 0x07: /* Cue-point text */
902 if (TRACE_ON(mcimidi
)) {
904 WORD len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
905 static char* info
[8] = {"", "Text", "Copyright", "Seq/Trk name",
906 "Instrument", "Lyric", "Marker", "Cue-point"};
907 WORD idx
= HIBYTE(LOWORD(mmt
->dwEventData
));
909 if (len
>= sizeof(buf
)) {
910 WARN("Buffer for text is too small (%d bytes, when %u are needed)\n", sizeof(buf
) - 1, len
);
911 len
= sizeof(buf
) - 1;
913 if (mmioRead(wmm
->hFile
, (HPSTR
)buf
, len
) == len
) {
914 buf
[len
] = 0; /* end string in case */
915 TRACE("%s => \"%s\"\n", (idx
< 8 ) ? info
[idx
] : "", buf
);
917 WARN("Couldn't read data for %s\n", (idx
< 8 ) ? info
[idx
] : "");
922 /* MIDI channel (cc) */
923 if (FIXME_ON(mcimidi
)) {
926 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
927 FIXME("NIY: MIDI channel=%u, track=%u\n", bt
, mmt
->wTrackNr
);
932 if (FIXME_ON(mcimidi
)) {
935 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
936 FIXME("NIY: MIDI port=%u, track=%u\n", bt
, mmt
->wTrackNr
);
939 case 0x2F: /* end of track */
942 case 0x51:/* set tempo */
943 /* Tempo is expressed in µ-seconds per midi quarter note
944 * for format 1 MIDI files, this can only be present on track #0
946 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
947 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt
->wTrackNr
);
952 MIDI_mciReadByte(wmm
, &tbt
); value
= ((DWORD
)tbt
) << 16;
953 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 8;
954 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 0;
955 TRACE("Setting tempo to %ld (BPM=%ld)\n", wmm
->dwTempo
, (value
) ? (60000000l / value
) : 0);
956 wmm
->dwTempo
= value
;
959 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
960 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
961 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt
->wTrackNr
);
962 } if (mmt
->dwEventPulse
!= 0) {
963 WARN("SMPTE track start can only be expressed at start of track (%lu)\n", mmt
->dwEventPulse
);
967 MIDI_mciReadByte(wmm
, &h
);
968 MIDI_mciReadByte(wmm
, &m
);
969 MIDI_mciReadByte(wmm
, &s
);
970 MIDI_mciReadByte(wmm
, &f
);
971 MIDI_mciReadByte(wmm
, &ff
);
972 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h
, m
, s
, f
, ff
);
975 case 0x58: /* file rythm */
976 if (TRACE_ON(mcimidi
)) {
977 BYTE num
, den
, cpmc
, _32npqn
;
979 MIDI_mciReadByte(wmm
, &num
);
980 MIDI_mciReadByte(wmm
, &den
); /* to notate e.g. 6/8 */
981 MIDI_mciReadByte(wmm
, &cpmc
); /* number of MIDI clocks per metronome click */
982 MIDI_mciReadByte(wmm
, &_32npqn
); /* number of notated 32nd notes per MIDI quarter note */
984 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num
, 1 << den
, cpmc
, _32npqn
);
987 case 0x59: /* key signature */
988 if (TRACE_ON(mcimidi
)) {
991 MIDI_mciReadByte(wmm
, &sf
);
992 MIDI_mciReadByte(wmm
, &mm
);
994 if (sf
>= 0x80) TRACE("%d flats\n", -(char)sf
);
995 else if (sf
> 0) TRACE("%d sharps\n", (char)sf
);
996 else TRACE("Key of C\n");
997 TRACE("Mode: %s\n", (mm
= 0) ? "major" : "minor");
1001 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt
->dwEventData
)));
1007 dwRet
= modMessage(wmm
->wMidiID
, MODM_DATA
, 0, mmt
->dwEventData
, 0);
1009 switch (LOBYTE(LOWORD(mmt
->dwEventData
)) & 0xF0) {
1015 dwRet
= modMessage(wmm
->wMidiID
, MODM_DATA
, 0, mmt
->dwEventData
, 0);
1019 mmt
->dwIndex
+= mmt
->wEventLength
;
1020 if (mmt
->dwIndex
< mmt
->dwFirst
|| mmt
->dwIndex
>= mmt
->dwLast
) {
1024 MIDI_mciReadNextEvent(wmm
, mmt
);
1028 /* stop all notes */
1029 /* should be (0x7800 | MIDI_CTL_CHANGE) instead of 0x78B0,
1030 * but MIDI_CTL_CHANGE is defined in OSS's soundcard.h and MCI must compile
1031 * without any reference to OSS
1033 /* FIXME: check if 0x78B0 is channel dependant or not. I coded it so that
1034 * it's channel dependent...
1038 for (chn
= 0; chn
< 16; chn
++)
1039 modMessage(wmm
->wMidiID
, MODM_DATA
, 0, 0x78B0 | chn
, 0);
1042 dwRet
= modMessage(wmm
->wMidiID
, MODM_CLOSE
, 0, 0L, 0L);
1043 wmm
->dwStatus
= MCI_MODE_STOP
;
1045 /* to restart playing at beginning when it's over */
1046 wmm
->dwPositionMS
= 0;
1048 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
1049 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1050 mciDriverNotify16((HWND16
)LOWORD(lpParms
->dwCallback
),
1051 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1056 /**************************************************************************
1057 * MIDI_mciRecord [internal]
1059 static DWORD
MIDI_mciRecord(UINT16 wDevID
, DWORD dwFlags
, LPMCI_RECORD_PARMS lpParms
)
1062 LPMIDIHDR16 lpMidiHdr
;
1064 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1066 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1068 if (wmm
== 0) return MCIERR_INVALID_DEVICE_ID
;
1070 if (wmm
->hFile
== 0) {
1071 WARN("Can't find file='%s' !\n", wmm
->lpstrElementName
);
1072 return MCIERR_FILE_NOT_FOUND
;
1074 start
= 1; end
= 99999;
1075 if (lpParms
&& (dwFlags
& MCI_FROM
)) {
1076 start
= lpParms
->dwFrom
;
1077 TRACE("MCI_FROM=%d \n", start
);
1079 if (lpParms
&& (dwFlags
& MCI_TO
)) {
1080 end
= lpParms
->dwTo
;
1081 TRACE("MCI_TO=%d \n", end
);
1083 lpMidiHdr
= USER_HEAP_LIN_ADDR(wmm
->hMidiHdr
);
1084 lpMidiHdr
->lpData
= (LPSTR
) HeapAlloc(GetProcessHeap(), 0, 1200);
1086 return MCIERR_OUT_OF_MEMORY
;
1087 lpMidiHdr
->dwBufferLength
= 1024;
1088 lpMidiHdr
->dwUser
= 0L;
1089 lpMidiHdr
->dwFlags
= 0L;
1090 dwRet
= midMessage(wmm
->wMidiID
, MIDM_PREPARE
, 0, (DWORD
)lpMidiHdr
, sizeof(MIDIHDR16
));
1091 TRACE("After MIDM_PREPARE \n");
1092 wmm
->dwStatus
= MCI_MODE_RECORD
;
1093 while (wmm
->dwStatus
!= MCI_MODE_STOP
) {
1094 TRACE("wmm->dwStatus=%p %d\n",
1095 &wmm
->dwStatus
, wmm
->dwStatus
);
1096 lpMidiHdr
->dwBytesRecorded
= 0;
1097 dwRet
= midMessage(wmm
->wMidiID
, MIDM_START
, 0, 0L, 0L);
1098 TRACE("After MIDM_START lpMidiHdr=%p dwBytesRecorded=%lu\n",
1099 lpMidiHdr
, lpMidiHdr
->dwBytesRecorded
);
1100 if (lpMidiHdr
->dwBytesRecorded
== 0) break;
1102 TRACE("Before MIDM_UNPREPARE \n");
1103 dwRet
= midMessage(wmm
->wMidiID
, MIDM_UNPREPARE
, 0, (DWORD
)lpMidiHdr
, sizeof(MIDIHDR16
));
1104 TRACE("After MIDM_UNPREPARE \n");
1105 if (lpMidiHdr
->lpData
!= NULL
) {
1106 HeapFree(GetProcessHeap(), 0, lpMidiHdr
->lpData
);
1107 lpMidiHdr
->lpData
= NULL
;
1109 wmm
->dwStatus
= MCI_MODE_STOP
;
1110 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
1111 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1112 mciDriverNotify16((HWND16
)LOWORD(lpParms
->dwCallback
),
1113 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1118 /**************************************************************************
1119 * MIDI_mciPause [internal]
1121 static DWORD
MIDI_mciPause(UINT16 wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1123 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1125 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1127 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1129 if (wmm
->dwStatus
== MCI_MODE_PLAY
) {
1130 /* stop all notes */
1131 /* see note in MIDI_mciPlay */
1133 for (chn
= 0; chn
< 16; chn
++)
1134 modMessage(wmm
->wMidiID
, MODM_DATA
, 0, 0x78B0 | chn
, 0);
1135 wmm
->dwStatus
= MCI_MODE_PAUSE
;
1137 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
1138 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1139 mciDriverNotify16((HWND16
)LOWORD(lpParms
->dwCallback
),
1140 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1146 /**************************************************************************
1147 * MIDI_mciResume [internal]
1149 static DWORD
MIDI_mciResume(UINT16 wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1151 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1153 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1155 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1157 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
1158 wmm
->wStartedPlaying
= FALSE
;
1159 wmm
->dwStatus
= MCI_MODE_PLAY
;
1161 if (lpParms
&& (dwFlags
& MCI_NOTIFY
)) {
1162 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1163 mciDriverNotify16((HWND16
)LOWORD(lpParms
->dwCallback
),
1164 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1169 /**************************************************************************
1170 * MIDI_mciSet [internal]
1172 static DWORD
MIDI_mciSet(UINT16 wDevID
, DWORD dwFlags
, LPMCI_SET_PARMS lpParms
)
1174 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1176 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1178 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1179 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1181 if (dwFlags
& MCI_SET_TIME_FORMAT
) {
1182 switch (lpParms
->dwTimeFormat
) {
1183 case MCI_FORMAT_MILLISECONDS
:
1184 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1185 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
1187 case MCI_FORMAT_SMPTE_24
:
1188 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1189 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
;
1191 case MCI_FORMAT_SMPTE_25
:
1192 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1193 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
;
1195 case MCI_FORMAT_SMPTE_30
:
1196 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1197 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
;
1200 WARN("Bad time format %lu!\n", lpParms
->dwTimeFormat
);
1201 return MCIERR_BAD_TIME_FORMAT
;
1204 if (dwFlags
& MCI_SET_VIDEO
) {
1205 TRACE("No support for video !\n");
1206 return MCIERR_UNSUPPORTED_FUNCTION
;
1208 if (dwFlags
& MCI_SET_DOOR_OPEN
) {
1209 TRACE("No support for door open !\n");
1210 return MCIERR_UNSUPPORTED_FUNCTION
;
1212 if (dwFlags
& MCI_SET_DOOR_CLOSED
) {
1213 TRACE("No support for door close !\n");
1214 return MCIERR_UNSUPPORTED_FUNCTION
;
1216 if (dwFlags
& MCI_SET_AUDIO
) {
1217 if (dwFlags
& MCI_SET_ON
) {
1218 TRACE("MCI_SET_ON audio !\n");
1219 } else if (dwFlags
& MCI_SET_OFF
) {
1220 TRACE("MCI_SET_OFF audio !\n");
1222 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1223 return MCIERR_BAD_INTEGER
;
1226 if (lpParms
->dwAudio
& MCI_SET_AUDIO_ALL
)
1227 TRACE("MCI_SET_AUDIO_ALL !\n");
1228 if (lpParms
->dwAudio
& MCI_SET_AUDIO_LEFT
)
1229 TRACE("MCI_SET_AUDIO_LEFT !\n");
1230 if (lpParms
->dwAudio
& MCI_SET_AUDIO_RIGHT
)
1231 TRACE("MCI_SET_AUDIO_RIGHT !\n");
1234 if (dwFlags
& MCI_SEQ_SET_MASTER
)
1235 TRACE("MCI_SEQ_SET_MASTER !\n");
1236 if (dwFlags
& MCI_SEQ_SET_SLAVE
)
1237 TRACE("MCI_SEQ_SET_SLAVE !\n");
1238 if (dwFlags
& MCI_SEQ_SET_OFFSET
)
1239 TRACE("MCI_SEQ_SET_OFFSET !\n");
1240 if (dwFlags
& MCI_SEQ_SET_PORT
)
1241 TRACE("MCI_SEQ_SET_PORT !\n");
1242 if (dwFlags
& MCI_SEQ_SET_TEMPO
)
1243 TRACE("MCI_SEQ_SET_TEMPO !\n");
1247 /**************************************************************************
1248 * MIDI_mciStatus [internal]
1250 static DWORD
MIDI_mciStatus(UINT16 wDevID
, DWORD dwFlags
, LPMCI_STATUS_PARMS lpParms
)
1252 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1255 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1257 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1258 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1260 if (dwFlags
& MCI_STATUS_ITEM
) {
1261 switch (lpParms
->dwItem
) {
1262 case MCI_STATUS_CURRENT_TRACK
:
1263 /* FIXME in Format 2 */
1264 lpParms
->dwReturn
= 1;
1265 TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms
->dwReturn
);
1267 case MCI_STATUS_LENGTH
:
1268 if ((dwFlags
& MCI_TRACK
) && wmm
->wFormat
== 2) {
1269 if (lpParms
->dwTrack
>= wmm
->nTracks
)
1270 return MCIERR_BAD_INTEGER
;
1271 /* FIXME: this is wrong if there is a tempo change inside the file */
1272 lpParms
->dwReturn
= MIDI_ConvertPulseToMS(wmm
, wmm
->tracks
[lpParms
->dwTrack
].dwLength
);
1274 lpParms
->dwReturn
= MIDI_GetMThdLengthMS(wmm
);
1276 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
, lpParms
->dwReturn
);
1277 TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms
->dwReturn
);
1279 case MCI_STATUS_MODE
:
1280 TRACE("MCI_STATUS_MODE => %u\n", wmm
->dwStatus
);
1281 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwStatus
, wmm
->dwStatus
);
1282 ret
= MCI_RESOURCE_RETURNED
;
1284 case MCI_STATUS_MEDIA_PRESENT
:
1285 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1286 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1287 ret
= MCI_RESOURCE_RETURNED
;
1289 case MCI_STATUS_NUMBER_OF_TRACKS
:
1290 lpParms
->dwReturn
= (wmm
->wFormat
== 2) ? wmm
->nTracks
: 1;
1291 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms
->dwReturn
);
1293 case MCI_STATUS_POSITION
:
1294 /* FIXME: do I need to use MCI_TRACK ? */
1295 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
,
1296 (dwFlags
& MCI_STATUS_START
) ? 0 : wmm
->dwPositionMS
);
1297 TRACE("MCI_STATUS_POSITION %s => %lu\n",
1298 (dwFlags
& MCI_STATUS_START
) ? "start" : "current", lpParms
->dwReturn
);
1300 case MCI_STATUS_READY
:
1301 lpParms
->dwReturn
= (wmm
->dwStatus
== MCI_MODE_NOT_READY
) ?
1302 MAKEMCIRESOURCE(FALSE
, MCI_FALSE
) : MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1303 ret
= MCI_RESOURCE_RETURNED
;
1304 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms
->dwReturn
));
1306 case MCI_STATUS_TIME_FORMAT
:
1307 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwMciTimeFormat
, wmm
->dwMciTimeFormat
);
1308 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms
->dwReturn
));
1309 ret
= MCI_RESOURCE_RETURNED
;
1311 case MCI_SEQ_STATUS_DIVTYPE
:
1312 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1313 if (wmm
->nDivision
> 0x8000) {
1314 switch (wmm
->nDivision
) {
1315 case 0xE8: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_24
; break; /* -24 */
1316 case 0xE7: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_25
; break; /* -25 */
1317 case 0xE3: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
1318 case 0xE2: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30
; break; /* -30 */
1319 default: FIXME("There is a bad bad programmer\n");
1322 lpParms
->dwReturn
= MCI_SEQ_DIV_PPQN
;
1324 lpParms
->dwReturn
= MAKEMCIRESOURCE(lpParms
->dwReturn
,lpParms
->dwReturn
);
1325 ret
= MCI_RESOURCE_RETURNED
;
1327 case MCI_SEQ_STATUS_MASTER
:
1328 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1329 lpParms
->dwReturn
= 0;
1331 case MCI_SEQ_STATUS_SLAVE
:
1332 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1333 lpParms
->dwReturn
= 0;
1335 case MCI_SEQ_STATUS_OFFSET
:
1336 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1337 lpParms
->dwReturn
= 0;
1339 case MCI_SEQ_STATUS_PORT
:
1340 TRACE("MCI_SEQ_STATUS_PORT !\n");
1341 lpParms
->dwReturn
= 0;
1343 case MCI_SEQ_STATUS_TEMPO
:
1344 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1345 lpParms
->dwReturn
= wmm
->dwTempo
;
1348 FIXME("Unknowm command %08lX !\n", lpParms
->dwItem
);
1349 return MCIERR_UNRECOGNIZED_COMMAND
;
1352 WARN("No Status-Item!\n");
1353 return MCIERR_UNRECOGNIZED_COMMAND
;
1355 if (dwFlags
& MCI_NOTIFY
) {
1356 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1357 mciDriverNotify16((HWND16
)LOWORD(lpParms
->dwCallback
),
1358 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1363 /**************************************************************************
1364 * MIDI_mciGetDevCaps [internal]
1366 static DWORD
MIDI_mciGetDevCaps(UINT16 wDevID
, DWORD dwFlags
,
1367 LPMCI_GETDEVCAPS_PARMS lpParms
)
1369 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1372 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1374 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1375 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1377 if (dwFlags
& MCI_GETDEVCAPS_ITEM
) {
1378 switch (lpParms
->dwItem
) {
1379 case MCI_GETDEVCAPS_DEVICE_TYPE
:
1380 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1381 lpParms
->dwReturn
= MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER
, MCI_DEVTYPE_SEQUENCER
);
1382 ret
= MCI_RESOURCE_RETURNED
;
1384 case MCI_GETDEVCAPS_HAS_AUDIO
:
1385 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1386 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1387 ret
= MCI_RESOURCE_RETURNED
;
1389 case MCI_GETDEVCAPS_HAS_VIDEO
:
1390 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1391 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1392 ret
= MCI_RESOURCE_RETURNED
;
1394 case MCI_GETDEVCAPS_USES_FILES
:
1395 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1396 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1397 ret
= MCI_RESOURCE_RETURNED
;
1399 case MCI_GETDEVCAPS_COMPOUND_DEVICE
:
1400 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1401 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1402 ret
= MCI_RESOURCE_RETURNED
;
1404 case MCI_GETDEVCAPS_CAN_EJECT
:
1405 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1406 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1407 ret
= MCI_RESOURCE_RETURNED
;
1409 case MCI_GETDEVCAPS_CAN_PLAY
:
1410 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1411 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1412 ret
= MCI_RESOURCE_RETURNED
;
1414 case MCI_GETDEVCAPS_CAN_RECORD
:
1415 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1416 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1417 ret
= MCI_RESOURCE_RETURNED
;
1419 case MCI_GETDEVCAPS_CAN_SAVE
:
1420 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1421 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1422 ret
= MCI_RESOURCE_RETURNED
;
1425 FIXME("Unknown capability (%08lx) !\n", lpParms
->dwItem
);
1426 return MCIERR_UNRECOGNIZED_COMMAND
;
1429 WARN("No GetDevCaps-Item !\n");
1430 return MCIERR_UNRECOGNIZED_COMMAND
;
1435 /**************************************************************************
1436 * MIDI_mciInfo [internal]
1438 static DWORD
MIDI_mciInfo(UINT16 wDevID
, DWORD dwFlags
, LPMCI_INFO_PARMSA lpParms
)
1441 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1443 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1445 if (lpParms
== NULL
|| lpParms
->lpstrReturn
== NULL
)
1446 return MCIERR_NULL_PARAMETER_BLOCK
;
1447 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1449 TRACE("buf=%p, len=%lu\n", lpParms
->lpstrReturn
, lpParms
->dwRetSize
);
1452 case MCI_INFO_PRODUCT
:
1453 str
= "Wine's MIDI sequencer";
1456 str
= wmm
->lpstrElementName
;
1459 /* FIXME: the following manifest constants are not defined in <WINE>/include/mmsystem.h */
1460 case MCI_INFO_COPYRIGHT
:
1466 WARN("Don't know this info command (%lu)\n", dwFlags
);
1467 return MCIERR_UNRECOGNIZED_COMMAND
;
1469 return MCI_WriteString(lpParms
->lpstrReturn
, lpParms
->dwRetSize
, str
);
1472 /**************************************************************************
1473 * MIDI_mciSeek [internal]
1475 static DWORD
MIDI_mciSeek(UINT16 wDevID
, DWORD dwFlags
, LPMCI_SEEK_PARMS lpParms
)
1478 WINE_MCIMIDI
* wmm
= MIDI_mciGetOpenDev(wDevID
);
1480 TRACE("(%04X, %08lX, %p);\n", wDevID
, dwFlags
, lpParms
);
1482 if (lpParms
== NULL
) {
1483 ret
= MCIERR_NULL_PARAMETER_BLOCK
;
1484 } else if (wmm
== NULL
) {
1485 ret
= MCIERR_INVALID_DEVICE_ID
;
1487 MIDI_mciStop(wDevID
, MCI_WAIT
, 0);
1489 if (dwFlags
& MCI_SEEK_TO_START
) {
1490 wmm
->dwPositionMS
= 0;
1491 } else if (dwFlags
& MCI_SEEK_TO_END
) {
1492 wmm
->dwPositionMS
= 0xFFFFFFFF; /* fixme */
1493 } else if (dwFlags
& MCI_TO
) {
1494 wmm
->dwPositionMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
1496 WARN("dwFlag doesn't tell where to seek to...\n");
1497 return MCIERR_MISSING_PARAMETER
;
1500 TRACE("Seeking to position=%lu ms\n", wmm
->dwPositionMS
);
1502 if (dwFlags
& MCI_NOTIFY
) {
1503 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms
->dwCallback
);
1504 mciDriverNotify16((HWND16
)LOWORD(lpParms
->dwCallback
),
1505 wmm
->wNotifyDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
1512 /*======================================================================*
1513 * MIDI entry points *
1514 *======================================================================*/
1516 /**************************************************************************
1517 * MCIMIDI_DriverProc [sample driver]
1519 LONG CALLBACK
MCIMIDI_DriverProc(DWORD dwDevID
, HDRVR hDriv
, DWORD wMsg
,
1520 DWORD dwParam1
, DWORD dwParam2
)
1523 case DRV_LOAD
: return 1;
1524 case DRV_FREE
: return 1;
1525 case DRV_ENABLE
: return 1;
1526 case DRV_DISABLE
: return 1;
1527 case DRV_QUERYCONFIGURE
: return 1;
1528 case DRV_CONFIGURE
: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK
); return 1;
1529 case DRV_INSTALL
: return DRVCNF_RESTART
;
1530 case DRV_REMOVE
: return DRVCNF_RESTART
;
1531 #ifdef SNDCTL_MIDI_INFO
1532 case DRV_OPEN
: return MIDI_drvOpen((LPSTR
)dwParam1
, (LPMCI_OPEN_DRIVER_PARMSA
)dwParam2
);
1533 case DRV_CLOSE
: return MIDI_drvClose(dwDevID
);
1534 case MCI_OPEN_DRIVER
: return MIDI_mciOpen (dwDevID
, dwParam1
, (LPMCI_OPEN_PARMSA
) dwParam2
);
1535 case MCI_CLOSE_DRIVER
: return MIDI_mciClose (dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1536 case MCI_PLAY
: return MIDI_mciPlay (dwDevID
, dwParam1
, (LPMCI_PLAY_PARMS
) dwParam2
);
1537 case MCI_RECORD
: return MIDI_mciRecord (dwDevID
, dwParam1
, (LPMCI_RECORD_PARMS
) dwParam2
);
1538 case MCI_STOP
: return MIDI_mciStop (dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1539 case MCI_SET
: return MIDI_mciSet (dwDevID
, dwParam1
, (LPMCI_SET_PARMS
) dwParam2
);
1540 case MCI_PAUSE
: return MIDI_mciPause (dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1541 case MCI_RESUME
: return MIDI_mciResume (dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1542 case MCI_STATUS
: return MIDI_mciStatus (dwDevID
, dwParam1
, (LPMCI_STATUS_PARMS
) dwParam2
);
1543 case MCI_GETDEVCAPS
: return MIDI_mciGetDevCaps(dwDevID
, dwParam1
, (LPMCI_GETDEVCAPS_PARMS
)dwParam2
);
1544 case MCI_INFO
: return MIDI_mciInfo (dwDevID
, dwParam1
, (LPMCI_INFO_PARMSA
) dwParam2
);
1545 case MCI_SEEK
: return MIDI_mciSeek (dwDevID
, dwParam1
, (LPMCI_SEEK_PARMS
) dwParam2
);
1547 case DRV_OPEN
: return 1;
1548 case DRV_CLOSE
: return 1;
1549 case MCI_OPEN_DRIVER
:
1550 case MCI_CLOSE_DRIVER
:
1558 case MCI_GETDEVCAPS
:
1562 /* commands that should be supported */
1578 WARN("Unsupported command=%s\n", MCI_MessageToString(wMsg
));
1580 /* commands that should report an error */
1582 FIXME("Unsupported command=%s\n", MCI_MessageToString(wMsg
));
1586 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1589 TRACE("Sending msg=%s to default driver proc\n", MCI_MessageToString(wMsg
));
1590 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1592 return MCIERR_UNRECOGNIZED_COMMAND
;