wineoss.drv: Use offsetof to calculate the size of a struct with variable length...
[wine/wine-gecko.git] / dlls / wineoss.drv / midi.c
blob11b7c917d9c3612638a659b408ab3c34ef33606b
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
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
29 /* TODO:
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)
42 #include "config.h"
43 #include "wine/port.h"
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #ifdef HAVE_UNISTD_H
50 # include <unistd.h>
51 #endif
52 #include <fcntl.h>
53 #include <errno.h>
54 #ifdef HAVE_SYS_IOCTL_H
55 # include <sys/ioctl.h>
56 #endif
57 #ifdef HAVE_POLL_H
58 #include <poll.h>
59 #endif
60 #ifdef HAVE_SYS_POLL_H
61 #include <sys/poll.h>
62 #endif
63 #include <sys/soundcard.h>
65 #include "windef.h"
66 #include "winbase.h"
67 #include "wingdi.h"
68 #include "winuser.h"
69 #include "winnls.h"
70 #include "mmddk.h"
71 #include "wine/unicode.h"
72 #include "wine/debug.h"
74 WINE_DEFAULT_DEBUG_CHANNEL(midi);
76 #ifdef SNDCTL_SEQ_NRMIDIS
77 #define HAVE_OSS_MIDI
78 #endif
80 #ifdef HAVE_OSS_MIDI
82 typedef struct {
83 int state; /* -1 disabled, 0 is no recording started, 1 in recording, bit 2 set if in sys exclusive recording */
84 DWORD bufsize;
85 MIDIOPENDESC midiDesc;
86 WORD wFlags;
87 LPMIDIHDR lpQueueHdr;
88 DWORD dwTotalPlayed;
89 unsigned char incoming[3];
90 unsigned char incPrev;
91 char incLen;
92 DWORD startTime;
93 MIDIINCAPSW caps;
94 } WINE_MIDIIN;
96 typedef struct {
97 BOOL bEnabled;
98 DWORD bufsize;
99 MIDIOPENDESC midiDesc;
100 WORD wFlags;
101 LPMIDIHDR lpQueueHdr;
102 DWORD dwTotalPlayed;
103 void* lpExtra; /* according to port type (MIDI, FM...), extra data when needed */
104 MIDIOUTCAPSW caps;
105 } WINE_MIDIOUT;
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 =
126 0, 0, &crit_sect,
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 wavetable internal synth
156 * MOD_SWSYNTH software internal synth
159 /* FIXME Is this really the correct equivalence from UNIX to
160 Windows Sound type */
162 switch (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;
166 default:
167 ERR("Cannot determine the type of this midi device. "
168 "Assuming FM Synth\n");
169 return MOD_FMSYNTH;
173 static int MIDI_loadcount;
174 /**************************************************************************
175 * OSS_MidiInit [internal]
177 * Initializes the MIDI devices information variables
179 static LRESULT OSS_MidiInit(void)
181 int i, status, numsynthdevs = 255, nummididevs = 255;
182 struct synth_info sinfo;
183 struct midi_info minfo;
185 TRACE("(%i)\n", MIDI_loadcount);
186 if (MIDI_loadcount++)
187 return 1;
189 TRACE("Initializing the MIDI variables.\n");
191 /* try to open device */
192 if (midiOpenSeq() == -1) {
193 return -1;
196 /* find how many Synth devices are there in the system */
197 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRSYNTHS, &numsynthdevs);
199 if (status == -1) {
200 ERR("ioctl for nr synth failed.\n");
201 midiCloseSeq();
202 return -1;
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 /* The following are mandatory for MOD_MIDIPORT */
221 MidiOutDev[i].caps.wChannelMask = 0xFFFF;
222 MidiOutDev[i].caps.wVoices = 0;
223 MidiOutDev[i].caps.wNotes = 0;
224 MidiOutDev[i].caps.dwSupport = 0;
226 sinfo.device = i;
227 status = ioctl(midiSeqFD, SNDCTL_SYNTH_INFO, &sinfo);
228 if (status == -1) {
229 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};
230 ERR("ioctl for synth info failed on %d, disabling it.\n", i);
232 wsprintfW(MidiOutDev[i].caps.szPname, fmt, i);
234 MidiOutDev[i].caps.wTechnology = MOD_MIDIPORT;
235 MidiOutDev[i].bEnabled = FALSE;
236 } else {
237 MultiByteToWideChar( CP_UNIXCP, 0, sinfo.name, -1,
238 MidiOutDev[i].caps.szPname,
239 sizeof(MidiOutDev[i].caps.szPname)/sizeof(WCHAR) );
240 MidiOutDev[i].caps.wTechnology = MIDI_UnixToWindowsDeviceType(sinfo.synth_type);
242 if (MOD_MIDIPORT != MidiOutDev[i].caps.wTechnology) {
243 /* FIXME Do we have this information?
244 * Assuming the soundcards can handle
245 * MIDICAPS_VOLUME and MIDICAPS_LRVOLUME but
246 * not MIDICAPS_CACHE.
248 MidiOutDev[i].caps.dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
249 MidiOutDev[i].caps.wVoices = sinfo.nr_voices;
251 /* FIXME Is it possible to know the maximum
252 * number of simultaneous notes of a soundcard ?
253 * I believe we don't have this information, but
254 * it's probably equal or more than wVoices
256 MidiOutDev[i].caps.wNotes = sinfo.nr_voices;
258 MidiOutDev[i].bEnabled = TRUE;
260 /* We also have the information sinfo.synth_subtype, not used here
262 if (sinfo.capabilities & SYNTH_CAP_INPUT) {
263 FIXME("Synthesizer supports MIDI in. Not yet supported.\n");
265 TRACE("SynthOut[%d]\tOSS info: synth type=%d/%d capa=%lx\n",
266 i, sinfo.synth_type, sinfo.synth_subtype, (long)sinfo.capabilities);
269 TRACE("SynthOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%d\n",
270 i, wine_dbgstr_w(MidiOutDev[i].caps.szPname),
271 MidiOutDev[i].caps.wTechnology,
272 MidiOutDev[i].caps.wVoices, MidiOutDev[i].caps.wNotes,
273 MidiOutDev[i].caps.wChannelMask, MidiOutDev[i].caps.dwSupport);
276 /* find how many MIDI devices are there in the system */
277 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRMIDIS, &nummididevs);
278 if (status == -1) {
279 ERR("ioctl on nr midi failed.\n");
280 nummididevs = 0;
281 goto wrapup;
284 /* FIXME: the two restrictions below could be loosened in some cases */
285 if (numsynthdevs + nummididevs > MAX_MIDIOUTDRV) {
286 ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
287 "Some MIDI devices will not be available.\n");
288 nummididevs = MAX_MIDIOUTDRV - numsynthdevs;
291 if (nummididevs > MAX_MIDIINDRV) {
292 ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
293 "Some MIDI devices will not be available.\n",MAX_MIDIINDRV,nummididevs);
294 nummididevs = MAX_MIDIINDRV;
297 for (i = 0; i < nummididevs; i++) {
298 minfo.device = i;
299 status = ioctl(midiSeqFD, SNDCTL_MIDI_INFO, &minfo);
300 if (status == -1) WARN("ioctl on midi info for device %d failed.\n", i);
302 /* This whole part is somewhat obscure to me. I'll keep trying to dig
303 info about it. If you happen to know, please tell us. The very
304 descriptive minfo.dev_type was not used here.
306 /* Manufacturer ID. We do not have access to this with soundcard.h
307 Does not seem to be a problem, because in mmsystem.h only
308 Microsoft's ID is listed */
309 MidiOutDev[numsynthdevs + i].caps.wMid = 0x00FF;
310 MidiOutDev[numsynthdevs + i].caps.wPid = 0x0001; /* FIXME Product ID */
311 /* Product Version. We simply say "1" */
312 MidiOutDev[numsynthdevs + i].caps.vDriverVersion = 0x001;
313 if (status == -1) {
314 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};
315 wsprintfW(MidiOutDev[numsynthdevs + i].caps.szPname, fmt, numsynthdevs + i);
316 MidiOutDev[numsynthdevs + i].bEnabled = FALSE;
317 } else {
318 MultiByteToWideChar(CP_UNIXCP, 0, minfo.name, -1,
319 MidiOutDev[numsynthdevs + i].caps.szPname,
320 sizeof(MidiOutDev[numsynthdevs + i].caps.szPname) / sizeof(WCHAR));
321 MidiOutDev[numsynthdevs + i].bEnabled = TRUE;
323 MidiOutDev[numsynthdevs + i].caps.wTechnology = MOD_MIDIPORT;
324 MidiOutDev[numsynthdevs + i].caps.wVoices = 0;
325 MidiOutDev[numsynthdevs + i].caps.wNotes = 0;
326 MidiOutDev[numsynthdevs + i].caps.wChannelMask= 0xFFFF;
327 MidiOutDev[numsynthdevs + i].caps.dwSupport = 0;
329 /* This whole part is somewhat obscure to me. I'll keep trying to dig
330 info about it. If you happen to know, please tell us. The very
331 descriptive minfo.dev_type was not used here.
333 /* Manufac ID. We do not have access to this with soundcard.h
334 Does not seem to be a problem, because in mmsystem.h only
335 Microsoft's ID is listed */
336 MidiInDev[i].caps.wMid = 0x00FF;
337 MidiInDev[i].caps.wPid = 0x0001; /* FIXME Product ID */
338 /* Product Version. We simply say "1" */
339 MidiInDev[i].caps.vDriverVersion = 0x001;
340 if (status == -1) {
341 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};
342 wsprintfW(MidiInDev[i].caps.szPname, fmt, numsynthdevs + i);
343 MidiInDev[i].state = -1;
344 } else {
345 MultiByteToWideChar(CP_UNIXCP, 0, minfo.name, -1,
346 MidiInDev[i].caps.szPname,
347 sizeof(MidiInDev[i].caps.szPname) / sizeof(WCHAR));
348 MidiInDev[i].state = 0;
350 MidiInDev[i].caps.dwSupport = 0; /* mandatory with MIDIINCAPS */
352 TRACE("OSS info: midi[%d] dev-type=%d capa=%lx\n"
353 "\tMidiOut[%d] name='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%d\n"
354 "\tMidiIn [%d] name='%s' support=%d\n",
355 i, minfo.dev_type, (long)minfo.capabilities,
356 numsynthdevs + i, wine_dbgstr_w(MidiOutDev[numsynthdevs + i].caps.szPname),
357 MidiOutDev[numsynthdevs + i].caps.wTechnology,
358 MidiOutDev[numsynthdevs + i].caps.wVoices, MidiOutDev[numsynthdevs + i].caps.wNotes,
359 MidiOutDev[numsynthdevs + i].caps.wChannelMask, MidiOutDev[numsynthdevs + i].caps.dwSupport,
360 i, wine_dbgstr_w(MidiInDev[i].caps.szPname), MidiInDev[i].caps.dwSupport);
363 wrapup:
364 /* windows does not seem to differentiate Synth from MIDI devices */
365 MODM_NumFMSynthDevs = numsynthdevs;
366 MODM_NumDevs = numsynthdevs + nummididevs;
368 MIDM_NumDevs = nummididevs;
370 /* close file and exit */
371 midiCloseSeq();
373 return 0;
376 /**************************************************************************
377 * OSS_MidiExit [internal]
379 * Release the MIDI devices information variables
381 static LRESULT OSS_MidiExit(void)
383 TRACE("(%i)\n", MIDI_loadcount);
385 if (--MIDI_loadcount)
386 return 1;
388 ZeroMemory(MidiInDev, sizeof(MidiInDev));
389 ZeroMemory(MidiOutDev, sizeof(MidiOutDev));
391 MODM_NumDevs = 0;
392 MODM_NumFMSynthDevs = 0;
393 MIDM_NumDevs = 0;
395 return 0;
398 /**************************************************************************
399 * MIDI_NotifyClient [internal]
401 static void MIDI_NotifyClient(UINT wDevID, WORD wMsg,
402 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
404 DWORD_PTR dwCallBack;
405 UINT uFlags;
406 HANDLE hDev;
407 DWORD_PTR dwInstance;
409 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",
410 wDevID, wMsg, dwParam1, dwParam2);
412 switch (wMsg) {
413 case MOM_OPEN:
414 case MOM_CLOSE:
415 case MOM_DONE:
416 case MOM_POSITIONCB:
417 if (wDevID > MODM_NumDevs) return;
419 dwCallBack = MidiOutDev[wDevID].midiDesc.dwCallback;
420 uFlags = MidiOutDev[wDevID].wFlags;
421 hDev = MidiOutDev[wDevID].midiDesc.hMidi;
422 dwInstance = MidiOutDev[wDevID].midiDesc.dwInstance;
423 break;
425 case MIM_OPEN:
426 case MIM_CLOSE:
427 case MIM_DATA:
428 case MIM_LONGDATA:
429 case MIM_ERROR:
430 case MIM_LONGERROR:
431 case MIM_MOREDATA:
432 if (wDevID > MIDM_NumDevs) return;
434 dwCallBack = MidiInDev[wDevID].midiDesc.dwCallback;
435 uFlags = MidiInDev[wDevID].wFlags;
436 hDev = MidiInDev[wDevID].midiDesc.hMidi;
437 dwInstance = MidiInDev[wDevID].midiDesc.dwInstance;
438 break;
439 default:
440 ERR("Unsupported MSW-MIDI message %u\n", wMsg);
441 return;
444 DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2);
447 static int midi_warn = 1;
448 /**************************************************************************
449 * midiOpenSeq [internal]
451 static int midiOpenSeq(void)
453 if (numOpenMidiSeq == 0) {
454 const char* device;
455 device=getenv("MIDIDEV");
456 if (!device) device="/dev/sequencer";
457 midiSeqFD = open(device, O_RDWR, 0);
458 if (midiSeqFD == -1) {
459 if (midi_warn)
461 WARN("Can't open MIDI device '%s' ! (%s). If your "
462 "program needs this (probably not): %s\n",
463 device, strerror(errno),
464 errno == ENOENT ?
465 "create it ! (\"man MAKEDEV\" ?)" :
466 errno == ENODEV ?
467 "load MIDI sequencer kernel driver !" :
468 errno == EACCES ?
469 "grant access ! (\"man chmod\")" : ""
472 midi_warn = 0;
473 return -1;
475 #if 0
476 if (fcntl(midiSeqFD, F_SETFL, O_NONBLOCK) < 0) {
477 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno, strerror(errno));
478 close(midiSeqFD);
479 midiSeqFD = -1;
480 return -1;
482 #endif
483 fcntl(midiSeqFD, F_SETFD, 1); /* set close on exec flag */
484 ioctl(midiSeqFD, SNDCTL_SEQ_RESET);
486 numOpenMidiSeq++;
487 return 0;
490 /**************************************************************************
491 * midiCloseSeq [internal]
493 static int midiCloseSeq(void)
495 if (--numOpenMidiSeq == 0) {
496 close(midiSeqFD);
497 midiSeqFD = -1;
499 return 0;
502 /* FIXME: this is a bad idea, it's even not static... */
503 SEQ_DEFINEBUF(1024);
505 /* FIXME: this is not reentrant, not static - because of global variable
506 * _seqbuf and al.
508 /**************************************************************************
509 * seqbuf_dump [internal]
511 * Used by SEQ_DUMPBUF to flush the buffer.
514 void seqbuf_dump(void)
516 if (_seqbufptr) {
517 if (write(midiSeqFD, _seqbuf, _seqbufptr) == -1) {
518 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
519 midiSeqFD, errno, strerror(errno));
521 /* FIXME:
522 * in any case buffer is lost so that if many errors occur the buffer
523 * will not overrun
525 _seqbufptr = 0;
529 /**************************************************************************
530 * midReceiveChar [internal]
532 static void midReceiveChar(WORD wDevID, unsigned char value, DWORD dwTime)
534 DWORD toSend = 0;
536 TRACE("Adding %02xh to %d[%d]\n", value, wDevID, MidiInDev[wDevID].incLen);
538 if (wDevID >= MIDM_NumDevs) {
539 WARN("bad devID\n");
540 return;
542 if (MidiInDev[wDevID].state <= 0) {
543 TRACE("disabled or input not started, thrown away\n");
544 return;
547 if (MidiInDev[wDevID].state & 2) { /* system exclusive */
548 LPMIDIHDR lpMidiHdr;
549 WORD sbfb = FALSE;
551 EnterCriticalSection(&crit_sect);
552 if ((lpMidiHdr = MidiInDev[wDevID].lpQueueHdr) != NULL) {
553 LPBYTE lpData = (LPBYTE) lpMidiHdr->lpData;
555 lpData[lpMidiHdr->dwBytesRecorded++] = value;
556 if (lpMidiHdr->dwBytesRecorded == lpMidiHdr->dwBufferLength) {
557 sbfb = TRUE;
560 if (value == 0xF7) { /* then end */
561 MidiInDev[wDevID].state &= ~2;
562 sbfb = TRUE;
564 if (sbfb && lpMidiHdr != NULL) {
565 lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
566 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
567 lpMidiHdr->dwFlags |= MHDR_DONE;
568 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr->lpNext;
569 MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD_PTR)lpMidiHdr, dwTime);
571 LeaveCriticalSection(&crit_sect);
572 return;
575 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
576 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
578 if (!IS_CMD(value) && MidiInDev[wDevID].incLen == 0) { /* try to reuse old cmd */
579 if (IS_CMD(MidiInDev[wDevID].incPrev) && !IS_SYS_CMD(MidiInDev[wDevID].incPrev)) {
580 MidiInDev[wDevID].incoming[0] = MidiInDev[wDevID].incPrev;
581 MidiInDev[wDevID].incLen = 1;
582 TRACE("Reusing old command %02xh\n", MidiInDev[wDevID].incPrev);
583 } else {
584 FIXME("error for midi-in, should generate MIM_ERROR notification:"
585 " prev=%02Xh, incLen=%02Xh\n",
586 MidiInDev[wDevID].incPrev, MidiInDev[wDevID].incLen);
587 return;
590 MidiInDev[wDevID].incoming[(int)(MidiInDev[wDevID].incLen++)] = value;
591 if (MidiInDev[wDevID].incLen == 1 && !IS_SYS_CMD(MidiInDev[wDevID].incoming[0])) {
592 /* store new cmd, just in case */
593 MidiInDev[wDevID].incPrev = MidiInDev[wDevID].incoming[0];
596 #undef IS_CMD
597 #undef IS_SYS_CMD
599 switch (MidiInDev[wDevID].incoming[0] & 0xF0) {
600 case MIDI_NOTEOFF:
601 case MIDI_NOTEON:
602 case MIDI_KEY_PRESSURE:
603 case MIDI_CTL_CHANGE:
604 case MIDI_PITCH_BEND:
605 if (MidiInDev[wDevID].incLen == 3) {
606 toSend = (MidiInDev[wDevID].incoming[2] << 16) |
607 (MidiInDev[wDevID].incoming[1] << 8) |
608 (MidiInDev[wDevID].incoming[0] << 0);
610 break;
611 case MIDI_PGM_CHANGE:
612 case MIDI_CHN_PRESSURE:
613 if (MidiInDev[wDevID].incLen == 2) {
614 toSend = (MidiInDev[wDevID].incoming[1] << 8) |
615 (MidiInDev[wDevID].incoming[0] << 0);
617 break;
618 case MIDI_SYSTEM_PREFIX:
619 if (MidiInDev[wDevID].incoming[0] == 0xF0) {
620 MidiInDev[wDevID].state |= 2;
621 MidiInDev[wDevID].incLen = 0;
622 } else {
623 if (MidiInDev[wDevID].incLen == 1) {
624 toSend = (MidiInDev[wDevID].incoming[0] << 0);
627 break;
628 default:
629 WARN("This shouldn't happen (%02X)\n", MidiInDev[wDevID].incoming[0]);
631 if (toSend != 0) {
632 TRACE("Sending event %08x\n", toSend);
633 MidiInDev[wDevID].incLen = 0;
634 dwTime -= MidiInDev[wDevID].startTime;
635 MIDI_NotifyClient(wDevID, MIM_DATA, toSend, dwTime);
639 static DWORD WINAPI midRecThread(LPVOID arg)
641 unsigned char buffer[256];
642 int len, idx;
643 DWORD dwTime;
644 struct pollfd pfd;
646 TRACE("Thread startup\n");
648 pfd.fd = midiSeqFD;
649 pfd.fd = POLLIN;
651 while(!end_thread) {
652 TRACE("Thread loop\n");
654 /* Check if an event is present */
655 if (poll(&pfd, 1, 250) <= 0)
656 continue;
658 len = read(midiSeqFD, buffer, sizeof(buffer));
659 TRACE("Received %d bytes\n", len);
661 if (len < 0) continue;
662 if ((len % 4) != 0) {
663 WARN("Bad length %d, errno %d (%s)\n", len, errno, strerror(errno));
664 continue;
667 dwTime = GetTickCount();
669 for (idx = 0; idx < len; ) {
670 if (buffer[idx] & 0x80) {
671 TRACE(
672 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
673 buffer[idx + 0], buffer[idx + 1],
674 buffer[idx + 2], buffer[idx + 3],
675 buffer[idx + 4], buffer[idx + 5],
676 buffer[idx + 6], buffer[idx + 7]);
677 idx += 8;
678 } else {
679 switch (buffer[idx + 0]) {
680 case SEQ_WAIT:
681 case SEQ_ECHO:
682 break;
683 case SEQ_MIDIPUTC:
684 midReceiveChar(buffer[idx + 2], buffer[idx + 1], dwTime);
685 break;
686 default:
687 TRACE("Unsupported event %d\n", buffer[idx + 0]);
688 break;
690 idx += 4;
694 return 0;
697 /**************************************************************************
698 * midGetDevCaps [internal]
700 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPSW lpCaps, DWORD dwSize)
702 TRACE("(%04X, %p, %08X);\n", wDevID, lpCaps, dwSize);
704 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
705 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
707 memcpy(lpCaps, &MidiInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
709 return MMSYSERR_NOERROR;
712 /**************************************************************************
713 * midOpen [internal]
715 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
717 TRACE("(%04X, %p, %08X);\n", wDevID, lpDesc, dwFlags);
719 if (lpDesc == NULL) {
720 WARN("Invalid Parameter !\n");
721 return MMSYSERR_INVALPARAM;
724 /* FIXME :
725 * how to check that content of lpDesc is correct ?
727 if (wDevID >= MIDM_NumDevs) {
728 WARN("wDevID too large (%u) !\n", wDevID);
729 return MMSYSERR_BADDEVICEID;
731 if (MidiInDev[wDevID].state == -1) {
732 WARN("device disabled\n");
733 return MIDIERR_NODEVICE;
735 if (MidiInDev[wDevID].midiDesc.hMidi != 0) {
736 WARN("device already open !\n");
737 return MMSYSERR_ALLOCATED;
739 if ((dwFlags & MIDI_IO_STATUS) != 0) {
740 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
741 dwFlags &= ~MIDI_IO_STATUS;
743 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
744 FIXME("Bad dwFlags\n");
745 return MMSYSERR_INVALFLAG;
748 if (midiOpenSeq() < 0) {
749 return MMSYSERR_ERROR;
752 if (numStartedMidiIn++ == 0) {
753 end_thread = 0;
754 hThread = CreateThread(NULL, 0, midRecThread, NULL, 0, NULL);
755 if (!hThread) {
756 numStartedMidiIn = 0;
757 WARN("Couldn't create thread for midi-in\n");
758 midiCloseSeq();
759 return MMSYSERR_ERROR;
761 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
762 TRACE("Created thread for midi-in\n");
765 MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
767 MidiInDev[wDevID].lpQueueHdr = NULL;
768 MidiInDev[wDevID].dwTotalPlayed = 0;
769 MidiInDev[wDevID].bufsize = 0x3FFF;
770 MidiInDev[wDevID].midiDesc = *lpDesc;
771 MidiInDev[wDevID].state = 0;
772 MidiInDev[wDevID].incLen = 0;
773 MidiInDev[wDevID].startTime = 0;
775 MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L);
776 return MMSYSERR_NOERROR;
779 /**************************************************************************
780 * midClose [internal]
782 static DWORD midClose(WORD wDevID)
784 int ret = MMSYSERR_NOERROR;
786 TRACE("(%04X);\n", wDevID);
788 if (wDevID >= MIDM_NumDevs) {
789 WARN("wDevID too big (%u) !\n", wDevID);
790 return MMSYSERR_BADDEVICEID;
792 if (MidiInDev[wDevID].midiDesc.hMidi == 0) {
793 WARN("device not opened !\n");
794 return MMSYSERR_ERROR;
796 if (MidiInDev[wDevID].lpQueueHdr != 0) {
797 return MIDIERR_STILLPLAYING;
800 if (midiSeqFD == -1) {
801 WARN("ooops !\n");
802 return MMSYSERR_ERROR;
804 if (--numStartedMidiIn == 0) {
805 TRACE("Stopping thread for midi-in\n");
806 end_thread = 1;
807 if (WaitForSingleObject(hThread, 5000) != WAIT_OBJECT_0) {
808 WARN("Thread end not signaled, force termination\n");
809 TerminateThread(hThread, 0);
811 TRACE("Stopped thread for midi-in\n");
813 midiCloseSeq();
815 MidiInDev[wDevID].bufsize = 0;
816 MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L);
817 MidiInDev[wDevID].midiDesc.hMidi = 0;
818 return ret;
821 /**************************************************************************
822 * midAddBuffer [internal]
824 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
826 TRACE("(%04X, %p, %d);\n", wDevID, lpMidiHdr, dwSize);
828 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
829 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
831 if (lpMidiHdr == NULL) return MMSYSERR_INVALPARAM;
832 if (dwSize < offsetof(MIDIHDR,dwOffset)) return MMSYSERR_INVALPARAM;
833 if (lpMidiHdr->dwBufferLength == 0) return MMSYSERR_INVALPARAM;
834 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
835 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
837 EnterCriticalSection(&crit_sect);
838 lpMidiHdr->dwFlags &= ~WHDR_DONE;
839 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
840 lpMidiHdr->dwBytesRecorded = 0;
841 lpMidiHdr->lpNext = 0;
842 if (MidiInDev[wDevID].lpQueueHdr == 0) {
843 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr;
844 } else {
845 LPMIDIHDR ptr;
847 for (ptr = MidiInDev[wDevID].lpQueueHdr;
848 ptr->lpNext != 0;
849 ptr = ptr->lpNext);
850 ptr->lpNext = lpMidiHdr;
852 LeaveCriticalSection(&crit_sect);
854 return MMSYSERR_NOERROR;
857 /**************************************************************************
858 * midPrepare [internal]
860 static DWORD midPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
862 TRACE("(%04X, %p, %d);\n", wDevID, lpMidiHdr, dwSize);
864 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 || lpMidiHdr->lpData == 0)
865 return MMSYSERR_INVALPARAM;
866 if (lpMidiHdr->dwFlags & MHDR_PREPARED)
867 return MMSYSERR_NOERROR;
869 lpMidiHdr->lpNext = 0;
870 lpMidiHdr->dwFlags |= MHDR_PREPARED;
871 lpMidiHdr->dwFlags &= ~(MHDR_DONE|MHDR_INQUEUE); /* flags cleared since w2k */
873 return MMSYSERR_NOERROR;
876 /**************************************************************************
877 * midUnprepare [internal]
879 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
881 TRACE("(%04X, %p, %d);\n", wDevID, lpMidiHdr, dwSize);
883 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 || lpMidiHdr->lpData == 0)
884 return MMSYSERR_INVALPARAM;
885 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
886 return MMSYSERR_NOERROR;
887 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
888 return MIDIERR_STILLPLAYING;
890 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
892 return MMSYSERR_NOERROR;
895 /**************************************************************************
896 * midReset [internal]
898 static DWORD midReset(WORD wDevID)
900 DWORD dwTime = GetTickCount();
902 TRACE("(%04X);\n", wDevID);
904 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
905 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
907 EnterCriticalSection(&crit_sect);
908 while (MidiInDev[wDevID].lpQueueHdr) {
909 MidiInDev[wDevID].lpQueueHdr->dwFlags &= ~MHDR_INQUEUE;
910 MidiInDev[wDevID].lpQueueHdr->dwFlags |= MHDR_DONE;
911 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
912 MIDI_NotifyClient(wDevID, MIM_LONGDATA,
913 (DWORD_PTR)MidiInDev[wDevID].lpQueueHdr, dwTime);
914 MidiInDev[wDevID].lpQueueHdr = MidiInDev[wDevID].lpQueueHdr->lpNext;
916 LeaveCriticalSection(&crit_sect);
918 return MMSYSERR_NOERROR;
921 /**************************************************************************
922 * midStart [internal]
924 static DWORD midStart(WORD wDevID)
926 TRACE("(%04X);\n", wDevID);
928 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
929 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
931 MidiInDev[wDevID].state = 1;
932 MidiInDev[wDevID].startTime = GetTickCount();
933 return MMSYSERR_NOERROR;
936 /**************************************************************************
937 * midStop [internal]
939 static DWORD midStop(WORD wDevID)
941 TRACE("(%04X);\n", wDevID);
943 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
944 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
946 MidiInDev[wDevID].state = 0;
947 return MMSYSERR_NOERROR;
950 /*-----------------------------------------------------------------------*/
952 typedef struct sVoice {
953 int note; /* 0 means not used */
954 int channel;
955 unsigned cntMark : 30,
956 status : 2;
957 #define sVS_UNUSED 0
958 #define sVS_PLAYING 1
959 #define sVS_SUSTAINED 2
960 } sVoice;
962 typedef struct sChannel {
963 int program;
965 int bender;
966 int benderRange;
967 /* controllers */
968 int bank; /* CTL_BANK_SELECT */
969 int volume; /* CTL_MAIN_VOLUME */
970 int balance; /* CTL_BALANCE */
971 int expression; /* CTL_EXPRESSION */
972 int sustain; /* CTL_SUSTAIN */
974 unsigned char nrgPmtMSB; /* Non register Parameters */
975 unsigned char nrgPmtLSB;
976 unsigned char regPmtMSB; /* Non register Parameters */
977 unsigned char regPmtLSB;
978 } sChannel;
980 typedef struct sFMextra {
981 unsigned counter;
982 int drumSetMask;
983 sChannel channel[16]; /* MIDI has only 16 channels */
984 sVoice voice[1]; /* dyn allocated according to sound card */
985 /* do not append fields below voice[1] since the size of this structure
986 * depends on the number of available voices on the FM synth...
988 } sFMextra;
990 extern const unsigned char midiFMInstrumentPatches[16 * 128];
991 extern const unsigned char midiFMDrumsPatches [16 * 128];
993 /**************************************************************************
994 * modFMLoad [internal]
996 static int modFMLoad(int dev)
998 int i;
999 struct sbi_instrument sbi;
1001 sbi.device = dev;
1002 sbi.key = FM_PATCH;
1004 memset(sbi.operators + 16, 0, 16);
1005 for (i = 0; i < 128; i++) {
1006 sbi.channel = i;
1007 memcpy(sbi.operators, midiFMInstrumentPatches + i * 16, 16);
1009 if (write(midiSeqFD, &sbi, sizeof(sbi)) == -1) {
1010 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
1011 return -1;
1014 for (i = 0; i < 128; i++) {
1015 sbi.channel = 128 + i;
1016 memcpy(sbi.operators, midiFMDrumsPatches + i * 16, 16);
1018 if (write(midiSeqFD, &sbi, sizeof(sbi)) == -1) {
1019 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
1020 return -1;
1023 return 0;
1026 /**************************************************************************
1027 * modFMReset [internal]
1029 static void modFMReset(WORD wDevID)
1031 sFMextra* extra = MidiOutDev[wDevID].lpExtra;
1032 sVoice* voice = extra->voice;
1033 sChannel* channel = extra->channel;
1034 int i;
1036 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1037 if (voice[i].status != sVS_UNUSED) {
1038 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1040 SEQ_KEY_PRESSURE(wDevID, i, 127, 0);
1041 SEQ_CONTROL(wDevID, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
1042 voice[i].note = 0;
1043 voice[i].channel = -1;
1044 voice[i].cntMark = 0;
1045 voice[i].status = sVS_UNUSED;
1047 for (i = 0; i < 16; i++) {
1048 channel[i].program = 0;
1049 channel[i].bender = 8192;
1050 channel[i].benderRange = 2;
1051 channel[i].bank = 0;
1052 channel[i].volume = 127;
1053 channel[i].balance = 64;
1054 channel[i].expression = 0;
1055 channel[i].sustain = 0;
1057 extra->counter = 0;
1058 extra->drumSetMask = 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
1059 SEQ_DUMPBUF();
1062 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
1064 /**************************************************************************
1065 * modGetDevCaps [internal]
1067 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPSW lpCaps, DWORD dwSize)
1069 TRACE("(%04X, %p, %08X);\n", wDevID, lpCaps, dwSize);
1071 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1072 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
1074 memcpy(lpCaps, &MidiOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1076 return MMSYSERR_NOERROR;
1079 /**************************************************************************
1080 * modOpen [internal]
1082 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
1084 TRACE("(%04X, %p, %08X);\n", wDevID, lpDesc, dwFlags);
1085 if (lpDesc == NULL) {
1086 WARN("Invalid Parameter !\n");
1087 return MMSYSERR_INVALPARAM;
1089 if (wDevID >= MODM_NumDevs) {
1090 TRACE("MAX_MIDIOUTDRV reached !\n");
1091 return MMSYSERR_BADDEVICEID;
1093 if (MidiOutDev[wDevID].midiDesc.hMidi != 0) {
1094 WARN("device already open !\n");
1095 return MMSYSERR_ALLOCATED;
1097 if (!MidiOutDev[wDevID].bEnabled) {
1098 WARN("device disabled !\n");
1099 return MIDIERR_NODEVICE;
1101 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
1102 WARN("bad dwFlags\n");
1103 return MMSYSERR_INVALFLAG;
1105 if (!MidiOutDev[wDevID].bEnabled) {
1106 TRACE("disabled wDevID\n");
1107 return MMSYSERR_NOTENABLED;
1110 MidiOutDev[wDevID].lpExtra = 0;
1112 switch (MidiOutDev[wDevID].caps.wTechnology) {
1113 case MOD_FMSYNTH:
1115 void* extra;
1117 extra = HeapAlloc(GetProcessHeap(), 0,
1118 offsetof(struct sFMextra, voice[MidiOutDev[wDevID].caps.wVoices]));
1120 if (extra == 0) {
1121 WARN("can't alloc extra data !\n");
1122 return MMSYSERR_NOMEM;
1124 MidiOutDev[wDevID].lpExtra = extra;
1125 if (midiOpenSeq() < 0) {
1126 MidiOutDev[wDevID].lpExtra = 0;
1127 HeapFree(GetProcessHeap(), 0, extra);
1128 return MMSYSERR_ERROR;
1130 if (modFMLoad(wDevID) < 0) {
1131 midiCloseSeq();
1132 MidiOutDev[wDevID].lpExtra = 0;
1133 HeapFree(GetProcessHeap(), 0, extra);
1134 return MMSYSERR_ERROR;
1136 modFMReset(wDevID);
1138 break;
1139 case MOD_MIDIPORT:
1140 case MOD_SYNTH:
1141 if (midiOpenSeq() < 0) {
1142 return MMSYSERR_ALLOCATED;
1144 break;
1145 default:
1146 WARN("Technology not supported (yet) %d !\n",
1147 MidiOutDev[wDevID].caps.wTechnology);
1148 return MMSYSERR_NOTENABLED;
1151 MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1153 MidiOutDev[wDevID].lpQueueHdr = NULL;
1154 MidiOutDev[wDevID].dwTotalPlayed = 0;
1155 MidiOutDev[wDevID].bufsize = 0x3FFF;
1156 MidiOutDev[wDevID].midiDesc = *lpDesc;
1158 MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L);
1159 TRACE("Successful !\n");
1160 return MMSYSERR_NOERROR;
1164 /**************************************************************************
1165 * modClose [internal]
1167 static DWORD modClose(WORD wDevID)
1169 int ret = MMSYSERR_NOERROR;
1171 TRACE("(%04X);\n", wDevID);
1173 if (MidiOutDev[wDevID].midiDesc.hMidi == 0) {
1174 WARN("device not opened !\n");
1175 return MMSYSERR_ERROR;
1177 /* FIXME: should test that no pending buffer is still in the queue for
1178 * playing */
1180 if (midiSeqFD == -1) {
1181 WARN("can't close !\n");
1182 return MMSYSERR_ERROR;
1185 switch (MidiOutDev[wDevID].caps.wTechnology) {
1186 case MOD_FMSYNTH:
1187 case MOD_MIDIPORT:
1188 midiCloseSeq();
1189 break;
1190 default:
1191 WARN("Technology not supported (yet) %d !\n",
1192 MidiOutDev[wDevID].caps.wTechnology);
1193 return MMSYSERR_NOTENABLED;
1196 HeapFree(GetProcessHeap(), 0, MidiOutDev[wDevID].lpExtra);
1197 MidiOutDev[wDevID].lpExtra = 0;
1199 MidiOutDev[wDevID].bufsize = 0;
1200 MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L);
1201 MidiOutDev[wDevID].midiDesc.hMidi = 0;
1202 return ret;
1205 /**************************************************************************
1206 * modData [internal]
1208 static DWORD modData(WORD wDevID, DWORD dwParam)
1210 WORD evt = LOBYTE(LOWORD(dwParam));
1211 WORD d1 = HIBYTE(LOWORD(dwParam));
1212 WORD d2 = LOBYTE(HIWORD(dwParam));
1214 TRACE("(%04X, %08X);\n", wDevID, dwParam);
1216 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1217 if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1219 if (midiSeqFD == -1) {
1220 WARN("can't play !\n");
1221 return MIDIERR_NODEVICE;
1223 switch (MidiOutDev[wDevID].caps.wTechnology) {
1224 case MOD_FMSYNTH:
1225 /* FIXME:
1226 * - chorus depth controller is not used
1229 sFMextra* extra = MidiOutDev[wDevID].lpExtra;
1230 sVoice* voice = extra->voice;
1231 sChannel* channel = extra->channel;
1232 int chn = (evt & 0x0F);
1233 int i, nv;
1235 switch (evt & 0xF0) {
1236 case MIDI_NOTEOFF:
1237 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1238 /* don't stop sustained notes */
1239 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1240 voice[i].status = sVS_UNUSED;
1241 SEQ_STOP_NOTE(wDevID, i, d1, d2);
1244 break;
1245 case MIDI_NOTEON:
1246 if (d2 == 0) { /* note off if velocity == 0 */
1247 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1248 /* don't stop sustained notes */
1249 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1250 voice[i].status = sVS_UNUSED;
1251 SEQ_STOP_NOTE(wDevID, i, d1, 64);
1254 break;
1256 /* finding out in this order :
1257 * - an empty voice
1258 * - if replaying the same note on the same channel
1259 * - the older voice (LRU)
1261 for (i = nv = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1262 if (voice[i].status == sVS_UNUSED ||
1263 (voice[i].note == d1 && voice[i].channel == chn)) {
1264 nv = i;
1265 break;
1267 if (voice[i].cntMark < voice[0].cntMark) {
1268 nv = i;
1271 TRACE(
1272 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1273 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1274 nv, channel[chn].program,
1275 channel[chn].balance,
1276 channel[chn].volume,
1277 channel[chn].bender, d1, d2);
1279 SEQ_SET_PATCH(wDevID, nv, IS_DRUM_CHANNEL(extra, chn) ?
1280 (128 + d1) : channel[chn].program);
1281 SEQ_BENDER_RANGE(wDevID, nv, channel[chn].benderRange * 100);
1282 SEQ_BENDER(wDevID, nv, channel[chn].bender);
1283 SEQ_CONTROL(wDevID, nv, CTL_PAN, channel[chn].balance);
1284 SEQ_CONTROL(wDevID, nv, CTL_EXPRESSION, channel[chn].expression);
1285 #if 0
1286 /* FIXME: does not really seem to work on my SB card and
1287 * screws everything up... so lay it down
1289 SEQ_CONTROL(wDevID, nv, CTL_MAIN_VOLUME, channel[chn].volume);
1290 #endif
1291 SEQ_START_NOTE(wDevID, nv, d1, d2);
1292 voice[nv].status = channel[chn].sustain ? sVS_SUSTAINED : sVS_PLAYING;
1293 voice[nv].note = d1;
1294 voice[nv].channel = chn;
1295 voice[nv].cntMark = extra->counter++;
1296 break;
1297 case MIDI_KEY_PRESSURE:
1298 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1299 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn && voice[i].note == d1) {
1300 SEQ_KEY_PRESSURE(wDevID, i, d1, d2);
1303 break;
1304 case MIDI_CTL_CHANGE:
1305 switch (d1) {
1306 case CTL_BANK_SELECT: channel[chn].bank = d2; break;
1307 case CTL_MAIN_VOLUME: channel[chn].volume = d2; break;
1308 case CTL_PAN: channel[chn].balance = d2; break;
1309 case CTL_EXPRESSION: channel[chn].expression = d2; break;
1310 case CTL_SUSTAIN: channel[chn].sustain = d2;
1311 if (d2) {
1312 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1313 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1314 voice[i].status = sVS_SUSTAINED;
1317 } else {
1318 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1319 if (voice[i].status == sVS_SUSTAINED && voice[i].channel == chn) {
1320 voice[i].status = sVS_UNUSED;
1321 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1325 break;
1326 case CTL_NONREG_PARM_NUM_LSB: channel[chn].nrgPmtLSB = d2; break;
1327 case CTL_NONREG_PARM_NUM_MSB: channel[chn].nrgPmtMSB = d2; break;
1328 case CTL_REGIST_PARM_NUM_LSB: channel[chn].regPmtLSB = d2; break;
1329 case CTL_REGIST_PARM_NUM_MSB: channel[chn].regPmtMSB = d2; break;
1330 case CTL_DATA_ENTRY:
1331 switch ((channel[chn].regPmtMSB << 8) | channel[chn].regPmtLSB) {
1332 case 0x0000:
1333 if (channel[chn].benderRange != d2) {
1334 channel[chn].benderRange = d2;
1335 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1336 if (voice[i].channel == chn) {
1337 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1341 break;
1343 case 0x7F7F:
1344 channel[chn].benderRange = 2;
1345 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1346 if (voice[i].channel == chn) {
1347 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1350 break;
1351 default:
1352 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1353 channel[chn].regPmtMSB, channel[chn].regPmtLSB,
1354 channel[chn].nrgPmtMSB, channel[chn].nrgPmtLSB,
1355 d2);
1356 break;
1358 break;
1360 case 0x78: /* all sounds off */
1361 /* FIXME: I don't know if I have to take care of the channel
1362 * for this control ?
1364 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1365 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1366 voice[i].status = sVS_UNUSED;
1367 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1370 break;
1371 case 0x7B: /* all notes off */
1372 /* FIXME: I don't know if I have to take care of the channel
1373 * for this control ?
1375 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1376 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1377 voice[i].status = sVS_UNUSED;
1378 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1381 break;
1382 default:
1383 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1384 d1, d2, chn);
1385 break;
1387 break;
1388 case MIDI_PGM_CHANGE:
1389 channel[chn].program = d1;
1390 break;
1391 case MIDI_CHN_PRESSURE:
1392 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1393 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1394 SEQ_KEY_PRESSURE(wDevID, i, voice[i].note, d1);
1397 break;
1398 case MIDI_PITCH_BEND:
1399 channel[chn].bender = (d2 << 7) + d1;
1400 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1401 if (voice[i].channel == chn) {
1402 SEQ_BENDER(wDevID, i, channel[chn].bender);
1405 break;
1406 case MIDI_SYSTEM_PREFIX:
1407 switch (evt & 0x0F) {
1408 case 0x0F: /* Reset */
1409 modFMReset(wDevID);
1410 break;
1411 default:
1412 WARN("Unsupported (yet) system event %02x\n", evt & 0x0F);
1414 break;
1415 default:
1416 WARN("Internal error, shouldn't happen (event=%08x)\n", evt & 0xF0);
1417 return MMSYSERR_NOTENABLED;
1420 break;
1421 case MOD_MIDIPORT:
1423 int dev = wDevID - MODM_NumFMSynthDevs;
1424 if (dev < 0) {
1425 WARN("Internal error on devID (%u) !\n", wDevID);
1426 return MIDIERR_NODEVICE;
1429 switch (evt & 0xF0) {
1430 case MIDI_NOTEOFF:
1431 case MIDI_NOTEON:
1432 case MIDI_KEY_PRESSURE:
1433 case MIDI_CTL_CHANGE:
1434 case MIDI_PITCH_BEND:
1435 SEQ_MIDIOUT(dev, evt);
1436 SEQ_MIDIOUT(dev, d1);
1437 SEQ_MIDIOUT(dev, d2);
1438 break;
1439 case MIDI_PGM_CHANGE:
1440 case MIDI_CHN_PRESSURE:
1441 SEQ_MIDIOUT(dev, evt);
1442 SEQ_MIDIOUT(dev, d1);
1443 break;
1444 case MIDI_SYSTEM_PREFIX:
1445 switch (evt & 0x0F) {
1446 case 0x00: /* System Exclusive, don't do it on modData,
1447 * should require modLongData*/
1448 case 0x01: /* Undefined */
1449 case 0x04: /* Undefined. */
1450 case 0x05: /* Undefined. */
1451 case 0x07: /* End of Exclusive. */
1452 case 0x09: /* Undefined. */
1453 case 0x0D: /* Undefined. */
1454 break;
1455 case 0x06: /* Tune Request */
1456 case 0x08: /* Timing Clock. */
1457 case 0x0A: /* Start. */
1458 case 0x0B: /* Continue */
1459 case 0x0C: /* Stop */
1460 case 0x0E: /* Active Sensing. */
1461 SEQ_MIDIOUT(dev, evt);
1462 break;
1463 case 0x0F: /* Reset */
1464 /* SEQ_MIDIOUT(dev, evt);
1465 this other way may be better */
1466 SEQ_MIDIOUT(dev, MIDI_SYSTEM_PREFIX);
1467 SEQ_MIDIOUT(dev, 0x7e);
1468 SEQ_MIDIOUT(dev, 0x7f);
1469 SEQ_MIDIOUT(dev, 0x09);
1470 SEQ_MIDIOUT(dev, 0x01);
1471 SEQ_MIDIOUT(dev, 0xf7);
1472 break;
1473 case 0x03: /* Song Select. */
1474 SEQ_MIDIOUT(dev, evt);
1475 SEQ_MIDIOUT(dev, d1);
1476 case 0x02: /* Song Position Pointer. */
1477 SEQ_MIDIOUT(dev, evt);
1478 SEQ_MIDIOUT(dev, d1);
1479 SEQ_MIDIOUT(dev, d2);
1481 break;
1484 break;
1485 default:
1486 WARN("Technology not supported (yet) %d !\n",
1487 MidiOutDev[wDevID].caps.wTechnology);
1488 return MMSYSERR_NOTENABLED;
1491 SEQ_DUMPBUF();
1493 return MMSYSERR_NOERROR;
1496 /**************************************************************************
1497 * modLongData [internal]
1499 static DWORD modLongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1501 int count;
1502 LPBYTE lpData;
1504 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
1506 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
1507 * but it seems to be used only for midi input.
1508 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
1511 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1512 if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1514 if (midiSeqFD == -1) {
1515 WARN("can't play !\n");
1516 return MIDIERR_NODEVICE;
1519 lpData = (LPBYTE) lpMidiHdr->lpData;
1521 if (lpData == NULL)
1522 return MIDIERR_UNPREPARED;
1523 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1524 return MIDIERR_UNPREPARED;
1525 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1526 return MIDIERR_STILLPLAYING;
1527 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1528 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1530 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1531 * data, or can it also contain raw MIDI data, to be split up and sent to
1532 * modShortData() ?
1533 * If the latter is true, then the following WARNing will fire up
1535 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
1536 WARN("The allegedly system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1539 TRACE("dwBufferLength=%u !\n", lpMidiHdr->dwBufferLength);
1540 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1541 lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
1542 lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
1544 switch (MidiOutDev[wDevID].caps.wTechnology) {
1545 case MOD_FMSYNTH:
1546 /* FIXME: I don't think there is much to do here */
1547 break;
1548 case MOD_MIDIPORT:
1549 if (lpData[0] != 0xF0) {
1550 /* Send end of System Exclusive */
1551 SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, 0xF0);
1552 WARN("Adding missing 0xF0 marker at the beginning of "
1553 "system exclusive byte stream\n");
1555 for (count = 0; count < lpMidiHdr->dwBufferLength; count++) {
1556 SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, lpData[count]);
1558 if (lpData[count - 1] != 0xF7) {
1559 /* Send end of System Exclusive */
1560 SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, 0xF7);
1561 WARN("Adding missing 0xF7 marker at the end of "
1562 "system exclusive byte stream\n");
1564 SEQ_DUMPBUF();
1565 break;
1566 default:
1567 WARN("Technology not supported (yet) %d !\n",
1568 MidiOutDev[wDevID].caps.wTechnology);
1569 return MMSYSERR_NOTENABLED;
1572 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1573 lpMidiHdr->dwFlags |= MHDR_DONE;
1574 MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD_PTR)lpMidiHdr, 0L);
1575 return MMSYSERR_NOERROR;
1578 /**************************************************************************
1579 * modPrepare [internal]
1581 static DWORD modPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1583 TRACE("(%04X, %p, %d);\n", wDevID, lpMidiHdr, dwSize);
1585 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 || lpMidiHdr->lpData == 0)
1586 return MMSYSERR_INVALPARAM;
1587 if (lpMidiHdr->dwFlags & MHDR_PREPARED)
1588 return MMSYSERR_NOERROR;
1590 lpMidiHdr->lpNext = 0;
1591 lpMidiHdr->dwFlags |= MHDR_PREPARED;
1592 lpMidiHdr->dwFlags &= ~(MHDR_DONE|MHDR_INQUEUE); /* flags cleared since w2k */
1593 return MMSYSERR_NOERROR;
1596 /**************************************************************************
1597 * modUnprepare [internal]
1599 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1601 TRACE("(%04X, %p, %d);\n", wDevID, lpMidiHdr, dwSize);
1603 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 || lpMidiHdr->lpData == 0)
1604 return MMSYSERR_INVALPARAM;
1605 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1606 return MMSYSERR_NOERROR;
1607 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1608 return MIDIERR_STILLPLAYING;
1609 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
1610 return MMSYSERR_NOERROR;
1613 /**************************************************************************
1614 * modGetVolume [internal]
1616 static DWORD modGetVolume(WORD wDevID, DWORD* lpdwVolume)
1618 if (!lpdwVolume) return MMSYSERR_INVALPARAM;
1619 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1620 *lpdwVolume = 0xFFFFFFFF;
1621 return (MidiOutDev[wDevID].caps.dwSupport & MIDICAPS_VOLUME) ? 0 : MMSYSERR_NOTSUPPORTED;
1624 /**************************************************************************
1625 * modReset [internal]
1627 static DWORD modReset(WORD wDevID)
1629 unsigned chn;
1631 TRACE("(%04X);\n", wDevID);
1633 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1634 if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1636 /* stop all notes */
1637 /* FIXME: check if 0x78B0 is channel dependent or not. I coded it so that
1638 * it's channel dependent...
1640 for (chn = 0; chn < 16; chn++) {
1641 /* turn off every note */
1642 modData(wDevID, 0x7800 | MIDI_CTL_CHANGE | chn);
1643 /* remove sustain on all channels */
1644 modData(wDevID, (CTL_SUSTAIN << 8) | MIDI_CTL_CHANGE | chn);
1646 /* FIXME: the LongData buffers must also be returned to the app */
1647 return MMSYSERR_NOERROR;
1650 #endif /* HAVE_OSS_MIDI */
1652 /*======================================================================*
1653 * MIDI entry points *
1654 *======================================================================*/
1656 /**************************************************************************
1657 * midMessage (WINEOSS.@)
1659 DWORD WINAPI OSS_midMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1660 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1662 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1663 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1664 switch (wMsg) {
1665 #ifdef HAVE_OSS_MIDI
1666 case DRVM_INIT:
1667 return OSS_MidiInit();
1668 case DRVM_EXIT:
1669 return OSS_MidiExit();
1670 case DRVM_ENABLE:
1671 case DRVM_DISABLE:
1672 /* FIXME: Pretend this is supported */
1673 return 0;
1674 case MIDM_OPEN:
1675 return midOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1676 case MIDM_CLOSE:
1677 return midClose(wDevID);
1678 case MIDM_ADDBUFFER:
1679 return midAddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1680 case MIDM_PREPARE:
1681 return midPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1682 case MIDM_UNPREPARE:
1683 return midUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1684 case MIDM_GETDEVCAPS:
1685 return midGetDevCaps(wDevID, (LPMIDIINCAPSW)dwParam1,dwParam2);
1686 case MIDM_GETNUMDEVS:
1687 return MIDM_NumDevs;
1688 case MIDM_RESET:
1689 return midReset(wDevID);
1690 case MIDM_START:
1691 return midStart(wDevID);
1692 case MIDM_STOP:
1693 return midStop(wDevID);
1694 #else
1695 case DRVM_INIT:
1696 case MIDM_GETNUMDEVS:
1697 return 0;
1698 #endif
1699 default:
1700 TRACE("Unsupported message\n");
1702 return MMSYSERR_NOTSUPPORTED;
1705 /**************************************************************************
1706 * modMessage (WINEOSS.@)
1708 DWORD WINAPI OSS_modMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1709 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1711 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1712 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1714 switch (wMsg) {
1715 #ifdef HAVE_OSS_MIDI
1716 case DRVM_INIT:
1717 return OSS_MidiInit();
1718 case DRVM_EXIT:
1719 return OSS_MidiExit();
1720 case DRVM_ENABLE:
1721 case DRVM_DISABLE:
1722 /* FIXME: Pretend this is supported */
1723 return 0;
1724 case MODM_OPEN:
1725 return modOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1726 case MODM_CLOSE:
1727 return modClose(wDevID);
1728 case MODM_DATA:
1729 return modData(wDevID, dwParam1);
1730 case MODM_LONGDATA:
1731 return modLongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1732 case MODM_PREPARE:
1733 return modPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1734 case MODM_UNPREPARE:
1735 return modUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1736 case MODM_GETDEVCAPS:
1737 return modGetDevCaps(wDevID, (LPMIDIOUTCAPSW)dwParam1, dwParam2);
1738 case MODM_GETNUMDEVS:
1739 return MODM_NumDevs;
1740 case MODM_GETVOLUME:
1741 return modGetVolume(wDevID, (DWORD*)dwParam1);
1742 case MODM_SETVOLUME:
1743 return 0;
1744 case MODM_RESET:
1745 return modReset(wDevID);
1746 #else
1747 case DRVM_INIT:
1748 case MODM_GETNUMDEVS:
1749 return 0;
1750 #endif
1751 default:
1752 TRACE("Unsupported message\n");
1754 return MMSYSERR_NOTSUPPORTED;
1757 /**************************************************************************
1758 * DriverProc (WINEOSS.1)
1760 LRESULT CALLBACK OSS_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1761 LPARAM dwParam1, LPARAM dwParam2)
1763 TRACE("(%08lX, %p, %08X, %08lX, %08lX)\n",
1764 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1766 switch(wMsg) {
1767 case DRV_LOAD:
1768 case DRV_FREE:
1769 case DRV_OPEN:
1770 case DRV_CLOSE:
1771 case DRV_ENABLE:
1772 case DRV_DISABLE:
1773 case DRV_QUERYCONFIGURE:
1774 case DRV_CONFIGURE:
1775 return 1;
1776 case DRV_INSTALL:
1777 case DRV_REMOVE:
1778 return DRV_SUCCESS;
1779 default:
1780 return 0;