2 * Sample MIDI Wine Driver for Linux
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1999 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * + implement it correctly
24 * + finish asynchronous commands
25 * + better implement non waiting command (without the MCI_WAIT flag).
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mcimidi
);
44 #define MIDI_NOTEOFF 0x80
45 #define MIDI_NOTEON 0x90
48 DWORD dwFirst
; /* offset in file of track */
49 DWORD dwLast
; /* number of bytes in file of track */
50 DWORD dwIndex
; /* current index in file (dwFirst <= dwIndex < dwLast) */
51 DWORD dwLength
; /* number of pulses in this track */
52 DWORD dwEventPulse
; /* current pulse # (event) pointed by dwIndex */
53 DWORD dwEventData
; /* current data (event) pointed by dwIndex */
54 WORD wEventLength
; /* current length (event) pointed by dwIndex */
55 WORD wStatus
: 1, /* 1 : playing, 0 : done */
57 wLastCommand
: 8; /* last MIDI command on track */
60 typedef struct tagWINE_MCIMIDI
{
61 UINT wDevID
; /* the MCI one */
63 int nUseCount
; /* Incremented for each shared open */
64 HANDLE hCallback
; /* Callback handle for pending notification */
65 HANDLE hThread
; /* Player thread */
66 HMMIO hFile
; /* mmio file handle open as Element */
67 LPWSTR lpstrElementName
; /* Name of file (if any) */
68 LPWSTR lpstrCopyright
;
70 WORD wPort
; /* the WINMM device unit */
71 WORD dwStatus
; /* one from MCI_MODE_xxxx */
72 DWORD dwMciTimeFormat
; /* One of the supported MCI_FORMAT_xxxx */
73 WORD wFormat
; /* Format of MIDI hFile (0, 1 or 2) */
74 WORD nTracks
; /* Number of tracks in hFile */
75 WORD nDivision
; /* Number of division in hFile PPQN or SMPTE */
77 DWORD dwTempo
; /* Tempo (# of 1/4 note per second */
78 MCI_MIDITRACK
* tracks
; /* Content of each track */
85 /*======================================================================*
86 * MCI MIDI implementation *
87 *======================================================================*/
89 static DWORD
mmr2mci(DWORD ret
)
92 case MMSYSERR_ALLOCATED
:
93 return MCIERR_SEQ_PORT_INUSE
;
95 return MCIERR_OUT_OF_MEMORY
;
96 case MMSYSERR_BADDEVICEID
: /* wine*.drv disabled */
97 return MCIERR_SEQ_PORT_NONEXISTENT
;
98 case MIDIERR_INVALIDSETUP
: /* from midimap.dll without snd-seq module */
99 return MCIERR_SEQ_PORT_MAPNODEVICE
;
105 static DWORD
MIDI_mciResume(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
);
107 /**************************************************************************
108 * MIDI_drvOpen [internal]
110 static DWORD
MIDI_drvOpen(LPCWSTR str
, LPMCI_OPEN_DRIVER_PARMSW modp
)
114 if (!modp
) return 0xFFFFFFFF;
116 wmm
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINE_MCIMIDI
));
121 wmm
->wDevID
= modp
->wDeviceID
;
122 mciSetDriverData(wmm
->wDevID
, (DWORD_PTR
)wmm
);
123 modp
->wCustomCommandTable
= MCI_NO_COMMAND_TABLE
;
124 modp
->wType
= MCI_DEVTYPE_SEQUENCER
;
125 return modp
->wDeviceID
;
128 /**************************************************************************
129 * MCIMIDI_drvClose [internal]
131 static DWORD
MIDI_drvClose(DWORD dwDevID
)
133 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(dwDevID
);
136 HeapFree(GetProcessHeap(), 0, wmm
);
137 mciSetDriverData(dwDevID
, 0);
140 return (dwDevID
== 0xFFFFFFFF) ? 1 : 0;
143 /**************************************************************************
144 * MIDI_mciGetOpenDev [internal]
146 static WINE_MCIMIDI
* MIDI_mciGetOpenDev(MCIDEVICEID wDevID
, UINT wMsg
)
148 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(wDevID
);
150 if (wmm
== NULL
|| ((wmm
->nUseCount
== 0) ^ (wMsg
== MCI_OPEN_DRIVER
))) {
151 WARN("Invalid wDevID=%u\n", wDevID
);
157 /**************************************************************************
158 * MIDI_mciNotify [internal]
160 * Notifications in MCI work like a 1-element queue.
161 * Each new notification request supersedes the previous one.
162 * This affects Play and Record; other commands are immediate.
164 static void MIDI_mciNotify(DWORD_PTR hWndCallBack
, WINE_MCIMIDI
* wmm
, UINT wStatus
)
166 /* We simply save one parameter by not passing the wDevID local
167 * to the command. They are the same (via mciGetDriverData).
169 MCIDEVICEID wDevID
= wmm
->wDevID
;
170 HANDLE old
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
171 if (old
) mciDriverNotify(old
, wDevID
, MCI_NOTIFY_SUPERSEDED
);
172 mciDriverNotify(HWND_32(LOWORD(hWndCallBack
)), wDevID
, wStatus
);
175 /**************************************************************************
176 * MIDI_mciReadByte [internal]
178 static DWORD
MIDI_mciReadByte(WINE_MCIMIDI
* wmm
, BYTE
*lpbyt
)
182 if (mmioRead(wmm
->hFile
, (HPSTR
)lpbyt
, sizeof(BYTE
)) != (long)sizeof(BYTE
)) {
183 WARN("Error reading wmm=%p\n", wmm
);
184 ret
= MCIERR_INVALID_FILE
;
190 /**************************************************************************
191 * MIDI_mciReadWord [internal]
193 static DWORD
MIDI_mciReadWord(WINE_MCIMIDI
* wmm
, LPWORD lpw
)
196 DWORD ret
= MCIERR_INVALID_FILE
;
198 if (MIDI_mciReadByte(wmm
, &hibyte
) == 0 &&
199 MIDI_mciReadByte(wmm
, &lobyte
) == 0) {
200 *lpw
= ((WORD
)hibyte
<< 8) + lobyte
;
206 /**************************************************************************
207 * MIDI_mciReadLong [internal]
209 static DWORD
MIDI_mciReadLong(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
212 DWORD ret
= MCIERR_INVALID_FILE
;
214 if (MIDI_mciReadWord(wmm
, &hiword
) == 0 &&
215 MIDI_mciReadWord(wmm
, &loword
) == 0) {
216 *lpdw
= MAKELONG(loword
, hiword
);
222 /**************************************************************************
223 * MIDI_mciReadVaryLen [internal]
225 static WORD
MIDI_mciReadVaryLen(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
232 if (MIDI_mciReadByte(wmm
, &byte
) != 0) {
235 value
= (value
<< 7) + (byte
& 0x7F);
237 } while (byte
& 0x80);
242 /**************************************************************************
243 * MIDI_mciReadNextEvent [internal]
245 static DWORD
MIDI_mciReadNextEvent(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
253 if (mmioSeek(wmm
->hFile
, mmt
->dwIndex
, SEEK_SET
) != mmt
->dwIndex
) {
254 WARN("Can't seek at %08X\n", mmt
->dwIndex
);
255 return MCIERR_INVALID_FILE
;
257 evtLength
= MIDI_mciReadVaryLen(wmm
, &evtPulse
) + 1; /* > 0 */
258 MIDI_mciReadByte(wmm
, &b1
);
262 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
266 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
268 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
269 if (evtLength
>= 0x10000u
) {
270 /* this limitation shouldn't be a problem */
271 WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
274 hw
= LOWORD(evtLength
);
279 if (b1
& 0x80) { /* use running status ? */
280 mmt
->wLastCommand
= b1
;
281 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
284 b1
= mmt
->wLastCommand
;
286 switch ((b1
>> 4) & 0x07) {
287 case 0: case 1: case 2: case 3: case 6:
288 MIDI_mciReadByte(wmm
, &b3
); evtLength
++;
294 WARN("Strange indeed b1=0x%02x\n", b1
);
298 if (mmt
->dwIndex
+ evtLength
> mmt
->dwLast
)
299 return MCIERR_INTERNAL
;
301 mmt
->dwEventPulse
+= evtPulse
;
302 mmt
->dwEventData
= (hw
<< 16) + (b2
<< 8) + b1
;
303 mmt
->wEventLength
= evtLength
;
306 TRACE("[%u] => pulse=%08x(%08x), data=%08x, length=%u\n",
307 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
308 mmt->dwEventData, mmt->wEventLength);
313 /**************************************************************************
314 * MIDI_mciReadMTrk [internal]
316 static DWORD
MIDI_mciReadMTrk(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
321 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
, (long)sizeof(FOURCC
)) !=
322 (long)sizeof(FOURCC
)) {
323 return MCIERR_INVALID_FILE
;
326 if (fourcc
!= mmioFOURCC('M', 'T', 'r', 'k')) {
327 WARN("Can't synchronize on 'MTrk' !\n");
328 return MCIERR_INVALID_FILE
;
331 if (MIDI_mciReadLong(wmm
, &toberead
) != 0) {
332 return MCIERR_INVALID_FILE
;
334 mmt
->dwFirst
= mmioSeek(wmm
->hFile
, 0, SEEK_CUR
); /* >= 0 */
335 mmt
->dwLast
= mmt
->dwFirst
+ toberead
;
337 /* compute # of pulses in this track */
338 mmt
->dwIndex
= mmt
->dwFirst
;
339 mmt
->dwEventPulse
= 0;
341 while (MIDI_mciReadNextEvent(wmm
, mmt
) == 0 && LOWORD(mmt
->dwEventData
) != 0x2FFF) {
345 mmt
->dwIndex
+= mmt
->wEventLength
;
347 switch (LOWORD(mmt
->dwEventData
)) {
350 len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
351 if (len
>= sizeof(buf
)) {
352 WARN("Buffer for text is too small (%u are needed)\n", len
);
353 len
= sizeof(buf
) - 1;
355 if (mmioRead(wmm
->hFile
, buf
, len
) == len
) {
356 buf
[len
] = 0; /* end string in case */
357 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
359 if (wmm
->lpstrCopyright
) {
360 WARN("Two copyright notices (%s|%s)\n", debugstr_w(wmm
->lpstrCopyright
), buf
);
361 HeapFree(GetProcessHeap(), 0, wmm
->lpstrCopyright
);
363 len
= MultiByteToWideChar( CP_ACP
, 0, buf
, -1, NULL
, 0 );
364 wmm
->lpstrCopyright
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
365 MultiByteToWideChar( CP_ACP
, 0, buf
, -1, wmm
->lpstrCopyright
, len
);
368 if (wmm
->lpstrName
) {
369 WARN("Two names (%s|%s)\n", debugstr_w(wmm
->lpstrName
), buf
);
370 HeapFree(GetProcessHeap(), 0, wmm
->lpstrName
);
371 } /* last name or name from last track wins */
372 len
= MultiByteToWideChar( CP_ACP
, 0, buf
, -1, NULL
, 0 );
373 wmm
->lpstrName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
374 MultiByteToWideChar( CP_ACP
, 0, buf
, -1, wmm
->lpstrName
, len
);
381 mmt
->dwLength
= mmt
->dwEventPulse
;
383 TRACE("Track %u has %u bytes and %u pulses\n", mmt
->wTrackNr
, toberead
, mmt
->dwLength
);
385 /* reset track data */
386 mmt
->wStatus
= 1; /* ok, playing */
387 mmt
->dwIndex
= mmt
->dwFirst
;
388 mmt
->dwEventPulse
= 0;
390 if (mmioSeek(wmm
->hFile
, 0, SEEK_CUR
) != mmt
->dwLast
) {
391 WARN("Ouch, out of sync seek=%u track=%u\n",
392 mmioSeek(wmm
->hFile
, 0, SEEK_CUR
), mmt
->dwLast
);
393 /* position at end of this track, to be ready to read next track */
394 mmioSeek(wmm
->hFile
, mmt
->dwLast
, SEEK_SET
);
400 /**************************************************************************
401 * MIDI_mciReadMThd [internal]
403 static DWORD
MIDI_mciReadMThd(WINE_MCIMIDI
* wmm
, DWORD dwOffset
)
409 TRACE("(%p, %08X);\n", wmm
, dwOffset
);
411 if (mmioSeek(wmm
->hFile
, dwOffset
, SEEK_SET
) != dwOffset
) {
412 WARN("Can't seek at %08X begin of 'MThd'\n", dwOffset
);
413 return MCIERR_INVALID_FILE
;
415 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
,
416 (long) sizeof(FOURCC
)) != (long) sizeof(FOURCC
))
417 return MCIERR_INVALID_FILE
;
419 if (fourcc
!= mmioFOURCC('M', 'T', 'h', 'd')) {
420 WARN("Can't synchronize on 'MThd' !\n");
421 return MCIERR_INVALID_FILE
;
424 if (MIDI_mciReadLong(wmm
, &toberead
) != 0 || toberead
< 3 * sizeof(WORD
))
425 return MCIERR_INVALID_FILE
;
427 if (MIDI_mciReadWord(wmm
, &wmm
->wFormat
) != 0 ||
428 MIDI_mciReadWord(wmm
, &wmm
->nTracks
) != 0 ||
429 MIDI_mciReadWord(wmm
, &wmm
->nDivision
) != 0) {
430 return MCIERR_INVALID_FILE
;
433 TRACE("toberead=0x%08X, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
434 toberead
, wmm
->wFormat
, wmm
->nTracks
, wmm
->nDivision
);
436 /* MS doc says that the MIDI MCI time format must be put by default to the format
437 * stored in the MIDI file...
439 if (wmm
->nDivision
> 0x8000) {
440 /* eric.pouech@lemel.fr 98/11
441 * I did not check this very code (pulses are expressed as SMPTE sub-frames).
442 * In about 40 MB of MIDI files I have, none was SMPTE based...
443 * I'm just wondering if this is widely used :-). So, if someone has one of
444 * these files, I'd like to know about it.
446 FIXME("Handling SMPTE time in MIDI files has not been tested\n"
447 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
449 switch (HIBYTE(wmm
->nDivision
)) {
450 case 0xE8: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
; break; /* -24 */
451 case 0xE7: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
; break; /* -25 */
452 case 0xE3: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
453 case 0xE2: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
; break; /* -30 */
455 WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm
->nDivision
));
456 return MCIERR_INVALID_FILE
;
458 switch (LOBYTE(wmm
->nDivision
)) {
459 case 4: /* MIDI Time Code */
462 case 80: /* SMPTE bit resolution */
465 WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm
->nDivision
));
466 return MCIERR_INVALID_FILE
;
468 } else if (wmm
->nDivision
== 0) {
469 WARN("Number of division is 0, can't support that !!\n");
470 return MCIERR_INVALID_FILE
;
472 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
475 switch (wmm
->wFormat
) {
477 if (wmm
->nTracks
!= 1) {
478 WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
486 WARN("Handling MIDI files which format = %d is not (yet) supported\n"
487 "Please report with MIDI file !\n", wmm
->wFormat
);
488 return MCIERR_INVALID_FILE
;
491 if (wmm
->nTracks
> 0x80) {
492 /* wTrackNr is 7 bits only */
493 FIXME("Truncating MIDI file with %u tracks\n", wmm
->nTracks
);
497 if ((wmm
->tracks
= HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK
) * wmm
->nTracks
)) == NULL
) {
498 return MCIERR_OUT_OF_MEMORY
;
501 toberead
-= 3 * sizeof(WORD
);
503 TRACE("Size of MThd > 6, skipping %d extra bytes\n", toberead
);
504 mmioSeek(wmm
->hFile
, toberead
, SEEK_CUR
);
507 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
508 wmm
->tracks
[nt
].wTrackNr
= nt
;
509 if (MIDI_mciReadMTrk(wmm
, &wmm
->tracks
[nt
]) != 0) {
510 WARN("Can't read 'MTrk' header\n");
511 return MCIERR_INVALID_FILE
;
515 wmm
->dwTempo
= 500000;
520 /**************************************************************************
521 * MIDI_ConvertPulseToMS [internal]
523 static DWORD
MIDI_ConvertPulseToMS(WINE_MCIMIDI
* wmm
, DWORD pulse
)
527 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
528 * may change during file playing
530 if (wmm
->nDivision
== 0) {
531 FIXME("Shouldn't happen. wmm->nDivision = 0\n");
532 } else if (wmm
->nDivision
> 0x8000) { /* SMPTE, unchecked FIXME? */
533 int nf
= -(char)HIBYTE(wmm
->nDivision
); /* number of frames */
534 int nsf
= LOBYTE(wmm
->nDivision
); /* number of sub-frames */
535 ret
= (pulse
* 1000) / (nf
* nsf
);
537 ret
= (DWORD
)((double)pulse
* ((double)wmm
->dwTempo
/ 1000) /
538 (double)wmm
->nDivision
);
542 TRACE("pulse=%u tempo=%u division=%u=0x%04x => ms=%u\n",
543 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
549 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
550 #define TIME_MS_IN_ONE_MINUTE (60*1000)
551 #define TIME_MS_IN_ONE_SECOND (1000)
553 /**************************************************************************
554 * MIDI_ConvertTimeFormatToMS [internal]
556 static DWORD
MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI
* wmm
, DWORD val
)
560 switch (wmm
->dwMciTimeFormat
) {
561 case MCI_FORMAT_MILLISECONDS
:
564 case MCI_FORMAT_SMPTE_24
:
566 (HIBYTE(HIWORD(val
)) * 125) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
567 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
569 case MCI_FORMAT_SMPTE_25
:
571 HIBYTE(HIWORD(val
)) * 40 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
572 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
574 case MCI_FORMAT_SMPTE_30
:
576 (HIBYTE(HIWORD(val
)) * 100) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
577 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
580 WARN("Bad time format %u!\n", wmm
->dwMciTimeFormat
);
583 TRACE("val=%u=0x%08x [tf=%u] => ret=%u\n", val, val, wmm->dwMciTimeFormat, ret);
588 /**************************************************************************
589 * MIDI_ConvertMSToTimeFormat [internal]
591 static DWORD
MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI
* wmm
, DWORD _val
)
593 DWORD ret
= 0, val
= _val
;
596 switch (wmm
->dwMciTimeFormat
) {
597 case MCI_FORMAT_MILLISECONDS
:
600 case MCI_FORMAT_SMPTE_24
:
601 case MCI_FORMAT_SMPTE_25
:
602 case MCI_FORMAT_SMPTE_30
:
603 h
= val
/ TIME_MS_IN_ONE_HOUR
;
604 m
= (val
-= h
* TIME_MS_IN_ONE_HOUR
) / TIME_MS_IN_ONE_MINUTE
;
605 s
= (val
-= m
* TIME_MS_IN_ONE_MINUTE
) / TIME_MS_IN_ONE_SECOND
;
606 switch (wmm
->dwMciTimeFormat
) {
607 case MCI_FORMAT_SMPTE_24
:
608 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
609 f
= (val
* 3) / 125; val
-= (f
* 125) / 3;
611 case MCI_FORMAT_SMPTE_25
:
612 /* one frame is 1000/25 ms long, 1000/25 == 40 */
613 f
= val
/ 40; val
-= f
* 40;
615 case MCI_FORMAT_SMPTE_30
:
616 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
617 f
= (val
* 3) / 100; val
-= (f
* 100) / 3;
620 FIXME("There must be some bad bad programmer\n");
623 /* val contains the number of ms which cannot make a complete frame */
624 /* FIXME: is this correct ? programs seem to be happy with that */
625 ret
= (f
<< 24) | (s
<< 16) | (m
<< 8) | (h
<< 0);
628 WARN("Bad time format %u!\n", wmm
->dwMciTimeFormat
);
631 TRACE("val=%u [tf=%u] => ret=%u=0x%08x\n", _val, wmm->dwMciTimeFormat, ret, ret);
636 /**************************************************************************
637 * MIDI_GetMThdLengthMS [internal]
639 static DWORD
MIDI_GetMThdLengthMS(WINE_MCIMIDI
* wmm
)
644 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
645 if (wmm
->wFormat
== 2) {
646 ret
+= wmm
->tracks
[nt
].dwLength
;
647 } else if (wmm
->tracks
[nt
].dwLength
> ret
) {
648 ret
= wmm
->tracks
[nt
].dwLength
;
651 /* FIXME: this is wrong if there is a tempo change inside the file */
652 return MIDI_ConvertPulseToMS(wmm
, ret
);
655 /**************************************************************************
656 * MIDI_mciOpen [internal]
658 static DWORD
MIDI_mciOpen(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_OPEN_PARMSW lpParms
)
662 TRACE("(%d, %08X, %p)\n", wmm
->wDevID
, dwFlags
, lpParms
);
664 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
665 if (dwFlags
& MCI_OPEN_SHAREABLE
)
666 return MCIERR_HARDWARE
;
668 if (wmm
->nUseCount
> 0) {
669 /* The driver is already opened on this channel
670 * MIDI sequencer cannot be shared
672 return MCIERR_DEVICE_OPEN
;
678 wmm
->wPort
= MIDI_MAPPER
;
679 wmm
->lpstrElementName
= NULL
;
681 TRACE("wDevID=%d (lpParams->wDeviceID=%d)\n", wmm
->wDevID
, lpParms
->wDeviceID
);
682 /* lpParms->wDeviceID = wDevID;*/
684 if (dwFlags
& MCI_OPEN_ELEMENT
) {
685 TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpParms
->lpstrElementName
));
686 if (lpParms
->lpstrElementName
&& strlenW(lpParms
->lpstrElementName
) > 0) {
687 wmm
->hFile
= mmioOpenW((LPWSTR
)lpParms
->lpstrElementName
, NULL
,
688 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
689 if (wmm
->hFile
== 0) {
690 WARN("Can't find file %s!\n", debugstr_w(lpParms
->lpstrElementName
));
692 return MCIERR_FILE_NOT_FOUND
;
694 wmm
->lpstrElementName
= HeapAlloc(GetProcessHeap(), 0,
695 (strlenW(lpParms
->lpstrElementName
) + 1) * sizeof(WCHAR
));
696 strcpyW(wmm
->lpstrElementName
, lpParms
->lpstrElementName
);
699 TRACE("hFile=%p\n", wmm
->hFile
);
701 wmm
->lpstrCopyright
= NULL
;
702 wmm
->lpstrName
= NULL
;
704 wmm
->dwStatus
= MCI_MODE_NOT_READY
; /* while loading file contents */
705 /* spec says it should be the default format from the MIDI file... */
706 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
708 if (wmm
->hFile
!= 0) {
713 if (mmioDescend(wmm
->hFile
, &ckMainRIFF
, NULL
, 0) != 0) {
714 dwRet
= MCIERR_INVALID_FILE
;
716 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
717 (LPSTR
)&ckMainRIFF
.ckid
, (LPSTR
)&ckMainRIFF
.fccType
, ckMainRIFF
.cksize
);
719 if (ckMainRIFF
.ckid
== FOURCC_RIFF
&& ckMainRIFF
.fccType
== mmioFOURCC('R', 'M', 'I', 'D')) {
720 mmckInfo
.ckid
= mmioFOURCC('d', 'a', 't', 'a');
721 mmioSeek(wmm
->hFile
, ckMainRIFF
.dwDataOffset
+ ((ckMainRIFF
.cksize
+ 1) & ~1), SEEK_SET
);
722 if (mmioDescend(wmm
->hFile
, &mmckInfo
, &ckMainRIFF
, MMIO_FINDCHUNK
) == 0) {
723 TRACE("... is a 'RMID' file\n");
724 dwOffset
= mmckInfo
.dwDataOffset
;
726 dwRet
= MCIERR_INVALID_FILE
;
729 if (dwRet
== 0 && MIDI_mciReadMThd(wmm
, dwOffset
) != 0) {
730 WARN("Can't read 'MThd' header\n");
731 dwRet
= MCIERR_INVALID_FILE
;
735 TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
743 mmioClose(wmm
->hFile
, 0);
745 HeapFree(GetProcessHeap(), 0, wmm
->tracks
);
746 HeapFree(GetProcessHeap(), 0, wmm
->lpstrElementName
);
747 HeapFree(GetProcessHeap(), 0, wmm
->lpstrCopyright
);
748 HeapFree(GetProcessHeap(), 0, wmm
->lpstrName
);
750 wmm
->dwPositionMS
= 0;
751 wmm
->dwStatus
= MCI_MODE_STOP
;
752 if (dwFlags
& MCI_NOTIFY
)
753 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
758 /**************************************************************************
759 * MIDI_mciStop [internal]
761 static DWORD
MIDI_mciStop(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
765 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
767 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
768 HANDLE old
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
769 if (old
) mciDriverNotify(old
, wmm
->wDevID
, MCI_NOTIFY_ABORTED
);
772 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
773 int oldstat
= wmm
->dwStatus
;
775 wmm
->dwStatus
= MCI_MODE_NOT_READY
;
776 if (oldstat
== MCI_MODE_PAUSE
)
777 dwRet
= midiOutReset((HMIDIOUT
)wmm
->hMidi
);
779 if ((dwFlags
& MCI_WAIT
) && wmm
->hThread
)
780 WaitForSingleObject(wmm
->hThread
, INFINITE
);
784 wmm
->dwStatus
= MCI_MODE_STOP
;
786 if ((dwFlags
& MCI_NOTIFY
) && lpParms
&& MMSYSERR_NOERROR
==dwRet
)
787 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
791 /**************************************************************************
792 * MIDI_mciClose [internal]
794 static DWORD
MIDI_mciClose(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
797 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
799 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
800 /* mciStop handles MCI_NOTIFY_ABORTED */
801 MIDI_mciStop(wmm
, MCI_WAIT
, lpParms
);
805 if (wmm
->nUseCount
== 0) {
806 if (wmm
->hFile
!= 0) {
807 mmioClose(wmm
->hFile
, 0);
809 TRACE("hFile closed !\n");
811 if (wmm
->hThread
) CloseHandle(wmm
->hThread
);
812 HeapFree(GetProcessHeap(), 0, wmm
->tracks
);
813 HeapFree(GetProcessHeap(), 0, wmm
->lpstrElementName
);
814 HeapFree(GetProcessHeap(), 0, wmm
->lpstrCopyright
);
815 HeapFree(GetProcessHeap(), 0, wmm
->lpstrName
);
817 TRACE("Shouldn't happen... nUseCount=%d\n", wmm
->nUseCount
);
818 return MCIERR_INTERNAL
;
821 if ((dwFlags
& MCI_NOTIFY
) && lpParms
) {
822 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
827 /**************************************************************************
828 * MIDI_mciFindNextEvent [internal]
830 static MCI_MIDITRACK
* MIDI_mciFindNextEvent(WINE_MCIMIDI
* wmm
, LPDWORD hiPulse
)
835 *hiPulse
= 0xFFFFFFFFul
;
837 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
838 mmt
= &wmm
->tracks
[nt
];
840 if (mmt
->wStatus
== 0)
842 if (mmt
->dwEventPulse
< *hiPulse
) {
843 *hiPulse
= mmt
->dwEventPulse
;
847 return (cnt
== 0xFFFFu
) ? 0 /* no more event on all tracks */
851 /**************************************************************************
852 * MIDI_player [internal]
854 static DWORD
MIDI_player(WINE_MCIMIDI
* wmm
, DWORD dwFlags
)
859 DWORD hiPulse
, dwStartMS
= wmm
->dwPositionMS
;
863 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
864 mmt
= &wmm
->tracks
[nt
];
866 mmt
->wStatus
= 1; /* ok, playing */
867 mmt
->dwIndex
= mmt
->dwFirst
;
868 if (wmm
->wFormat
== 2 && nt
> 0) {
869 mmt
->dwEventPulse
= wmm
->tracks
[nt
- 1].dwLength
;
871 mmt
->dwEventPulse
= 0;
873 MIDI_mciReadNextEvent(wmm
, mmt
); /* FIXME == 0 */
876 dwRet
= midiOutOpen((LPHMIDIOUT
)&wmm
->hMidi
, wmm
->wPort
, 0L, 0L, CALLBACK_NULL
);
877 if (dwRet
!= MMSYSERR_NOERROR
) {
878 return mmr2mci(dwRet
);
882 wmm
->dwTempo
= 500000;
883 wmm
->dwPositionMS
= 0;
884 wmm
->wStartedPlaying
= FALSE
;
886 while (wmm
->dwStatus
!= MCI_MODE_STOP
&& wmm
->dwStatus
!= MCI_MODE_NOT_READY
) {
887 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
888 * too much. Tell gcc not to optimize status value using volatile.
890 while (((volatile WINE_MCIMIDI
*)wmm
)->dwStatus
== MCI_MODE_PAUSE
);
892 doPlay
= (wmm
->dwPositionMS
>= dwStartMS
&& wmm
->dwPositionMS
<= wmm
->dwEndMS
);
894 TRACE("wmm->dwStatus=%d, doPlay=%c\n", wmm
->dwStatus
, doPlay
? 'T' : 'F');
896 if ((mmt
= MIDI_mciFindNextEvent(wmm
, &hiPulse
)) == NULL
)
897 break; /* no more event on tracks */
899 /* if starting playing, then set StartTicks to the value it would have had
900 * if play had started at position 0
902 if (doPlay
&& !wmm
->wStartedPlaying
) {
903 wmm
->dwStartTicks
= GetTickCount() - MIDI_ConvertPulseToMS(wmm
, wmm
->dwPulse
);
904 wmm
->wStartedPlaying
= TRUE
;
905 TRACE("Setting dwStartTicks to %u\n", wmm
->dwStartTicks
);
908 if (hiPulse
> wmm
->dwPulse
) {
909 wmm
->dwPositionMS
+= MIDI_ConvertPulseToMS(wmm
, hiPulse
- wmm
->dwPulse
);
911 DWORD togo
= wmm
->dwStartTicks
+ wmm
->dwPositionMS
;
912 DWORD tc
= GetTickCount();
914 TRACE("Pulses hi=0x%08x <> cur=0x%08x\n", hiPulse
, wmm
->dwPulse
);
915 TRACE("Wait until %u => %u ms\n",
916 tc
- wmm
->dwStartTicks
, togo
- wmm
->dwStartTicks
);
920 wmm
->dwPulse
= hiPulse
;
923 switch (LOBYTE(LOWORD(mmt
->dwEventData
))) {
925 case 0xF7: /* sysex events */
927 FIXME("Not handling SysEx events (yet)\n");
931 /* position after meta data header */
932 mmioSeek(wmm
->hFile
, mmt
->dwIndex
+ HIWORD(mmt
->dwEventData
), SEEK_SET
);
933 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
934 case 0x00: /* 16-bit sequence number */
935 if (TRACE_ON(mcimidi
)) {
938 MIDI_mciReadWord(wmm
, &twd
); /* == 0 */
939 TRACE("Got sequence number %u\n", twd
);
942 case 0x01: /* any text */
943 case 0x02: /* Copyright Message text */
944 case 0x03: /* Sequence/Track Name text */
945 case 0x04: /* Instrument Name text */
946 case 0x05: /* Lyric text */
947 case 0x06: /* Marker text */
948 case 0x07: /* Cue-point text */
949 if (TRACE_ON(mcimidi
)) {
951 WORD len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
952 static const char* const info
[8] = {"", "Text", "Copyright", "Seq/Trk name",
953 "Instrument", "Lyric", "Marker", "Cue-point"};
954 WORD idx
= HIBYTE(LOWORD(mmt
->dwEventData
));
956 if (len
>= sizeof(buf
)) {
957 WARN("Buffer for text is too small (%u are needed)\n", len
);
958 len
= sizeof(buf
) - 1;
960 if (mmioRead(wmm
->hFile
, buf
, len
) == len
) {
961 buf
[len
] = 0; /* end string in case */
962 TRACE("%s => \"%s\"\n", (idx
< 8 ) ? info
[idx
] : "", buf
);
964 WARN("Couldn't read data for %s\n", (idx
< 8) ? info
[idx
] : "");
969 /* MIDI channel (cc) */
970 if (FIXME_ON(mcimidi
)) {
973 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
974 FIXME("NIY: MIDI channel=%u, track=%u\n", bt
, mmt
->wTrackNr
);
979 if (FIXME_ON(mcimidi
)) {
982 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
983 FIXME("NIY: MIDI port=%u, track=%u\n", bt
, mmt
->wTrackNr
);
986 case 0x2F: /* end of track */
989 case 0x51:/* set tempo */
990 /* Tempo is expressed in -seconds per midi quarter note
991 * for format 1 MIDI files, this can only be present on track #0
993 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
994 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt
->wTrackNr
);
999 MIDI_mciReadByte(wmm
, &tbt
); value
= ((DWORD
)tbt
) << 16;
1000 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 8;
1001 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 0;
1002 TRACE("Setting tempo to %d (BPM=%d)\n", wmm
->dwTempo
, (value
) ? (60000000 / value
) : 0);
1003 wmm
->dwTempo
= value
;
1006 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
1007 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
1008 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt
->wTrackNr
);
1009 } if (mmt
->dwEventPulse
!= 0) {
1010 WARN("SMPTE track start can only be expressed at start of track (%u)\n", mmt
->dwEventPulse
);
1012 BYTE h
, m
, s
, f
, ff
;
1014 MIDI_mciReadByte(wmm
, &h
);
1015 MIDI_mciReadByte(wmm
, &m
);
1016 MIDI_mciReadByte(wmm
, &s
);
1017 MIDI_mciReadByte(wmm
, &f
);
1018 MIDI_mciReadByte(wmm
, &ff
);
1019 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h
, m
, s
, f
, ff
);
1022 case 0x58: /* file rhythm */
1023 if (TRACE_ON(mcimidi
)) {
1024 BYTE num
, den
, cpmc
, _32npqn
;
1026 MIDI_mciReadByte(wmm
, &num
);
1027 MIDI_mciReadByte(wmm
, &den
); /* to notate e.g. 6/8 */
1028 MIDI_mciReadByte(wmm
, &cpmc
); /* number of MIDI clocks per metronome click */
1029 MIDI_mciReadByte(wmm
, &_32npqn
); /* number of notated 32nd notes per MIDI quarter note */
1031 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num
, 1 << den
, cpmc
, _32npqn
);
1034 case 0x59: /* key signature */
1035 if (TRACE_ON(mcimidi
)) {
1038 MIDI_mciReadByte(wmm
, &sf
);
1039 MIDI_mciReadByte(wmm
, &mm
);
1041 if (sf
>= 0x80) TRACE("%d flats\n", -(char)sf
);
1042 else if (sf
> 0) TRACE("%d sharps\n", (char)sf
);
1043 else TRACE("Key of C\n");
1044 TRACE("Mode: %s\n", (mm
== 0) ? "major" : "minor");
1048 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt
->dwEventData
)));
1054 dwRet
= midiOutShortMsg((HMIDIOUT
)wmm
->hMidi
, mmt
->dwEventData
);
1056 switch (LOBYTE(LOWORD(mmt
->dwEventData
)) & 0xF0) {
1062 dwRet
= midiOutShortMsg((HMIDIOUT
)wmm
->hMidi
, mmt
->dwEventData
);
1066 mmt
->dwIndex
+= mmt
->wEventLength
;
1067 if (mmt
->dwIndex
< mmt
->dwFirst
|| mmt
->dwIndex
>= mmt
->dwLast
) {
1071 MIDI_mciReadNextEvent(wmm
, mmt
);
1075 midiOutReset((HMIDIOUT
)wmm
->hMidi
);
1077 dwRet
= midiOutClose((HMIDIOUT
)wmm
->hMidi
);
1079 if (dwFlags
& MCI_NOTIFY
)
1080 oldcb
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
1082 wmm
->dwStatus
= MCI_MODE_STOP
;
1084 /* Let the potentially asynchronous commands support FAILURE notification. */
1085 if (oldcb
) mciDriverNotify(oldcb
, wmm
->wDevID
,
1086 dwRet
? MCI_NOTIFY_FAILURE
: MCI_NOTIFY_SUCCESSFUL
);
1087 return mmr2mci(dwRet
);
1090 static DWORD CALLBACK
MIDI_Starter(void *ptr
)
1092 WINE_MCIMIDI
* wmm
= ptr
;
1093 return MIDI_player(wmm
, MCI_NOTIFY
);
1096 static DWORD
ensurePlayerThread(WINE_MCIMIDI
* wmm
)
1101 switch (wmm
->dwStatus
) {
1103 return MCIERR_NONAPPLICABLE_FUNCTION
;
1104 case MCI_MODE_PAUSE
:
1105 return MIDI_mciResume(wmm
, 0, NULL
);
1107 /* the player was not stopped, use it */
1112 wmm
->dwStatus
= MCI_MODE_PLAY
;
1114 WaitForSingleObject(wmm
->hThread
, INFINITE
);
1115 CloseHandle(wmm
->hThread
);
1118 wmm
->hThread
= CreateThread(NULL
, 0, MIDI_Starter
, wmm
, 0, NULL
);
1119 if (!wmm
->hThread
) {
1120 dwRet
= MCIERR_OUT_OF_MEMORY
;
1122 SetThreadPriority(wmm
->hThread
, THREAD_PRIORITY_TIME_CRITICAL
);
1126 wmm
->dwStatus
= MCI_MODE_STOP
;
1131 /**************************************************************************
1132 * MIDI_mciPlay [internal]
1134 static DWORD
MIDI_mciPlay(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_PLAY_PARMS lpParms
)
1136 DWORD dwStartMS
, dwEndMS
;
1140 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1142 if (wmm
->hFile
== 0) {
1143 WARN("Can't play: no file %s!\n", debugstr_w(wmm
->lpstrElementName
));
1144 return MCIERR_FILE_NOT_FOUND
;
1147 if (lpParms
&& (dwFlags
& MCI_TO
)) {
1148 dwEndMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
1149 /* FIXME: if (dwEndMS > length) return MCIERR_OUTOFRANGE; */
1151 dwEndMS
= 0xFFFFFFFFul
; /* FIXME: dwEndMS = length; */
1153 if (lpParms
&& (dwFlags
& MCI_FROM
)) {
1154 dwStartMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwFrom
);
1156 dwStartMS
= wmm
->dwPositionMS
;
1158 if (dwEndMS
< dwStartMS
)
1159 return MCIERR_OUTOFRANGE
;
1161 if (dwFlags
& MCI_FROM
) {
1162 /* Stop with MCI_NOTIFY_ABORTED and set new position. */
1163 MIDI_mciStop(wmm
, MCI_WAIT
, NULL
);
1164 wmm
->dwPositionMS
= dwStartMS
;
1165 } /* else use existing player. */
1166 if (wmm
->dwEndMS
!= dwEndMS
) {
1167 oldcb
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
1168 if (oldcb
) mciDriverNotify(oldcb
, wmm
->wDevID
, MCI_NOTIFY_ABORTED
);
1169 wmm
->dwEndMS
= dwEndMS
;
1172 TRACE("Playing from %u to %u\n", dwStartMS
, dwEndMS
);
1174 if ((dwFlags
& MCI_NOTIFY
) && lpParms
) {
1175 oldcb
= InterlockedExchangePointer(&wmm
->hCallback
, HWND_32(LOWORD(lpParms
->dwCallback
)));
1176 if (oldcb
) mciDriverNotify(oldcb
, wmm
->wDevID
, MCI_NOTIFY_SUPERSEDED
);
1179 dwRet
= ensurePlayerThread(wmm
);
1181 if (!dwRet
&& (dwFlags
& MCI_WAIT
)) {
1182 WaitForSingleObject(wmm
->hThread
, INFINITE
);
1183 GetExitCodeThread(wmm
->hThread
, &dwRet
);
1184 /* STATUS_PENDING cannot happen. It folds onto MCIERR_UNRECOGNIZED_KEYWORD */
1186 /* The player thread performs notification at exit. */
1190 /**************************************************************************
1191 * MIDI_mciPause [internal]
1193 static DWORD
MIDI_mciPause(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1195 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1197 if (wmm
->dwStatus
== MCI_MODE_PLAY
) {
1198 /* stop all notes */
1200 for (chn
= 0; chn
< 16; chn
++)
1201 midiOutShortMsg((HMIDIOUT
)(wmm
->hMidi
), 0x78B0 | chn
);
1202 wmm
->dwStatus
= MCI_MODE_PAUSE
;
1204 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1205 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1209 /**************************************************************************
1210 * MIDI_mciResume [internal]
1212 static DWORD
MIDI_mciResume(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1214 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1216 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
1217 wmm
->wStartedPlaying
= FALSE
;
1218 wmm
->dwStatus
= MCI_MODE_PLAY
;
1220 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1221 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1225 /**************************************************************************
1226 * MIDI_mciSet [internal]
1228 static DWORD
MIDI_mciSet(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_SEQ_SET_PARMS lpParms
)
1230 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1232 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1234 if (dwFlags
& MCI_SET_TIME_FORMAT
) {
1235 switch (lpParms
->dwTimeFormat
) {
1236 case MCI_FORMAT_MILLISECONDS
:
1237 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1238 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
1240 case MCI_FORMAT_SMPTE_24
:
1241 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1242 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
;
1244 case MCI_FORMAT_SMPTE_25
:
1245 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1246 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
;
1248 case MCI_FORMAT_SMPTE_30
:
1249 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1250 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
;
1253 WARN("Bad time format %u!\n", lpParms
->dwTimeFormat
);
1254 return MCIERR_BAD_TIME_FORMAT
;
1257 if (dwFlags
& MCI_SET_VIDEO
) {
1258 TRACE("No support for video !\n");
1259 return MCIERR_UNSUPPORTED_FUNCTION
;
1261 if (dwFlags
& MCI_SET_DOOR_OPEN
) {
1262 TRACE("No support for door open !\n");
1263 return MCIERR_UNSUPPORTED_FUNCTION
;
1265 if (dwFlags
& MCI_SET_DOOR_CLOSED
) {
1266 TRACE("No support for door close !\n");
1267 return MCIERR_UNSUPPORTED_FUNCTION
;
1269 if (dwFlags
& MCI_SET_AUDIO
) {
1270 if (dwFlags
& MCI_SET_ON
) {
1271 TRACE("MCI_SET_ON audio !\n");
1272 } else if (dwFlags
& MCI_SET_OFF
) {
1273 TRACE("MCI_SET_OFF audio !\n");
1275 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1276 return MCIERR_BAD_INTEGER
;
1279 switch (lpParms
->dwAudio
)
1281 case MCI_SET_AUDIO_ALL
: TRACE("MCI_SET_AUDIO_ALL !\n"); break;
1282 case MCI_SET_AUDIO_LEFT
: TRACE("MCI_SET_AUDIO_LEFT !\n"); break;
1283 case MCI_SET_AUDIO_RIGHT
: TRACE("MCI_SET_AUDIO_RIGHT !\n"); break;
1284 default: WARN("Unknown audio channel %u\n", lpParms
->dwAudio
); break;
1288 if (dwFlags
& MCI_SEQ_SET_MASTER
)
1289 TRACE("MCI_SEQ_SET_MASTER !\n");
1290 if (dwFlags
& MCI_SEQ_SET_SLAVE
)
1291 TRACE("MCI_SEQ_SET_SLAVE !\n");
1292 if (dwFlags
& MCI_SEQ_SET_OFFSET
)
1293 TRACE("MCI_SEQ_SET_OFFSET !\n");
1294 if (dwFlags
& MCI_SEQ_SET_PORT
) {
1295 TRACE("MCI_SEQ_SET_PORT = %d\n", lpParms
->dwPort
);
1296 if ((UINT16
)lpParms
->dwPort
!= (UINT16
)MIDI_MAPPER
&&
1297 (UINT16
)lpParms
->dwPort
>= midiOutGetNumDevs())
1298 /* FIXME: input/output port distinction? */
1299 return MCIERR_SEQ_PORT_NONEXISTENT
;
1300 /* FIXME: Native manages to swap the device while playing! */
1301 wmm
->wPort
= lpParms
->dwPort
;
1303 if (dwFlags
& MCI_SEQ_SET_TEMPO
)
1304 TRACE("MCI_SEQ_SET_TEMPO !\n");
1305 if (dwFlags
& MCI_NOTIFY
)
1306 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1310 /**************************************************************************
1311 * MIDI_mciStatus [internal]
1313 static DWORD
MIDI_mciStatus(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_STATUS_PARMS lpParms
)
1317 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1319 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1321 if (dwFlags
& MCI_STATUS_ITEM
) {
1322 switch (lpParms
->dwItem
) {
1323 case MCI_STATUS_CURRENT_TRACK
:
1324 /* FIXME in Format 2 */
1325 lpParms
->dwReturn
= 1;
1326 TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms
->dwReturn
);
1328 case MCI_STATUS_LENGTH
:
1329 if ((dwFlags
& MCI_TRACK
) && wmm
->wFormat
== 2) {
1330 if (lpParms
->dwTrack
>= wmm
->nTracks
)
1331 return MCIERR_OUTOFRANGE
;
1332 /* FIXME: this is wrong if there is a tempo change inside the file */
1333 lpParms
->dwReturn
= MIDI_ConvertPulseToMS(wmm
, wmm
->tracks
[lpParms
->dwTrack
].dwLength
);
1335 lpParms
->dwReturn
= MIDI_GetMThdLengthMS(wmm
);
1337 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
, lpParms
->dwReturn
);
1338 /* FIXME: ret = MCI_COLONIZED4_RETURN if SMPTE */
1339 TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms
->dwReturn
);
1341 case MCI_STATUS_MODE
:
1342 TRACE("MCI_STATUS_MODE => %u\n", wmm
->dwStatus
);
1343 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwStatus
, wmm
->dwStatus
);
1344 ret
= MCI_RESOURCE_RETURNED
;
1346 case MCI_STATUS_MEDIA_PRESENT
:
1347 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1348 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1349 ret
= MCI_RESOURCE_RETURNED
;
1351 case MCI_STATUS_NUMBER_OF_TRACKS
:
1352 lpParms
->dwReturn
= (wmm
->wFormat
== 2) ? wmm
->nTracks
: 1;
1353 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms
->dwReturn
);
1355 case MCI_STATUS_POSITION
:
1356 /* FIXME: check MCI_TRACK == 1 if set */
1357 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
,
1358 (dwFlags
& MCI_STATUS_START
) ? 0 : wmm
->dwPositionMS
);
1359 /* FIXME: ret = MCI_COLONIZED4_RETURN if SMPTE */
1360 TRACE("MCI_STATUS_POSITION %s => %lu\n",
1361 (dwFlags
& MCI_STATUS_START
) ? "start" : "current", lpParms
->dwReturn
);
1363 case MCI_STATUS_READY
:
1364 lpParms
->dwReturn
= (wmm
->dwStatus
== MCI_MODE_NOT_READY
) ?
1365 MAKEMCIRESOURCE(FALSE
, MCI_FALSE
) : MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1366 ret
= MCI_RESOURCE_RETURNED
;
1367 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms
->dwReturn
));
1369 case MCI_STATUS_TIME_FORMAT
:
1370 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwMciTimeFormat
, MCI_FORMAT_RETURN_BASE
+ wmm
->dwMciTimeFormat
);
1371 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms
->dwReturn
));
1372 ret
= MCI_RESOURCE_RETURNED
;
1374 case MCI_SEQ_STATUS_DIVTYPE
:
1375 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1376 if (wmm
->nDivision
> 0x8000) {
1377 switch (HIBYTE(wmm
->nDivision
)) {
1378 case 0xE8: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_24
; break; /* -24 */
1379 case 0xE7: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_25
; break; /* -25 */
1380 case 0xE3: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
1381 case 0xE2: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30
; break; /* -30 */
1382 default: FIXME("There is a bad bad programmer\n");
1385 lpParms
->dwReturn
= MCI_SEQ_DIV_PPQN
;
1387 lpParms
->dwReturn
= MAKEMCIRESOURCE(lpParms
->dwReturn
,lpParms
->dwReturn
);
1388 ret
= MCI_RESOURCE_RETURNED
;
1390 case MCI_SEQ_STATUS_MASTER
:
1391 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1392 lpParms
->dwReturn
= MAKEMCIRESOURCE(MCI_SEQ_NONE
, MCI_SEQ_NONE_S
);
1393 ret
= MCI_RESOURCE_RETURNED
;
1395 case MCI_SEQ_STATUS_SLAVE
:
1396 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1397 lpParms
->dwReturn
= MAKEMCIRESOURCE(MCI_SEQ_FILE
, MCI_SEQ_FILE_S
);
1398 ret
= MCI_RESOURCE_RETURNED
;
1400 case MCI_SEQ_STATUS_OFFSET
:
1401 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1402 lpParms
->dwReturn
= 0;
1404 case MCI_SEQ_STATUS_PORT
:
1405 if (wmm
->wPort
!= (UINT16
)MIDI_MAPPER
)
1406 lpParms
->dwReturn
= wmm
->wPort
;
1408 lpParms
->dwReturn
= MAKEMCIRESOURCE(MIDI_MAPPER
, MCI_SEQ_MAPPER_S
);
1409 ret
= MCI_RESOURCE_RETURNED
;
1411 TRACE("MCI_SEQ_STATUS_PORT (%u) => %d\n", wmm
->wDevID
, wmm
->wPort
);
1413 case MCI_SEQ_STATUS_TEMPO
:
1414 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1415 lpParms
->dwReturn
= wmm
->dwTempo
;
1418 FIXME("Unknown command %08X !\n", lpParms
->dwItem
);
1419 return MCIERR_UNSUPPORTED_FUNCTION
;
1422 return MCIERR_MISSING_PARAMETER
;
1424 if ((dwFlags
& MCI_NOTIFY
) && HRESULT_CODE(ret
)==0)
1425 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1429 /**************************************************************************
1430 * MIDI_mciGetDevCaps [internal]
1432 static DWORD
MIDI_mciGetDevCaps(WINE_MCIMIDI
* wmm
, DWORD dwFlags
,
1433 LPMCI_GETDEVCAPS_PARMS lpParms
)
1437 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1439 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1441 if (dwFlags
& MCI_GETDEVCAPS_ITEM
) {
1442 switch (lpParms
->dwItem
) {
1443 case MCI_GETDEVCAPS_DEVICE_TYPE
:
1444 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1445 lpParms
->dwReturn
= MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER
, MCI_DEVTYPE_SEQUENCER
);
1446 ret
= MCI_RESOURCE_RETURNED
;
1448 case MCI_GETDEVCAPS_HAS_AUDIO
:
1449 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1450 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1451 ret
= MCI_RESOURCE_RETURNED
;
1453 case MCI_GETDEVCAPS_HAS_VIDEO
:
1454 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1455 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1456 ret
= MCI_RESOURCE_RETURNED
;
1458 case MCI_GETDEVCAPS_USES_FILES
:
1459 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1460 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1461 ret
= MCI_RESOURCE_RETURNED
;
1463 case MCI_GETDEVCAPS_COMPOUND_DEVICE
:
1464 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1465 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1466 ret
= MCI_RESOURCE_RETURNED
;
1468 case MCI_GETDEVCAPS_CAN_EJECT
:
1469 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1470 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1471 ret
= MCI_RESOURCE_RETURNED
;
1473 case MCI_GETDEVCAPS_CAN_PLAY
:
1474 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1475 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1476 ret
= MCI_RESOURCE_RETURNED
;
1478 case MCI_GETDEVCAPS_CAN_RECORD
:
1479 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1480 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1481 ret
= MCI_RESOURCE_RETURNED
;
1483 case MCI_GETDEVCAPS_CAN_SAVE
:
1484 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1485 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1486 ret
= MCI_RESOURCE_RETURNED
;
1489 FIXME("Unknown capability (%08x) !\n", lpParms
->dwItem
);
1490 return MCIERR_UNRECOGNIZED_COMMAND
;
1493 WARN("No GetDevCaps-Item !\n");
1494 return MCIERR_UNRECOGNIZED_COMMAND
;
1496 if ((dwFlags
& MCI_NOTIFY
) && HRESULT_CODE(ret
)==0)
1497 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1501 /**************************************************************************
1502 * MIDI_mciInfo [internal]
1504 static DWORD
MIDI_mciInfo(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_INFO_PARMSW lpParms
)
1508 static const WCHAR wszMidiSeq
[] = {'W','i','n','e','\'','s',' ','M','I','D','I',' ','s','e','q','u','e','n','c','e','r',0};
1510 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1512 if (lpParms
== NULL
|| lpParms
->lpstrReturn
== NULL
)
1513 return MCIERR_NULL_PARAMETER_BLOCK
;
1515 TRACE("buf=%p, len=%u\n", lpParms
->lpstrReturn
, lpParms
->dwRetSize
);
1517 switch (dwFlags
& ~(MCI_WAIT
|MCI_NOTIFY
)) {
1518 case MCI_INFO_PRODUCT
: str
= wszMidiSeq
; break;
1519 case MCI_INFO_FILE
: str
= wmm
->lpstrElementName
; break;
1520 case MCI_INFO_COPYRIGHT
: str
= wmm
->lpstrCopyright
; break;
1521 case MCI_INFO_NAME
: str
= wmm
->lpstrName
; break;
1523 WARN("Don't know this info command (%u)\n", dwFlags
);
1524 return MCIERR_MISSING_PARAMETER
; /* not MCIERR_FLAGS_... */
1527 if (lpParms
->dwRetSize
) {
1529 /* FIXME? Since NT, mciwave, mciseq and mcicda set dwRetSize
1530 * to the number of characters written, excluding \0. */
1531 lstrcpynW(lpParms
->lpstrReturn
, str
? str
: &zero
, lpParms
->dwRetSize
);
1532 } else ret
= MCIERR_PARAM_OVERFLOW
;
1534 if (MMSYSERR_NOERROR
==ret
&& (dwFlags
& MCI_NOTIFY
))
1535 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1539 /**************************************************************************
1540 * MIDI_mciSeek [internal]
1542 static DWORD
MIDI_mciSeek(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_SEEK_PARMS lpParms
)
1546 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1548 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1550 position
= dwFlags
& (MCI_SEEK_TO_START
|MCI_SEEK_TO_END
|MCI_TO
);
1551 if (!position
) return MCIERR_MISSING_PARAMETER
;
1552 if (position
&(position
-1)) return MCIERR_FLAGS_NOT_COMPATIBLE
;
1554 MIDI_mciStop(wmm
, MCI_WAIT
, 0);
1556 if (dwFlags
& MCI_TO
) { /* FIXME: compare with length */
1557 wmm
->dwPositionMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
1558 } else if (dwFlags
& MCI_SEEK_TO_START
) {
1559 wmm
->dwPositionMS
= 0;
1561 wmm
->dwPositionMS
= 0xFFFFFFFF; /* FIXME */
1564 TRACE("Seeking to position=%u ms\n", wmm
->dwPositionMS
);
1566 if (dwFlags
& MCI_NOTIFY
)
1567 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1572 /*======================================================================*
1573 * MIDI entry points *
1574 *======================================================================*/
1576 /**************************************************************************
1577 * DriverProc (MCISEQ.@)
1579 LRESULT CALLBACK
MCIMIDI_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1580 LPARAM dwParam1
, LPARAM dwParam2
)
1584 case DRV_LOAD
: return 1;
1585 case DRV_FREE
: return 1;
1586 case DRV_ENABLE
: return 1;
1587 case DRV_DISABLE
: return 1;
1588 case DRV_QUERYCONFIGURE
: return 1;
1589 case DRV_CONFIGURE
: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK
); return 1;
1590 case DRV_INSTALL
: return DRVCNF_RESTART
;
1591 case DRV_REMOVE
: return DRVCNF_RESTART
;
1592 case DRV_OPEN
: return MIDI_drvOpen((LPCWSTR
)dwParam1
, (LPMCI_OPEN_DRIVER_PARMSW
)dwParam2
);
1593 case DRV_CLOSE
: return MIDI_drvClose(dwDevID
);
1595 if ((wMsg
< DRV_MCI_FIRST
) || (wMsg
> DRV_MCI_LAST
)) {
1596 TRACE("Sending msg %04x to default driver proc\n", wMsg
);
1597 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1600 wmm
= MIDI_mciGetOpenDev(dwDevID
, wMsg
);
1601 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1604 case MCI_OPEN_DRIVER
: return MIDI_mciOpen (wmm
, dwParam1
, (LPMCI_OPEN_PARMSW
) dwParam2
);
1605 case MCI_CLOSE_DRIVER
: return MIDI_mciClose (wmm
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1606 case MCI_PLAY
: return MIDI_mciPlay (wmm
, dwParam1
, (LPMCI_PLAY_PARMS
) dwParam2
);
1607 case MCI_STOP
: return MIDI_mciStop (wmm
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1608 case MCI_SET
: return MIDI_mciSet (wmm
, dwParam1
, (LPMCI_SEQ_SET_PARMS
) dwParam2
);
1609 case MCI_PAUSE
: return MIDI_mciPause (wmm
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1610 case MCI_RESUME
: return MIDI_mciResume (wmm
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1611 case MCI_STATUS
: return MIDI_mciStatus (wmm
, dwParam1
, (LPMCI_STATUS_PARMS
) dwParam2
);
1612 case MCI_GETDEVCAPS
: return MIDI_mciGetDevCaps(wmm
, dwParam1
, (LPMCI_GETDEVCAPS_PARMS
)dwParam2
);
1613 case MCI_INFO
: return MIDI_mciInfo (wmm
, dwParam1
, (LPMCI_INFO_PARMSW
) dwParam2
);
1614 case MCI_SEEK
: return MIDI_mciSeek (wmm
, dwParam1
, (LPMCI_SEEK_PARMS
) dwParam2
);
1617 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1620 TRACE("Unsupported command [0x%x]\n", wMsg
);
1621 return MCIERR_UNSUPPORTED_FUNCTION
; /* Win9x: MCIERR_UNRECOGNIZED_COMMAND */