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 split 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 * + use better instrument definition for OPL/2 (midiPatch.c) or
31 * use existing instrument definition (from playmidi or kmid)
32 * with a .winerc option
33 * + have a look at OPL/3 ?
34 * + implement asynchronous playback of MidiHdr
35 * + implement STREAM'ed MidiHdr (question: how shall we share the
36 * code between the midiStream functions in MMSYSTEM/WINMM and
37 * the code for the low level driver)
38 * + use a more accurate read mechanism than the one of snooping on
39 * timers (like select on fd)
43 #include "wine/port.h"
54 #ifdef HAVE_SYS_IOCTL_H
55 # include <sys/ioctl.h>
60 #ifdef HAVE_SYS_POLL_H
71 #include "wine/unicode.h"
72 #include "wine/debug.h"
74 WINE_DEFAULT_DEBUG_CHANNEL(midi);
76 #ifdef SNDCTL_SEQ_NRMIDIS
83 int state; /* -1 disabled, 0 is no recording started, 1 in recording, bit 2 set if in sys exclusive recording */
85 MIDIOPENDESC midiDesc;
89 unsigned char incoming[3];
90 unsigned char incPrev;
99 MIDIOPENDESC midiDesc;
101 LPMIDIHDR lpQueueHdr;
103 void* lpExtra; /* according to port type (MIDI, FM...), extra data when needed */
107 static WINE_MIDIIN MidiInDev [MAX_MIDIINDRV ];
108 static WINE_MIDIOUT MidiOutDev[MAX_MIDIOUTDRV];
110 /* this is the total number of MIDI out devices found (synth and port) */
111 static int MODM_NumDevs = 0;
112 /* this is the number of FM synthesizers (index from 0 to NUMFMSYNTHDEVS - 1) */
113 static int MODM_NumFMSynthDevs = 0;
114 /* the Midi ports have index from NUMFMSYNTHDEVS to NumDevs - 1 */
116 /* this is the total number of MIDI out devices found */
117 static int MIDM_NumDevs = 0;
119 static int midiSeqFD = -1;
120 static int numOpenMidiSeq = 0;
121 static int numStartedMidiIn = 0;
123 static CRITICAL_SECTION crit_sect; /* protects all MidiIn buffers queues */
124 static CRITICAL_SECTION_DEBUG critsect_debug =
127 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
128 0, 0, { (DWORD_PTR)(__FILE__ ": crit_sect") }
130 static CRITICAL_SECTION crit_sect = { &critsect_debug, -1, 0, 0, 0, 0 };
132 static int end_thread;
133 static HANDLE hThread;
135 /*======================================================================*
136 * Low level MIDI implementation *
137 *======================================================================*/
139 static int midiOpenSeq(void);
140 static int midiCloseSeq(void);
142 /**************************************************************************
143 * MIDI_unixToWindowsDeviceType [internal]
145 * return the Windows equivalent to a Unix Device Type
148 static int MIDI_UnixToWindowsDeviceType(int type)
150 /* MOD_MIDIPORT output port
151 * MOD_SYNTH generic internal synth
152 * MOD_SQSYNTH square wave internal synth
153 * MOD_FMSYNTH FM internal synth
154 * MOD_MAPPER MIDI mapper
155 * MOD_WAVETABLE hardware watetable internal synth
156 * MOD_SWSYNTH software internal synth
159 /* FIXME Is this really the correct equivalence from UNIX to
160 Windows Sound type */
163 case SYNTH_TYPE_FM: return MOD_FMSYNTH;
164 case SYNTH_TYPE_SAMPLE: return MOD_SYNTH;
165 case SYNTH_TYPE_MIDI: return MOD_MIDIPORT;
167 ERR("Cannot determine the type of this midi device. "
168 "Assuming FM Synth\n");
173 /**************************************************************************
174 * OSS_MidiInit [internal]
176 * Initializes the MIDI devices information variables
178 LRESULT OSS_MidiInit(void)
180 int i, status, numsynthdevs = 255, nummididevs = 255;
181 struct synth_info sinfo;
182 struct midi_info minfo;
183 static BOOL bInitDone = FALSE;
188 TRACE("Initializing the MIDI variables.\n");
191 /* try to open device */
192 if (midiOpenSeq() == -1) {
196 /* find how many Synth devices are there in the system */
197 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRSYNTHS, &numsynthdevs);
200 ERR("ioctl for nr synth failed.\n");
205 if (numsynthdevs > MAX_MIDIOUTDRV) {
206 ERR("MAX_MIDIOUTDRV (%d) was enough for the number of devices (%d). "
207 "Some FM devices will not be available.\n",MAX_MIDIOUTDRV,numsynthdevs);
208 numsynthdevs = MAX_MIDIOUTDRV;
211 for (i = 0; i < numsynthdevs; i++) {
212 /* Manufac ID. We do not have access to this with soundcard.h
213 * Does not seem to be a problem, because in mmsystem.h only
214 * Microsoft's ID is listed.
216 MidiOutDev[i].caps.wMid = 0x00FF;
217 MidiOutDev[i].caps.wPid = 0x0001; /* FIXME Product ID */
218 /* Product Version. We simply say "1" */
219 MidiOutDev[i].caps.vDriverVersion = 0x001;
220 MidiOutDev[i].caps.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 MidiOutDev[i].caps.dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
230 status = ioctl(midiSeqFD, SNDCTL_SYNTH_INFO, &sinfo);
232 static const WCHAR fmt[] = {'W','i','n','e',' ','O','S','S',' ','M','i','d','i',' ','O','u','t',' ','(','#','%','d',')',' ','d','i','s','a','b','l','e','d',0};
233 ERR("ioctl for synth info failed on %d, disabling it.\n", i);
235 wsprintfW(MidiOutDev[i].caps.szPname, fmt, i);
237 MidiOutDev[i].caps.wTechnology = MOD_MIDIPORT;
238 MidiOutDev[i].caps.wVoices = 16;
239 MidiOutDev[i].caps.wNotes = 16;
240 MidiOutDev[i].bEnabled = FALSE;
242 MultiByteToWideChar( CP_ACP, 0, sinfo.name, -1,
243 MidiOutDev[i].caps.szPname,
244 sizeof(MidiOutDev[i].caps.szPname)/sizeof(WCHAR) );
246 MidiOutDev[i].caps.wTechnology = MIDI_UnixToWindowsDeviceType(sinfo.synth_type);
247 MidiOutDev[i].caps.wVoices = sinfo.nr_voices;
249 /* FIXME Is it possible to know the maximum
250 * number of simultaneous notes of a soundcard ?
251 * I believe we don't have this information, but
252 * it's probably equal or more than wVoices
254 MidiOutDev[i].caps.wNotes = sinfo.nr_voices;
255 MidiOutDev[i].bEnabled = TRUE;
258 /* We also have the information sinfo.synth_subtype, not used here
261 if (sinfo.capabilities & SYNTH_CAP_INPUT) {
262 FIXME("Synthesizer supports MIDI in. Not yet supported.\n");
265 TRACE("SynthOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%d\n"
266 "\tOSS info: synth subtype=%d capa=%lx\n",
267 i, wine_dbgstr_w(MidiOutDev[i].caps.szPname),
268 MidiOutDev[i].caps.wTechnology,
269 MidiOutDev[i].caps.wVoices, MidiOutDev[i].caps.wNotes,
270 MidiOutDev[i].caps.wChannelMask, MidiOutDev[i].caps.dwSupport,
271 sinfo.synth_subtype, (long)sinfo.capabilities);
274 /* find how many MIDI devices are there in the system */
275 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRMIDIS, &nummididevs);
277 ERR("ioctl on nr midi failed.\n");
282 /* FIXME: the two restrictions below could be loosened in some cases */
283 if (numsynthdevs + nummididevs > MAX_MIDIOUTDRV) {
284 ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
285 "Some MIDI devices will not be available.\n");
286 nummididevs = MAX_MIDIOUTDRV - numsynthdevs;
289 if (nummididevs > MAX_MIDIINDRV) {
290 ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
291 "Some MIDI devices will not be available.\n",MAX_MIDIINDRV,nummididevs);
292 nummididevs = MAX_MIDIINDRV;
295 for (i = 0; i < nummididevs; i++) {
297 status = ioctl(midiSeqFD, SNDCTL_MIDI_INFO, &minfo);
298 if (status == -1) WARN("ioctl on midi info for device %d failed.\n", i);
300 /* This whole part is somewhat obscure to me. I'll keep trying to dig
301 info about it. If you happen to know, please tell us. The very
302 descriptive minfo.dev_type was not used here.
304 /* Manufacturer ID. We do not have access to this with soundcard.h
305 Does not seem to be a problem, because in mmsystem.h only
306 Microsoft's ID is listed */
307 MidiOutDev[numsynthdevs + i].caps.wMid = 0x00FF;
308 MidiOutDev[numsynthdevs + i].caps.wPid = 0x0001; /* FIXME Product ID */
309 /* Product Version. We simply say "1" */
310 MidiOutDev[numsynthdevs + i].caps.vDriverVersion = 0x001;
312 static const WCHAR fmt[] = {'W','i','n','e',' ','O','S','S',' ','M','i','d','i',' ','O','u','t',' ','(','#','%','d',')',' ','d','i','s','a','b','l','e','d',0};
313 wsprintfW(MidiOutDev[numsynthdevs + i].caps.szPname, fmt, numsynthdevs + i);
314 MidiOutDev[numsynthdevs + i].bEnabled = FALSE;
316 MultiByteToWideChar(CP_ACP, 0, minfo.name, -1,
317 MidiOutDev[numsynthdevs + i].caps.szPname,
318 sizeof(MidiOutDev[numsynthdevs + i].caps.szPname) / sizeof(WCHAR));
319 MidiOutDev[numsynthdevs + i].bEnabled = TRUE;
321 MidiOutDev[numsynthdevs + i].caps.wTechnology = MOD_MIDIPORT; /* FIXME Is this right? */
322 /* Does it make any difference? */
323 MidiOutDev[numsynthdevs + i].caps.wVoices = 16;
324 /* Does it make any difference? */
325 MidiOutDev[numsynthdevs + i].caps.wNotes = 16;
326 MidiOutDev[numsynthdevs + i].caps.wChannelMask= 0xFFFF;
328 /* FIXME Does it make any difference? */
329 MidiOutDev[numsynthdevs + i].caps.dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
331 /* This whole part is somewhat obscure to me. I'll keep trying to dig
332 info about it. If you happen to know, please tell us. The very
333 descriptive minfo.dev_type was not used here.
335 /* Manufac ID. We do not have access to this with soundcard.h
336 Does not seem to be a problem, because in mmsystem.h only
337 Microsoft's ID is listed */
338 MidiInDev[i].caps.wMid = 0x00FF;
339 MidiInDev[i].caps.wPid = 0x0001; /* FIXME Product ID */
340 /* Product Version. We simply say "1" */
341 MidiInDev[i].caps.vDriverVersion = 0x001;
343 static const WCHAR fmt[] = {'W','i','n','e',' ','O','S','S',' ','M','i','d','i',' ','I','n',' ','(','#','%','d',')',' ','d','i','s','a','b','l','e','d',0};
344 wsprintfW(MidiInDev[i].caps.szPname, fmt, numsynthdevs + i);
345 MidiInDev[i].state = -1;
347 MultiByteToWideChar(CP_ACP, 0, minfo.name, -1,
348 MidiInDev[i].caps.szPname,
349 sizeof(MidiInDev[i].caps.szPname) / sizeof(WCHAR));
350 MidiInDev[i].state = 0;
352 /* FIXME : could we get better information than that ? */
353 MidiInDev[i].caps.dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
355 TRACE("MidiOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%d\n"
356 "MidiIn [%d]\tname='%s' support=%d\n"
357 "\tOSS info: midi dev-type=%d, capa=%lx\n",
358 i, wine_dbgstr_w(MidiOutDev[numsynthdevs + i].caps.szPname),
359 MidiOutDev[numsynthdevs + i].caps.wTechnology,
360 MidiOutDev[numsynthdevs + i].caps.wVoices, MidiOutDev[numsynthdevs + i].caps.wNotes,
361 MidiOutDev[numsynthdevs + i].caps.wChannelMask, MidiOutDev[numsynthdevs + i].caps.dwSupport,
362 i, wine_dbgstr_w(MidiInDev[i].caps.szPname), MidiInDev[i].caps.dwSupport,
363 minfo.dev_type, (long)minfo.capabilities);
367 /* windows does not seem to differentiate Synth from MIDI devices */
368 MODM_NumFMSynthDevs = numsynthdevs;
369 MODM_NumDevs = numsynthdevs + nummididevs;
371 MIDM_NumDevs = nummididevs;
373 /* close file and exit */
379 /**************************************************************************
380 * OSS_MidiExit [internal]
382 * Release the MIDI devices information variables
384 LRESULT OSS_MidiExit(void)
388 ZeroMemory(MidiInDev, sizeof(MidiInDev));
389 ZeroMemory(MidiOutDev, sizeof(MidiOutDev));
392 MODM_NumFMSynthDevs = 0;
398 /**************************************************************************
399 * MIDI_NotifyClient [internal]
401 static DWORD MIDI_NotifyClient(UINT wDevID, WORD wMsg,
402 DWORD dwParam1, DWORD dwParam2)
409 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04X dwParam2 = %04X\n",
410 wDevID, wMsg, dwParam1, dwParam2);
417 if (wDevID > MODM_NumDevs)
418 return MMSYSERR_BADDEVICEID;
420 dwCallBack = MidiOutDev[wDevID].midiDesc.dwCallback;
421 uFlags = MidiOutDev[wDevID].wFlags;
422 hDev = MidiOutDev[wDevID].midiDesc.hMidi;
423 dwInstance = MidiOutDev[wDevID].midiDesc.dwInstance;
433 if (wDevID > MIDM_NumDevs)
434 return MMSYSERR_BADDEVICEID;
436 dwCallBack = MidiInDev[wDevID].midiDesc.dwCallback;
437 uFlags = MidiInDev[wDevID].wFlags;
438 hDev = MidiInDev[wDevID].midiDesc.hMidi;
439 dwInstance = MidiInDev[wDevID].midiDesc.dwInstance;
442 WARN("Unsupported MSW-MIDI message %u\n", wMsg);
443 return MMSYSERR_ERROR;
446 return DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2) ?
450 static int midi_warn = 1;
451 /**************************************************************************
452 * midiOpenSeq [internal]
454 static int midiOpenSeq(void)
456 if (numOpenMidiSeq == 0) {
458 device=getenv("MIDIDEV");
459 if (!device) device="/dev/sequencer";
460 midiSeqFD = open(device, O_RDWR, 0);
461 if (midiSeqFD == -1) {
464 WARN("Can't open MIDI device '%s' ! (%s). If your "
465 "program needs this (probably not): %s\n",
466 device, strerror(errno),
468 "create it ! (\"man MAKEDEV\" ?)" :
470 "load MIDI sequencer kernel driver !" :
472 "grant access ! (\"man chmod\")" : ""
479 if (fcntl(midiSeqFD, F_SETFL, O_NONBLOCK) < 0) {
480 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno, strerror(errno));
486 fcntl(midiSeqFD, F_SETFD, 1); /* set close on exec flag */
487 ioctl(midiSeqFD, SNDCTL_SEQ_RESET);
493 /**************************************************************************
494 * midiCloseSeq [internal]
496 static int midiCloseSeq(void)
498 if (--numOpenMidiSeq == 0) {
505 /* FIXME: this is a bad idea, it's even not static... */
508 /* FIXME: this is not reentrant, not static - because of global variable
511 /**************************************************************************
512 * seqbuf_dump [internal]
514 * Used by SEQ_DUMPBUF to flush the buffer.
517 void seqbuf_dump(void)
520 if (write(midiSeqFD, _seqbuf, _seqbufptr) == -1) {
521 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
522 midiSeqFD, errno, strerror(errno));
525 * in any case buffer is lost so that if many errors occur the buffer
532 /**************************************************************************
533 * midReceiveChar [internal]
535 static void midReceiveChar(WORD wDevID, unsigned char value, DWORD dwTime)
539 TRACE("Adding %02xh to %d[%d]\n", value, wDevID, MidiInDev[wDevID].incLen);
541 if (wDevID >= MIDM_NumDevs) {
545 if (MidiInDev[wDevID].state <= 0) {
546 TRACE("disabled or input not started, thrown away\n");
550 if (MidiInDev[wDevID].state & 2) { /* system exclusive */
554 EnterCriticalSection(&crit_sect);
555 if ((lpMidiHdr = MidiInDev[wDevID].lpQueueHdr) != NULL) {
556 LPBYTE lpData = (LPBYTE) lpMidiHdr->lpData;
558 lpData[lpMidiHdr->dwBytesRecorded++] = value;
559 if (lpMidiHdr->dwBytesRecorded == lpMidiHdr->dwBufferLength) {
563 if (value == 0xF7) { /* then end */
564 MidiInDev[wDevID].state &= ~2;
567 if (sbfb && lpMidiHdr != NULL) {
568 lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
569 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
570 lpMidiHdr->dwFlags |= MHDR_DONE;
571 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr->lpNext;
572 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD)lpMidiHdr, dwTime) != MMSYSERR_NOERROR) {
573 WARN("Couldn't notify client\n");
576 LeaveCriticalSection(&crit_sect);
580 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
581 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
583 if (!IS_CMD(value) && MidiInDev[wDevID].incLen == 0) { /* try to reuse old cmd */
584 if (IS_CMD(MidiInDev[wDevID].incPrev) && !IS_SYS_CMD(MidiInDev[wDevID].incPrev)) {
585 MidiInDev[wDevID].incoming[0] = MidiInDev[wDevID].incPrev;
586 MidiInDev[wDevID].incLen = 1;
587 TRACE("Reusing old command %02xh\n", MidiInDev[wDevID].incPrev);
589 FIXME("error for midi-in, should generate MIM_ERROR notification:"
590 " prev=%02Xh, incLen=%02Xh\n",
591 MidiInDev[wDevID].incPrev, MidiInDev[wDevID].incLen);
595 MidiInDev[wDevID].incoming[(int)(MidiInDev[wDevID].incLen++)] = value;
596 if (MidiInDev[wDevID].incLen == 1 && !IS_SYS_CMD(MidiInDev[wDevID].incoming[0])) {
597 /* store new cmd, just in case */
598 MidiInDev[wDevID].incPrev = MidiInDev[wDevID].incoming[0];
604 switch (MidiInDev[wDevID].incoming[0] & 0xF0) {
607 case MIDI_KEY_PRESSURE:
608 case MIDI_CTL_CHANGE:
609 case MIDI_PITCH_BEND:
610 if (MidiInDev[wDevID].incLen == 3) {
611 toSend = (MidiInDev[wDevID].incoming[2] << 16) |
612 (MidiInDev[wDevID].incoming[1] << 8) |
613 (MidiInDev[wDevID].incoming[0] << 0);
616 case MIDI_PGM_CHANGE:
617 case MIDI_CHN_PRESSURE:
618 if (MidiInDev[wDevID].incLen == 2) {
619 toSend = (MidiInDev[wDevID].incoming[1] << 8) |
620 (MidiInDev[wDevID].incoming[0] << 0);
623 case MIDI_SYSTEM_PREFIX:
624 if (MidiInDev[wDevID].incoming[0] == 0xF0) {
625 MidiInDev[wDevID].state |= 2;
626 MidiInDev[wDevID].incLen = 0;
628 if (MidiInDev[wDevID].incLen == 1) {
629 toSend = (MidiInDev[wDevID].incoming[0] << 0);
634 WARN("This shouldn't happen (%02X)\n", MidiInDev[wDevID].incoming[0]);
637 TRACE("Sending event %08x\n", toSend);
638 MidiInDev[wDevID].incLen = 0;
639 dwTime -= MidiInDev[wDevID].startTime;
640 if (MIDI_NotifyClient(wDevID, MIM_DATA, toSend, dwTime) != MMSYSERR_NOERROR) {
641 WARN("Couldn't notify client\n");
646 static DWORD WINAPI midRecThread(LPVOID arg)
648 unsigned char buffer[256];
653 TRACE("Thread startup\n");
659 TRACE("Thread loop\n");
661 /* Check if an event is present */
662 if (poll(&pfd, 1, 250) <= 0)
665 len = read(midiSeqFD, buffer, sizeof(buffer));
666 TRACE("Received %d bytes\n", len);
668 if (len < 0) continue;
669 if ((len % 4) != 0) {
670 WARN("Bad length %d, errno %d (%s)\n", len, errno, strerror(errno));
674 dwTime = GetTickCount();
676 for (idx = 0; idx < len; ) {
677 if (buffer[idx] & 0x80) {
679 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
680 buffer[idx + 0], buffer[idx + 1],
681 buffer[idx + 2], buffer[idx + 3],
682 buffer[idx + 4], buffer[idx + 5],
683 buffer[idx + 6], buffer[idx + 7]);
686 switch (buffer[idx + 0]) {
691 midReceiveChar(buffer[idx + 2], buffer[idx + 1], dwTime);
694 TRACE("Unsupported event %d\n", buffer[idx + 0]);
704 /**************************************************************************
705 * midGetDevCaps [internal]
707 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPSW lpCaps, DWORD dwSize)
709 TRACE("(%04X, %p, %08X);\n", wDevID, lpCaps, dwSize);
711 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
712 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
714 memcpy(lpCaps, &MidiInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
716 return MMSYSERR_NOERROR;
719 /**************************************************************************
722 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
724 TRACE("(%04X, %p, %08X);\n", wDevID, lpDesc, dwFlags);
726 if (lpDesc == NULL) {
727 WARN("Invalid Parameter !\n");
728 return MMSYSERR_INVALPARAM;
732 * how to check that content of lpDesc is correct ?
734 if (wDevID >= MIDM_NumDevs) {
735 WARN("wDevID too large (%u) !\n", wDevID);
736 return MMSYSERR_BADDEVICEID;
738 if (MidiInDev[wDevID].state == -1) {
739 WARN("device disabled\n");
740 return MIDIERR_NODEVICE;
742 if (MidiInDev[wDevID].midiDesc.hMidi != 0) {
743 WARN("device already open !\n");
744 return MMSYSERR_ALLOCATED;
746 if ((dwFlags & MIDI_IO_STATUS) != 0) {
747 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
748 dwFlags &= ~MIDI_IO_STATUS;
750 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
751 FIXME("Bad dwFlags\n");
752 return MMSYSERR_INVALFLAG;
755 if (midiOpenSeq() < 0) {
756 return MMSYSERR_ERROR;
759 if (numStartedMidiIn++ == 0) {
761 hThread = CreateThread(NULL, 0, midRecThread, NULL, 0, NULL);
763 numStartedMidiIn = 0;
764 WARN("Couldn't create thread for midi-in\n");
766 return MMSYSERR_ERROR;
768 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
769 TRACE("Created thread for midi-in\n");
772 MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
774 MidiInDev[wDevID].lpQueueHdr = NULL;
775 MidiInDev[wDevID].dwTotalPlayed = 0;
776 MidiInDev[wDevID].bufsize = 0x3FFF;
777 MidiInDev[wDevID].midiDesc = *lpDesc;
778 MidiInDev[wDevID].state = 0;
779 MidiInDev[wDevID].incLen = 0;
780 MidiInDev[wDevID].startTime = 0;
782 if (MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
783 WARN("can't notify client !\n");
784 return MMSYSERR_INVALPARAM;
786 return MMSYSERR_NOERROR;
789 /**************************************************************************
790 * midClose [internal]
792 static DWORD midClose(WORD wDevID)
794 int ret = MMSYSERR_NOERROR;
796 TRACE("(%04X);\n", wDevID);
798 if (wDevID >= MIDM_NumDevs) {
799 WARN("wDevID too big (%u) !\n", wDevID);
800 return MMSYSERR_BADDEVICEID;
802 if (MidiInDev[wDevID].midiDesc.hMidi == 0) {
803 WARN("device not opened !\n");
804 return MMSYSERR_ERROR;
806 if (MidiInDev[wDevID].lpQueueHdr != 0) {
807 return MIDIERR_STILLPLAYING;
810 if (midiSeqFD == -1) {
812 return MMSYSERR_ERROR;
814 if (--numStartedMidiIn == 0) {
815 TRACE("Stopping thread for midi-in\n");
817 if (WaitForSingleObject(hThread, 5000) != WAIT_OBJECT_0) {
818 WARN("Thread end not signaled, force termination\n");
819 TerminateThread(hThread, 0);
821 TRACE("Stopped thread for midi-in\n");
825 MidiInDev[wDevID].bufsize = 0;
826 if (MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
827 WARN("can't notify client !\n");
828 ret = MMSYSERR_INVALPARAM;
830 MidiInDev[wDevID].midiDesc.hMidi = 0;
834 /**************************************************************************
835 * midAddBuffer [internal]
837 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
839 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
841 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
842 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
844 if (lpMidiHdr == NULL) return MMSYSERR_INVALPARAM;
845 if (sizeof(MIDIHDR) > dwSize) return MMSYSERR_INVALPARAM;
846 if (lpMidiHdr->dwBufferLength == 0) return MMSYSERR_INVALPARAM;
847 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
848 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
850 EnterCriticalSection(&crit_sect);
851 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
852 if (MidiInDev[wDevID].lpQueueHdr == 0) {
853 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr;
857 for (ptr = MidiInDev[wDevID].lpQueueHdr;
860 ptr->lpNext = lpMidiHdr;
862 LeaveCriticalSection(&crit_sect);
864 return MMSYSERR_NOERROR;
867 /**************************************************************************
868 * midPrepare [internal]
870 static DWORD midPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
872 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
874 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
875 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
876 lpMidiHdr->dwBufferLength >= 0x10000ul)
877 return MMSYSERR_INVALPARAM;
879 lpMidiHdr->lpNext = 0;
880 lpMidiHdr->dwFlags |= MHDR_PREPARED;
881 lpMidiHdr->dwBytesRecorded = 0;
883 return MMSYSERR_NOERROR;
886 /**************************************************************************
887 * midUnprepare [internal]
889 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
891 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
893 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
894 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
896 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
897 lpMidiHdr->lpData == 0 || lpMidiHdr->dwBufferLength >= 0x10000ul)
898 return MMSYSERR_INVALPARAM;
900 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
901 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
903 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
905 return MMSYSERR_NOERROR;
908 /**************************************************************************
909 * midReset [internal]
911 static DWORD midReset(WORD wDevID)
913 DWORD dwTime = GetTickCount();
915 TRACE("(%04X);\n", wDevID);
917 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
918 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
920 EnterCriticalSection(&crit_sect);
921 while (MidiInDev[wDevID].lpQueueHdr) {
922 MidiInDev[wDevID].lpQueueHdr->dwFlags &= ~MHDR_INQUEUE;
923 MidiInDev[wDevID].lpQueueHdr->dwFlags |= MHDR_DONE;
924 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
925 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA,
926 (DWORD)MidiInDev[wDevID].lpQueueHdr, dwTime) != MMSYSERR_NOERROR) {
927 WARN("Couldn't notify client\n");
929 MidiInDev[wDevID].lpQueueHdr = MidiInDev[wDevID].lpQueueHdr->lpNext;
931 LeaveCriticalSection(&crit_sect);
933 return MMSYSERR_NOERROR;
937 /**************************************************************************
938 * midStart [internal]
940 static DWORD midStart(WORD wDevID)
942 TRACE("(%04X);\n", wDevID);
944 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
945 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
947 MidiInDev[wDevID].state = 1;
948 MidiInDev[wDevID].startTime = GetTickCount();
949 return MMSYSERR_NOERROR;
952 /**************************************************************************
955 static DWORD midStop(WORD wDevID)
957 TRACE("(%04X);\n", wDevID);
959 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
960 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
962 MidiInDev[wDevID].state = 0;
963 return MMSYSERR_NOERROR;
966 /*-----------------------------------------------------------------------*/
968 typedef struct sVoice {
969 int note; /* 0 means not used */
971 unsigned cntMark : 30,
974 #define sVS_PLAYING 1
975 #define sVS_SUSTAINED 2
978 typedef struct sChannel {
984 int bank; /* CTL_BANK_SELECT */
985 int volume; /* CTL_MAIN_VOLUME */
986 int balance; /* CTL_BALANCE */
987 int expression; /* CTL_EXPRESSION */
988 int sustain; /* CTL_SUSTAIN */
990 unsigned char nrgPmtMSB; /* Non register Parameters */
991 unsigned char nrgPmtLSB;
992 unsigned char regPmtMSB; /* Non register Parameters */
993 unsigned char regPmtLSB;
996 typedef struct sFMextra {
999 sChannel channel[16]; /* MIDI has only 16 channels */
1000 sVoice voice[1]; /* dyn allocated according to sound card */
1001 /* do not append fields below voice[1] since the size of this structure
1002 * depends on the number of available voices on the FM synth...
1006 extern const unsigned char midiFMInstrumentPatches[16 * 128];
1007 extern const unsigned char midiFMDrumsPatches [16 * 128];
1009 /**************************************************************************
1010 * modFMLoad [internal]
1012 static int modFMLoad(int dev)
1015 struct sbi_instrument sbi;
1020 memset(sbi.operators + 16, 0, 16);
1021 for (i = 0; i < 128; i++) {
1023 memcpy(sbi.operators, midiFMInstrumentPatches + i * 16, 16);
1025 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
1026 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
1030 for (i = 0; i < 128; i++) {
1031 sbi.channel = 128 + i;
1032 memcpy(sbi.operators, midiFMDrumsPatches + i * 16, 16);
1034 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
1035 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
1042 /**************************************************************************
1043 * modFMReset [internal]
1045 static void modFMReset(WORD wDevID)
1047 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
1048 sVoice* voice = extra->voice;
1049 sChannel* channel = extra->channel;
1052 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1053 if (voice[i].status != sVS_UNUSED) {
1054 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1056 SEQ_KEY_PRESSURE(wDevID, i, 127, 0);
1057 SEQ_CONTROL(wDevID, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
1059 voice[i].channel = -1;
1060 voice[i].cntMark = 0;
1061 voice[i].status = sVS_UNUSED;
1063 for (i = 0; i < 16; i++) {
1064 channel[i].program = 0;
1065 channel[i].bender = 8192;
1066 channel[i].benderRange = 2;
1067 channel[i].bank = 0;
1068 channel[i].volume = 127;
1069 channel[i].balance = 64;
1070 channel[i].expression = 0;
1071 channel[i].sustain = 0;
1074 extra->drumSetMask = 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
1078 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
1080 /**************************************************************************
1081 * modGetDevCaps [internal]
1083 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPSW lpCaps, DWORD dwSize)
1085 TRACE("(%04X, %p, %08X);\n", wDevID, lpCaps, dwSize);
1087 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1088 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
1090 memcpy(lpCaps, &MidiOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1092 return MMSYSERR_NOERROR;
1095 /**************************************************************************
1096 * modOpen [internal]
1098 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
1100 TRACE("(%04X, %p, %08X);\n", wDevID, lpDesc, dwFlags);
1101 if (lpDesc == NULL) {
1102 WARN("Invalid Parameter !\n");
1103 return MMSYSERR_INVALPARAM;
1105 if (wDevID >= MODM_NumDevs) {
1106 TRACE("MAX_MIDIOUTDRV reached !\n");
1107 return MMSYSERR_BADDEVICEID;
1109 if (MidiOutDev[wDevID].midiDesc.hMidi != 0) {
1110 WARN("device already open !\n");
1111 return MMSYSERR_ALLOCATED;
1113 if (!MidiOutDev[wDevID].bEnabled) {
1114 WARN("device disabled !\n");
1115 return MIDIERR_NODEVICE;
1117 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
1118 WARN("bad dwFlags\n");
1119 return MMSYSERR_INVALFLAG;
1121 if (!MidiOutDev[wDevID].bEnabled) {
1122 TRACE("disabled wDevID\n");
1123 return MMSYSERR_NOTENABLED;
1126 MidiOutDev[wDevID].lpExtra = 0;
1128 switch (MidiOutDev[wDevID].caps.wTechnology) {
1133 extra = HeapAlloc(GetProcessHeap(), 0,
1134 sizeof(struct sFMextra) +
1135 sizeof(struct sVoice) * (MidiOutDev[wDevID].caps.wVoices - 1));
1138 WARN("can't alloc extra data !\n");
1139 return MMSYSERR_NOMEM;
1141 MidiOutDev[wDevID].lpExtra = extra;
1142 if (midiOpenSeq() < 0) {
1143 MidiOutDev[wDevID].lpExtra = 0;
1144 HeapFree(GetProcessHeap(), 0, extra);
1145 return MMSYSERR_ERROR;
1147 if (modFMLoad(wDevID) < 0) {
1149 MidiOutDev[wDevID].lpExtra = 0;
1150 HeapFree(GetProcessHeap(), 0, extra);
1151 return MMSYSERR_ERROR;
1158 if (midiOpenSeq() < 0) {
1159 return MMSYSERR_ALLOCATED;
1163 WARN("Technology not supported (yet) %d !\n",
1164 MidiOutDev[wDevID].caps.wTechnology);
1165 return MMSYSERR_NOTENABLED;
1168 MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1170 MidiOutDev[wDevID].lpQueueHdr = NULL;
1171 MidiOutDev[wDevID].dwTotalPlayed = 0;
1172 MidiOutDev[wDevID].bufsize = 0x3FFF;
1173 MidiOutDev[wDevID].midiDesc = *lpDesc;
1175 if (MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
1176 WARN("can't notify client !\n");
1177 return MMSYSERR_INVALPARAM;
1179 TRACE("Successful !\n");
1180 return MMSYSERR_NOERROR;
1184 /**************************************************************************
1185 * modClose [internal]
1187 static DWORD modClose(WORD wDevID)
1189 int ret = MMSYSERR_NOERROR;
1191 TRACE("(%04X);\n", wDevID);
1193 if (MidiOutDev[wDevID].midiDesc.hMidi == 0) {
1194 WARN("device not opened !\n");
1195 return MMSYSERR_ERROR;
1197 /* FIXME: should test that no pending buffer is still in the queue for
1200 if (midiSeqFD == -1) {
1201 WARN("can't close !\n");
1202 return MMSYSERR_ERROR;
1205 switch (MidiOutDev[wDevID].caps.wTechnology) {
1211 WARN("Technology not supported (yet) %d !\n",
1212 MidiOutDev[wDevID].caps.wTechnology);
1213 return MMSYSERR_NOTENABLED;
1216 HeapFree(GetProcessHeap(), 0, MidiOutDev[wDevID].lpExtra);
1217 MidiOutDev[wDevID].lpExtra = 0;
1219 MidiOutDev[wDevID].bufsize = 0;
1220 if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
1221 WARN("can't notify client !\n");
1222 ret = MMSYSERR_INVALPARAM;
1224 MidiOutDev[wDevID].midiDesc.hMidi = 0;
1228 /**************************************************************************
1229 * modData [internal]
1231 static DWORD modData(WORD wDevID, DWORD dwParam)
1233 WORD evt = LOBYTE(LOWORD(dwParam));
1234 WORD d1 = HIBYTE(LOWORD(dwParam));
1235 WORD d2 = LOBYTE(HIWORD(dwParam));
1237 TRACE("(%04X, %08X);\n", wDevID, dwParam);
1239 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1240 if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1242 if (midiSeqFD == -1) {
1243 WARN("can't play !\n");
1244 return MIDIERR_NODEVICE;
1246 switch (MidiOutDev[wDevID].caps.wTechnology) {
1249 * - chorus depth controller is not used
1252 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
1253 sVoice* voice = extra->voice;
1254 sChannel* channel = extra->channel;
1255 int chn = (evt & 0x0F);
1258 switch (evt & 0xF0) {
1260 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1261 /* don't stop sustained notes */
1262 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1263 voice[i].status = sVS_UNUSED;
1264 SEQ_STOP_NOTE(wDevID, i, d1, d2);
1269 if (d2 == 0) { /* note off if velocity == 0 */
1270 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1271 /* don't stop sustained notes */
1272 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1273 voice[i].status = sVS_UNUSED;
1274 SEQ_STOP_NOTE(wDevID, i, d1, 64);
1279 /* finding out in this order :
1281 * - if replaying the same note on the same channel
1282 * - the older voice (LRU)
1284 for (i = nv = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1285 if (voice[i].status == sVS_UNUSED ||
1286 (voice[i].note == d1 && voice[i].channel == chn)) {
1290 if (voice[i].cntMark < voice[0].cntMark) {
1295 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1296 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1297 nv, channel[chn].program,
1298 channel[chn].balance,
1299 channel[chn].volume,
1300 channel[chn].bender, d1, d2);
1302 SEQ_SET_PATCH(wDevID, nv, IS_DRUM_CHANNEL(extra, chn) ?
1303 (128 + d1) : channel[chn].program);
1304 SEQ_BENDER_RANGE(wDevID, nv, channel[chn].benderRange * 100);
1305 SEQ_BENDER(wDevID, nv, channel[chn].bender);
1306 SEQ_CONTROL(wDevID, nv, CTL_PAN, channel[chn].balance);
1307 SEQ_CONTROL(wDevID, nv, CTL_EXPRESSION, channel[chn].expression);
1309 /* FIXME: does not really seem to work on my SB card and
1310 * screws everything up... so lay it down
1312 SEQ_CONTROL(wDevID, nv, CTL_MAIN_VOLUME, channel[chn].volume);
1314 SEQ_START_NOTE(wDevID, nv, d1, d2);
1315 voice[nv].status = channel[chn].sustain ? sVS_SUSTAINED : sVS_PLAYING;
1316 voice[nv].note = d1;
1317 voice[nv].channel = chn;
1318 voice[nv].cntMark = extra->counter++;
1320 case MIDI_KEY_PRESSURE:
1321 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1322 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn && voice[i].note == d1) {
1323 SEQ_KEY_PRESSURE(wDevID, i, d1, d2);
1327 case MIDI_CTL_CHANGE:
1329 case CTL_BANK_SELECT: channel[chn].bank = d2; break;
1330 case CTL_MAIN_VOLUME: channel[chn].volume = d2; break;
1331 case CTL_PAN: channel[chn].balance = d2; break;
1332 case CTL_EXPRESSION: channel[chn].expression = d2; break;
1333 case CTL_SUSTAIN: channel[chn].sustain = d2;
1335 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1336 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1337 voice[i].status = sVS_SUSTAINED;
1341 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1342 if (voice[i].status == sVS_SUSTAINED && voice[i].channel == chn) {
1343 voice[i].status = sVS_UNUSED;
1344 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1349 case CTL_NONREG_PARM_NUM_LSB: channel[chn].nrgPmtLSB = d2; break;
1350 case CTL_NONREG_PARM_NUM_MSB: channel[chn].nrgPmtMSB = d2; break;
1351 case CTL_REGIST_PARM_NUM_LSB: channel[chn].regPmtLSB = d2; break;
1352 case CTL_REGIST_PARM_NUM_MSB: channel[chn].regPmtMSB = d2; break;
1353 case CTL_DATA_ENTRY:
1354 switch ((channel[chn].regPmtMSB << 8) | channel[chn].regPmtLSB) {
1356 if (channel[chn].benderRange != d2) {
1357 channel[chn].benderRange = d2;
1358 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1359 if (voice[i].channel == chn) {
1360 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1367 channel[chn].benderRange = 2;
1368 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1369 if (voice[i].channel == chn) {
1370 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1375 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1376 channel[chn].regPmtMSB, channel[chn].regPmtLSB,
1377 channel[chn].nrgPmtMSB, channel[chn].nrgPmtLSB,
1383 case 0x78: /* all sounds off */
1384 /* FIXME: I don't know if I have to take care of the channel
1385 * for this control ?
1387 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1388 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1389 voice[i].status = sVS_UNUSED;
1390 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1394 case 0x7B: /* all notes off */
1395 /* FIXME: I don't know if I have to take care of the channel
1396 * for this control ?
1398 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1399 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1400 voice[i].status = sVS_UNUSED;
1401 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1406 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1411 case MIDI_PGM_CHANGE:
1412 channel[chn].program = d1;
1414 case MIDI_CHN_PRESSURE:
1415 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1416 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1417 SEQ_KEY_PRESSURE(wDevID, i, voice[i].note, d1);
1421 case MIDI_PITCH_BEND:
1422 channel[chn].bender = (d2 << 7) + d1;
1423 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1424 if (voice[i].channel == chn) {
1425 SEQ_BENDER(wDevID, i, channel[chn].bender);
1429 case MIDI_SYSTEM_PREFIX:
1430 switch (evt & 0x0F) {
1431 case 0x0F: /* Reset */
1435 WARN("Unsupported (yet) system event %02x\n", evt & 0x0F);
1439 WARN("Internal error, shouldn't happen (event=%08x)\n", evt & 0xF0);
1440 return MMSYSERR_NOTENABLED;
1446 int dev = wDevID - MODM_NumFMSynthDevs;
1448 WARN("Internal error on devID (%u) !\n", wDevID);
1449 return MIDIERR_NODEVICE;
1452 switch (evt & 0xF0) {
1455 case MIDI_KEY_PRESSURE:
1456 case MIDI_CTL_CHANGE:
1457 case MIDI_PITCH_BEND:
1458 SEQ_MIDIOUT(dev, evt);
1459 SEQ_MIDIOUT(dev, d1);
1460 SEQ_MIDIOUT(dev, d2);
1462 case MIDI_PGM_CHANGE:
1463 case MIDI_CHN_PRESSURE:
1464 SEQ_MIDIOUT(dev, evt);
1465 SEQ_MIDIOUT(dev, d1);
1467 case MIDI_SYSTEM_PREFIX:
1468 switch (evt & 0x0F) {
1469 case 0x00: /* System Exclusive, don't do it on modData,
1470 * should require modLongData*/
1471 case 0x01: /* Undefined */
1472 case 0x04: /* Undefined. */
1473 case 0x05: /* Undefined. */
1474 case 0x07: /* End of Exclusive. */
1475 case 0x09: /* Undefined. */
1476 case 0x0D: /* Undefined. */
1478 case 0x06: /* Tune Request */
1479 case 0x08: /* Timing Clock. */
1480 case 0x0A: /* Start. */
1481 case 0x0B: /* Continue */
1482 case 0x0C: /* Stop */
1483 case 0x0E: /* Active Sensing. */
1484 SEQ_MIDIOUT(dev, evt);
1486 case 0x0F: /* Reset */
1487 /* SEQ_MIDIOUT(dev, evt);
1488 this other way may be better */
1489 SEQ_MIDIOUT(dev, MIDI_SYSTEM_PREFIX);
1490 SEQ_MIDIOUT(dev, 0x7e);
1491 SEQ_MIDIOUT(dev, 0x7f);
1492 SEQ_MIDIOUT(dev, 0x09);
1493 SEQ_MIDIOUT(dev, 0x01);
1494 SEQ_MIDIOUT(dev, 0xf7);
1496 case 0x03: /* Song Select. */
1497 SEQ_MIDIOUT(dev, evt);
1498 SEQ_MIDIOUT(dev, d1);
1499 case 0x02: /* Song Position Pointer. */
1500 SEQ_MIDIOUT(dev, evt);
1501 SEQ_MIDIOUT(dev, d1);
1502 SEQ_MIDIOUT(dev, d2);
1509 WARN("Technology not supported (yet) %d !\n",
1510 MidiOutDev[wDevID].caps.wTechnology);
1511 return MMSYSERR_NOTENABLED;
1516 return MMSYSERR_NOERROR;
1519 /**************************************************************************
1520 * modLongData [internal]
1522 static DWORD modLongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1527 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
1529 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
1530 * but it seems to be used only for midi input.
1531 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
1534 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1535 if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1537 if (midiSeqFD == -1) {
1538 WARN("can't play !\n");
1539 return MIDIERR_NODEVICE;
1542 lpData = (LPBYTE) lpMidiHdr->lpData;
1545 return MIDIERR_UNPREPARED;
1546 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1547 return MIDIERR_UNPREPARED;
1548 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1549 return MIDIERR_STILLPLAYING;
1550 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1551 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1553 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1554 * data, or can it also contain raw MIDI data, to be split up and sent to
1556 * If the latest is true, then the following WARNing will fire up
1558 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
1559 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1562 TRACE("dwBufferLength=%u !\n", lpMidiHdr->dwBufferLength);
1563 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1564 lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
1565 lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
1567 switch (MidiOutDev[wDevID].caps.wTechnology) {
1569 /* FIXME: I don't think there is much to do here */
1572 if (lpData[0] != 0xF0) {
1573 /* Send end of System Exclusive */
1574 SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, 0xF0);
1575 WARN("Adding missing 0xF0 marker at the beginning of "
1576 "system exclusive byte stream\n");
1578 for (count = 0; count < lpMidiHdr->dwBufferLength; count++) {
1579 SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, lpData[count]);
1581 if (lpData[count - 1] != 0xF7) {
1582 /* Send end of System Exclusive */
1583 SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, 0xF7);
1584 WARN("Adding missing 0xF7 marker at the end of "
1585 "system exclusive byte stream\n");
1590 WARN("Technology not supported (yet) %d !\n",
1591 MidiOutDev[wDevID].caps.wTechnology);
1592 return MMSYSERR_NOTENABLED;
1595 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1596 lpMidiHdr->dwFlags |= MHDR_DONE;
1597 if (MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD)lpMidiHdr, 0L) != MMSYSERR_NOERROR) {
1598 WARN("can't notify client !\n");
1599 return MMSYSERR_INVALPARAM;
1601 return MMSYSERR_NOERROR;
1604 /**************************************************************************
1605 * modPrepare [internal]
1607 static DWORD modPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1609 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
1611 if (midiSeqFD == -1) {
1612 WARN("can't prepare !\n");
1613 return MMSYSERR_NOTENABLED;
1616 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
1617 * asks to prepare MIDIHDR which dwFlags != 0.
1618 * So at least check for the inqueue flag
1620 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
1621 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
1622 lpMidiHdr->dwBufferLength >= 0x10000ul) {
1623 WARN("%p %p %08x %d/%d\n", lpMidiHdr, lpMidiHdr->lpData,
1624 lpMidiHdr->dwFlags, sizeof(MIDIHDR), dwSize);
1625 return MMSYSERR_INVALPARAM;
1628 lpMidiHdr->lpNext = 0;
1629 lpMidiHdr->dwFlags |= MHDR_PREPARED;
1630 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1631 return MMSYSERR_NOERROR;
1634 /**************************************************************************
1635 * modUnprepare [internal]
1637 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1639 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
1641 if (midiSeqFD == -1) {
1642 WARN("can't unprepare !\n");
1643 return MMSYSERR_NOTENABLED;
1646 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0)
1647 return MMSYSERR_INVALPARAM;
1648 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1649 return MIDIERR_STILLPLAYING;
1650 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
1651 return MMSYSERR_NOERROR;
1654 /**************************************************************************
1655 * modReset [internal]
1657 static DWORD modReset(WORD wDevID)
1661 TRACE("(%04X);\n", wDevID);
1663 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1664 if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1666 /* stop all notes */
1667 /* FIXME: check if 0x78B0 is channel dependent or not. I coded it so that
1668 * it's channel dependent...
1670 for (chn = 0; chn < 16; chn++) {
1671 /* turn off every note */
1672 modData(wDevID, 0x7800 | MIDI_CTL_CHANGE | chn);
1673 /* remove sustain on all channels */
1674 modData(wDevID, (CTL_SUSTAIN << 8) | MIDI_CTL_CHANGE | chn);
1676 /* FIXME: the LongData buffers must also be returned to the app */
1677 return MMSYSERR_NOERROR;
1680 #else /* HAVE_OSS_MIDI */
1682 LRESULT OSS_MidiInit(void)
1688 LRESULT OSS_MidiExit(void)
1695 #endif /* HAVE_OSS_MIDI */
1697 /*======================================================================*
1698 * MIDI entry points *
1699 *======================================================================*/
1701 /**************************************************************************
1702 * midMessage (WINEOSS.4)
1704 DWORD WINAPI OSS_midMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1705 DWORD dwParam1, DWORD dwParam2)
1707 TRACE("(%04X, %04X, %08X, %08X, %08X);\n",
1708 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1710 #ifdef HAVE_OSS_MIDI
1715 /* FIXME: Pretend this is supported */
1718 return midOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1720 return midClose(wDevID);
1721 case MIDM_ADDBUFFER:
1722 return midAddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1724 return midPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1725 case MIDM_UNPREPARE:
1726 return midUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1727 case MIDM_GETDEVCAPS:
1728 return midGetDevCaps(wDevID, (LPMIDIINCAPSW)dwParam1,dwParam2);
1729 case MIDM_GETNUMDEVS:
1730 return MIDM_NumDevs;
1732 return midReset(wDevID);
1734 return midStart(wDevID);
1736 return midStop(wDevID);
1739 TRACE("Unsupported message\n");
1741 return MMSYSERR_NOTSUPPORTED;
1744 /**************************************************************************
1745 * modMessage (WINEOSS.5)
1747 DWORD WINAPI OSS_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1748 DWORD dwParam1, DWORD dwParam2)
1750 TRACE("(%04X, %04X, %08X, %08X, %08X);\n",
1751 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1754 #ifdef HAVE_OSS_MIDI
1759 /* FIXME: Pretend this is supported */
1762 return modOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1764 return modClose(wDevID);
1766 return modData(wDevID, dwParam1);
1768 return modLongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1770 return modPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1771 case MODM_UNPREPARE:
1772 return modUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1773 case MODM_GETDEVCAPS:
1774 return modGetDevCaps(wDevID, (LPMIDIOUTCAPSW)dwParam1, dwParam2);
1775 case MODM_GETNUMDEVS:
1776 return MODM_NumDevs;
1777 case MODM_GETVOLUME:
1779 case MODM_SETVOLUME:
1782 return modReset(wDevID);
1785 TRACE("Unsupported message\n");
1787 return MMSYSERR_NOTSUPPORTED;
1790 /*-----------------------------------------------------------------------*/