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).
28 #define WIN32_NO_STATUS
36 //#include "winbase.h"
41 #include <wine/debug.h>
42 #include <wine/unicode.h>
44 WINE_DEFAULT_DEBUG_CHANNEL(mcimidi
);
46 #define MIDI_NOTEOFF 0x80
47 #define MIDI_NOTEON 0x90
50 DWORD dwFirst
; /* offset in file of track */
51 DWORD dwLast
; /* number of bytes in file of track */
52 DWORD dwIndex
; /* current index in file (dwFirst <= dwIndex < dwLast) */
53 DWORD dwLength
; /* number of pulses in this track */
54 DWORD dwEventPulse
; /* current pulse # (event) pointed by dwIndex */
55 DWORD dwEventData
; /* current data (event) pointed by dwIndex */
56 WORD wEventLength
; /* current length (event) pointed by dwIndex */
57 WORD wStatus
: 1, /* 1 : playing, 0 : done */
59 wLastCommand
: 8; /* last MIDI command on track */
62 typedef struct tagWINE_MCIMIDI
{
63 UINT wDevID
; /* the MCI one */
65 int nUseCount
; /* Incremented for each shared open */
66 HANDLE hCallback
; /* Callback handle for pending notification */
67 HMMIO hFile
; /* mmio file handle open as Element */
68 LPWSTR lpstrElementName
; /* Name of file (if any) */
69 LPWSTR lpstrCopyright
;
71 WORD wPort
; /* the WINMM device unit */
72 WORD dwStatus
; /* one from MCI_MODE_xxxx */
73 DWORD dwMciTimeFormat
; /* One of the supported MCI_FORMAT_xxxx */
74 WORD wFormat
; /* Format of MIDI hFile (0, 1 or 2) */
75 WORD nTracks
; /* Number of tracks in hFile */
76 WORD nDivision
; /* Number of division in hFile PPQN or SMPTE */
78 DWORD dwTempo
; /* Tempo (# of 1/4 note per second */
79 MCI_MIDITRACK
* tracks
; /* Content of each track */
85 /* ===================================================================
86 * ===================================================================
87 * FIXME: should be using the new mmThreadXXXX functions from WINMM
89 * it would require to add a wine internal flag to mmThreadCreate
90 * in order to pass a 32 bit function instead of a 16 bit
91 * ===================================================================
92 * =================================================================== */
101 /* EPP DWORD WINAPI mciSendCommandA(UINT wDevID, UINT wMsg, DWORD dwParam1, DWORD dwParam2); */
103 /**************************************************************************
104 * MCI_SCAStarter [internal]
106 static DWORD CALLBACK
MCI_SCAStarter(LPVOID arg
)
108 struct SCA
* sca
= arg
;
111 TRACE("In thread before async command (%08x,%u,%08lx,%08lx)\n",
112 sca
->wDevID
, sca
->wMsg
, sca
->dwParam1
, sca
->dwParam2
);
113 ret
= mciSendCommandA(sca
->wDevID
, sca
->wMsg
, sca
->dwParam1
| MCI_WAIT
, sca
->dwParam2
);
114 TRACE("In thread after async command (%08x,%u,%08lx,%08lx)\n",
115 sca
->wDevID
, sca
->wMsg
, sca
->dwParam1
, sca
->dwParam2
);
116 HeapFree(GetProcessHeap(), 0, sca
);
120 /**************************************************************************
121 * MCI_SendCommandAsync [internal]
123 static DWORD
MCI_SendCommandAsync(UINT wDevID
, UINT wMsg
, DWORD_PTR dwParam1
,
124 DWORD_PTR dwParam2
, UINT size
)
127 struct SCA
* sca
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct SCA
) + size
);
130 return MCIERR_OUT_OF_MEMORY
;
132 sca
->wDevID
= wDevID
;
134 sca
->dwParam1
= dwParam1
;
136 if (size
&& dwParam2
) {
137 sca
->dwParam2
= (DWORD_PTR
)sca
+ sizeof(struct SCA
);
138 /* copy structure passed by program in dwParam2 to be sure
139 * we can still use it whatever the program does
141 memcpy((LPVOID
)sca
->dwParam2
, (LPVOID
)dwParam2
, size
);
143 sca
->dwParam2
= dwParam2
;
146 if ((handle
= CreateThread(NULL
, 0, MCI_SCAStarter
, sca
, 0, NULL
)) == 0) {
147 WARN("Couldn't allocate thread for async command handling, sending synchronously\n");
148 return MCI_SCAStarter(sca
);
150 SetThreadPriority(handle
, THREAD_PRIORITY_TIME_CRITICAL
);
155 /*======================================================================*
156 * MCI MIDI implementation *
157 *======================================================================*/
159 static DWORD
MIDI_mciResume(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
);
161 /**************************************************************************
162 * MIDI_drvOpen [internal]
164 static DWORD
MIDI_drvOpen(LPCWSTR str
, LPMCI_OPEN_DRIVER_PARMSW modp
)
168 if (!modp
) return 0xFFFFFFFF;
170 wmm
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINE_MCIMIDI
));
175 wmm
->wDevID
= modp
->wDeviceID
;
176 mciSetDriverData(wmm
->wDevID
, (DWORD_PTR
)wmm
);
177 modp
->wCustomCommandTable
= MCI_NO_COMMAND_TABLE
;
178 modp
->wType
= MCI_DEVTYPE_SEQUENCER
;
179 return modp
->wDeviceID
;
182 /**************************************************************************
183 * MCIMIDI_drvClose [internal]
185 static DWORD
MIDI_drvClose(DWORD dwDevID
)
187 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(dwDevID
);
190 HeapFree(GetProcessHeap(), 0, wmm
);
191 mciSetDriverData(dwDevID
, 0);
194 return (dwDevID
== 0xFFFFFFFF) ? 1 : 0;
197 /**************************************************************************
198 * MIDI_mciGetOpenDev [internal]
200 static WINE_MCIMIDI
* MIDI_mciGetOpenDev(MCIDEVICEID wDevID
, UINT wMsg
)
202 WINE_MCIMIDI
* wmm
= (WINE_MCIMIDI
*)mciGetDriverData(wDevID
);
204 if (wmm
== NULL
|| ((wmm
->nUseCount
== 0) ^ (wMsg
== MCI_OPEN_DRIVER
))) {
205 WARN("Invalid wDevID=%u\n", wDevID
);
211 /**************************************************************************
212 * MIDI_mciNotify [internal]
214 * Notifications in MCI work like a 1-element queue.
215 * Each new notification request supersedes the previous one.
216 * This affects Play and Record; other commands are immediate.
218 static void MIDI_mciNotify(DWORD_PTR hWndCallBack
, WINE_MCIMIDI
* wmm
, UINT wStatus
)
220 /* We simply save one parameter by not passing the wDevID local
221 * to the command. They are the same (via mciGetDriverData).
223 MCIDEVICEID wDevID
= wmm
->wDevID
;
224 HANDLE old
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
225 if (old
) mciDriverNotify(old
, wDevID
, MCI_NOTIFY_SUPERSEDED
);
226 mciDriverNotify(HWND_32(LOWORD(hWndCallBack
)), wDevID
, wStatus
);
229 /**************************************************************************
230 * MIDI_mciReadByte [internal]
232 static DWORD
MIDI_mciReadByte(WINE_MCIMIDI
* wmm
, BYTE
*lpbyt
)
237 mmioRead(wmm
->hFile
, (HPSTR
)lpbyt
, sizeof(BYTE
)) != (long)sizeof(BYTE
)) {
238 WARN("Error reading wmm=%p\n", wmm
);
239 ret
= MCIERR_INVALID_FILE
;
245 /**************************************************************************
246 * MIDI_mciReadWord [internal]
248 static DWORD
MIDI_mciReadWord(WINE_MCIMIDI
* wmm
, LPWORD lpw
)
251 DWORD ret
= MCIERR_INVALID_FILE
;
254 MIDI_mciReadByte(wmm
, &hibyte
) == 0 &&
255 MIDI_mciReadByte(wmm
, &lobyte
) == 0) {
256 *lpw
= ((WORD
)hibyte
<< 8) + lobyte
;
262 /**************************************************************************
263 * MIDI_mciReadLong [internal]
265 static DWORD
MIDI_mciReadLong(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
268 DWORD ret
= MCIERR_INVALID_FILE
;
271 MIDI_mciReadWord(wmm
, &hiword
) == 0 &&
272 MIDI_mciReadWord(wmm
, &loword
) == 0) {
273 *lpdw
= MAKELONG(loword
, hiword
);
279 /**************************************************************************
280 * MIDI_mciReadVaryLen [internal]
282 static WORD
MIDI_mciReadVaryLen(WINE_MCIMIDI
* wmm
, LPDWORD lpdw
)
289 ret
= MCIERR_INVALID_FILE
;
292 if (MIDI_mciReadByte(wmm
, &byte
) != 0) {
295 value
= (value
<< 7) + (byte
& 0x7F);
297 } while (byte
& 0x80);
300 TRACE("val=%08X\n", value);
306 /**************************************************************************
307 * MIDI_mciReadNextEvent [internal]
309 static DWORD
MIDI_mciReadNextEvent(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
317 if (mmioSeek(wmm
->hFile
, mmt
->dwIndex
, SEEK_SET
) != mmt
->dwIndex
) {
318 WARN("Can't seek at %08X\n", mmt
->dwIndex
);
319 return MCIERR_INVALID_FILE
;
321 evtLength
= MIDI_mciReadVaryLen(wmm
, &evtPulse
) + 1; /* > 0 */
322 MIDI_mciReadByte(wmm
, &b1
);
326 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
330 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
332 evtLength
+= MIDI_mciReadVaryLen(wmm
, &tmp
);
333 if (evtLength
>= 0x10000u
) {
334 /* this limitation shouldn't be a problem */
335 WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
338 hw
= LOWORD(evtLength
);
343 if (b1
& 0x80) { /* use running status ? */
344 mmt
->wLastCommand
= b1
;
345 MIDI_mciReadByte(wmm
, &b2
); evtLength
++;
348 b1
= mmt
->wLastCommand
;
350 switch ((b1
>> 4) & 0x07) {
351 case 0: case 1: case 2: case 3: case 6:
352 MIDI_mciReadByte(wmm
, &b3
); evtLength
++;
358 WARN("Strange indeed b1=0x%02x\n", b1
);
362 if (mmt
->dwIndex
+ evtLength
> mmt
->dwLast
)
363 return MCIERR_INTERNAL
;
365 mmt
->dwEventPulse
+= evtPulse
;
366 mmt
->dwEventData
= (hw
<< 16) + (b2
<< 8) + b1
;
367 mmt
->wEventLength
= evtLength
;
370 TRACE("[%u] => pulse=%08x(%08x), data=%08x, length=%u\n",
371 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
372 mmt->dwEventData, mmt->wEventLength);
377 /**************************************************************************
378 * MIDI_mciReadMTrk [internal]
380 static DWORD
MIDI_mciReadMTrk(WINE_MCIMIDI
* wmm
, MCI_MIDITRACK
* mmt
)
385 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
, (long)sizeof(FOURCC
)) !=
386 (long)sizeof(FOURCC
)) {
387 return MCIERR_INVALID_FILE
;
390 if (fourcc
!= mmioFOURCC('M', 'T', 'r', 'k')) {
391 WARN("Can't synchronize on 'MTrk' !\n");
392 return MCIERR_INVALID_FILE
;
395 if (MIDI_mciReadLong(wmm
, &toberead
) != 0) {
396 return MCIERR_INVALID_FILE
;
398 mmt
->dwFirst
= mmioSeek(wmm
->hFile
, 0, SEEK_CUR
); /* >= 0 */
399 mmt
->dwLast
= mmt
->dwFirst
+ toberead
;
401 /* compute # of pulses in this track */
402 mmt
->dwIndex
= mmt
->dwFirst
;
403 mmt
->dwEventPulse
= 0;
405 while (MIDI_mciReadNextEvent(wmm
, mmt
) == 0 && LOWORD(mmt
->dwEventData
) != 0x2FFF) {
409 mmt
->dwIndex
+= mmt
->wEventLength
;
411 switch (LOWORD(mmt
->dwEventData
)) {
414 /* position after meta data header */
415 mmioSeek(wmm
->hFile
, mmt
->dwIndex
+ HIWORD(mmt
->dwEventData
), SEEK_SET
);
416 len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
418 if (len
>= sizeof(buf
)) {
419 WARN("Buffer for text is too small (%u are needed)\n", len
);
420 len
= sizeof(buf
) - 1;
422 if (mmioRead(wmm
->hFile
, buf
, len
) == len
) {
423 buf
[len
] = 0; /* end string in case */
424 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
426 if (wmm
->lpstrCopyright
) {
427 WARN("Two copyright notices (%s|%s)\n", debugstr_w(wmm
->lpstrCopyright
), buf
);
429 len
= MultiByteToWideChar( CP_ACP
, 0, buf
, -1, NULL
, 0 );
430 wmm
->lpstrCopyright
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
431 MultiByteToWideChar( CP_ACP
, 0, buf
, -1, wmm
->lpstrCopyright
, len
);
435 if (wmm
->lpstrName
) {
436 WARN("Two names (%s|%s)\n", debugstr_w(wmm
->lpstrName
), buf
);
438 len
= MultiByteToWideChar( CP_ACP
, 0, buf
, -1, NULL
, 0 );
439 wmm
->lpstrName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
440 MultiByteToWideChar( CP_ACP
, 0, buf
, -1, wmm
->lpstrName
, len
);
448 mmt
->dwLength
= mmt
->dwEventPulse
;
450 TRACE("Track %u has %u bytes and %u pulses\n", mmt
->wTrackNr
, toberead
, mmt
->dwLength
);
452 /* reset track data */
453 mmt
->wStatus
= 1; /* ok, playing */
454 mmt
->dwIndex
= mmt
->dwFirst
;
455 mmt
->dwEventPulse
= 0;
457 if (mmioSeek(wmm
->hFile
, 0, SEEK_CUR
) != mmt
->dwLast
) {
458 WARN("Ouch, out of sync seek=%u track=%u\n",
459 mmioSeek(wmm
->hFile
, 0, SEEK_CUR
), mmt
->dwLast
);
460 /* position at end of this track, to be ready to read next track */
461 mmioSeek(wmm
->hFile
, mmt
->dwLast
, SEEK_SET
);
467 /**************************************************************************
468 * MIDI_mciReadMThd [internal]
470 static DWORD
MIDI_mciReadMThd(WINE_MCIMIDI
* wmm
, DWORD dwOffset
)
476 TRACE("(%p, %08X);\n", wmm
, dwOffset
);
478 if (mmioSeek(wmm
->hFile
, dwOffset
, SEEK_SET
) != dwOffset
) {
479 WARN("Can't seek at %08X begin of 'MThd'\n", dwOffset
);
480 return MCIERR_INVALID_FILE
;
482 if (mmioRead(wmm
->hFile
, (HPSTR
)&fourcc
,
483 (long) sizeof(FOURCC
)) != (long) sizeof(FOURCC
))
484 return MCIERR_INVALID_FILE
;
486 if (fourcc
!= mmioFOURCC('M', 'T', 'h', 'd')) {
487 WARN("Can't synchronize on 'MThd' !\n");
488 return MCIERR_INVALID_FILE
;
491 if (MIDI_mciReadLong(wmm
, &toberead
) != 0 || toberead
< 3 * sizeof(WORD
))
492 return MCIERR_INVALID_FILE
;
494 if (MIDI_mciReadWord(wmm
, &wmm
->wFormat
) != 0 ||
495 MIDI_mciReadWord(wmm
, &wmm
->nTracks
) != 0 ||
496 MIDI_mciReadWord(wmm
, &wmm
->nDivision
) != 0) {
497 return MCIERR_INVALID_FILE
;
500 TRACE("toberead=0x%08X, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
501 toberead
, wmm
->wFormat
, wmm
->nTracks
, wmm
->nDivision
);
503 /* MS doc says that the MIDI MCI time format must be put by default to the format
504 * stored in the MIDI file...
506 if (wmm
->nDivision
> 0x8000) {
507 /* eric.pouech@lemel.fr 98/11
508 * I did not check this very code (pulses are expressed as SMPTE sub-frames).
509 * In about 40 MB of MIDI files I have, none was SMPTE based...
510 * I'm just wondering if this is widely used :-). So, if someone has one of
511 * these files, I'd like to know about it.
513 FIXME("Handling SMPTE time in MIDI files has not been tested\n"
514 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
516 switch (HIBYTE(wmm
->nDivision
)) {
517 case 0xE8: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
; break; /* -24 */
518 case 0xE7: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
; break; /* -25 */
519 case 0xE3: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
520 case 0xE2: wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
; break; /* -30 */
522 WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm
->nDivision
));
523 return MCIERR_INVALID_FILE
;
525 switch (LOBYTE(wmm
->nDivision
)) {
526 case 4: /* MIDI Time Code */
529 case 80: /* SMPTE bit resolution */
532 WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm
->nDivision
));
533 return MCIERR_INVALID_FILE
;
535 } else if (wmm
->nDivision
== 0) {
536 WARN("Number of division is 0, can't support that !!\n");
537 return MCIERR_INVALID_FILE
;
539 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
542 switch (wmm
->wFormat
) {
544 if (wmm
->nTracks
!= 1) {
545 WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
553 WARN("Handling MIDI files which format = %d is not (yet) supported\n"
554 "Please report with MIDI file !\n", wmm
->wFormat
);
555 return MCIERR_INVALID_FILE
;
558 if (wmm
->nTracks
& 0x8000) {
559 /* this shouldn't be a problem... */
560 WARN("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
561 wmm
->nTracks
= 0x7FFF;
564 if ((wmm
->tracks
= HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK
) * wmm
->nTracks
)) == NULL
) {
565 return MCIERR_OUT_OF_MEMORY
;
568 toberead
-= 3 * sizeof(WORD
);
570 TRACE("Size of MThd > 6, skipping %d extra bytes\n", toberead
);
571 mmioSeek(wmm
->hFile
, toberead
, SEEK_CUR
);
574 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
575 wmm
->tracks
[nt
].wTrackNr
= nt
;
576 if (MIDI_mciReadMTrk(wmm
, &wmm
->tracks
[nt
]) != 0) {
577 WARN("Can't read 'MTrk' header\n");
578 return MCIERR_INVALID_FILE
;
582 wmm
->dwTempo
= 500000;
587 /**************************************************************************
588 * MIDI_ConvertPulseToMS [internal]
590 static DWORD
MIDI_ConvertPulseToMS(WINE_MCIMIDI
* wmm
, DWORD pulse
)
594 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
595 * may change during file playing
597 if (wmm
->nDivision
== 0) {
598 FIXME("Shouldn't happen. wmm->nDivision = 0\n");
599 } else if (wmm
->nDivision
> 0x8000) { /* SMPTE, unchecked FIXME? */
600 int nf
= -(char)HIBYTE(wmm
->nDivision
); /* number of frames */
601 int nsf
= LOBYTE(wmm
->nDivision
); /* number of sub-frames */
602 ret
= (pulse
* 1000) / (nf
* nsf
);
604 ret
= (DWORD
)((double)pulse
* ((double)wmm
->dwTempo
/ 1000) /
605 (double)wmm
->nDivision
);
609 TRACE("pulse=%u tempo=%u division=%u=0x%04x => ms=%u\n",
610 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
616 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
617 #define TIME_MS_IN_ONE_MINUTE (60*1000)
618 #define TIME_MS_IN_ONE_SECOND (1000)
620 /**************************************************************************
621 * MIDI_ConvertTimeFormatToMS [internal]
623 static DWORD
MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI
* wmm
, DWORD val
)
627 switch (wmm
->dwMciTimeFormat
) {
628 case MCI_FORMAT_MILLISECONDS
:
631 case MCI_FORMAT_SMPTE_24
:
633 (HIBYTE(HIWORD(val
)) * 125) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
634 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
636 case MCI_FORMAT_SMPTE_25
:
638 HIBYTE(HIWORD(val
)) * 40 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
639 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
641 case MCI_FORMAT_SMPTE_30
:
643 (HIBYTE(HIWORD(val
)) * 100) / 3 + LOBYTE(HIWORD(val
)) * TIME_MS_IN_ONE_SECOND
+
644 HIBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_MINUTE
+ LOBYTE(LOWORD(val
)) * TIME_MS_IN_ONE_HOUR
;
647 WARN("Bad time format %u!\n", wmm
->dwMciTimeFormat
);
650 TRACE("val=%u=0x%08x [tf=%u] => ret=%u\n", val, val, wmm->dwMciTimeFormat, ret);
655 /**************************************************************************
656 * MIDI_ConvertMSToTimeFormat [internal]
658 static DWORD
MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI
* wmm
, DWORD _val
)
660 DWORD ret
= 0, val
= _val
;
663 switch (wmm
->dwMciTimeFormat
) {
664 case MCI_FORMAT_MILLISECONDS
:
667 case MCI_FORMAT_SMPTE_24
:
668 case MCI_FORMAT_SMPTE_25
:
669 case MCI_FORMAT_SMPTE_30
:
670 h
= val
/ TIME_MS_IN_ONE_HOUR
;
671 m
= (val
-= h
* TIME_MS_IN_ONE_HOUR
) / TIME_MS_IN_ONE_MINUTE
;
672 s
= (val
-= m
* TIME_MS_IN_ONE_MINUTE
) / TIME_MS_IN_ONE_SECOND
;
673 switch (wmm
->dwMciTimeFormat
) {
674 case MCI_FORMAT_SMPTE_24
:
675 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
676 f
= (val
* 3) / 125; val
-= (f
* 125) / 3;
678 case MCI_FORMAT_SMPTE_25
:
679 /* one frame is 1000/25 ms long, 1000/25 == 40 */
680 f
= val
/ 40; val
-= f
* 40;
682 case MCI_FORMAT_SMPTE_30
:
683 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
684 f
= (val
* 3) / 100; val
-= (f
* 100) / 3;
687 FIXME("There must be some bad bad programmer\n");
690 /* val contains the number of ms which cannot make a complete frame */
691 /* FIXME: is this correct ? programs seem to be happy with that */
692 ret
= (f
<< 24) | (s
<< 16) | (m
<< 8) | (h
<< 0);
695 WARN("Bad time format %u!\n", wmm
->dwMciTimeFormat
);
698 TRACE("val=%u [tf=%u] => ret=%u=0x%08x\n", _val, wmm->dwMciTimeFormat, ret, ret);
703 /**************************************************************************
704 * MIDI_GetMThdLengthMS [internal]
706 static DWORD
MIDI_GetMThdLengthMS(WINE_MCIMIDI
* wmm
)
711 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
712 if (wmm
->wFormat
== 2) {
713 ret
+= wmm
->tracks
[nt
].dwLength
;
714 } else if (wmm
->tracks
[nt
].dwLength
> ret
) {
715 ret
= wmm
->tracks
[nt
].dwLength
;
718 /* FIXME: this is wrong if there is a tempo change inside the file */
719 return MIDI_ConvertPulseToMS(wmm
, ret
);
722 /**************************************************************************
723 * MIDI_mciOpen [internal]
725 static DWORD
MIDI_mciOpen(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_OPEN_PARMSW lpParms
)
729 TRACE("(%d, %08X, %p)\n", wmm
->wDevID
, dwFlags
, lpParms
);
731 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
732 if (dwFlags
& MCI_OPEN_SHAREABLE
)
733 return MCIERR_HARDWARE
;
735 if (wmm
->nUseCount
> 0) {
736 /* The driver is already opened on this channel
737 * MIDI sequencer cannot be shared
739 return MCIERR_DEVICE_OPEN
;
745 wmm
->wPort
= MIDI_MAPPER
;
746 wmm
->lpstrElementName
= NULL
;
748 TRACE("wDevID=%d (lpParams->wDeviceID=%d)\n", wmm
->wDevID
, lpParms
->wDeviceID
);
749 /* lpParms->wDeviceID = wDevID;*/
751 if (dwFlags
& MCI_OPEN_ELEMENT
) {
752 TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpParms
->lpstrElementName
));
753 if (lpParms
->lpstrElementName
&& strlenW(lpParms
->lpstrElementName
) > 0) {
754 wmm
->hFile
= mmioOpenW((LPWSTR
)lpParms
->lpstrElementName
, NULL
,
755 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
756 if (wmm
->hFile
== 0) {
757 WARN("Can't find file %s!\n", debugstr_w(lpParms
->lpstrElementName
));
759 return MCIERR_FILE_NOT_FOUND
;
761 wmm
->lpstrElementName
= HeapAlloc(GetProcessHeap(), 0,
762 (strlenW(lpParms
->lpstrElementName
) + 1) * sizeof(WCHAR
));
763 strcpyW(wmm
->lpstrElementName
, lpParms
->lpstrElementName
);
766 TRACE("hFile=%p\n", wmm
->hFile
);
768 wmm
->lpstrCopyright
= NULL
;
769 wmm
->lpstrName
= NULL
;
771 wmm
->dwStatus
= MCI_MODE_NOT_READY
; /* while loading file contents */
772 /* spec says it should be the default format from the MIDI file... */
773 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
775 if (wmm
->hFile
!= 0) {
780 if (mmioDescend(wmm
->hFile
, &ckMainRIFF
, NULL
, 0) != 0) {
781 dwRet
= MCIERR_INVALID_FILE
;
783 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
784 (LPSTR
)&ckMainRIFF
.ckid
, (LPSTR
)&ckMainRIFF
.fccType
, ckMainRIFF
.cksize
);
786 if (ckMainRIFF
.ckid
== FOURCC_RIFF
&& ckMainRIFF
.fccType
== mmioFOURCC('R', 'M', 'I', 'D')) {
787 mmckInfo
.ckid
= mmioFOURCC('d', 'a', 't', 'a');
788 mmioSeek(wmm
->hFile
, ckMainRIFF
.dwDataOffset
+ ((ckMainRIFF
.cksize
+ 1) & ~1), SEEK_SET
);
789 if (mmioDescend(wmm
->hFile
, &mmckInfo
, &ckMainRIFF
, MMIO_FINDCHUNK
) == 0) {
790 TRACE("... is a 'RMID' file\n");
791 dwOffset
= mmckInfo
.dwDataOffset
;
793 dwRet
= MCIERR_INVALID_FILE
;
796 if (dwRet
== 0 && MIDI_mciReadMThd(wmm
, dwOffset
) != 0) {
797 WARN("Can't read 'MThd' header\n");
798 dwRet
= MCIERR_INVALID_FILE
;
802 TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
810 mmioClose(wmm
->hFile
, 0);
813 wmm
->dwPositionMS
= 0;
814 wmm
->dwStatus
= MCI_MODE_STOP
;
815 if (dwFlags
& MCI_NOTIFY
)
816 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
821 /**************************************************************************
822 * MIDI_mciStop [internal]
824 static DWORD
MIDI_mciStop(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
828 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
830 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
831 HANDLE old
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
832 if (old
) mciDriverNotify(old
, wmm
->wDevID
, MCI_NOTIFY_ABORTED
);
835 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
836 int oldstat
= wmm
->dwStatus
;
838 wmm
->dwStatus
= MCI_MODE_NOT_READY
;
839 if (oldstat
== MCI_MODE_PAUSE
)
840 dwRet
= midiOutReset((HMIDIOUT
)wmm
->hMidi
);
842 while (wmm
->dwStatus
!= MCI_MODE_STOP
)
847 wmm
->dwStatus
= MCI_MODE_STOP
;
849 if ((dwFlags
& MCI_NOTIFY
) && lpParms
&& MMSYSERR_NOERROR
==dwRet
)
850 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
854 /**************************************************************************
855 * MIDI_mciClose [internal]
857 static DWORD
MIDI_mciClose(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
860 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
862 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
863 /* mciStop handles MCI_NOTIFY_ABORTED */
864 MIDI_mciStop(wmm
, MCI_WAIT
, lpParms
);
868 if (wmm
->nUseCount
== 0) {
869 if (wmm
->hFile
!= 0) {
870 mmioClose(wmm
->hFile
, 0);
872 TRACE("hFile closed !\n");
874 HeapFree(GetProcessHeap(), 0, wmm
->tracks
);
875 HeapFree(GetProcessHeap(), 0, wmm
->lpstrElementName
);
876 HeapFree(GetProcessHeap(), 0, wmm
->lpstrCopyright
);
877 HeapFree(GetProcessHeap(), 0, wmm
->lpstrName
);
879 TRACE("Shouldn't happen... nUseCount=%d\n", wmm
->nUseCount
);
880 return MCIERR_INTERNAL
;
883 if ((dwFlags
& MCI_NOTIFY
) && lpParms
) {
884 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
889 /**************************************************************************
890 * MIDI_mciFindNextEvent [internal]
892 static MCI_MIDITRACK
* MIDI_mciFindNextEvent(WINE_MCIMIDI
* wmm
, LPDWORD hiPulse
)
897 *hiPulse
= 0xFFFFFFFFul
;
899 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
900 mmt
= &wmm
->tracks
[nt
];
902 if (mmt
->wStatus
== 0)
904 if (mmt
->dwEventPulse
< *hiPulse
) {
905 *hiPulse
= mmt
->dwEventPulse
;
909 return (cnt
== 0xFFFFu
) ? 0 /* no more event on all tracks */
913 /**************************************************************************
914 * MIDI_mciPlay [internal]
916 static DWORD
MIDI_mciPlay(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_PLAY_PARMS lpParms
)
918 DWORD dwStartMS
, dwEndMS
;
925 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
927 if (wmm
->hFile
== 0) {
928 WARN("Can't play: no file %s!\n", debugstr_w(wmm
->lpstrElementName
));
929 return MCIERR_FILE_NOT_FOUND
;
932 if (wmm
->dwStatus
!= MCI_MODE_STOP
) {
933 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
934 /* FIXME: parameters (start/end) in lpParams may not be used */
935 return MIDI_mciResume(wmm
, dwFlags
, (LPMCI_GENERIC_PARMS
)lpParms
);
937 WARN("Can't play: device is not stopped !\n");
938 return MCIERR_INTERNAL
;
941 if (!(dwFlags
& MCI_WAIT
)) {
942 return MCI_SendCommandAsync(wmm
->wDevID
, MCI_PLAY
, dwFlags
, (DWORD_PTR
)lpParms
, sizeof(MCI_PLAY_PARMS
));
945 if (lpParms
&& (dwFlags
& MCI_FROM
)) {
946 dwStartMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwFrom
);
948 dwStartMS
= wmm
->dwPositionMS
;
951 if (lpParms
&& (dwFlags
& MCI_TO
)) {
952 dwEndMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
954 dwEndMS
= 0xFFFFFFFFul
;
957 TRACE("Playing from %u to %u\n", dwStartMS
, dwEndMS
);
959 oldcb
= InterlockedExchangePointer(&wmm
->hCallback
,
960 (dwFlags
& MCI_NOTIFY
) ? HWND_32(LOWORD(lpParms
->dwCallback
)) : NULL
);
961 if (oldcb
) mciDriverNotify(oldcb
, wmm
->wDevID
, MCI_NOTIFY_ABORTED
);
965 for (nt
= 0; nt
< wmm
->nTracks
; nt
++) {
966 mmt
= &wmm
->tracks
[nt
];
968 mmt
->wStatus
= 1; /* ok, playing */
969 mmt
->dwIndex
= mmt
->dwFirst
;
970 if (wmm
->wFormat
== 2 && nt
> 0) {
971 mmt
->dwEventPulse
= wmm
->tracks
[nt
- 1].dwLength
;
973 mmt
->dwEventPulse
= 0;
975 MIDI_mciReadNextEvent(wmm
, mmt
); /* FIXME == 0 */
978 dwRet
= midiOutOpen((LPHMIDIOUT
)&wmm
->hMidi
, wmm
->wPort
, 0L, 0L, CALLBACK_NULL
);
979 if (dwRet
!= MMSYSERR_NOERROR
) {
984 wmm
->dwTempo
= 500000;
985 wmm
->dwStatus
= MCI_MODE_PLAY
;
986 wmm
->dwPositionMS
= 0;
987 wmm
->wStartedPlaying
= FALSE
;
989 while (wmm
->dwStatus
!= MCI_MODE_STOP
&& wmm
->dwStatus
!= MCI_MODE_NOT_READY
) {
990 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
991 * too much. Tell gcc not to optimize status value using volatile.
993 while (((volatile WINE_MCIMIDI
*)wmm
)->dwStatus
== MCI_MODE_PAUSE
);
995 doPlay
= (wmm
->dwPositionMS
>= dwStartMS
&& wmm
->dwPositionMS
<= dwEndMS
);
997 TRACE("wmm->dwStatus=%d, doPlay=%c\n", wmm
->dwStatus
, doPlay
? 'T' : 'F');
999 if ((mmt
= MIDI_mciFindNextEvent(wmm
, &hiPulse
)) == NULL
)
1000 break; /* no more event on tracks */
1002 /* if starting playing, then set StartTicks to the value it would have had
1003 * if play had started at position 0
1005 if (doPlay
&& !wmm
->wStartedPlaying
) {
1006 wmm
->dwStartTicks
= GetTickCount() - MIDI_ConvertPulseToMS(wmm
, wmm
->dwPulse
);
1007 wmm
->wStartedPlaying
= TRUE
;
1008 TRACE("Setting dwStartTicks to %u\n", wmm
->dwStartTicks
);
1011 if (hiPulse
> wmm
->dwPulse
) {
1012 wmm
->dwPositionMS
+= MIDI_ConvertPulseToMS(wmm
, hiPulse
- wmm
->dwPulse
);
1014 DWORD togo
= wmm
->dwStartTicks
+ wmm
->dwPositionMS
;
1015 DWORD tc
= GetTickCount();
1017 TRACE("Pulses hi=0x%08x <> cur=0x%08x\n", hiPulse
, wmm
->dwPulse
);
1018 TRACE("Wait until %u => %u ms\n",
1019 tc
- wmm
->dwStartTicks
, togo
- wmm
->dwStartTicks
);
1023 wmm
->dwPulse
= hiPulse
;
1026 switch (LOBYTE(LOWORD(mmt
->dwEventData
))) {
1028 case 0xF7: /* sysex events */
1030 FIXME("Not handling SysEx events (yet)\n");
1034 /* position after meta data header */
1035 mmioSeek(wmm
->hFile
, mmt
->dwIndex
+ HIWORD(mmt
->dwEventData
), SEEK_SET
);
1036 switch (HIBYTE(LOWORD(mmt
->dwEventData
))) {
1037 case 0x00: /* 16-bit sequence number */
1038 if (TRACE_ON(mcimidi
)) {
1041 MIDI_mciReadWord(wmm
, &twd
); /* == 0 */
1042 TRACE("Got sequence number %u\n", twd
);
1045 case 0x01: /* any text */
1046 case 0x02: /* Copyright Message text */
1047 case 0x03: /* Sequence/Track Name text */
1048 case 0x04: /* Instrument Name text */
1049 case 0x05: /* Lyric text */
1050 case 0x06: /* Marker text */
1051 case 0x07: /* Cue-point text */
1052 if (TRACE_ON(mcimidi
)) {
1054 WORD len
= mmt
->wEventLength
- HIWORD(mmt
->dwEventData
);
1055 static const char* const info
[8] = {"", "Text", "Copyright", "Seq/Trk name",
1056 "Instrument", "Lyric", "Marker", "Cue-point"};
1057 WORD idx
= HIBYTE(LOWORD(mmt
->dwEventData
));
1059 if (len
>= sizeof(buf
)) {
1060 WARN("Buffer for text is too small (%u are needed)\n", len
);
1061 len
= sizeof(buf
) - 1;
1063 if (mmioRead(wmm
->hFile
, buf
, len
) == len
) {
1064 buf
[len
] = 0; /* end string in case */
1065 TRACE("%s => \"%s\"\n", (idx
< 8 ) ? info
[idx
] : "", buf
);
1067 WARN("Couldn't read data for %s\n", (idx
< 8) ? info
[idx
] : "");
1072 /* MIDI channel (cc) */
1073 if (FIXME_ON(mcimidi
)) {
1076 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
1077 FIXME("NIY: MIDI channel=%u, track=%u\n", bt
, mmt
->wTrackNr
);
1081 /* MIDI port (pp) */
1082 if (FIXME_ON(mcimidi
)) {
1085 MIDI_mciReadByte(wmm
, &bt
); /* == 0 */
1086 FIXME("NIY: MIDI port=%u, track=%u\n", bt
, mmt
->wTrackNr
);
1089 case 0x2F: /* end of track */
1092 case 0x51:/* set tempo */
1093 /* Tempo is expressed in -seconds per midi quarter note
1094 * for format 1 MIDI files, this can only be present on track #0
1096 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
1097 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt
->wTrackNr
);
1102 MIDI_mciReadByte(wmm
, &tbt
); value
= ((DWORD
)tbt
) << 16;
1103 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 8;
1104 MIDI_mciReadByte(wmm
, &tbt
); value
|= ((DWORD
)tbt
) << 0;
1105 TRACE("Setting tempo to %d (BPM=%d)\n", wmm
->dwTempo
, (value
) ? (60000000 / value
) : 0);
1106 wmm
->dwTempo
= value
;
1109 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
1110 if (mmt
->wTrackNr
!= 0 && wmm
->wFormat
== 1) {
1111 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt
->wTrackNr
);
1112 } if (mmt
->dwEventPulse
!= 0) {
1113 WARN("SMPTE track start can only be expressed at start of track (%u)\n", mmt
->dwEventPulse
);
1115 BYTE h
, m
, s
, f
, ff
;
1117 MIDI_mciReadByte(wmm
, &h
);
1118 MIDI_mciReadByte(wmm
, &m
);
1119 MIDI_mciReadByte(wmm
, &s
);
1120 MIDI_mciReadByte(wmm
, &f
);
1121 MIDI_mciReadByte(wmm
, &ff
);
1122 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h
, m
, s
, f
, ff
);
1125 case 0x58: /* file rhythm */
1126 if (TRACE_ON(mcimidi
)) {
1127 BYTE num
, den
, cpmc
, _32npqn
;
1129 MIDI_mciReadByte(wmm
, &num
);
1130 MIDI_mciReadByte(wmm
, &den
); /* to notate e.g. 6/8 */
1131 MIDI_mciReadByte(wmm
, &cpmc
); /* number of MIDI clocks per metronome click */
1132 MIDI_mciReadByte(wmm
, &_32npqn
); /* number of notated 32nd notes per MIDI quarter note */
1134 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num
, 1 << den
, cpmc
, _32npqn
);
1137 case 0x59: /* key signature */
1138 if (TRACE_ON(mcimidi
)) {
1141 MIDI_mciReadByte(wmm
, &sf
);
1142 MIDI_mciReadByte(wmm
, &mm
);
1144 if (sf
>= 0x80) TRACE("%d flats\n", -(char)sf
);
1145 else if (sf
> 0) TRACE("%d sharps\n", (char)sf
);
1146 else TRACE("Key of C\n");
1147 TRACE("Mode: %s\n", (mm
== 0) ? "major" : "minor");
1151 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt
->dwEventData
)));
1157 dwRet
= midiOutShortMsg((HMIDIOUT
)wmm
->hMidi
, mmt
->dwEventData
);
1159 switch (LOBYTE(LOWORD(mmt
->dwEventData
)) & 0xF0) {
1165 dwRet
= midiOutShortMsg((HMIDIOUT
)wmm
->hMidi
, mmt
->dwEventData
);
1169 mmt
->dwIndex
+= mmt
->wEventLength
;
1170 if (mmt
->dwIndex
< mmt
->dwFirst
|| mmt
->dwIndex
>= mmt
->dwLast
) {
1174 MIDI_mciReadNextEvent(wmm
, mmt
);
1178 midiOutReset((HMIDIOUT
)wmm
->hMidi
);
1180 dwRet
= midiOutClose((HMIDIOUT
)wmm
->hMidi
);
1182 if (dwFlags
& MCI_NOTIFY
)
1183 oldcb
= InterlockedExchangePointer(&wmm
->hCallback
, NULL
);
1185 wmm
->dwStatus
= MCI_MODE_STOP
;
1187 /* Let the potentially asynchronous commands support FAILURE notification. */
1188 if (oldcb
) mciDriverNotify(oldcb
, wmm
->wDevID
,
1189 dwRet
? MCI_NOTIFY_FAILURE
: MCI_NOTIFY_SUCCESSFUL
);
1193 /**************************************************************************
1194 * MIDI_mciPause [internal]
1196 static DWORD
MIDI_mciPause(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1198 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1200 if (wmm
->dwStatus
== MCI_MODE_PLAY
) {
1201 /* stop all notes */
1203 for (chn
= 0; chn
< 16; chn
++)
1204 midiOutShortMsg((HMIDIOUT
)(wmm
->hMidi
), 0x78B0 | chn
);
1205 wmm
->dwStatus
= MCI_MODE_PAUSE
;
1207 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1208 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1212 /**************************************************************************
1213 * MIDI_mciResume [internal]
1215 static DWORD
MIDI_mciResume(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1217 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1219 if (wmm
->dwStatus
== MCI_MODE_PAUSE
) {
1220 wmm
->wStartedPlaying
= FALSE
;
1221 wmm
->dwStatus
= MCI_MODE_PLAY
;
1223 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1224 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1228 /**************************************************************************
1229 * MIDI_mciSet [internal]
1231 static DWORD
MIDI_mciSet(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_SEQ_SET_PARMS lpParms
)
1233 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1235 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1237 if (dwFlags
& MCI_SET_TIME_FORMAT
) {
1238 switch (lpParms
->dwTimeFormat
) {
1239 case MCI_FORMAT_MILLISECONDS
:
1240 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1241 wmm
->dwMciTimeFormat
= MCI_FORMAT_MILLISECONDS
;
1243 case MCI_FORMAT_SMPTE_24
:
1244 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1245 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_24
;
1247 case MCI_FORMAT_SMPTE_25
:
1248 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1249 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_25
;
1251 case MCI_FORMAT_SMPTE_30
:
1252 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1253 wmm
->dwMciTimeFormat
= MCI_FORMAT_SMPTE_30
;
1256 WARN("Bad time format %u!\n", lpParms
->dwTimeFormat
);
1257 return MCIERR_BAD_TIME_FORMAT
;
1260 if (dwFlags
& MCI_SET_VIDEO
) {
1261 TRACE("No support for video !\n");
1262 return MCIERR_UNSUPPORTED_FUNCTION
;
1264 if (dwFlags
& MCI_SET_DOOR_OPEN
) {
1265 TRACE("No support for door open !\n");
1266 return MCIERR_UNSUPPORTED_FUNCTION
;
1268 if (dwFlags
& MCI_SET_DOOR_CLOSED
) {
1269 TRACE("No support for door close !\n");
1270 return MCIERR_UNSUPPORTED_FUNCTION
;
1272 if (dwFlags
& MCI_SET_AUDIO
) {
1273 if (dwFlags
& MCI_SET_ON
) {
1274 TRACE("MCI_SET_ON audio !\n");
1275 } else if (dwFlags
& MCI_SET_OFF
) {
1276 TRACE("MCI_SET_OFF audio !\n");
1278 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1279 return MCIERR_BAD_INTEGER
;
1282 switch (lpParms
->dwAudio
)
1284 case MCI_SET_AUDIO_ALL
: TRACE("MCI_SET_AUDIO_ALL !\n"); break;
1285 case MCI_SET_AUDIO_LEFT
: TRACE("MCI_SET_AUDIO_LEFT !\n"); break;
1286 case MCI_SET_AUDIO_RIGHT
: TRACE("MCI_SET_AUDIO_RIGHT !\n"); break;
1287 default: WARN("Unknown audio channel %u\n", lpParms
->dwAudio
); break;
1291 if (dwFlags
& MCI_SEQ_SET_MASTER
)
1292 TRACE("MCI_SEQ_SET_MASTER !\n");
1293 if (dwFlags
& MCI_SEQ_SET_SLAVE
)
1294 TRACE("MCI_SEQ_SET_SLAVE !\n");
1295 if (dwFlags
& MCI_SEQ_SET_OFFSET
)
1296 TRACE("MCI_SEQ_SET_OFFSET !\n");
1297 if (dwFlags
& MCI_SEQ_SET_PORT
) {
1298 TRACE("MCI_SEQ_SET_PORT = %d\n", lpParms
->dwPort
);
1299 if ((UINT16
)lpParms
->dwPort
!= (UINT16
)MIDI_MAPPER
&&
1300 (UINT16
)lpParms
->dwPort
>= midiOutGetNumDevs())
1301 /* FIXME: input/output port distinction? */
1302 return MCIERR_SEQ_PORT_NONEXISTENT
;
1303 /* FIXME: Native manages to swap the device while playing! */
1304 wmm
->wPort
= lpParms
->dwPort
;
1306 if (dwFlags
& MCI_SEQ_SET_TEMPO
)
1307 TRACE("MCI_SEQ_SET_TEMPO !\n");
1308 if (dwFlags
& MCI_NOTIFY
)
1309 MIDI_mciNotify(lpParms
->dwCallback
, wmm
, MCI_NOTIFY_SUCCESSFUL
);
1313 /**************************************************************************
1314 * MIDI_mciStatus [internal]
1316 static DWORD
MIDI_mciStatus(WINE_MCIMIDI
* wmm
, DWORD dwFlags
, LPMCI_STATUS_PARMS lpParms
)
1320 TRACE("(%d, %08X, %p);\n", wmm
->wDevID
, dwFlags
, lpParms
);
1322 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1324 if (dwFlags
& MCI_STATUS_ITEM
) {
1325 switch (lpParms
->dwItem
) {
1326 case MCI_STATUS_CURRENT_TRACK
:
1327 /* FIXME in Format 2 */
1328 lpParms
->dwReturn
= 1;
1329 TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms
->dwReturn
);
1331 case MCI_STATUS_LENGTH
:
1332 if ((dwFlags
& MCI_TRACK
) && wmm
->wFormat
== 2) {
1333 if (lpParms
->dwTrack
>= wmm
->nTracks
)
1334 return MCIERR_BAD_INTEGER
;
1335 /* FIXME: this is wrong if there is a tempo change inside the file */
1336 lpParms
->dwReturn
= MIDI_ConvertPulseToMS(wmm
, wmm
->tracks
[lpParms
->dwTrack
].dwLength
);
1338 lpParms
->dwReturn
= MIDI_GetMThdLengthMS(wmm
);
1340 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
, lpParms
->dwReturn
);
1341 TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms
->dwReturn
);
1343 case MCI_STATUS_MODE
:
1344 TRACE("MCI_STATUS_MODE => %u\n", wmm
->dwStatus
);
1345 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwStatus
, wmm
->dwStatus
);
1346 ret
= MCI_RESOURCE_RETURNED
;
1348 case MCI_STATUS_MEDIA_PRESENT
:
1349 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1350 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1351 ret
= MCI_RESOURCE_RETURNED
;
1353 case MCI_STATUS_NUMBER_OF_TRACKS
:
1354 lpParms
->dwReturn
= (wmm
->wFormat
== 2) ? wmm
->nTracks
: 1;
1355 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms
->dwReturn
);
1357 case MCI_STATUS_POSITION
:
1358 /* FIXME: do I need to use MCI_TRACK ? */
1359 lpParms
->dwReturn
= MIDI_ConvertMSToTimeFormat(wmm
,
1360 (dwFlags
& MCI_STATUS_START
) ? 0 : wmm
->dwPositionMS
);
1361 TRACE("MCI_STATUS_POSITION %s => %lu\n",
1362 (dwFlags
& MCI_STATUS_START
) ? "start" : "current", lpParms
->dwReturn
);
1364 case MCI_STATUS_READY
:
1365 lpParms
->dwReturn
= (wmm
->dwStatus
== MCI_MODE_NOT_READY
) ?
1366 MAKEMCIRESOURCE(FALSE
, MCI_FALSE
) : MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
1367 ret
= MCI_RESOURCE_RETURNED
;
1368 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms
->dwReturn
));
1370 case MCI_STATUS_TIME_FORMAT
:
1371 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmm
->dwMciTimeFormat
, MCI_FORMAT_RETURN_BASE
+ wmm
->dwMciTimeFormat
);
1372 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms
->dwReturn
));
1373 ret
= MCI_RESOURCE_RETURNED
;
1375 case MCI_SEQ_STATUS_DIVTYPE
:
1376 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1377 if (wmm
->nDivision
> 0x8000) {
1378 switch (wmm
->nDivision
) {
1379 case 0xE8: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_24
; break; /* -24 */
1380 case 0xE7: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_25
; break; /* -25 */
1381 case 0xE3: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30DROP
; break; /* -29 */ /* is the MCI constant correct ? */
1382 case 0xE2: lpParms
->dwReturn
= MCI_SEQ_DIV_SMPTE_30
; break; /* -30 */
1383 default: FIXME("There is a bad bad programmer\n");
1386 lpParms
->dwReturn
= MCI_SEQ_DIV_PPQN
;
1388 lpParms
->dwReturn
= MAKEMCIRESOURCE(lpParms
->dwReturn
,lpParms
->dwReturn
);
1389 ret
= MCI_RESOURCE_RETURNED
;
1391 case MCI_SEQ_STATUS_MASTER
:
1392 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1393 lpParms
->dwReturn
= 0;
1395 case MCI_SEQ_STATUS_SLAVE
:
1396 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1397 lpParms
->dwReturn
= 0;
1399 case MCI_SEQ_STATUS_OFFSET
:
1400 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1401 lpParms
->dwReturn
= 0;
1403 case MCI_SEQ_STATUS_PORT
:
1404 if (wmm
->wPort
!= (UINT16
)MIDI_MAPPER
)
1405 lpParms
->dwReturn
= wmm
->wPort
;
1407 lpParms
->dwReturn
= MAKEMCIRESOURCE(MIDI_MAPPER
, MCI_SEQ_MAPPER_S
);
1408 ret
= MCI_RESOURCE_RETURNED
;
1410 TRACE("MCI_SEQ_STATUS_PORT (%u) => %d\n", wmm
->wDevID
, wmm
->wPort
);
1412 case MCI_SEQ_STATUS_TEMPO
:
1413 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1414 lpParms
->dwReturn
= wmm
->dwTempo
;
1417 FIXME("Unknown command %08X !\n", lpParms
->dwItem
);
1418 return MCIERR_UNRECOGNIZED_COMMAND
;
1421 WARN("No Status-Item!\n");
1422 return MCIERR_UNRECOGNIZED_COMMAND
;
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_UNRECOGNIZED_COMMAND
;
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
) {
1549 ret
= MCIERR_NULL_PARAMETER_BLOCK
;
1551 MIDI_mciStop(wmm
, MCI_WAIT
, 0);
1553 if (dwFlags
& MCI_SEEK_TO_START
) {
1554 wmm
->dwPositionMS
= 0;
1555 } else if (dwFlags
& MCI_SEEK_TO_END
) {
1556 wmm
->dwPositionMS
= 0xFFFFFFFF; /* FIXME */
1557 } else if (dwFlags
& MCI_TO
) {
1558 wmm
->dwPositionMS
= MIDI_ConvertTimeFormatToMS(wmm
, lpParms
->dwTo
);
1560 WARN("dwFlag doesn't tell where to seek to...\n");
1561 return MCIERR_MISSING_PARAMETER
;
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 */