1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * Sample MIDI Wine Driver for Open Sound System (basically Linux)
6 * Copyright 1994 Martin Ayotte
7 * Copyright 1998 Luiz Otavio L. Zorzella (init procedures)
8 * Copyright 1998/1999 Eric POUECH :
9 * 98/7 changes for making this MIDI driver work on OSS
10 * current support is limited to MIDI ports of OSS systems
11 * 98/9 rewriting MCI code for MIDI
12 * 98/11 splitted in midi.c and mcimidi.c
20 #include <sys/ioctl.h>
28 #include "debugtools.h"
31 DEFAULT_DEBUG_CHANNEL(midi);
35 #define MIDI_SEQ "/dev/sequencer"
40 MIDIOPENDESC midiDesc;
44 unsigned char incoming[3];
45 unsigned char incPrev;
53 MIDIOPENDESC midiDesc;
57 void* lpExtra; /* according to port type (MIDI, FM...), extra data when needed */
60 static WINE_MIDIIN MidiInDev [MAX_MIDIINDRV ];
61 static WINE_MIDIOUT MidiOutDev[MAX_MIDIOUTDRV];
63 /* this is the total number of MIDI out devices found */
64 static int MODM_NUMDEVS = 0;
65 /* this is the number of FM synthetizers (index from 0 to
66 NUMFMSYNTHDEVS - 1) */
67 static int MODM_NUMFMSYNTHDEVS = 0;
68 /* this is the number of Midi ports (index from NUMFMSYNTHDEVS to
69 NUMFMSYNTHDEVS + NUMMIDIDEVS - 1) */
70 static int MODM_NUMMIDIDEVS = 0;
72 /* this is the total number of MIDI out devices found */
73 static int MIDM_NUMDEVS = 0;
75 static int midiSeqFD = -1;
76 static int numOpenMidiSeq = 0;
77 static UINT midiInTimerID = 0;
78 static int numStartedMidiIn = 0;
80 /* this structure holds pointers with information for each MIDI
83 static LPMIDIOUTCAPSA midiOutDevices[MAX_MIDIOUTDRV];
85 /* this structure holds pointers with information for each MIDI
88 static LPMIDIINCAPSA midiInDevices [MAX_MIDIINDRV];
91 * FIXME : all tests on device ID for midXXX and modYYY are made against
92 * MAX_MIDIxxDRV (when they are made) but should be done against the actual
93 * number of midi devices found...
96 /*======================================================================*
97 * Low level MIDI implementation *
98 *======================================================================*/
100 static int midiOpenSeq(void);
101 static int midiCloseSeq(void);
103 /**************************************************************************
104 * unixToWindowsDeviceType [internal]
106 * return the Windows equivalent to a Unix Device Type
109 static int MIDI_UnixToWindowsDeviceType(int type)
111 /* MOD_MIDIPORT output port
112 * MOD_SYNTH generic internal synth
113 * MOD_SQSYNTH square wave internal synth
114 * MOD_FMSYNTH FM internal synth
115 * MOD_MAPPER MIDI mapper
118 /* FIXME Is this really the correct equivalence from UNIX to
119 Windows Sound type */
122 case SYNTH_TYPE_FM: return MOD_FMSYNTH;
123 case SYNTH_TYPE_SAMPLE: return MOD_SYNTH;
124 case SYNTH_TYPE_MIDI: return MOD_MIDIPORT;
126 ERR("Cannot determine the type of this midi device. "
127 "Assuming FM Synth\n");
133 /**************************************************************************
134 * OSS_MidiInit [internal]
136 * Initializes the MIDI devices information variables
138 BOOL OSS_MidiInit(void)
140 int i, status, numsynthdevs = 255, nummididevs = 255;
141 struct synth_info sinfo;
142 struct midi_info minfo;
143 static BOOL bInitDone = FALSE;
148 TRACE("Initializing the MIDI variables.\n");
151 /* try to open device */
152 if (midiOpenSeq() == -1) {
156 /* find how many Synth devices are there in the system */
157 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRSYNTHS, &numsynthdevs);
160 ERR("ioctl for nr synth failed.\n");
165 if (numsynthdevs > MAX_MIDIOUTDRV) {
166 ERR("MAX_MIDIOUTDRV (%d) was enough for the number of devices (%d). "
167 "Some FM devices will not be available.\n",MAX_MIDIOUTDRV,numsynthdevs);
168 numsynthdevs = MAX_MIDIOUTDRV;
171 for (i = 0; i < numsynthdevs; i++) {
172 LPMIDIOUTCAPSA tmplpCaps;
175 status = ioctl(midiSeqFD, SNDCTL_SYNTH_INFO, &sinfo);
177 ERR("ioctl for synth info failed.\n");
182 tmplpCaps = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA));
185 /* We also have the information sinfo.synth_subtype, not used here
188 /* Manufac ID. We do not have access to this with soundcard.h
189 * Does not seem to be a problem, because in mmsystem.h only
190 * Microsoft's ID is listed.
192 tmplpCaps->wMid = 0x00FF;
193 tmplpCaps->wPid = 0x0001; /* FIXME Product ID */
194 /* Product Version. We simply say "1" */
195 tmplpCaps->vDriverVersion = 0x001;
196 strcpy(tmplpCaps->szPname, sinfo.name);
198 tmplpCaps->wTechnology = MIDI_UnixToWindowsDeviceType(sinfo.synth_type);
199 tmplpCaps->wVoices = sinfo.nr_voices;
201 /* FIXME Is it possible to know the maximum
202 * number of simultaneous notes of a soundcard ?
203 * I believe we don't have this information, but
204 * it's probably equal or more than wVoices
206 tmplpCaps->wNotes = sinfo.nr_voices;
208 /* FIXME Do we have this information?
209 * Assuming the soundcards can handle
210 * MIDICAPS_VOLUME and MIDICAPS_LRVOLUME but
211 * not MIDICAPS_CACHE.
213 tmplpCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
215 midiOutDevices[i] = tmplpCaps;
217 if (sinfo.capabilities & SYNTH_CAP_INPUT) {
218 FIXME("Synthesizer support MIDI in. Not supported yet (please report)\n");
221 TRACE("name='%s', techn=%d voices=%d notes=%d support=%ld\n",
222 tmplpCaps->szPname, tmplpCaps->wTechnology,
223 tmplpCaps->wVoices, tmplpCaps->wNotes, tmplpCaps->dwSupport);
224 TRACE("OSS info: synth subtype=%d capa=%lx\n",
225 sinfo.synth_subtype, (long)sinfo.capabilities);
228 /* find how many MIDI devices are there in the system */
229 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRMIDIS, &nummididevs);
231 ERR("ioctl on nr midi failed.\n");
236 /* FIXME: the two restrictions below could be loosen in some cases */
237 if (numsynthdevs + nummididevs > MAX_MIDIOUTDRV) {
238 ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
239 "Some MIDI devices will not be available.\n");
240 nummididevs = MAX_MIDIOUTDRV - numsynthdevs;
243 if (nummididevs > MAX_MIDIINDRV) {
244 ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
245 "Some MIDI devices will not be available.\n",MAX_MIDIINDRV,nummididevs);
246 nummididevs = MAX_MIDIINDRV;
249 for (i = 0; i < nummididevs; i++) {
250 LPMIDIOUTCAPSA tmplpOutCaps;
251 LPMIDIINCAPSA tmplpInCaps;
254 status = ioctl(midiSeqFD, SNDCTL_MIDI_INFO, &minfo);
256 ERR("ioctl on midi info for device %d failed.\n", i);
261 tmplpOutCaps = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA));
264 /* This whole part is somewhat obscure to me. I'll keep trying to dig
265 info about it. If you happen to know, please tell us. The very
266 descritive minfo.dev_type was not used here.
268 /* Manufac ID. We do not have access to this with soundcard.h
269 Does not seem to be a problem, because in mmsystem.h only
270 Microsoft's ID is listed */
271 tmplpOutCaps->wMid = 0x00FF;
272 tmplpOutCaps->wPid = 0x0001; /* FIXME Product ID */
273 /* Product Version. We simply say "1" */
274 tmplpOutCaps->vDriverVersion = 0x001;
275 strcpy(tmplpOutCaps->szPname, minfo.name);
277 tmplpOutCaps->wTechnology = MOD_MIDIPORT; /* FIXME Is this right? */
278 /* Does it make any difference? */
279 tmplpOutCaps->wVoices = 16;
280 /* Does it make any difference? */
281 tmplpOutCaps->wNotes = 16;
282 /* FIXME Does it make any difference? */
283 tmplpOutCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
285 midiOutDevices[numsynthdevs + i] = tmplpOutCaps;
287 tmplpInCaps = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA));
290 /* This whole part is somewhat obscure to me. I'll keep trying to dig
291 info about it. If you happen to know, please tell us. The very
292 descritive minfo.dev_type was not used here.
294 /* Manufac ID. We do not have access to this with soundcard.h
295 Does not seem to be a problem, because in mmsystem.h only
296 Microsoft's ID is listed */
297 tmplpInCaps->wMid = 0x00FF;
298 tmplpInCaps->wPid = 0x0001; /* FIXME Product ID */
299 /* Product Version. We simply say "1" */
300 tmplpInCaps->vDriverVersion = 0x001;
301 strcpy(tmplpInCaps->szPname, minfo.name);
303 /* FIXME : could we get better information than that ? */
304 tmplpInCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
306 midiInDevices[i] = tmplpInCaps;
308 TRACE("name='%s' techn=%d voices=%d notes=%d support=%ld\n",
309 tmplpOutCaps->szPname, tmplpOutCaps->wTechnology, tmplpOutCaps->wVoices,
310 tmplpOutCaps->wNotes, tmplpOutCaps->dwSupport);
311 TRACE("OSS info: midi dev-type=%d, capa=%lx\n",
312 minfo.dev_type, (long)minfo.capabilities);
315 /* windows does not seem to differentiate Synth from MIDI devices */
316 MODM_NUMFMSYNTHDEVS = numsynthdevs;
317 MODM_NUMMIDIDEVS = nummididevs;
318 MODM_NUMDEVS = numsynthdevs + nummididevs;
320 MIDM_NUMDEVS = nummididevs;
322 /* close file and exit */
328 /**************************************************************************
329 * MIDI_NotifyClient [internal]
331 static DWORD MIDI_NotifyClient(UINT wDevID, WORD wMsg,
332 DWORD dwParam1, DWORD dwParam2)
339 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",
340 wDevID, wMsg, dwParam1, dwParam2);
346 if (wDevID > MAX_MIDIOUTDRV)
347 return MCIERR_INTERNAL;
349 dwCallBack = MidiOutDev[wDevID].midiDesc.dwCallback;
350 uFlags = MidiOutDev[wDevID].wFlags;
351 hDev = MidiOutDev[wDevID].midiDesc.hMidi;
352 dwInstance = MidiOutDev[wDevID].midiDesc.dwInstance;
359 if (wDevID > MAX_MIDIINDRV)
360 return MCIERR_INTERNAL;
362 dwCallBack = MidiInDev[wDevID].midiDesc.dwCallback;
363 uFlags = MidiInDev[wDevID].wFlags;
364 hDev = MidiInDev[wDevID].midiDesc.hMidi;
365 dwInstance = MidiInDev[wDevID].midiDesc.dwInstance;
368 WARN("Unsupported MSW-MIDI message %u\n", wMsg);
369 return MCIERR_INTERNAL;
372 return DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2) ?
376 static int midi_warn = 1;
377 /**************************************************************************
378 * midiOpenSeq [internal]
380 static int midiOpenSeq(void)
382 if (numOpenMidiSeq == 0) {
383 midiSeqFD = open(MIDI_SEQ, O_RDWR, 0);
384 if (midiSeqFD == -1) {
387 WARN("Can't open MIDI device '%s' ! (%s). If your "
388 "program needs this (probably not): %s\n",
389 MIDI_SEQ, strerror(errno),
391 "create it ! (\"man MAKEDEV\" ?)" :
393 "load MIDI sequencer kernel driver !" :
395 "grant access ! (\"man chmod\")" : ""
401 if (fcntl(midiSeqFD, F_SETFL, O_NONBLOCK) < 0) {
402 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno, strerror(errno));
407 fcntl(midiSeqFD, F_SETFD, 1); /* set close on exec flag */
408 ioctl(midiSeqFD, SNDCTL_SEQ_RESET);
414 /**************************************************************************
415 * midiCloseSeq [internal]
417 static int midiCloseSeq(void)
419 if (--numOpenMidiSeq == 0) {
426 /* FIXME: this is a bad idea, it's even not static... */
429 /* FIXME: this is not reentrant, not static - because of global variable
432 /**************************************************************************
433 * seqbuf_dump [internal]
435 void seqbuf_dump(void)
438 if (write(midiSeqFD, _seqbuf, _seqbufptr) == -1) {
439 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
440 midiSeqFD, errno, strerror(errno));
443 * in any case buffer is lost so that if many errors occur the buffer
450 static void midReceiveChar(WORD wDevID, unsigned char value, DWORD dwTime)
454 TRACE("Adding %02xh to %d[%d]\n", value, wDevID, MidiInDev[wDevID].incLen);
456 if (wDevID >= MAX_MIDIINDRV) {
460 if (MidiInDev[wDevID].state == 0) {
461 TRACE("input not started, thrown away\n");
465 if (MidiInDev[wDevID].state & 2) { /* system exclusive */
466 LPMIDIHDR lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
470 LPBYTE lpData = lpMidiHdr->lpData;
472 lpData[lpMidiHdr->dwBytesRecorded++] = value;
473 if (lpMidiHdr->dwBytesRecorded == lpMidiHdr->dwBufferLength) {
477 if (value == 0xF7) { /* then end */
478 MidiInDev[wDevID].state &= ~2;
481 if (sbfb && lpMidiHdr != NULL) {
482 lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
483 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
484 lpMidiHdr->dwFlags |= MHDR_DONE;
485 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)lpMidiHdr->lpNext;
486 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD)lpMidiHdr, dwTime) != MMSYSERR_NOERROR) {
487 WARN("Couldn't notify client\n");
493 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
494 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
496 if (!IS_CMD(value) && MidiInDev[wDevID].incLen == 0) { /* try to reuse old cmd */
497 if (IS_CMD(MidiInDev[wDevID].incPrev) && !IS_SYS_CMD(MidiInDev[wDevID].incPrev)) {
498 MidiInDev[wDevID].incoming[0] = MidiInDev[wDevID].incPrev;
499 MidiInDev[wDevID].incLen = 1;
500 TRACE("Reusing old command %02xh\n", MidiInDev[wDevID].incPrev);
502 FIXME("error for midi-in, should generate MIM_ERROR notification:"
503 " prev=%02Xh, incLen=%02Xh\n",
504 MidiInDev[wDevID].incPrev, MidiInDev[wDevID].incLen);
508 MidiInDev[wDevID].incoming[(int)(MidiInDev[wDevID].incLen++)] = value;
509 if (MidiInDev[wDevID].incLen == 1 && !IS_SYS_CMD(MidiInDev[wDevID].incoming[0])) {
510 /* store new cmd, just in case */
511 MidiInDev[wDevID].incPrev = MidiInDev[wDevID].incoming[0];
517 switch (MidiInDev[wDevID].incoming[0] & 0xF0) {
520 case MIDI_KEY_PRESSURE:
521 case MIDI_CTL_CHANGE:
522 case MIDI_PITCH_BEND:
523 if (MidiInDev[wDevID].incLen == 3) {
524 toSend = (MidiInDev[wDevID].incoming[2] << 16) |
525 (MidiInDev[wDevID].incoming[1] << 8) |
526 (MidiInDev[wDevID].incoming[0] << 0);
529 case MIDI_PGM_CHANGE:
530 case MIDI_CHN_PRESSURE:
531 if (MidiInDev[wDevID].incLen == 2) {
532 toSend = (MidiInDev[wDevID].incoming[1] << 8) |
533 (MidiInDev[wDevID].incoming[0] << 0);
536 case MIDI_SYSTEM_PREFIX:
537 if (MidiInDev[wDevID].incoming[0] == 0xF0) {
538 MidiInDev[wDevID].state |= 2;
539 MidiInDev[wDevID].incLen = 0;
541 if (MidiInDev[wDevID].incLen == 1) {
542 toSend = (MidiInDev[wDevID].incoming[0] << 0);
547 WARN("This shouldn't happen (%02X)\n", MidiInDev[wDevID].incoming[0]);
550 TRACE("Sending event %08lx\n", toSend);
551 MidiInDev[wDevID].incLen = 0;
552 dwTime -= MidiInDev[wDevID].startTime;
553 if (MIDI_NotifyClient(wDevID, MIM_DATA, toSend, dwTime) != MMSYSERR_NOERROR) {
554 WARN("Couldn't notify client\n");
559 static VOID WINAPI midTimeCallback(HWND hwnd, UINT msg, UINT id, DWORD dwTime)
561 unsigned char buffer[256];
564 TRACE("(%04X, %d, %d, %lu)\n", hwnd, msg, id, dwTime);
566 len = read(midiSeqFD, buffer, sizeof(buffer));
569 if ((len % 4) != 0) {
570 WARN("Bad length %d, errno %d (%s)\n", len, errno, strerror(errno));
574 for (idx = 0; idx < len; ) {
575 if (buffer[idx] & 0x80) {
577 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
578 buffer[idx + 0], buffer[idx + 1],
579 buffer[idx + 2], buffer[idx + 3],
580 buffer[idx + 4], buffer[idx + 5],
581 buffer[idx + 6], buffer[idx + 7]);
584 switch (buffer[idx + 0]) {
589 midReceiveChar(buffer[idx + 2], buffer[idx + 1], dwTime);
592 TRACE("Unsupported event %d\n", buffer[idx + 0]);
600 /**************************************************************************
601 * midGetDevCaps [internal]
603 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPSA lpCaps, DWORD dwSize)
605 TRACE("(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
607 if (wDevID >= MIDM_NUMDEVS) return MMSYSERR_BADDEVICEID;
608 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
610 memcpy(lpCaps, midiInDevices[wDevID], min(dwSize, sizeof(*lpCaps)));
612 return MMSYSERR_NOERROR;
615 /**************************************************************************
618 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
620 TRACE("(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
622 if (lpDesc == NULL) {
623 WARN("Invalid Parameter !\n");
624 return MMSYSERR_INVALPARAM;
628 * how to check that content of lpDesc is correct ?
630 if (wDevID >= MAX_MIDIINDRV) {
631 WARN("wDevID too large (%u) !\n", wDevID);
632 return MMSYSERR_BADDEVICEID;
634 if (MidiInDev[wDevID].midiDesc.hMidi != 0) {
635 WARN("device already open !\n");
636 return MMSYSERR_ALLOCATED;
638 if ((dwFlags & MIDI_IO_STATUS) != 0) {
639 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
640 dwFlags &= ~MIDI_IO_STATUS;
642 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
643 FIXME("Bad dwFlags\n");
644 return MMSYSERR_INVALFLAG;
647 if (midiOpenSeq() < 0) {
648 return MMSYSERR_ERROR;
651 if (numStartedMidiIn++ == 0) {
652 midiInTimerID = SetTimer(0, 0, 250, midTimeCallback);
653 if (!midiInTimerID) {
654 numStartedMidiIn = 0;
655 WARN("Couldn't start timer for midi-in\n");
657 return MMSYSERR_ERROR;
659 TRACE("Starting timer (%u) for midi-in\n", midiInTimerID);
662 MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
664 MidiInDev[wDevID].lpQueueHdr = NULL;
665 MidiInDev[wDevID].dwTotalPlayed = 0;
666 MidiInDev[wDevID].bufsize = 0x3FFF;
667 MidiInDev[wDevID].midiDesc = *lpDesc;
668 MidiInDev[wDevID].state = 0;
669 MidiInDev[wDevID].incLen = 0;
670 MidiInDev[wDevID].startTime = 0;
672 if (MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
673 WARN("can't notify client !\n");
674 return MMSYSERR_INVALPARAM;
676 return MMSYSERR_NOERROR;
679 /**************************************************************************
680 * midClose [internal]
682 static DWORD midClose(WORD wDevID)
684 int ret = MMSYSERR_NOERROR;
686 TRACE("(%04X);\n", wDevID);
688 if (wDevID >= MAX_MIDIINDRV) {
689 WARN("wDevID too big (%u) !\n", wDevID);
690 return MMSYSERR_BADDEVICEID;
692 if (MidiInDev[wDevID].midiDesc.hMidi == 0) {
693 WARN("device not opened !\n");
694 return MMSYSERR_ERROR;
696 if (MidiInDev[wDevID].lpQueueHdr != 0) {
697 return MIDIERR_STILLPLAYING;
700 if (midiSeqFD == -1) {
702 return MMSYSERR_ERROR;
704 if (--numStartedMidiIn == 0) {
705 TRACE("Stopping timer for midi-in\n");
706 if (!KillTimer(0, midiInTimerID)) {
707 WARN("Couldn't stop timer for midi-in\n");
713 MidiInDev[wDevID].bufsize = 0;
714 if (MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
715 WARN("can't notify client !\n");
716 ret = MMSYSERR_INVALPARAM;
718 MidiInDev[wDevID].midiDesc.hMidi = 0;
722 /**************************************************************************
723 * midAddBuffer [internal]
725 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
727 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
729 if (lpMidiHdr == NULL) return MMSYSERR_INVALPARAM;
730 if (sizeof(MIDIHDR) > dwSize) return MMSYSERR_INVALPARAM;
731 if (lpMidiHdr->dwBufferLength == 0) return MMSYSERR_INVALPARAM;
732 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
733 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
735 if (MidiInDev[wDevID].lpQueueHdr == 0) {
736 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr;
740 for (ptr = MidiInDev[wDevID].lpQueueHdr;
742 ptr = (LPMIDIHDR)ptr->lpNext);
743 ptr->lpNext = (struct midihdr_tag*)lpMidiHdr;
745 return MMSYSERR_NOERROR;
748 /**************************************************************************
749 * midPrepare [internal]
751 static DWORD midPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
753 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
755 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
756 lpMidiHdr->lpData == 0 || lpMidiHdr->dwFlags != 0 ||
757 lpMidiHdr->dwBufferLength >= 0x10000ul)
758 return MMSYSERR_INVALPARAM;
760 lpMidiHdr->lpNext = 0;
761 lpMidiHdr->dwFlags |= MHDR_PREPARED;
762 lpMidiHdr->dwBytesRecorded = 0;
764 return MMSYSERR_NOERROR;
767 /**************************************************************************
768 * midUnprepare [internal]
770 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
772 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
774 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
775 lpMidiHdr->lpData == 0 || lpMidiHdr->dwBufferLength >= 0x10000ul)
776 return MMSYSERR_INVALPARAM;
778 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
779 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
781 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
783 return MMSYSERR_NOERROR;
786 /**************************************************************************
787 * midReset [internal]
789 static DWORD midReset(WORD wDevID)
791 DWORD dwTime = GetTickCount();
793 TRACE("(%04X);\n", wDevID);
795 while (MidiInDev[wDevID].lpQueueHdr) {
796 MidiInDev[wDevID].lpQueueHdr->dwFlags &= ~MHDR_INQUEUE;
797 MidiInDev[wDevID].lpQueueHdr->dwFlags |= MHDR_DONE;
798 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
799 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA,
800 (DWORD)MidiInDev[wDevID].lpQueueHdr, dwTime) != MMSYSERR_NOERROR) {
801 WARN("Couldn't notify client\n");
803 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)MidiInDev[wDevID].lpQueueHdr->lpNext;
806 return MMSYSERR_NOERROR;
810 /**************************************************************************
811 * midStart [internal]
813 static DWORD midStart(WORD wDevID)
815 TRACE("(%04X);\n", wDevID);
817 /* FIXME : should test value of wDevID */
819 MidiInDev[wDevID].state = 1;
820 MidiInDev[wDevID].startTime = GetTickCount();
821 return MMSYSERR_NOERROR;
824 /**************************************************************************
827 static DWORD midStop(WORD wDevID)
829 TRACE("(%04X);\n", wDevID);
831 /* FIXME : should test value of wDevID */
832 MidiInDev[wDevID].state = 0;
833 return MMSYSERR_NOERROR;
836 /*-----------------------------------------------------------------------*/
838 typedef struct sVoice {
839 int note; /* 0 means not used */
841 unsigned cntMark : 30,
844 #define sVS_PLAYING 1
845 #define sVS_SUSTAINED 2
848 typedef struct sChannel {
854 int bank; /* CTL_BANK_SELECT */
855 int volume; /* CTL_MAIN_VOLUME */
856 int balance; /* CTL_BALANCE */
857 int expression; /* CTL_EXPRESSION */
858 int sustain; /* CTL_SUSTAIN */
860 unsigned char nrgPmtMSB; /* Non register Parameters */
861 unsigned char nrgPmtLSB;
862 unsigned char regPmtMSB; /* Non register Parameters */
863 unsigned char regPmtLSB;
866 typedef struct sFMextra {
869 sChannel channel[16]; /* MIDI has only 16 channels */
870 sVoice voice[1]; /* dyn allocated according to sound card */
871 /* do not append fields below voice[1] since the size of this structure
872 * depends on the number of available voices on the FM synth...
876 extern unsigned char midiFMInstrumentPatches[16 * 128];
877 extern unsigned char midiFMDrumsPatches [16 * 128];
879 /**************************************************************************
880 * modFMLoad [internal]
882 static int modFMLoad(int dev)
885 struct sbi_instrument sbi;
890 memset(sbi.operators + 16, 0, 16);
891 for (i = 0; i < 128; i++) {
893 memcpy(sbi.operators, midiFMInstrumentPatches + i * 16, 16);
895 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
896 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
900 for (i = 0; i < 128; i++) {
901 sbi.channel = 128 + i;
902 memcpy(sbi.operators, midiFMDrumsPatches + i * 16, 16);
904 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
905 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
912 /**************************************************************************
913 * modFMReset [internal]
915 static void modFMReset(WORD wDevID)
917 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
918 sVoice* voice = extra->voice;
919 sChannel* channel = extra->channel;
922 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
923 if (voice[i].status != sVS_UNUSED) {
924 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
926 SEQ_KEY_PRESSURE(wDevID, i, 127, 0);
927 SEQ_CONTROL(wDevID, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
929 voice[i].channel = -1;
930 voice[i].cntMark = 0;
931 voice[i].status = sVS_UNUSED;
933 for (i = 0; i < 16; i++) {
934 channel[i].program = 0;
935 channel[i].bender = 8192;
936 channel[i].benderRange = 2;
938 channel[i].volume = 127;
939 channel[i].balance = 64;
940 channel[i].expression = 0;
941 channel[i].sustain = 0;
944 extra->drumSetMask = 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
948 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
950 /**************************************************************************
951 * modGetDevCaps [internal]
953 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPSA lpCaps, DWORD dwSize)
955 TRACE("(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
957 if (wDevID >= MODM_NUMDEVS) return MMSYSERR_BADDEVICEID;
958 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
960 memcpy(lpCaps, midiOutDevices[wDevID], min(dwSize, sizeof(*lpCaps)));
962 return MMSYSERR_NOERROR;
965 /**************************************************************************
968 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
970 TRACE("(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
971 if (lpDesc == NULL) {
972 WARN("Invalid Parameter !\n");
973 return MMSYSERR_INVALPARAM;
975 if (wDevID >= MAX_MIDIOUTDRV) {
976 TRACE("MAX_MIDIOUTDRV reached !\n");
977 return MMSYSERR_BADDEVICEID;
979 if (MidiOutDev[wDevID].midiDesc.hMidi != 0) {
980 WARN("device already open !\n");
981 return MMSYSERR_ALLOCATED;
983 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
984 WARN("bad dwFlags\n");
985 return MMSYSERR_INVALFLAG;
987 if (midiOutDevices[wDevID] == NULL) {
988 TRACE("un-allocated wDevID\n");
989 return MMSYSERR_BADDEVICEID;
992 MidiOutDev[wDevID].lpExtra = 0;
994 switch (midiOutDevices[wDevID]->wTechnology) {
997 void* extra = HeapAlloc(GetProcessHeap(), 0,
998 sizeof(struct sFMextra) +
999 sizeof(struct sVoice) * (midiOutDevices[wDevID]->wVoices - 1));
1002 WARN("can't alloc extra data !\n");
1003 return MMSYSERR_NOMEM;
1005 MidiOutDev[wDevID].lpExtra = extra;
1006 if (midiOpenSeq() < 0) {
1007 MidiOutDev[wDevID].lpExtra = 0;
1008 HeapFree(GetProcessHeap(), 0, extra);
1009 return MMSYSERR_ERROR;
1011 if (modFMLoad(wDevID) < 0) {
1013 MidiOutDev[wDevID].lpExtra = 0;
1014 HeapFree(GetProcessHeap(), 0, extra);
1015 return MMSYSERR_ERROR;
1021 if (midiOpenSeq() < 0) {
1022 return MMSYSERR_ALLOCATED;
1026 WARN("Technology not supported (yet) %d !\n",
1027 midiOutDevices[wDevID]->wTechnology);
1028 return MMSYSERR_NOTENABLED;
1031 MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1033 MidiOutDev[wDevID].lpQueueHdr = NULL;
1034 MidiOutDev[wDevID].dwTotalPlayed = 0;
1035 MidiOutDev[wDevID].bufsize = 0x3FFF;
1036 MidiOutDev[wDevID].midiDesc = *lpDesc;
1038 if (MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
1039 WARN("can't notify client !\n");
1040 return MMSYSERR_INVALPARAM;
1042 TRACE("Successful !\n");
1043 return MMSYSERR_NOERROR;
1047 /**************************************************************************
1048 * modClose [internal]
1050 static DWORD modClose(WORD wDevID)
1052 int ret = MMSYSERR_NOERROR;
1054 TRACE("(%04X);\n", wDevID);
1056 if (MidiOutDev[wDevID].midiDesc.hMidi == 0) {
1057 WARN("device not opened !\n");
1058 return MMSYSERR_ERROR;
1060 /* FIXME: should test that no pending buffer is still in the queue for
1063 if (midiSeqFD == -1) {
1064 WARN("can't close !\n");
1065 return MMSYSERR_ERROR;
1068 switch (midiOutDevices[wDevID]->wTechnology) {
1074 WARN("Technology not supported (yet) %d !\n",
1075 midiOutDevices[wDevID]->wTechnology);
1076 return MMSYSERR_NOTENABLED;
1079 if (MidiOutDev[wDevID].lpExtra != 0) {
1080 HeapFree(GetProcessHeap(), 0, MidiOutDev[wDevID].lpExtra);
1081 MidiOutDev[wDevID].lpExtra = 0;
1084 MidiOutDev[wDevID].bufsize = 0;
1085 if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
1086 WARN("can't notify client !\n");
1087 ret = MMSYSERR_INVALPARAM;
1089 MidiOutDev[wDevID].midiDesc.hMidi = 0;
1093 /**************************************************************************
1094 * modData [internal]
1096 static DWORD modData(WORD wDevID, DWORD dwParam)
1098 WORD evt = LOBYTE(LOWORD(dwParam));
1099 WORD d1 = HIBYTE(LOWORD(dwParam));
1100 WORD d2 = LOBYTE(HIWORD(dwParam));
1102 TRACE("(%04X, %08lX);\n", wDevID, dwParam);
1104 if (midiSeqFD == -1) {
1105 WARN("can't play !\n");
1106 return MIDIERR_NODEVICE;
1108 switch (midiOutDevices[wDevID]->wTechnology) {
1111 * - chorus depth controller is not used
1114 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
1115 sVoice* voice = extra->voice;
1116 sChannel* channel = extra->channel;
1117 int chn = (evt & 0x0F);
1120 switch (evt & 0xF0) {
1122 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1123 /* don't stop sustained notes */
1124 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1125 voice[i].status = sVS_UNUSED;
1126 SEQ_STOP_NOTE(wDevID, i, d1, d2);
1131 if (d2 == 0) { /* note off if velocity == 0 */
1132 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1133 /* don't stop sustained notes */
1134 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1135 voice[i].status = sVS_UNUSED;
1136 SEQ_STOP_NOTE(wDevID, i, d1, 64);
1141 /* finding out in this order :
1143 * - if replaying the same note on the same channel
1144 * - the older voice (LRU)
1146 for (i = nv = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1147 if (voice[i].status == sVS_UNUSED ||
1148 (voice[i].note == d1 && voice[i].channel == chn)) {
1152 if (voice[i].cntMark < voice[0].cntMark) {
1157 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1158 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1159 nv, channel[chn].program,
1160 channel[chn].balance,
1161 channel[chn].volume,
1162 channel[chn].bender, d1, d2);
1164 SEQ_SET_PATCH(wDevID, nv, IS_DRUM_CHANNEL(extra, chn) ?
1165 (128 + d1) : channel[chn].program);
1166 SEQ_BENDER_RANGE(wDevID, nv, channel[chn].benderRange * 100);
1167 SEQ_BENDER(wDevID, nv, channel[chn].bender);
1168 SEQ_CONTROL(wDevID, nv, CTL_PAN, channel[chn].balance);
1169 SEQ_CONTROL(wDevID, nv, CTL_EXPRESSION, channel[chn].expression);
1171 /* FIXME: does not really seem to work on my SB card and
1172 * screws everything up... so lay it down
1174 SEQ_CONTROL(wDevID, nv, CTL_MAIN_VOLUME, channel[chn].volume);
1176 SEQ_START_NOTE(wDevID, nv, d1, d2);
1177 voice[nv].status = channel[chn].sustain ? sVS_SUSTAINED : sVS_PLAYING;
1178 voice[nv].note = d1;
1179 voice[nv].channel = chn;
1180 voice[nv].cntMark = extra->counter++;
1182 case MIDI_KEY_PRESSURE:
1183 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1184 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn && voice[i].note == d1) {
1185 SEQ_KEY_PRESSURE(wDevID, i, d1, d2);
1189 case MIDI_CTL_CHANGE:
1191 case CTL_BANK_SELECT: channel[chn].bank = d2; break;
1192 case CTL_MAIN_VOLUME: channel[chn].volume = d2; break;
1193 case CTL_PAN: channel[chn].balance = d2; break;
1194 case CTL_EXPRESSION: channel[chn].expression = d2; break;
1195 case CTL_SUSTAIN: channel[chn].sustain = d2;
1197 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1198 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1199 voice[i].status = sVS_SUSTAINED;
1203 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1204 if (voice[i].status == sVS_SUSTAINED && voice[i].channel == chn) {
1205 voice[i].status = sVS_UNUSED;
1206 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1211 case CTL_NONREG_PARM_NUM_LSB: channel[chn].nrgPmtLSB = d2; break;
1212 case CTL_NONREG_PARM_NUM_MSB: channel[chn].nrgPmtMSB = d2; break;
1213 case CTL_REGIST_PARM_NUM_LSB: channel[chn].regPmtLSB = d2; break;
1214 case CTL_REGIST_PARM_NUM_MSB: channel[chn].regPmtMSB = d2; break;
1215 case CTL_DATA_ENTRY:
1216 switch ((channel[chn].regPmtMSB << 8) | channel[chn].regPmtLSB) {
1218 if (channel[chn].benderRange != d2) {
1219 channel[chn].benderRange = d2;
1220 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1221 if (voice[i].channel == chn) {
1222 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1229 channel[chn].benderRange = 2;
1230 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1231 if (voice[i].channel == chn) {
1232 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1237 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1238 channel[chn].regPmtMSB, channel[chn].regPmtLSB,
1239 channel[chn].nrgPmtMSB, channel[chn].nrgPmtLSB,
1245 case 0x78: /* all sounds off */
1246 /* FIXME: I don't know if I have to take care of the channel
1247 * for this control ?
1249 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1250 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1251 voice[i].status = sVS_UNUSED;
1252 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1256 case 0x7B: /* all notes off */
1257 /* FIXME: I don't know if I have to take care of the channel
1258 * for this control ?
1260 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1261 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1262 voice[i].status = sVS_UNUSED;
1263 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1268 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1273 case MIDI_PGM_CHANGE:
1274 channel[chn].program = d1;
1276 case MIDI_CHN_PRESSURE:
1277 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1278 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1279 SEQ_KEY_PRESSURE(wDevID, i, voice[i].note, d1);
1283 case MIDI_PITCH_BEND:
1284 channel[chn].bender = (d2 << 7) + d1;
1285 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1286 if (voice[i].channel == chn) {
1287 SEQ_BENDER(wDevID, i, channel[chn].bender);
1291 case MIDI_SYSTEM_PREFIX:
1292 switch (evt & 0x0F) {
1293 case 0x0F: /* Reset */
1297 WARN("Unsupported (yet) system event %02x\n", evt & 0x0F);
1301 WARN("Internal error, shouldn't happen (event=%08x)\n", evt & 0xF0);
1302 return MMSYSERR_NOTENABLED;
1308 int dev = wDevID - MODM_NUMFMSYNTHDEVS;
1310 WARN("Internal error on devID (%u) !\n", wDevID);
1311 return MIDIERR_NODEVICE;
1314 switch (evt & 0xF0) {
1317 case MIDI_KEY_PRESSURE:
1318 case MIDI_CTL_CHANGE:
1319 case MIDI_PITCH_BEND:
1320 SEQ_MIDIOUT(dev, evt);
1321 SEQ_MIDIOUT(dev, d1);
1322 SEQ_MIDIOUT(dev, d2);
1324 case MIDI_PGM_CHANGE:
1325 case MIDI_CHN_PRESSURE:
1326 SEQ_MIDIOUT(dev, evt);
1327 SEQ_MIDIOUT(dev, d1);
1329 case MIDI_SYSTEM_PREFIX:
1330 switch (evt & 0x0F) {
1331 case 0x00: /* System Exclusive, don't do it on modData,
1332 * should require modLongData*/
1333 case 0x01: /* Undefined */
1334 case 0x04: /* Undefined. */
1335 case 0x05: /* Undefined. */
1336 case 0x07: /* End of Exclusive. */
1337 case 0x09: /* Undefined. */
1338 case 0x0D: /* Undefined. */
1340 case 0x06: /* Tune Request */
1341 case 0x08: /* Timing Clock. */
1342 case 0x0A: /* Start. */
1343 case 0x0B: /* Continue */
1344 case 0x0C: /* Stop */
1345 case 0x0E: /* Active Sensing. */
1346 SEQ_MIDIOUT(dev, evt);
1348 case 0x0F: /* Reset */
1349 /* SEQ_MIDIOUT(dev, evt);
1350 this other way may be better */
1351 SEQ_MIDIOUT(dev, MIDI_SYSTEM_PREFIX);
1352 SEQ_MIDIOUT(dev, 0x7e);
1353 SEQ_MIDIOUT(dev, 0x7f);
1354 SEQ_MIDIOUT(dev, 0x09);
1355 SEQ_MIDIOUT(dev, 0x01);
1356 SEQ_MIDIOUT(dev, 0xf7);
1358 case 0x03: /* Song Select. */
1359 SEQ_MIDIOUT(dev, evt);
1360 SEQ_MIDIOUT(dev, d1);
1361 case 0x02: /* Song Position Pointer. */
1362 SEQ_MIDIOUT(dev, evt);
1363 SEQ_MIDIOUT(dev, d1);
1364 SEQ_MIDIOUT(dev, d2);
1371 WARN("Technology not supported (yet) %d !\n",
1372 midiOutDevices[wDevID]->wTechnology);
1373 return MMSYSERR_NOTENABLED;
1378 return MMSYSERR_NOERROR;
1381 /**************************************************************************
1382 * modLongData [internal]
1384 static DWORD modLongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1389 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1391 if (midiSeqFD == -1) {
1392 WARN("can't play !\n");
1393 return MIDIERR_NODEVICE;
1396 lpData = lpMidiHdr->lpData;
1399 return MIDIERR_UNPREPARED;
1400 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1401 return MIDIERR_UNPREPARED;
1402 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1403 return MIDIERR_STILLPLAYING;
1404 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1405 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1407 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1408 * data, or can it also contain raw MIDI data, to be split up and sent to
1410 * If the latest is true, then the following WARNing will fire up
1412 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
1413 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1416 TRACE("dwBufferLength=%lu !\n", lpMidiHdr->dwBufferLength);
1417 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1418 lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
1419 lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
1421 switch (midiOutDevices[wDevID]->wTechnology) {
1423 /* FIXME: I don't think there is much to do here */
1426 if (lpData[0] != 0xF0) {
1427 /* Send end of System Exclusive */
1428 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF0);
1429 WARN("Adding missing 0xF0 marker at the begining of "
1430 "system exclusive byte stream\n");
1432 for (count = 0; count < lpMidiHdr->dwBytesRecorded; count++) {
1433 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, lpData[count]);
1435 if (lpData[count - 1] != 0xF7) {
1436 /* Send end of System Exclusive */
1437 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF7);
1438 WARN("Adding missing 0xF7 marker at the end of "
1439 "system exclusive byte stream\n");
1444 WARN("Technology not supported (yet) %d !\n",
1445 midiOutDevices[wDevID]->wTechnology);
1446 return MMSYSERR_NOTENABLED;
1449 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1450 lpMidiHdr->dwFlags |= MHDR_DONE;
1451 if (MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD)lpMidiHdr, 0L) != MMSYSERR_NOERROR) {
1452 WARN("can't notify client !\n");
1453 return MMSYSERR_INVALPARAM;
1455 return MMSYSERR_NOERROR;
1458 /**************************************************************************
1459 * modPrepare [internal]
1461 static DWORD modPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1463 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1465 if (midiSeqFD == -1) {
1466 WARN("can't prepare !\n");
1467 return MMSYSERR_NOTENABLED;
1470 /* MS doc says taht dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
1471 * asks to prepare MIDIHDR which dwFlags != 0.
1472 * So at least check for the inqueue flag
1474 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
1475 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
1476 lpMidiHdr->dwBufferLength >= 0x10000ul) {
1477 WARN("%p %p %08lx %d/%ld\n", lpMidiHdr, lpMidiHdr->lpData,
1478 lpMidiHdr->dwFlags, sizeof(MIDIHDR), dwSize);
1479 return MMSYSERR_INVALPARAM;
1482 lpMidiHdr->lpNext = 0;
1483 lpMidiHdr->dwFlags |= MHDR_PREPARED;
1484 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1485 return MMSYSERR_NOERROR;
1488 /**************************************************************************
1489 * modUnprepare [internal]
1491 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1493 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1495 if (midiSeqFD == -1) {
1496 WARN("can't unprepare !\n");
1497 return MMSYSERR_NOTENABLED;
1500 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0)
1501 return MMSYSERR_INVALPARAM;
1502 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1503 return MIDIERR_STILLPLAYING;
1504 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
1505 return MMSYSERR_NOERROR;
1508 /**************************************************************************
1509 * modReset [internal]
1511 static DWORD modReset(WORD wDevID)
1515 TRACE("(%04X);\n", wDevID);
1517 /* stop all notes */
1518 /* FIXME: check if 0x78B0 is channel dependant or not. I coded it so that
1519 * it's channel dependent...
1521 for (chn = 0; chn < 16; chn++) {
1522 /* turn off every note */
1523 modData(wDevID, 0x7800 | MIDI_CTL_CHANGE | chn);
1524 /* remove sustain on all channels */
1525 modData(wDevID, (CTL_SUSTAIN << 8) | MIDI_CTL_CHANGE | chn);
1527 /* FIXME: the LongData buffers must also be returned to the app */
1528 return MMSYSERR_NOERROR;
1531 #endif /* HAVE_OSS_MIDI */
1533 /*======================================================================*
1534 * MIDI entry points *
1535 *======================================================================*/
1537 /**************************************************************************
1538 * OSS_midMessage [sample driver]
1540 DWORD WINAPI OSS_midMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1541 DWORD dwParam1, DWORD dwParam2)
1543 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1544 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1546 #ifdef HAVE_OSS_MIDI
1550 /* FIXME: Pretend this is supported */
1553 return midOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1555 return midClose(wDevID);
1556 case MIDM_ADDBUFFER:
1557 return midAddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1559 return midPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1560 case MIDM_UNPREPARE:
1561 return midUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1562 case MIDM_GETDEVCAPS:
1563 return midGetDevCaps(wDevID, (LPMIDIINCAPSA)dwParam1,dwParam2);
1564 case MIDM_GETNUMDEVS:
1565 return MIDM_NUMDEVS;
1567 return midReset(wDevID);
1569 return midStart(wDevID);
1571 return midStop(wDevID);
1574 TRACE("Unsupported message\n");
1576 return MMSYSERR_NOTSUPPORTED;
1579 /**************************************************************************
1580 * OSS_modMessage [sample driver]
1582 DWORD WINAPI OSS_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1583 DWORD dwParam1, DWORD dwParam2)
1585 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1586 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1589 #ifdef HAVE_OSS_MIDI
1594 /* FIXME: Pretend this is supported */
1597 return modOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1599 return modClose(wDevID);
1601 return modData(wDevID, dwParam1);
1603 return modLongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1605 return modPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1606 case MODM_UNPREPARE:
1607 return modUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1608 case MODM_GETDEVCAPS:
1609 return modGetDevCaps(wDevID, (LPMIDIOUTCAPSA)dwParam1, dwParam2);
1610 case MODM_GETNUMDEVS:
1611 return MODM_NUMDEVS;
1612 case MODM_GETVOLUME:
1614 case MODM_SETVOLUME:
1617 return modReset(wDevID);
1620 TRACE("Unsupported message\n");
1622 return MMSYSERR_NOTSUPPORTED;
1625 /*-----------------------------------------------------------------------*/