kernel32/tests: Add more partial DBCS character tests that contains a null character.
[wine.git] / dlls / wineoss.drv / midi.c
blob3a2ab2580d673d80d94f245c70205075ed0811fa
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
78 typedef struct {
79 int state; /* -1 disabled, 0 is no recording started, 1 in recording, bit 2 set if in sys exclusive recording */
80 DWORD bufsize;
81 MIDIOPENDESC midiDesc;
82 WORD wFlags;
83 LPMIDIHDR lpQueueHdr;
84 DWORD dwTotalPlayed;
85 unsigned char incoming[3];
86 unsigned char incPrev;
87 char incLen;
88 DWORD startTime;
89 MIDIINCAPSW caps;
90 } WINE_MIDIIN;
92 typedef struct {
93 BOOL bEnabled;
94 DWORD bufsize;
95 MIDIOPENDESC midiDesc;
96 WORD wFlags;
97 LPMIDIHDR lpQueueHdr;
98 DWORD dwTotalPlayed;
99 void* lpExtra; /* according to port type (MIDI, FM...), extra data when needed */
100 MIDIOUTCAPSW caps;
101 } WINE_MIDIOUT;
103 static WINE_MIDIIN MidiInDev [MAX_MIDIINDRV ];
104 static WINE_MIDIOUT MidiOutDev[MAX_MIDIOUTDRV];
106 /* this is the total number of MIDI out devices found (synth and port) */
107 static int MODM_NumDevs = 0;
108 /* this is the number of FM synthesizers (index from 0 to NUMFMSYNTHDEVS - 1) */
109 static int MODM_NumFMSynthDevs = 0;
110 /* the Midi ports have index from NUMFMSYNTHDEVS to NumDevs - 1 */
112 /* this is the total number of MIDI out devices found */
113 static int MIDM_NumDevs = 0;
115 static int midiSeqFD = -1;
116 static int numOpenMidiSeq = 0;
117 static int numStartedMidiIn = 0;
119 static CRITICAL_SECTION crit_sect; /* protects all MidiIn buffers queues */
120 static CRITICAL_SECTION_DEBUG critsect_debug =
122 0, 0, &crit_sect,
123 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
124 0, 0, { (DWORD_PTR)(__FILE__ ": crit_sect") }
126 static CRITICAL_SECTION crit_sect = { &critsect_debug, -1, 0, 0, 0, 0 };
128 static int end_thread;
129 static HANDLE hThread;
131 /*======================================================================*
132 * Low level MIDI implementation *
133 *======================================================================*/
135 static int midiOpenSeq(void);
136 static int midiCloseSeq(void);
138 /**************************************************************************
139 * MIDI_unixToWindowsDeviceType [internal]
141 * return the Windows equivalent to a Unix Device Type
144 static int MIDI_UnixToWindowsDeviceType(int type)
146 /* MOD_MIDIPORT output port
147 * MOD_SYNTH generic internal synth
148 * MOD_SQSYNTH square wave internal synth
149 * MOD_FMSYNTH FM internal synth
150 * MOD_MAPPER MIDI mapper
151 * MOD_WAVETABLE hardware wavetable internal synth
152 * MOD_SWSYNTH software internal synth
155 /* FIXME Is this really the correct equivalence from UNIX to
156 Windows Sound type */
158 switch (type) {
159 case SYNTH_TYPE_FM: return MOD_FMSYNTH;
160 case SYNTH_TYPE_SAMPLE: return MOD_SYNTH;
161 case SYNTH_TYPE_MIDI: return MOD_MIDIPORT;
162 default:
163 ERR("Cannot determine the type of this midi device. "
164 "Assuming FM Synth\n");
165 return MOD_FMSYNTH;
169 static int MIDI_loadcount;
170 /**************************************************************************
171 * OSS_MidiInit [internal]
173 * Initializes the MIDI devices information variables
175 static LRESULT OSS_MidiInit(void)
177 int i, status, numsynthdevs = 255, nummididevs = 255;
178 struct synth_info sinfo;
179 struct midi_info minfo;
181 TRACE("(%i)\n", MIDI_loadcount);
182 if (MIDI_loadcount++)
183 return 1;
185 TRACE("Initializing the MIDI variables.\n");
187 /* try to open device */
188 if (midiOpenSeq() == -1) {
189 return -1;
192 /* find how many Synth devices are there in the system */
193 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRSYNTHS, &numsynthdevs);
195 if (status == -1) {
196 ERR("ioctl for nr synth failed.\n");
197 midiCloseSeq();
198 return -1;
201 if (numsynthdevs > MAX_MIDIOUTDRV) {
202 ERR("MAX_MIDIOUTDRV (%d) was enough for the number of devices (%d). "
203 "Some FM devices will not be available.\n",MAX_MIDIOUTDRV,numsynthdevs);
204 numsynthdevs = MAX_MIDIOUTDRV;
207 for (i = 0; i < numsynthdevs; i++) {
208 /* Manufac ID. We do not have access to this with soundcard.h
209 * Does not seem to be a problem, because in mmsystem.h only
210 * Microsoft's ID is listed.
212 MidiOutDev[i].caps.wMid = 0x00FF;
213 MidiOutDev[i].caps.wPid = 0x0001; /* FIXME Product ID */
214 /* Product Version. We simply say "1" */
215 MidiOutDev[i].caps.vDriverVersion = 0x001;
216 /* The following are mandatory for MOD_MIDIPORT */
217 MidiOutDev[i].caps.wChannelMask = 0xFFFF;
218 MidiOutDev[i].caps.wVoices = 0;
219 MidiOutDev[i].caps.wNotes = 0;
220 MidiOutDev[i].caps.dwSupport = 0;
222 sinfo.device = i;
223 status = ioctl(midiSeqFD, SNDCTL_SYNTH_INFO, &sinfo);
224 if (status == -1) {
225 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};
226 ERR("ioctl for synth info failed on %d, disabling it.\n", i);
228 wsprintfW(MidiOutDev[i].caps.szPname, fmt, i);
230 MidiOutDev[i].caps.wTechnology = MOD_MIDIPORT;
231 MidiOutDev[i].bEnabled = FALSE;
232 } else {
233 MultiByteToWideChar( CP_UNIXCP, 0, sinfo.name, -1,
234 MidiOutDev[i].caps.szPname,
235 sizeof(MidiOutDev[i].caps.szPname)/sizeof(WCHAR) );
236 MidiOutDev[i].caps.wTechnology = MIDI_UnixToWindowsDeviceType(sinfo.synth_type);
238 if (MOD_MIDIPORT != MidiOutDev[i].caps.wTechnology) {
239 /* FIXME Do we have this information?
240 * Assuming the soundcards can handle
241 * MIDICAPS_VOLUME and MIDICAPS_LRVOLUME but
242 * not MIDICAPS_CACHE.
244 MidiOutDev[i].caps.dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
245 MidiOutDev[i].caps.wVoices = sinfo.nr_voices;
247 /* FIXME Is it possible to know the maximum
248 * number of simultaneous notes of a soundcard ?
249 * I believe we don't have this information, but
250 * it's probably equal or more than wVoices
252 MidiOutDev[i].caps.wNotes = sinfo.nr_voices;
254 MidiOutDev[i].bEnabled = TRUE;
256 /* We also have the information sinfo.synth_subtype, not used here
258 if (sinfo.capabilities & SYNTH_CAP_INPUT) {
259 FIXME("Synthesizer supports MIDI in. Not yet supported.\n");
261 TRACE("SynthOut[%d]\tOSS info: synth type=%d/%d capa=%lx\n",
262 i, sinfo.synth_type, sinfo.synth_subtype, (long)sinfo.capabilities);
265 TRACE("SynthOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%d\n",
266 i, wine_dbgstr_w(MidiOutDev[i].caps.szPname),
267 MidiOutDev[i].caps.wTechnology,
268 MidiOutDev[i].caps.wVoices, MidiOutDev[i].caps.wNotes,
269 MidiOutDev[i].caps.wChannelMask, MidiOutDev[i].caps.dwSupport);
272 /* find how many MIDI devices are there in the system */
273 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRMIDIS, &nummididevs);
274 if (status == -1) {
275 ERR("ioctl on nr midi failed.\n");
276 nummididevs = 0;
277 goto wrapup;
280 /* FIXME: the two restrictions below could be loosened in some cases */
281 if (numsynthdevs + nummididevs > MAX_MIDIOUTDRV) {
282 ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
283 "Some MIDI devices will not be available.\n");
284 nummididevs = MAX_MIDIOUTDRV - numsynthdevs;
287 if (nummididevs > MAX_MIDIINDRV) {
288 ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
289 "Some MIDI devices will not be available.\n",MAX_MIDIINDRV,nummididevs);
290 nummididevs = MAX_MIDIINDRV;
293 for (i = 0; i < nummididevs; i++) {
294 minfo.device = i;
295 status = ioctl(midiSeqFD, SNDCTL_MIDI_INFO, &minfo);
296 if (status == -1) WARN("ioctl on midi info for device %d failed.\n", i);
298 /* This whole part is somewhat obscure to me. I'll keep trying to dig
299 info about it. If you happen to know, please tell us. The very
300 descriptive minfo.dev_type was not used here.
302 /* Manufacturer ID. We do not have access to this with soundcard.h
303 Does not seem to be a problem, because in mmsystem.h only
304 Microsoft's ID is listed */
305 MidiOutDev[numsynthdevs + i].caps.wMid = 0x00FF;
306 MidiOutDev[numsynthdevs + i].caps.wPid = 0x0001; /* FIXME Product ID */
307 /* Product Version. We simply say "1" */
308 MidiOutDev[numsynthdevs + i].caps.vDriverVersion = 0x001;
309 if (status == -1) {
310 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};
311 wsprintfW(MidiOutDev[numsynthdevs + i].caps.szPname, fmt, numsynthdevs + i);
312 MidiOutDev[numsynthdevs + i].bEnabled = FALSE;
313 } else {
314 MultiByteToWideChar(CP_UNIXCP, 0, minfo.name, -1,
315 MidiOutDev[numsynthdevs + i].caps.szPname,
316 sizeof(MidiOutDev[numsynthdevs + i].caps.szPname) / sizeof(WCHAR));
317 MidiOutDev[numsynthdevs + i].bEnabled = TRUE;
319 MidiOutDev[numsynthdevs + i].caps.wTechnology = MOD_MIDIPORT;
320 MidiOutDev[numsynthdevs + i].caps.wVoices = 0;
321 MidiOutDev[numsynthdevs + i].caps.wNotes = 0;
322 MidiOutDev[numsynthdevs + i].caps.wChannelMask= 0xFFFF;
323 MidiOutDev[numsynthdevs + i].caps.dwSupport = 0;
325 /* This whole part is somewhat obscure to me. I'll keep trying to dig
326 info about it. If you happen to know, please tell us. The very
327 descriptive minfo.dev_type was not used here.
329 /* Manufac ID. We do not have access to this with soundcard.h
330 Does not seem to be a problem, because in mmsystem.h only
331 Microsoft's ID is listed */
332 MidiInDev[i].caps.wMid = 0x00FF;
333 MidiInDev[i].caps.wPid = 0x0001; /* FIXME Product ID */
334 /* Product Version. We simply say "1" */
335 MidiInDev[i].caps.vDriverVersion = 0x001;
336 if (status == -1) {
337 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};
338 wsprintfW(MidiInDev[i].caps.szPname, fmt, numsynthdevs + i);
339 MidiInDev[i].state = -1;
340 } else {
341 MultiByteToWideChar(CP_UNIXCP, 0, minfo.name, -1,
342 MidiInDev[i].caps.szPname,
343 sizeof(MidiInDev[i].caps.szPname) / sizeof(WCHAR));
344 MidiInDev[i].state = 0;
346 MidiInDev[i].caps.dwSupport = 0; /* mandatory with MIDIINCAPS */
348 TRACE("OSS info: midi[%d] dev-type=%d capa=%lx\n"
349 "\tMidiOut[%d] name='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%d\n"
350 "\tMidiIn [%d] name='%s' support=%d\n",
351 i, minfo.dev_type, (long)minfo.capabilities,
352 numsynthdevs + i, wine_dbgstr_w(MidiOutDev[numsynthdevs + i].caps.szPname),
353 MidiOutDev[numsynthdevs + i].caps.wTechnology,
354 MidiOutDev[numsynthdevs + i].caps.wVoices, MidiOutDev[numsynthdevs + i].caps.wNotes,
355 MidiOutDev[numsynthdevs + i].caps.wChannelMask, MidiOutDev[numsynthdevs + i].caps.dwSupport,
356 i, wine_dbgstr_w(MidiInDev[i].caps.szPname), MidiInDev[i].caps.dwSupport);
359 wrapup:
360 /* windows does not seem to differentiate Synth from MIDI devices */
361 MODM_NumFMSynthDevs = numsynthdevs;
362 MODM_NumDevs = numsynthdevs + nummididevs;
364 MIDM_NumDevs = nummididevs;
366 /* close file and exit */
367 midiCloseSeq();
369 return 0;
372 /**************************************************************************
373 * OSS_MidiExit [internal]
375 * Release the MIDI devices information variables
377 static LRESULT OSS_MidiExit(void)
379 TRACE("(%i)\n", MIDI_loadcount);
381 if (--MIDI_loadcount)
382 return 1;
384 ZeroMemory(MidiInDev, sizeof(MidiInDev));
385 ZeroMemory(MidiOutDev, sizeof(MidiOutDev));
387 MODM_NumDevs = 0;
388 MODM_NumFMSynthDevs = 0;
389 MIDM_NumDevs = 0;
391 return 0;
394 /**************************************************************************
395 * MIDI_NotifyClient [internal]
397 static void MIDI_NotifyClient(UINT wDevID, WORD wMsg,
398 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
400 DWORD_PTR dwCallBack;
401 UINT uFlags;
402 HANDLE hDev;
403 DWORD_PTR dwInstance;
405 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",
406 wDevID, wMsg, dwParam1, dwParam2);
408 switch (wMsg) {
409 case MOM_OPEN:
410 case MOM_CLOSE:
411 case MOM_DONE:
412 case MOM_POSITIONCB:
413 if (wDevID > MODM_NumDevs) return;
415 dwCallBack = MidiOutDev[wDevID].midiDesc.dwCallback;
416 uFlags = MidiOutDev[wDevID].wFlags;
417 hDev = MidiOutDev[wDevID].midiDesc.hMidi;
418 dwInstance = MidiOutDev[wDevID].midiDesc.dwInstance;
419 break;
421 case MIM_OPEN:
422 case MIM_CLOSE:
423 case MIM_DATA:
424 case MIM_LONGDATA:
425 case MIM_ERROR:
426 case MIM_LONGERROR:
427 case MIM_MOREDATA:
428 if (wDevID > MIDM_NumDevs) return;
430 dwCallBack = MidiInDev[wDevID].midiDesc.dwCallback;
431 uFlags = MidiInDev[wDevID].wFlags;
432 hDev = MidiInDev[wDevID].midiDesc.hMidi;
433 dwInstance = MidiInDev[wDevID].midiDesc.dwInstance;
434 break;
435 default:
436 ERR("Unsupported MSW-MIDI message %u\n", wMsg);
437 return;
440 DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2);
443 static int midi_warn = 1;
444 /**************************************************************************
445 * midiOpenSeq [internal]
447 static int midiOpenSeq(void)
449 if (numOpenMidiSeq == 0) {
450 const char* device;
451 device=getenv("MIDIDEV");
452 if (!device) device="/dev/sequencer";
453 midiSeqFD = open(device, O_RDWR, 0);
454 if (midiSeqFD == -1) {
455 if (midi_warn)
457 WARN("Can't open MIDI device '%s' ! (%s). If your "
458 "program needs this (probably not): %s\n",
459 device, strerror(errno),
460 errno == ENOENT ?
461 "create it ! (\"man MAKEDEV\" ?)" :
462 errno == ENODEV ?
463 "load MIDI sequencer kernel driver !" :
464 errno == EACCES ?
465 "grant access ! (\"man chmod\")" : ""
468 midi_warn = 0;
469 return -1;
471 #if 0
472 if (fcntl(midiSeqFD, F_SETFL, O_NONBLOCK) < 0) {
473 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno, strerror(errno));
474 close(midiSeqFD);
475 midiSeqFD = -1;
476 return -1;
478 #endif
479 fcntl(midiSeqFD, F_SETFD, 1); /* set close on exec flag */
480 ioctl(midiSeqFD, SNDCTL_SEQ_RESET);
482 numOpenMidiSeq++;
483 return 0;
486 /**************************************************************************
487 * midiCloseSeq [internal]
489 static int midiCloseSeq(void)
491 if (--numOpenMidiSeq == 0) {
492 close(midiSeqFD);
493 midiSeqFD = -1;
495 return 0;
498 /* FIXME: this is a bad idea, it's even not static... */
499 SEQ_DEFINEBUF(1024);
501 /* FIXME: this is not reentrant, not static - because of global variable
502 * _seqbuf and al.
504 /**************************************************************************
505 * seqbuf_dump [internal]
507 * Used by SEQ_DUMPBUF to flush the buffer.
510 void seqbuf_dump(void)
512 if (_seqbufptr) {
513 if (write(midiSeqFD, _seqbuf, _seqbufptr) == -1) {
514 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
515 midiSeqFD, errno, strerror(errno));
517 /* FIXME:
518 * in any case buffer is lost so that if many errors occur the buffer
519 * will not overrun
521 _seqbufptr = 0;
525 /**************************************************************************
526 * midReceiveChar [internal]
528 static void midReceiveChar(WORD wDevID, unsigned char value, DWORD dwTime)
530 DWORD toSend = 0;
532 TRACE("Adding %02xh to %d[%d]\n", value, wDevID, MidiInDev[wDevID].incLen);
534 if (wDevID >= MIDM_NumDevs) {
535 WARN("bad devID\n");
536 return;
538 if (MidiInDev[wDevID].state <= 0) {
539 TRACE("disabled or input not started, thrown away\n");
540 return;
543 if (MidiInDev[wDevID].state & 2) { /* system exclusive */
544 LPMIDIHDR lpMidiHdr;
545 BOOL sbfb = FALSE;
547 EnterCriticalSection(&crit_sect);
548 if ((lpMidiHdr = MidiInDev[wDevID].lpQueueHdr) != NULL) {
549 LPBYTE lpData = (LPBYTE) lpMidiHdr->lpData;
551 lpData[lpMidiHdr->dwBytesRecorded++] = value;
552 if (lpMidiHdr->dwBytesRecorded == lpMidiHdr->dwBufferLength) {
553 sbfb = TRUE;
556 if (value == 0xF7) { /* then end */
557 MidiInDev[wDevID].state &= ~2;
558 sbfb = TRUE;
560 if (sbfb && lpMidiHdr != NULL) {
561 lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
562 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr->lpNext;
563 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
564 lpMidiHdr->dwFlags |= MHDR_DONE;
565 MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD_PTR)lpMidiHdr, dwTime);
567 LeaveCriticalSection(&crit_sect);
568 return;
571 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
572 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
574 if (!IS_CMD(value) && MidiInDev[wDevID].incLen == 0) { /* try to reuse old cmd */
575 if (IS_CMD(MidiInDev[wDevID].incPrev) && !IS_SYS_CMD(MidiInDev[wDevID].incPrev)) {
576 MidiInDev[wDevID].incoming[0] = MidiInDev[wDevID].incPrev;
577 MidiInDev[wDevID].incLen = 1;
578 TRACE("Reusing old command %02xh\n", MidiInDev[wDevID].incPrev);
579 } else {
580 FIXME("error for midi-in, should generate MIM_ERROR notification:"
581 " prev=%02Xh, incLen=%02Xh\n",
582 MidiInDev[wDevID].incPrev, MidiInDev[wDevID].incLen);
583 return;
586 MidiInDev[wDevID].incoming[(int)(MidiInDev[wDevID].incLen++)] = value;
587 if (MidiInDev[wDevID].incLen == 1 && !IS_SYS_CMD(MidiInDev[wDevID].incoming[0])) {
588 /* store new cmd, just in case */
589 MidiInDev[wDevID].incPrev = MidiInDev[wDevID].incoming[0];
592 #undef IS_CMD
593 #undef IS_SYS_CMD
595 switch (MidiInDev[wDevID].incoming[0] & 0xF0) {
596 case MIDI_NOTEOFF:
597 case MIDI_NOTEON:
598 case MIDI_KEY_PRESSURE:
599 case MIDI_CTL_CHANGE:
600 case MIDI_PITCH_BEND:
601 if (MidiInDev[wDevID].incLen == 3) {
602 toSend = (MidiInDev[wDevID].incoming[2] << 16) |
603 (MidiInDev[wDevID].incoming[1] << 8) |
604 (MidiInDev[wDevID].incoming[0] << 0);
606 break;
607 case MIDI_PGM_CHANGE:
608 case MIDI_CHN_PRESSURE:
609 if (MidiInDev[wDevID].incLen == 2) {
610 toSend = (MidiInDev[wDevID].incoming[1] << 8) |
611 (MidiInDev[wDevID].incoming[0] << 0);
613 break;
614 case MIDI_SYSTEM_PREFIX:
615 if (MidiInDev[wDevID].incoming[0] == 0xF0) {
616 MidiInDev[wDevID].state |= 2;
617 MidiInDev[wDevID].incLen = 0;
618 } else {
619 if (MidiInDev[wDevID].incLen == 1) {
620 toSend = (MidiInDev[wDevID].incoming[0] << 0);
623 break;
624 default:
625 WARN("This shouldn't happen (%02X)\n", MidiInDev[wDevID].incoming[0]);
627 if (toSend != 0) {
628 TRACE("Sending event %08x\n", toSend);
629 MidiInDev[wDevID].incLen = 0;
630 dwTime -= MidiInDev[wDevID].startTime;
631 MIDI_NotifyClient(wDevID, MIM_DATA, toSend, dwTime);
635 static DWORD WINAPI midRecThread(LPVOID arg)
637 unsigned char buffer[256];
638 int len, idx;
639 DWORD dwTime;
640 struct pollfd pfd;
642 TRACE("Thread startup\n");
644 pfd.fd = midiSeqFD;
645 pfd.events = POLLIN;
647 while(!end_thread) {
648 TRACE("Thread loop\n");
650 /* Check if an event is present */
651 if (poll(&pfd, 1, 250) <= 0)
652 continue;
654 len = read(midiSeqFD, buffer, sizeof(buffer));
655 TRACE("Received %d bytes\n", len);
657 if (len < 0) continue;
658 if ((len % 4) != 0) {
659 WARN("Bad length %d, errno %d (%s)\n", len, errno, strerror(errno));
660 continue;
663 dwTime = GetTickCount();
665 for (idx = 0; idx < len; ) {
666 if (buffer[idx] & 0x80) {
667 TRACE(
668 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
669 buffer[idx + 0], buffer[idx + 1],
670 buffer[idx + 2], buffer[idx + 3],
671 buffer[idx + 4], buffer[idx + 5],
672 buffer[idx + 6], buffer[idx + 7]);
673 idx += 8;
674 } else {
675 switch (buffer[idx + 0]) {
676 case SEQ_WAIT:
677 case SEQ_ECHO:
678 break;
679 case SEQ_MIDIPUTC:
680 midReceiveChar(buffer[idx + 2], buffer[idx + 1], dwTime);
681 break;
682 default:
683 TRACE("Unsupported event %d\n", buffer[idx + 0]);
684 break;
686 idx += 4;
690 return 0;
693 /**************************************************************************
694 * midGetDevCaps [internal]
696 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPSW lpCaps, DWORD dwSize)
698 TRACE("(%04X, %p, %08X);\n", wDevID, lpCaps, dwSize);
700 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
701 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
703 memcpy(lpCaps, &MidiInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
705 return MMSYSERR_NOERROR;
708 /**************************************************************************
709 * midOpen [internal]
711 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
713 TRACE("(%04X, %p, %08X);\n", wDevID, lpDesc, dwFlags);
715 if (lpDesc == NULL) {
716 WARN("Invalid Parameter !\n");
717 return MMSYSERR_INVALPARAM;
720 /* FIXME :
721 * how to check that content of lpDesc is correct ?
723 if (wDevID >= MIDM_NumDevs) {
724 WARN("wDevID too large (%u) !\n", wDevID);
725 return MMSYSERR_BADDEVICEID;
727 if (MidiInDev[wDevID].state == -1) {
728 WARN("device disabled\n");
729 return MIDIERR_NODEVICE;
731 if (MidiInDev[wDevID].midiDesc.hMidi != 0) {
732 WARN("device already open !\n");
733 return MMSYSERR_ALLOCATED;
735 if ((dwFlags & MIDI_IO_STATUS) != 0) {
736 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
737 dwFlags &= ~MIDI_IO_STATUS;
739 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
740 FIXME("Bad dwFlags\n");
741 return MMSYSERR_INVALFLAG;
744 if (midiOpenSeq() < 0) {
745 return MMSYSERR_ERROR;
748 if (numStartedMidiIn++ == 0) {
749 end_thread = 0;
750 hThread = CreateThread(NULL, 0, midRecThread, NULL, 0, NULL);
751 if (!hThread) {
752 numStartedMidiIn = 0;
753 WARN("Couldn't create thread for midi-in\n");
754 midiCloseSeq();
755 return MMSYSERR_ERROR;
757 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
758 TRACE("Created thread for midi-in\n");
761 MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
763 MidiInDev[wDevID].lpQueueHdr = NULL;
764 MidiInDev[wDevID].dwTotalPlayed = 0;
765 MidiInDev[wDevID].bufsize = 0x3FFF;
766 MidiInDev[wDevID].midiDesc = *lpDesc;
767 MidiInDev[wDevID].state = 0;
768 MidiInDev[wDevID].incLen = 0;
769 MidiInDev[wDevID].startTime = 0;
771 MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L);
772 return MMSYSERR_NOERROR;
775 /**************************************************************************
776 * midClose [internal]
778 static DWORD midClose(WORD wDevID)
780 int ret = MMSYSERR_NOERROR;
782 TRACE("(%04X);\n", wDevID);
784 if (wDevID >= MIDM_NumDevs) {
785 WARN("wDevID too big (%u) !\n", wDevID);
786 return MMSYSERR_BADDEVICEID;
788 if (MidiInDev[wDevID].midiDesc.hMidi == 0) {
789 WARN("device not opened !\n");
790 return MMSYSERR_ERROR;
792 if (MidiInDev[wDevID].lpQueueHdr != 0) {
793 return MIDIERR_STILLPLAYING;
796 if (midiSeqFD == -1) {
797 WARN("ooops !\n");
798 return MMSYSERR_ERROR;
800 if (--numStartedMidiIn == 0) {
801 TRACE("Stopping thread for midi-in\n");
802 end_thread = 1;
803 if (WaitForSingleObject(hThread, 5000) != WAIT_OBJECT_0) {
804 WARN("Thread end not signaled, force termination\n");
805 TerminateThread(hThread, 0);
807 TRACE("Stopped thread for midi-in\n");
809 midiCloseSeq();
811 MidiInDev[wDevID].bufsize = 0;
812 MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L);
813 MidiInDev[wDevID].midiDesc.hMidi = 0;
814 return ret;
817 /**************************************************************************
818 * midAddBuffer [internal]
820 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
822 TRACE("(%04X, %p, %d);\n", wDevID, lpMidiHdr, dwSize);
824 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
825 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
827 if (lpMidiHdr == NULL) return MMSYSERR_INVALPARAM;
828 if (dwSize < offsetof(MIDIHDR,dwOffset)) return MMSYSERR_INVALPARAM;
829 if (lpMidiHdr->dwBufferLength == 0) return MMSYSERR_INVALPARAM;
830 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
831 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
833 EnterCriticalSection(&crit_sect);
834 lpMidiHdr->dwFlags &= ~WHDR_DONE;
835 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
836 lpMidiHdr->dwBytesRecorded = 0;
837 lpMidiHdr->lpNext = 0;
838 if (MidiInDev[wDevID].lpQueueHdr == 0) {
839 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr;
840 } else {
841 LPMIDIHDR ptr;
843 for (ptr = MidiInDev[wDevID].lpQueueHdr;
844 ptr->lpNext != 0;
845 ptr = ptr->lpNext);
846 ptr->lpNext = lpMidiHdr;
848 LeaveCriticalSection(&crit_sect);
850 return MMSYSERR_NOERROR;
853 /**************************************************************************
854 * midPrepare [internal]
856 static DWORD midPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
858 TRACE("(%04X, %p, %d);\n", wDevID, lpMidiHdr, dwSize);
860 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 || lpMidiHdr->lpData == 0)
861 return MMSYSERR_INVALPARAM;
862 if (lpMidiHdr->dwFlags & MHDR_PREPARED)
863 return MMSYSERR_NOERROR;
865 lpMidiHdr->lpNext = 0;
866 lpMidiHdr->dwFlags |= MHDR_PREPARED;
867 lpMidiHdr->dwFlags &= ~(MHDR_DONE|MHDR_INQUEUE); /* flags cleared since w2k */
869 return MMSYSERR_NOERROR;
872 /**************************************************************************
873 * midUnprepare [internal]
875 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
877 TRACE("(%04X, %p, %d);\n", wDevID, lpMidiHdr, dwSize);
879 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 || lpMidiHdr->lpData == 0)
880 return MMSYSERR_INVALPARAM;
881 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
882 return MMSYSERR_NOERROR;
883 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
884 return MIDIERR_STILLPLAYING;
886 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
888 return MMSYSERR_NOERROR;
891 /**************************************************************************
892 * midReset [internal]
894 static DWORD midReset(WORD wDevID)
896 DWORD dwTime = GetTickCount();
898 TRACE("(%04X);\n", wDevID);
900 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
901 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
903 EnterCriticalSection(&crit_sect);
904 while (MidiInDev[wDevID].lpQueueHdr) {
905 LPMIDIHDR lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
906 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr->lpNext;
907 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
908 lpMidiHdr->dwFlags |= MHDR_DONE;
909 MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD_PTR)lpMidiHdr, dwTime);
911 LeaveCriticalSection(&crit_sect);
913 return MMSYSERR_NOERROR;
916 /**************************************************************************
917 * midStart [internal]
919 static DWORD midStart(WORD wDevID)
921 TRACE("(%04X);\n", wDevID);
923 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
924 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
926 MidiInDev[wDevID].state = 1;
927 MidiInDev[wDevID].startTime = GetTickCount();
928 return MMSYSERR_NOERROR;
931 /**************************************************************************
932 * midStop [internal]
934 static DWORD midStop(WORD wDevID)
936 TRACE("(%04X);\n", wDevID);
938 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
939 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
941 MidiInDev[wDevID].state = 0;
942 return MMSYSERR_NOERROR;
945 /*-----------------------------------------------------------------------*/
947 typedef struct sVoice {
948 int note; /* 0 means not used */
949 int channel;
950 unsigned cntMark : 30,
951 status : 2;
952 #define sVS_UNUSED 0
953 #define sVS_PLAYING 1
954 #define sVS_SUSTAINED 2
955 } sVoice;
957 typedef struct sChannel {
958 int program;
960 int bender;
961 int benderRange;
962 /* controllers */
963 int bank; /* CTL_BANK_SELECT */
964 int volume; /* CTL_MAIN_VOLUME */
965 int balance; /* CTL_BALANCE */
966 int expression; /* CTL_EXPRESSION */
967 int sustain; /* CTL_SUSTAIN */
969 unsigned char nrgPmtMSB; /* Non register Parameters */
970 unsigned char nrgPmtLSB;
971 unsigned char regPmtMSB; /* Non register Parameters */
972 unsigned char regPmtLSB;
973 } sChannel;
975 typedef struct sFMextra {
976 unsigned counter;
977 int drumSetMask;
978 sChannel channel[16]; /* MIDI has only 16 channels */
979 sVoice voice[1]; /* dyn allocated according to sound card */
980 /* do not append fields below voice[1] since the size of this structure
981 * depends on the number of available voices on the FM synth...
983 } sFMextra;
985 extern const unsigned char midiFMInstrumentPatches[16 * 128];
986 extern const unsigned char midiFMDrumsPatches [16 * 128];
988 /**************************************************************************
989 * modFMLoad [internal]
991 static int modFMLoad(int dev)
993 int i;
994 struct sbi_instrument sbi;
996 sbi.device = dev;
997 sbi.key = FM_PATCH;
999 memset(sbi.operators + 16, 0, 16);
1000 for (i = 0; i < 128; i++) {
1001 sbi.channel = i;
1002 memcpy(sbi.operators, midiFMInstrumentPatches + i * 16, 16);
1004 if (write(midiSeqFD, &sbi, sizeof(sbi)) == -1) {
1005 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
1006 return -1;
1009 for (i = 0; i < 128; i++) {
1010 sbi.channel = 128 + i;
1011 memcpy(sbi.operators, midiFMDrumsPatches + i * 16, 16);
1013 if (write(midiSeqFD, &sbi, sizeof(sbi)) == -1) {
1014 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
1015 return -1;
1018 return 0;
1021 /**************************************************************************
1022 * modFMReset [internal]
1024 static void modFMReset(WORD wDevID)
1026 sFMextra* extra = MidiOutDev[wDevID].lpExtra;
1027 sVoice* voice = extra->voice;
1028 sChannel* channel = extra->channel;
1029 int i;
1031 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1032 if (voice[i].status != sVS_UNUSED) {
1033 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1035 SEQ_KEY_PRESSURE(wDevID, i, 127, 0);
1036 SEQ_CONTROL(wDevID, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
1037 voice[i].note = 0;
1038 voice[i].channel = -1;
1039 voice[i].cntMark = 0;
1040 voice[i].status = sVS_UNUSED;
1042 for (i = 0; i < 16; i++) {
1043 channel[i].program = 0;
1044 channel[i].bender = 8192;
1045 channel[i].benderRange = 2;
1046 channel[i].bank = 0;
1047 channel[i].volume = 127;
1048 channel[i].balance = 64;
1049 channel[i].expression = 0;
1050 channel[i].sustain = 0;
1052 extra->counter = 0;
1053 extra->drumSetMask = 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
1054 SEQ_DUMPBUF();
1057 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
1059 /**************************************************************************
1060 * modGetDevCaps [internal]
1062 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPSW lpCaps, DWORD dwSize)
1064 TRACE("(%04X, %p, %08X);\n", wDevID, lpCaps, dwSize);
1066 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1067 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
1069 memcpy(lpCaps, &MidiOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1071 return MMSYSERR_NOERROR;
1074 /**************************************************************************
1075 * modOpen [internal]
1077 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
1079 TRACE("(%04X, %p, %08X);\n", wDevID, lpDesc, dwFlags);
1080 if (lpDesc == NULL) {
1081 WARN("Invalid Parameter !\n");
1082 return MMSYSERR_INVALPARAM;
1084 if (wDevID >= MODM_NumDevs) {
1085 TRACE("MAX_MIDIOUTDRV reached !\n");
1086 return MMSYSERR_BADDEVICEID;
1088 if (MidiOutDev[wDevID].midiDesc.hMidi != 0) {
1089 WARN("device already open !\n");
1090 return MMSYSERR_ALLOCATED;
1092 if (!MidiOutDev[wDevID].bEnabled) {
1093 WARN("device disabled !\n");
1094 return MIDIERR_NODEVICE;
1096 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
1097 WARN("bad dwFlags\n");
1098 return MMSYSERR_INVALFLAG;
1101 MidiOutDev[wDevID].lpExtra = 0;
1103 switch (MidiOutDev[wDevID].caps.wTechnology) {
1104 case MOD_FMSYNTH:
1106 void* extra;
1108 extra = HeapAlloc(GetProcessHeap(), 0,
1109 offsetof(struct sFMextra, voice[MidiOutDev[wDevID].caps.wVoices]));
1111 if (extra == 0) {
1112 WARN("can't alloc extra data !\n");
1113 return MMSYSERR_NOMEM;
1115 MidiOutDev[wDevID].lpExtra = extra;
1116 if (midiOpenSeq() < 0) {
1117 MidiOutDev[wDevID].lpExtra = 0;
1118 HeapFree(GetProcessHeap(), 0, extra);
1119 return MMSYSERR_ERROR;
1121 if (modFMLoad(wDevID) < 0) {
1122 midiCloseSeq();
1123 MidiOutDev[wDevID].lpExtra = 0;
1124 HeapFree(GetProcessHeap(), 0, extra);
1125 return MMSYSERR_ERROR;
1127 modFMReset(wDevID);
1129 break;
1130 case MOD_MIDIPORT:
1131 case MOD_SYNTH:
1132 if (midiOpenSeq() < 0) {
1133 return MMSYSERR_ALLOCATED;
1135 break;
1136 default:
1137 WARN("Technology not supported (yet) %d !\n",
1138 MidiOutDev[wDevID].caps.wTechnology);
1139 return MMSYSERR_NOTENABLED;
1142 MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1144 MidiOutDev[wDevID].lpQueueHdr = NULL;
1145 MidiOutDev[wDevID].dwTotalPlayed = 0;
1146 MidiOutDev[wDevID].bufsize = 0x3FFF;
1147 MidiOutDev[wDevID].midiDesc = *lpDesc;
1149 MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L);
1150 TRACE("Successful !\n");
1151 return MMSYSERR_NOERROR;
1155 /**************************************************************************
1156 * modClose [internal]
1158 static DWORD modClose(WORD wDevID)
1160 int ret = MMSYSERR_NOERROR;
1162 TRACE("(%04X);\n", wDevID);
1164 if (MidiOutDev[wDevID].midiDesc.hMidi == 0) {
1165 WARN("device not opened !\n");
1166 return MMSYSERR_ERROR;
1168 /* FIXME: should test that no pending buffer is still in the queue for
1169 * playing */
1171 if (midiSeqFD == -1) {
1172 WARN("can't close !\n");
1173 return MMSYSERR_ERROR;
1176 switch (MidiOutDev[wDevID].caps.wTechnology) {
1177 case MOD_FMSYNTH:
1178 case MOD_MIDIPORT:
1179 midiCloseSeq();
1180 break;
1181 default:
1182 WARN("Technology not supported (yet) %d !\n",
1183 MidiOutDev[wDevID].caps.wTechnology);
1184 return MMSYSERR_NOTENABLED;
1187 HeapFree(GetProcessHeap(), 0, MidiOutDev[wDevID].lpExtra);
1188 MidiOutDev[wDevID].lpExtra = 0;
1190 MidiOutDev[wDevID].bufsize = 0;
1191 MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L);
1192 MidiOutDev[wDevID].midiDesc.hMidi = 0;
1193 return ret;
1196 /**************************************************************************
1197 * modData [internal]
1199 static DWORD modData(WORD wDevID, DWORD dwParam)
1201 WORD evt = LOBYTE(LOWORD(dwParam));
1202 WORD d1 = HIBYTE(LOWORD(dwParam));
1203 WORD d2 = LOBYTE(HIWORD(dwParam));
1205 TRACE("(%04X, %08X);\n", wDevID, dwParam);
1207 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1208 if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1210 if (midiSeqFD == -1) {
1211 WARN("can't play !\n");
1212 return MIDIERR_NODEVICE;
1214 switch (MidiOutDev[wDevID].caps.wTechnology) {
1215 case MOD_FMSYNTH:
1216 /* FIXME:
1217 * - chorus depth controller is not used
1220 sFMextra* extra = MidiOutDev[wDevID].lpExtra;
1221 sVoice* voice = extra->voice;
1222 sChannel* channel = extra->channel;
1223 int chn = (evt & 0x0F);
1224 int i, nv;
1226 switch (evt & 0xF0) {
1227 case MIDI_NOTEOFF:
1228 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1229 /* don't stop sustained notes */
1230 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1231 voice[i].status = sVS_UNUSED;
1232 SEQ_STOP_NOTE(wDevID, i, d1, d2);
1235 break;
1236 case MIDI_NOTEON:
1237 if (d2 == 0) { /* note off if velocity == 0 */
1238 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1239 /* don't stop sustained notes */
1240 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1241 voice[i].status = sVS_UNUSED;
1242 SEQ_STOP_NOTE(wDevID, i, d1, 64);
1245 break;
1247 /* finding out in this order :
1248 * - an empty voice
1249 * - if replaying the same note on the same channel
1250 * - the older voice (LRU)
1252 for (i = nv = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1253 if (voice[i].status == sVS_UNUSED ||
1254 (voice[i].note == d1 && voice[i].channel == chn)) {
1255 nv = i;
1256 break;
1258 if (voice[i].cntMark < voice[0].cntMark) {
1259 nv = i;
1262 TRACE(
1263 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1264 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1265 nv, channel[chn].program,
1266 channel[chn].balance,
1267 channel[chn].volume,
1268 channel[chn].bender, d1, d2);
1270 SEQ_SET_PATCH(wDevID, nv, IS_DRUM_CHANNEL(extra, chn) ?
1271 (128 + d1) : channel[chn].program);
1272 SEQ_BENDER_RANGE(wDevID, nv, channel[chn].benderRange * 100);
1273 SEQ_BENDER(wDevID, nv, channel[chn].bender);
1274 SEQ_CONTROL(wDevID, nv, CTL_PAN, channel[chn].balance);
1275 SEQ_CONTROL(wDevID, nv, CTL_EXPRESSION, channel[chn].expression);
1276 #if 0
1277 /* FIXME: does not really seem to work on my SB card and
1278 * screws everything up... so lay it down
1280 SEQ_CONTROL(wDevID, nv, CTL_MAIN_VOLUME, channel[chn].volume);
1281 #endif
1282 SEQ_START_NOTE(wDevID, nv, d1, d2);
1283 voice[nv].status = channel[chn].sustain ? sVS_SUSTAINED : sVS_PLAYING;
1284 voice[nv].note = d1;
1285 voice[nv].channel = chn;
1286 voice[nv].cntMark = extra->counter++;
1287 break;
1288 case MIDI_KEY_PRESSURE:
1289 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1290 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn && voice[i].note == d1) {
1291 SEQ_KEY_PRESSURE(wDevID, i, d1, d2);
1294 break;
1295 case MIDI_CTL_CHANGE:
1296 switch (d1) {
1297 case CTL_BANK_SELECT: channel[chn].bank = d2; break;
1298 case CTL_MAIN_VOLUME: channel[chn].volume = d2; break;
1299 case CTL_PAN: channel[chn].balance = d2; break;
1300 case CTL_EXPRESSION: channel[chn].expression = d2; break;
1301 case CTL_SUSTAIN: channel[chn].sustain = d2;
1302 if (d2) {
1303 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1304 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1305 voice[i].status = sVS_SUSTAINED;
1308 } else {
1309 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1310 if (voice[i].status == sVS_SUSTAINED && voice[i].channel == chn) {
1311 voice[i].status = sVS_UNUSED;
1312 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1316 break;
1317 case CTL_NONREG_PARM_NUM_LSB: channel[chn].nrgPmtLSB = d2; break;
1318 case CTL_NONREG_PARM_NUM_MSB: channel[chn].nrgPmtMSB = d2; break;
1319 case CTL_REGIST_PARM_NUM_LSB: channel[chn].regPmtLSB = d2; break;
1320 case CTL_REGIST_PARM_NUM_MSB: channel[chn].regPmtMSB = d2; break;
1321 case CTL_DATA_ENTRY:
1322 switch ((channel[chn].regPmtMSB << 8) | channel[chn].regPmtLSB) {
1323 case 0x0000:
1324 if (channel[chn].benderRange != d2) {
1325 channel[chn].benderRange = d2;
1326 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1327 if (voice[i].channel == chn) {
1328 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1332 break;
1334 case 0x7F7F:
1335 channel[chn].benderRange = 2;
1336 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1337 if (voice[i].channel == chn) {
1338 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1341 break;
1342 default:
1343 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1344 channel[chn].regPmtMSB, channel[chn].regPmtLSB,
1345 channel[chn].nrgPmtMSB, channel[chn].nrgPmtLSB,
1346 d2);
1347 break;
1349 break;
1351 case 0x78: /* all sounds off */
1352 /* FIXME: I don't know if I have to take care of the channel
1353 * for this control ?
1355 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1356 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1357 voice[i].status = sVS_UNUSED;
1358 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1361 break;
1362 case 0x7B: /* all notes off */
1363 /* FIXME: I don't know if I have to take care of the channel
1364 * for this control ?
1366 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1367 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1368 voice[i].status = sVS_UNUSED;
1369 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1372 break;
1373 default:
1374 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1375 d1, d2, chn);
1376 break;
1378 break;
1379 case MIDI_PGM_CHANGE:
1380 channel[chn].program = d1;
1381 break;
1382 case MIDI_CHN_PRESSURE:
1383 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1384 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1385 SEQ_KEY_PRESSURE(wDevID, i, voice[i].note, d1);
1388 break;
1389 case MIDI_PITCH_BEND:
1390 channel[chn].bender = (d2 << 7) + d1;
1391 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1392 if (voice[i].channel == chn) {
1393 SEQ_BENDER(wDevID, i, channel[chn].bender);
1396 break;
1397 case MIDI_SYSTEM_PREFIX:
1398 switch (evt & 0x0F) {
1399 case 0x0F: /* Reset */
1400 modFMReset(wDevID);
1401 break;
1402 default:
1403 WARN("Unsupported (yet) system event %02x\n", evt & 0x0F);
1405 break;
1406 default:
1407 WARN("Internal error, shouldn't happen (event=%08x)\n", evt & 0xF0);
1408 return MMSYSERR_NOTENABLED;
1411 break;
1412 case MOD_MIDIPORT:
1414 int dev = wDevID - MODM_NumFMSynthDevs;
1415 if (dev < 0) {
1416 WARN("Internal error on devID (%u) !\n", wDevID);
1417 return MIDIERR_NODEVICE;
1420 switch (evt & 0xF0) {
1421 case MIDI_NOTEOFF:
1422 case MIDI_NOTEON:
1423 case MIDI_KEY_PRESSURE:
1424 case MIDI_CTL_CHANGE:
1425 case MIDI_PITCH_BEND:
1426 SEQ_MIDIOUT(dev, evt);
1427 SEQ_MIDIOUT(dev, d1);
1428 SEQ_MIDIOUT(dev, d2);
1429 break;
1430 case MIDI_PGM_CHANGE:
1431 case MIDI_CHN_PRESSURE:
1432 SEQ_MIDIOUT(dev, evt);
1433 SEQ_MIDIOUT(dev, d1);
1434 break;
1435 case MIDI_SYSTEM_PREFIX:
1436 switch (evt & 0x0F) {
1437 case 0x00: /* System Exclusive, don't do it on modData,
1438 * should require modLongData*/
1439 case 0x04: /* Undefined. */
1440 case 0x05: /* Undefined. */
1441 case 0x07: /* End of Exclusive. */
1442 case 0x09: /* Undefined. */
1443 case 0x0D: /* Undefined. */
1444 break;
1445 case 0x06: /* Tune Request */
1446 case 0x08: /* Timing Clock. */
1447 case 0x0A: /* Start. */
1448 case 0x0B: /* Continue */
1449 case 0x0C: /* Stop */
1450 case 0x0E: /* Active Sensing. */
1451 SEQ_MIDIOUT(dev, evt);
1452 break;
1453 case 0x0F: /* Reset */
1454 /* SEQ_MIDIOUT(dev, evt);
1455 this other way may be better */
1456 SEQ_MIDIOUT(dev, MIDI_SYSTEM_PREFIX);
1457 SEQ_MIDIOUT(dev, 0x7e);
1458 SEQ_MIDIOUT(dev, 0x7f);
1459 SEQ_MIDIOUT(dev, 0x09);
1460 SEQ_MIDIOUT(dev, 0x01);
1461 SEQ_MIDIOUT(dev, 0xf7);
1462 break;
1463 case 0x01: /* MTC Quarter frame */
1464 case 0x03: /* Song Select. */
1465 SEQ_MIDIOUT(dev, evt);
1466 SEQ_MIDIOUT(dev, d1);
1467 case 0x02: /* Song Position Pointer. */
1468 SEQ_MIDIOUT(dev, evt);
1469 SEQ_MIDIOUT(dev, d1);
1470 SEQ_MIDIOUT(dev, d2);
1472 break;
1475 break;
1476 default:
1477 WARN("Technology not supported (yet) %d !\n",
1478 MidiOutDev[wDevID].caps.wTechnology);
1479 return MMSYSERR_NOTENABLED;
1482 SEQ_DUMPBUF();
1484 return MMSYSERR_NOERROR;
1487 /**************************************************************************
1488 * modLongData [internal]
1490 static DWORD modLongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1492 int count;
1493 LPBYTE lpData;
1495 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
1497 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
1498 * but it seems to be used only for midi input.
1499 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
1502 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1503 if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1505 if (midiSeqFD == -1) {
1506 WARN("can't play !\n");
1507 return MIDIERR_NODEVICE;
1510 lpData = (LPBYTE) lpMidiHdr->lpData;
1512 if (lpData == NULL)
1513 return MIDIERR_UNPREPARED;
1514 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1515 return MIDIERR_UNPREPARED;
1516 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1517 return MIDIERR_STILLPLAYING;
1518 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1519 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1521 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1522 * data, or can it also contain raw MIDI data, to be split up and sent to
1523 * modShortData() ?
1524 * If the latter is true, then the following WARNing will fire up
1526 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
1527 WARN("The allegedly system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1530 TRACE("dwBufferLength=%u !\n", lpMidiHdr->dwBufferLength);
1531 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1532 lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
1533 lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
1535 switch (MidiOutDev[wDevID].caps.wTechnology) {
1536 case MOD_FMSYNTH:
1537 /* FIXME: I don't think there is much to do here */
1538 break;
1539 case MOD_MIDIPORT:
1540 if (lpData[0] != 0xF0) {
1541 /* Send end of System Exclusive */
1542 SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, 0xF0);
1543 WARN("Adding missing 0xF0 marker at the beginning of "
1544 "system exclusive byte stream\n");
1546 for (count = 0; count < lpMidiHdr->dwBufferLength; count++) {
1547 SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, lpData[count]);
1549 if (lpData[count - 1] != 0xF7) {
1550 /* Send end of System Exclusive */
1551 SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, 0xF7);
1552 WARN("Adding missing 0xF7 marker at the end of "
1553 "system exclusive byte stream\n");
1555 SEQ_DUMPBUF();
1556 break;
1557 default:
1558 WARN("Technology not supported (yet) %d !\n",
1559 MidiOutDev[wDevID].caps.wTechnology);
1560 return MMSYSERR_NOTENABLED;
1563 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1564 lpMidiHdr->dwFlags |= MHDR_DONE;
1565 MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD_PTR)lpMidiHdr, 0L);
1566 return MMSYSERR_NOERROR;
1569 /**************************************************************************
1570 * modPrepare [internal]
1572 static DWORD modPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1574 TRACE("(%04X, %p, %d);\n", wDevID, lpMidiHdr, dwSize);
1576 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 || lpMidiHdr->lpData == 0)
1577 return MMSYSERR_INVALPARAM;
1578 if (lpMidiHdr->dwFlags & MHDR_PREPARED)
1579 return MMSYSERR_NOERROR;
1581 lpMidiHdr->lpNext = 0;
1582 lpMidiHdr->dwFlags |= MHDR_PREPARED;
1583 lpMidiHdr->dwFlags &= ~(MHDR_DONE|MHDR_INQUEUE); /* flags cleared since w2k */
1584 return MMSYSERR_NOERROR;
1587 /**************************************************************************
1588 * modUnprepare [internal]
1590 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1592 TRACE("(%04X, %p, %d);\n", wDevID, lpMidiHdr, dwSize);
1594 if (dwSize < offsetof(MIDIHDR,dwOffset) || lpMidiHdr == 0 || lpMidiHdr->lpData == 0)
1595 return MMSYSERR_INVALPARAM;
1596 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1597 return MMSYSERR_NOERROR;
1598 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1599 return MIDIERR_STILLPLAYING;
1600 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
1601 return MMSYSERR_NOERROR;
1604 /**************************************************************************
1605 * modGetVolume [internal]
1607 static DWORD modGetVolume(WORD wDevID, DWORD* lpdwVolume)
1609 if (!lpdwVolume) return MMSYSERR_INVALPARAM;
1610 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1611 *lpdwVolume = 0xFFFFFFFF;
1612 return (MidiOutDev[wDevID].caps.dwSupport & MIDICAPS_VOLUME) ? 0 : MMSYSERR_NOTSUPPORTED;
1615 /**************************************************************************
1616 * modReset [internal]
1618 static DWORD modReset(WORD wDevID)
1620 unsigned chn;
1622 TRACE("(%04X);\n", wDevID);
1624 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1625 if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1627 /* stop all notes */
1628 /* FIXME: check if 0x78B0 is channel dependent or not. I coded it so that
1629 * it's channel dependent...
1631 for (chn = 0; chn < 16; chn++) {
1632 /* turn off every note */
1633 modData(wDevID, 0x7800 | MIDI_CTL_CHANGE | chn);
1634 /* remove sustain on all channels */
1635 modData(wDevID, (CTL_SUSTAIN << 8) | MIDI_CTL_CHANGE | chn);
1637 /* FIXME: the LongData buffers must also be returned to the app */
1638 return MMSYSERR_NOERROR;
1641 #endif /* SNDCTL_SEQ_NRMIDIS */
1643 /*======================================================================*
1644 * MIDI entry points *
1645 *======================================================================*/
1647 /**************************************************************************
1648 * midMessage (WINEOSS.@)
1650 DWORD WINAPI OSS_midMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1651 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1653 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1654 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1655 switch (wMsg) {
1656 #ifdef SNDCTL_SEQ_NRMIDIS
1657 case DRVM_INIT:
1658 return OSS_MidiInit();
1659 case DRVM_EXIT:
1660 return OSS_MidiExit();
1661 case DRVM_ENABLE:
1662 case DRVM_DISABLE:
1663 /* FIXME: Pretend this is supported */
1664 return 0;
1665 case MIDM_OPEN:
1666 return midOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1667 case MIDM_CLOSE:
1668 return midClose(wDevID);
1669 case MIDM_ADDBUFFER:
1670 return midAddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1671 case MIDM_PREPARE:
1672 return midPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1673 case MIDM_UNPREPARE:
1674 return midUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1675 case MIDM_GETDEVCAPS:
1676 return midGetDevCaps(wDevID, (LPMIDIINCAPSW)dwParam1,dwParam2);
1677 case MIDM_GETNUMDEVS:
1678 return MIDM_NumDevs;
1679 case MIDM_RESET:
1680 return midReset(wDevID);
1681 case MIDM_START:
1682 return midStart(wDevID);
1683 case MIDM_STOP:
1684 return midStop(wDevID);
1685 #else
1686 case DRVM_INIT:
1687 case MIDM_GETNUMDEVS:
1688 return 0;
1689 #endif
1690 default:
1691 TRACE("Unsupported message\n");
1693 return MMSYSERR_NOTSUPPORTED;
1696 /**************************************************************************
1697 * modMessage (WINEOSS.@)
1699 DWORD WINAPI OSS_modMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1700 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1702 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1703 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1705 switch (wMsg) {
1706 #ifdef SNDCTL_SEQ_NRMIDIS
1707 case DRVM_INIT:
1708 return OSS_MidiInit();
1709 case DRVM_EXIT:
1710 return OSS_MidiExit();
1711 case DRVM_ENABLE:
1712 case DRVM_DISABLE:
1713 /* FIXME: Pretend this is supported */
1714 return 0;
1715 case MODM_OPEN:
1716 return modOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1717 case MODM_CLOSE:
1718 return modClose(wDevID);
1719 case MODM_DATA:
1720 return modData(wDevID, dwParam1);
1721 case MODM_LONGDATA:
1722 return modLongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1723 case MODM_PREPARE:
1724 return modPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1725 case MODM_UNPREPARE:
1726 return modUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1727 case MODM_GETDEVCAPS:
1728 return modGetDevCaps(wDevID, (LPMIDIOUTCAPSW)dwParam1, dwParam2);
1729 case MODM_GETNUMDEVS:
1730 return MODM_NumDevs;
1731 case MODM_GETVOLUME:
1732 return modGetVolume(wDevID, (DWORD*)dwParam1);
1733 case MODM_SETVOLUME:
1734 return 0;
1735 case MODM_RESET:
1736 return modReset(wDevID);
1737 #else
1738 case DRVM_INIT:
1739 case MODM_GETNUMDEVS:
1740 return 0;
1741 #endif
1742 default:
1743 TRACE("Unsupported message\n");
1745 return MMSYSERR_NOTSUPPORTED;
1748 /**************************************************************************
1749 * DriverProc (WINEOSS.1)
1751 LRESULT CALLBACK OSS_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1752 LPARAM dwParam1, LPARAM dwParam2)
1754 TRACE("(%08lX, %p, %08X, %08lX, %08lX)\n",
1755 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1757 switch(wMsg) {
1758 case DRV_LOAD:
1759 case DRV_FREE:
1760 case DRV_OPEN:
1761 case DRV_CLOSE:
1762 case DRV_ENABLE:
1763 case DRV_DISABLE:
1764 case DRV_QUERYCONFIGURE:
1765 case DRV_CONFIGURE:
1766 return 1;
1767 case DRV_INSTALL:
1768 case DRV_REMOVE:
1769 return DRV_SUCCESS;
1770 default:
1771 return 0;