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>
27 #include "debugtools.h"
31 DEFAULT_DEBUG_CHANNEL(midi)
35 #define MIDI_SEQ "/dev/sequencer"
40 LPMIDIOPENDESC midiDesc;
44 unsigned char incoming[3];
45 unsigned char incPrev;
53 LPMIDIOPENDESC 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("Synthetizer 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) {
386 MESSAGE("Can't open MIDI device '%s', errno %d (%s) !\n",
387 MIDI_SEQ, errno, strerror(errno));
391 if (fcntl(midiSeqFD, F_SETFL, O_NONBLOCK) < 0) {
392 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno, strerror(errno));
397 ioctl(midiSeqFD, SNDCTL_SEQ_RESET);
403 /**************************************************************************
404 * midiCloseSeq [internal]
406 static int midiCloseSeq(void)
408 if (--numOpenMidiSeq == 0) {
415 /* FIXME: this is a bad idea, it's even not static... */
418 /* FIXME: this is not reentrant, not static - because of global variable
421 /**************************************************************************
422 * seqbuf_dump [internal]
424 void seqbuf_dump(void)
427 if (write(midiSeqFD, _seqbuf, _seqbufptr) == -1) {
428 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
429 midiSeqFD, errno, strerror(errno));
432 * in any case buffer is lost so that if many errors occur the buffer
439 static void midReceiveChar(WORD wDevID, unsigned char value, DWORD dwTime)
443 TRACE("Adding %02xh to %d[%d]\n", value, wDevID, MidiInDev[wDevID].incLen);
445 if (wDevID >= MAX_MIDIINDRV) {
449 if (MidiInDev[wDevID].state == 0) {
450 TRACE("input not started, thrown away\n");
454 if (MidiInDev[wDevID].state & 2) { /* system exclusive */
455 LPMIDIHDR lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
459 LPBYTE lpData = lpMidiHdr->lpData;
461 lpData[lpMidiHdr->dwBytesRecorded++] = value;
462 if (lpMidiHdr->dwBytesRecorded == lpMidiHdr->dwBufferLength) {
466 if (value == 0xF7) { /* then end */
467 MidiInDev[wDevID].state &= ~2;
470 if (sbfb && lpMidiHdr != NULL) {
471 lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
472 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
473 lpMidiHdr->dwFlags |= MHDR_DONE;
474 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)lpMidiHdr->lpNext;
475 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD)lpMidiHdr, dwTime) != MMSYSERR_NOERROR) {
476 WARN("Couldn't notify client\n");
482 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
483 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
485 if (!IS_CMD(value) && MidiInDev[wDevID].incLen == 0) { /* try to reuse old cmd */
486 if (IS_CMD(MidiInDev[wDevID].incPrev) && !IS_SYS_CMD(MidiInDev[wDevID].incPrev)) {
487 MidiInDev[wDevID].incoming[0] = MidiInDev[wDevID].incPrev;
488 MidiInDev[wDevID].incLen = 1;
489 TRACE("Reusing old command %02xh\n", MidiInDev[wDevID].incPrev);
491 FIXME("error for midi-in, should generate MIM_ERROR notification:"
492 " prev=%02Xh, incLen=%02Xh\n",
493 MidiInDev[wDevID].incPrev, MidiInDev[wDevID].incLen);
497 MidiInDev[wDevID].incoming[(int)(MidiInDev[wDevID].incLen++)] = value;
498 if (MidiInDev[wDevID].incLen == 1 && !IS_SYS_CMD(MidiInDev[wDevID].incoming[0])) {
499 /* store new cmd, just in case */
500 MidiInDev[wDevID].incPrev = MidiInDev[wDevID].incoming[0];
504 #undef IS_SYS_CMD(_x)
506 switch (MidiInDev[wDevID].incoming[0] & 0xF0) {
509 case MIDI_KEY_PRESSURE:
510 case MIDI_CTL_CHANGE:
511 case MIDI_PITCH_BEND:
512 if (MidiInDev[wDevID].incLen == 3) {
513 toSend = (MidiInDev[wDevID].incoming[2] << 16) |
514 (MidiInDev[wDevID].incoming[1] << 8) |
515 (MidiInDev[wDevID].incoming[0] << 0);
518 case MIDI_PGM_CHANGE:
519 case MIDI_CHN_PRESSURE:
520 if (MidiInDev[wDevID].incLen == 2) {
521 toSend = (MidiInDev[wDevID].incoming[1] << 8) |
522 (MidiInDev[wDevID].incoming[0] << 0);
525 case MIDI_SYSTEM_PREFIX:
526 if (MidiInDev[wDevID].incoming[0] == 0xF0) {
527 MidiInDev[wDevID].state |= 2;
528 MidiInDev[wDevID].incLen = 0;
530 if (MidiInDev[wDevID].incLen == 1) {
531 toSend = (MidiInDev[wDevID].incoming[0] << 0);
536 WARN("This shouldn't happen (%02X)\n", MidiInDev[wDevID].incoming[0]);
539 TRACE("Sending event %08lx\n", toSend);
540 MidiInDev[wDevID].incLen = 0;
541 dwTime -= MidiInDev[wDevID].startTime;
542 if (MIDI_NotifyClient(wDevID, MIM_DATA, toSend, dwTime) != MMSYSERR_NOERROR) {
543 WARN("Couldn't notify client\n");
548 static VOID WINAPI midTimeCallback(HWND hwnd, UINT msg, UINT id, DWORD dwTime)
550 unsigned char buffer[256];
553 TRACE("(%04X, %d, %d, %lu)\n", hwnd, msg, id, dwTime);
555 len = read(midiSeqFD, buffer, sizeof(buffer));
558 if ((len % 4) != 0) {
559 WARN("Bad length %d, errno %d (%s)\n", len, errno, strerror(errno));
563 for (idx = 0; idx < len; ) {
564 if (buffer[idx] & 0x80) {
566 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
567 buffer[idx + 0], buffer[idx + 1],
568 buffer[idx + 2], buffer[idx + 3],
569 buffer[idx + 4], buffer[idx + 5],
570 buffer[idx + 6], buffer[idx + 7]);
573 switch (buffer[idx + 0]) {
578 midReceiveChar(buffer[idx + 2], buffer[idx + 1], dwTime);
581 TRACE("Unsupported event %d\n", buffer[idx + 0]);
589 /**************************************************************************
590 * midGetDevCaps [internal]
592 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPSA lpCaps, DWORD dwSize)
594 TRACE("(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
596 if (wDevID >= MIDM_NUMDEVS) return MMSYSERR_BADDEVICEID;
597 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
599 memcpy(lpCaps, midiInDevices[wDevID], min(dwSize, sizeof(*lpCaps)));
601 return MMSYSERR_NOERROR;
604 /**************************************************************************
607 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
609 TRACE("(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
611 if (lpDesc == NULL) {
612 WARN("Invalid Parameter !\n");
613 return MMSYSERR_INVALPARAM;
617 * how to check that content of lpDesc is correct ?
619 if (wDevID >= MAX_MIDIINDRV) {
620 WARN("wDevID too large (%u) !\n", wDevID);
621 return MMSYSERR_BADDEVICEID;
623 if (MidiInDev[wDevID].midiDesc != 0) {
624 WARN("device already open !\n");
625 return MMSYSERR_ALLOCATED;
627 if ((dwFlags & MIDI_IO_STATUS) != 0) {
628 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
629 dwFlags &= ~MIDI_IO_STATUS;
631 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
632 FIXME("Bad dwFlags\n");
633 return MMSYSERR_INVALFLAG;
636 if (midiOpenSeq() < 0) {
637 return MMSYSERR_ERROR;
640 if (numStartedMidiIn++ == 0) {
641 midiInTimerID = SetTimer(0, 0, 250, midTimeCallback);
642 if (!midiInTimerID) {
643 numStartedMidiIn = 0;
644 WARN("Couldn't start timer for midi-in\n");
646 return MMSYSERR_ERROR;
648 TRACE("Starting timer (%u) for midi-in\n", midiInTimerID);
651 MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
653 MidiInDev[wDevID].lpQueueHdr = NULL;
654 MidiInDev[wDevID].dwTotalPlayed = 0;
655 MidiInDev[wDevID].bufsize = 0x3FFF;
656 MidiInDev[wDevID].midiDesc = lpDesc;
657 MidiInDev[wDevID].state = 0;
658 MidiInDev[wDevID].incLen = 0;
659 MidiInDev[wDevID].startTime = 0;
661 if (MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
662 WARN("can't notify client !\n");
663 return MMSYSERR_INVALPARAM;
665 return MMSYSERR_NOERROR;
668 /**************************************************************************
669 * midClose [internal]
671 static DWORD midClose(WORD wDevID)
673 int ret = MMSYSERR_NOERROR;
675 TRACE("(%04X);\n", wDevID);
677 if (wDevID >= MAX_MIDIINDRV) {
678 WARN("wDevID too big (%u) !\n", wDevID);
679 return MMSYSERR_BADDEVICEID;
681 if (MidiInDev[wDevID].midiDesc == 0) {
682 WARN("device not opened !\n");
683 return MMSYSERR_ERROR;
685 if (MidiInDev[wDevID].lpQueueHdr != 0) {
686 return MIDIERR_STILLPLAYING;
689 if (midiSeqFD == -1) {
691 return MMSYSERR_ERROR;
693 if (--numStartedMidiIn == 0) {
694 TRACE("Stopping timer for midi-in\n");
695 if (!KillTimer(0, midiInTimerID)) {
696 WARN("Couldn't stop timer for midi-in\n");
702 MidiInDev[wDevID].bufsize = 0;
703 if (MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
704 WARN("can't notify client !\n");
705 ret = MMSYSERR_INVALPARAM;
707 MidiInDev[wDevID].midiDesc = 0;
711 /**************************************************************************
712 * midAddBuffer [internal]
714 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
716 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
718 if (lpMidiHdr == NULL) return MMSYSERR_INVALPARAM;
719 if (sizeof(MIDIHDR) > dwSize) return MMSYSERR_INVALPARAM;
720 if (lpMidiHdr->dwBufferLength == 0) return MMSYSERR_INVALPARAM;
721 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
722 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
724 if (MidiInDev[wDevID].lpQueueHdr == 0) {
725 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr;
729 for (ptr = MidiInDev[wDevID].lpQueueHdr;
731 ptr = (LPMIDIHDR)ptr->lpNext);
732 ptr->lpNext = (struct midihdr_tag*)lpMidiHdr;
734 return MMSYSERR_NOERROR;
737 /**************************************************************************
738 * midPrepare [internal]
740 static DWORD midPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
742 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
744 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
745 lpMidiHdr->lpData == 0 || lpMidiHdr->dwFlags != 0 ||
746 lpMidiHdr->dwBufferLength >= 0x10000ul)
747 return MMSYSERR_INVALPARAM;
749 lpMidiHdr->lpNext = 0;
750 lpMidiHdr->dwFlags |= MHDR_PREPARED;
751 lpMidiHdr->dwBytesRecorded = 0;
753 return MMSYSERR_NOERROR;
756 /**************************************************************************
757 * midUnprepare [internal]
759 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
761 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
763 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
764 lpMidiHdr->lpData == 0 || lpMidiHdr->dwBufferLength >= 0x10000ul)
765 return MMSYSERR_INVALPARAM;
767 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
768 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
770 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
772 return MMSYSERR_NOERROR;
775 /**************************************************************************
776 * midReset [internal]
778 static DWORD midReset(WORD wDevID)
780 DWORD dwTime = GetTickCount();
782 TRACE("(%04X);\n", wDevID);
784 while (MidiInDev[wDevID].lpQueueHdr) {
785 MidiInDev[wDevID].lpQueueHdr->dwFlags &= ~MHDR_INQUEUE;
786 MidiInDev[wDevID].lpQueueHdr->dwFlags |= MHDR_DONE;
787 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
788 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA,
789 (DWORD)MidiInDev[wDevID].lpQueueHdr, dwTime) != MMSYSERR_NOERROR) {
790 WARN("Couldn't notify client\n");
792 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)MidiInDev[wDevID].lpQueueHdr->lpNext;
795 return MMSYSERR_NOERROR;
799 /**************************************************************************
800 * midStart [internal]
802 static DWORD midStart(WORD wDevID)
804 TRACE("(%04X);\n", wDevID);
806 /* FIXME : should test value of wDevID */
808 MidiInDev[wDevID].state = 1;
809 MidiInDev[wDevID].startTime = GetTickCount();
810 return MMSYSERR_NOERROR;
813 /**************************************************************************
816 static DWORD midStop(WORD wDevID)
818 TRACE("(%04X);\n", wDevID);
820 /* FIXME : should test value of wDevID */
821 MidiInDev[wDevID].state = 0;
822 return MMSYSERR_NOERROR;
825 /*-----------------------------------------------------------------------*/
827 typedef struct sVoice {
828 int note; /* 0 means not used */
830 unsigned cntMark : 30,
833 #define sVS_PLAYING 1
834 #define sVS_SUSTAINED 2
837 typedef struct sChannel {
843 int bank; /* CTL_BANK_SELECT */
844 int volume; /* CTL_MAIN_VOLUME */
845 int balance; /* CTL_BALANCE */
846 int expression; /* CTL_EXPRESSION */
847 int sustain; /* CTL_SUSTAIN */
849 unsigned char nrgPmtMSB; /* Non register Parameters */
850 unsigned char nrgPmtLSB;
851 unsigned char regPmtMSB; /* Non register Parameters */
852 unsigned char regPmtLSB;
855 typedef struct sFMextra {
858 sChannel channel[16]; /* MIDI has only 16 channels */
859 sVoice voice[1]; /* dyn allocated according to sound card */
860 /* do not append fields below voice[1] since the size of this structure
861 * depends on the number of available voices on the FM synth...
865 extern unsigned char midiFMInstrumentPatches[16 * 128];
866 extern unsigned char midiFMDrumsPatches [16 * 128];
868 /**************************************************************************
869 * modFMLoad [internal]
871 static int modFMLoad(int dev)
874 struct sbi_instrument sbi;
879 memset(sbi.operators + 16, 0, 16);
880 for (i = 0; i < 128; i++) {
882 memcpy(sbi.operators, midiFMInstrumentPatches + i * 16, 16);
884 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
885 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
889 for (i = 0; i < 128; i++) {
890 sbi.channel = 128 + i;
891 memcpy(sbi.operators, midiFMDrumsPatches + i * 16, 16);
893 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
894 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
901 /**************************************************************************
902 * modFMReset [internal]
904 static void modFMReset(WORD wDevID)
906 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
907 sVoice* voice = extra->voice;
908 sChannel* channel = extra->channel;
911 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
912 if (voice[i].status != sVS_UNUSED) {
913 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
915 SEQ_KEY_PRESSURE(wDevID, i, 127, 0);
916 SEQ_CONTROL(wDevID, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
918 voice[i].channel = -1;
919 voice[i].cntMark = 0;
920 voice[i].status = sVS_UNUSED;
922 for (i = 0; i < 16; i++) {
923 channel[i].program = 0;
924 channel[i].bender = 8192;
925 channel[i].benderRange = 2;
927 channel[i].volume = 127;
928 channel[i].balance = 64;
929 channel[i].expression = 0;
930 channel[i].sustain = 0;
933 extra->drumSetMask = 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
937 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
939 /**************************************************************************
940 * modGetDevCaps [internal]
942 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPSA lpCaps, DWORD dwSize)
944 TRACE("(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
946 if (wDevID >= MODM_NUMDEVS) return MMSYSERR_BADDEVICEID;
947 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
949 memcpy(lpCaps, midiOutDevices[wDevID], min(dwSize, sizeof(*lpCaps)));
951 return MMSYSERR_NOERROR;
954 /**************************************************************************
957 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
959 TRACE("(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
960 if (lpDesc == NULL) {
961 WARN("Invalid Parameter !\n");
962 return MMSYSERR_INVALPARAM;
964 if (wDevID >= MAX_MIDIOUTDRV) {
965 TRACE("MAX_MIDIOUTDRV reached !\n");
966 return MMSYSERR_BADDEVICEID;
968 if (MidiOutDev[wDevID].midiDesc != 0) {
969 WARN("device already open !\n");
970 return MMSYSERR_ALLOCATED;
972 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
973 WARN("bad dwFlags\n");
974 return MMSYSERR_INVALFLAG;
976 if (midiOutDevices[wDevID] == NULL) {
977 TRACE("un-allocated wDevID\n");
978 return MMSYSERR_BADDEVICEID;
981 MidiOutDev[wDevID].lpExtra = 0;
983 switch (midiOutDevices[wDevID]->wTechnology) {
986 void* extra = HeapAlloc(GetProcessHeap(), 0,
987 sizeof(struct sFMextra) +
988 sizeof(struct sVoice) * (midiOutDevices[wDevID]->wVoices - 1));
991 WARN("can't alloc extra data !\n");
992 return MMSYSERR_NOMEM;
994 MidiOutDev[wDevID].lpExtra = extra;
995 if (midiOpenSeq() < 0) {
996 MidiOutDev[wDevID].lpExtra = 0;
997 HeapFree(GetProcessHeap(), 0, extra);
998 return MMSYSERR_ERROR;
1000 if (modFMLoad(wDevID) < 0) {
1002 MidiOutDev[wDevID].lpExtra = 0;
1003 HeapFree(GetProcessHeap(), 0, extra);
1004 return MMSYSERR_ERROR;
1010 if (midiOpenSeq() < 0) {
1011 return MMSYSERR_ALLOCATED;
1015 WARN("Technology not supported (yet) %d !\n",
1016 midiOutDevices[wDevID]->wTechnology);
1017 return MMSYSERR_NOTENABLED;
1020 MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1022 MidiOutDev[wDevID].lpQueueHdr = NULL;
1023 MidiOutDev[wDevID].dwTotalPlayed = 0;
1024 MidiOutDev[wDevID].bufsize = 0x3FFF;
1025 MidiOutDev[wDevID].midiDesc = lpDesc;
1027 if (MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
1028 WARN("can't notify client !\n");
1029 return MMSYSERR_INVALPARAM;
1031 TRACE("Successful !\n");
1032 return MMSYSERR_NOERROR;
1036 /**************************************************************************
1037 * modClose [internal]
1039 static DWORD modClose(WORD wDevID)
1041 int ret = MMSYSERR_NOERROR;
1043 TRACE("(%04X);\n", wDevID);
1045 if (MidiOutDev[wDevID].midiDesc == 0) {
1046 WARN("device not opened !\n");
1047 return MMSYSERR_ERROR;
1049 /* FIXME: should test that no pending buffer is still in the queue for
1052 if (midiSeqFD == -1) {
1053 WARN("can't close !\n");
1054 return MMSYSERR_ERROR;
1057 switch (midiOutDevices[wDevID]->wTechnology) {
1063 WARN("Technology not supported (yet) %d !\n",
1064 midiOutDevices[wDevID]->wTechnology);
1065 return MMSYSERR_NOTENABLED;
1068 if (MidiOutDev[wDevID].lpExtra != 0) {
1069 HeapFree(GetProcessHeap(), 0, MidiOutDev[wDevID].lpExtra);
1070 MidiOutDev[wDevID].lpExtra = 0;
1073 MidiOutDev[wDevID].bufsize = 0;
1074 if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
1075 WARN("can't notify client !\n");
1076 ret = MMSYSERR_INVALPARAM;
1078 MidiOutDev[wDevID].midiDesc = 0;
1082 /**************************************************************************
1083 * modData [internal]
1085 static DWORD modData(WORD wDevID, DWORD dwParam)
1087 WORD evt = LOBYTE(LOWORD(dwParam));
1088 WORD d1 = HIBYTE(LOWORD(dwParam));
1089 WORD d2 = LOBYTE(HIWORD(dwParam));
1091 TRACE("(%04X, %08lX);\n", wDevID, dwParam);
1093 if (midiSeqFD == -1) {
1094 WARN("can't play !\n");
1095 return MIDIERR_NODEVICE;
1097 switch (midiOutDevices[wDevID]->wTechnology) {
1100 * - chorus depth controller is not used
1103 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
1104 sVoice* voice = extra->voice;
1105 sChannel* channel = extra->channel;
1106 int chn = (evt & 0x0F);
1109 switch (evt & 0xF0) {
1111 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1112 /* don't stop sustained notes */
1113 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1114 voice[i].status = sVS_UNUSED;
1115 SEQ_STOP_NOTE(wDevID, i, d1, d2);
1120 if (d2 == 0) { /* note off if velocity == 0 */
1121 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1122 /* don't stop sustained notes */
1123 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1124 voice[i].status = sVS_UNUSED;
1125 SEQ_STOP_NOTE(wDevID, i, d1, 64);
1130 /* finding out in this order :
1132 * - if replaying the same note on the same channel
1133 * - the older voice (LRU)
1135 for (i = nv = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1136 if (voice[i].status == sVS_UNUSED ||
1137 (voice[i].note == d1 && voice[i].channel == chn)) {
1141 if (voice[i].cntMark < voice[0].cntMark) {
1146 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1147 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1148 nv, channel[chn].program,
1149 channel[chn].balance,
1150 channel[chn].volume,
1151 channel[chn].bender, d1, d2);
1153 SEQ_SET_PATCH(wDevID, nv, IS_DRUM_CHANNEL(extra, chn) ?
1154 (128 + d1) : channel[chn].program);
1155 SEQ_BENDER_RANGE(wDevID, nv, channel[chn].benderRange * 100);
1156 SEQ_BENDER(wDevID, nv, channel[chn].bender);
1157 SEQ_CONTROL(wDevID, nv, CTL_PAN, channel[chn].balance);
1158 SEQ_CONTROL(wDevID, nv, CTL_EXPRESSION, channel[chn].expression);
1160 /* FIXME: does not really seem to work on my SB card and
1161 * screws everything up... so lay it down
1163 SEQ_CONTROL(wDevID, nv, CTL_MAIN_VOLUME, channel[chn].volume);
1165 SEQ_START_NOTE(wDevID, nv, d1, d2);
1166 voice[nv].status = channel[chn].sustain ? sVS_SUSTAINED : sVS_PLAYING;
1167 voice[nv].note = d1;
1168 voice[nv].channel = chn;
1169 voice[nv].cntMark = extra->counter++;
1171 case MIDI_KEY_PRESSURE:
1172 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1173 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn && voice[i].note == d1) {
1174 SEQ_KEY_PRESSURE(wDevID, i, d1, d2);
1178 case MIDI_CTL_CHANGE:
1180 case CTL_BANK_SELECT: channel[chn].bank = d2; break;
1181 case CTL_MAIN_VOLUME: channel[chn].volume = d2; break;
1182 case CTL_PAN: channel[chn].balance = d2; break;
1183 case CTL_EXPRESSION: channel[chn].expression = d2; break;
1184 case CTL_SUSTAIN: channel[chn].sustain = d2;
1186 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1187 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1188 voice[i].status = sVS_SUSTAINED;
1192 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1193 if (voice[i].status == sVS_SUSTAINED && voice[i].channel == chn) {
1194 voice[i].status = sVS_UNUSED;
1195 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1200 case CTL_NONREG_PARM_NUM_LSB: channel[chn].nrgPmtLSB = d2; break;
1201 case CTL_NONREG_PARM_NUM_MSB: channel[chn].nrgPmtMSB = d2; break;
1202 case CTL_REGIST_PARM_NUM_LSB: channel[chn].regPmtLSB = d2; break;
1203 case CTL_REGIST_PARM_NUM_MSB: channel[chn].regPmtMSB = d2; break;
1204 case CTL_DATA_ENTRY:
1205 switch ((channel[chn].regPmtMSB << 8) | channel[chn].regPmtLSB) {
1207 if (channel[chn].benderRange != d2) {
1208 channel[chn].benderRange = d2;
1209 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1210 if (voice[i].channel == chn) {
1211 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1218 channel[chn].benderRange = 2;
1219 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1220 if (voice[i].channel == chn) {
1221 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1226 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1227 channel[chn].regPmtMSB, channel[chn].regPmtLSB,
1228 channel[chn].nrgPmtMSB, channel[chn].nrgPmtLSB,
1234 case 0x78: /* all sounds off */
1235 /* FIXME: I don't know if I have to take care of the channel
1236 * for this control ?
1238 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1239 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1240 voice[i].status = sVS_UNUSED;
1241 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1245 case 0x7B: /* all notes 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_PLAYING && voice[i].channel == chn) {
1251 voice[i].status = sVS_UNUSED;
1252 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1257 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1262 case MIDI_PGM_CHANGE:
1263 channel[chn].program = d1;
1265 case MIDI_CHN_PRESSURE:
1266 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1267 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1268 SEQ_KEY_PRESSURE(wDevID, i, voice[i].note, d1);
1272 case MIDI_PITCH_BEND:
1273 channel[chn].bender = (d2 << 7) + d1;
1274 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1275 if (voice[i].channel == chn) {
1276 SEQ_BENDER(wDevID, i, channel[chn].bender);
1280 case MIDI_SYSTEM_PREFIX:
1281 switch (evt & 0x0F) {
1282 case 0x0F: /* Reset */
1286 WARN("Unsupported (yet) system event %02x\n", evt & 0x0F);
1290 WARN("Internal error, shouldn't happen (event=%08x)\n", evt & 0xF0);
1291 return MMSYSERR_NOTENABLED;
1297 int dev = wDevID - MODM_NUMFMSYNTHDEVS;
1299 WARN("Internal error on devID (%u) !\n", wDevID);
1300 return MIDIERR_NODEVICE;
1303 switch (evt & 0xF0) {
1306 case MIDI_KEY_PRESSURE:
1307 case MIDI_CTL_CHANGE:
1308 case MIDI_PITCH_BEND:
1309 SEQ_MIDIOUT(dev, evt);
1310 SEQ_MIDIOUT(dev, d1);
1311 SEQ_MIDIOUT(dev, d2);
1313 case MIDI_PGM_CHANGE:
1314 case MIDI_CHN_PRESSURE:
1315 SEQ_MIDIOUT(dev, evt);
1316 SEQ_MIDIOUT(dev, d1);
1318 case MIDI_SYSTEM_PREFIX:
1319 switch (evt & 0x0F) {
1320 case 0x00: /* System Exclusive, don't do it on modData,
1321 * should require modLongData*/
1322 case 0x01: /* Undefined */
1323 case 0x04: /* Undefined. */
1324 case 0x05: /* Undefined. */
1325 case 0x07: /* End of Exclusive. */
1326 case 0x09: /* Undefined. */
1327 case 0x0D: /* Undefined. */
1329 case 0x06: /* Tune Request */
1330 case 0x08: /* Timing Clock. */
1331 case 0x0A: /* Start. */
1332 case 0x0B: /* Continue */
1333 case 0x0C: /* Stop */
1334 case 0x0E: /* Active Sensing. */
1335 SEQ_MIDIOUT(dev, evt);
1337 case 0x0F: /* Reset */
1338 /* SEQ_MIDIOUT(dev, evt);
1339 this other way may be better */
1340 SEQ_MIDIOUT(dev, MIDI_SYSTEM_PREFIX);
1341 SEQ_MIDIOUT(dev, 0x7e);
1342 SEQ_MIDIOUT(dev, 0x7f);
1343 SEQ_MIDIOUT(dev, 0x09);
1344 SEQ_MIDIOUT(dev, 0x01);
1345 SEQ_MIDIOUT(dev, 0xf7);
1347 case 0x03: /* Song Select. */
1348 SEQ_MIDIOUT(dev, evt);
1349 SEQ_MIDIOUT(dev, d1);
1350 case 0x02: /* Song Position Pointer. */
1351 SEQ_MIDIOUT(dev, evt);
1352 SEQ_MIDIOUT(dev, d1);
1353 SEQ_MIDIOUT(dev, d2);
1360 WARN("Technology not supported (yet) %d !\n",
1361 midiOutDevices[wDevID]->wTechnology);
1362 return MMSYSERR_NOTENABLED;
1367 return MMSYSERR_NOERROR;
1370 /**************************************************************************
1371 * modLongData [internal]
1373 static DWORD modLongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1378 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1380 if (midiSeqFD == -1) {
1381 WARN("can't play !\n");
1382 return MIDIERR_NODEVICE;
1385 lpData = lpMidiHdr->lpData;
1388 return MIDIERR_UNPREPARED;
1389 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1390 return MIDIERR_UNPREPARED;
1391 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1392 return MIDIERR_STILLPLAYING;
1393 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1394 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1396 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1397 * data, or can it also contain raw MIDI data, to be split up and sent to
1399 * If the latest is true, then the following WARNing will fire up
1401 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
1402 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1405 TRACE("dwBufferLength=%lu !\n", lpMidiHdr->dwBufferLength);
1406 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1407 lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
1408 lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
1410 switch (midiOutDevices[wDevID]->wTechnology) {
1412 /* FIXME: I don't think there is much to do here */
1415 if (lpData[0] != 0xF0) {
1416 /* Send end of System Exclusive */
1417 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF0);
1418 WARN("Adding missing 0xF0 marker at the begining of "
1419 "system exclusive byte stream\n");
1421 for (count = 0; count < lpMidiHdr->dwBytesRecorded; count++) {
1422 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, lpData[count]);
1424 if (lpData[count - 1] != 0xF7) {
1425 /* Send end of System Exclusive */
1426 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF7);
1427 WARN("Adding missing 0xF7 marker at the end of "
1428 "system exclusive byte stream\n");
1433 WARN("Technology not supported (yet) %d !\n",
1434 midiOutDevices[wDevID]->wTechnology);
1435 return MMSYSERR_NOTENABLED;
1438 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1439 lpMidiHdr->dwFlags |= MHDR_DONE;
1440 if (MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD)lpMidiHdr, 0L) != MMSYSERR_NOERROR) {
1441 WARN("can't notify client !\n");
1442 return MMSYSERR_INVALPARAM;
1444 return MMSYSERR_NOERROR;
1447 /**************************************************************************
1448 * modPrepare [internal]
1450 static DWORD modPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1452 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1454 if (midiSeqFD == -1) {
1455 WARN("can't prepare !\n");
1456 return MMSYSERR_NOTENABLED;
1459 /* MS doc says taht dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
1460 * asks to prepare MIDIHDR which dwFlags != 0.
1461 * So at least check for the inqueue flag
1463 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
1464 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
1465 lpMidiHdr->dwBufferLength >= 0x10000ul) {
1466 WARN("%p %p %08lx %d/%ld\n", lpMidiHdr, lpMidiHdr->lpData,
1467 lpMidiHdr->dwFlags, sizeof(MIDIHDR), dwSize);
1468 return MMSYSERR_INVALPARAM;
1471 lpMidiHdr->lpNext = 0;
1472 lpMidiHdr->dwFlags |= MHDR_PREPARED;
1473 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1474 return MMSYSERR_NOERROR;
1477 /**************************************************************************
1478 * modUnprepare [internal]
1480 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1482 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1484 if (midiSeqFD == -1) {
1485 WARN("can't unprepare !\n");
1486 return MMSYSERR_NOTENABLED;
1489 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0)
1490 return MMSYSERR_INVALPARAM;
1491 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1492 return MIDIERR_STILLPLAYING;
1493 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
1494 return MMSYSERR_NOERROR;
1497 /**************************************************************************
1498 * modReset [internal]
1500 static DWORD modReset(WORD wDevID)
1504 TRACE("(%04X);\n", wDevID);
1506 /* stop all notes */
1507 /* FIXME: check if 0x78B0 is channel dependant or not. I coded it so that
1508 * it's channel dependent...
1510 for (chn = 0; chn < 16; chn++) {
1511 /* turn off every note */
1512 modData(wDevID, 0x7800 | MIDI_CTL_CHANGE | chn);
1513 /* remove sustain on all channels */
1514 modData(wDevID, (CTL_SUSTAIN << 8) | MIDI_CTL_CHANGE | chn);
1516 /* FIXME: the LongData buffers must also be returned to the app */
1517 return MMSYSERR_NOERROR;
1520 #endif /* HAVE_OSS_MIDI */
1522 /*======================================================================*
1523 * MIDI entry points *
1524 *======================================================================*/
1526 /**************************************************************************
1527 * OSS_midMessage [sample driver]
1529 DWORD WINAPI OSS_midMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1530 DWORD dwParam1, DWORD dwParam2)
1532 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1533 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1535 #ifdef HAVE_OSS_MIDI
1539 /* FIXME: Pretend this is supported */
1542 return midOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1544 return midClose(wDevID);
1545 case MIDM_ADDBUFFER:
1546 return midAddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1548 return midPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1549 case MIDM_UNPREPARE:
1550 return midUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1551 case MIDM_GETDEVCAPS:
1552 return midGetDevCaps(wDevID, (LPMIDIINCAPSA)dwParam1,dwParam2);
1553 case MIDM_GETNUMDEVS:
1554 return MIDM_NUMDEVS;
1556 return midReset(wDevID);
1558 return midStart(wDevID);
1560 return midStop(wDevID);
1563 TRACE("Unsupported message\n");
1565 return MMSYSERR_NOTSUPPORTED;
1568 /**************************************************************************
1569 * OSS_modMessage [sample driver]
1571 DWORD WINAPI OSS_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1572 DWORD dwParam1, DWORD dwParam2)
1574 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1575 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1578 #ifdef HAVE_OSS_MIDI
1583 /* FIXME: Pretend this is supported */
1586 return modOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1588 return modClose(wDevID);
1590 return modData(wDevID, dwParam1);
1592 return modLongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1594 return modPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1595 case MODM_UNPREPARE:
1596 return modUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1597 case MODM_GETDEVCAPS:
1598 return modGetDevCaps(wDevID, (LPMIDIOUTCAPSA)dwParam1, dwParam2);
1599 case MODM_GETNUMDEVS:
1600 return MODM_NUMDEVS;
1601 case MODM_GETVOLUME:
1603 case MODM_SETVOLUME:
1606 return modReset(wDevID);
1609 TRACE("Unsupported message\n");
1611 return MMSYSERR_NOTSUPPORTED;
1614 /*-----------------------------------------------------------------------*/