2 * Sample MIDI Wine Driver for Linux
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1999 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * + implement it correctly
24 * + finish asynchronous commands
25 * + better implement non waiting command (without the MCI_WAIT flag).
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mcimidi
);
44 #define MIDI_NOTEOFF 0x80
45 #define MIDI_NOTEON 0x90
48 DWORD dwFirst
; /* offset in file of track */
49 DWORD dwLast
; /* number of bytes in file of track */
50 DWORD dwIndex
; /* current index in file (dwFirst <= dwIndex < dwLast) */
51 DWORD dwLength
; /* number of pulses in this track */
52 DWORD dwEventPulse
; /* current pulse # (event) pointed by dwIndex */
53 DWORD dwEventData
; /* current data (event) pointed by dwIndex */
54 WORD wEventLength
; /* current length (event) pointed by dwIndex */
55 WORD wStatus
: 1, /* 1 : playing, 0 : done */
57 wLastCommand
: 8; /* last MIDI command on track */
60 typedef struct tagWINE_MCIMIDI
{
61 UINT wDevID
; /* the MCI one */
63 int nUseCount
; /* Incremented for each shared open */
64 HANDLE hCallback
; /* Callback handle for pending notification */
65 HMMIO hFile
; /* mmio file handle open as Element */
66 LPWSTR lpstrElementName
; /* Name of file (if any) */
67 LPWSTR lpstrCopyright
;
69 WORD wPort
; /* the WINMM device unit */
70 WORD dwStatus
; /* one from MCI_MODE_xxxx */
71 DWORD dwMciTimeFormat
; /* One of the supported MCI_FORMAT_xxxx */
72 WORD wFormat
; /* Format of MIDI hFile (0, 1 or 2) */
73 WORD nTracks
; /* Number of tracks in hFile */
74 WORD nDivision
; /* Number of division in hFile PPQN or SMPTE */
76 DWORD dwTempo
; /* Tempo (# of 1/4 note per second */
77 MCI_MIDITRACK
* tracks
; /* Content of each track */
83 /* ===================================================================
84 * ===================================================================
85 * FIXME: should be using the new mmThreadXXXX functions from WINMM
87 * it would require to add a wine internal flag to mmThreadCreate
88 * in order to pass a 32 bit function instead of a 16 bit
89 * ===================================================================
90 * =================================================================== */
99 /* EPP DWORD WINAPI mciSendCommandA(UINT wDevID, UINT wMsg, DWORD dwParam1, DWORD dwParam2); */
101 /**************************************************************************
102 * MCI_SCAStarter [internal]
104 static DWORD CALLBACK
MCI_SCAStarter(LPVOID arg
)
106 struct SCA
* sca
= arg
;
109 TRACE("In thread before async command (%08x,%u,%08lx,%08lx)\n",
110 sca
->wDevID
, sca
->wMsg
, sca
->dwParam1
, sca
->dwParam2
);
111 ret
= mciSendCommandA(sca
->wDevID
, sca
->wMsg
, sca
->dwParam1
| MCI_WAIT
, sca
->dwParam2
);
112 TRACE("In thread after async command (%08x,%u,%08lx,%08lx)\n",
113 sca
->wDevID
, sca
->wMsg
, sca
->dwParam1
, sca
->dwParam2
);
114 HeapFree(GetProcessHeap(), 0, sca
);
118 /**************************************************************************
119 * MCI_SendCommandAsync [internal]
121 static DWORD
MCI_SendCommandAsync(UINT wDevID
, UINT wMsg
, DWORD_PTR dwParam1
,
122 DWORD_PTR dwParam2
, UINT size
)
125 struct SCA
* sca
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct SCA
) + size
);
128 return MCIERR_OUT_OF_MEMORY
;
130 sca
->wDevID
= wDevID
;
132 sca
->dwParam1
= dwParam1
;
134 if (size
&& dwParam2
) {
135 sca
->dwParam2
= (DWORD_PTR
)sca
+ sizeof(struct SCA
);
136 /* copy structure passed by program in dwParam2 to be sure
137 * we can still use it whatever the program does
139 memcpy((LPVOID
)sca
->dwParam2
, (LPVOID
)dwParam2
, size
);
141 sca
->dwParam2
= dwParam2
;
144 if ((handle
= CreateThread(NULL
, 0, MCI_SCAStarter
, sca
, 0, NULL
)) == 0) {
145 WARN("Couldn't allocate thread for async command handling, sending synchronously\n");
146 return MCI_SCAStarter(sca
);
148 SetThreadPriority(handle
, THREAD_PRIORITY_TIME_CRITICAL
);
153 /*======================================================================*
154 * MCI MIDI implementation *
155 *======================================================================*/
157 static DWORD
MIDI_mciResume(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
);
159 /**************************************************************************
160 * MIDI_drvOpen [internal]
162 static DWORD
MIDI_drvOpen(LPCWSTR str
, LPMCI_OPEN_DRIVER_PARMSW modp
)
166 if (!modp
) return 0xFFFFFFFF;
168 wmm
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINE_MCIMIDI
));
173 wmm
->wDevID
= modp
->wDeviceID
;
174 mciSetDriverData(wmm
->wDevID
, (DWORD_PTR
)wmm
);
175 modp
->wCustomCommandTable
= MCI_NO_COMMAND_TABLE
;
176 modp
->wType
= MCI_DEVTYPE_SEQUENCER
;
177 return modp
->wDeviceID
;
180 /**************************************************************************
181 * MCIMIDI_drvClose [internal]
183 static DWORD
MIDI_drvClose(DWORD dwDevID
)
185 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(dwDevID
);
188 HeapFree(GetProcessHeap(), 0, wmm
);
189 mciSetDriverData(dwDevID
, 0);
192 return (dwDevID
== 0xFFFFFFFF) ? 1 : 0;
195 /**************************************************************************
196 * MIDI_mciGetOpenDev [internal]
198 static WINE_MCIMIDI
* MIDI_mciGetOpenDev(MCIDEVICEID wDevID
, UINT wMsg
)
200 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(wDevID
);
202 if (wmm
== NULL
|| ((wmm
->nUseCount
== 0) ^ (wMsg
== MCI_OPEN_DRIVER
))) {
203 WARN("Invalid wDevID=%u\n", wDevID
);
209 /**************************************************************************
210 * MIDI_mciNotify [internal]
212 * Notifications in MCI work like a 1-element queue.
213 * Each new notification request supersedes the previous one.
214 * This affects Play and Record; other commands are immediate.
216 static void MIDI_mciNotify(DWORD_PTR hWndCallBack
, WINE_MCIMIDI
* wmm
, UINT wStatus
)
218 /* We simply save one parameter by not passing the wDevID local
219 * to the command. They are the same (via mciGetDriverData).
221 MCIDEVICEID wDevID
= wmm
->wDevID
;
222 HANDLE old
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
223 if (old
) mciDriverNotify(old
, wDevID
, MCI_NOTIFY_SUPERSEDED
);
224 mciDriverNotify(HWND_32(LOWORD(hWndCallBack
)), wDevID
, wStatus
);
227 /**************************************************************************
228 * MIDI_mciReadByte [internal]
230 static DWORD
MIDI_mciReadByte(WINE_MCIMIDI
* wmm
, BYTE
*lpbyt
)
235 mmioRead(wmm
->hFile
, (HPSTR
)lpbyt
, sizeof(BYTE
)) != (long)sizeof(BYTE
)) {
236 WARN("Error reading wmm=%p\n", wmm
);
237 ret
= MCIERR_INVALID_FILE
;
243 /**************************************************************************
244 * MIDI_mciReadWord [internal]
246 static DWORD
MIDI_mciReadWord(WINE_MCIMIDI
* wmm
, LPWORD lpw
)
249 DWORD ret
= MCIERR_INVALID_FILE
;
252 MIDI_mciReadByte(wmm
, &hibyte
) == 0 &&
253 MIDI_mciReadByte(wmm
, &lobyte
) == 0) {
254 *lpw
= ((WORD
)hibyte
<< 8) + lobyte
;
260 /**************************************************************************
261 * MIDI_mciReadLong [internal]
263 static DWORD
MIDI_mciReadLong(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
266 DWORD ret
= MCIERR_INVALID_FILE
;
269 MIDI_mciReadWord(wmm
, &hiword
) == 0 &&
270 MIDI_mciReadWord(wmm
, &loword
) == 0) {
271 *lpdw
= MAKELONG(loword
, hiword
);
277 /**************************************************************************
278 * MIDI_mciReadVaryLen [internal]
280 static WORD
MIDI_mciReadVaryLen(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
287 ret
= MCIERR_INVALID_FILE
;
290 if (MIDI_mciReadByte(wmm
, &byte
) != 0) {
293 value
= (value
<< 7) + (byte
& 0x7F);
295 } while (byte
& 0x80);
298 TRACE("val=%08X\n", value);
304 /**************************************************************************
305 * MIDI_mciReadNextEvent [internal]
307 static DWORD
MIDI_mciReadNextEvent(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
315 if (mmioSeek(wmm
->hFile
, mmt
->dwIndex
, SEEK_SET
) != mmt
->dwIndex
) {
316 WARN("Can't seek at %08X\n", mmt
->dwIndex
);
317 return MCIERR_INVALID_FILE
;
319 evtLength
= MIDI_mciReadVaryLen(wmm
, &evtPulse
) + 1; /* > 0 */
320 MIDI_mciReadByte(wmm
, &b1
);
324 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
328 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
330 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
331 if (evtLength
>= 0x10000u
) {
332 /* this limitation shouldn't be a problem */
333 WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
336 hw
= LOWORD(evtLength
);
341 if (b1
& 0x80) { /* use running status ? */
342 mmt
->wLastCommand
= b1
;
343 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
346 b1
= mmt
->wLastCommand
;
348 switch ((b1
>> 4) & 0x07) {
349 case 0: case 1: case 2: case 3: case 6:
350 MIDI_mciReadByte(wmm
, &b3
); evtLength
++;
356 WARN("Strange indeed b1=0x%02x\n", b1
);
360 if (mmt
->dwIndex
+ evtLength
> mmt
->dwLast
)
361 return MCIERR_INTERNAL
;
363 mmt
->dwEventPulse
+= evtPulse
;
364 mmt
->dwEventData
= (hw
<< 16) + (b2
<< 8) + b1
;
365 mmt
->wEventLength
= evtLength
;
368 TRACE("[%u] => pulse=%08x(%08x), data=%08x, length=%u\n",
369 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
370 mmt->dwEventData, mmt->wEventLength);
375 /**************************************************************************
376 * MIDI_mciReadMTrk [internal]
378 static DWORD
MIDI_mciReadMTrk(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
383 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
, (long)sizeof(FOURCC
)) !=
384 (long)sizeof(FOURCC
)) {
385 return MCIERR_INVALID_FILE
;
388 if (fourcc
!= mmioFOURCC('M', 'T', 'r', 'k')) {
389 WARN("Can't synchronize on 'MTrk' !\n");
390 return MCIERR_INVALID_FILE
;
393 if (MIDI_mciReadLong(wmm
, &toberead
) != 0) {
394 return MCIERR_INVALID_FILE
;
396 mmt
->dwFirst
= mmioSeek(wmm
->hFile
, 0, SEEK_CUR
); /* >= 0 */
397 mmt
->dwLast
= mmt
->dwFirst
+ toberead
;
399 /* compute # of pulses in this track */
400 mmt
->dwIndex
= mmt
->dwFirst
;
401 mmt
->dwEventPulse
= 0;
403 while (MIDI_mciReadNextEvent(wmm
, mmt
) == 0 && LOWORD(mmt
->dwEventData
) != 0x2FFF) {
407 mmt
->dwIndex
+= mmt
->wEventLength
;
409 switch (LOWORD(mmt
->dwEventData
)) {
412 /* position after meta data header */
413 mmioSeek(wmm
->hFile
, mmt
->dwIndex
+ HIWORD(mmt
->dwEventData
), SEEK_SET
);
414 len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
416 if (len
>= sizeof(buf
)) {
417 WARN("Buffer for text is too small (%u are needed)\n", len
);
418 len
= sizeof(buf
) - 1;
420 if (mmioRead(wmm
->hFile
, buf
, len
) == len
) {
421 buf
[len
] = 0; /* end string in case */
422 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
424 if (wmm
->lpstrCopyright
) {
425 WARN("Two copyright notices (%s|%s)\n", debugstr_w(wmm
->lpstrCopyright
), buf
);
427 len
= MultiByteToWideChar( CP_ACP
, 0, buf
, -1, NULL
, 0 );
428 wmm
->lpstrCopyright
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
429 MultiByteToWideChar( CP_ACP
, 0, buf
, -1, wmm
->lpstrCopyright
, len
);
433 if (wmm
->lpstrName
) {
434 WARN("Two names (%s|%s)\n", debugstr_w(wmm
->lpstrName
), buf
);
436 len
= MultiByteToWideChar( CP_ACP
, 0, buf
, -1, NULL
, 0 );
437 wmm
->lpstrName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
438 MultiByteToWideChar( CP_ACP
, 0, buf
, -1, wmm
->lpstrName
, len
);
446 mmt
->dwLength
= mmt
->dwEventPulse
;
448 TRACE("Track %u has %u bytes and %u pulses\n", mmt
->wTrackNr
, toberead
, mmt
->dwLength
);
450 /* reset track data */
451 mmt
->wStatus
= 1; /* ok, playing */
452 mmt
->dwIndex
= mmt
->dwFirst
;
453 mmt
->dwEventPulse
= 0;
455 if (mmioSeek(wmm
->hFile
, 0, SEEK_CUR
) != mmt
->dwLast
) {
456 WARN("Ouch, out of sync seek=%u track=%u\n",
457 mmioSeek(wmm
->hFile
, 0, SEEK_CUR
), mmt
->dwLast
);
458 /* position at end of this track, to be ready to read next track */
459 mmioSeek(wmm
->hFile
, mmt
->dwLast
, SEEK_SET
);
465 /**************************************************************************
466 * MIDI_mciReadMThd [internal]
468 static DWORD
MIDI_mciReadMThd(WINE_MCIMIDI
* wmm
, DWORD dwOffset
)
474 TRACE("(%p, %08X);\n", wmm
, dwOffset
);
476 if (mmioSeek(wmm
->hFile
, dwOffset
, SEEK_SET
) != dwOffset
) {
477 WARN("Can't seek at %08X begin of 'MThd'\n", dwOffset
);
478 return MCIERR_INVALID_FILE
;
480 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
,
481 (long) sizeof(FOURCC
)) != (long) sizeof(FOURCC
))
482 return MCIERR_INVALID_FILE
;
484 if (fourcc
!= mmioFOURCC('M', 'T', 'h', 'd')) {
485 WARN("Can't synchronize on 'MThd' !\n");
486 return MCIERR_INVALID_FILE
;
489 if (MIDI_mciReadLong(wmm
, &toberead
) != 0 || toberead
< 3 * sizeof(WORD
))
490 return MCIERR_INVALID_FILE
;
492 if (MIDI_mciReadWord(wmm
, &wmm
->wFormat
) != 0 ||
493 MIDI_mciReadWord(wmm
, &wmm
->nTracks
) != 0 ||
494 MIDI_mciReadWord(wmm
, &wmm
->nDivision
) != 0) {
495 return MCIERR_INVALID_FILE
;
498 TRACE("toberead=0x%08X, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
499 toberead
, wmm
->wFormat
, wmm
->nTracks
, wmm
->nDivision
);
501 /* MS doc says that the MIDI MCI time format must be put by default to the format
502 * stored in the MIDI file...
504 if (wmm
->nDivision
> 0x8000) {
505 /* eric.pouech@lemel.fr 98/11
506 * I did not check this very code (pulses are expressed as SMPTE sub-frames).
507 * In about 40 MB of MIDI files I have, none was SMPTE based...
508 * I'm just wondering if this is widely used :-). So, if someone has one of
509 * these files, I'd like to know about it.
511 FIXME("Handling SMPTE time in MIDI files has not been tested\n"
512 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
514 switch (HIBYTE(wmm
->nDivision
)) {
515 case 0xE8: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
; break; /* -24 */
516 case 0xE7: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
; break; /* -25 */
517 case 0xE3: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
518 case 0xE2: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
; break; /* -30 */
520 WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm
->nDivision
));
521 return MCIERR_INVALID_FILE
;
523 switch (LOBYTE(wmm
->nDivision
)) {
524 case 4: /* MIDI Time Code */
527 case 80: /* SMPTE bit resolution */
530 WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm
->nDivision
));
531 return MCIERR_INVALID_FILE
;
533 } else if (wmm
->nDivision
== 0) {
534 WARN("Number of division is 0, can't support that !!\n");
535 return MCIERR_INVALID_FILE
;
537 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
540 switch (wmm
->wFormat
) {
542 if (wmm
->nTracks
!= 1) {
543 WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
551 WARN("Handling MIDI files which format = %d is not (yet) supported\n"
552 "Please report with MIDI file !\n", wmm
->wFormat
);
553 return MCIERR_INVALID_FILE
;
556 if (wmm
->nTracks
& 0x8000) {
557 /* this shouldn't be a problem... */
558 WARN("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
559 wmm
->nTracks
= 0x7FFF;
562 if ((wmm
->tracks
= HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK
) * wmm
->nTracks
)) == NULL
) {
563 return MCIERR_OUT_OF_MEMORY
;
566 toberead
-= 3 * sizeof(WORD
);
568 TRACE("Size of MThd > 6, skipping %d extra bytes\n", toberead
);
569 mmioSeek(wmm
->hFile
, toberead
, SEEK_CUR
);
572 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
573 wmm
->tracks
[nt
].wTrackNr
= nt
;
574 if (MIDI_mciReadMTrk(wmm
, &wmm
->tracks
[nt
]) != 0) {
575 WARN("Can't read 'MTrk' header\n");
576 return MCIERR_INVALID_FILE
;
580 wmm
->dwTempo
= 500000;
585 /**************************************************************************
586 * MIDI_ConvertPulseToMS [internal]
588 static DWORD
MIDI_ConvertPulseToMS(WINE_MCIMIDI
* wmm
, DWORD pulse
)
592 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
593 * may change during file playing
595 if (wmm
->nDivision
== 0) {
596 FIXME("Shouldn't happen. wmm->nDivision = 0\n");
597 } else if (wmm
->nDivision
> 0x8000) { /* SMPTE, unchecked FIXME? */
598 int nf
= -(char)HIBYTE(wmm
->nDivision
); /* number of frames */
599 int nsf
= LOBYTE(wmm
->nDivision
); /* number of sub-frames */
600 ret
= (pulse
* 1000) / (nf
* nsf
);
602 ret
= (DWORD
)((double)pulse
* ((double)wmm
->dwTempo
/ 1000) /
603 (double)wmm
->nDivision
);
607 TRACE("pulse=%u tempo=%u division=%u=0x%04x => ms=%u\n",
608 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
614 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
615 #define TIME_MS_IN_ONE_MINUTE (60*1000)
616 #define TIME_MS_IN_ONE_SECOND (1000)
618 /**************************************************************************
619 * MIDI_ConvertTimeFormatToMS [internal]
621 static DWORD
MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI
* wmm
, DWORD val
)
625 switch (wmm
->dwMciTimeFormat
) {
626 case MCI_FORMAT_MILLISECONDS
:
629 case MCI_FORMAT_SMPTE_24
:
631 (HIBYTE(HIWORD(val
)) * 125) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
632 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
634 case MCI_FORMAT_SMPTE_25
:
636 HIBYTE(HIWORD(val
)) * 40 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
637 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
639 case MCI_FORMAT_SMPTE_30
:
641 (HIBYTE(HIWORD(val
)) * 100) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
642 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
645 WARN("Bad time format %u!\n", wmm
->dwMciTimeFormat
);
648 TRACE("val=%u=0x%08x [tf=%u] => ret=%u\n", val, val, wmm->dwMciTimeFormat, ret);
653 /**************************************************************************
654 * MIDI_ConvertMSToTimeFormat [internal]
656 static DWORD
MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI
* wmm
, DWORD _val
)
658 DWORD ret
= 0, val
= _val
;
661 switch (wmm
->dwMciTimeFormat
) {
662 case MCI_FORMAT_MILLISECONDS
:
665 case MCI_FORMAT_SMPTE_24
:
666 case MCI_FORMAT_SMPTE_25
:
667 case MCI_FORMAT_SMPTE_30
:
668 h
= val
/ TIME_MS_IN_ONE_HOUR
;
669 m
= (val
-= h
* TIME_MS_IN_ONE_HOUR
) / TIME_MS_IN_ONE_MINUTE
;
670 s
= (val
-= m
* TIME_MS_IN_ONE_MINUTE
) / TIME_MS_IN_ONE_SECOND
;
671 switch (wmm
->dwMciTimeFormat
) {
672 case MCI_FORMAT_SMPTE_24
:
673 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
674 f
= (val
* 3) / 125; val
-= (f
* 125) / 3;
676 case MCI_FORMAT_SMPTE_25
:
677 /* one frame is 1000/25 ms long, 1000/25 == 40 */
678 f
= val
/ 40; val
-= f
* 40;
680 case MCI_FORMAT_SMPTE_30
:
681 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
682 f
= (val
* 3) / 100; val
-= (f
* 100) / 3;
685 FIXME("There must be some bad bad programmer\n");
688 /* val contains the number of ms which cannot make a complete frame */
689 /* FIXME: is this correct ? programs seem to be happy with that */
690 ret
= (f
<< 24) | (s
<< 16) | (m
<< 8) | (h
<< 0);
693 WARN("Bad time format %u!\n", wmm
->dwMciTimeFormat
);
696 TRACE("val=%u [tf=%u] => ret=%u=0x%08x\n", _val, wmm->dwMciTimeFormat, ret, ret);
701 /**************************************************************************
702 * MIDI_GetMThdLengthMS [internal]
704 static DWORD
MIDI_GetMThdLengthMS(WINE_MCIMIDI
* wmm
)
709 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
710 if (wmm
->wFormat
== 2) {
711 ret
+= wmm
->tracks
[nt
].dwLength
;
712 } else if (wmm
->tracks
[nt
].dwLength
> ret
) {
713 ret
= wmm
->tracks
[nt
].dwLength
;
716 /* FIXME: this is wrong if there is a tempo change inside the file */
717 return MIDI_ConvertPulseToMS(wmm
, ret
);
720 /**************************************************************************
721 * MIDI_mciOpen [internal]
723 static DWORD
MIDI_mciOpen(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_OPEN_PARMSW lpParms
)
727 TRACE("(%d, %08X, %p)\n", wmm
->wDevID
, dwFlags
, lpParms
);
729 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
730 if (dwFlags
& MCI_OPEN_SHAREABLE
)
731 return MCIERR_HARDWARE
;
733 if (wmm
->nUseCount
> 0) {
734 /* The driver is already opened on this channel
735 * MIDI sequencer cannot be shared
737 return MCIERR_DEVICE_OPEN
;
743 wmm
->wPort
= MIDI_MAPPER
;
744 wmm
->lpstrElementName
= NULL
;
746 TRACE("wDevID=%d (lpParams->wDeviceID=%d)\n", wmm
->wDevID
, lpParms
->wDeviceID
);
747 /* lpParms->wDeviceID = wDevID;*/
749 if (dwFlags
& MCI_OPEN_ELEMENT
) {
750 TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpParms
->lpstrElementName
));
751 if (lpParms
->lpstrElementName
&& strlenW(lpParms
->lpstrElementName
) > 0) {
752 wmm
->hFile
= mmioOpenW((LPWSTR
)lpParms
->lpstrElementName
, NULL
,
753 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
754 if (wmm
->hFile
== 0) {
755 WARN("Can't find file %s!\n", debugstr_w(lpParms
->lpstrElementName
));
757 return MCIERR_FILE_NOT_FOUND
;
759 wmm
->lpstrElementName
= HeapAlloc(GetProcessHeap(), 0,
760 (strlenW(lpParms
->lpstrElementName
) + 1) * sizeof(WCHAR
));
761 strcpyW(wmm
->lpstrElementName
, lpParms
->lpstrElementName
);
764 TRACE("hFile=%p\n", wmm
->hFile
);
766 wmm
->lpstrCopyright
= NULL
;
767 wmm
->lpstrName
= NULL
;
769 wmm
->dwStatus
= MCI_MODE_NOT_READY
; /* while loading file contents */
770 /* spec says it should be the default format from the MIDI file... */
771 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
773 if (wmm
->hFile
!= 0) {
778 if (mmioDescend(wmm
->hFile
, &ckMainRIFF
, NULL
, 0) != 0) {
779 dwRet
= MCIERR_INVALID_FILE
;
781 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
782 (LPSTR
)&ckMainRIFF
.ckid
, (LPSTR
)&ckMainRIFF
.fccType
, ckMainRIFF
.cksize
);
784 if (ckMainRIFF
.ckid
== FOURCC_RIFF
&& ckMainRIFF
.fccType
== mmioFOURCC('R', 'M', 'I', 'D')) {
785 mmckInfo
.ckid
= mmioFOURCC('d', 'a', 't', 'a');
786 mmioSeek(wmm
->hFile
, ckMainRIFF
.dwDataOffset
+ ((ckMainRIFF
.cksize
+ 1) & ~1), SEEK_SET
);
787 if (mmioDescend(wmm
->hFile
, &mmckInfo
, &ckMainRIFF
, MMIO_FINDCHUNK
) == 0) {
788 TRACE("... is a 'RMID' file\n");
789 dwOffset
= mmckInfo
.dwDataOffset
;
791 dwRet
= MCIERR_INVALID_FILE
;
794 if (dwRet
== 0 && MIDI_mciReadMThd(wmm
, dwOffset
) != 0) {
795 WARN("Can't read 'MThd' header\n");
796 dwRet
= MCIERR_INVALID_FILE
;
800 TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
808 mmioClose(wmm
->hFile
, 0);
811 wmm
->dwPositionMS
= 0;
812 wmm
->dwStatus
= MCI_MODE_STOP
;
813 if (dwFlags
& MCI_NOTIFY
)
814 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
819 /**************************************************************************
820 * MIDI_mciStop [internal]
822 static DWORD
MIDI_mciStop(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
826 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
828 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
829 HANDLE old
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
830 if (old
) mciDriverNotify(old
, wmm
->wDevID
, MCI_NOTIFY_ABORTED
);
833 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
834 int oldstat
= wmm
->dwStatus
;
836 wmm
->dwStatus
= MCI_MODE_NOT_READY
;
837 if (oldstat
== MCI_MODE_PAUSE
)
838 dwRet
= midiOutReset((HMIDIOUT
)wmm
->hMidi
);
840 while (wmm
->dwStatus
!= MCI_MODE_STOP
)
845 wmm
->dwStatus
= MCI_MODE_STOP
;
847 if ((dwFlags
& MCI_NOTIFY
) && lpParms
&& MMSYSERR_NOERROR
==dwRet
)
848 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
852 /**************************************************************************
853 * MIDI_mciClose [internal]
855 static DWORD
MIDI_mciClose(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
858 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
860 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
861 /* mciStop handles MCI_NOTIFY_ABORTED */
862 MIDI_mciStop(wmm
, MCI_WAIT
, lpParms
);
866 if (wmm
->nUseCount
== 0) {
867 if (wmm
->hFile
!= 0) {
868 mmioClose(wmm
->hFile
, 0);
870 TRACE("hFile closed !\n");
872 HeapFree(GetProcessHeap(), 0, wmm
->tracks
);
873 HeapFree(GetProcessHeap(), 0, wmm
->lpstrElementName
);
874 HeapFree(GetProcessHeap(), 0, wmm
->lpstrCopyright
);
875 HeapFree(GetProcessHeap(), 0, wmm
->lpstrName
);
877 TRACE("Shouldn't happen... nUseCount=%d\n", wmm
->nUseCount
);
878 return MCIERR_INTERNAL
;
881 if ((dwFlags
& MCI_NOTIFY
) && lpParms
) {
882 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
887 /**************************************************************************
888 * MIDI_mciFindNextEvent [internal]
890 static MCI_MIDITRACK
* MIDI_mciFindNextEvent(WINE_MCIMIDI
* wmm
, LPDWORD hiPulse
)
895 *hiPulse
= 0xFFFFFFFFul
;
897 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
898 mmt
= &wmm
->tracks
[nt
];
900 if (mmt
->wStatus
== 0)
902 if (mmt
->dwEventPulse
< *hiPulse
) {
903 *hiPulse
= mmt
->dwEventPulse
;
907 return (cnt
== 0xFFFFu
) ? 0 /* no more event on all tracks */
911 /**************************************************************************
912 * MIDI_mciPlay [internal]
914 static DWORD
MIDI_mciPlay(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_PLAY_PARMS lpParms
)
916 DWORD dwStartMS
, dwEndMS
;
923 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
925 if (wmm
->hFile
== 0) {
926 WARN("Can't play: no file %s!\n", debugstr_w(wmm
->lpstrElementName
));
927 return MCIERR_FILE_NOT_FOUND
;
930 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
931 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
932 /* FIXME: parameters (start/end) in lpParams may not be used */
933 return MIDI_mciResume(wmm
, dwFlags
, (LPMCI_GENERIC_PARMS
)lpParms
);
935 WARN("Can't play: device is not stopped !\n");
936 return MCIERR_INTERNAL
;
939 if (!(dwFlags
& MCI_WAIT
)) {
940 return MCI_SendCommandAsync(wmm
->wDevID
, MCI_PLAY
, dwFlags
, (DWORD_PTR
)lpParms
, sizeof(MCI_PLAY_PARMS
));
943 if (lpParms
&& (dwFlags
& MCI_FROM
)) {
944 dwStartMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwFrom
);
946 dwStartMS
= wmm
->dwPositionMS
;
949 if (lpParms
&& (dwFlags
& MCI_TO
)) {
950 dwEndMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
952 dwEndMS
= 0xFFFFFFFFul
;
955 TRACE("Playing from %u to %u\n", dwStartMS
, dwEndMS
);
957 oldcb
= InterlockedExchangePointer(&wmm
->hCallback
,
958 (dwFlags
& MCI_NOTIFY
) ? HWND_32(LOWORD(lpParms
->dwCallback
)) : NULL
);
959 if (oldcb
) mciDriverNotify(oldcb
, wmm
->wDevID
, MCI_NOTIFY_ABORTED
);
963 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
964 mmt
= &wmm
->tracks
[nt
];
966 mmt
->wStatus
= 1; /* ok, playing */
967 mmt
->dwIndex
= mmt
->dwFirst
;
968 if (wmm
->wFormat
== 2 && nt
> 0) {
969 mmt
->dwEventPulse
= wmm
->tracks
[nt
- 1].dwLength
;
971 mmt
->dwEventPulse
= 0;
973 MIDI_mciReadNextEvent(wmm
, mmt
); /* FIXME == 0 */
976 dwRet
= midiOutOpen((LPHMIDIOUT
)&wmm
->hMidi
, wmm
->wPort
, 0L, 0L, CALLBACK_NULL
);
977 if (dwRet
!= MMSYSERR_NOERROR
) {
982 wmm
->dwTempo
= 500000;
983 wmm
->dwStatus
= MCI_MODE_PLAY
;
984 wmm
->dwPositionMS
= 0;
985 wmm
->wStartedPlaying
= FALSE
;
987 while (wmm
->dwStatus
!= MCI_MODE_STOP
&& wmm
->dwStatus
!= MCI_MODE_NOT_READY
) {
988 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
989 * too much. Tell gcc not to optimize status value using volatile.
991 while (((volatile WINE_MCIMIDI
*)wmm
)->dwStatus
== MCI_MODE_PAUSE
);
993 doPlay
= (wmm
->dwPositionMS
>= dwStartMS
&& wmm
->dwPositionMS
<= dwEndMS
);
995 TRACE("wmm->dwStatus=%d, doPlay=%c\n", wmm
->dwStatus
, doPlay
? 'T' : 'F');
997 if ((mmt
= MIDI_mciFindNextEvent(wmm
, &hiPulse
)) == NULL
)
998 break; /* no more event on tracks */
1000 /* if starting playing, then set StartTicks to the value it would have had
1001 * if play had started at position 0
1003 if (doPlay
&& !wmm
->wStartedPlaying
) {
1004 wmm
->dwStartTicks
= GetTickCount() - MIDI_ConvertPulseToMS(wmm
, wmm
->dwPulse
);
1005 wmm
->wStartedPlaying
= TRUE
;
1006 TRACE("Setting dwStartTicks to %u\n", wmm
->dwStartTicks
);
1009 if (hiPulse
> wmm
->dwPulse
) {
1010 wmm
->dwPositionMS
+= MIDI_ConvertPulseToMS(wmm
, hiPulse
- wmm
->dwPulse
);
1012 DWORD togo
= wmm
->dwStartTicks
+ wmm
->dwPositionMS
;
1013 DWORD tc
= GetTickCount();
1015 TRACE("Pulses hi=0x%08x <> cur=0x%08x\n", hiPulse
, wmm
->dwPulse
);
1016 TRACE("Wait until %u => %u ms\n",
1017 tc
- wmm
->dwStartTicks
, togo
- wmm
->dwStartTicks
);
1021 wmm
->dwPulse
= hiPulse
;
1024 switch (LOBYTE(LOWORD(mmt
->dwEventData
))) {
1026 case 0xF7: /* sysex events */
1028 FIXME("Not handling SysEx events (yet)\n");
1032 /* position after meta data header */
1033 mmioSeek(wmm
->hFile
, mmt
->dwIndex
+ HIWORD(mmt
->dwEventData
), SEEK_SET
);
1034 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
1035 case 0x00: /* 16-bit sequence number */
1036 if (TRACE_ON(mcimidi
)) {
1039 MIDI_mciReadWord(wmm
, &twd
); /* == 0 */
1040 TRACE("Got sequence number %u\n", twd
);
1043 case 0x01: /* any text */
1044 case 0x02: /* Copyright Message text */
1045 case 0x03: /* Sequence/Track Name text */
1046 case 0x04: /* Instrument Name text */
1047 case 0x05: /* Lyric text */
1048 case 0x06: /* Marker text */
1049 case 0x07: /* Cue-point text */
1050 if (TRACE_ON(mcimidi
)) {
1052 WORD len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
1053 static const char* const info
[8] = {"", "Text", "Copyright", "Seq/Trk name",
1054 "Instrument", "Lyric", "Marker", "Cue-point"};
1055 WORD idx
= HIBYTE(LOWORD(mmt
->dwEventData
));
1057 if (len
>= sizeof(buf
)) {
1058 WARN("Buffer for text is too small (%u are needed)\n", len
);
1059 len
= sizeof(buf
) - 1;
1061 if (mmioRead(wmm
->hFile
, buf
, len
) == len
) {
1062 buf
[len
] = 0; /* end string in case */
1063 TRACE("%s => \"%s\"\n", (idx
< 8 ) ? info
[idx
] : "", buf
);
1065 WARN("Couldn't read data for %s\n", (idx
< 8) ? info
[idx
] : "");
1070 /* MIDI channel (cc) */
1071 if (FIXME_ON(mcimidi
)) {
1074 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
1075 FIXME("NIY: MIDI channel=%u, track=%u\n", bt
, mmt
->wTrackNr
);
1079 /* MIDI port (pp) */
1080 if (FIXME_ON(mcimidi
)) {
1083 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
1084 FIXME("NIY: MIDI port=%u, track=%u\n", bt
, mmt
->wTrackNr
);
1087 case 0x2F: /* end of track */
1090 case 0x51:/* set tempo */
1091 /* Tempo is expressed in -seconds per midi quarter note
1092 * for format 1 MIDI files, this can only be present on track #0
1094 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
1095 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt
->wTrackNr
);
1100 MIDI_mciReadByte(wmm
, &tbt
); value
= ((DWORD
)tbt
) << 16;
1101 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 8;
1102 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 0;
1103 TRACE("Setting tempo to %d (BPM=%d)\n", wmm
->dwTempo
, (value
) ? (60000000 / value
) : 0);
1104 wmm
->dwTempo
= value
;
1107 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
1108 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
1109 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt
->wTrackNr
);
1110 } if (mmt
->dwEventPulse
!= 0) {
1111 WARN("SMPTE track start can only be expressed at start of track (%u)\n", mmt
->dwEventPulse
);
1113 BYTE h
, m
, s
, f
, ff
;
1115 MIDI_mciReadByte(wmm
, &h
);
1116 MIDI_mciReadByte(wmm
, &m
);
1117 MIDI_mciReadByte(wmm
, &s
);
1118 MIDI_mciReadByte(wmm
, &f
);
1119 MIDI_mciReadByte(wmm
, &ff
);
1120 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h
, m
, s
, f
, ff
);
1123 case 0x58: /* file rhythm */
1124 if (TRACE_ON(mcimidi
)) {
1125 BYTE num
, den
, cpmc
, _32npqn
;
1127 MIDI_mciReadByte(wmm
, &num
);
1128 MIDI_mciReadByte(wmm
, &den
); /* to notate e.g. 6/8 */
1129 MIDI_mciReadByte(wmm
, &cpmc
); /* number of MIDI clocks per metronome click */
1130 MIDI_mciReadByte(wmm
, &_32npqn
); /* number of notated 32nd notes per MIDI quarter note */
1132 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num
, 1 << den
, cpmc
, _32npqn
);
1135 case 0x59: /* key signature */
1136 if (TRACE_ON(mcimidi
)) {
1139 MIDI_mciReadByte(wmm
, &sf
);
1140 MIDI_mciReadByte(wmm
, &mm
);
1142 if (sf
>= 0x80) TRACE("%d flats\n", -(char)sf
);
1143 else if (sf
> 0) TRACE("%d sharps\n", (char)sf
);
1144 else TRACE("Key of C\n");
1145 TRACE("Mode: %s\n", (mm
== 0) ? "major" : "minor");
1149 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt
->dwEventData
)));
1155 dwRet
= midiOutShortMsg((HMIDIOUT
)wmm
->hMidi
, mmt
->dwEventData
);
1157 switch (LOBYTE(LOWORD(mmt
->dwEventData
)) & 0xF0) {
1163 dwRet
= midiOutShortMsg((HMIDIOUT
)wmm
->hMidi
, mmt
->dwEventData
);
1167 mmt
->dwIndex
+= mmt
->wEventLength
;
1168 if (mmt
->dwIndex
< mmt
->dwFirst
|| mmt
->dwIndex
>= mmt
->dwLast
) {
1172 MIDI_mciReadNextEvent(wmm
, mmt
);
1176 midiOutReset((HMIDIOUT
)wmm
->hMidi
);
1178 dwRet
= midiOutClose((HMIDIOUT
)wmm
->hMidi
);
1180 if (dwFlags
& MCI_NOTIFY
)
1181 oldcb
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
1183 wmm
->dwStatus
= MCI_MODE_STOP
;
1185 /* Let the potentially asynchronous commands support FAILURE notification. */
1186 if (oldcb
) mciDriverNotify(oldcb
, wmm
->wDevID
,
1187 dwRet
? MCI_NOTIFY_FAILURE
: MCI_NOTIFY_SUCCESSFUL
);
1191 /**************************************************************************
1192 * MIDI_mciPause [internal]
1194 static DWORD
MIDI_mciPause(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1196 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1198 if (wmm
->dwStatus
== MCI_MODE_PLAY
) {
1199 /* stop all notes */
1201 for (chn
= 0; chn
< 16; chn
++)
1202 midiOutShortMsg((HMIDIOUT
)(wmm
->hMidi
), 0x78B0 | chn
);
1203 wmm
->dwStatus
= MCI_MODE_PAUSE
;
1205 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1206 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1210 /**************************************************************************
1211 * MIDI_mciResume [internal]
1213 static DWORD
MIDI_mciResume(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1215 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1217 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
1218 wmm
->wStartedPlaying
= FALSE
;
1219 wmm
->dwStatus
= MCI_MODE_PLAY
;
1221 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1222 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1226 /**************************************************************************
1227 * MIDI_mciSet [internal]
1229 static DWORD
MIDI_mciSet(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_SEQ_SET_PARMS lpParms
)
1231 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1233 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1235 if (dwFlags
& MCI_SET_TIME_FORMAT
) {
1236 switch (lpParms
->dwTimeFormat
) {
1237 case MCI_FORMAT_MILLISECONDS
:
1238 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1239 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
1241 case MCI_FORMAT_SMPTE_24
:
1242 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1243 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
;
1245 case MCI_FORMAT_SMPTE_25
:
1246 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1247 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
;
1249 case MCI_FORMAT_SMPTE_30
:
1250 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1251 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
;
1254 WARN("Bad time format %u!\n", lpParms
->dwTimeFormat
);
1255 return MCIERR_BAD_TIME_FORMAT
;
1258 if (dwFlags
& MCI_SET_VIDEO
) {
1259 TRACE("No support for video !\n");
1260 return MCIERR_UNSUPPORTED_FUNCTION
;
1262 if (dwFlags
& MCI_SET_DOOR_OPEN
) {
1263 TRACE("No support for door open !\n");
1264 return MCIERR_UNSUPPORTED_FUNCTION
;
1266 if (dwFlags
& MCI_SET_DOOR_CLOSED
) {
1267 TRACE("No support for door close !\n");
1268 return MCIERR_UNSUPPORTED_FUNCTION
;
1270 if (dwFlags
& MCI_SET_AUDIO
) {
1271 if (dwFlags
& MCI_SET_ON
) {
1272 TRACE("MCI_SET_ON audio !\n");
1273 } else if (dwFlags
& MCI_SET_OFF
) {
1274 TRACE("MCI_SET_OFF audio !\n");
1276 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1277 return MCIERR_BAD_INTEGER
;
1280 switch (lpParms
->dwAudio
)
1282 case MCI_SET_AUDIO_ALL
: TRACE("MCI_SET_AUDIO_ALL !\n"); break;
1283 case MCI_SET_AUDIO_LEFT
: TRACE("MCI_SET_AUDIO_LEFT !\n"); break;
1284 case MCI_SET_AUDIO_RIGHT
: TRACE("MCI_SET_AUDIO_RIGHT !\n"); break;
1285 default: WARN("Unknown audio channel %u\n", lpParms
->dwAudio
); break;
1289 if (dwFlags
& MCI_SEQ_SET_MASTER
)
1290 TRACE("MCI_SEQ_SET_MASTER !\n");
1291 if (dwFlags
& MCI_SEQ_SET_SLAVE
)
1292 TRACE("MCI_SEQ_SET_SLAVE !\n");
1293 if (dwFlags
& MCI_SEQ_SET_OFFSET
)
1294 TRACE("MCI_SEQ_SET_OFFSET !\n");
1295 if (dwFlags
& MCI_SEQ_SET_PORT
) {
1296 TRACE("MCI_SEQ_SET_PORT = %d\n", lpParms
->dwPort
);
1297 if ((UINT16
)lpParms
->dwPort
!= (UINT16
)MIDI_MAPPER
&&
1298 (UINT16
)lpParms
->dwPort
>= midiOutGetNumDevs())
1299 /* FIXME: input/output port distinction? */
1300 return MCIERR_SEQ_PORT_NONEXISTENT
;
1301 /* FIXME: Native manages to swap the device while playing! */
1302 wmm
->wPort
= lpParms
->dwPort
;
1304 if (dwFlags
& MCI_SEQ_SET_TEMPO
)
1305 TRACE("MCI_SEQ_SET_TEMPO !\n");
1306 if (dwFlags
& MCI_NOTIFY
)
1307 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1311 /**************************************************************************
1312 * MIDI_mciStatus [internal]
1314 static DWORD
MIDI_mciStatus(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_STATUS_PARMS lpParms
)
1318 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1320 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1322 if (dwFlags
& MCI_STATUS_ITEM
) {
1323 switch (lpParms
->dwItem
) {
1324 case MCI_STATUS_CURRENT_TRACK
:
1325 /* FIXME in Format 2 */
1326 lpParms
->dwReturn
= 1;
1327 TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms
->dwReturn
);
1329 case MCI_STATUS_LENGTH
:
1330 if ((dwFlags
& MCI_TRACK
) && wmm
->wFormat
== 2) {
1331 if (lpParms
->dwTrack
>= wmm
->nTracks
)
1332 return MCIERR_BAD_INTEGER
;
1333 /* FIXME: this is wrong if there is a tempo change inside the file */
1334 lpParms
->dwReturn
= MIDI_ConvertPulseToMS(wmm
, wmm
->tracks
[lpParms
->dwTrack
].dwLength
);
1336 lpParms
->dwReturn
= MIDI_GetMThdLengthMS(wmm
);
1338 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
, lpParms
->dwReturn
);
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: do I need to use MCI_TRACK ? */
1357 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
,
1358 (dwFlags
& MCI_STATUS_START
) ? 0 : wmm
->dwPositionMS
);
1359 TRACE("MCI_STATUS_POSITION %s => %lu\n",
1360 (dwFlags
& MCI_STATUS_START
) ? "start" : "current", lpParms
->dwReturn
);
1362 case MCI_STATUS_READY
:
1363 lpParms
->dwReturn
= (wmm
->dwStatus
== MCI_MODE_NOT_READY
) ?
1364 MAKEMCIRESOURCE(FALSE
, MCI_FALSE
) : MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1365 ret
= MCI_RESOURCE_RETURNED
;
1366 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms
->dwReturn
));
1368 case MCI_STATUS_TIME_FORMAT
:
1369 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwMciTimeFormat
, MCI_FORMAT_RETURN_BASE
+ wmm
->dwMciTimeFormat
);
1370 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms
->dwReturn
));
1371 ret
= MCI_RESOURCE_RETURNED
;
1373 case MCI_SEQ_STATUS_DIVTYPE
:
1374 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1375 if (wmm
->nDivision
> 0x8000) {
1376 switch (wmm
->nDivision
) {
1377 case 0xE8: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_24
; break; /* -24 */
1378 case 0xE7: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_25
; break; /* -25 */
1379 case 0xE3: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
1380 case 0xE2: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30
; break; /* -30 */
1381 default: FIXME("There is a bad bad programmer\n");
1384 lpParms
->dwReturn
= MCI_SEQ_DIV_PPQN
;
1386 lpParms
->dwReturn
= MAKEMCIRESOURCE(lpParms
->dwReturn
,lpParms
->dwReturn
);
1387 ret
= MCI_RESOURCE_RETURNED
;
1389 case MCI_SEQ_STATUS_MASTER
:
1390 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1391 lpParms
->dwReturn
= 0;
1393 case MCI_SEQ_STATUS_SLAVE
:
1394 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1395 lpParms
->dwReturn
= 0;
1397 case MCI_SEQ_STATUS_OFFSET
:
1398 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1399 lpParms
->dwReturn
= 0;
1401 case MCI_SEQ_STATUS_PORT
:
1402 if (wmm
->wPort
!= (UINT16
)MIDI_MAPPER
)
1403 lpParms
->dwReturn
= wmm
->wPort
;
1405 lpParms
->dwReturn
= MAKEMCIRESOURCE(MIDI_MAPPER
, MCI_SEQ_MAPPER_S
);
1406 ret
= MCI_RESOURCE_RETURNED
;
1408 TRACE("MCI_SEQ_STATUS_PORT (%u) => %d\n", wmm
->wDevID
, wmm
->wPort
);
1410 case MCI_SEQ_STATUS_TEMPO
:
1411 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1412 lpParms
->dwReturn
= wmm
->dwTempo
;
1415 FIXME("Unknown command %08X !\n", lpParms
->dwItem
);
1416 return MCIERR_UNRECOGNIZED_COMMAND
;
1419 WARN("No Status-Item!\n");
1420 return MCIERR_UNRECOGNIZED_COMMAND
;
1422 if ((dwFlags
& MCI_NOTIFY
) && HRESULT_CODE(ret
)==0)
1423 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1427 /**************************************************************************
1428 * MIDI_mciGetDevCaps [internal]
1430 static DWORD
MIDI_mciGetDevCaps(WINE_MCIMIDI
* wmm
, DWORD dwFlags
,
1431 LPMCI_GETDEVCAPS_PARMS lpParms
)
1435 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1437 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1439 if (dwFlags
& MCI_GETDEVCAPS_ITEM
) {
1440 switch (lpParms
->dwItem
) {
1441 case MCI_GETDEVCAPS_DEVICE_TYPE
:
1442 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1443 lpParms
->dwReturn
= MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER
, MCI_DEVTYPE_SEQUENCER
);
1444 ret
= MCI_RESOURCE_RETURNED
;
1446 case MCI_GETDEVCAPS_HAS_AUDIO
:
1447 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1448 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1449 ret
= MCI_RESOURCE_RETURNED
;
1451 case MCI_GETDEVCAPS_HAS_VIDEO
:
1452 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1453 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1454 ret
= MCI_RESOURCE_RETURNED
;
1456 case MCI_GETDEVCAPS_USES_FILES
:
1457 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1458 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1459 ret
= MCI_RESOURCE_RETURNED
;
1461 case MCI_GETDEVCAPS_COMPOUND_DEVICE
:
1462 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1463 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1464 ret
= MCI_RESOURCE_RETURNED
;
1466 case MCI_GETDEVCAPS_CAN_EJECT
:
1467 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1468 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1469 ret
= MCI_RESOURCE_RETURNED
;
1471 case MCI_GETDEVCAPS_CAN_PLAY
:
1472 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1473 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1474 ret
= MCI_RESOURCE_RETURNED
;
1476 case MCI_GETDEVCAPS_CAN_RECORD
:
1477 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1478 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1479 ret
= MCI_RESOURCE_RETURNED
;
1481 case MCI_GETDEVCAPS_CAN_SAVE
:
1482 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1483 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
1484 ret
= MCI_RESOURCE_RETURNED
;
1487 FIXME("Unknown capability (%08x) !\n", lpParms
->dwItem
);
1488 return MCIERR_UNRECOGNIZED_COMMAND
;
1491 WARN("No GetDevCaps-Item !\n");
1492 return MCIERR_UNRECOGNIZED_COMMAND
;
1494 if ((dwFlags
& MCI_NOTIFY
) && HRESULT_CODE(ret
)==0)
1495 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1499 /**************************************************************************
1500 * MIDI_mciInfo [internal]
1502 static DWORD
MIDI_mciInfo(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_INFO_PARMSW lpParms
)
1506 static const WCHAR wszMidiSeq
[] = {'W','i','n','e','\'','s',' ','M','I','D','I',' ','s','e','q','u','e','n','c','e','r',0};
1508 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1510 if (lpParms
== NULL
|| lpParms
->lpstrReturn
== NULL
)
1511 return MCIERR_NULL_PARAMETER_BLOCK
;
1513 TRACE("buf=%p, len=%u\n", lpParms
->lpstrReturn
, lpParms
->dwRetSize
);
1515 switch (dwFlags
& ~(MCI_WAIT
|MCI_NOTIFY
)) {
1516 case MCI_INFO_PRODUCT
: str
= wszMidiSeq
; break;
1517 case MCI_INFO_FILE
: str
= wmm
->lpstrElementName
; break;
1518 case MCI_INFO_COPYRIGHT
: str
= wmm
->lpstrCopyright
; break;
1519 case MCI_INFO_NAME
: str
= wmm
->lpstrName
; break;
1521 WARN("Don't know this info command (%u)\n", dwFlags
);
1522 return MCIERR_UNRECOGNIZED_COMMAND
;
1525 if (lpParms
->dwRetSize
) {
1527 /* FIXME? Since NT, mciwave, mciseq and mcicda set dwRetSize
1528 * to the number of characters written, excluding \0. */
1529 lstrcpynW(lpParms
->lpstrReturn
, str
? str
: &zero
, lpParms
->dwRetSize
);
1530 } else ret
= MCIERR_PARAM_OVERFLOW
;
1532 if (MMSYSERR_NOERROR
==ret
&& (dwFlags
& MCI_NOTIFY
))
1533 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1537 /**************************************************************************
1538 * MIDI_mciSeek [internal]
1540 static DWORD
MIDI_mciSeek(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_SEEK_PARMS lpParms
)
1544 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1546 if (lpParms
== NULL
) {
1547 ret
= MCIERR_NULL_PARAMETER_BLOCK
;
1549 MIDI_mciStop(wmm
, MCI_WAIT
, 0);
1551 if (dwFlags
& MCI_SEEK_TO_START
) {
1552 wmm
->dwPositionMS
= 0;
1553 } else if (dwFlags
& MCI_SEEK_TO_END
) {
1554 wmm
->dwPositionMS
= 0xFFFFFFFF; /* FIXME */
1555 } else if (dwFlags
& MCI_TO
) {
1556 wmm
->dwPositionMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
1558 WARN("dwFlag doesn't tell where to seek to...\n");
1559 return MCIERR_MISSING_PARAMETER
;
1562 TRACE("Seeking to position=%u ms\n", wmm
->dwPositionMS
);
1564 if (dwFlags
& MCI_NOTIFY
)
1565 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1570 /*======================================================================*
1571 * MIDI entry points *
1572 *======================================================================*/
1574 /**************************************************************************
1575 * DriverProc (MCISEQ.@)
1577 LRESULT CALLBACK
MCIMIDI_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1578 LPARAM dwParam1
, LPARAM dwParam2
)
1582 case DRV_LOAD
: return 1;
1583 case DRV_FREE
: return 1;
1584 case DRV_ENABLE
: return 1;
1585 case DRV_DISABLE
: return 1;
1586 case DRV_QUERYCONFIGURE
: return 1;
1587 case DRV_CONFIGURE
: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK
); return 1;
1588 case DRV_INSTALL
: return DRVCNF_RESTART
;
1589 case DRV_REMOVE
: return DRVCNF_RESTART
;
1590 case DRV_OPEN
: return MIDI_drvOpen((LPCWSTR
)dwParam1
, (LPMCI_OPEN_DRIVER_PARMSW
)dwParam2
);
1591 case DRV_CLOSE
: return MIDI_drvClose(dwDevID
);
1593 if ((wMsg
< DRV_MCI_FIRST
) || (wMsg
> DRV_MCI_LAST
)) {
1594 TRACE("Sending msg %04x to default driver proc\n", wMsg
);
1595 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1598 wmm
= MIDI_mciGetOpenDev(dwDevID
, wMsg
);
1599 if (wmm
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1602 case MCI_OPEN_DRIVER
: return MIDI_mciOpen (wmm
, dwParam1
, (LPMCI_OPEN_PARMSW
) dwParam2
);
1603 case MCI_CLOSE_DRIVER
: return MIDI_mciClose (wmm
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1604 case MCI_PLAY
: return MIDI_mciPlay (wmm
, dwParam1
, (LPMCI_PLAY_PARMS
) dwParam2
);
1605 case MCI_STOP
: return MIDI_mciStop (wmm
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1606 case MCI_SET
: return MIDI_mciSet (wmm
, dwParam1
, (LPMCI_SEQ_SET_PARMS
) dwParam2
);
1607 case MCI_PAUSE
: return MIDI_mciPause (wmm
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1608 case MCI_RESUME
: return MIDI_mciResume (wmm
, dwParam1
, (LPMCI_GENERIC_PARMS
) dwParam2
);
1609 case MCI_STATUS
: return MIDI_mciStatus (wmm
, dwParam1
, (LPMCI_STATUS_PARMS
) dwParam2
);
1610 case MCI_GETDEVCAPS
: return MIDI_mciGetDevCaps(wmm
, dwParam1
, (LPMCI_GETDEVCAPS_PARMS
)dwParam2
);
1611 case MCI_INFO
: return MIDI_mciInfo (wmm
, dwParam1
, (LPMCI_INFO_PARMSW
) dwParam2
);
1612 case MCI_SEEK
: return MIDI_mciSeek (wmm
, dwParam1
, (LPMCI_SEEK_PARMS
) dwParam2
);
1615 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1618 TRACE("Unsupported command [0x%x]\n", wMsg
);
1619 return MCIERR_UNSUPPORTED_FUNCTION
; /* Win9x: MCIERR_UNRECOGNIZED_COMMAND */