1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * Sample MIDI Wine Driver for Linux
6 * Copyright 1994 Martin Ayotte
11 * 98/7 changes for making this MIDI driver work on OSS
12 * current support is limited to MIDI ports of OSS systems
13 * 98/9 rewriting MCI code for MIDI
14 * 98/11 splitted in midi.c and mcimidi.c
19 #include "multimedia.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(mcimidi)
28 DWORD dwFirst; /* offset in file of track */
29 DWORD dwLast; /* number of bytes in file of track */
30 DWORD dwIndex; /* current index in file (dwFirst <= dwIndex < dwLast) */
31 DWORD dwLength; /* number of pulses in this track */
32 DWORD dwEventPulse; /* current pulse # (event) pointed by dwIndex */
33 DWORD dwEventData; /* current data (event) pointed by dwIndex */
34 WORD wEventLength; /* current length (event) pointed by dwIndex */
35 WORD wStatus : 1, /* 1 : playing, 0 : done */
43 int nUseCount; /* Incremented for each shared open */
44 WORD wNotifyDeviceID; /* MCI device ID with a pending notification */
45 HANDLE16 hCallback; /* Callback handle for pending notification */
46 HMMIO hFile; /* mmio file handle open as Element */
47 MCI_OPEN_PARMSA openParms;
49 WORD dwStatus; /* one from MCI_MODE_xxxx */
50 DWORD dwMciTimeFormat; /* One of the supported MCI_FORMAT_xxxx */
51 WORD wFormat; /* Format of MIDI hFile (0, 1 or 2) */
52 WORD nTracks; /* Number of tracks in hFile */
53 WORD nDivision; /* Number of division in hFile PPQN or SMPTE */
55 DWORD dwTempo; /* Tempo (# of 1/4 note per second */
56 MCI_MIDITRACK* tracks; /* Content of each track */
62 #define MAX_MCIMIDIDRV (1)
63 static WINE_MCIMIDI MCIMidiDev[MAX_MCIMIDIDRV];
65 /*======================================================================*
66 * MCI MIDI implemantation *
67 *======================================================================*/
69 #ifdef SNDCTL_MIDI_INFO
71 static DWORD MIDI_mciResume(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms);
73 /**************************************************************************
74 * MIDI_drvGetDrv [internal]
76 static WINE_MCIMIDI* MIDI_drvGetDrv(UINT16 wDevID)
80 for (i = 0; i < MAX_MCIMIDIDRV; i++) {
81 if (MCIMidiDev[i].wDevID == wDevID) {
82 return &MCIMidiDev[i];
88 /**************************************************************************
89 * MIDI_drvOpen [internal]
91 static DWORD MIDI_drvOpen(LPSTR str, LPMCI_OPEN_DRIVER_PARMSA modp)
95 for (i = 0; i < MAX_MCIMIDIDRV; i++) {
96 if (MCIMidiDev[i].wDevID == 0) {
97 MCIMidiDev[i].wDevID = modp->wDeviceID;
98 modp->wCustomCommandTable = -1;
99 modp->wType = MCI_DEVTYPE_CD_AUDIO;
100 return modp->wDeviceID;
106 /**************************************************************************
107 * MCIMIDI_drvClose [internal]
109 static DWORD MIDI_drvClose(DWORD dwDevID)
111 WINE_MCIMIDI* wmm = MIDI_drvGetDrv(dwDevID);
120 /**************************************************************************
121 * MIDI_mciGetOpenDev [internal]
123 static WINE_MCIMIDI* MIDI_mciGetOpenDev(UINT16 wDevID)
125 WINE_MCIMIDI* wmm = MIDI_drvGetDrv(wDevID);
127 if (wmm == NULL || wmm->nUseCount == 0) {
128 WARN("Invalid wDevID=%u\n", wDevID);
134 /**************************************************************************
135 * MIDI_mciReadByte [internal]
137 static DWORD MIDI_mciReadByte(WINE_MCIMIDI* wmm, BYTE *lpbyt)
142 mmioRead(wmm->hFile, (HPSTR)lpbyt, (long)sizeof(BYTE)) != (long)sizeof(BYTE)) {
143 WARN("Error reading wmm=%p\n", wmm);
144 ret = MCIERR_INVALID_FILE;
150 /**************************************************************************
151 * MIDI_mciReadWord [internal]
153 static DWORD MIDI_mciReadWord(WINE_MCIMIDI* wmm, LPWORD lpw)
156 DWORD ret = MCIERR_INVALID_FILE;
159 MIDI_mciReadByte(wmm, &hibyte) == 0 &&
160 MIDI_mciReadByte(wmm, &lobyte) == 0) {
161 *lpw = ((WORD)hibyte << 8) + lobyte;
167 /**************************************************************************
168 * MIDI_mciReadLong [internal]
170 static DWORD MIDI_mciReadLong(WINE_MCIMIDI* wmm, LPDWORD lpdw)
173 DWORD ret = MCIERR_INVALID_FILE;
176 MIDI_mciReadWord(wmm, &hiword) == 0 &&
177 MIDI_mciReadWord(wmm, &loword) == 0) {
178 *lpdw = MAKELONG(loword, hiword);
184 /**************************************************************************
185 * MIDI_mciReadVaryLen [internal]
187 static WORD MIDI_mciReadVaryLen(WINE_MCIMIDI* wmm, LPDWORD lpdw)
194 ret = MCIERR_INVALID_FILE;
197 if (MIDI_mciReadByte(wmm, &byte) != 0) {
200 value = (value << 7) + (byte & 0x7F);
202 } while (byte & 0x80);
205 TRACE("val=%08lX \n", value);
211 /**************************************************************************
212 * MIDI_mciReadNextEvent [internal]
214 static DWORD MIDI_mciReadNextEvent(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
222 if (mmioSeek(wmm->hFile, mmt->dwIndex, SEEK_SET) != mmt->dwIndex) {
223 WARN("Can't seek at %08lX \n", mmt->dwIndex);
224 return MCIERR_INVALID_FILE;
226 evtLength = MIDI_mciReadVaryLen(wmm, &evtPulse) + 1; /* > 0 */
227 MIDI_mciReadByte(wmm, &b1);
231 evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
235 MIDI_mciReadByte(wmm, &b2); evtLength++;
237 evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
238 if (evtLength >= 0x10000u) {
239 /* this limitation shouldn't be a problem */
240 WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
243 hw = LOWORD(evtLength);
248 if (b1 & 0x80) { /* use running status ? */
249 mmt->wLastCommand = b1;
250 MIDI_mciReadByte(wmm, &b2); evtLength++;
253 b1 = mmt->wLastCommand;
255 switch ((b1 >> 4) & 0x07) {
256 case 0: case 1: case 2: case 3: case 6:
257 MIDI_mciReadByte(wmm, &b3); evtLength++;
263 WARN("Strange indeed b1=0x%02x\n", b1);
267 if (mmt->dwIndex + evtLength > mmt->dwLast)
268 return MCIERR_INTERNAL;
270 mmt->dwEventPulse += evtPulse;
271 mmt->dwEventData = (hw << 16) + (b2 << 8) + b1;
272 mmt->wEventLength = evtLength;
275 TRACE("[%u] => pulse=%08lx(%08lx), data=%08lx, length=%u\n",
276 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
277 mmt->dwEventData, mmt->wEventLength);
282 /**************************************************************************
283 * MIDI_mciReadMTrk [internal]
285 static DWORD MIDI_mciReadMTrk(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
290 if (mmioRead(wmm->hFile, (HPSTR)&fourcc, (long)sizeof(FOURCC)) !=
291 (long)sizeof(FOURCC)) {
292 return MCIERR_INVALID_FILE;
295 if (fourcc != mmioFOURCC('M', 'T', 'r', 'k')) {
296 WARN("Can't synchronize on 'MTrk' !\n");
297 return MCIERR_INVALID_FILE;
300 if (MIDI_mciReadLong(wmm, &toberead) != 0) {
301 return MCIERR_INVALID_FILE;
303 mmt->dwFirst = mmioSeek(wmm->hFile, 0, SEEK_CUR); /* >= 0 */
304 mmt->dwLast = mmt->dwFirst + toberead;
306 /* compute # of pulses in this track */
307 mmt->dwIndex = mmt->dwFirst;
308 mmt->dwEventPulse = 0;
310 while (MIDI_mciReadNextEvent(wmm, mmt) == 0 &&
311 LOWORD(mmt->dwEventData) != 0x2FFF) {
312 mmt->dwIndex += mmt->wEventLength;
314 mmt->dwLength = mmt->dwEventPulse;
316 TRACE("Track %u has %lu bytes and %lu pulses\n", mmt->wTrackNr, toberead, mmt->dwLength);
318 /* reset track data */
319 mmt->wStatus = 1; /* ok, playing */
320 mmt->dwIndex = mmt->dwFirst;
321 mmt->dwEventPulse = 0;
323 if (mmioSeek(wmm->hFile, 0, SEEK_CUR) != mmt->dwLast) {
324 WARN("Ouch, out of sync seek=%lu track=%lu\n",
325 mmioSeek(wmm->hFile, 0, SEEK_CUR), mmt->dwLast);
326 /* position at end of this track, to be ready to read next track */
327 mmioSeek(wmm->hFile, mmt->dwLast, SEEK_SET);
333 /**************************************************************************
334 * MIDI_mciReadMThd [internal]
336 static DWORD MIDI_mciReadMThd(WINE_MCIMIDI* wmm, DWORD dwOffset)
342 TRACE("(%p, %08lX);\n", wmm, dwOffset);
344 if (mmioSeek(wmm->hFile, dwOffset, SEEK_SET) != dwOffset) {
345 WARN("Can't seek at %08lX begin of 'MThd' \n", dwOffset);
346 return MCIERR_INVALID_FILE;
348 if (mmioRead(wmm->hFile, (HPSTR)&fourcc,
349 (long) sizeof(FOURCC)) != (long) sizeof(FOURCC))
350 return MCIERR_INVALID_FILE;
352 if (fourcc != mmioFOURCC('M', 'T', 'h', 'd')) {
353 WARN("Can't synchronize on 'MThd' !\n");
354 return MCIERR_INVALID_FILE;
357 if (MIDI_mciReadLong(wmm, &toberead) != 0 || toberead < 3 * sizeof(WORD))
358 return MCIERR_INVALID_FILE;
360 if (MIDI_mciReadWord(wmm, &wmm->wFormat) != 0 ||
361 MIDI_mciReadWord(wmm, &wmm->nTracks) != 0 ||
362 MIDI_mciReadWord(wmm, &wmm->nDivision) != 0) {
363 return MCIERR_INVALID_FILE;
366 TRACE("toberead=0x%08lX, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
367 toberead, wmm->wFormat, wmm->nTracks, wmm->nDivision);
369 /* MS doc says that the MIDI MCI time format must be put by default to the format
370 * stored in the MIDI file...
372 if (wmm->nDivision > 0x8000) {
373 /* eric.pouech@lemel.fr 98/11
374 * In did not check this very code (pulses are expressed as SMPTE sub-frames).
375 * In about 40 MB of MIDI files I have, none was SMPTE based...
376 * I'm just wondering if this is widely used :-). So, if someone has one of
377 * these files, I'd like to know about.
379 FIXME("Handling SMPTE time in MIDI files has not been tested\n"
380 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
382 switch (HIBYTE(wmm->nDivision)) {
383 case 0xE8: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24; break; /* -24 */
384 case 0xE7: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25; break; /* -25 */
385 case 0xE3: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
386 case 0xE2: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30; break; /* -30 */
388 WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm->nDivision));
389 return MCIERR_INVALID_FILE;
391 switch (LOBYTE(wmm->nDivision)) {
392 case 4: /* MIDI Time Code */
395 case 80: /* SMPTE bit resolution */
398 WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm->nDivision));
399 return MCIERR_INVALID_FILE;
401 } else if (wmm->nDivision == 0) {
402 WARN("Number of division is 0, can't support that !!\n");
403 return MCIERR_INVALID_FILE;
405 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
408 switch (wmm->wFormat) {
410 if (wmm->nTracks != 1) {
411 WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
419 WARN("Handling MIDI files which format = %d is not (yet) supported\n"
420 "Please report with MIDI file !\n", wmm->wFormat);
421 return MCIERR_INVALID_FILE;
424 if (wmm->nTracks & 0x8000) {
425 /* this shouldn't be a problem... */
426 WARN("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
427 wmm->nTracks = 0x7FFF;
430 if ((wmm->tracks = malloc(sizeof(MCI_MIDITRACK) * wmm->nTracks)) == NULL) {
431 return MCIERR_OUT_OF_MEMORY;
434 toberead -= 3 * sizeof(WORD);
436 TRACE("Size of MThd > 6, skipping %ld extra bytes\n", toberead);
437 mmioSeek(wmm->hFile, toberead, SEEK_CUR);
440 for (nt = 0; nt < wmm->nTracks; nt++) {
441 wmm->tracks[nt].wTrackNr = nt;
442 if (MIDI_mciReadMTrk(wmm, &wmm->tracks[nt]) != 0) {
443 WARN("Can't read 'MTrk' header \n");
444 return MCIERR_INVALID_FILE;
448 wmm->dwTempo = 500000;
453 /**************************************************************************
454 * MIDI_ConvertPulseToMS [internal]
456 static DWORD MIDI_ConvertPulseToMS(WINE_MCIMIDI* wmm, DWORD pulse)
460 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
461 * may change during file playing
463 if (wmm->nDivision == 0) {
464 FIXME("Shouldn't happen. wmm->nDivision = 0\n");
465 } else if (wmm->nDivision > 0x8000) { /* SMPTE, unchecked FIXME? */
466 int nf = -(char)HIBYTE(wmm->nDivision); /* number of frames */
467 int nsf = LOBYTE(wmm->nDivision); /* number of sub-frames */
468 ret = (pulse * 1000) / (nf * nsf);
470 ret = (DWORD)((double)pulse * ((double)wmm->dwTempo / 1000) /
471 (double)wmm->nDivision);
475 TRACE("pulse=%lu tempo=%lu division=%u=0x%04x => ms=%lu\n",
476 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
482 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
483 #define TIME_MS_IN_ONE_MINUTE (60*1000)
484 #define TIME_MS_IN_ONE_SECOND (1000)
486 /**************************************************************************
487 * MIDI_ConvertTimeFormatToMS [internal]
489 static DWORD MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI* wmm, DWORD val)
493 switch (wmm->dwMciTimeFormat) {
494 case MCI_FORMAT_MILLISECONDS:
497 case MCI_FORMAT_SMPTE_24:
499 (HIBYTE(HIWORD(val)) * 125) / 3 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
500 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
502 case MCI_FORMAT_SMPTE_25:
504 HIBYTE(HIWORD(val)) * 40 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
505 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
507 case MCI_FORMAT_SMPTE_30:
509 (HIBYTE(HIWORD(val)) * 100) / 3 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
510 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
513 WARN("Bad time format %lu!\n", wmm->dwMciTimeFormat);
516 TRACE("val=%lu=0x%08lx [tf=%lu] => ret=%lu\n", val, val, wmm->dwMciTimeFormat, ret);
521 /**************************************************************************
522 * MIDI_ConvertMSToTimeFormat [internal]
524 static DWORD MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI* wmm, DWORD _val)
526 DWORD ret = 0, val = _val;
529 switch (wmm->dwMciTimeFormat) {
530 case MCI_FORMAT_MILLISECONDS:
533 case MCI_FORMAT_SMPTE_24:
534 case MCI_FORMAT_SMPTE_25:
535 case MCI_FORMAT_SMPTE_30:
536 h = val / TIME_MS_IN_ONE_HOUR;
537 m = (val -= h * TIME_MS_IN_ONE_HOUR) / TIME_MS_IN_ONE_MINUTE;
538 s = (val -= m * TIME_MS_IN_ONE_MINUTE) / TIME_MS_IN_ONE_SECOND;
539 switch (wmm->dwMciTimeFormat) {
540 case MCI_FORMAT_SMPTE_24:
541 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
542 f = (val * 3) / 125; val -= (f * 125) / 3;
544 case MCI_FORMAT_SMPTE_25:
545 /* one frame is 1000/25 ms long, 1000/25 == 40 */
546 f = val / 40; val -= f * 40;
548 case MCI_FORMAT_SMPTE_30:
549 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
550 f = (val * 3) / 100; val -= (f * 100) / 3;
553 FIXME("There must be some bad bad programmer\n");
556 /* val contains the number of ms which cannot make a complete frame */
557 /* FIXME: is this correct ? programs seem to be happy with that */
558 ret = (f << 24) | (s << 16) | (m << 8) | (h << 0);
561 WARN("Bad time format %lu!\n", wmm->dwMciTimeFormat);
564 TRACE("val=%lu [tf=%lu] => ret=%lu=0x%08lx\n", _val, wmm->dwMciTimeFormat, ret, ret);
569 /**************************************************************************
570 * MIDI_GetMThdLengthMS [internal]
572 static DWORD MIDI_GetMThdLengthMS(WINE_MCIMIDI* wmm)
577 for (nt = 0; nt < wmm->nTracks; nt++) {
578 if (wmm->wFormat == 2) {
579 ret += wmm->tracks[nt].dwLength;
580 } else if (wmm->tracks[nt].dwLength > ret) {
581 ret = wmm->tracks[nt].dwLength;
584 /* FIXME: this is wrong if there is a tempo change inside the file */
585 return MIDI_ConvertPulseToMS(wmm, ret);
588 /**************************************************************************
589 * MIDI_mciOpen [internal]
591 static DWORD MIDI_mciOpen(UINT16 wDevID, DWORD dwFlags, LPMCI_OPEN_PARMSA lpParms)
593 MIDIOPENDESC midiOpenDesc;
596 WINE_MCIMIDI* wmm = MIDI_drvGetDrv(wDevID);
598 TRACE("(%04x, %08lX, %p)\n", wDevID, dwFlags, lpParms);
600 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
601 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
602 if (dwFlags & MCI_OPEN_SHAREABLE)
603 return MCIERR_HARDWARE;
605 if (wmm->nUseCount > 0) {
606 /* The driver is already opened on this channel
607 * MIDI sequencer cannot be shared
609 return MCIERR_DEVICE_OPEN;
615 dwDeviceID = lpParms->wDeviceID;
617 TRACE("wDevID=%04X (lpParams->wDeviceID=%08lX)\n", wDevID, dwDeviceID);
618 /* lpParms->wDeviceID = wDevID;*/
620 if (dwFlags & MCI_OPEN_ELEMENT) {
621 LPSTR lpstrElementName;
623 lpstrElementName = lpParms->lpstrElementName;
625 TRACE("MCI_OPEN_ELEMENT '%s' !\n", lpstrElementName);
626 if (lpstrElementName && strlen(lpstrElementName) > 0) {
627 wmm->hFile = mmioOpenA(lpstrElementName, NULL,
628 MMIO_ALLOCBUF | MMIO_READWRITE | MMIO_DENYWRITE);
629 if (wmm->hFile == 0) {
630 WARN("Can't find file='%s' !\n", lpstrElementName);
632 return MCIERR_FILE_NOT_FOUND;
638 TRACE("hFile=%u\n", wmm->hFile);
640 memcpy(&wmm->openParms, lpParms, sizeof(MCI_OPEN_PARMSA));
642 wmm->wNotifyDeviceID = dwDeviceID;
643 wmm->dwStatus = MCI_MODE_NOT_READY; /* while loading file contents */
644 /* spec says it should be the default format from the MIDI file... */
645 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
647 midiOpenDesc.hMidi = 0;
649 if (wmm->hFile != 0) {
654 if (mmioDescend(wmm->hFile, &ckMainRIFF, NULL, 0) != 0) {
655 dwRet = MCIERR_INVALID_FILE;
657 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08lX \n",
658 (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, ckMainRIFF.cksize);
660 if (ckMainRIFF.ckid == FOURCC_RIFF && ckMainRIFF.fccType == mmioFOURCC('R', 'M', 'I', 'D')) {
661 mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
662 if (mmioDescend(wmm->hFile, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK) == 0) {
663 TRACE("... is a 'RMID' file \n");
664 dwOffset = mmckInfo.dwDataOffset;
666 dwRet = MCIERR_INVALID_FILE;
669 if (dwRet == 0 && MIDI_mciReadMThd(wmm, dwOffset) != 0) {
670 WARN("Can't read 'MThd' header \n");
671 dwRet = MCIERR_INVALID_FILE;
675 TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
683 mmioClose(wmm->hFile, 0);
686 wmm->dwPositionMS = 0;
687 wmm->dwStatus = MCI_MODE_STOP;
688 wmm->hMidiHdr = USER_HEAP_ALLOC(sizeof(MIDIHDR16));
690 dwRet = modMessage(wmm->wMidiID, MODM_OPEN, 0, (DWORD)&midiOpenDesc, CALLBACK_NULL);
691 /* dwRet = midMessage(wmm->wMidiID, MIDM_OPEN, 0, (DWORD)&midiOpenDesc, CALLBACK_NULL);*/
696 /**************************************************************************
697 * MIDI_mciStop [internal]
699 static DWORD MIDI_mciStop(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
701 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
703 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
705 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
707 wmm->dwStatus = MCI_MODE_STOP;
708 TRACE("wmm->dwStatus=%d\n", wmm->dwStatus);
710 if (lpParms && (dwFlags & MCI_NOTIFY)) {
711 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
712 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
713 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
718 /**************************************************************************
719 * MIDI_mciClose [internal]
721 static DWORD MIDI_mciClose(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
724 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
726 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
728 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
730 if (wmm->dwStatus != MCI_MODE_STOP) {
731 MIDI_mciStop(wDevID, MCI_WAIT, lpParms);
734 wmm->dwStatus = MCI_MODE_STOP;
736 if (wmm->nUseCount == 0) {
737 if (wmm->hFile != 0) {
738 mmioClose(wmm->hFile, 0);
740 TRACE("hFile closed !\n");
742 USER_HEAP_FREE(wmm->hMidiHdr);
744 dwRet = modMessage(wmm->wMidiID, MODM_CLOSE, 0, 0L, 0L);
745 if (dwRet != MMSYSERR_NOERROR) return MCIERR_INTERNAL;
747 dwRet = midMessage(wmm->wMidiID, MIDM_CLOSE, 0, 0L, 0L);
748 if (dwRet != MMSYSERR_NOERROR) return MCIERR_INTERNAL;
751 TRACE("Shouldn't happen... nUseCount=%d\n", wmm->nUseCount);
752 return MCIERR_INTERNAL;
755 if (lpParms && (dwFlags & MCI_NOTIFY)) {
756 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
757 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
758 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
763 /**************************************************************************
764 * MIDI_mciFindNextEvent [internal]
766 static MCI_MIDITRACK* MIDI_mciFindNextEvent(WINE_MCIMIDI* wmm, LPDWORD hiPulse)
771 *hiPulse = 0xFFFFFFFFul;
773 for (nt = 0; nt < wmm->nTracks; nt++) {
774 mmt = &wmm->tracks[nt];
776 if (mmt->wStatus == 0)
778 if (mmt->dwEventPulse < *hiPulse) {
779 *hiPulse = mmt->dwEventPulse;
783 return (cnt == 0xFFFFu) ? 0 /* no more event on all tracks */
787 /**************************************************************************
788 * MIDI_mciPlay [internal]
790 static DWORD MIDI_mciPlay(UINT16 wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
792 DWORD dwStartMS, dwEndMS, dwRet;
796 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
798 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
800 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
802 if (wmm->hFile == 0) {
803 WARN("Can't play: no file '%s' !\n", wmm->openParms.lpstrElementName);
804 return MCIERR_FILE_NOT_FOUND;
807 if (wmm->dwStatus != MCI_MODE_STOP) {
808 if (wmm->dwStatus == MCI_MODE_PAUSE) {
809 /* FIXME: parameters (start/end) in lpParams may not be used */
810 return MIDI_mciResume(wDevID, dwFlags, (LPMCI_GENERIC_PARMS)lpParms);
812 WARN("Can't play: device is not stopped !\n");
813 return MCIERR_INTERNAL;
816 if (!(dwFlags & MCI_WAIT)) {
817 /** FIXME: I'm not 100% sure that wNotifyDeviceID is the right value in all cases ??? */
818 return MCI_SendCommandAsync(wmm->wNotifyDeviceID, MCI_PLAY, dwFlags, (DWORD)lpParms, sizeof(LPMCI_PLAY_PARMS));
821 if (lpParms && (dwFlags & MCI_FROM)) {
822 dwStartMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwFrom);
824 dwStartMS = wmm->dwPositionMS;
827 if (lpParms && (dwFlags & MCI_TO)) {
828 dwEndMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
830 dwEndMS = 0xFFFFFFFFul;
833 TRACE("Playing from %lu to %lu\n", dwStartMS, dwEndMS);
836 for (nt = 0; nt < wmm->nTracks; nt++) {
837 mmt = &wmm->tracks[nt];
839 mmt->wStatus = 1; /* ok, playing */
840 mmt->dwIndex = mmt->dwFirst;
841 if (wmm->wFormat == 2 && nt > 0) {
842 mmt->dwEventPulse = wmm->tracks[nt - 1].dwLength;
844 mmt->dwEventPulse = 0;
846 MIDI_mciReadNextEvent(wmm, mmt); /* FIXME == 0 */
850 wmm->dwTempo = 500000;
851 wmm->dwStatus = MCI_MODE_PLAY;
852 wmm->dwPositionMS = 0;
853 wmm->wStartedPlaying = FALSE;
855 while (wmm->dwStatus != MCI_MODE_STOP) {
856 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
857 * too much. Tell gcc not to optimize status value using volatile.
859 while (((volatile WINE_MCIMIDI*)wmm)->dwStatus == MCI_MODE_PAUSE);
861 doPlay = (wmm->dwPositionMS >= dwStartMS && wmm->dwPositionMS <= dwEndMS);
863 TRACE("wmm->dwStatus=%d, doPlay=%s\n", wmm->dwStatus, doPlay ? "T" : "F");
865 if ((mmt = MIDI_mciFindNextEvent(wmm, &hiPulse)) == NULL)
866 break; /* no more event on tracks */
868 /* if starting playing, then set StartTicks to the value it would have had
869 * if play had started at position 0
871 if (doPlay && !wmm->wStartedPlaying) {
872 wmm->dwStartTicks = GetTickCount() - MIDI_ConvertPulseToMS(wmm, wmm->dwPulse);
873 wmm->wStartedPlaying = TRUE;
874 TRACE("Setting dwStartTicks to %lu\n", wmm->dwStartTicks);
877 if (hiPulse > wmm->dwPulse) {
878 wmm->dwPositionMS += MIDI_ConvertPulseToMS(wmm, hiPulse - wmm->dwPulse);
880 DWORD togo = wmm->dwStartTicks + wmm->dwPositionMS;
881 DWORD tc = GetTickCount();
883 TRACE("Pulses hi=0x%08lx <> cur=0x%08lx\n", hiPulse, wmm->dwPulse);
884 TRACE("Wait until %lu => %lu ms\n",
885 tc - wmm->dwStartTicks, togo - wmm->dwStartTicks);
889 wmm->dwPulse = hiPulse;
892 switch (LOBYTE(LOWORD(mmt->dwEventData))) {
894 case 0xF7: /* sysex events */
896 FIXME("Not handling SysEx events (yet)\n");
900 /* position after meta data header */
901 mmioSeek(wmm->hFile, mmt->dwIndex + HIWORD(mmt->dwEventData), SEEK_SET);
902 switch (HIBYTE(LOWORD(mmt->dwEventData))) {
903 case 0x00: /* 16-bit sequence number */
904 if (TRACE_ON(mcimidi)) {
907 MIDI_mciReadWord(wmm, &twd); /* == 0 */
908 TRACE("Got sequence number %u\n", twd);
911 case 0x01: /* any text */
912 case 0x02: /* Copyright Message text */
913 case 0x03: /* Sequence/Track Name text */
914 case 0x04: /* Instrument Name text */
915 case 0x05: /* Lyric text */
916 case 0x06: /* Marker text */
917 case 0x07: /* Cue-point text */
918 if (TRACE_ON(mcimidi)) {
920 WORD len = mmt->wEventLength - HIWORD(mmt->dwEventData);
921 static char* info[8] = {"", "Text", "Copyright", "Seq/Trk name",
922 "Instrument", "Lyric", "Marker", "Cue-point"};
923 WORD idx = HIBYTE(LOWORD(mmt->dwEventData));
925 if (len >= sizeof(buf)) {
926 WARN("Buffer for text is too small (%d bytes, when %u are needed)\n", sizeof(buf) - 1, len);
927 len = sizeof(buf) - 1;
929 if (mmioRead(wmm->hFile, (HPSTR)buf, len) == len) {
930 buf[len] = 0; /* end string in case */
931 TRACE("%s => \"%s\"\n", (idx < 8 ) ? info[idx] : "", buf);
933 WARN("Couldn't read data for %s\n", (idx < 8 ) ? info[idx] : "");
938 /* MIDI channel (cc) */
939 if (FIXME_ON(mcimidi)) {
942 MIDI_mciReadByte(wmm, &bt); /* == 0 */
943 FIXME("NIY: MIDI channel=%u, track=%u\n", bt, mmt->wTrackNr);
948 if (FIXME_ON(mcimidi)) {
951 MIDI_mciReadByte(wmm, &bt); /* == 0 */
952 FIXME("NIY: MIDI port=%u, track=%u\n", bt, mmt->wTrackNr);
955 case 0x2F: /* end of track */
958 case 0x51:/* set tempo */
959 /* Tempo is expressed in µ-seconds per midi quarter note
960 * for format 1 MIDI files, this can only be present on track #0
962 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
963 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt->wTrackNr);
968 MIDI_mciReadByte(wmm, &tbt); value = ((DWORD)tbt) << 16;
969 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 8;
970 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 0;
971 TRACE("Setting tempo to %ld (BPM=%ld)\n", wmm->dwTempo, (value) ? (60000000l / value) : 0);
972 wmm->dwTempo = value;
975 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
976 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
977 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt->wTrackNr);
978 } if (mmt->dwEventPulse != 0) {
979 WARN("SMPTE track start can only be expressed at start of track (%lu)\n", mmt->dwEventPulse);
983 MIDI_mciReadByte(wmm, &h);
984 MIDI_mciReadByte(wmm, &m);
985 MIDI_mciReadByte(wmm, &s);
986 MIDI_mciReadByte(wmm, &f);
987 MIDI_mciReadByte(wmm, &ff);
988 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h, m, s, f, ff);
991 case 0x58: /* file rythm */
992 if (TRACE_ON(mcimidi)) {
993 BYTE num, den, cpmc, _32npqn;
995 MIDI_mciReadByte(wmm, &num);
996 MIDI_mciReadByte(wmm, &den); /* to notate e.g. 6/8 */
997 MIDI_mciReadByte(wmm, &cpmc); /* number of MIDI clocks per metronome click */
998 MIDI_mciReadByte(wmm, &_32npqn); /* number of notated 32nd notes per MIDI quarter note */
1000 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num, 1 << den, cpmc, _32npqn);
1003 case 0x59: /* key signature */
1004 if (TRACE_ON(mcimidi)) {
1007 MIDI_mciReadByte(wmm, &sf);
1008 MIDI_mciReadByte(wmm, &mm);
1010 if (sf >= 0x80) TRACE("%d flats\n", -(char)sf);
1011 else if (sf > 0) TRACE("%d sharps\n", (char)sf);
1012 else TRACE("Key of C\n");
1013 TRACE("Mode: %s\n", (mm = 0) ? "major" : "minor");
1017 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt->dwEventData)));
1023 dwRet = modMessage(wmm->wMidiID, MODM_DATA, 0, mmt->dwEventData, 0);
1025 switch (LOBYTE(LOWORD(mmt->dwEventData)) & 0xF0) {
1031 dwRet = modMessage(wmm->wMidiID, MODM_DATA, 0, mmt->dwEventData, 0);
1035 mmt->dwIndex += mmt->wEventLength;
1036 if (mmt->dwIndex < mmt->dwFirst || mmt->dwIndex >= mmt->dwLast) {
1040 MIDI_mciReadNextEvent(wmm, mmt);
1044 /* stop all notes */
1045 /* should be (0x7800 | MIDI_CTL_CHANGE) instead of 0x78B0,
1046 * but MIDI_CTL_CHANGE is defined in OSS's soundcard.h and MCI must compile
1047 * without any reference to OSS
1049 /* FIXME: check if 0x78B0 is channel dependant or not. I coded it so that
1050 * it's channel dependent...
1054 for (chn = 0; chn < 16; chn++)
1055 modMessage(wmm->wMidiID, MODM_DATA, 0, 0x78B0 | chn, 0);
1058 wmm->dwStatus = MCI_MODE_STOP;
1060 /* to restart playing at beginning when it's over */
1061 wmm->dwPositionMS = 0;
1063 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1064 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1065 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
1066 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1071 /**************************************************************************
1072 * MIDI_mciRecord [internal]
1074 static DWORD MIDI_mciRecord(UINT16 wDevID, DWORD dwFlags, LPMCI_RECORD_PARMS lpParms)
1077 LPMIDIHDR16 lpMidiHdr;
1079 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1081 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1083 if (wmm == 0) return MCIERR_INVALID_DEVICE_ID;
1085 if (wmm->hFile == 0) {
1086 WARN("Can't find file='%08lx' !\n",
1087 (DWORD)wmm->openParms.lpstrElementName);
1088 return MCIERR_FILE_NOT_FOUND;
1090 start = 1; end = 99999;
1091 if (lpParms && (dwFlags & MCI_FROM)) {
1092 start = lpParms->dwFrom;
1093 TRACE("MCI_FROM=%d \n", start);
1095 if (lpParms && (dwFlags & MCI_TO)) {
1096 end = lpParms->dwTo;
1097 TRACE("MCI_TO=%d \n", end);
1099 lpMidiHdr = USER_HEAP_LIN_ADDR(wmm->hMidiHdr);
1100 lpMidiHdr->lpData = (LPSTR) malloc(1200);
1102 return MCIERR_OUT_OF_MEMORY;
1103 lpMidiHdr->dwBufferLength = 1024;
1104 lpMidiHdr->dwUser = 0L;
1105 lpMidiHdr->dwFlags = 0L;
1106 dwRet = midMessage(wmm->wMidiID, MIDM_PREPARE, 0, (DWORD)lpMidiHdr, sizeof(MIDIHDR16));
1107 TRACE("After MIDM_PREPARE \n");
1108 wmm->dwStatus = MCI_MODE_RECORD;
1109 while (wmm->dwStatus != MCI_MODE_STOP) {
1110 TRACE("wmm->dwStatus=%p %d\n",
1111 &wmm->dwStatus, wmm->dwStatus);
1112 lpMidiHdr->dwBytesRecorded = 0;
1113 dwRet = midMessage(wmm->wMidiID, MIDM_START, 0, 0L, 0L);
1114 TRACE("After MIDM_START lpMidiHdr=%p dwBytesRecorded=%lu\n",
1115 lpMidiHdr, lpMidiHdr->dwBytesRecorded);
1116 if (lpMidiHdr->dwBytesRecorded == 0) break;
1118 TRACE("Before MIDM_UNPREPARE \n");
1119 dwRet = midMessage(wmm->wMidiID, MIDM_UNPREPARE, 0, (DWORD)lpMidiHdr, sizeof(MIDIHDR16));
1120 TRACE("After MIDM_UNPREPARE \n");
1121 if (lpMidiHdr->lpData != NULL) {
1122 free(lpMidiHdr->lpData);
1123 lpMidiHdr->lpData = NULL;
1125 wmm->dwStatus = MCI_MODE_STOP;
1126 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1127 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1128 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
1129 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1134 /**************************************************************************
1135 * MIDI_mciPause [internal]
1137 static DWORD MIDI_mciPause(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1139 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1141 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1143 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1145 if (wmm->dwStatus == MCI_MODE_PLAY) {
1146 /* stop all notes */
1147 /* see note in MIDI_mciPlay */
1149 for (chn = 0; chn < 16; chn++)
1150 modMessage(wmm->wMidiID, MODM_DATA, 0, 0x78B0 | chn, 0);
1151 wmm->dwStatus = MCI_MODE_PAUSE;
1153 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1154 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1155 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
1156 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1162 /**************************************************************************
1163 * MIDI_mciResume [internal]
1165 static DWORD MIDI_mciResume(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1167 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1169 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1171 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1173 if (wmm->dwStatus == MCI_MODE_PAUSE) {
1174 wmm->wStartedPlaying = FALSE;
1175 wmm->dwStatus = MCI_MODE_PLAY;
1177 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1178 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1179 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
1180 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1185 /**************************************************************************
1186 * MIDI_mciSet [internal]
1188 static DWORD MIDI_mciSet(UINT16 wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
1190 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1192 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1194 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1195 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1197 if (dwFlags & MCI_SET_TIME_FORMAT) {
1198 switch (lpParms->dwTimeFormat) {
1199 case MCI_FORMAT_MILLISECONDS:
1200 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1201 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
1203 case MCI_FORMAT_SMPTE_24:
1204 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1205 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24;
1207 case MCI_FORMAT_SMPTE_25:
1208 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1209 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25;
1211 case MCI_FORMAT_SMPTE_30:
1212 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1213 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30;
1216 WARN("Bad time format %lu!\n", lpParms->dwTimeFormat);
1217 return MCIERR_BAD_TIME_FORMAT;
1220 if (dwFlags & MCI_SET_VIDEO) {
1221 TRACE("No support for video !\n");
1222 return MCIERR_UNSUPPORTED_FUNCTION;
1224 if (dwFlags & MCI_SET_DOOR_OPEN) {
1225 TRACE("No support for door open !\n");
1226 return MCIERR_UNSUPPORTED_FUNCTION;
1228 if (dwFlags & MCI_SET_DOOR_CLOSED) {
1229 TRACE("No support for door close !\n");
1230 return MCIERR_UNSUPPORTED_FUNCTION;
1232 if (dwFlags & MCI_SET_AUDIO) {
1233 if (dwFlags & MCI_SET_ON) {
1234 TRACE("MCI_SET_ON audio !\n");
1235 } else if (dwFlags & MCI_SET_OFF) {
1236 TRACE("MCI_SET_OFF audio !\n");
1238 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1239 return MCIERR_BAD_INTEGER;
1242 if (lpParms->dwAudio & MCI_SET_AUDIO_ALL)
1243 TRACE("MCI_SET_AUDIO_ALL !\n");
1244 if (lpParms->dwAudio & MCI_SET_AUDIO_LEFT)
1245 TRACE("MCI_SET_AUDIO_LEFT !\n");
1246 if (lpParms->dwAudio & MCI_SET_AUDIO_RIGHT)
1247 TRACE("MCI_SET_AUDIO_RIGHT !\n");
1250 if (dwFlags & MCI_SEQ_SET_MASTER)
1251 TRACE("MCI_SEQ_SET_MASTER !\n");
1252 if (dwFlags & MCI_SEQ_SET_SLAVE)
1253 TRACE("MCI_SEQ_SET_SLAVE !\n");
1254 if (dwFlags & MCI_SEQ_SET_OFFSET)
1255 TRACE("MCI_SEQ_SET_OFFSET !\n");
1256 if (dwFlags & MCI_SEQ_SET_PORT)
1257 TRACE("MCI_SEQ_SET_PORT !\n");
1258 if (dwFlags & MCI_SEQ_SET_TEMPO)
1259 TRACE("MCI_SEQ_SET_TEMPO !\n");
1263 /**************************************************************************
1264 * MIDI_mciStatus [internal]
1266 static DWORD MIDI_mciStatus(UINT16 wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
1268 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1270 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1272 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1273 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1275 if (dwFlags & MCI_STATUS_ITEM) {
1276 switch (lpParms->dwItem) {
1277 case MCI_STATUS_CURRENT_TRACK:
1278 /* FIXME in Format 2 */
1279 lpParms->dwReturn = 1;
1280 TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms->dwReturn);
1282 case MCI_STATUS_LENGTH:
1283 if ((dwFlags & MCI_TRACK) && wmm->wFormat == 2) {
1284 if (lpParms->dwTrack >= wmm->nTracks)
1285 return MCIERR_BAD_INTEGER;
1286 /* FIXME: this is wrong if there is a tempo change inside the file */
1287 lpParms->dwReturn = MIDI_ConvertPulseToMS(wmm, wmm->tracks[lpParms->dwTrack].dwLength);
1289 lpParms->dwReturn = MIDI_GetMThdLengthMS(wmm);
1291 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm, lpParms->dwReturn);
1292 TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms->dwReturn);
1294 case MCI_STATUS_MODE:
1295 lpParms->dwReturn = wmm->dwStatus;
1296 TRACE("MCI_STATUS_MODE => %lu\n", lpParms->dwReturn);
1298 case MCI_STATUS_MEDIA_PRESENT:
1299 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1300 lpParms->dwReturn = TRUE;
1302 case MCI_STATUS_NUMBER_OF_TRACKS:
1303 lpParms->dwReturn = (wmm->wFormat == 2) ? wmm->nTracks : 1;
1304 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms->dwReturn);
1306 case MCI_STATUS_POSITION:
1307 /* FIXME: do I need to use MCI_TRACK ? */
1308 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm,
1309 (dwFlags & MCI_STATUS_START) ? 0 : wmm->dwPositionMS);
1310 TRACE("MCI_STATUS_POSITION %s => %lu\n",
1311 (dwFlags & MCI_STATUS_START) ? "start" : "current", lpParms->dwReturn);
1313 case MCI_STATUS_READY:
1314 lpParms->dwReturn = (wmm->dwStatus != MCI_MODE_NOT_READY);
1315 TRACE("MCI_STATUS_READY = %lu\n", lpParms->dwReturn);
1317 case MCI_STATUS_TIME_FORMAT:
1318 lpParms->dwReturn = wmm->dwMciTimeFormat;
1319 TRACE("MCI_STATUS_TIME_FORMAT => %lu\n", lpParms->dwReturn);
1321 case MCI_SEQ_STATUS_DIVTYPE:
1322 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1323 if (wmm->nDivision > 0x8000) {
1324 switch (wmm->nDivision) {
1325 case 0xE8: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_24; break; /* -24 */
1326 case 0xE7: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_25; break; /* -25 */
1327 case 0xE3: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
1328 case 0xE2: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30; break; /* -30 */
1329 default: FIXME("There is a bad bad programmer\n");
1332 lpParms->dwReturn = MCI_SEQ_DIV_PPQN;
1335 case MCI_SEQ_STATUS_MASTER:
1336 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1337 lpParms->dwReturn = 0;
1339 case MCI_SEQ_STATUS_SLAVE:
1340 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1341 lpParms->dwReturn = 0;
1343 case MCI_SEQ_STATUS_OFFSET:
1344 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1345 lpParms->dwReturn = 0;
1347 case MCI_SEQ_STATUS_PORT:
1348 TRACE("MCI_SEQ_STATUS_PORT !\n");
1349 lpParms->dwReturn = 0;
1351 case MCI_SEQ_STATUS_TEMPO:
1352 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1353 lpParms->dwReturn = wmm->dwTempo;
1356 WARN("Unknowm command %08lX !\n", lpParms->dwItem);
1357 return MCIERR_UNRECOGNIZED_COMMAND;
1360 WARN("No Status-Item!\n");
1361 return MCIERR_UNRECOGNIZED_COMMAND;
1363 if (dwFlags & MCI_NOTIFY) {
1364 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1365 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
1366 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1371 /**************************************************************************
1372 * MIDI_mciGetDevCaps [internal]
1374 static DWORD MIDI_mciGetDevCaps(UINT16 wDevID, DWORD dwFlags,
1375 LPMCI_GETDEVCAPS_PARMS lpParms)
1377 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1379 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1381 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1382 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1384 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
1385 switch (lpParms->dwItem) {
1386 case MCI_GETDEVCAPS_DEVICE_TYPE:
1387 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1388 lpParms->dwReturn = MCI_DEVTYPE_SEQUENCER;
1390 case MCI_GETDEVCAPS_HAS_AUDIO:
1391 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1392 lpParms->dwReturn = TRUE;
1394 case MCI_GETDEVCAPS_HAS_VIDEO:
1395 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1396 lpParms->dwReturn = FALSE;
1398 case MCI_GETDEVCAPS_USES_FILES:
1399 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1400 lpParms->dwReturn = TRUE;
1402 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
1403 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1404 lpParms->dwReturn = TRUE;
1406 case MCI_GETDEVCAPS_CAN_EJECT:
1407 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1408 lpParms->dwReturn = FALSE;
1410 case MCI_GETDEVCAPS_CAN_PLAY:
1411 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1412 lpParms->dwReturn = TRUE;
1414 case MCI_GETDEVCAPS_CAN_RECORD:
1415 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1416 lpParms->dwReturn = TRUE;
1418 case MCI_GETDEVCAPS_CAN_SAVE:
1419 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1420 lpParms->dwReturn = FALSE;
1423 TRACE("Unknown capability (%08lx) !\n", lpParms->dwItem);
1424 return MCIERR_UNRECOGNIZED_COMMAND;
1427 TRACE("No GetDevCaps-Item !\n");
1428 return MCIERR_UNRECOGNIZED_COMMAND;
1433 /**************************************************************************
1434 * MIDI_mciInfo [internal]
1436 static DWORD MIDI_mciInfo(UINT16 wDevID, DWORD dwFlags, LPMCI_INFO_PARMSA lpParms)
1440 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1442 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1444 if (lpParms == NULL || lpParms->lpstrReturn == NULL) {
1445 ret = MCIERR_NULL_PARAMETER_BLOCK;
1446 } else if (wmm == NULL) {
1447 ret = MCIERR_INVALID_DEVICE_ID;
1449 TRACE("buf=%p, len=%lu\n", lpParms->lpstrReturn, lpParms->dwRetSize);
1452 case MCI_INFO_PRODUCT:
1453 str = "Wine's MIDI sequencer";
1456 str = wmm->openParms.lpstrElementName;
1459 /* FIXME: the following manifest constants are not defined in <WINE>/include/mmsystem.h */
1460 case MCI_INFO_COPYRIGHT:
1466 WARN("Don't know this info command (%lu)\n", dwFlags);
1467 ret = MCIERR_UNRECOGNIZED_COMMAND;
1471 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, str);
1473 lpParms->lpstrReturn[0] = 0;
1479 /**************************************************************************
1480 * MIDI_mciSeek [internal]
1482 static DWORD MIDI_mciSeek(UINT16 wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1485 WINE_MCIMIDI* wmm = MIDI_mciGetOpenDev(wDevID);
1487 TRACE("(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
1489 if (lpParms == NULL) {
1490 ret = MCIERR_NULL_PARAMETER_BLOCK;
1491 } else if (wmm == NULL) {
1492 ret = MCIERR_INVALID_DEVICE_ID;
1494 MIDI_mciStop(wDevID, MCI_WAIT, 0);
1496 if (dwFlags & MCI_SEEK_TO_START) {
1497 wmm->dwPositionMS = 0;
1498 } else if (dwFlags & MCI_SEEK_TO_END) {
1499 wmm->dwPositionMS = 0xFFFFFFFF; /* fixme */
1500 } else if (dwFlags & MCI_TO) {
1501 wmm->dwPositionMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
1503 WARN("dwFlag doesn't tell where to seek to...\n");
1504 return MCIERR_MISSING_PARAMETER;
1507 TRACE("Seeking to position=%lu ms\n", wmm->dwPositionMS);
1509 if (dwFlags & MCI_NOTIFY) {
1510 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1511 mciDriverNotify16((HWND16)LOWORD(lpParms->dwCallback),
1512 wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1519 /*======================================================================*
1520 * MIDI entry points *
1521 *======================================================================*/
1523 /**************************************************************************
1524 * MCIMIDI_DriverProc [sample driver]
1526 LONG CALLBACK MCIMIDI_DriverProc(DWORD dwDevID, HDRVR hDriv, DWORD wMsg,
1527 DWORD dwParam1, DWORD dwParam2)
1530 case DRV_LOAD: return 1;
1531 case DRV_FREE: return 1;
1532 case DRV_OPEN: return MIDI_drvOpen((LPSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSA)dwParam2);
1533 case DRV_CLOSE: return MIDI_drvClose(dwDevID);
1534 case DRV_ENABLE: return 1;
1535 case DRV_DISABLE: return 1;
1536 case DRV_QUERYCONFIGURE: return 1;
1537 case DRV_CONFIGURE: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK); return 1;
1538 case DRV_INSTALL: return DRVCNF_RESTART;
1539 case DRV_REMOVE: return DRVCNF_RESTART;
1540 #ifdef SNDCTL_MIDI_INFO
1541 case MCI_OPEN_DRIVER: return MIDI_mciOpen (dwDevID, dwParam1, (LPMCI_OPEN_PARMSA) dwParam2);
1542 case MCI_CLOSE_DRIVER: return MIDI_mciClose (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1543 case MCI_PLAY: return MIDI_mciPlay (dwDevID, dwParam1, (LPMCI_PLAY_PARMS) dwParam2);
1544 case MCI_RECORD: return MIDI_mciRecord (dwDevID, dwParam1, (LPMCI_RECORD_PARMS) dwParam2);
1545 case MCI_STOP: return MIDI_mciStop (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1546 case MCI_SET: return MIDI_mciSet (dwDevID, dwParam1, (LPMCI_SET_PARMS) dwParam2);
1547 case MCI_PAUSE: return MIDI_mciPause (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1548 case MCI_RESUME: return MIDI_mciResume (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1549 case MCI_STATUS: return MIDI_mciStatus (dwDevID, dwParam1, (LPMCI_STATUS_PARMS) dwParam2);
1550 case MCI_GETDEVCAPS: return MIDI_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1551 case MCI_INFO: return MIDI_mciInfo (dwDevID, dwParam1, (LPMCI_INFO_PARMSA) dwParam2);
1552 case MCI_SEEK: return MIDI_mciSeek (dwDevID, dwParam1, (LPMCI_SEEK_PARMS) dwParam2);
1554 case MCI_OPEN_DRIVER:
1555 case MCI_CLOSE_DRIVER:
1563 case MCI_GETDEVCAPS:
1583 WARN("Unsupported command=%s\n", MCI_CommandToString(wMsg));
1587 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1590 TRACE("Sending msg=%s to default driver proc\n", MCI_CommandToString(wMsg));
1591 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1593 return MCIERR_UNRECOGNIZED_COMMAND;