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>
26 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(midi)
34 #define MIDI_SEQ "/dev/sequencer"
39 LPMIDIOPENDESC midiDesc;
43 unsigned char incoming[3];
44 unsigned char incPrev;
52 LPMIDIOPENDESC midiDesc;
56 void* lpExtra; /* according to port type (MIDI, FM...), extra data when needed */
59 static WINE_MIDIIN MidiInDev [MAX_MIDIINDRV ];
60 static WINE_MIDIOUT MidiOutDev[MAX_MIDIOUTDRV];
62 /* this is the total number of MIDI out devices found */
63 static int MODM_NUMDEVS = 0;
64 /* this is the number of FM synthetizers (index from 0 to
65 NUMFMSYNTHDEVS - 1) */
66 static int MODM_NUMFMSYNTHDEVS = 0;
67 /* this is the number of Midi ports (index from NUMFMSYNTHDEVS to
68 NUMFMSYNTHDEVS + NUMMIDIDEVS - 1) */
69 static int MODM_NUMMIDIDEVS = 0;
71 /* this is the total number of MIDI out devices found */
72 static int MIDM_NUMDEVS = 0;
74 static int midiSeqFD = -1;
75 static int numOpenMidiSeq = 0;
76 static UINT midiInTimerID = 0;
77 static int numStartedMidiIn = 0;
79 /* this structure holds pointers with information for each MIDI
82 static LPMIDIOUTCAPSA midiOutDevices[MAX_MIDIOUTDRV];
84 /* this structure holds pointers with information for each MIDI
87 static LPMIDIINCAPSA midiInDevices [MAX_MIDIINDRV];
90 * FIXME : all tests on device ID for midXXX and modYYY are made against
91 * MAX_MIDIxxDRV (when they are made) but should be done against the actual
92 * number of midi devices found...
95 /*======================================================================*
96 * Low level MIDI implementation *
97 *======================================================================*/
99 static int midiOpenSeq(void);
100 static int midiCloseSeq(void);
102 /**************************************************************************
103 * unixToWindowsDeviceType [internal]
105 * return the Windows equivalent to a Unix Device Type
108 static int MIDI_UnixToWindowsDeviceType(int type)
110 /* MOD_MIDIPORT output port
111 * MOD_SYNTH generic internal synth
112 * MOD_SQSYNTH square wave internal synth
113 * MOD_FMSYNTH FM internal synth
114 * MOD_MAPPER MIDI mapper
117 /* FIXME Is this really the correct equivalence from UNIX to
118 Windows Sound type */
121 case SYNTH_TYPE_FM: return MOD_FMSYNTH;
122 case SYNTH_TYPE_SAMPLE: return MOD_SYNTH;
123 case SYNTH_TYPE_MIDI: return MOD_MIDIPORT;
125 ERR("Cannot determine the type of this midi device. "
126 "Assuming FM Synth\n");
132 /**************************************************************************
133 * OSS_MidiInit [internal]
135 * Initializes the MIDI devices information variables
137 BOOL OSS_MidiInit(void)
139 int i, status, numsynthdevs = 255, nummididevs = 255;
140 struct synth_info sinfo;
141 struct midi_info minfo;
142 static BOOL bInitDone = FALSE;
147 TRACE("Initializing the MIDI variables.\n");
150 /* try to open device */
151 if (midiOpenSeq() == -1) {
155 /* find how many Synth devices are there in the system */
156 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRSYNTHS, &numsynthdevs);
159 ERR("ioctl for nr synth failed.\n");
164 if (numsynthdevs > MAX_MIDIOUTDRV) {
165 ERR("MAX_MIDIOUTDRV (%d) was enough for the number of devices (%d). "
166 "Some FM devices will not be available.\n",MAX_MIDIOUTDRV,numsynthdevs);
167 numsynthdevs = MAX_MIDIOUTDRV;
170 for (i = 0; i < numsynthdevs; i++) {
171 LPMIDIOUTCAPSA tmplpCaps;
174 status = ioctl(midiSeqFD, SNDCTL_SYNTH_INFO, &sinfo);
176 ERR("ioctl for synth info failed.\n");
181 tmplpCaps = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA));
184 /* We also have the information sinfo.synth_subtype, not used here
187 /* Manufac ID. We do not have access to this with soundcard.h
188 * Does not seem to be a problem, because in mmsystem.h only
189 * Microsoft's ID is listed.
191 tmplpCaps->wMid = 0x00FF;
192 tmplpCaps->wPid = 0x0001; /* FIXME Product ID */
193 /* Product Version. We simply say "1" */
194 tmplpCaps->vDriverVersion = 0x001;
195 strcpy(tmplpCaps->szPname, sinfo.name);
197 tmplpCaps->wTechnology = MIDI_UnixToWindowsDeviceType(sinfo.synth_type);
198 tmplpCaps->wVoices = sinfo.nr_voices;
200 /* FIXME Is it possible to know the maximum
201 * number of simultaneous notes of a soundcard ?
202 * I believe we don't have this information, but
203 * it's probably equal or more than wVoices
205 tmplpCaps->wNotes = sinfo.nr_voices;
207 /* FIXME Do we have this information?
208 * Assuming the soundcards can handle
209 * MIDICAPS_VOLUME and MIDICAPS_LRVOLUME but
210 * not MIDICAPS_CACHE.
212 tmplpCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
214 midiOutDevices[i] = tmplpCaps;
216 if (sinfo.capabilities & SYNTH_CAP_INPUT) {
217 FIXME("Synthetizer support MIDI in. Not supported yet (please report)\n");
220 TRACE("name='%s', techn=%d voices=%d notes=%d support=%ld\n",
221 tmplpCaps->szPname, tmplpCaps->wTechnology,
222 tmplpCaps->wVoices, tmplpCaps->wNotes, tmplpCaps->dwSupport);
223 TRACE("OSS info: synth subtype=%d capa=%lx\n",
224 sinfo.synth_subtype, (long)sinfo.capabilities);
227 /* find how many MIDI devices are there in the system */
228 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRMIDIS, &nummididevs);
230 ERR("ioctl on nr midi failed.\n");
235 /* FIXME: the two restrictions below could be loosen in some cases */
236 if (numsynthdevs + nummididevs > MAX_MIDIOUTDRV) {
237 ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
238 "Some MIDI devices will not be available.\n");
239 nummididevs = MAX_MIDIOUTDRV - numsynthdevs;
242 if (nummididevs > MAX_MIDIINDRV) {
243 ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
244 "Some MIDI devices will not be available.\n",MAX_MIDIINDRV,nummididevs);
245 nummididevs = MAX_MIDIINDRV;
248 for (i = 0; i < nummididevs; i++) {
249 LPMIDIOUTCAPSA tmplpOutCaps;
250 LPMIDIINCAPSA tmplpInCaps;
253 status = ioctl(midiSeqFD, SNDCTL_MIDI_INFO, &minfo);
255 ERR("ioctl on midi info for device %d failed.\n", i);
260 tmplpOutCaps = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA));
263 /* This whole part is somewhat obscure to me. I'll keep trying to dig
264 info about it. If you happen to know, please tell us. The very
265 descritive minfo.dev_type was not used here.
267 /* Manufac ID. We do not have access to this with soundcard.h
268 Does not seem to be a problem, because in mmsystem.h only
269 Microsoft's ID is listed */
270 tmplpOutCaps->wMid = 0x00FF;
271 tmplpOutCaps->wPid = 0x0001; /* FIXME Product ID */
272 /* Product Version. We simply say "1" */
273 tmplpOutCaps->vDriverVersion = 0x001;
274 strcpy(tmplpOutCaps->szPname, minfo.name);
276 tmplpOutCaps->wTechnology = MOD_MIDIPORT; /* FIXME Is this right? */
277 /* Does it make any difference? */
278 tmplpOutCaps->wVoices = 16;
279 /* Does it make any difference? */
280 tmplpOutCaps->wNotes = 16;
281 /* FIXME Does it make any difference? */
282 tmplpOutCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
284 midiOutDevices[numsynthdevs + i] = tmplpOutCaps;
286 tmplpInCaps = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA));
289 /* This whole part is somewhat obscure to me. I'll keep trying to dig
290 info about it. If you happen to know, please tell us. The very
291 descritive minfo.dev_type was not used here.
293 /* Manufac ID. We do not have access to this with soundcard.h
294 Does not seem to be a problem, because in mmsystem.h only
295 Microsoft's ID is listed */
296 tmplpInCaps->wMid = 0x00FF;
297 tmplpInCaps->wPid = 0x0001; /* FIXME Product ID */
298 /* Product Version. We simply say "1" */
299 tmplpInCaps->vDriverVersion = 0x001;
300 strcpy(tmplpInCaps->szPname, minfo.name);
302 /* FIXME : could we get better information than that ? */
303 tmplpInCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
305 midiInDevices[i] = tmplpInCaps;
307 TRACE("name='%s' techn=%d voices=%d notes=%d support=%ld\n",
308 tmplpOutCaps->szPname, tmplpOutCaps->wTechnology, tmplpOutCaps->wVoices,
309 tmplpOutCaps->wNotes, tmplpOutCaps->dwSupport);
310 TRACE("OSS info: midi dev-type=%d, capa=%lx\n",
311 minfo.dev_type, (long)minfo.capabilities);
314 /* windows does not seem to differentiate Synth from MIDI devices */
315 MODM_NUMFMSYNTHDEVS = numsynthdevs;
316 MODM_NUMMIDIDEVS = nummididevs;
317 MODM_NUMDEVS = numsynthdevs + nummididevs;
319 MIDM_NUMDEVS = nummididevs;
321 /* close file and exit */
327 /**************************************************************************
328 * MIDI_NotifyClient [internal]
330 static DWORD MIDI_NotifyClient(UINT wDevID, WORD wMsg,
331 DWORD dwParam1, DWORD dwParam2)
338 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",
339 wDevID, wMsg, dwParam1, dwParam2);
345 if (wDevID > MAX_MIDIOUTDRV)
346 return MCIERR_INTERNAL;
348 dwCallBack = MidiOutDev[wDevID].midiDesc->dwCallback;
349 uFlags = MidiOutDev[wDevID].wFlags;
350 hDev = MidiOutDev[wDevID].midiDesc->hMidi;
351 dwInstance = MidiOutDev[wDevID].midiDesc->dwInstance;
358 if (wDevID > MAX_MIDIINDRV)
359 return MCIERR_INTERNAL;
361 dwCallBack = MidiInDev[wDevID].midiDesc->dwCallback;
362 uFlags = MidiInDev[wDevID].wFlags;
363 hDev = MidiInDev[wDevID].midiDesc->hMidi;
364 dwInstance = MidiInDev[wDevID].midiDesc->dwInstance;
367 WARN("Unsupported MSW-MIDI message %u\n", wMsg);
368 return MCIERR_INTERNAL;
371 return DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2) ?
375 static int midi_warn = 1;
376 /**************************************************************************
377 * midiOpenSeq [internal]
379 static int midiOpenSeq(void)
381 if (numOpenMidiSeq == 0) {
382 midiSeqFD = open(MIDI_SEQ, O_RDWR, 0);
383 if (midiSeqFD == -1) {
386 MESSAGE("Can't open MIDI device '%s' ! (%s)%s\n",
387 MIDI_SEQ, strerror(errno),
389 ": create it ! (\"man MAKEDEV\" ?)" :
391 ": Load MIDI sequencer kernel driver !" :
393 ": Grant access ! (\"man chmod\")" : ""
399 if (fcntl(midiSeqFD, F_SETFL, O_NONBLOCK) < 0) {
400 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno, strerror(errno));
405 ioctl(midiSeqFD, SNDCTL_SEQ_RESET);
411 /**************************************************************************
412 * midiCloseSeq [internal]
414 static int midiCloseSeq(void)
416 if (--numOpenMidiSeq == 0) {
423 /* FIXME: this is a bad idea, it's even not static... */
426 /* FIXME: this is not reentrant, not static - because of global variable
429 /**************************************************************************
430 * seqbuf_dump [internal]
432 void seqbuf_dump(void)
435 if (write(midiSeqFD, _seqbuf, _seqbufptr) == -1) {
436 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
437 midiSeqFD, errno, strerror(errno));
440 * in any case buffer is lost so that if many errors occur the buffer
447 static void midReceiveChar(WORD wDevID, unsigned char value, DWORD dwTime)
451 TRACE("Adding %02xh to %d[%d]\n", value, wDevID, MidiInDev[wDevID].incLen);
453 if (wDevID >= MAX_MIDIINDRV) {
457 if (MidiInDev[wDevID].state == 0) {
458 TRACE("input not started, thrown away\n");
462 if (MidiInDev[wDevID].state & 2) { /* system exclusive */
463 LPMIDIHDR lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
467 LPBYTE lpData = lpMidiHdr->lpData;
469 lpData[lpMidiHdr->dwBytesRecorded++] = value;
470 if (lpMidiHdr->dwBytesRecorded == lpMidiHdr->dwBufferLength) {
474 if (value == 0xF7) { /* then end */
475 MidiInDev[wDevID].state &= ~2;
478 if (sbfb && lpMidiHdr != NULL) {
479 lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
480 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
481 lpMidiHdr->dwFlags |= MHDR_DONE;
482 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)lpMidiHdr->lpNext;
483 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD)lpMidiHdr, dwTime) != MMSYSERR_NOERROR) {
484 WARN("Couldn't notify client\n");
490 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
491 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
493 if (!IS_CMD(value) && MidiInDev[wDevID].incLen == 0) { /* try to reuse old cmd */
494 if (IS_CMD(MidiInDev[wDevID].incPrev) && !IS_SYS_CMD(MidiInDev[wDevID].incPrev)) {
495 MidiInDev[wDevID].incoming[0] = MidiInDev[wDevID].incPrev;
496 MidiInDev[wDevID].incLen = 1;
497 TRACE("Reusing old command %02xh\n", MidiInDev[wDevID].incPrev);
499 FIXME("error for midi-in, should generate MIM_ERROR notification:"
500 " prev=%02Xh, incLen=%02Xh\n",
501 MidiInDev[wDevID].incPrev, MidiInDev[wDevID].incLen);
505 MidiInDev[wDevID].incoming[(int)(MidiInDev[wDevID].incLen++)] = value;
506 if (MidiInDev[wDevID].incLen == 1 && !IS_SYS_CMD(MidiInDev[wDevID].incoming[0])) {
507 /* store new cmd, just in case */
508 MidiInDev[wDevID].incPrev = MidiInDev[wDevID].incoming[0];
512 #undef IS_SYS_CMD(_x)
514 switch (MidiInDev[wDevID].incoming[0] & 0xF0) {
517 case MIDI_KEY_PRESSURE:
518 case MIDI_CTL_CHANGE:
519 case MIDI_PITCH_BEND:
520 if (MidiInDev[wDevID].incLen == 3) {
521 toSend = (MidiInDev[wDevID].incoming[2] << 16) |
522 (MidiInDev[wDevID].incoming[1] << 8) |
523 (MidiInDev[wDevID].incoming[0] << 0);
526 case MIDI_PGM_CHANGE:
527 case MIDI_CHN_PRESSURE:
528 if (MidiInDev[wDevID].incLen == 2) {
529 toSend = (MidiInDev[wDevID].incoming[1] << 8) |
530 (MidiInDev[wDevID].incoming[0] << 0);
533 case MIDI_SYSTEM_PREFIX:
534 if (MidiInDev[wDevID].incoming[0] == 0xF0) {
535 MidiInDev[wDevID].state |= 2;
536 MidiInDev[wDevID].incLen = 0;
538 if (MidiInDev[wDevID].incLen == 1) {
539 toSend = (MidiInDev[wDevID].incoming[0] << 0);
544 WARN("This shouldn't happen (%02X)\n", MidiInDev[wDevID].incoming[0]);
547 TRACE("Sending event %08lx\n", toSend);
548 MidiInDev[wDevID].incLen = 0;
549 dwTime -= MidiInDev[wDevID].startTime;
550 if (MIDI_NotifyClient(wDevID, MIM_DATA, toSend, dwTime) != MMSYSERR_NOERROR) {
551 WARN("Couldn't notify client\n");
556 static VOID WINAPI midTimeCallback(HWND hwnd, UINT msg, UINT id, DWORD dwTime)
558 unsigned char buffer[256];
561 TRACE("(%04X, %d, %d, %lu)\n", hwnd, msg, id, dwTime);
563 len = read(midiSeqFD, buffer, sizeof(buffer));
566 if ((len % 4) != 0) {
567 WARN("Bad length %d, errno %d (%s)\n", len, errno, strerror(errno));
571 for (idx = 0; idx < len; ) {
572 if (buffer[idx] & 0x80) {
574 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
575 buffer[idx + 0], buffer[idx + 1],
576 buffer[idx + 2], buffer[idx + 3],
577 buffer[idx + 4], buffer[idx + 5],
578 buffer[idx + 6], buffer[idx + 7]);
581 switch (buffer[idx + 0]) {
586 midReceiveChar(buffer[idx + 2], buffer[idx + 1], dwTime);
589 TRACE("Unsupported event %d\n", buffer[idx + 0]);
597 /**************************************************************************
598 * midGetDevCaps [internal]
600 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPSA lpCaps, DWORD dwSize)
602 TRACE("(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
604 if (wDevID >= MIDM_NUMDEVS) return MMSYSERR_BADDEVICEID;
605 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
607 memcpy(lpCaps, midiInDevices[wDevID], min(dwSize, sizeof(*lpCaps)));
609 return MMSYSERR_NOERROR;
612 /**************************************************************************
615 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
617 TRACE("(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
619 if (lpDesc == NULL) {
620 WARN("Invalid Parameter !\n");
621 return MMSYSERR_INVALPARAM;
625 * how to check that content of lpDesc is correct ?
627 if (wDevID >= MAX_MIDIINDRV) {
628 WARN("wDevID too large (%u) !\n", wDevID);
629 return MMSYSERR_BADDEVICEID;
631 if (MidiInDev[wDevID].midiDesc != 0) {
632 WARN("device already open !\n");
633 return MMSYSERR_ALLOCATED;
635 if ((dwFlags & MIDI_IO_STATUS) != 0) {
636 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
637 dwFlags &= ~MIDI_IO_STATUS;
639 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
640 FIXME("Bad dwFlags\n");
641 return MMSYSERR_INVALFLAG;
644 if (midiOpenSeq() < 0) {
645 return MMSYSERR_ERROR;
648 if (numStartedMidiIn++ == 0) {
649 midiInTimerID = SetTimer(0, 0, 250, midTimeCallback);
650 if (!midiInTimerID) {
651 numStartedMidiIn = 0;
652 WARN("Couldn't start timer for midi-in\n");
654 return MMSYSERR_ERROR;
656 TRACE("Starting timer (%u) for midi-in\n", midiInTimerID);
659 MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
661 MidiInDev[wDevID].lpQueueHdr = NULL;
662 MidiInDev[wDevID].dwTotalPlayed = 0;
663 MidiInDev[wDevID].bufsize = 0x3FFF;
664 MidiInDev[wDevID].midiDesc = lpDesc;
665 MidiInDev[wDevID].state = 0;
666 MidiInDev[wDevID].incLen = 0;
667 MidiInDev[wDevID].startTime = 0;
669 if (MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
670 WARN("can't notify client !\n");
671 return MMSYSERR_INVALPARAM;
673 return MMSYSERR_NOERROR;
676 /**************************************************************************
677 * midClose [internal]
679 static DWORD midClose(WORD wDevID)
681 int ret = MMSYSERR_NOERROR;
683 TRACE("(%04X);\n", wDevID);
685 if (wDevID >= MAX_MIDIINDRV) {
686 WARN("wDevID too big (%u) !\n", wDevID);
687 return MMSYSERR_BADDEVICEID;
689 if (MidiInDev[wDevID].midiDesc == 0) {
690 WARN("device not opened !\n");
691 return MMSYSERR_ERROR;
693 if (MidiInDev[wDevID].lpQueueHdr != 0) {
694 return MIDIERR_STILLPLAYING;
697 if (midiSeqFD == -1) {
699 return MMSYSERR_ERROR;
701 if (--numStartedMidiIn == 0) {
702 TRACE("Stopping timer for midi-in\n");
703 if (!KillTimer(0, midiInTimerID)) {
704 WARN("Couldn't stop timer for midi-in\n");
710 MidiInDev[wDevID].bufsize = 0;
711 if (MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
712 WARN("can't notify client !\n");
713 ret = MMSYSERR_INVALPARAM;
715 MidiInDev[wDevID].midiDesc = 0;
719 /**************************************************************************
720 * midAddBuffer [internal]
722 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
724 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
726 if (lpMidiHdr == NULL) return MMSYSERR_INVALPARAM;
727 if (sizeof(MIDIHDR) > dwSize) return MMSYSERR_INVALPARAM;
728 if (lpMidiHdr->dwBufferLength == 0) return MMSYSERR_INVALPARAM;
729 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
730 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
732 if (MidiInDev[wDevID].lpQueueHdr == 0) {
733 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr;
737 for (ptr = MidiInDev[wDevID].lpQueueHdr;
739 ptr = (LPMIDIHDR)ptr->lpNext);
740 ptr->lpNext = (struct midihdr_tag*)lpMidiHdr;
742 return MMSYSERR_NOERROR;
745 /**************************************************************************
746 * midPrepare [internal]
748 static DWORD midPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
750 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
752 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
753 lpMidiHdr->lpData == 0 || lpMidiHdr->dwFlags != 0 ||
754 lpMidiHdr->dwBufferLength >= 0x10000ul)
755 return MMSYSERR_INVALPARAM;
757 lpMidiHdr->lpNext = 0;
758 lpMidiHdr->dwFlags |= MHDR_PREPARED;
759 lpMidiHdr->dwBytesRecorded = 0;
761 return MMSYSERR_NOERROR;
764 /**************************************************************************
765 * midUnprepare [internal]
767 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
769 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
771 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
772 lpMidiHdr->lpData == 0 || lpMidiHdr->dwBufferLength >= 0x10000ul)
773 return MMSYSERR_INVALPARAM;
775 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
776 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
778 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
780 return MMSYSERR_NOERROR;
783 /**************************************************************************
784 * midReset [internal]
786 static DWORD midReset(WORD wDevID)
788 DWORD dwTime = GetTickCount();
790 TRACE("(%04X);\n", wDevID);
792 while (MidiInDev[wDevID].lpQueueHdr) {
793 MidiInDev[wDevID].lpQueueHdr->dwFlags &= ~MHDR_INQUEUE;
794 MidiInDev[wDevID].lpQueueHdr->dwFlags |= MHDR_DONE;
795 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
796 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA,
797 (DWORD)MidiInDev[wDevID].lpQueueHdr, dwTime) != MMSYSERR_NOERROR) {
798 WARN("Couldn't notify client\n");
800 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)MidiInDev[wDevID].lpQueueHdr->lpNext;
803 return MMSYSERR_NOERROR;
807 /**************************************************************************
808 * midStart [internal]
810 static DWORD midStart(WORD wDevID)
812 TRACE("(%04X);\n", wDevID);
814 /* FIXME : should test value of wDevID */
816 MidiInDev[wDevID].state = 1;
817 MidiInDev[wDevID].startTime = GetTickCount();
818 return MMSYSERR_NOERROR;
821 /**************************************************************************
824 static DWORD midStop(WORD wDevID)
826 TRACE("(%04X);\n", wDevID);
828 /* FIXME : should test value of wDevID */
829 MidiInDev[wDevID].state = 0;
830 return MMSYSERR_NOERROR;
833 /*-----------------------------------------------------------------------*/
835 typedef struct sVoice {
836 int note; /* 0 means not used */
838 unsigned cntMark : 30,
841 #define sVS_PLAYING 1
842 #define sVS_SUSTAINED 2
845 typedef struct sChannel {
851 int bank; /* CTL_BANK_SELECT */
852 int volume; /* CTL_MAIN_VOLUME */
853 int balance; /* CTL_BALANCE */
854 int expression; /* CTL_EXPRESSION */
855 int sustain; /* CTL_SUSTAIN */
857 unsigned char nrgPmtMSB; /* Non register Parameters */
858 unsigned char nrgPmtLSB;
859 unsigned char regPmtMSB; /* Non register Parameters */
860 unsigned char regPmtLSB;
863 typedef struct sFMextra {
866 sChannel channel[16]; /* MIDI has only 16 channels */
867 sVoice voice[1]; /* dyn allocated according to sound card */
868 /* do not append fields below voice[1] since the size of this structure
869 * depends on the number of available voices on the FM synth...
873 extern unsigned char midiFMInstrumentPatches[16 * 128];
874 extern unsigned char midiFMDrumsPatches [16 * 128];
876 /**************************************************************************
877 * modFMLoad [internal]
879 static int modFMLoad(int dev)
882 struct sbi_instrument sbi;
887 memset(sbi.operators + 16, 0, 16);
888 for (i = 0; i < 128; i++) {
890 memcpy(sbi.operators, midiFMInstrumentPatches + i * 16, 16);
892 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
893 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
897 for (i = 0; i < 128; i++) {
898 sbi.channel = 128 + i;
899 memcpy(sbi.operators, midiFMDrumsPatches + i * 16, 16);
901 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
902 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
909 /**************************************************************************
910 * modFMReset [internal]
912 static void modFMReset(WORD wDevID)
914 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
915 sVoice* voice = extra->voice;
916 sChannel* channel = extra->channel;
919 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
920 if (voice[i].status != sVS_UNUSED) {
921 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
923 SEQ_KEY_PRESSURE(wDevID, i, 127, 0);
924 SEQ_CONTROL(wDevID, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
926 voice[i].channel = -1;
927 voice[i].cntMark = 0;
928 voice[i].status = sVS_UNUSED;
930 for (i = 0; i < 16; i++) {
931 channel[i].program = 0;
932 channel[i].bender = 8192;
933 channel[i].benderRange = 2;
935 channel[i].volume = 127;
936 channel[i].balance = 64;
937 channel[i].expression = 0;
938 channel[i].sustain = 0;
941 extra->drumSetMask = 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
945 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
947 /**************************************************************************
948 * modGetDevCaps [internal]
950 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPSA lpCaps, DWORD dwSize)
952 TRACE("(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
954 if (wDevID >= MODM_NUMDEVS) return MMSYSERR_BADDEVICEID;
955 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
957 memcpy(lpCaps, midiOutDevices[wDevID], min(dwSize, sizeof(*lpCaps)));
959 return MMSYSERR_NOERROR;
962 /**************************************************************************
965 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
967 TRACE("(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
968 if (lpDesc == NULL) {
969 WARN("Invalid Parameter !\n");
970 return MMSYSERR_INVALPARAM;
972 if (wDevID >= MAX_MIDIOUTDRV) {
973 TRACE("MAX_MIDIOUTDRV reached !\n");
974 return MMSYSERR_BADDEVICEID;
976 if (MidiOutDev[wDevID].midiDesc != 0) {
977 WARN("device already open !\n");
978 return MMSYSERR_ALLOCATED;
980 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
981 WARN("bad dwFlags\n");
982 return MMSYSERR_INVALFLAG;
984 if (midiOutDevices[wDevID] == NULL) {
985 TRACE("un-allocated wDevID\n");
986 return MMSYSERR_BADDEVICEID;
989 MidiOutDev[wDevID].lpExtra = 0;
991 switch (midiOutDevices[wDevID]->wTechnology) {
994 void* extra = HeapAlloc(GetProcessHeap(), 0,
995 sizeof(struct sFMextra) +
996 sizeof(struct sVoice) * (midiOutDevices[wDevID]->wVoices - 1));
999 WARN("can't alloc extra data !\n");
1000 return MMSYSERR_NOMEM;
1002 MidiOutDev[wDevID].lpExtra = extra;
1003 if (midiOpenSeq() < 0) {
1004 MidiOutDev[wDevID].lpExtra = 0;
1005 HeapFree(GetProcessHeap(), 0, extra);
1006 return MMSYSERR_ERROR;
1008 if (modFMLoad(wDevID) < 0) {
1010 MidiOutDev[wDevID].lpExtra = 0;
1011 HeapFree(GetProcessHeap(), 0, extra);
1012 return MMSYSERR_ERROR;
1018 if (midiOpenSeq() < 0) {
1019 return MMSYSERR_ALLOCATED;
1023 WARN("Technology not supported (yet) %d !\n",
1024 midiOutDevices[wDevID]->wTechnology);
1025 return MMSYSERR_NOTENABLED;
1028 MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1030 MidiOutDev[wDevID].lpQueueHdr = NULL;
1031 MidiOutDev[wDevID].dwTotalPlayed = 0;
1032 MidiOutDev[wDevID].bufsize = 0x3FFF;
1033 MidiOutDev[wDevID].midiDesc = lpDesc;
1035 if (MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
1036 WARN("can't notify client !\n");
1037 return MMSYSERR_INVALPARAM;
1039 TRACE("Successful !\n");
1040 return MMSYSERR_NOERROR;
1044 /**************************************************************************
1045 * modClose [internal]
1047 static DWORD modClose(WORD wDevID)
1049 int ret = MMSYSERR_NOERROR;
1051 TRACE("(%04X);\n", wDevID);
1053 if (MidiOutDev[wDevID].midiDesc == 0) {
1054 WARN("device not opened !\n");
1055 return MMSYSERR_ERROR;
1057 /* FIXME: should test that no pending buffer is still in the queue for
1060 if (midiSeqFD == -1) {
1061 WARN("can't close !\n");
1062 return MMSYSERR_ERROR;
1065 switch (midiOutDevices[wDevID]->wTechnology) {
1071 WARN("Technology not supported (yet) %d !\n",
1072 midiOutDevices[wDevID]->wTechnology);
1073 return MMSYSERR_NOTENABLED;
1076 if (MidiOutDev[wDevID].lpExtra != 0) {
1077 HeapFree(GetProcessHeap(), 0, MidiOutDev[wDevID].lpExtra);
1078 MidiOutDev[wDevID].lpExtra = 0;
1081 MidiOutDev[wDevID].bufsize = 0;
1082 if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
1083 WARN("can't notify client !\n");
1084 ret = MMSYSERR_INVALPARAM;
1086 MidiOutDev[wDevID].midiDesc = 0;
1090 /**************************************************************************
1091 * modData [internal]
1093 static DWORD modData(WORD wDevID, DWORD dwParam)
1095 WORD evt = LOBYTE(LOWORD(dwParam));
1096 WORD d1 = HIBYTE(LOWORD(dwParam));
1097 WORD d2 = LOBYTE(HIWORD(dwParam));
1099 TRACE("(%04X, %08lX);\n", wDevID, dwParam);
1101 if (midiSeqFD == -1) {
1102 WARN("can't play !\n");
1103 return MIDIERR_NODEVICE;
1105 switch (midiOutDevices[wDevID]->wTechnology) {
1108 * - chorus depth controller is not used
1111 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
1112 sVoice* voice = extra->voice;
1113 sChannel* channel = extra->channel;
1114 int chn = (evt & 0x0F);
1117 switch (evt & 0xF0) {
1119 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1120 /* don't stop sustained notes */
1121 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1122 voice[i].status = sVS_UNUSED;
1123 SEQ_STOP_NOTE(wDevID, i, d1, d2);
1128 if (d2 == 0) { /* note off if velocity == 0 */
1129 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1130 /* don't stop sustained notes */
1131 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1132 voice[i].status = sVS_UNUSED;
1133 SEQ_STOP_NOTE(wDevID, i, d1, 64);
1138 /* finding out in this order :
1140 * - if replaying the same note on the same channel
1141 * - the older voice (LRU)
1143 for (i = nv = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1144 if (voice[i].status == sVS_UNUSED ||
1145 (voice[i].note == d1 && voice[i].channel == chn)) {
1149 if (voice[i].cntMark < voice[0].cntMark) {
1154 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1155 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1156 nv, channel[chn].program,
1157 channel[chn].balance,
1158 channel[chn].volume,
1159 channel[chn].bender, d1, d2);
1161 SEQ_SET_PATCH(wDevID, nv, IS_DRUM_CHANNEL(extra, chn) ?
1162 (128 + d1) : channel[chn].program);
1163 SEQ_BENDER_RANGE(wDevID, nv, channel[chn].benderRange * 100);
1164 SEQ_BENDER(wDevID, nv, channel[chn].bender);
1165 SEQ_CONTROL(wDevID, nv, CTL_PAN, channel[chn].balance);
1166 SEQ_CONTROL(wDevID, nv, CTL_EXPRESSION, channel[chn].expression);
1168 /* FIXME: does not really seem to work on my SB card and
1169 * screws everything up... so lay it down
1171 SEQ_CONTROL(wDevID, nv, CTL_MAIN_VOLUME, channel[chn].volume);
1173 SEQ_START_NOTE(wDevID, nv, d1, d2);
1174 voice[nv].status = channel[chn].sustain ? sVS_SUSTAINED : sVS_PLAYING;
1175 voice[nv].note = d1;
1176 voice[nv].channel = chn;
1177 voice[nv].cntMark = extra->counter++;
1179 case MIDI_KEY_PRESSURE:
1180 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1181 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn && voice[i].note == d1) {
1182 SEQ_KEY_PRESSURE(wDevID, i, d1, d2);
1186 case MIDI_CTL_CHANGE:
1188 case CTL_BANK_SELECT: channel[chn].bank = d2; break;
1189 case CTL_MAIN_VOLUME: channel[chn].volume = d2; break;
1190 case CTL_PAN: channel[chn].balance = d2; break;
1191 case CTL_EXPRESSION: channel[chn].expression = d2; break;
1192 case CTL_SUSTAIN: channel[chn].sustain = d2;
1194 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1195 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1196 voice[i].status = sVS_SUSTAINED;
1200 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1201 if (voice[i].status == sVS_SUSTAINED && voice[i].channel == chn) {
1202 voice[i].status = sVS_UNUSED;
1203 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1208 case CTL_NONREG_PARM_NUM_LSB: channel[chn].nrgPmtLSB = d2; break;
1209 case CTL_NONREG_PARM_NUM_MSB: channel[chn].nrgPmtMSB = d2; break;
1210 case CTL_REGIST_PARM_NUM_LSB: channel[chn].regPmtLSB = d2; break;
1211 case CTL_REGIST_PARM_NUM_MSB: channel[chn].regPmtMSB = d2; break;
1212 case CTL_DATA_ENTRY:
1213 switch ((channel[chn].regPmtMSB << 8) | channel[chn].regPmtLSB) {
1215 if (channel[chn].benderRange != d2) {
1216 channel[chn].benderRange = d2;
1217 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1218 if (voice[i].channel == chn) {
1219 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1226 channel[chn].benderRange = 2;
1227 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1228 if (voice[i].channel == chn) {
1229 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1234 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1235 channel[chn].regPmtMSB, channel[chn].regPmtLSB,
1236 channel[chn].nrgPmtMSB, channel[chn].nrgPmtLSB,
1242 case 0x78: /* all sounds off */
1243 /* FIXME: I don't know if I have to take care of the channel
1244 * for this control ?
1246 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1247 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1248 voice[i].status = sVS_UNUSED;
1249 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1253 case 0x7B: /* all notes off */
1254 /* FIXME: I don't know if I have to take care of the channel
1255 * for this control ?
1257 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1258 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1259 voice[i].status = sVS_UNUSED;
1260 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1265 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1270 case MIDI_PGM_CHANGE:
1271 channel[chn].program = d1;
1273 case MIDI_CHN_PRESSURE:
1274 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1275 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1276 SEQ_KEY_PRESSURE(wDevID, i, voice[i].note, d1);
1280 case MIDI_PITCH_BEND:
1281 channel[chn].bender = (d2 << 7) + d1;
1282 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1283 if (voice[i].channel == chn) {
1284 SEQ_BENDER(wDevID, i, channel[chn].bender);
1288 case MIDI_SYSTEM_PREFIX:
1289 switch (evt & 0x0F) {
1290 case 0x0F: /* Reset */
1294 WARN("Unsupported (yet) system event %02x\n", evt & 0x0F);
1298 WARN("Internal error, shouldn't happen (event=%08x)\n", evt & 0xF0);
1299 return MMSYSERR_NOTENABLED;
1305 int dev = wDevID - MODM_NUMFMSYNTHDEVS;
1307 WARN("Internal error on devID (%u) !\n", wDevID);
1308 return MIDIERR_NODEVICE;
1311 switch (evt & 0xF0) {
1314 case MIDI_KEY_PRESSURE:
1315 case MIDI_CTL_CHANGE:
1316 case MIDI_PITCH_BEND:
1317 SEQ_MIDIOUT(dev, evt);
1318 SEQ_MIDIOUT(dev, d1);
1319 SEQ_MIDIOUT(dev, d2);
1321 case MIDI_PGM_CHANGE:
1322 case MIDI_CHN_PRESSURE:
1323 SEQ_MIDIOUT(dev, evt);
1324 SEQ_MIDIOUT(dev, d1);
1326 case MIDI_SYSTEM_PREFIX:
1327 switch (evt & 0x0F) {
1328 case 0x00: /* System Exclusive, don't do it on modData,
1329 * should require modLongData*/
1330 case 0x01: /* Undefined */
1331 case 0x04: /* Undefined. */
1332 case 0x05: /* Undefined. */
1333 case 0x07: /* End of Exclusive. */
1334 case 0x09: /* Undefined. */
1335 case 0x0D: /* Undefined. */
1337 case 0x06: /* Tune Request */
1338 case 0x08: /* Timing Clock. */
1339 case 0x0A: /* Start. */
1340 case 0x0B: /* Continue */
1341 case 0x0C: /* Stop */
1342 case 0x0E: /* Active Sensing. */
1343 SEQ_MIDIOUT(dev, evt);
1345 case 0x0F: /* Reset */
1346 /* SEQ_MIDIOUT(dev, evt);
1347 this other way may be better */
1348 SEQ_MIDIOUT(dev, MIDI_SYSTEM_PREFIX);
1349 SEQ_MIDIOUT(dev, 0x7e);
1350 SEQ_MIDIOUT(dev, 0x7f);
1351 SEQ_MIDIOUT(dev, 0x09);
1352 SEQ_MIDIOUT(dev, 0x01);
1353 SEQ_MIDIOUT(dev, 0xf7);
1355 case 0x03: /* Song Select. */
1356 SEQ_MIDIOUT(dev, evt);
1357 SEQ_MIDIOUT(dev, d1);
1358 case 0x02: /* Song Position Pointer. */
1359 SEQ_MIDIOUT(dev, evt);
1360 SEQ_MIDIOUT(dev, d1);
1361 SEQ_MIDIOUT(dev, d2);
1368 WARN("Technology not supported (yet) %d !\n",
1369 midiOutDevices[wDevID]->wTechnology);
1370 return MMSYSERR_NOTENABLED;
1375 return MMSYSERR_NOERROR;
1378 /**************************************************************************
1379 * modLongData [internal]
1381 static DWORD modLongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1386 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1388 if (midiSeqFD == -1) {
1389 WARN("can't play !\n");
1390 return MIDIERR_NODEVICE;
1393 lpData = lpMidiHdr->lpData;
1396 return MIDIERR_UNPREPARED;
1397 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1398 return MIDIERR_UNPREPARED;
1399 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1400 return MIDIERR_STILLPLAYING;
1401 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1402 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1404 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1405 * data, or can it also contain raw MIDI data, to be split up and sent to
1407 * If the latest is true, then the following WARNing will fire up
1409 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
1410 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1413 TRACE("dwBufferLength=%lu !\n", lpMidiHdr->dwBufferLength);
1414 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1415 lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
1416 lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
1418 switch (midiOutDevices[wDevID]->wTechnology) {
1420 /* FIXME: I don't think there is much to do here */
1423 if (lpData[0] != 0xF0) {
1424 /* Send end of System Exclusive */
1425 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF0);
1426 WARN("Adding missing 0xF0 marker at the begining of "
1427 "system exclusive byte stream\n");
1429 for (count = 0; count < lpMidiHdr->dwBytesRecorded; count++) {
1430 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, lpData[count]);
1432 if (lpData[count - 1] != 0xF7) {
1433 /* Send end of System Exclusive */
1434 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF7);
1435 WARN("Adding missing 0xF7 marker at the end of "
1436 "system exclusive byte stream\n");
1441 WARN("Technology not supported (yet) %d !\n",
1442 midiOutDevices[wDevID]->wTechnology);
1443 return MMSYSERR_NOTENABLED;
1446 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1447 lpMidiHdr->dwFlags |= MHDR_DONE;
1448 if (MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD)lpMidiHdr, 0L) != MMSYSERR_NOERROR) {
1449 WARN("can't notify client !\n");
1450 return MMSYSERR_INVALPARAM;
1452 return MMSYSERR_NOERROR;
1455 /**************************************************************************
1456 * modPrepare [internal]
1458 static DWORD modPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1460 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1462 if (midiSeqFD == -1) {
1463 WARN("can't prepare !\n");
1464 return MMSYSERR_NOTENABLED;
1467 /* MS doc says taht dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
1468 * asks to prepare MIDIHDR which dwFlags != 0.
1469 * So at least check for the inqueue flag
1471 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
1472 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
1473 lpMidiHdr->dwBufferLength >= 0x10000ul) {
1474 WARN("%p %p %08lx %d/%ld\n", lpMidiHdr, lpMidiHdr->lpData,
1475 lpMidiHdr->dwFlags, sizeof(MIDIHDR), dwSize);
1476 return MMSYSERR_INVALPARAM;
1479 lpMidiHdr->lpNext = 0;
1480 lpMidiHdr->dwFlags |= MHDR_PREPARED;
1481 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1482 return MMSYSERR_NOERROR;
1485 /**************************************************************************
1486 * modUnprepare [internal]
1488 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1490 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1492 if (midiSeqFD == -1) {
1493 WARN("can't unprepare !\n");
1494 return MMSYSERR_NOTENABLED;
1497 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0)
1498 return MMSYSERR_INVALPARAM;
1499 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1500 return MIDIERR_STILLPLAYING;
1501 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
1502 return MMSYSERR_NOERROR;
1505 /**************************************************************************
1506 * modReset [internal]
1508 static DWORD modReset(WORD wDevID)
1512 TRACE("(%04X);\n", wDevID);
1514 /* stop all notes */
1515 /* FIXME: check if 0x78B0 is channel dependant or not. I coded it so that
1516 * it's channel dependent...
1518 for (chn = 0; chn < 16; chn++) {
1519 /* turn off every note */
1520 modData(wDevID, 0x7800 | MIDI_CTL_CHANGE | chn);
1521 /* remove sustain on all channels */
1522 modData(wDevID, (CTL_SUSTAIN << 8) | MIDI_CTL_CHANGE | chn);
1524 /* FIXME: the LongData buffers must also be returned to the app */
1525 return MMSYSERR_NOERROR;
1528 #endif /* HAVE_OSS_MIDI */
1530 /*======================================================================*
1531 * MIDI entry points *
1532 *======================================================================*/
1534 /**************************************************************************
1535 * OSS_midMessage [sample driver]
1537 DWORD WINAPI OSS_midMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1538 DWORD dwParam1, DWORD dwParam2)
1540 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1541 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1543 #ifdef HAVE_OSS_MIDI
1547 /* FIXME: Pretend this is supported */
1550 return midOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1552 return midClose(wDevID);
1553 case MIDM_ADDBUFFER:
1554 return midAddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1556 return midPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1557 case MIDM_UNPREPARE:
1558 return midUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1559 case MIDM_GETDEVCAPS:
1560 return midGetDevCaps(wDevID, (LPMIDIINCAPSA)dwParam1,dwParam2);
1561 case MIDM_GETNUMDEVS:
1562 return MIDM_NUMDEVS;
1564 return midReset(wDevID);
1566 return midStart(wDevID);
1568 return midStop(wDevID);
1571 TRACE("Unsupported message\n");
1573 return MMSYSERR_NOTSUPPORTED;
1576 /**************************************************************************
1577 * OSS_modMessage [sample driver]
1579 DWORD WINAPI OSS_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1580 DWORD dwParam1, DWORD dwParam2)
1582 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1583 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1586 #ifdef HAVE_OSS_MIDI
1591 /* FIXME: Pretend this is supported */
1594 return modOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1596 return modClose(wDevID);
1598 return modData(wDevID, dwParam1);
1600 return modLongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1602 return modPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1603 case MODM_UNPREPARE:
1604 return modUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1605 case MODM_GETDEVCAPS:
1606 return modGetDevCaps(wDevID, (LPMIDIOUTCAPSA)dwParam1, dwParam2);
1607 case MODM_GETNUMDEVS:
1608 return MODM_NUMDEVS;
1609 case MODM_GETVOLUME:
1611 case MODM_SETVOLUME:
1614 return modReset(wDevID);
1617 TRACE("Unsupported message\n");
1619 return MMSYSERR_NOTSUPPORTED;
1622 /*-----------------------------------------------------------------------*/