- removed (now obsolete) driver.h file (it's content is now in mmsystem.h)
[wine/multimedia.git] / dlls / winmm / wineoss / midi.c
blob9fdbeebe75825f46b64a23b39cd63d81a78172a6
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
15 #include "config.h"
17 #include <string.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <sys/ioctl.h>
21 #include "windef.h"
22 #include "wingdi.h"
23 #include "winuser.h"
24 #include "mmddk.h"
25 #include "oss.h"
26 #include "debugtools.h"
27 #include "heap.h"
28 #include "ldt.h"
30 DEFAULT_DEBUG_CHANNEL(midi)
32 #ifdef HAVE_OSS_MIDI
34 #define MIDI_SEQ "/dev/sequencer"
36 typedef struct {
37 int state;
38 DWORD bufsize;
39 LPMIDIOPENDESC midiDesc;
40 WORD wFlags;
41 LPMIDIHDR lpQueueHdr;
42 DWORD dwTotalPlayed;
43 unsigned char incoming[3];
44 unsigned char incPrev;
45 char incLen;
46 DWORD startTime;
47 } WINE_MIDIIN;
49 typedef struct {
50 int state;
51 DWORD bufsize;
52 LPMIDIOPENDESC midiDesc;
53 WORD wFlags;
54 LPMIDIHDR lpQueueHdr;
55 DWORD dwTotalPlayed;
56 void* lpExtra; /* according to port type (MIDI, FM...), extra data when needed */
57 } WINE_MIDIOUT;
59 static WINE_MIDIIN MidiInDev [MAX_MIDIINDRV ];
60 static WINE_MIDIOUT MidiOutDev[MAX_MIDIOUTDRV];
62 /* this is the total number of MIDI out devices found */
63 static int MODM_NUMDEVS = 0;
64 /* this is the number of FM synthetizers (index from 0 to
65 NUMFMSYNTHDEVS - 1) */
66 static int MODM_NUMFMSYNTHDEVS = 0;
67 /* this is the number of Midi ports (index from NUMFMSYNTHDEVS to
68 NUMFMSYNTHDEVS + NUMMIDIDEVS - 1) */
69 static int MODM_NUMMIDIDEVS = 0;
71 /* this is the total number of MIDI out devices found */
72 static int MIDM_NUMDEVS = 0;
74 static int midiSeqFD = -1;
75 static int numOpenMidiSeq = 0;
76 static UINT midiInTimerID = 0;
77 static int numStartedMidiIn = 0;
79 /* this structure holds pointers with information for each MIDI
80 * out device found.
82 static LPMIDIOUTCAPSA midiOutDevices[MAX_MIDIOUTDRV];
84 /* this structure holds pointers with information for each MIDI
85 * in device found.
87 static LPMIDIINCAPSA midiInDevices [MAX_MIDIINDRV];
89 /*
90 * FIXME : all tests on device ID for midXXX and modYYY are made against
91 * MAX_MIDIxxDRV (when they are made) but should be done against the actual
92 * number of midi devices found...
95 /*======================================================================*
96 * Low level MIDI implementation *
97 *======================================================================*/
99 static int midiOpenSeq(void);
100 static int midiCloseSeq(void);
102 /**************************************************************************
103 * unixToWindowsDeviceType [internal]
105 * return the Windows equivalent to a Unix Device Type
108 static int MIDI_UnixToWindowsDeviceType(int type)
110 /* MOD_MIDIPORT output port
111 * MOD_SYNTH generic internal synth
112 * MOD_SQSYNTH square wave internal synth
113 * MOD_FMSYNTH FM internal synth
114 * MOD_MAPPER MIDI mapper
117 /* FIXME Is this really the correct equivalence from UNIX to
118 Windows Sound type */
120 switch (type) {
121 case SYNTH_TYPE_FM: return MOD_FMSYNTH;
122 case SYNTH_TYPE_SAMPLE: return MOD_SYNTH;
123 case SYNTH_TYPE_MIDI: return MOD_MIDIPORT;
124 default:
125 ERR("Cannot determine the type of this midi device. "
126 "Assuming FM Synth\n");
127 return MOD_FMSYNTH;
129 return MOD_FMSYNTH;
132 /**************************************************************************
133 * OSS_MidiInit [internal]
135 * Initializes the MIDI devices information variables
137 BOOL OSS_MidiInit(void)
139 int i, status, numsynthdevs = 255, nummididevs = 255;
140 struct synth_info sinfo;
141 struct midi_info minfo;
142 static BOOL bInitDone = FALSE;
144 if (bInitDone)
145 return TRUE;
147 TRACE("Initializing the MIDI variables.\n");
148 bInitDone = TRUE;
150 /* try to open device */
151 if (midiOpenSeq() == -1) {
152 return TRUE;
155 /* find how many Synth devices are there in the system */
156 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRSYNTHS, &numsynthdevs);
158 if (status == -1) {
159 ERR("ioctl for nr synth failed.\n");
160 midiCloseSeq();
161 return TRUE;
164 if (numsynthdevs > MAX_MIDIOUTDRV) {
165 ERR("MAX_MIDIOUTDRV (%d) was enough for the number of devices (%d). "
166 "Some FM devices will not be available.\n",MAX_MIDIOUTDRV,numsynthdevs);
167 numsynthdevs = MAX_MIDIOUTDRV;
170 for (i = 0; i < numsynthdevs; i++) {
171 LPMIDIOUTCAPSA tmplpCaps;
173 sinfo.device = i;
174 status = ioctl(midiSeqFD, SNDCTL_SYNTH_INFO, &sinfo);
175 if (status == -1) {
176 ERR("ioctl for synth info failed.\n");
177 midiCloseSeq();
178 return TRUE;
181 tmplpCaps = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA));
182 if (!tmplpCaps)
183 break;
184 /* We also have the information sinfo.synth_subtype, not used here
187 /* Manufac ID. We do not have access to this with soundcard.h
188 * Does not seem to be a problem, because in mmsystem.h only
189 * Microsoft's ID is listed.
191 tmplpCaps->wMid = 0x00FF;
192 tmplpCaps->wPid = 0x0001; /* FIXME Product ID */
193 /* Product Version. We simply say "1" */
194 tmplpCaps->vDriverVersion = 0x001;
195 strcpy(tmplpCaps->szPname, sinfo.name);
197 tmplpCaps->wTechnology = MIDI_UnixToWindowsDeviceType(sinfo.synth_type);
198 tmplpCaps->wVoices = sinfo.nr_voices;
200 /* FIXME Is it possible to know the maximum
201 * number of simultaneous notes of a soundcard ?
202 * I believe we don't have this information, but
203 * it's probably equal or more than wVoices
205 tmplpCaps->wNotes = sinfo.nr_voices;
207 /* FIXME Do we have this information?
208 * Assuming the soundcards can handle
209 * MIDICAPS_VOLUME and MIDICAPS_LRVOLUME but
210 * not MIDICAPS_CACHE.
212 tmplpCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
214 midiOutDevices[i] = tmplpCaps;
216 if (sinfo.capabilities & SYNTH_CAP_INPUT) {
217 FIXME("Synthetizer support MIDI in. Not supported yet (please report)\n");
220 TRACE("name='%s', techn=%d voices=%d notes=%d support=%ld\n",
221 tmplpCaps->szPname, tmplpCaps->wTechnology,
222 tmplpCaps->wVoices, tmplpCaps->wNotes, tmplpCaps->dwSupport);
223 TRACE("OSS info: synth subtype=%d capa=%lx\n",
224 sinfo.synth_subtype, (long)sinfo.capabilities);
227 /* find how many MIDI devices are there in the system */
228 status = ioctl(midiSeqFD, SNDCTL_SEQ_NRMIDIS, &nummididevs);
229 if (status == -1) {
230 ERR("ioctl on nr midi failed.\n");
231 midiCloseSeq();
232 return TRUE;
235 /* FIXME: the two restrictions below could be loosen in some cases */
236 if (numsynthdevs + nummididevs > MAX_MIDIOUTDRV) {
237 ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
238 "Some MIDI devices will not be available.\n");
239 nummididevs = MAX_MIDIOUTDRV - numsynthdevs;
242 if (nummididevs > MAX_MIDIINDRV) {
243 ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
244 "Some MIDI devices will not be available.\n",MAX_MIDIINDRV,nummididevs);
245 nummididevs = MAX_MIDIINDRV;
248 for (i = 0; i < nummididevs; i++) {
249 LPMIDIOUTCAPSA tmplpOutCaps;
250 LPMIDIINCAPSA tmplpInCaps;
252 minfo.device = i;
253 status = ioctl(midiSeqFD, SNDCTL_MIDI_INFO, &minfo);
254 if (status == -1) {
255 ERR("ioctl on midi info for device %d failed.\n", i);
256 midiCloseSeq();
257 return TRUE;
260 tmplpOutCaps = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA));
261 if (!tmplpOutCaps)
262 break;
263 /* This whole part is somewhat obscure to me. I'll keep trying to dig
264 info about it. If you happen to know, please tell us. The very
265 descritive minfo.dev_type was not used here.
267 /* Manufac ID. We do not have access to this with soundcard.h
268 Does not seem to be a problem, because in mmsystem.h only
269 Microsoft's ID is listed */
270 tmplpOutCaps->wMid = 0x00FF;
271 tmplpOutCaps->wPid = 0x0001; /* FIXME Product ID */
272 /* Product Version. We simply say "1" */
273 tmplpOutCaps->vDriverVersion = 0x001;
274 strcpy(tmplpOutCaps->szPname, minfo.name);
276 tmplpOutCaps->wTechnology = MOD_MIDIPORT; /* FIXME Is this right? */
277 /* Does it make any difference? */
278 tmplpOutCaps->wVoices = 16;
279 /* Does it make any difference? */
280 tmplpOutCaps->wNotes = 16;
281 /* FIXME Does it make any difference? */
282 tmplpOutCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
284 midiOutDevices[numsynthdevs + i] = tmplpOutCaps;
286 tmplpInCaps = HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA));
287 if (!tmplpInCaps)
288 break;
289 /* This whole part is somewhat obscure to me. I'll keep trying to dig
290 info about it. If you happen to know, please tell us. The very
291 descritive minfo.dev_type was not used here.
293 /* Manufac ID. We do not have access to this with soundcard.h
294 Does not seem to be a problem, because in mmsystem.h only
295 Microsoft's ID is listed */
296 tmplpInCaps->wMid = 0x00FF;
297 tmplpInCaps->wPid = 0x0001; /* FIXME Product ID */
298 /* Product Version. We simply say "1" */
299 tmplpInCaps->vDriverVersion = 0x001;
300 strcpy(tmplpInCaps->szPname, minfo.name);
302 /* FIXME : could we get better information than that ? */
303 tmplpInCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
305 midiInDevices[i] = tmplpInCaps;
307 TRACE("name='%s' techn=%d voices=%d notes=%d support=%ld\n",
308 tmplpOutCaps->szPname, tmplpOutCaps->wTechnology, tmplpOutCaps->wVoices,
309 tmplpOutCaps->wNotes, tmplpOutCaps->dwSupport);
310 TRACE("OSS info: midi dev-type=%d, capa=%lx\n",
311 minfo.dev_type, (long)minfo.capabilities);
314 /* windows does not seem to differentiate Synth from MIDI devices */
315 MODM_NUMFMSYNTHDEVS = numsynthdevs;
316 MODM_NUMMIDIDEVS = nummididevs;
317 MODM_NUMDEVS = numsynthdevs + nummididevs;
319 MIDM_NUMDEVS = nummididevs;
321 /* close file and exit */
322 midiCloseSeq();
324 return TRUE;
327 /**************************************************************************
328 * MIDI_NotifyClient [internal]
330 static DWORD MIDI_NotifyClient(UINT wDevID, WORD wMsg,
331 DWORD dwParam1, DWORD dwParam2)
333 DWORD dwCallBack;
334 UINT uFlags;
335 HANDLE hDev;
336 DWORD dwInstance;
338 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",
339 wDevID, wMsg, dwParam1, dwParam2);
341 switch (wMsg) {
342 case MOM_OPEN:
343 case MOM_CLOSE:
344 case MOM_DONE:
345 if (wDevID > MAX_MIDIOUTDRV)
346 return MCIERR_INTERNAL;
348 dwCallBack = MidiOutDev[wDevID].midiDesc->dwCallback;
349 uFlags = MidiOutDev[wDevID].wFlags;
350 hDev = MidiOutDev[wDevID].midiDesc->hMidi;
351 dwInstance = MidiOutDev[wDevID].midiDesc->dwInstance;
352 break;
354 case MIM_OPEN:
355 case MIM_CLOSE:
356 case MIM_DATA:
357 case MIM_ERROR:
358 if (wDevID > MAX_MIDIINDRV)
359 return MCIERR_INTERNAL;
361 dwCallBack = MidiInDev[wDevID].midiDesc->dwCallback;
362 uFlags = MidiInDev[wDevID].wFlags;
363 hDev = MidiInDev[wDevID].midiDesc->hMidi;
364 dwInstance = MidiInDev[wDevID].midiDesc->dwInstance;
365 break;
366 default:
367 WARN("Unsupported MSW-MIDI message %u\n", wMsg);
368 return MCIERR_INTERNAL;
371 return DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2) ?
372 0 : MCIERR_INTERNAL;
375 static int midi_warn = 1;
376 /**************************************************************************
377 * midiOpenSeq [internal]
379 static int midiOpenSeq(void)
381 if (numOpenMidiSeq == 0) {
382 midiSeqFD = open(MIDI_SEQ, O_RDWR, 0);
383 if (midiSeqFD == -1) {
384 if (midi_warn)
385 MESSAGE("Can't open MIDI device '%s', errno %d (%s) !\n",
386 MIDI_SEQ, errno, strerror(errno));
387 midi_warn = 0;
388 return -1;
390 if (fcntl(midiSeqFD, F_SETFL, O_NONBLOCK) < 0) {
391 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno, strerror(errno));
392 close(midiSeqFD);
393 midiSeqFD = -1;
394 return -1;
396 ioctl(midiSeqFD, SNDCTL_SEQ_RESET);
398 numOpenMidiSeq++;
399 return 0;
402 /**************************************************************************
403 * midiCloseSeq [internal]
405 static int midiCloseSeq(void)
407 if (--numOpenMidiSeq == 0) {
408 close(midiSeqFD);
409 midiSeqFD = -1;
411 return 0;
414 /* FIXME: this is a bad idea, it's even not static... */
415 SEQ_DEFINEBUF(1024);
417 /* FIXME: this is not reentrant, not static - because of global variable
418 * _seqbuf and al.
420 /**************************************************************************
421 * seqbuf_dump [internal]
423 void seqbuf_dump(void)
425 if (_seqbufptr) {
426 if (write(midiSeqFD, _seqbuf, _seqbufptr) == -1) {
427 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
428 midiSeqFD, errno, strerror(errno));
430 /* FIXME:
431 * in any case buffer is lost so that if many errors occur the buffer
432 * will not overrun
434 _seqbufptr = 0;
438 static void midReceiveChar(WORD wDevID, unsigned char value, DWORD dwTime)
440 DWORD toSend = 0;
442 TRACE("Adding %02xh to %d[%d]\n", value, wDevID, MidiInDev[wDevID].incLen);
444 if (wDevID >= MAX_MIDIINDRV) {
445 WARN("bad devID\n");
446 return;
448 if (MidiInDev[wDevID].state == 0) {
449 TRACE("input not started, thrown away\n");
450 return;
453 if (MidiInDev[wDevID].state & 2) { /* system exclusive */
454 LPMIDIHDR lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
455 WORD sbfb = FALSE;
457 if (lpMidiHdr) {
458 LPBYTE lpData = lpMidiHdr->lpData;
460 lpData[lpMidiHdr->dwBytesRecorded++] = value;
461 if (lpMidiHdr->dwBytesRecorded == lpMidiHdr->dwBufferLength) {
462 sbfb = TRUE;
465 if (value == 0xF7) { /* then end */
466 MidiInDev[wDevID].state &= ~2;
467 sbfb = TRUE;
469 if (sbfb && lpMidiHdr != NULL) {
470 lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
471 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
472 lpMidiHdr->dwFlags |= MHDR_DONE;
473 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)lpMidiHdr->lpNext;
474 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD)lpMidiHdr, dwTime) != MMSYSERR_NOERROR) {
475 WARN("Couldn't notify client\n");
478 return;
481 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
482 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
484 if (!IS_CMD(value) && MidiInDev[wDevID].incLen == 0) { /* try to reuse old cmd */
485 if (IS_CMD(MidiInDev[wDevID].incPrev) && !IS_SYS_CMD(MidiInDev[wDevID].incPrev)) {
486 MidiInDev[wDevID].incoming[0] = MidiInDev[wDevID].incPrev;
487 MidiInDev[wDevID].incLen = 1;
488 TRACE("Reusing old command %02xh\n", MidiInDev[wDevID].incPrev);
489 } else {
490 FIXME("error for midi-in, should generate MIM_ERROR notification:"
491 " prev=%02Xh, incLen=%02Xh\n",
492 MidiInDev[wDevID].incPrev, MidiInDev[wDevID].incLen);
493 return;
496 MidiInDev[wDevID].incoming[(int)(MidiInDev[wDevID].incLen++)] = value;
497 if (MidiInDev[wDevID].incLen == 1 && !IS_SYS_CMD(MidiInDev[wDevID].incoming[0])) {
498 /* store new cmd, just in case */
499 MidiInDev[wDevID].incPrev = MidiInDev[wDevID].incoming[0];
502 #undef IS_CMD(_x)
503 #undef IS_SYS_CMD(_x)
505 switch (MidiInDev[wDevID].incoming[0] & 0xF0) {
506 case MIDI_NOTEOFF:
507 case MIDI_NOTEON:
508 case MIDI_KEY_PRESSURE:
509 case MIDI_CTL_CHANGE:
510 case MIDI_PITCH_BEND:
511 if (MidiInDev[wDevID].incLen == 3) {
512 toSend = (MidiInDev[wDevID].incoming[2] << 16) |
513 (MidiInDev[wDevID].incoming[1] << 8) |
514 (MidiInDev[wDevID].incoming[0] << 0);
516 break;
517 case MIDI_PGM_CHANGE:
518 case MIDI_CHN_PRESSURE:
519 if (MidiInDev[wDevID].incLen == 2) {
520 toSend = (MidiInDev[wDevID].incoming[1] << 8) |
521 (MidiInDev[wDevID].incoming[0] << 0);
523 break;
524 case MIDI_SYSTEM_PREFIX:
525 if (MidiInDev[wDevID].incoming[0] == 0xF0) {
526 MidiInDev[wDevID].state |= 2;
527 MidiInDev[wDevID].incLen = 0;
528 } else {
529 if (MidiInDev[wDevID].incLen == 1) {
530 toSend = (MidiInDev[wDevID].incoming[0] << 0);
533 break;
534 default:
535 WARN("This shouldn't happen (%02X)\n", MidiInDev[wDevID].incoming[0]);
537 if (toSend != 0) {
538 TRACE("Sending event %08lx\n", toSend);
539 MidiInDev[wDevID].incLen = 0;
540 dwTime -= MidiInDev[wDevID].startTime;
541 if (MIDI_NotifyClient(wDevID, MIM_DATA, toSend, dwTime) != MMSYSERR_NOERROR) {
542 WARN("Couldn't notify client\n");
547 static VOID WINAPI midTimeCallback(HWND hwnd, UINT msg, UINT id, DWORD dwTime)
549 unsigned char buffer[256];
550 int len, idx;
552 TRACE("(%04X, %d, %d, %lu)\n", hwnd, msg, id, dwTime);
554 len = read(midiSeqFD, buffer, sizeof(buffer));
556 if (len < 0) return;
557 if ((len % 4) != 0) {
558 WARN("Bad length %d, errno %d (%s)\n", len, errno, strerror(errno));
559 return;
562 for (idx = 0; idx < len; ) {
563 if (buffer[idx] & 0x80) {
564 TRACE(
565 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
566 buffer[idx + 0], buffer[idx + 1],
567 buffer[idx + 2], buffer[idx + 3],
568 buffer[idx + 4], buffer[idx + 5],
569 buffer[idx + 6], buffer[idx + 7]);
570 idx += 8;
571 } else {
572 switch (buffer[idx + 0]) {
573 case SEQ_WAIT:
574 case SEQ_ECHO:
575 break;
576 case SEQ_MIDIPUTC:
577 midReceiveChar(buffer[idx + 2], buffer[idx + 1], dwTime);
578 break;
579 default:
580 TRACE("Unsupported event %d\n", buffer[idx + 0]);
581 break;
583 idx += 4;
588 /**************************************************************************
589 * midGetDevCaps [internal]
591 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPSA lpCaps, DWORD dwSize)
593 TRACE("(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
595 if (wDevID >= MIDM_NUMDEVS) return MMSYSERR_BADDEVICEID;
596 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
598 memcpy(lpCaps, midiInDevices[wDevID], min(dwSize, sizeof(*lpCaps)));
600 return MMSYSERR_NOERROR;
603 /**************************************************************************
604 * midOpen [internal]
606 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
608 TRACE("(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
610 if (lpDesc == NULL) {
611 WARN("Invalid Parameter !\n");
612 return MMSYSERR_INVALPARAM;
615 /* FIXME :
616 * how to check that content of lpDesc is correct ?
618 if (wDevID >= MAX_MIDIINDRV) {
619 WARN("wDevID too large (%u) !\n", wDevID);
620 return MMSYSERR_BADDEVICEID;
622 if (MidiInDev[wDevID].midiDesc != 0) {
623 WARN("device already open !\n");
624 return MMSYSERR_ALLOCATED;
626 if ((dwFlags & MIDI_IO_STATUS) != 0) {
627 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
628 dwFlags &= ~MIDI_IO_STATUS;
630 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
631 FIXME("Bad dwFlags\n");
632 return MMSYSERR_INVALFLAG;
635 if (midiOpenSeq() < 0) {
636 return MMSYSERR_ERROR;
639 if (numStartedMidiIn++ == 0) {
640 midiInTimerID = SetTimer(0, 0, 250, midTimeCallback);
641 if (!midiInTimerID) {
642 numStartedMidiIn = 0;
643 WARN("Couldn't start timer for midi-in\n");
644 midiCloseSeq();
645 return MMSYSERR_ERROR;
647 TRACE("Starting timer (%u) for midi-in\n", midiInTimerID);
650 MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
652 MidiInDev[wDevID].lpQueueHdr = NULL;
653 MidiInDev[wDevID].dwTotalPlayed = 0;
654 MidiInDev[wDevID].bufsize = 0x3FFF;
655 MidiInDev[wDevID].midiDesc = lpDesc;
656 MidiInDev[wDevID].state = 0;
657 MidiInDev[wDevID].incLen = 0;
658 MidiInDev[wDevID].startTime = 0;
660 if (MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
661 WARN("can't notify client !\n");
662 return MMSYSERR_INVALPARAM;
664 return MMSYSERR_NOERROR;
667 /**************************************************************************
668 * midClose [internal]
670 static DWORD midClose(WORD wDevID)
672 int ret = MMSYSERR_NOERROR;
674 TRACE("(%04X);\n", wDevID);
676 if (wDevID >= MAX_MIDIINDRV) {
677 WARN("wDevID too big (%u) !\n", wDevID);
678 return MMSYSERR_BADDEVICEID;
680 if (MidiInDev[wDevID].midiDesc == 0) {
681 WARN("device not opened !\n");
682 return MMSYSERR_ERROR;
684 if (MidiInDev[wDevID].lpQueueHdr != 0) {
685 return MIDIERR_STILLPLAYING;
688 if (midiSeqFD == -1) {
689 WARN("ooops !\n");
690 return MMSYSERR_ERROR;
692 if (--numStartedMidiIn == 0) {
693 TRACE("Stopping timer for midi-in\n");
694 if (!KillTimer(0, midiInTimerID)) {
695 WARN("Couldn't stop timer for midi-in\n");
697 midiInTimerID = 0;
699 midiCloseSeq();
701 MidiInDev[wDevID].bufsize = 0;
702 if (MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
703 WARN("can't notify client !\n");
704 ret = MMSYSERR_INVALPARAM;
706 MidiInDev[wDevID].midiDesc = 0;
707 return ret;
710 /**************************************************************************
711 * midAddBuffer [internal]
713 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
715 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
717 if (lpMidiHdr == NULL) return MMSYSERR_INVALPARAM;
718 if (sizeof(MIDIHDR) > dwSize) return MMSYSERR_INVALPARAM;
719 if (lpMidiHdr->dwBufferLength == 0) return MMSYSERR_INVALPARAM;
720 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
721 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
723 if (MidiInDev[wDevID].lpQueueHdr == 0) {
724 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr;
725 } else {
726 LPMIDIHDR ptr;
728 for (ptr = MidiInDev[wDevID].lpQueueHdr;
729 ptr->lpNext != 0;
730 ptr = (LPMIDIHDR)ptr->lpNext);
731 ptr->lpNext = (struct midihdr_tag*)lpMidiHdr;
733 return MMSYSERR_NOERROR;
736 /**************************************************************************
737 * midPrepare [internal]
739 static DWORD midPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
741 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
743 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
744 lpMidiHdr->lpData == 0 || lpMidiHdr->dwFlags != 0 ||
745 lpMidiHdr->dwBufferLength >= 0x10000ul)
746 return MMSYSERR_INVALPARAM;
748 lpMidiHdr->lpNext = 0;
749 lpMidiHdr->dwFlags |= MHDR_PREPARED;
750 lpMidiHdr->dwBytesRecorded = 0;
752 return MMSYSERR_NOERROR;
755 /**************************************************************************
756 * midUnprepare [internal]
758 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
760 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
762 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
763 lpMidiHdr->lpData == 0 || lpMidiHdr->dwBufferLength >= 0x10000ul)
764 return MMSYSERR_INVALPARAM;
766 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
767 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
769 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
771 return MMSYSERR_NOERROR;
774 /**************************************************************************
775 * midReset [internal]
777 static DWORD midReset(WORD wDevID)
779 DWORD dwTime = GetTickCount();
781 TRACE("(%04X);\n", wDevID);
783 while (MidiInDev[wDevID].lpQueueHdr) {
784 MidiInDev[wDevID].lpQueueHdr->dwFlags &= ~MHDR_INQUEUE;
785 MidiInDev[wDevID].lpQueueHdr->dwFlags |= MHDR_DONE;
786 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
787 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA,
788 (DWORD)MidiInDev[wDevID].lpQueueHdr, dwTime) != MMSYSERR_NOERROR) {
789 WARN("Couldn't notify client\n");
791 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)MidiInDev[wDevID].lpQueueHdr->lpNext;
794 return MMSYSERR_NOERROR;
798 /**************************************************************************
799 * midStart [internal]
801 static DWORD midStart(WORD wDevID)
803 TRACE("(%04X);\n", wDevID);
805 /* FIXME : should test value of wDevID */
807 MidiInDev[wDevID].state = 1;
808 MidiInDev[wDevID].startTime = GetTickCount();
809 return MMSYSERR_NOERROR;
812 /**************************************************************************
813 * midStop [internal]
815 static DWORD midStop(WORD wDevID)
817 TRACE("(%04X);\n", wDevID);
819 /* FIXME : should test value of wDevID */
820 MidiInDev[wDevID].state = 0;
821 return MMSYSERR_NOERROR;
824 /*-----------------------------------------------------------------------*/
826 typedef struct sVoice {
827 int note; /* 0 means not used */
828 int channel;
829 unsigned cntMark : 30,
830 status : 2;
831 #define sVS_UNUSED 0
832 #define sVS_PLAYING 1
833 #define sVS_SUSTAINED 2
834 } sVoice;
836 typedef struct sChannel {
837 int program;
839 int bender;
840 int benderRange;
841 /* controlers */
842 int bank; /* CTL_BANK_SELECT */
843 int volume; /* CTL_MAIN_VOLUME */
844 int balance; /* CTL_BALANCE */
845 int expression; /* CTL_EXPRESSION */
846 int sustain; /* CTL_SUSTAIN */
848 unsigned char nrgPmtMSB; /* Non register Parameters */
849 unsigned char nrgPmtLSB;
850 unsigned char regPmtMSB; /* Non register Parameters */
851 unsigned char regPmtLSB;
852 } sChannel;
854 typedef struct sFMextra {
855 unsigned counter;
856 int drumSetMask;
857 sChannel channel[16]; /* MIDI has only 16 channels */
858 sVoice voice[1]; /* dyn allocated according to sound card */
859 /* do not append fields below voice[1] since the size of this structure
860 * depends on the number of available voices on the FM synth...
862 } sFMextra;
864 extern unsigned char midiFMInstrumentPatches[16 * 128];
865 extern unsigned char midiFMDrumsPatches [16 * 128];
867 /**************************************************************************
868 * modFMLoad [internal]
870 static int modFMLoad(int dev)
872 int i;
873 struct sbi_instrument sbi;
875 sbi.device = dev;
876 sbi.key = FM_PATCH;
878 memset(sbi.operators + 16, 0, 16);
879 for (i = 0; i < 128; i++) {
880 sbi.channel = i;
881 memcpy(sbi.operators, midiFMInstrumentPatches + i * 16, 16);
883 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
884 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
885 return -1;
888 for (i = 0; i < 128; i++) {
889 sbi.channel = 128 + i;
890 memcpy(sbi.operators, midiFMDrumsPatches + i * 16, 16);
892 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
893 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
894 return -1;
897 return 0;
900 /**************************************************************************
901 * modFMReset [internal]
903 static void modFMReset(WORD wDevID)
905 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
906 sVoice* voice = extra->voice;
907 sChannel* channel = extra->channel;
908 int i;
910 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
911 if (voice[i].status != sVS_UNUSED) {
912 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
914 SEQ_KEY_PRESSURE(wDevID, i, 127, 0);
915 SEQ_CONTROL(wDevID, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
916 voice[i].note = 0;
917 voice[i].channel = -1;
918 voice[i].cntMark = 0;
919 voice[i].status = sVS_UNUSED;
921 for (i = 0; i < 16; i++) {
922 channel[i].program = 0;
923 channel[i].bender = 8192;
924 channel[i].benderRange = 2;
925 channel[i].bank = 0;
926 channel[i].volume = 127;
927 channel[i].balance = 64;
928 channel[i].expression = 0;
929 channel[i].sustain = 0;
931 extra->counter = 0;
932 extra->drumSetMask = 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
933 SEQ_DUMPBUF();
936 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
938 /**************************************************************************
939 * modGetDevCaps [internal]
941 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPSA lpCaps, DWORD dwSize)
943 TRACE("(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
945 if (wDevID >= MODM_NUMDEVS) return MMSYSERR_BADDEVICEID;
946 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
948 memcpy(lpCaps, midiOutDevices[wDevID], min(dwSize, sizeof(*lpCaps)));
950 return MMSYSERR_NOERROR;
953 /**************************************************************************
954 * modOpen [internal]
956 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
958 TRACE("(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
959 if (lpDesc == NULL) {
960 WARN("Invalid Parameter !\n");
961 return MMSYSERR_INVALPARAM;
963 if (wDevID >= MAX_MIDIOUTDRV) {
964 TRACE("MAX_MIDIOUTDRV reached !\n");
965 return MMSYSERR_BADDEVICEID;
967 if (MidiOutDev[wDevID].midiDesc != 0) {
968 WARN("device already open !\n");
969 return MMSYSERR_ALLOCATED;
971 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
972 WARN("bad dwFlags\n");
973 return MMSYSERR_INVALFLAG;
975 if (midiOutDevices[wDevID] == NULL) {
976 TRACE("un-allocated wDevID\n");
977 return MMSYSERR_BADDEVICEID;
980 MidiOutDev[wDevID].lpExtra = 0;
982 switch (midiOutDevices[wDevID]->wTechnology) {
983 case MOD_FMSYNTH:
985 void* extra = HeapAlloc(GetProcessHeap(), 0,
986 sizeof(struct sFMextra) +
987 sizeof(struct sVoice) * (midiOutDevices[wDevID]->wVoices - 1));
989 if (extra == 0) {
990 WARN("can't alloc extra data !\n");
991 return MMSYSERR_NOMEM;
993 MidiOutDev[wDevID].lpExtra = extra;
994 if (midiOpenSeq() < 0) {
995 MidiOutDev[wDevID].lpExtra = 0;
996 HeapFree(GetProcessHeap(), 0, extra);
997 return MMSYSERR_ERROR;
999 if (modFMLoad(wDevID) < 0) {
1000 midiCloseSeq();
1001 MidiOutDev[wDevID].lpExtra = 0;
1002 HeapFree(GetProcessHeap(), 0, extra);
1003 return MMSYSERR_ERROR;
1005 modFMReset(wDevID);
1007 break;
1008 case MOD_MIDIPORT:
1009 if (midiOpenSeq() < 0) {
1010 return MMSYSERR_ALLOCATED;
1012 break;
1013 default:
1014 WARN("Technology not supported (yet) %d !\n",
1015 midiOutDevices[wDevID]->wTechnology);
1016 return MMSYSERR_NOTENABLED;
1019 MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1021 MidiOutDev[wDevID].lpQueueHdr = NULL;
1022 MidiOutDev[wDevID].dwTotalPlayed = 0;
1023 MidiOutDev[wDevID].bufsize = 0x3FFF;
1024 MidiOutDev[wDevID].midiDesc = lpDesc;
1026 if (MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
1027 WARN("can't notify client !\n");
1028 return MMSYSERR_INVALPARAM;
1030 TRACE("Successful !\n");
1031 return MMSYSERR_NOERROR;
1035 /**************************************************************************
1036 * modClose [internal]
1038 static DWORD modClose(WORD wDevID)
1040 int ret = MMSYSERR_NOERROR;
1042 TRACE("(%04X);\n", wDevID);
1044 if (MidiOutDev[wDevID].midiDesc == 0) {
1045 WARN("device not opened !\n");
1046 return MMSYSERR_ERROR;
1048 /* FIXME: should test that no pending buffer is still in the queue for
1049 * playing */
1051 if (midiSeqFD == -1) {
1052 WARN("can't close !\n");
1053 return MMSYSERR_ERROR;
1056 switch (midiOutDevices[wDevID]->wTechnology) {
1057 case MOD_FMSYNTH:
1058 case MOD_MIDIPORT:
1059 midiCloseSeq();
1060 break;
1061 default:
1062 WARN("Technology not supported (yet) %d !\n",
1063 midiOutDevices[wDevID]->wTechnology);
1064 return MMSYSERR_NOTENABLED;
1067 if (MidiOutDev[wDevID].lpExtra != 0) {
1068 HeapFree(GetProcessHeap(), 0, MidiOutDev[wDevID].lpExtra);
1069 MidiOutDev[wDevID].lpExtra = 0;
1072 MidiOutDev[wDevID].bufsize = 0;
1073 if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
1074 WARN("can't notify client !\n");
1075 ret = MMSYSERR_INVALPARAM;
1077 MidiOutDev[wDevID].midiDesc = 0;
1078 return ret;
1081 /**************************************************************************
1082 * modData [internal]
1084 static DWORD modData(WORD wDevID, DWORD dwParam)
1086 WORD evt = LOBYTE(LOWORD(dwParam));
1087 WORD d1 = HIBYTE(LOWORD(dwParam));
1088 WORD d2 = LOBYTE(HIWORD(dwParam));
1090 TRACE("(%04X, %08lX);\n", wDevID, dwParam);
1092 if (midiSeqFD == -1) {
1093 WARN("can't play !\n");
1094 return MIDIERR_NODEVICE;
1096 switch (midiOutDevices[wDevID]->wTechnology) {
1097 case MOD_FMSYNTH:
1098 /* FIXME:
1099 * - chorus depth controller is not used
1102 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
1103 sVoice* voice = extra->voice;
1104 sChannel* channel = extra->channel;
1105 int chn = (evt & 0x0F);
1106 int i, nv;
1108 switch (evt & 0xF0) {
1109 case MIDI_NOTEOFF:
1110 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1111 /* don't stop sustained notes */
1112 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1113 voice[i].status = sVS_UNUSED;
1114 SEQ_STOP_NOTE(wDevID, i, d1, d2);
1117 break;
1118 case MIDI_NOTEON:
1119 if (d2 == 0) { /* note off if velocity == 0 */
1120 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1121 /* don't stop sustained notes */
1122 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1123 voice[i].status = sVS_UNUSED;
1124 SEQ_STOP_NOTE(wDevID, i, d1, 64);
1127 break;
1129 /* finding out in this order :
1130 * - an empty voice
1131 * - if replaying the same note on the same channel
1132 * - the older voice (LRU)
1134 for (i = nv = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1135 if (voice[i].status == sVS_UNUSED ||
1136 (voice[i].note == d1 && voice[i].channel == chn)) {
1137 nv = i;
1138 break;
1140 if (voice[i].cntMark < voice[0].cntMark) {
1141 nv = i;
1144 TRACE(
1145 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1146 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1147 nv, channel[chn].program,
1148 channel[chn].balance,
1149 channel[chn].volume,
1150 channel[chn].bender, d1, d2);
1152 SEQ_SET_PATCH(wDevID, nv, IS_DRUM_CHANNEL(extra, chn) ?
1153 (128 + d1) : channel[chn].program);
1154 SEQ_BENDER_RANGE(wDevID, nv, channel[chn].benderRange * 100);
1155 SEQ_BENDER(wDevID, nv, channel[chn].bender);
1156 SEQ_CONTROL(wDevID, nv, CTL_PAN, channel[chn].balance);
1157 SEQ_CONTROL(wDevID, nv, CTL_EXPRESSION, channel[chn].expression);
1158 #if 0
1159 /* FIXME: does not really seem to work on my SB card and
1160 * screws everything up... so lay it down
1162 SEQ_CONTROL(wDevID, nv, CTL_MAIN_VOLUME, channel[chn].volume);
1163 #endif
1164 SEQ_START_NOTE(wDevID, nv, d1, d2);
1165 voice[nv].status = channel[chn].sustain ? sVS_SUSTAINED : sVS_PLAYING;
1166 voice[nv].note = d1;
1167 voice[nv].channel = chn;
1168 voice[nv].cntMark = extra->counter++;
1169 break;
1170 case MIDI_KEY_PRESSURE:
1171 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1172 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn && voice[i].note == d1) {
1173 SEQ_KEY_PRESSURE(wDevID, i, d1, d2);
1176 break;
1177 case MIDI_CTL_CHANGE:
1178 switch (d1) {
1179 case CTL_BANK_SELECT: channel[chn].bank = d2; break;
1180 case CTL_MAIN_VOLUME: channel[chn].volume = d2; break;
1181 case CTL_PAN: channel[chn].balance = d2; break;
1182 case CTL_EXPRESSION: channel[chn].expression = d2; break;
1183 case CTL_SUSTAIN: channel[chn].sustain = d2;
1184 if (d2) {
1185 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1186 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1187 voice[i].status = sVS_SUSTAINED;
1190 } else {
1191 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1192 if (voice[i].status == sVS_SUSTAINED && voice[i].channel == chn) {
1193 voice[i].status = sVS_UNUSED;
1194 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1198 break;
1199 case CTL_NONREG_PARM_NUM_LSB: channel[chn].nrgPmtLSB = d2; break;
1200 case CTL_NONREG_PARM_NUM_MSB: channel[chn].nrgPmtMSB = d2; break;
1201 case CTL_REGIST_PARM_NUM_LSB: channel[chn].regPmtLSB = d2; break;
1202 case CTL_REGIST_PARM_NUM_MSB: channel[chn].regPmtMSB = d2; break;
1203 case CTL_DATA_ENTRY:
1204 switch ((channel[chn].regPmtMSB << 8) | channel[chn].regPmtLSB) {
1205 case 0x0000:
1206 if (channel[chn].benderRange != d2) {
1207 channel[chn].benderRange = d2;
1208 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1209 if (voice[i].channel == chn) {
1210 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1214 break;
1216 case 0x7F7F:
1217 channel[chn].benderRange = 2;
1218 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1219 if (voice[i].channel == chn) {
1220 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1223 break;
1224 default:
1225 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1226 channel[chn].regPmtMSB, channel[chn].regPmtLSB,
1227 channel[chn].nrgPmtMSB, channel[chn].nrgPmtLSB,
1228 d2);
1229 break;
1231 break;
1233 case 0x78: /* all sounds off */
1234 /* FIXME: I don't know if I have to take care of the channel
1235 * for this control ?
1237 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1238 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1239 voice[i].status = sVS_UNUSED;
1240 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1243 break;
1244 case 0x7B: /* all notes off */
1245 /* FIXME: I don't know if I have to take care of the channel
1246 * for this control ?
1248 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1249 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1250 voice[i].status = sVS_UNUSED;
1251 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1254 break;
1255 default:
1256 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1257 d1, d2, chn);
1258 break;
1260 break;
1261 case MIDI_PGM_CHANGE:
1262 channel[chn].program = d1;
1263 break;
1264 case MIDI_CHN_PRESSURE:
1265 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1266 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1267 SEQ_KEY_PRESSURE(wDevID, i, voice[i].note, d1);
1270 break;
1271 case MIDI_PITCH_BEND:
1272 channel[chn].bender = (d2 << 7) + d1;
1273 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1274 if (voice[i].channel == chn) {
1275 SEQ_BENDER(wDevID, i, channel[chn].bender);
1278 break;
1279 case MIDI_SYSTEM_PREFIX:
1280 switch (evt & 0x0F) {
1281 case 0x0F: /* Reset */
1282 modFMReset(wDevID);
1283 break;
1284 default:
1285 WARN("Unsupported (yet) system event %02x\n", evt & 0x0F);
1287 break;
1288 default:
1289 WARN("Internal error, shouldn't happen (event=%08x)\n", evt & 0xF0);
1290 return MMSYSERR_NOTENABLED;
1293 break;
1294 case MOD_MIDIPORT:
1296 int dev = wDevID - MODM_NUMFMSYNTHDEVS;
1297 if (dev < 0) {
1298 WARN("Internal error on devID (%u) !\n", wDevID);
1299 return MIDIERR_NODEVICE;
1302 switch (evt & 0xF0) {
1303 case MIDI_NOTEOFF:
1304 case MIDI_NOTEON:
1305 case MIDI_KEY_PRESSURE:
1306 case MIDI_CTL_CHANGE:
1307 case MIDI_PITCH_BEND:
1308 SEQ_MIDIOUT(dev, evt);
1309 SEQ_MIDIOUT(dev, d1);
1310 SEQ_MIDIOUT(dev, d2);
1311 break;
1312 case MIDI_PGM_CHANGE:
1313 case MIDI_CHN_PRESSURE:
1314 SEQ_MIDIOUT(dev, evt);
1315 SEQ_MIDIOUT(dev, d1);
1316 break;
1317 case MIDI_SYSTEM_PREFIX:
1318 switch (evt & 0x0F) {
1319 case 0x00: /* System Exclusive, don't do it on modData,
1320 * should require modLongData*/
1321 case 0x01: /* Undefined */
1322 case 0x04: /* Undefined. */
1323 case 0x05: /* Undefined. */
1324 case 0x07: /* End of Exclusive. */
1325 case 0x09: /* Undefined. */
1326 case 0x0D: /* Undefined. */
1327 break;
1328 case 0x06: /* Tune Request */
1329 case 0x08: /* Timing Clock. */
1330 case 0x0A: /* Start. */
1331 case 0x0B: /* Continue */
1332 case 0x0C: /* Stop */
1333 case 0x0E: /* Active Sensing. */
1334 SEQ_MIDIOUT(dev, evt);
1335 break;
1336 case 0x0F: /* Reset */
1337 /* SEQ_MIDIOUT(dev, evt);
1338 this other way may be better */
1339 SEQ_MIDIOUT(dev, MIDI_SYSTEM_PREFIX);
1340 SEQ_MIDIOUT(dev, 0x7e);
1341 SEQ_MIDIOUT(dev, 0x7f);
1342 SEQ_MIDIOUT(dev, 0x09);
1343 SEQ_MIDIOUT(dev, 0x01);
1344 SEQ_MIDIOUT(dev, 0xf7);
1345 break;
1346 case 0x03: /* Song Select. */
1347 SEQ_MIDIOUT(dev, evt);
1348 SEQ_MIDIOUT(dev, d1);
1349 case 0x02: /* Song Position Pointer. */
1350 SEQ_MIDIOUT(dev, evt);
1351 SEQ_MIDIOUT(dev, d1);
1352 SEQ_MIDIOUT(dev, d2);
1354 break;
1357 break;
1358 default:
1359 WARN("Technology not supported (yet) %d !\n",
1360 midiOutDevices[wDevID]->wTechnology);
1361 return MMSYSERR_NOTENABLED;
1364 SEQ_DUMPBUF();
1366 return MMSYSERR_NOERROR;
1369 /**************************************************************************
1370 * modLongData [internal]
1372 static DWORD modLongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1374 int count;
1375 LPBYTE lpData;
1377 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1379 if (midiSeqFD == -1) {
1380 WARN("can't play !\n");
1381 return MIDIERR_NODEVICE;
1384 lpData = lpMidiHdr->lpData;
1386 if (lpData == NULL)
1387 return MIDIERR_UNPREPARED;
1388 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1389 return MIDIERR_UNPREPARED;
1390 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1391 return MIDIERR_STILLPLAYING;
1392 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1393 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1395 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1396 * data, or can it also contain raw MIDI data, to be split up and sent to
1397 * modShortData() ?
1398 * If the latest is true, then the following WARNing will fire up
1400 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
1401 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1404 TRACE("dwBufferLength=%lu !\n", lpMidiHdr->dwBufferLength);
1405 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1406 lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
1407 lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
1409 switch (midiOutDevices[wDevID]->wTechnology) {
1410 case MOD_FMSYNTH:
1411 /* FIXME: I don't think there is much to do here */
1412 break;
1413 case MOD_MIDIPORT:
1414 if (lpData[0] != 0xF0) {
1415 /* Send end of System Exclusive */
1416 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF0);
1417 WARN("Adding missing 0xF0 marker at the begining of "
1418 "system exclusive byte stream\n");
1420 for (count = 0; count < lpMidiHdr->dwBytesRecorded; count++) {
1421 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, lpData[count]);
1423 if (lpData[count - 1] != 0xF7) {
1424 /* Send end of System Exclusive */
1425 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF7);
1426 WARN("Adding missing 0xF7 marker at the end of "
1427 "system exclusive byte stream\n");
1429 SEQ_DUMPBUF();
1430 break;
1431 default:
1432 WARN("Technology not supported (yet) %d !\n",
1433 midiOutDevices[wDevID]->wTechnology);
1434 return MMSYSERR_NOTENABLED;
1437 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1438 lpMidiHdr->dwFlags |= MHDR_DONE;
1439 if (MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD)lpMidiHdr, 0L) != MMSYSERR_NOERROR) {
1440 WARN("can't notify client !\n");
1441 return MMSYSERR_INVALPARAM;
1443 return MMSYSERR_NOERROR;
1446 /**************************************************************************
1447 * modPrepare [internal]
1449 static DWORD modPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1451 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1453 if (midiSeqFD == -1) {
1454 WARN("can't prepare !\n");
1455 return MMSYSERR_NOTENABLED;
1458 /* MS doc says taht dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
1459 * asks to prepare MIDIHDR which dwFlags != 0.
1460 * So at least check for the inqueue flag
1462 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
1463 lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0 ||
1464 lpMidiHdr->dwBufferLength >= 0x10000ul) {
1465 WARN("%p %p %08lx %d/%ld\n", lpMidiHdr, lpMidiHdr->lpData,
1466 lpMidiHdr->dwFlags, sizeof(MIDIHDR), dwSize);
1467 return MMSYSERR_INVALPARAM;
1470 lpMidiHdr->lpNext = 0;
1471 lpMidiHdr->dwFlags |= MHDR_PREPARED;
1472 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1473 return MMSYSERR_NOERROR;
1476 /**************************************************************************
1477 * modUnprepare [internal]
1479 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1481 TRACE("(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1483 if (midiSeqFD == -1) {
1484 WARN("can't unprepare !\n");
1485 return MMSYSERR_NOTENABLED;
1488 if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0)
1489 return MMSYSERR_INVALPARAM;
1490 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1491 return MIDIERR_STILLPLAYING;
1492 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
1493 return MMSYSERR_NOERROR;
1496 /**************************************************************************
1497 * modReset [internal]
1499 static DWORD modReset(WORD wDevID)
1501 unsigned chn;
1503 TRACE("(%04X);\n", wDevID);
1505 /* stop all notes */
1506 /* FIXME: check if 0x78B0 is channel dependant or not. I coded it so that
1507 * it's channel dependent...
1509 for (chn = 0; chn < 16; chn++) {
1510 /* turn off every note */
1511 modData(wDevID, 0x7800 | MIDI_CTL_CHANGE | chn);
1512 /* remove sustain on all channels */
1513 modData(wDevID, (CTL_SUSTAIN << 8) | MIDI_CTL_CHANGE | chn);
1515 /* FIXME: the LongData buffers must also be returned to the app */
1516 return MMSYSERR_NOERROR;
1519 #endif /* HAVE_OSS_MIDI */
1521 /*======================================================================*
1522 * MIDI entry points *
1523 *======================================================================*/
1525 /**************************************************************************
1526 * OSS_midMessage [sample driver]
1528 DWORD WINAPI OSS_midMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1529 DWORD dwParam1, DWORD dwParam2)
1531 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1532 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1533 switch (wMsg) {
1534 #ifdef HAVE_OSS_MIDI
1535 case DRVM_INIT:
1536 case DRVM_ENABLE:
1537 case DRVM_DISABLE:
1538 /* FIXME: Pretend this is supported */
1539 return 0;
1540 case MIDM_OPEN:
1541 return midOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1542 case MIDM_CLOSE:
1543 return midClose(wDevID);
1544 case MIDM_ADDBUFFER:
1545 return midAddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1546 case MIDM_PREPARE:
1547 return midPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1548 case MIDM_UNPREPARE:
1549 return midUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1550 case MIDM_GETDEVCAPS:
1551 return midGetDevCaps(wDevID, (LPMIDIINCAPSA)dwParam1,dwParam2);
1552 case MIDM_GETNUMDEVS:
1553 return MIDM_NUMDEVS;
1554 case MIDM_RESET:
1555 return midReset(wDevID);
1556 case MIDM_START:
1557 return midStart(wDevID);
1558 case MIDM_STOP:
1559 return midStop(wDevID);
1560 #endif
1561 default:
1562 TRACE("Unsupported message\n");
1564 return MMSYSERR_NOTSUPPORTED;
1567 /**************************************************************************
1568 * OSS_modMessage [sample driver]
1570 DWORD WINAPI OSS_modMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1571 DWORD dwParam1, DWORD dwParam2)
1573 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1574 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1576 switch (wMsg) {
1577 #ifdef HAVE_OSS_MIDI
1578 case DRVM_INIT:
1579 case DRVM_EXIT:
1580 case DRVM_ENABLE:
1581 case DRVM_DISABLE:
1582 /* FIXME: Pretend this is supported */
1583 return 0;
1584 case MODM_OPEN:
1585 return modOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1586 case MODM_CLOSE:
1587 return modClose(wDevID);
1588 case MODM_DATA:
1589 return modData(wDevID, dwParam1);
1590 case MODM_LONGDATA:
1591 return modLongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1592 case MODM_PREPARE:
1593 return modPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1594 case MODM_UNPREPARE:
1595 return modUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1596 case MODM_GETDEVCAPS:
1597 return modGetDevCaps(wDevID, (LPMIDIOUTCAPSA)dwParam1, dwParam2);
1598 case MODM_GETNUMDEVS:
1599 return MODM_NUMDEVS;
1600 case MODM_GETVOLUME:
1601 return 0;
1602 case MODM_SETVOLUME:
1603 return 0;
1604 case MODM_RESET:
1605 return modReset(wDevID);
1606 #endif
1607 default:
1608 TRACE("Unsupported message\n");
1610 return MMSYSERR_NOTSUPPORTED;
1613 /*-----------------------------------------------------------------------*/