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
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <sys/ioctl.h>
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(midi);
48 #define MIDI_SEQ "/dev/sequencer"
53 MIDIOPENDESC midiDesc;
57 unsigned char incoming[3];
58 unsigned char incPrev;
66 MIDIOPENDESC midiDesc;
70 void* lpExtra; /* according to port type (MIDI, FM...), extra data when needed */
73 static WINE_MIDIIN MidiInDev [MAX_MIDIINDRV ];
74 static WINE_MIDIOUT MidiOutDev[MAX_MIDIOUTDRV];
76 /* this is the total number of MIDI out devices found */
77 static int MODM_NUMDEVS = 0;
78 /* this is the number of FM synthetizers (index from 0 to
79 NUMFMSYNTHDEVS - 1) */
80 static int MODM_NUMFMSYNTHDEVS = 0;
81 /* this is the number of Midi ports (index from NUMFMSYNTHDEVS to
82 NUMFMSYNTHDEVS + NUMMIDIDEVS - 1) */
83 static int MODM_NUMMIDIDEVS = 0;
85 /* this is the total number of MIDI out devices found */
86 static int MIDM_NUMDEVS = 0;
88 static int midiSeqFD = -1;
89 static int numOpenMidiSeq = 0;
90 static UINT midiInTimerID = 0;
91 static int numStartedMidiIn = 0;
93 /* this structure holds pointers with information for each MIDI
96 static LPMIDIOUTCAPSA midiOutDevices[MAX_MIDIOUTDRV];
98 /* this structure holds pointers with information for each MIDI
101 static LPMIDIINCAPSA midiInDevices [MAX_MIDIINDRV];
104 * FIXME : all tests on device ID for midXXX and modYYY are made against
105 * MAX_MIDIxxDRV (when they are made) but should be done against the actual
106 * number of midi devices found...
109 /*======================================================================*
110 * Low level MIDI implementation *
111 *======================================================================*/
113 static int midiOpenSeq(void);
114 static int midiCloseSeq(void);
116 /**************************************************************************
117 * unixToWindowsDeviceType [internal]
119 * return the Windows equivalent to a Unix Device Type
122 static int MIDI_UnixToWindowsDeviceType(int type)
124 /* MOD_MIDIPORT output port
125 * MOD_SYNTH generic internal synth
126 * MOD_SQSYNTH square wave internal synth
127 * MOD_FMSYNTH FM internal synth
128 * MOD_MAPPER MIDI mapper
131 /* FIXME Is this really the correct equivalence from UNIX to
132 Windows Sound type */
135 case SYNTH_TYPE_FM: return MOD_FMSYNTH;
136 case SYNTH_TYPE_SAMPLE: return MOD_SYNTH;
137 case SYNTH_TYPE_MIDI: return MOD_MIDIPORT;
139 ERR("Cannot determine the type of this midi device. "
140 "Assuming FM Synth\n");
146 /**************************************************************************
147 * OSS_MidiInit [internal]
149 * Initializes the MIDI devices information variables
151 BOOL OSS_MidiInit(void)
153 int i, status, numsynthdevs = 255, nummididevs = 255;
154 struct synth_info sinfo;
155 struct midi_info minfo;
156 static BOOL bInitDone = FALSE;
161 TRACE("Initializing the MIDI variables.\n");
164 /* try to open device */
165 if (midiOpenSeq() == -1) {
169 /* find how many Synth devices are there in the system */
170 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRSYNTHS, &numsynthdevs);
173 ERR("ioctl for nr synth failed.\n");
178 if (numsynthdevs > MAX_MIDIOUTDRV) {
179 ERR("MAX_MIDIOUTDRV (%d) was enough for the number of devices (%d). "
180 "Some FM devices will not be available.\n",MAX_MIDIOUTDRV,numsynthdevs);
181 numsynthdevs = MAX_MIDIOUTDRV;
184 for (i = 0; i < numsynthdevs; i++) {
185 LPMIDIOUTCAPSA tmplpCaps;
188 status = ioctl(midiSeqFD, SNDCTL_SYNTH_INFO, &sinfo);
190 ERR("ioctl for synth info failed.\n");
195 tmplpCaps = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA));
198 /* We also have the information sinfo.synth_subtype, not used here
201 /* Manufac ID. We do not have access to this with soundcard.h
202 * Does not seem to be a problem, because in mmsystem.h only
203 * Microsoft's ID is listed.
205 tmplpCaps->wMid = 0x00FF;
206 tmplpCaps->wPid = 0x0001; /* FIXME Product ID */
207 /* Product Version. We simply say "1" */
208 tmplpCaps->vDriverVersion = 0x001;
209 strcpy(tmplpCaps->szPname, sinfo.name);
211 tmplpCaps->wTechnology = MIDI_UnixToWindowsDeviceType(sinfo.synth_type);
212 tmplpCaps->wVoices = sinfo.nr_voices;
214 /* FIXME Is it possible to know the maximum
215 * number of simultaneous notes of a soundcard ?
216 * I believe we don't have this information, but
217 * it's probably equal or more than wVoices
219 tmplpCaps->wNotes = sinfo.nr_voices;
220 tmplpCaps->wChannelMask= 0xFFFF;
222 /* FIXME Do we have this information?
223 * Assuming the soundcards can handle
224 * MIDICAPS_VOLUME and MIDICAPS_LRVOLUME but
225 * not MIDICAPS_CACHE.
227 tmplpCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
229 midiOutDevices[i] = tmplpCaps;
231 if (sinfo.capabilities & SYNTH_CAP_INPUT) {
232 FIXME("Synthesizer support MIDI in. Not supported yet (please report)\n");
235 TRACE("SynthOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%ld\n"
236 "\tOSS info: synth subtype=%d capa=%lx\n",
237 i, tmplpCaps->szPname, tmplpCaps->wTechnology, tmplpCaps->wVoices,
238 tmplpCaps->wNotes, tmplpCaps->wChannelMask, tmplpCaps->dwSupport,
239 sinfo.synth_subtype, (long)sinfo.capabilities);
242 /* find how many MIDI devices are there in the system */
243 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRMIDIS, &nummididevs);
245 ERR("ioctl on nr midi failed.\n");
250 /* FIXME: the two restrictions below could be loosen in some cases */
251 if (numsynthdevs + nummididevs > MAX_MIDIOUTDRV) {
252 ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
253 "Some MIDI devices will not be available.\n");
254 nummididevs = MAX_MIDIOUTDRV - numsynthdevs;
257 if (nummididevs > MAX_MIDIINDRV) {
258 ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
259 "Some MIDI devices will not be available.\n",MAX_MIDIINDRV,nummididevs);
260 nummididevs = MAX_MIDIINDRV;
263 for (i = 0; i < nummididevs; i++) {
264 LPMIDIOUTCAPSA tmplpOutCaps;
265 LPMIDIINCAPSA tmplpInCaps;
268 status = ioctl(midiSeqFD, SNDCTL_MIDI_INFO, &minfo);
270 ERR("ioctl on midi info for device %d failed.\n", i);
275 tmplpOutCaps = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA));
278 /* This whole part is somewhat obscure to me. I'll keep trying to dig
279 info about it. If you happen to know, please tell us. The very
280 descritive minfo.dev_type was not used here.
282 /* Manufac ID. We do not have access to this with soundcard.h
283 Does not seem to be a problem, because in mmsystem.h only
284 Microsoft's ID is listed */
285 tmplpOutCaps->wMid = 0x00FF;
286 tmplpOutCaps->wPid = 0x0001; /* FIXME Product ID */
287 /* Product Version. We simply say "1" */
288 tmplpOutCaps->vDriverVersion = 0x001;
289 strcpy(tmplpOutCaps->szPname, minfo.name);
291 tmplpOutCaps->wTechnology = MOD_MIDIPORT; /* FIXME Is this right? */
292 /* Does it make any difference? */
293 tmplpOutCaps->wVoices = 16;
294 /* Does it make any difference? */
295 tmplpOutCaps->wNotes = 16;
296 tmplpOutCaps->wChannelMask= 0xFFFF;
298 /* FIXME Does it make any difference? */
299 tmplpOutCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
301 midiOutDevices[numsynthdevs + i] = tmplpOutCaps;
303 tmplpInCaps = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIINCAPSA));
306 /* This whole part is somewhat obscure to me. I'll keep trying to dig
307 info about it. If you happen to know, please tell us. The very
308 descritive minfo.dev_type was not used here.
310 /* Manufac ID. We do not have access to this with soundcard.h
311 Does not seem to be a problem, because in mmsystem.h only
312 Microsoft's ID is listed */
313 tmplpInCaps->wMid = 0x00FF;
314 tmplpInCaps->wPid = 0x0001; /* FIXME Product ID */
315 /* Product Version. We simply say "1" */
316 tmplpInCaps->vDriverVersion = 0x001;
317 strcpy(tmplpInCaps->szPname, minfo.name);
319 /* FIXME : could we get better information than that ? */
320 tmplpInCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
322 midiInDevices[i] = tmplpInCaps;
324 TRACE("MidiOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%ld\n"
325 "MidiIn [%d]\tname='%s' support=%ld\n"
326 "\tOSS info: midi dev-type=%d, capa=%lx\n",
327 i, tmplpOutCaps->szPname, tmplpOutCaps->wTechnology,
328 tmplpOutCaps->wVoices, tmplpOutCaps->wNotes,
329 tmplpOutCaps->wChannelMask, tmplpOutCaps->dwSupport,
330 i, tmplpInCaps->szPname, tmplpInCaps->dwSupport,
331 minfo.dev_type, (long)minfo.capabilities);
334 /* windows does not seem to differentiate Synth from MIDI devices */
335 MODM_NUMFMSYNTHDEVS = numsynthdevs;
336 MODM_NUMMIDIDEVS = nummididevs;
337 MODM_NUMDEVS = numsynthdevs + nummididevs;
339 MIDM_NUMDEVS = nummididevs;
341 /* close file and exit */
347 /**************************************************************************
348 * MIDI_NotifyClient [internal]
350 static DWORD MIDI_NotifyClient(UINT wDevID, WORD wMsg,
351 DWORD dwParam1, DWORD dwParam2)
358 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",
359 wDevID, wMsg, dwParam1, dwParam2);
365 if (wDevID > MAX_MIDIOUTDRV)
366 return MCIERR_INTERNAL;
368 dwCallBack = MidiOutDev[wDevID].midiDesc.dwCallback;
369 uFlags = MidiOutDev[wDevID].wFlags;
370 hDev = MidiOutDev[wDevID].midiDesc.hMidi;
371 dwInstance = MidiOutDev[wDevID].midiDesc.dwInstance;
378 if (wDevID > MAX_MIDIINDRV)
379 return MCIERR_INTERNAL;
381 dwCallBack = MidiInDev[wDevID].midiDesc.dwCallback;
382 uFlags = MidiInDev[wDevID].wFlags;
383 hDev = MidiInDev[wDevID].midiDesc.hMidi;
384 dwInstance = MidiInDev[wDevID].midiDesc.dwInstance;
387 WARN("Unsupported MSW-MIDI message %u\n", wMsg);
388 return MCIERR_INTERNAL;
391 return DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2) ?
395 static int midi_warn = 1;
396 /**************************************************************************
397 * midiOpenSeq [internal]
399 static int midiOpenSeq(void)
401 if (numOpenMidiSeq == 0) {
402 midiSeqFD = open(MIDI_SEQ, O_RDWR, 0);
403 if (midiSeqFD == -1) {
406 WARN("Can't open MIDI device '%s' ! (%s). If your "
407 "program needs this (probably not): %s\n",
408 MIDI_SEQ, strerror(errno),
410 "create it ! (\"man MAKEDEV\" ?)" :
412 "load MIDI sequencer kernel driver !" :
414 "grant access ! (\"man chmod\")" : ""
420 if (fcntl(midiSeqFD, F_SETFL, O_NONBLOCK) < 0) {
421 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno, strerror(errno));
426 fcntl(midiSeqFD, F_SETFD, 1); /* set close on exec flag */
427 ioctl(midiSeqFD, SNDCTL_SEQ_RESET);
433 /**************************************************************************
434 * midiCloseSeq [internal]
436 static int midiCloseSeq(void)
438 if (--numOpenMidiSeq == 0) {
445 /* FIXME: this is a bad idea, it's even not static... */
448 /* FIXME: this is not reentrant, not static - because of global variable
451 /**************************************************************************
452 * seqbuf_dump [internal]
454 void seqbuf_dump(void)
457 if (write(midiSeqFD, _seqbuf, _seqbufptr) == -1) {
458 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
459 midiSeqFD, errno, strerror(errno));
462 * in any case buffer is lost so that if many errors occur the buffer
469 static void midReceiveChar(WORD wDevID, unsigned char value, DWORD dwTime)
473 TRACE("Adding %02xh to %d[%d]\n", value, wDevID, MidiInDev[wDevID].incLen);
475 if (wDevID >= MAX_MIDIINDRV) {
479 if (MidiInDev[wDevID].state == 0) {
480 TRACE("input not started, thrown away\n");
484 if (MidiInDev[wDevID].state & 2) { /* system exclusive */
485 LPMIDIHDR lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
489 LPBYTE lpData = lpMidiHdr->lpData;
491 lpData[lpMidiHdr->dwBytesRecorded++] = value;
492 if (lpMidiHdr->dwBytesRecorded == lpMidiHdr->dwBufferLength) {
496 if (value == 0xF7) { /* then end */
497 MidiInDev[wDevID].state &= ~2;
500 if (sbfb && lpMidiHdr != NULL) {
501 lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
502 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
503 lpMidiHdr->dwFlags |= MHDR_DONE;
504 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)lpMidiHdr->lpNext;
505 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD)lpMidiHdr, dwTime) != MMSYSERR_NOERROR) {
506 WARN("Couldn't notify client\n");
512 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
513 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
515 if (!IS_CMD(value) && MidiInDev[wDevID].incLen == 0) { /* try to reuse old cmd */
516 if (IS_CMD(MidiInDev[wDevID].incPrev) && !IS_SYS_CMD(MidiInDev[wDevID].incPrev)) {
517 MidiInDev[wDevID].incoming[0] = MidiInDev[wDevID].incPrev;
518 MidiInDev[wDevID].incLen = 1;
519 TRACE("Reusing old command %02xh\n", MidiInDev[wDevID].incPrev);
521 FIXME("error for midi-in, should generate MIM_ERROR notification:"
522 " prev=%02Xh, incLen=%02Xh\n",
523 MidiInDev[wDevID].incPrev, MidiInDev[wDevID].incLen);
527 MidiInDev[wDevID].incoming[(int)(MidiInDev[wDevID].incLen++)] = value;
528 if (MidiInDev[wDevID].incLen == 1 && !IS_SYS_CMD(MidiInDev[wDevID].incoming[0])) {
529 /* store new cmd, just in case */
530 MidiInDev[wDevID].incPrev = MidiInDev[wDevID].incoming[0];
536 switch (MidiInDev[wDevID].incoming[0] & 0xF0) {
539 case MIDI_KEY_PRESSURE:
540 case MIDI_CTL_CHANGE:
541 case MIDI_PITCH_BEND:
542 if (MidiInDev[wDevID].incLen == 3) {
543 toSend = (MidiInDev[wDevID].incoming[2] << 16) |
544 (MidiInDev[wDevID].incoming[1] << 8) |
545 (MidiInDev[wDevID].incoming[0] << 0);
548 case MIDI_PGM_CHANGE:
549 case MIDI_CHN_PRESSURE:
550 if (MidiInDev[wDevID].incLen == 2) {
551 toSend = (MidiInDev[wDevID].incoming[1] << 8) |
552 (MidiInDev[wDevID].incoming[0] << 0);
555 case MIDI_SYSTEM_PREFIX:
556 if (MidiInDev[wDevID].incoming[0] == 0xF0) {
557 MidiInDev[wDevID].state |= 2;
558 MidiInDev[wDevID].incLen = 0;
560 if (MidiInDev[wDevID].incLen == 1) {
561 toSend = (MidiInDev[wDevID].incoming[0] << 0);
566 WARN("This shouldn't happen (%02X)\n", MidiInDev[wDevID].incoming[0]);
569 TRACE("Sending event %08lx\n", toSend);
570 MidiInDev[wDevID].incLen = 0;
571 dwTime -= MidiInDev[wDevID].startTime;
572 if (MIDI_NotifyClient(wDevID, MIM_DATA, toSend, dwTime) != MMSYSERR_NOERROR) {
573 WARN("Couldn't notify client\n");
578 static VOID WINAPI midTimeCallback(HWND hwnd, UINT msg, UINT id, DWORD dwTime)
580 unsigned char buffer[256];
583 TRACE("(%04X, %d, %d, %lu)\n", hwnd, msg, id, dwTime);
585 len = read(midiSeqFD, buffer, sizeof(buffer));
588 if ((len % 4) != 0) {
589 WARN("Bad length %d, errno %d (%s)\n", len, errno, strerror(errno));
593 for (idx = 0; idx < len; ) {
594 if (buffer[idx] & 0x80) {
596 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
597 buffer[idx + 0], buffer[idx + 1],
598 buffer[idx + 2], buffer[idx + 3],
599 buffer[idx + 4], buffer[idx + 5],
600 buffer[idx + 6], buffer[idx + 7]);
603 switch (buffer[idx + 0]) {
608 midReceiveChar(buffer[idx + 2], buffer[idx + 1], dwTime);
611 TRACE("Unsupported event %d\n", buffer[idx + 0]);
619 /**************************************************************************
620 * midGetDevCaps [internal]
622 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPSA lpCaps, DWORD dwSize)
624 TRACE("(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
626 if (wDevID >= MIDM_NUMDEVS) return MMSYSERR_BADDEVICEID;
627 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
629 memcpy(lpCaps, midiInDevices[wDevID], min(dwSize, sizeof(*lpCaps)));
631 return MMSYSERR_NOERROR;
634 /**************************************************************************
637 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
639 TRACE("(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
641 if (lpDesc == NULL) {
642 WARN("Invalid Parameter !\n");
643 return MMSYSERR_INVALPARAM;
647 * how to check that content of lpDesc is correct ?
649 if (wDevID >= MAX_MIDIINDRV) {
650 WARN("wDevID too large (%u) !\n", wDevID);
651 return MMSYSERR_BADDEVICEID;
653 if (MidiInDev[wDevID].midiDesc.hMidi != 0) {
654 WARN("device already open !\n");
655 return MMSYSERR_ALLOCATED;
657 if ((dwFlags & MIDI_IO_STATUS) != 0) {
658 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
659 dwFlags &= ~MIDI_IO_STATUS;
661 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
662 FIXME("Bad dwFlags\n");
663 return MMSYSERR_INVALFLAG;
666 if (midiOpenSeq() < 0) {
667 return MMSYSERR_ERROR;
670 if (numStartedMidiIn++ == 0) {
671 midiInTimerID = SetTimer(0, 0, 250, midTimeCallback);
672 if (!midiInTimerID) {
673 numStartedMidiIn = 0;
674 WARN("Couldn't start timer for midi-in\n");
676 return MMSYSERR_ERROR;
678 TRACE("Starting timer (%u) for midi-in\n", midiInTimerID);
681 MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
683 MidiInDev[wDevID].lpQueueHdr = NULL;
684 MidiInDev[wDevID].dwTotalPlayed = 0;
685 MidiInDev[wDevID].bufsize = 0x3FFF;
686 MidiInDev[wDevID].midiDesc = *lpDesc;
687 MidiInDev[wDevID].state = 0;
688 MidiInDev[wDevID].incLen = 0;
689 MidiInDev[wDevID].startTime = 0;
691 if (MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
692 WARN("can't notify client !\n");
693 return MMSYSERR_INVALPARAM;
695 return MMSYSERR_NOERROR;
698 /**************************************************************************
699 * midClose [internal]
701 static DWORD midClose(WORD wDevID)
703 int ret = MMSYSERR_NOERROR;
705 TRACE("(%04X);\n", wDevID);
707 if (wDevID >= MAX_MIDIINDRV) {
708 WARN("wDevID too big (%u) !\n", wDevID);
709 return MMSYSERR_BADDEVICEID;
711 if (MidiInDev[wDevID].midiDesc.hMidi == 0) {
712 WARN("device not opened !\n");
713 return MMSYSERR_ERROR;
715 if (MidiInDev[wDevID].lpQueueHdr != 0) {
716 return MIDIERR_STILLPLAYING;
719 if (midiSeqFD == -1) {
721 return MMSYSERR_ERROR;
723 if (--numStartedMidiIn == 0) {
724 TRACE("Stopping timer for midi-in\n");
725 if (!KillTimer(0, midiInTimerID)) {
726 WARN("Couldn't stop timer for midi-in\n");
732 MidiInDev[wDevID].bufsize = 0;
733 if (MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
734 WARN("can't notify client !\n");
735 ret = MMSYSERR_INVALPARAM;
737 MidiInDev[wDevID].midiDesc.hMidi = 0;
741 /**************************************************************************
742 * midAddBuffer [internal]
744 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
746 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
748 if (lpMidiHdr == NULL) return MMSYSERR_INVALPARAM;
749 if (sizeof(MIDIHDR) > dwSize) return MMSYSERR_INVALPARAM;
750 if (lpMidiHdr->dwBufferLength == 0) return MMSYSERR_INVALPARAM;
751 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
752 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
754 if (MidiInDev[wDevID].lpQueueHdr == 0) {
755 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr;
759 for (ptr = MidiInDev[wDevID].lpQueueHdr;
761 ptr = (LPMIDIHDR)ptr->lpNext);
762 ptr->lpNext = (struct midihdr_tag*)lpMidiHdr;
764 return MMSYSERR_NOERROR;
767 /**************************************************************************
768 * midPrepare [internal]
770 static DWORD midPrepare(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->dwFlags != 0 ||
776 lpMidiHdr->dwBufferLength >= 0x10000ul)
777 return MMSYSERR_INVALPARAM;
779 lpMidiHdr->lpNext = 0;
780 lpMidiHdr->dwFlags |= MHDR_PREPARED;
781 lpMidiHdr->dwBytesRecorded = 0;
783 return MMSYSERR_NOERROR;
786 /**************************************************************************
787 * midUnprepare [internal]
789 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
791 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
793 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
794 lpMidiHdr->lpData == 0 || lpMidiHdr->dwBufferLength >= 0x10000ul)
795 return MMSYSERR_INVALPARAM;
797 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
798 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
800 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
802 return MMSYSERR_NOERROR;
805 /**************************************************************************
806 * midReset [internal]
808 static DWORD midReset(WORD wDevID)
810 DWORD dwTime = GetTickCount();
812 TRACE("(%04X);\n", wDevID);
814 while (MidiInDev[wDevID].lpQueueHdr) {
815 MidiInDev[wDevID].lpQueueHdr->dwFlags &= ~MHDR_INQUEUE;
816 MidiInDev[wDevID].lpQueueHdr->dwFlags |= MHDR_DONE;
817 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
818 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA,
819 (DWORD)MidiInDev[wDevID].lpQueueHdr, dwTime) != MMSYSERR_NOERROR) {
820 WARN("Couldn't notify client\n");
822 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)MidiInDev[wDevID].lpQueueHdr->lpNext;
825 return MMSYSERR_NOERROR;
829 /**************************************************************************
830 * midStart [internal]
832 static DWORD midStart(WORD wDevID)
834 TRACE("(%04X);\n", wDevID);
836 /* FIXME : should test value of wDevID */
838 MidiInDev[wDevID].state = 1;
839 MidiInDev[wDevID].startTime = GetTickCount();
840 return MMSYSERR_NOERROR;
843 /**************************************************************************
846 static DWORD midStop(WORD wDevID)
848 TRACE("(%04X);\n", wDevID);
850 /* FIXME : should test value of wDevID */
851 MidiInDev[wDevID].state = 0;
852 return MMSYSERR_NOERROR;
855 /*-----------------------------------------------------------------------*/
857 typedef struct sVoice {
858 int note; /* 0 means not used */
860 unsigned cntMark : 30,
863 #define sVS_PLAYING 1
864 #define sVS_SUSTAINED 2
867 typedef struct sChannel {
873 int bank; /* CTL_BANK_SELECT */
874 int volume; /* CTL_MAIN_VOLUME */
875 int balance; /* CTL_BALANCE */
876 int expression; /* CTL_EXPRESSION */
877 int sustain; /* CTL_SUSTAIN */
879 unsigned char nrgPmtMSB; /* Non register Parameters */
880 unsigned char nrgPmtLSB;
881 unsigned char regPmtMSB; /* Non register Parameters */
882 unsigned char regPmtLSB;
885 typedef struct sFMextra {
888 sChannel channel[16]; /* MIDI has only 16 channels */
889 sVoice voice[1]; /* dyn allocated according to sound card */
890 /* do not append fields below voice[1] since the size of this structure
891 * depends on the number of available voices on the FM synth...
895 extern unsigned char midiFMInstrumentPatches[16 * 128];
896 extern unsigned char midiFMDrumsPatches [16 * 128];
898 /**************************************************************************
899 * modFMLoad [internal]
901 static int modFMLoad(int dev)
904 struct sbi_instrument sbi;
909 memset(sbi.operators + 16, 0, 16);
910 for (i = 0; i < 128; i++) {
912 memcpy(sbi.operators, midiFMInstrumentPatches + i * 16, 16);
914 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
915 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
919 for (i = 0; i < 128; i++) {
920 sbi.channel = 128 + i;
921 memcpy(sbi.operators, midiFMDrumsPatches + i * 16, 16);
923 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
924 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
931 /**************************************************************************
932 * modFMReset [internal]
934 static void modFMReset(WORD wDevID)
936 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
937 sVoice* voice = extra->voice;
938 sChannel* channel = extra->channel;
941 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
942 if (voice[i].status != sVS_UNUSED) {
943 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
945 SEQ_KEY_PRESSURE(wDevID, i, 127, 0);
946 SEQ_CONTROL(wDevID, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
948 voice[i].channel = -1;
949 voice[i].cntMark = 0;
950 voice[i].status = sVS_UNUSED;
952 for (i = 0; i < 16; i++) {
953 channel[i].program = 0;
954 channel[i].bender = 8192;
955 channel[i].benderRange = 2;
957 channel[i].volume = 127;
958 channel[i].balance = 64;
959 channel[i].expression = 0;
960 channel[i].sustain = 0;
963 extra->drumSetMask = 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
967 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
969 /**************************************************************************
970 * modGetDevCaps [internal]
972 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPSA lpCaps, DWORD dwSize)
974 TRACE("(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
976 if (wDevID >= MODM_NUMDEVS) return MMSYSERR_BADDEVICEID;
977 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
979 memcpy(lpCaps, midiOutDevices[wDevID], min(dwSize, sizeof(*lpCaps)));
981 return MMSYSERR_NOERROR;
984 /**************************************************************************
987 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
989 TRACE("(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
990 if (lpDesc == NULL) {
991 WARN("Invalid Parameter !\n");
992 return MMSYSERR_INVALPARAM;
994 if (wDevID >= MAX_MIDIOUTDRV) {
995 TRACE("MAX_MIDIOUTDRV reached !\n");
996 return MMSYSERR_BADDEVICEID;
998 if (MidiOutDev[wDevID].midiDesc.hMidi != 0) {
999 WARN("device already open !\n");
1000 return MMSYSERR_ALLOCATED;
1002 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
1003 WARN("bad dwFlags\n");
1004 return MMSYSERR_INVALFLAG;
1006 if (midiOutDevices[wDevID] == NULL) {
1007 TRACE("un-allocated wDevID\n");
1008 return MMSYSERR_BADDEVICEID;
1011 MidiOutDev[wDevID].lpExtra = 0;
1013 switch (midiOutDevices[wDevID]->wTechnology) {
1016 void* extra = HeapAlloc(GetProcessHeap(), 0,
1017 sizeof(struct sFMextra) +
1018 sizeof(struct sVoice) * (midiOutDevices[wDevID]->wVoices - 1));
1021 WARN("can't alloc extra data !\n");
1022 return MMSYSERR_NOMEM;
1024 MidiOutDev[wDevID].lpExtra = extra;
1025 if (midiOpenSeq() < 0) {
1026 MidiOutDev[wDevID].lpExtra = 0;
1027 HeapFree(GetProcessHeap(), 0, extra);
1028 return MMSYSERR_ERROR;
1030 if (modFMLoad(wDevID) < 0) {
1032 MidiOutDev[wDevID].lpExtra = 0;
1033 HeapFree(GetProcessHeap(), 0, extra);
1034 return MMSYSERR_ERROR;
1040 if (midiOpenSeq() < 0) {
1041 return MMSYSERR_ALLOCATED;
1045 WARN("Technology not supported (yet) %d !\n",
1046 midiOutDevices[wDevID]->wTechnology);
1047 return MMSYSERR_NOTENABLED;
1050 MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1052 MidiOutDev[wDevID].lpQueueHdr = NULL;
1053 MidiOutDev[wDevID].dwTotalPlayed = 0;
1054 MidiOutDev[wDevID].bufsize = 0x3FFF;
1055 MidiOutDev[wDevID].midiDesc = *lpDesc;
1057 if (MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
1058 WARN("can't notify client !\n");
1059 return MMSYSERR_INVALPARAM;
1061 TRACE("Successful !\n");
1062 return MMSYSERR_NOERROR;
1066 /**************************************************************************
1067 * modClose [internal]
1069 static DWORD modClose(WORD wDevID)
1071 int ret = MMSYSERR_NOERROR;
1073 TRACE("(%04X);\n", wDevID);
1075 if (MidiOutDev[wDevID].midiDesc.hMidi == 0) {
1076 WARN("device not opened !\n");
1077 return MMSYSERR_ERROR;
1079 /* FIXME: should test that no pending buffer is still in the queue for
1082 if (midiSeqFD == -1) {
1083 WARN("can't close !\n");
1084 return MMSYSERR_ERROR;
1087 switch (midiOutDevices[wDevID]->wTechnology) {
1093 WARN("Technology not supported (yet) %d !\n",
1094 midiOutDevices[wDevID]->wTechnology);
1095 return MMSYSERR_NOTENABLED;
1098 if (MidiOutDev[wDevID].lpExtra != 0) {
1099 HeapFree(GetProcessHeap(), 0, MidiOutDev[wDevID].lpExtra);
1100 MidiOutDev[wDevID].lpExtra = 0;
1103 MidiOutDev[wDevID].bufsize = 0;
1104 if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
1105 WARN("can't notify client !\n");
1106 ret = MMSYSERR_INVALPARAM;
1108 MidiOutDev[wDevID].midiDesc.hMidi = 0;
1112 /**************************************************************************
1113 * modData [internal]
1115 static DWORD modData(WORD wDevID, DWORD dwParam)
1117 WORD evt = LOBYTE(LOWORD(dwParam));
1118 WORD d1 = HIBYTE(LOWORD(dwParam));
1119 WORD d2 = LOBYTE(HIWORD(dwParam));
1121 TRACE("(%04X, %08lX);\n", wDevID, dwParam);
1123 if (midiSeqFD == -1) {
1124 WARN("can't play !\n");
1125 return MIDIERR_NODEVICE;
1127 switch (midiOutDevices[wDevID]->wTechnology) {
1130 * - chorus depth controller is not used
1133 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
1134 sVoice* voice = extra->voice;
1135 sChannel* channel = extra->channel;
1136 int chn = (evt & 0x0F);
1139 switch (evt & 0xF0) {
1141 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1142 /* don't stop sustained notes */
1143 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1144 voice[i].status = sVS_UNUSED;
1145 SEQ_STOP_NOTE(wDevID, i, d1, d2);
1150 if (d2 == 0) { /* note off if velocity == 0 */
1151 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1152 /* don't stop sustained notes */
1153 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1154 voice[i].status = sVS_UNUSED;
1155 SEQ_STOP_NOTE(wDevID, i, d1, 64);
1160 /* finding out in this order :
1162 * - if replaying the same note on the same channel
1163 * - the older voice (LRU)
1165 for (i = nv = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1166 if (voice[i].status == sVS_UNUSED ||
1167 (voice[i].note == d1 && voice[i].channel == chn)) {
1171 if (voice[i].cntMark < voice[0].cntMark) {
1176 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1177 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1178 nv, channel[chn].program,
1179 channel[chn].balance,
1180 channel[chn].volume,
1181 channel[chn].bender, d1, d2);
1183 SEQ_SET_PATCH(wDevID, nv, IS_DRUM_CHANNEL(extra, chn) ?
1184 (128 + d1) : channel[chn].program);
1185 SEQ_BENDER_RANGE(wDevID, nv, channel[chn].benderRange * 100);
1186 SEQ_BENDER(wDevID, nv, channel[chn].bender);
1187 SEQ_CONTROL(wDevID, nv, CTL_PAN, channel[chn].balance);
1188 SEQ_CONTROL(wDevID, nv, CTL_EXPRESSION, channel[chn].expression);
1190 /* FIXME: does not really seem to work on my SB card and
1191 * screws everything up... so lay it down
1193 SEQ_CONTROL(wDevID, nv, CTL_MAIN_VOLUME, channel[chn].volume);
1195 SEQ_START_NOTE(wDevID, nv, d1, d2);
1196 voice[nv].status = channel[chn].sustain ? sVS_SUSTAINED : sVS_PLAYING;
1197 voice[nv].note = d1;
1198 voice[nv].channel = chn;
1199 voice[nv].cntMark = extra->counter++;
1201 case MIDI_KEY_PRESSURE:
1202 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1203 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn && voice[i].note == d1) {
1204 SEQ_KEY_PRESSURE(wDevID, i, d1, d2);
1208 case MIDI_CTL_CHANGE:
1210 case CTL_BANK_SELECT: channel[chn].bank = d2; break;
1211 case CTL_MAIN_VOLUME: channel[chn].volume = d2; break;
1212 case CTL_PAN: channel[chn].balance = d2; break;
1213 case CTL_EXPRESSION: channel[chn].expression = d2; break;
1214 case CTL_SUSTAIN: channel[chn].sustain = d2;
1216 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1217 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1218 voice[i].status = sVS_SUSTAINED;
1222 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1223 if (voice[i].status == sVS_SUSTAINED && voice[i].channel == chn) {
1224 voice[i].status = sVS_UNUSED;
1225 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1230 case CTL_NONREG_PARM_NUM_LSB: channel[chn].nrgPmtLSB = d2; break;
1231 case CTL_NONREG_PARM_NUM_MSB: channel[chn].nrgPmtMSB = d2; break;
1232 case CTL_REGIST_PARM_NUM_LSB: channel[chn].regPmtLSB = d2; break;
1233 case CTL_REGIST_PARM_NUM_MSB: channel[chn].regPmtMSB = d2; break;
1234 case CTL_DATA_ENTRY:
1235 switch ((channel[chn].regPmtMSB << 8) | channel[chn].regPmtLSB) {
1237 if (channel[chn].benderRange != d2) {
1238 channel[chn].benderRange = d2;
1239 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1240 if (voice[i].channel == chn) {
1241 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1248 channel[chn].benderRange = 2;
1249 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1250 if (voice[i].channel == chn) {
1251 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1256 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1257 channel[chn].regPmtMSB, channel[chn].regPmtLSB,
1258 channel[chn].nrgPmtMSB, channel[chn].nrgPmtLSB,
1264 case 0x78: /* all sounds off */
1265 /* FIXME: I don't know if I have to take care of the channel
1266 * for this control ?
1268 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1269 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1270 voice[i].status = sVS_UNUSED;
1271 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1275 case 0x7B: /* all notes off */
1276 /* FIXME: I don't know if I have to take care of the channel
1277 * for this control ?
1279 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1280 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1281 voice[i].status = sVS_UNUSED;
1282 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1287 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1292 case MIDI_PGM_CHANGE:
1293 channel[chn].program = d1;
1295 case MIDI_CHN_PRESSURE:
1296 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1297 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1298 SEQ_KEY_PRESSURE(wDevID, i, voice[i].note, d1);
1302 case MIDI_PITCH_BEND:
1303 channel[chn].bender = (d2 << 7) + d1;
1304 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1305 if (voice[i].channel == chn) {
1306 SEQ_BENDER(wDevID, i, channel[chn].bender);
1310 case MIDI_SYSTEM_PREFIX:
1311 switch (evt & 0x0F) {
1312 case 0x0F: /* Reset */
1316 WARN("Unsupported (yet) system event %02x\n", evt & 0x0F);
1320 WARN("Internal error, shouldn't happen (event=%08x)\n", evt & 0xF0);
1321 return MMSYSERR_NOTENABLED;
1327 int dev = wDevID - MODM_NUMFMSYNTHDEVS;
1329 WARN("Internal error on devID (%u) !\n", wDevID);
1330 return MIDIERR_NODEVICE;
1333 switch (evt & 0xF0) {
1336 case MIDI_KEY_PRESSURE:
1337 case MIDI_CTL_CHANGE:
1338 case MIDI_PITCH_BEND:
1339 SEQ_MIDIOUT(dev, evt);
1340 SEQ_MIDIOUT(dev, d1);
1341 SEQ_MIDIOUT(dev, d2);
1343 case MIDI_PGM_CHANGE:
1344 case MIDI_CHN_PRESSURE:
1345 SEQ_MIDIOUT(dev, evt);
1346 SEQ_MIDIOUT(dev, d1);
1348 case MIDI_SYSTEM_PREFIX:
1349 switch (evt & 0x0F) {
1350 case 0x00: /* System Exclusive, don't do it on modData,
1351 * should require modLongData*/
1352 case 0x01: /* Undefined */
1353 case 0x04: /* Undefined. */
1354 case 0x05: /* Undefined. */
1355 case 0x07: /* End of Exclusive. */
1356 case 0x09: /* Undefined. */
1357 case 0x0D: /* Undefined. */
1359 case 0x06: /* Tune Request */
1360 case 0x08: /* Timing Clock. */
1361 case 0x0A: /* Start. */
1362 case 0x0B: /* Continue */
1363 case 0x0C: /* Stop */
1364 case 0x0E: /* Active Sensing. */
1365 SEQ_MIDIOUT(dev, evt);
1367 case 0x0F: /* Reset */
1368 /* SEQ_MIDIOUT(dev, evt);
1369 this other way may be better */
1370 SEQ_MIDIOUT(dev, MIDI_SYSTEM_PREFIX);
1371 SEQ_MIDIOUT(dev, 0x7e);
1372 SEQ_MIDIOUT(dev, 0x7f);
1373 SEQ_MIDIOUT(dev, 0x09);
1374 SEQ_MIDIOUT(dev, 0x01);
1375 SEQ_MIDIOUT(dev, 0xf7);
1377 case 0x03: /* Song Select. */
1378 SEQ_MIDIOUT(dev, evt);
1379 SEQ_MIDIOUT(dev, d1);
1380 case 0x02: /* Song Position Pointer. */
1381 SEQ_MIDIOUT(dev, evt);
1382 SEQ_MIDIOUT(dev, d1);
1383 SEQ_MIDIOUT(dev, d2);
1390 WARN("Technology not supported (yet) %d !\n",
1391 midiOutDevices[wDevID]->wTechnology);
1392 return MMSYSERR_NOTENABLED;
1397 return MMSYSERR_NOERROR;
1400 /**************************************************************************
1401 * modLongData [internal]
1403 static DWORD modLongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1408 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1410 if (midiSeqFD == -1) {
1411 WARN("can't play !\n");
1412 return MIDIERR_NODEVICE;
1415 lpData = lpMidiHdr->lpData;
1418 return MIDIERR_UNPREPARED;
1419 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1420 return MIDIERR_UNPREPARED;
1421 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1422 return MIDIERR_STILLPLAYING;
1423 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1424 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1426 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1427 * data, or can it also contain raw MIDI data, to be split up and sent to
1429 * If the latest is true, then the following WARNing will fire up
1431 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
1432 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1435 TRACE("dwBufferLength=%lu !\n", lpMidiHdr->dwBufferLength);
1436 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1437 lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
1438 lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
1440 switch (midiOutDevices[wDevID]->wTechnology) {
1442 /* FIXME: I don't think there is much to do here */
1445 if (lpData[0] != 0xF0) {
1446 /* Send end of System Exclusive */
1447 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF0);
1448 WARN("Adding missing 0xF0 marker at the beginning of "
1449 "system exclusive byte stream\n");
1451 for (count = 0; count < lpMidiHdr->dwBytesRecorded; count++) {
1452 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, lpData[count]);
1454 if (lpData[count - 1] != 0xF7) {
1455 /* Send end of System Exclusive */
1456 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF7);
1457 WARN("Adding missing 0xF7 marker at the end of "
1458 "system exclusive byte stream\n");
1463 WARN("Technology not supported (yet) %d !\n",
1464 midiOutDevices[wDevID]->wTechnology);
1465 return MMSYSERR_NOTENABLED;
1468 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1469 lpMidiHdr->dwFlags |= MHDR_DONE;
1470 if (MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD)lpMidiHdr, 0L) != MMSYSERR_NOERROR) {
1471 WARN("can't notify client !\n");
1472 return MMSYSERR_INVALPARAM;
1474 return MMSYSERR_NOERROR;
1477 /**************************************************************************
1478 * modPrepare [internal]
1480 static DWORD modPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1482 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1484 if (midiSeqFD == -1) {
1485 WARN("can't prepare !\n");
1486 return MMSYSERR_NOTENABLED;
1489 /* MS doc says taht dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
1490 * asks to prepare MIDIHDR which dwFlags != 0.
1491 * So at least check for the inqueue flag
1493 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
1494 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
1495 lpMidiHdr->dwBufferLength >= 0x10000ul) {
1496 WARN("%p %p %08lx %d/%ld\n", lpMidiHdr, lpMidiHdr->lpData,
1497 lpMidiHdr->dwFlags, sizeof(MIDIHDR), dwSize);
1498 return MMSYSERR_INVALPARAM;
1501 lpMidiHdr->lpNext = 0;
1502 lpMidiHdr->dwFlags |= MHDR_PREPARED;
1503 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1504 return MMSYSERR_NOERROR;
1507 /**************************************************************************
1508 * modUnprepare [internal]
1510 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1512 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1514 if (midiSeqFD == -1) {
1515 WARN("can't unprepare !\n");
1516 return MMSYSERR_NOTENABLED;
1519 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0)
1520 return MMSYSERR_INVALPARAM;
1521 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1522 return MIDIERR_STILLPLAYING;
1523 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
1524 return MMSYSERR_NOERROR;
1527 /**************************************************************************
1528 * modReset [internal]
1530 static DWORD modReset(WORD wDevID)
1534 TRACE("(%04X);\n", wDevID);
1536 /* stop all notes */
1537 /* FIXME: check if 0x78B0 is channel dependant or not. I coded it so that
1538 * it's channel dependent...
1540 for (chn = 0; chn < 16; chn++) {
1541 /* turn off every note */
1542 modData(wDevID, 0x7800 | MIDI_CTL_CHANGE | chn);
1543 /* remove sustain on all channels */
1544 modData(wDevID, (CTL_SUSTAIN << 8) | MIDI_CTL_CHANGE | chn);
1546 /* FIXME: the LongData buffers must also be returned to the app */
1547 return MMSYSERR_NOERROR;
1550 #endif /* HAVE_OSS_MIDI */
1552 /*======================================================================*
1553 * MIDI entry points *
1554 *======================================================================*/
1556 /**************************************************************************
1557 * midMessage (WINEOSS.4)
1559 DWORD WINAPI OSS_midMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1560 DWORD dwParam1, DWORD dwParam2)
1562 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1563 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1565 #ifdef HAVE_OSS_MIDI
1569 /* FIXME: Pretend this is supported */
1572 return midOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1574 return midClose(wDevID);
1575 case MIDM_ADDBUFFER:
1576 return midAddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1578 return midPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1579 case MIDM_UNPREPARE:
1580 return midUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1581 case MIDM_GETDEVCAPS:
1582 return midGetDevCaps(wDevID, (LPMIDIINCAPSA)dwParam1,dwParam2);
1583 case MIDM_GETNUMDEVS:
1584 return MIDM_NUMDEVS;
1586 return midReset(wDevID);
1588 return midStart(wDevID);
1590 return midStop(wDevID);
1593 TRACE("Unsupported message\n");
1595 return MMSYSERR_NOTSUPPORTED;
1598 /**************************************************************************
1599 * modMessage (WINEOSS.5)
1601 DWORD WINAPI OSS_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1602 DWORD dwParam1, DWORD dwParam2)
1604 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1605 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1608 #ifdef HAVE_OSS_MIDI
1613 /* FIXME: Pretend this is supported */
1616 return modOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1618 return modClose(wDevID);
1620 return modData(wDevID, dwParam1);
1622 return modLongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1624 return modPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1625 case MODM_UNPREPARE:
1626 return modUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1627 case MODM_GETDEVCAPS:
1628 return modGetDevCaps(wDevID, (LPMIDIOUTCAPSA)dwParam1, dwParam2);
1629 case MODM_GETNUMDEVS:
1630 return MODM_NUMDEVS;
1631 case MODM_GETVOLUME:
1633 case MODM_SETVOLUME:
1636 return modReset(wDevID);
1639 TRACE("Unsupported message\n");
1641 return MMSYSERR_NOTSUPPORTED;
1644 /*-----------------------------------------------------------------------*/