change default iSmCaptionWidth to 12
[wine/kumbayo.git] / dlls / wineoss.drv / midi.c
blob939b725262c8bce8ad2935edbe2104daa1107421
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 splitted in midi.c and mcimidi.c
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 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
64 #include "windef.h"
65 #include "winbase.h"
66 #include "wingdi.h"
67 #include "winuser.h"
68 #include "winnls.h"
69 #include "mmddk.h"
70 #include "oss.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 synthetizers (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 watetable 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 /**************************************************************************
174 * OSS_MidiInit [internal]
176 * Initializes the MIDI devices information variables
178 LRESULT OSS_MidiInit(void)
180 int i, status, numsynthdevs = 255, nummididevs = 255;
181 struct synth_info sinfo;
182 struct midi_info minfo;
183 static BOOL bInitDone = FALSE;
185 if (bInitDone)
186 return 0;
188 TRACE("Initializing the MIDI variables.\n");
189 bInitDone = TRUE;
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 MidiOutDev[i].caps.wChannelMask = 0xFFFF;
222 /* FIXME Do we have this information?
223 * Assuming the soundcards can handle
224 * MIDICAPS_VOLUME and MIDICAPS_LRVOLUME but
225 * not MIDICAPS_CACHE.
227 MidiOutDev[i].caps.dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
229 sinfo.device = i;
230 status = ioctl(midiSeqFD, SNDCTL_SYNTH_INFO, &sinfo);
231 if (status == -1) {
232 static const WCHAR fmt[] = {'W','i','n','e',' ','O','S','S',' ','M','i','d','i',' ','O','u','t',' ','(','#','%','d',')',' ','d','i','s','a','b','l','e','d',0};
233 ERR("ioctl for synth info failed on %d, disabling it.\n", i);
235 wsprintfW(MidiOutDev[i].caps.szPname, fmt, i);
237 MidiOutDev[i].caps.wTechnology = MOD_MIDIPORT;
238 MidiOutDev[i].caps.wVoices = 16;
239 MidiOutDev[i].caps.wNotes = 16;
240 MidiOutDev[i].bEnabled = FALSE;
241 } else {
242 MultiByteToWideChar( CP_ACP, 0, sinfo.name, -1,
243 MidiOutDev[i].caps.szPname,
244 sizeof(MidiOutDev[i].caps.szPname)/sizeof(WCHAR) );
246 MidiOutDev[i].caps.wTechnology = MIDI_UnixToWindowsDeviceType(sinfo.synth_type);
247 MidiOutDev[i].caps.wVoices = sinfo.nr_voices;
249 /* FIXME Is it possible to know the maximum
250 * number of simultaneous notes of a soundcard ?
251 * I believe we don't have this information, but
252 * it's probably equal or more than wVoices
254 MidiOutDev[i].caps.wNotes = sinfo.nr_voices;
255 MidiOutDev[i].bEnabled = TRUE;
258 /* We also have the information sinfo.synth_subtype, not used here
261 if (sinfo.capabilities & SYNTH_CAP_INPUT) {
262 FIXME("Synthesizer supports MIDI in. Not yet supported.\n");
265 TRACE("SynthOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%d\n"
266 "\tOSS info: synth subtype=%d capa=%lx\n",
267 i, wine_dbgstr_w(MidiOutDev[i].caps.szPname),
268 MidiOutDev[i].caps.wTechnology,
269 MidiOutDev[i].caps.wVoices, MidiOutDev[i].caps.wNotes,
270 MidiOutDev[i].caps.wChannelMask, MidiOutDev[i].caps.dwSupport,
271 sinfo.synth_subtype, (long)sinfo.capabilities);
274 /* find how many MIDI devices are there in the system */
275 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRMIDIS, &nummididevs);
276 if (status == -1) {
277 ERR("ioctl on nr midi failed.\n");
278 nummididevs = 0;
279 goto wrapup;
282 /* FIXME: the two restrictions below could be loosened in some cases */
283 if (numsynthdevs + nummididevs > MAX_MIDIOUTDRV) {
284 ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
285 "Some MIDI devices will not be available.\n");
286 nummididevs = MAX_MIDIOUTDRV - numsynthdevs;
289 if (nummididevs > MAX_MIDIINDRV) {
290 ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
291 "Some MIDI devices will not be available.\n",MAX_MIDIINDRV,nummididevs);
292 nummididevs = MAX_MIDIINDRV;
295 for (i = 0; i < nummididevs; i++) {
296 minfo.device = i;
297 status = ioctl(midiSeqFD, SNDCTL_MIDI_INFO, &minfo);
298 if (status == -1) WARN("ioctl on midi info for device %d failed.\n", i);
300 /* This whole part is somewhat obscure to me. I'll keep trying to dig
301 info about it. If you happen to know, please tell us. The very
302 descritive minfo.dev_type was not used here.
304 /* Manufac ID. We do not have access to this with soundcard.h
305 Does not seem to be a problem, because in mmsystem.h only
306 Microsoft's ID is listed */
307 MidiOutDev[numsynthdevs + i].caps.wMid = 0x00FF;
308 MidiOutDev[numsynthdevs + i].caps.wPid = 0x0001; /* FIXME Product ID */
309 /* Product Version. We simply say "1" */
310 MidiOutDev[numsynthdevs + i].caps.vDriverVersion = 0x001;
311 if (status == -1) {
312 static const WCHAR fmt[] = {'W','i','n','e',' ','O','S','S',' ','M','i','d','i',' ','O','u','t',' ','(','#','%','d',')',' ','d','i','s','a','b','l','e','d',0};
313 wsprintfW(MidiOutDev[numsynthdevs + i].caps.szPname, fmt, numsynthdevs + i);
314 MidiOutDev[numsynthdevs + i].bEnabled = FALSE;
315 } else {
316 MultiByteToWideChar(CP_ACP, 0, minfo.name, -1,
317 MidiOutDev[numsynthdevs + i].caps.szPname,
318 sizeof(MidiOutDev[numsynthdevs + i].caps.szPname) / sizeof(WCHAR));
319 MidiOutDev[numsynthdevs + i].bEnabled = TRUE;
321 MidiOutDev[numsynthdevs + i].caps.wTechnology = MOD_MIDIPORT; /* FIXME Is this right? */
322 /* Does it make any difference? */
323 MidiOutDev[numsynthdevs + i].caps.wVoices = 16;
324 /* Does it make any difference? */
325 MidiOutDev[numsynthdevs + i].caps.wNotes = 16;
326 MidiOutDev[numsynthdevs + i].caps.wChannelMask= 0xFFFF;
328 /* FIXME Does it make any difference? */
329 MidiOutDev[numsynthdevs + i].caps.dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
331 /* This whole part is somewhat obscure to me. I'll keep trying to dig
332 info about it. If you happen to know, please tell us. The very
333 descritive minfo.dev_type was not used here.
335 /* Manufac ID. We do not have access to this with soundcard.h
336 Does not seem to be a problem, because in mmsystem.h only
337 Microsoft's ID is listed */
338 MidiInDev[i].caps.wMid = 0x00FF;
339 MidiInDev[i].caps.wPid = 0x0001; /* FIXME Product ID */
340 /* Product Version. We simply say "1" */
341 MidiInDev[i].caps.vDriverVersion = 0x001;
342 if (status == -1) {
343 static const WCHAR fmt[] = {'W','i','n','e',' ','O','S','S',' ','M','i','d','i',' ','I','n',' ','(','#','%','d',')',' ','d','i','s','a','b','l','e','d',0};
344 wsprintfW(MidiInDev[i].caps.szPname, fmt, numsynthdevs + i);
345 MidiInDev[i].state = -1;
346 } else {
347 MultiByteToWideChar(CP_ACP, 0, minfo.name, -1,
348 MidiInDev[i].caps.szPname,
349 sizeof(MidiInDev[i].caps.szPname) / sizeof(WCHAR));
350 MidiInDev[i].state = 0;
352 /* FIXME : could we get better information than that ? */
353 MidiInDev[i].caps.dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
355 TRACE("MidiOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%d\n"
356 "MidiIn [%d]\tname='%s' support=%d\n"
357 "\tOSS info: midi dev-type=%d, capa=%lx\n",
358 i, wine_dbgstr_w(MidiOutDev[numsynthdevs + i].caps.szPname),
359 MidiOutDev[numsynthdevs + i].caps.wTechnology,
360 MidiOutDev[numsynthdevs + i].caps.wVoices, MidiOutDev[numsynthdevs + i].caps.wNotes,
361 MidiOutDev[numsynthdevs + i].caps.wChannelMask, MidiOutDev[numsynthdevs + i].caps.dwSupport,
362 i, wine_dbgstr_w(MidiInDev[i].caps.szPname), MidiInDev[i].caps.dwSupport,
363 minfo.dev_type, (long)minfo.capabilities);
366 wrapup:
367 /* windows does not seem to differentiate Synth from MIDI devices */
368 MODM_NumFMSynthDevs = numsynthdevs;
369 MODM_NumDevs = numsynthdevs + nummididevs;
371 MIDM_NumDevs = nummididevs;
373 /* close file and exit */
374 midiCloseSeq();
376 return 0;
379 /**************************************************************************
380 * OSS_MidiExit [internal]
382 * Release the MIDI devices information variables
384 LRESULT OSS_MidiExit(void)
386 TRACE("()\n");
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 DWORD MIDI_NotifyClient(UINT wDevID, WORD wMsg,
402 DWORD dwParam1, DWORD dwParam2)
404 DWORD dwCallBack;
405 UINT uFlags;
406 HANDLE hDev;
407 DWORD dwInstance;
409 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04X dwParam2 = %04X\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)
418 return MMSYSERR_BADDEVICEID;
420 dwCallBack = MidiOutDev[wDevID].midiDesc.dwCallback;
421 uFlags = MidiOutDev[wDevID].wFlags;
422 hDev = MidiOutDev[wDevID].midiDesc.hMidi;
423 dwInstance = MidiOutDev[wDevID].midiDesc.dwInstance;
424 break;
426 case MIM_OPEN:
427 case MIM_CLOSE:
428 case MIM_DATA:
429 case MIM_LONGDATA:
430 case MIM_ERROR:
431 case MIM_LONGERROR:
432 case MIM_MOREDATA:
433 if (wDevID > MIDM_NumDevs)
434 return MMSYSERR_BADDEVICEID;
436 dwCallBack = MidiInDev[wDevID].midiDesc.dwCallback;
437 uFlags = MidiInDev[wDevID].wFlags;
438 hDev = MidiInDev[wDevID].midiDesc.hMidi;
439 dwInstance = MidiInDev[wDevID].midiDesc.dwInstance;
440 break;
441 default:
442 WARN("Unsupported MSW-MIDI message %u\n", wMsg);
443 return MMSYSERR_ERROR;
446 return DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2) ?
447 0 : MMSYSERR_ERROR;
450 static int midi_warn = 1;
451 /**************************************************************************
452 * midiOpenSeq [internal]
454 static int midiOpenSeq(void)
456 if (numOpenMidiSeq == 0) {
457 const char* device;
458 device=getenv("MIDIDEV");
459 if (!device) device="/dev/sequencer";
460 midiSeqFD = open(device, O_RDWR, 0);
461 if (midiSeqFD == -1) {
462 if (midi_warn)
464 WARN("Can't open MIDI device '%s' ! (%s). If your "
465 "program needs this (probably not): %s\n",
466 device, strerror(errno),
467 errno == ENOENT ?
468 "create it ! (\"man MAKEDEV\" ?)" :
469 errno == ENODEV ?
470 "load MIDI sequencer kernel driver !" :
471 errno == EACCES ?
472 "grant access ! (\"man chmod\")" : ""
475 midi_warn = 0;
476 return -1;
478 #if 0
479 if (fcntl(midiSeqFD, F_SETFL, O_NONBLOCK) < 0) {
480 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno, strerror(errno));
481 close(midiSeqFD);
482 midiSeqFD = -1;
483 return -1;
485 #endif
486 fcntl(midiSeqFD, F_SETFD, 1); /* set close on exec flag */
487 ioctl(midiSeqFD, SNDCTL_SEQ_RESET);
489 numOpenMidiSeq++;
490 return 0;
493 /**************************************************************************
494 * midiCloseSeq [internal]
496 static int midiCloseSeq(void)
498 if (--numOpenMidiSeq == 0) {
499 close(midiSeqFD);
500 midiSeqFD = -1;
502 return 0;
505 /* FIXME: this is a bad idea, it's even not static... */
506 SEQ_DEFINEBUF(1024);
508 /* FIXME: this is not reentrant, not static - because of global variable
509 * _seqbuf and al.
511 /**************************************************************************
512 * seqbuf_dump [internal]
514 * Used by SEQ_DUMPBUF to flush the buffer.
517 void seqbuf_dump(void)
519 if (_seqbufptr) {
520 if (write(midiSeqFD, _seqbuf, _seqbufptr) == -1) {
521 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
522 midiSeqFD, errno, strerror(errno));
524 /* FIXME:
525 * in any case buffer is lost so that if many errors occur the buffer
526 * will not overrun
528 _seqbufptr = 0;
532 /**************************************************************************
533 * midReceiveChar [internal]
535 static void midReceiveChar(WORD wDevID, unsigned char value, DWORD dwTime)
537 DWORD toSend = 0;
539 TRACE("Adding %02xh to %d[%d]\n", value, wDevID, MidiInDev[wDevID].incLen);
541 if (wDevID >= MIDM_NumDevs) {
542 WARN("bad devID\n");
543 return;
545 if (MidiInDev[wDevID].state <= 0) {
546 TRACE("disabled or input not started, thrown away\n");
547 return;
550 if (MidiInDev[wDevID].state & 2) { /* system exclusive */
551 LPMIDIHDR lpMidiHdr;
552 WORD sbfb = FALSE;
554 EnterCriticalSection(&crit_sect);
555 if ((lpMidiHdr = MidiInDev[wDevID].lpQueueHdr) != NULL) {
556 LPBYTE lpData = (LPBYTE) lpMidiHdr->lpData;
558 lpData[lpMidiHdr->dwBytesRecorded++] = value;
559 if (lpMidiHdr->dwBytesRecorded == lpMidiHdr->dwBufferLength) {
560 sbfb = TRUE;
563 if (value == 0xF7) { /* then end */
564 MidiInDev[wDevID].state &= ~2;
565 sbfb = TRUE;
567 if (sbfb && lpMidiHdr != NULL) {
568 lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
569 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
570 lpMidiHdr->dwFlags |= MHDR_DONE;
571 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)lpMidiHdr->lpNext;
572 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD)lpMidiHdr, dwTime) != MMSYSERR_NOERROR) {
573 WARN("Couldn't notify client\n");
576 LeaveCriticalSection(&crit_sect);
577 return;
580 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
581 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
583 if (!IS_CMD(value) && MidiInDev[wDevID].incLen == 0) { /* try to reuse old cmd */
584 if (IS_CMD(MidiInDev[wDevID].incPrev) && !IS_SYS_CMD(MidiInDev[wDevID].incPrev)) {
585 MidiInDev[wDevID].incoming[0] = MidiInDev[wDevID].incPrev;
586 MidiInDev[wDevID].incLen = 1;
587 TRACE("Reusing old command %02xh\n", MidiInDev[wDevID].incPrev);
588 } else {
589 FIXME("error for midi-in, should generate MIM_ERROR notification:"
590 " prev=%02Xh, incLen=%02Xh\n",
591 MidiInDev[wDevID].incPrev, MidiInDev[wDevID].incLen);
592 return;
595 MidiInDev[wDevID].incoming[(int)(MidiInDev[wDevID].incLen++)] = value;
596 if (MidiInDev[wDevID].incLen == 1 && !IS_SYS_CMD(MidiInDev[wDevID].incoming[0])) {
597 /* store new cmd, just in case */
598 MidiInDev[wDevID].incPrev = MidiInDev[wDevID].incoming[0];
601 #undef IS_CMD
602 #undef IS_SYS_CMD
604 switch (MidiInDev[wDevID].incoming[0] & 0xF0) {
605 case MIDI_NOTEOFF:
606 case MIDI_NOTEON:
607 case MIDI_KEY_PRESSURE:
608 case MIDI_CTL_CHANGE:
609 case MIDI_PITCH_BEND:
610 if (MidiInDev[wDevID].incLen == 3) {
611 toSend = (MidiInDev[wDevID].incoming[2] << 16) |
612 (MidiInDev[wDevID].incoming[1] << 8) |
613 (MidiInDev[wDevID].incoming[0] << 0);
615 break;
616 case MIDI_PGM_CHANGE:
617 case MIDI_CHN_PRESSURE:
618 if (MidiInDev[wDevID].incLen == 2) {
619 toSend = (MidiInDev[wDevID].incoming[1] << 8) |
620 (MidiInDev[wDevID].incoming[0] << 0);
622 break;
623 case MIDI_SYSTEM_PREFIX:
624 if (MidiInDev[wDevID].incoming[0] == 0xF0) {
625 MidiInDev[wDevID].state |= 2;
626 MidiInDev[wDevID].incLen = 0;
627 } else {
628 if (MidiInDev[wDevID].incLen == 1) {
629 toSend = (MidiInDev[wDevID].incoming[0] << 0);
632 break;
633 default:
634 WARN("This shouldn't happen (%02X)\n", MidiInDev[wDevID].incoming[0]);
636 if (toSend != 0) {
637 TRACE("Sending event %08x\n", toSend);
638 MidiInDev[wDevID].incLen = 0;
639 dwTime -= MidiInDev[wDevID].startTime;
640 if (MIDI_NotifyClient(wDevID, MIM_DATA, toSend, dwTime) != MMSYSERR_NOERROR) {
641 WARN("Couldn't notify client\n");
646 static DWORD WINAPI midRecThread(LPVOID arg)
648 unsigned char buffer[256];
649 int len, idx;
650 DWORD dwTime;
651 struct pollfd pfd;
653 TRACE("Thread startup\n");
655 RENAME_CURRENT_THREAD("oss midi recorder thread");
657 pfd.fd = midiSeqFD;
658 pfd.fd = POLLIN;
660 while(!end_thread) {
661 TRACE("Thread loop\n");
663 /* Check if an event is present */
664 if (poll(&pfd, 1, 250) <= 0)
665 continue;
667 len = read(midiSeqFD, buffer, sizeof(buffer));
668 TRACE("Reveived %d bytes\n", len);
670 if (len < 0) continue;
671 if ((len % 4) != 0) {
672 WARN("Bad length %d, errno %d (%s)\n", len, errno, strerror(errno));
673 continue;
676 dwTime = GetTickCount();
678 for (idx = 0; idx < len; ) {
679 if (buffer[idx] & 0x80) {
680 TRACE(
681 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
682 buffer[idx + 0], buffer[idx + 1],
683 buffer[idx + 2], buffer[idx + 3],
684 buffer[idx + 4], buffer[idx + 5],
685 buffer[idx + 6], buffer[idx + 7]);
686 idx += 8;
687 } else {
688 switch (buffer[idx + 0]) {
689 case SEQ_WAIT:
690 case SEQ_ECHO:
691 break;
692 case SEQ_MIDIPUTC:
693 midReceiveChar(buffer[idx + 2], buffer[idx + 1], dwTime);
694 break;
695 default:
696 TRACE("Unsupported event %d\n", buffer[idx + 0]);
697 break;
699 idx += 4;
703 return 0;
706 /**************************************************************************
707 * midGetDevCaps [internal]
709 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPSW lpCaps, DWORD dwSize)
711 TRACE("(%04X, %p, %08X);\n", wDevID, lpCaps, dwSize);
713 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
714 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
716 memcpy(lpCaps, &MidiInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
718 return MMSYSERR_NOERROR;
721 /**************************************************************************
722 * midOpen [internal]
724 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
726 TRACE("(%04X, %p, %08X);\n", wDevID, lpDesc, dwFlags);
728 if (lpDesc == NULL) {
729 WARN("Invalid Parameter !\n");
730 return MMSYSERR_INVALPARAM;
733 /* FIXME :
734 * how to check that content of lpDesc is correct ?
736 if (wDevID >= MIDM_NumDevs) {
737 WARN("wDevID too large (%u) !\n", wDevID);
738 return MMSYSERR_BADDEVICEID;
740 if (MidiInDev[wDevID].state == -1) {
741 WARN("device disabled\n");
742 return MIDIERR_NODEVICE;
744 if (MidiInDev[wDevID].midiDesc.hMidi != 0) {
745 WARN("device already open !\n");
746 return MMSYSERR_ALLOCATED;
748 if ((dwFlags & MIDI_IO_STATUS) != 0) {
749 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
750 dwFlags &= ~MIDI_IO_STATUS;
752 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
753 FIXME("Bad dwFlags\n");
754 return MMSYSERR_INVALFLAG;
757 if (midiOpenSeq() < 0) {
758 return MMSYSERR_ERROR;
761 if (numStartedMidiIn++ == 0) {
762 end_thread = 0;
763 hThread = CreateThread(NULL, 0, midRecThread, NULL, 0, NULL);
764 if (!hThread) {
765 numStartedMidiIn = 0;
766 WARN("Couldn't create thread for midi-in\n");
767 midiCloseSeq();
768 return MMSYSERR_ERROR;
770 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
771 TRACE("Created thread for midi-in\n");
774 MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
776 MidiInDev[wDevID].lpQueueHdr = NULL;
777 MidiInDev[wDevID].dwTotalPlayed = 0;
778 MidiInDev[wDevID].bufsize = 0x3FFF;
779 MidiInDev[wDevID].midiDesc = *lpDesc;
780 MidiInDev[wDevID].state = 0;
781 MidiInDev[wDevID].incLen = 0;
782 MidiInDev[wDevID].startTime = 0;
784 if (MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
785 WARN("can't notify client !\n");
786 return MMSYSERR_INVALPARAM;
788 return MMSYSERR_NOERROR;
791 /**************************************************************************
792 * midClose [internal]
794 static DWORD midClose(WORD wDevID)
796 int ret = MMSYSERR_NOERROR;
798 TRACE("(%04X);\n", wDevID);
800 if (wDevID >= MIDM_NumDevs) {
801 WARN("wDevID too big (%u) !\n", wDevID);
802 return MMSYSERR_BADDEVICEID;
804 if (MidiInDev[wDevID].midiDesc.hMidi == 0) {
805 WARN("device not opened !\n");
806 return MMSYSERR_ERROR;
808 if (MidiInDev[wDevID].lpQueueHdr != 0) {
809 return MIDIERR_STILLPLAYING;
812 if (midiSeqFD == -1) {
813 WARN("ooops !\n");
814 return MMSYSERR_ERROR;
816 if (--numStartedMidiIn == 0) {
817 TRACE("Stopping thread for midi-in\n");
818 end_thread = 1;
819 if (WaitForSingleObject(hThread, 5000) != WAIT_OBJECT_0) {
820 WARN("Thread end not signaled, force termination\n");
821 TerminateThread(hThread, 0);
823 TRACE("Stopped thread for midi-in\n");
825 midiCloseSeq();
827 MidiInDev[wDevID].bufsize = 0;
828 if (MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
829 WARN("can't notify client !\n");
830 ret = MMSYSERR_INVALPARAM;
832 MidiInDev[wDevID].midiDesc.hMidi = 0;
833 return ret;
836 /**************************************************************************
837 * midAddBuffer [internal]
839 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
841 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
843 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
844 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
846 if (lpMidiHdr == NULL) return MMSYSERR_INVALPARAM;
847 if (sizeof(MIDIHDR) > dwSize) return MMSYSERR_INVALPARAM;
848 if (lpMidiHdr->dwBufferLength == 0) return MMSYSERR_INVALPARAM;
849 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
850 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
852 EnterCriticalSection(&crit_sect);
853 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
854 if (MidiInDev[wDevID].lpQueueHdr == 0) {
855 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr;
856 } else {
857 LPMIDIHDR ptr;
859 for (ptr = MidiInDev[wDevID].lpQueueHdr;
860 ptr->lpNext != 0;
861 ptr = (LPMIDIHDR)ptr->lpNext);
862 ptr->lpNext = (struct midihdr_tag*)lpMidiHdr;
864 LeaveCriticalSection(&crit_sect);
866 return MMSYSERR_NOERROR;
869 /**************************************************************************
870 * midPrepare [internal]
872 static DWORD midPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
874 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
876 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
877 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
878 lpMidiHdr->dwBufferLength >= 0x10000ul)
879 return MMSYSERR_INVALPARAM;
881 lpMidiHdr->lpNext = 0;
882 lpMidiHdr->dwFlags |= MHDR_PREPARED;
883 lpMidiHdr->dwBytesRecorded = 0;
885 return MMSYSERR_NOERROR;
888 /**************************************************************************
889 * midUnprepare [internal]
891 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
893 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
895 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
896 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
898 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
899 lpMidiHdr->lpData == 0 || lpMidiHdr->dwBufferLength >= 0x10000ul)
900 return MMSYSERR_INVALPARAM;
902 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
903 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
905 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
907 return MMSYSERR_NOERROR;
910 /**************************************************************************
911 * midReset [internal]
913 static DWORD midReset(WORD wDevID)
915 DWORD dwTime = GetTickCount();
917 TRACE("(%04X);\n", wDevID);
919 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
920 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
922 EnterCriticalSection(&crit_sect);
923 while (MidiInDev[wDevID].lpQueueHdr) {
924 MidiInDev[wDevID].lpQueueHdr->dwFlags &= ~MHDR_INQUEUE;
925 MidiInDev[wDevID].lpQueueHdr->dwFlags |= MHDR_DONE;
926 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
927 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA,
928 (DWORD)MidiInDev[wDevID].lpQueueHdr, dwTime) != MMSYSERR_NOERROR) {
929 WARN("Couldn't notify client\n");
931 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)MidiInDev[wDevID].lpQueueHdr->lpNext;
933 LeaveCriticalSection(&crit_sect);
935 return MMSYSERR_NOERROR;
939 /**************************************************************************
940 * midStart [internal]
942 static DWORD midStart(WORD wDevID)
944 TRACE("(%04X);\n", wDevID);
946 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
947 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
949 MidiInDev[wDevID].state = 1;
950 MidiInDev[wDevID].startTime = GetTickCount();
951 return MMSYSERR_NOERROR;
954 /**************************************************************************
955 * midStop [internal]
957 static DWORD midStop(WORD wDevID)
959 TRACE("(%04X);\n", wDevID);
961 if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
962 if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
964 MidiInDev[wDevID].state = 0;
965 return MMSYSERR_NOERROR;
968 /*-----------------------------------------------------------------------*/
970 typedef struct sVoice {
971 int note; /* 0 means not used */
972 int channel;
973 unsigned cntMark : 30,
974 status : 2;
975 #define sVS_UNUSED 0
976 #define sVS_PLAYING 1
977 #define sVS_SUSTAINED 2
978 } sVoice;
980 typedef struct sChannel {
981 int program;
983 int bender;
984 int benderRange;
985 /* controlers */
986 int bank; /* CTL_BANK_SELECT */
987 int volume; /* CTL_MAIN_VOLUME */
988 int balance; /* CTL_BALANCE */
989 int expression; /* CTL_EXPRESSION */
990 int sustain; /* CTL_SUSTAIN */
992 unsigned char nrgPmtMSB; /* Non register Parameters */
993 unsigned char nrgPmtLSB;
994 unsigned char regPmtMSB; /* Non register Parameters */
995 unsigned char regPmtLSB;
996 } sChannel;
998 typedef struct sFMextra {
999 unsigned counter;
1000 int drumSetMask;
1001 sChannel channel[16]; /* MIDI has only 16 channels */
1002 sVoice voice[1]; /* dyn allocated according to sound card */
1003 /* do not append fields below voice[1] since the size of this structure
1004 * depends on the number of available voices on the FM synth...
1006 } sFMextra;
1008 extern const unsigned char midiFMInstrumentPatches[16 * 128];
1009 extern const unsigned char midiFMDrumsPatches [16 * 128];
1011 /**************************************************************************
1012 * modFMLoad [internal]
1014 static int modFMLoad(int dev)
1016 int i;
1017 struct sbi_instrument sbi;
1019 sbi.device = dev;
1020 sbi.key = FM_PATCH;
1022 memset(sbi.operators + 16, 0, 16);
1023 for (i = 0; i < 128; i++) {
1024 sbi.channel = i;
1025 memcpy(sbi.operators, midiFMInstrumentPatches + i * 16, 16);
1027 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
1028 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
1029 return -1;
1032 for (i = 0; i < 128; i++) {
1033 sbi.channel = 128 + i;
1034 memcpy(sbi.operators, midiFMDrumsPatches + i * 16, 16);
1036 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
1037 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
1038 return -1;
1041 return 0;
1044 /**************************************************************************
1045 * modFMReset [internal]
1047 static void modFMReset(WORD wDevID)
1049 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
1050 sVoice* voice = extra->voice;
1051 sChannel* channel = extra->channel;
1052 int i;
1054 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1055 if (voice[i].status != sVS_UNUSED) {
1056 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1058 SEQ_KEY_PRESSURE(wDevID, i, 127, 0);
1059 SEQ_CONTROL(wDevID, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
1060 voice[i].note = 0;
1061 voice[i].channel = -1;
1062 voice[i].cntMark = 0;
1063 voice[i].status = sVS_UNUSED;
1065 for (i = 0; i < 16; i++) {
1066 channel[i].program = 0;
1067 channel[i].bender = 8192;
1068 channel[i].benderRange = 2;
1069 channel[i].bank = 0;
1070 channel[i].volume = 127;
1071 channel[i].balance = 64;
1072 channel[i].expression = 0;
1073 channel[i].sustain = 0;
1075 extra->counter = 0;
1076 extra->drumSetMask = 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
1077 SEQ_DUMPBUF();
1080 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
1082 /**************************************************************************
1083 * modGetDevCaps [internal]
1085 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPSW lpCaps, DWORD dwSize)
1087 TRACE("(%04X, %p, %08X);\n", wDevID, lpCaps, dwSize);
1089 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1090 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
1092 memcpy(lpCaps, &MidiOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1094 return MMSYSERR_NOERROR;
1097 /**************************************************************************
1098 * modOpen [internal]
1100 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
1102 TRACE("(%04X, %p, %08X);\n", wDevID, lpDesc, dwFlags);
1103 if (lpDesc == NULL) {
1104 WARN("Invalid Parameter !\n");
1105 return MMSYSERR_INVALPARAM;
1107 if (wDevID >= MODM_NumDevs) {
1108 TRACE("MAX_MIDIOUTDRV reached !\n");
1109 return MMSYSERR_BADDEVICEID;
1111 if (MidiOutDev[wDevID].midiDesc.hMidi != 0) {
1112 WARN("device already open !\n");
1113 return MMSYSERR_ALLOCATED;
1115 if (!MidiOutDev[wDevID].bEnabled) {
1116 WARN("device disabled !\n");
1117 return MIDIERR_NODEVICE;
1119 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
1120 WARN("bad dwFlags\n");
1121 return MMSYSERR_INVALFLAG;
1123 if (!MidiOutDev[wDevID].bEnabled) {
1124 TRACE("disabled wDevID\n");
1125 return MMSYSERR_NOTENABLED;
1128 MidiOutDev[wDevID].lpExtra = 0;
1130 switch (MidiOutDev[wDevID].caps.wTechnology) {
1131 case MOD_FMSYNTH:
1133 void* extra;
1135 extra = HeapAlloc(GetProcessHeap(), 0,
1136 sizeof(struct sFMextra) +
1137 sizeof(struct sVoice) * (MidiOutDev[wDevID].caps.wVoices - 1));
1139 if (extra == 0) {
1140 WARN("can't alloc extra data !\n");
1141 return MMSYSERR_NOMEM;
1143 MidiOutDev[wDevID].lpExtra = extra;
1144 if (midiOpenSeq() < 0) {
1145 MidiOutDev[wDevID].lpExtra = 0;
1146 HeapFree(GetProcessHeap(), 0, extra);
1147 return MMSYSERR_ERROR;
1149 if (modFMLoad(wDevID) < 0) {
1150 midiCloseSeq();
1151 MidiOutDev[wDevID].lpExtra = 0;
1152 HeapFree(GetProcessHeap(), 0, extra);
1153 return MMSYSERR_ERROR;
1155 modFMReset(wDevID);
1157 break;
1158 case MOD_MIDIPORT:
1159 case MOD_SYNTH:
1160 if (midiOpenSeq() < 0) {
1161 return MMSYSERR_ALLOCATED;
1163 break;
1164 default:
1165 WARN("Technology not supported (yet) %d !\n",
1166 MidiOutDev[wDevID].caps.wTechnology);
1167 return MMSYSERR_NOTENABLED;
1170 MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1172 MidiOutDev[wDevID].lpQueueHdr = NULL;
1173 MidiOutDev[wDevID].dwTotalPlayed = 0;
1174 MidiOutDev[wDevID].bufsize = 0x3FFF;
1175 MidiOutDev[wDevID].midiDesc = *lpDesc;
1177 if (MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
1178 WARN("can't notify client !\n");
1179 return MMSYSERR_INVALPARAM;
1181 TRACE("Successful !\n");
1182 return MMSYSERR_NOERROR;
1186 /**************************************************************************
1187 * modClose [internal]
1189 static DWORD modClose(WORD wDevID)
1191 int ret = MMSYSERR_NOERROR;
1193 TRACE("(%04X);\n", wDevID);
1195 if (MidiOutDev[wDevID].midiDesc.hMidi == 0) {
1196 WARN("device not opened !\n");
1197 return MMSYSERR_ERROR;
1199 /* FIXME: should test that no pending buffer is still in the queue for
1200 * playing */
1202 if (midiSeqFD == -1) {
1203 WARN("can't close !\n");
1204 return MMSYSERR_ERROR;
1207 switch (MidiOutDev[wDevID].caps.wTechnology) {
1208 case MOD_FMSYNTH:
1209 case MOD_MIDIPORT:
1210 midiCloseSeq();
1211 break;
1212 default:
1213 WARN("Technology not supported (yet) %d !\n",
1214 MidiOutDev[wDevID].caps.wTechnology);
1215 return MMSYSERR_NOTENABLED;
1218 HeapFree(GetProcessHeap(), 0, MidiOutDev[wDevID].lpExtra);
1219 MidiOutDev[wDevID].lpExtra = 0;
1221 MidiOutDev[wDevID].bufsize = 0;
1222 if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
1223 WARN("can't notify client !\n");
1224 ret = MMSYSERR_INVALPARAM;
1226 MidiOutDev[wDevID].midiDesc.hMidi = 0;
1227 return ret;
1230 /**************************************************************************
1231 * modData [internal]
1233 static DWORD modData(WORD wDevID, DWORD dwParam)
1235 WORD evt = LOBYTE(LOWORD(dwParam));
1236 WORD d1 = HIBYTE(LOWORD(dwParam));
1237 WORD d2 = LOBYTE(HIWORD(dwParam));
1239 TRACE("(%04X, %08X);\n", wDevID, dwParam);
1241 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1242 if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1244 if (midiSeqFD == -1) {
1245 WARN("can't play !\n");
1246 return MIDIERR_NODEVICE;
1248 switch (MidiOutDev[wDevID].caps.wTechnology) {
1249 case MOD_FMSYNTH:
1250 /* FIXME:
1251 * - chorus depth controller is not used
1254 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
1255 sVoice* voice = extra->voice;
1256 sChannel* channel = extra->channel;
1257 int chn = (evt & 0x0F);
1258 int i, nv;
1260 switch (evt & 0xF0) {
1261 case MIDI_NOTEOFF:
1262 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1263 /* don't stop sustained notes */
1264 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1265 voice[i].status = sVS_UNUSED;
1266 SEQ_STOP_NOTE(wDevID, i, d1, d2);
1269 break;
1270 case MIDI_NOTEON:
1271 if (d2 == 0) { /* note off if velocity == 0 */
1272 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1273 /* don't stop sustained notes */
1274 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1275 voice[i].status = sVS_UNUSED;
1276 SEQ_STOP_NOTE(wDevID, i, d1, 64);
1279 break;
1281 /* finding out in this order :
1282 * - an empty voice
1283 * - if replaying the same note on the same channel
1284 * - the older voice (LRU)
1286 for (i = nv = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1287 if (voice[i].status == sVS_UNUSED ||
1288 (voice[i].note == d1 && voice[i].channel == chn)) {
1289 nv = i;
1290 break;
1292 if (voice[i].cntMark < voice[0].cntMark) {
1293 nv = i;
1296 TRACE(
1297 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1298 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1299 nv, channel[chn].program,
1300 channel[chn].balance,
1301 channel[chn].volume,
1302 channel[chn].bender, d1, d2);
1304 SEQ_SET_PATCH(wDevID, nv, IS_DRUM_CHANNEL(extra, chn) ?
1305 (128 + d1) : channel[chn].program);
1306 SEQ_BENDER_RANGE(wDevID, nv, channel[chn].benderRange * 100);
1307 SEQ_BENDER(wDevID, nv, channel[chn].bender);
1308 SEQ_CONTROL(wDevID, nv, CTL_PAN, channel[chn].balance);
1309 SEQ_CONTROL(wDevID, nv, CTL_EXPRESSION, channel[chn].expression);
1310 #if 0
1311 /* FIXME: does not really seem to work on my SB card and
1312 * screws everything up... so lay it down
1314 SEQ_CONTROL(wDevID, nv, CTL_MAIN_VOLUME, channel[chn].volume);
1315 #endif
1316 SEQ_START_NOTE(wDevID, nv, d1, d2);
1317 voice[nv].status = channel[chn].sustain ? sVS_SUSTAINED : sVS_PLAYING;
1318 voice[nv].note = d1;
1319 voice[nv].channel = chn;
1320 voice[nv].cntMark = extra->counter++;
1321 break;
1322 case MIDI_KEY_PRESSURE:
1323 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1324 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn && voice[i].note == d1) {
1325 SEQ_KEY_PRESSURE(wDevID, i, d1, d2);
1328 break;
1329 case MIDI_CTL_CHANGE:
1330 switch (d1) {
1331 case CTL_BANK_SELECT: channel[chn].bank = d2; break;
1332 case CTL_MAIN_VOLUME: channel[chn].volume = d2; break;
1333 case CTL_PAN: channel[chn].balance = d2; break;
1334 case CTL_EXPRESSION: channel[chn].expression = d2; break;
1335 case CTL_SUSTAIN: channel[chn].sustain = d2;
1336 if (d2) {
1337 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1338 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1339 voice[i].status = sVS_SUSTAINED;
1342 } else {
1343 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1344 if (voice[i].status == sVS_SUSTAINED && voice[i].channel == chn) {
1345 voice[i].status = sVS_UNUSED;
1346 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1350 break;
1351 case CTL_NONREG_PARM_NUM_LSB: channel[chn].nrgPmtLSB = d2; break;
1352 case CTL_NONREG_PARM_NUM_MSB: channel[chn].nrgPmtMSB = d2; break;
1353 case CTL_REGIST_PARM_NUM_LSB: channel[chn].regPmtLSB = d2; break;
1354 case CTL_REGIST_PARM_NUM_MSB: channel[chn].regPmtMSB = d2; break;
1355 case CTL_DATA_ENTRY:
1356 switch ((channel[chn].regPmtMSB << 8) | channel[chn].regPmtLSB) {
1357 case 0x0000:
1358 if (channel[chn].benderRange != d2) {
1359 channel[chn].benderRange = d2;
1360 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1361 if (voice[i].channel == chn) {
1362 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1366 break;
1368 case 0x7F7F:
1369 channel[chn].benderRange = 2;
1370 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1371 if (voice[i].channel == chn) {
1372 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1375 break;
1376 default:
1377 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1378 channel[chn].regPmtMSB, channel[chn].regPmtLSB,
1379 channel[chn].nrgPmtMSB, channel[chn].nrgPmtLSB,
1380 d2);
1381 break;
1383 break;
1385 case 0x78: /* all sounds off */
1386 /* FIXME: I don't know if I have to take care of the channel
1387 * for this control ?
1389 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1390 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1391 voice[i].status = sVS_UNUSED;
1392 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1395 break;
1396 case 0x7B: /* all notes off */
1397 /* FIXME: I don't know if I have to take care of the channel
1398 * for this control ?
1400 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1401 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1402 voice[i].status = sVS_UNUSED;
1403 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1406 break;
1407 default:
1408 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1409 d1, d2, chn);
1410 break;
1412 break;
1413 case MIDI_PGM_CHANGE:
1414 channel[chn].program = d1;
1415 break;
1416 case MIDI_CHN_PRESSURE:
1417 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1418 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1419 SEQ_KEY_PRESSURE(wDevID, i, voice[i].note, d1);
1422 break;
1423 case MIDI_PITCH_BEND:
1424 channel[chn].bender = (d2 << 7) + d1;
1425 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1426 if (voice[i].channel == chn) {
1427 SEQ_BENDER(wDevID, i, channel[chn].bender);
1430 break;
1431 case MIDI_SYSTEM_PREFIX:
1432 switch (evt & 0x0F) {
1433 case 0x0F: /* Reset */
1434 modFMReset(wDevID);
1435 break;
1436 default:
1437 WARN("Unsupported (yet) system event %02x\n", evt & 0x0F);
1439 break;
1440 default:
1441 WARN("Internal error, shouldn't happen (event=%08x)\n", evt & 0xF0);
1442 return MMSYSERR_NOTENABLED;
1445 break;
1446 case MOD_MIDIPORT:
1448 int dev = wDevID - MODM_NumFMSynthDevs;
1449 if (dev < 0) {
1450 WARN("Internal error on devID (%u) !\n", wDevID);
1451 return MIDIERR_NODEVICE;
1454 switch (evt & 0xF0) {
1455 case MIDI_NOTEOFF:
1456 case MIDI_NOTEON:
1457 case MIDI_KEY_PRESSURE:
1458 case MIDI_CTL_CHANGE:
1459 case MIDI_PITCH_BEND:
1460 SEQ_MIDIOUT(dev, evt);
1461 SEQ_MIDIOUT(dev, d1);
1462 SEQ_MIDIOUT(dev, d2);
1463 break;
1464 case MIDI_PGM_CHANGE:
1465 case MIDI_CHN_PRESSURE:
1466 SEQ_MIDIOUT(dev, evt);
1467 SEQ_MIDIOUT(dev, d1);
1468 break;
1469 case MIDI_SYSTEM_PREFIX:
1470 switch (evt & 0x0F) {
1471 case 0x00: /* System Exclusive, don't do it on modData,
1472 * should require modLongData*/
1473 case 0x01: /* Undefined */
1474 case 0x04: /* Undefined. */
1475 case 0x05: /* Undefined. */
1476 case 0x07: /* End of Exclusive. */
1477 case 0x09: /* Undefined. */
1478 case 0x0D: /* Undefined. */
1479 break;
1480 case 0x06: /* Tune Request */
1481 case 0x08: /* Timing Clock. */
1482 case 0x0A: /* Start. */
1483 case 0x0B: /* Continue */
1484 case 0x0C: /* Stop */
1485 case 0x0E: /* Active Sensing. */
1486 SEQ_MIDIOUT(dev, evt);
1487 break;
1488 case 0x0F: /* Reset */
1489 /* SEQ_MIDIOUT(dev, evt);
1490 this other way may be better */
1491 SEQ_MIDIOUT(dev, MIDI_SYSTEM_PREFIX);
1492 SEQ_MIDIOUT(dev, 0x7e);
1493 SEQ_MIDIOUT(dev, 0x7f);
1494 SEQ_MIDIOUT(dev, 0x09);
1495 SEQ_MIDIOUT(dev, 0x01);
1496 SEQ_MIDIOUT(dev, 0xf7);
1497 break;
1498 case 0x03: /* Song Select. */
1499 SEQ_MIDIOUT(dev, evt);
1500 SEQ_MIDIOUT(dev, d1);
1501 case 0x02: /* Song Position Pointer. */
1502 SEQ_MIDIOUT(dev, evt);
1503 SEQ_MIDIOUT(dev, d1);
1504 SEQ_MIDIOUT(dev, d2);
1506 break;
1509 break;
1510 default:
1511 WARN("Technology not supported (yet) %d !\n",
1512 MidiOutDev[wDevID].caps.wTechnology);
1513 return MMSYSERR_NOTENABLED;
1516 SEQ_DUMPBUF();
1518 return MMSYSERR_NOERROR;
1521 /**************************************************************************
1522 * modLongData [internal]
1524 static DWORD modLongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1526 int count;
1527 LPBYTE lpData;
1529 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
1531 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
1532 * but it seems to be used only for midi input.
1533 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
1536 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1537 if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1539 if (midiSeqFD == -1) {
1540 WARN("can't play !\n");
1541 return MIDIERR_NODEVICE;
1544 lpData = (LPBYTE) lpMidiHdr->lpData;
1546 if (lpData == NULL)
1547 return MIDIERR_UNPREPARED;
1548 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1549 return MIDIERR_UNPREPARED;
1550 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1551 return MIDIERR_STILLPLAYING;
1552 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1553 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1555 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1556 * data, or can it also contain raw MIDI data, to be split up and sent to
1557 * modShortData() ?
1558 * If the latest is true, then the following WARNing will fire up
1560 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
1561 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1564 TRACE("dwBufferLength=%u !\n", lpMidiHdr->dwBufferLength);
1565 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1566 lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
1567 lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
1569 switch (MidiOutDev[wDevID].caps.wTechnology) {
1570 case MOD_FMSYNTH:
1571 /* FIXME: I don't think there is much to do here */
1572 break;
1573 case MOD_MIDIPORT:
1574 if (lpData[0] != 0xF0) {
1575 /* Send end of System Exclusive */
1576 SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, 0xF0);
1577 WARN("Adding missing 0xF0 marker at the beginning of "
1578 "system exclusive byte stream\n");
1580 for (count = 0; count < lpMidiHdr->dwBufferLength; count++) {
1581 SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, lpData[count]);
1583 if (lpData[count - 1] != 0xF7) {
1584 /* Send end of System Exclusive */
1585 SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, 0xF7);
1586 WARN("Adding missing 0xF7 marker at the end of "
1587 "system exclusive byte stream\n");
1589 SEQ_DUMPBUF();
1590 break;
1591 default:
1592 WARN("Technology not supported (yet) %d !\n",
1593 MidiOutDev[wDevID].caps.wTechnology);
1594 return MMSYSERR_NOTENABLED;
1597 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1598 lpMidiHdr->dwFlags |= MHDR_DONE;
1599 if (MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD)lpMidiHdr, 0L) != MMSYSERR_NOERROR) {
1600 WARN("can't notify client !\n");
1601 return MMSYSERR_INVALPARAM;
1603 return MMSYSERR_NOERROR;
1606 /**************************************************************************
1607 * modPrepare [internal]
1609 static DWORD modPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1611 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
1613 if (midiSeqFD == -1) {
1614 WARN("can't prepare !\n");
1615 return MMSYSERR_NOTENABLED;
1618 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
1619 * asks to prepare MIDIHDR which dwFlags != 0.
1620 * So at least check for the inqueue flag
1622 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
1623 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
1624 lpMidiHdr->dwBufferLength >= 0x10000ul) {
1625 WARN("%p %p %08x %d/%d\n", lpMidiHdr, lpMidiHdr->lpData,
1626 lpMidiHdr->dwFlags, sizeof(MIDIHDR), dwSize);
1627 return MMSYSERR_INVALPARAM;
1630 lpMidiHdr->lpNext = 0;
1631 lpMidiHdr->dwFlags |= MHDR_PREPARED;
1632 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1633 return MMSYSERR_NOERROR;
1636 /**************************************************************************
1637 * modUnprepare [internal]
1639 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1641 TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
1643 if (midiSeqFD == -1) {
1644 WARN("can't unprepare !\n");
1645 return MMSYSERR_NOTENABLED;
1648 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0)
1649 return MMSYSERR_INVALPARAM;
1650 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1651 return MIDIERR_STILLPLAYING;
1652 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
1653 return MMSYSERR_NOERROR;
1656 /**************************************************************************
1657 * modReset [internal]
1659 static DWORD modReset(WORD wDevID)
1661 unsigned chn;
1663 TRACE("(%04X);\n", wDevID);
1665 if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1666 if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1668 /* stop all notes */
1669 /* FIXME: check if 0x78B0 is channel dependent or not. I coded it so that
1670 * it's channel dependent...
1672 for (chn = 0; chn < 16; chn++) {
1673 /* turn off every note */
1674 modData(wDevID, 0x7800 | MIDI_CTL_CHANGE | chn);
1675 /* remove sustain on all channels */
1676 modData(wDevID, (CTL_SUSTAIN << 8) | MIDI_CTL_CHANGE | chn);
1678 /* FIXME: the LongData buffers must also be returned to the app */
1679 return MMSYSERR_NOERROR;
1682 #else /* HAVE_OSS_MIDI */
1684 LRESULT OSS_MidiInit(void)
1686 TRACE("()\n");
1687 return FALSE;
1690 LRESULT OSS_MidiExit(void)
1692 TRACE("()\n");
1693 return 0;
1697 #endif /* HAVE_OSS_MIDI */
1699 /*======================================================================*
1700 * MIDI entry points *
1701 *======================================================================*/
1703 /**************************************************************************
1704 * midMessage (WINEOSS.4)
1706 DWORD WINAPI OSS_midMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1707 DWORD dwParam1, DWORD dwParam2)
1709 TRACE("(%04X, %04X, %08X, %08X, %08X);\n",
1710 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1711 switch (wMsg) {
1712 #ifdef HAVE_OSS_MIDI
1713 case DRVM_INIT:
1714 case DRVM_EXIT:
1715 case DRVM_ENABLE:
1716 case DRVM_DISABLE:
1717 /* FIXME: Pretend this is supported */
1718 return 0;
1719 case MIDM_OPEN:
1720 return midOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1721 case MIDM_CLOSE:
1722 return midClose(wDevID);
1723 case MIDM_ADDBUFFER:
1724 return midAddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1725 case MIDM_PREPARE:
1726 return midPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1727 case MIDM_UNPREPARE:
1728 return midUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1729 case MIDM_GETDEVCAPS:
1730 return midGetDevCaps(wDevID, (LPMIDIINCAPSW)dwParam1,dwParam2);
1731 case MIDM_GETNUMDEVS:
1732 return MIDM_NumDevs;
1733 case MIDM_RESET:
1734 return midReset(wDevID);
1735 case MIDM_START:
1736 return midStart(wDevID);
1737 case MIDM_STOP:
1738 return midStop(wDevID);
1739 #endif
1740 default:
1741 TRACE("Unsupported message\n");
1743 return MMSYSERR_NOTSUPPORTED;
1746 /**************************************************************************
1747 * modMessage (WINEOSS.5)
1749 DWORD WINAPI OSS_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1750 DWORD dwParam1, DWORD dwParam2)
1752 TRACE("(%04X, %04X, %08X, %08X, %08X);\n",
1753 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1755 switch (wMsg) {
1756 #ifdef HAVE_OSS_MIDI
1757 case DRVM_INIT:
1758 case DRVM_EXIT:
1759 case DRVM_ENABLE:
1760 case DRVM_DISABLE:
1761 /* FIXME: Pretend this is supported */
1762 return 0;
1763 case MODM_OPEN:
1764 return modOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1765 case MODM_CLOSE:
1766 return modClose(wDevID);
1767 case MODM_DATA:
1768 return modData(wDevID, dwParam1);
1769 case MODM_LONGDATA:
1770 return modLongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1771 case MODM_PREPARE:
1772 return modPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1773 case MODM_UNPREPARE:
1774 return modUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1775 case MODM_GETDEVCAPS:
1776 return modGetDevCaps(wDevID, (LPMIDIOUTCAPSW)dwParam1, dwParam2);
1777 case MODM_GETNUMDEVS:
1778 return MODM_NumDevs;
1779 case MODM_GETVOLUME:
1780 return 0;
1781 case MODM_SETVOLUME:
1782 return 0;
1783 case MODM_RESET:
1784 return modReset(wDevID);
1785 #endif
1786 default:
1787 TRACE("Unsupported message\n");
1789 return MMSYSERR_NOTSUPPORTED;
1792 /*-----------------------------------------------------------------------*/