1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * Sample MIDI Wine Driver for Open Sound System (basically Linux)
6 * Copyright 1994 Martin Ayotte
7 * Copyright 1998 Luiz Otavio L. Zorzella (init procedures)
8 * Copyright 1998/1999 Eric POUECH :
9 * 98/7 changes for making this MIDI driver work on OSS
10 * current support is limited to MIDI ports of OSS systems
11 * 98/9 rewriting MCI code for MIDI
12 * 98/11 split in midi.c and mcimidi.c
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 * + use better instrument definition for OPL/2 (midiPatch.c) or
31 * use existing instrument definition (from playmidi or kmid)
32 * with a .winerc option
33 * + have a look at OPL/3 ?
34 * + implement asynchronous playback of MidiHdr
35 * + implement STREAM'ed MidiHdr (question: how shall we share the
36 * code between the midiStream functions in MMSYSTEM/WINMM and
37 * the code for the low level driver)
38 * + use a more accurate read mechanism than the one of snooping on
39 * timers (like select on fd)
43 #include "wine/port.h"
54 #ifdef HAVE_SYS_IOCTL_H
55 # include <sys/ioctl.h>
60 #ifdef HAVE_SYS_POLL_H
63 #include <sys/soundcard.h>
71 #include "wine/unicode.h"
72 #include "wine/debug.h"
74 WINE_DEFAULT_DEBUG_CHANNEL(midi
);
76 #ifdef SNDCTL_SEQ_NRMIDIS
83 int state
; /* -1 disabled, 0 is no recording started, 1 in recording, bit 2 set if in sys exclusive recording */
85 MIDIOPENDESC midiDesc
;
89 unsigned char incoming
[3];
90 unsigned char incPrev
;
99 MIDIOPENDESC midiDesc
;
101 LPMIDIHDR lpQueueHdr
;
103 void* lpExtra
; /* according to port type (MIDI, FM...), extra data when needed */
107 static WINE_MIDIIN MidiInDev
[MAX_MIDIINDRV
];
108 static WINE_MIDIOUT MidiOutDev
[MAX_MIDIOUTDRV
];
110 /* this is the total number of MIDI out devices found (synth and port) */
111 static int MODM_NumDevs
= 0;
112 /* this is the number of FM synthesizers (index from 0 to NUMFMSYNTHDEVS - 1) */
113 static int MODM_NumFMSynthDevs
= 0;
114 /* the Midi ports have index from NUMFMSYNTHDEVS to NumDevs - 1 */
116 /* this is the total number of MIDI out devices found */
117 static int MIDM_NumDevs
= 0;
119 static int midiSeqFD
= -1;
120 static int numOpenMidiSeq
= 0;
121 static int numStartedMidiIn
= 0;
123 static CRITICAL_SECTION crit_sect
; /* protects all MidiIn buffers queues */
124 static CRITICAL_SECTION_DEBUG critsect_debug
=
127 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
128 0, 0, { (DWORD_PTR
)(__FILE__
": crit_sect") }
130 static CRITICAL_SECTION crit_sect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
132 static int end_thread
;
133 static HANDLE hThread
;
135 /*======================================================================*
136 * Low level MIDI implementation *
137 *======================================================================*/
139 static int midiOpenSeq(void);
140 static int midiCloseSeq(void);
142 /**************************************************************************
143 * MIDI_unixToWindowsDeviceType [internal]
145 * return the Windows equivalent to a Unix Device Type
148 static int MIDI_UnixToWindowsDeviceType(int type
)
150 /* MOD_MIDIPORT output port
151 * MOD_SYNTH generic internal synth
152 * MOD_SQSYNTH square wave internal synth
153 * MOD_FMSYNTH FM internal synth
154 * MOD_MAPPER MIDI mapper
155 * MOD_WAVETABLE hardware wavetable internal synth
156 * MOD_SWSYNTH software internal synth
159 /* FIXME Is this really the correct equivalence from UNIX to
160 Windows Sound type */
163 case SYNTH_TYPE_FM
: return MOD_FMSYNTH
;
164 case SYNTH_TYPE_SAMPLE
: return MOD_SYNTH
;
165 case SYNTH_TYPE_MIDI
: return MOD_MIDIPORT
;
167 ERR("Cannot determine the type of this midi device. "
168 "Assuming FM Synth\n");
173 static int MIDI_loadcount
;
174 /**************************************************************************
175 * OSS_MidiInit [internal]
177 * Initializes the MIDI devices information variables
179 static LRESULT
OSS_MidiInit(void)
181 int i
, status
, numsynthdevs
= 255, nummididevs
= 255;
182 struct synth_info sinfo
;
183 struct midi_info minfo
;
185 TRACE("(%i)\n", MIDI_loadcount
);
186 if (MIDI_loadcount
++)
189 TRACE("Initializing the MIDI variables.\n");
191 /* try to open device */
192 if (midiOpenSeq() == -1) {
196 /* find how many Synth devices are there in the system */
197 status
= ioctl(midiSeqFD
, SNDCTL_SEQ_NRSYNTHS
, &numsynthdevs
);
200 ERR("ioctl for nr synth failed.\n");
205 if (numsynthdevs
> MAX_MIDIOUTDRV
) {
206 ERR("MAX_MIDIOUTDRV (%d) was enough for the number of devices (%d). "
207 "Some FM devices will not be available.\n",MAX_MIDIOUTDRV
,numsynthdevs
);
208 numsynthdevs
= MAX_MIDIOUTDRV
;
211 for (i
= 0; i
< numsynthdevs
; i
++) {
212 /* Manufac ID. We do not have access to this with soundcard.h
213 * Does not seem to be a problem, because in mmsystem.h only
214 * Microsoft's ID is listed.
216 MidiOutDev
[i
].caps
.wMid
= 0x00FF;
217 MidiOutDev
[i
].caps
.wPid
= 0x0001; /* FIXME Product ID */
218 /* Product Version. We simply say "1" */
219 MidiOutDev
[i
].caps
.vDriverVersion
= 0x001;
220 /* The following are mandatory for MOD_MIDIPORT */
221 MidiOutDev
[i
].caps
.wChannelMask
= 0xFFFF;
222 MidiOutDev
[i
].caps
.wVoices
= 0;
223 MidiOutDev
[i
].caps
.wNotes
= 0;
224 MidiOutDev
[i
].caps
.dwSupport
= 0;
227 status
= ioctl(midiSeqFD
, SNDCTL_SYNTH_INFO
, &sinfo
);
229 static const WCHAR fmt
[] = {'W','i','n','e',' ','O','S','S',' ','M','i','d','i',' ','O','u','t',' ','#','%','d',' ','d','i','s','a','b','l','e','d',0};
230 ERR("ioctl for synth info failed on %d, disabling it.\n", i
);
232 wsprintfW(MidiOutDev
[i
].caps
.szPname
, fmt
, i
);
234 MidiOutDev
[i
].caps
.wTechnology
= MOD_MIDIPORT
;
235 MidiOutDev
[i
].bEnabled
= FALSE
;
237 MultiByteToWideChar( CP_UNIXCP
, 0, sinfo
.name
, -1,
238 MidiOutDev
[i
].caps
.szPname
,
239 sizeof(MidiOutDev
[i
].caps
.szPname
)/sizeof(WCHAR
) );
240 MidiOutDev
[i
].caps
.wTechnology
= MIDI_UnixToWindowsDeviceType(sinfo
.synth_type
);
242 if (MOD_MIDIPORT
!= MidiOutDev
[i
].caps
.wTechnology
) {
243 /* FIXME Do we have this information?
244 * Assuming the soundcards can handle
245 * MIDICAPS_VOLUME and MIDICAPS_LRVOLUME but
246 * not MIDICAPS_CACHE.
248 MidiOutDev
[i
].caps
.dwSupport
= MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
;
249 MidiOutDev
[i
].caps
.wVoices
= sinfo
.nr_voices
;
251 /* FIXME Is it possible to know the maximum
252 * number of simultaneous notes of a soundcard ?
253 * I believe we don't have this information, but
254 * it's probably equal or more than wVoices
256 MidiOutDev
[i
].caps
.wNotes
= sinfo
.nr_voices
;
258 MidiOutDev
[i
].bEnabled
= TRUE
;
260 /* We also have the information sinfo.synth_subtype, not used here
262 if (sinfo
.capabilities
& SYNTH_CAP_INPUT
) {
263 FIXME("Synthesizer supports MIDI in. Not yet supported.\n");
265 TRACE("SynthOut[%d]\tOSS info: synth type=%d/%d capa=%lx\n",
266 i
, sinfo
.synth_type
, sinfo
.synth_subtype
, (long)sinfo
.capabilities
);
269 TRACE("SynthOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%d\n",
270 i
, wine_dbgstr_w(MidiOutDev
[i
].caps
.szPname
),
271 MidiOutDev
[i
].caps
.wTechnology
,
272 MidiOutDev
[i
].caps
.wVoices
, MidiOutDev
[i
].caps
.wNotes
,
273 MidiOutDev
[i
].caps
.wChannelMask
, MidiOutDev
[i
].caps
.dwSupport
);
276 /* find how many MIDI devices are there in the system */
277 status
= ioctl(midiSeqFD
, SNDCTL_SEQ_NRMIDIS
, &nummididevs
);
279 ERR("ioctl on nr midi failed.\n");
284 /* FIXME: the two restrictions below could be loosened in some cases */
285 if (numsynthdevs
+ nummididevs
> MAX_MIDIOUTDRV
) {
286 ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
287 "Some MIDI devices will not be available.\n");
288 nummididevs
= MAX_MIDIOUTDRV
- numsynthdevs
;
291 if (nummididevs
> MAX_MIDIINDRV
) {
292 ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
293 "Some MIDI devices will not be available.\n",MAX_MIDIINDRV
,nummididevs
);
294 nummididevs
= MAX_MIDIINDRV
;
297 for (i
= 0; i
< nummididevs
; i
++) {
299 status
= ioctl(midiSeqFD
, SNDCTL_MIDI_INFO
, &minfo
);
300 if (status
== -1) WARN("ioctl on midi info for device %d failed.\n", i
);
302 /* This whole part is somewhat obscure to me. I'll keep trying to dig
303 info about it. If you happen to know, please tell us. The very
304 descriptive minfo.dev_type was not used here.
306 /* Manufacturer ID. We do not have access to this with soundcard.h
307 Does not seem to be a problem, because in mmsystem.h only
308 Microsoft's ID is listed */
309 MidiOutDev
[numsynthdevs
+ i
].caps
.wMid
= 0x00FF;
310 MidiOutDev
[numsynthdevs
+ i
].caps
.wPid
= 0x0001; /* FIXME Product ID */
311 /* Product Version. We simply say "1" */
312 MidiOutDev
[numsynthdevs
+ i
].caps
.vDriverVersion
= 0x001;
314 static const WCHAR fmt
[] = {'W','i','n','e',' ','O','S','S',' ','M','i','d','i',' ','O','u','t',' ','#','%','d',' ','d','i','s','a','b','l','e','d',0};
315 wsprintfW(MidiOutDev
[numsynthdevs
+ i
].caps
.szPname
, fmt
, numsynthdevs
+ i
);
316 MidiOutDev
[numsynthdevs
+ i
].bEnabled
= FALSE
;
318 MultiByteToWideChar(CP_UNIXCP
, 0, minfo
.name
, -1,
319 MidiOutDev
[numsynthdevs
+ i
].caps
.szPname
,
320 sizeof(MidiOutDev
[numsynthdevs
+ i
].caps
.szPname
) / sizeof(WCHAR
));
321 MidiOutDev
[numsynthdevs
+ i
].bEnabled
= TRUE
;
323 MidiOutDev
[numsynthdevs
+ i
].caps
.wTechnology
= MOD_MIDIPORT
;
324 MidiOutDev
[numsynthdevs
+ i
].caps
.wVoices
= 0;
325 MidiOutDev
[numsynthdevs
+ i
].caps
.wNotes
= 0;
326 MidiOutDev
[numsynthdevs
+ i
].caps
.wChannelMask
= 0xFFFF;
327 MidiOutDev
[numsynthdevs
+ i
].caps
.dwSupport
= 0;
329 /* This whole part is somewhat obscure to me. I'll keep trying to dig
330 info about it. If you happen to know, please tell us. The very
331 descriptive minfo.dev_type was not used here.
333 /* Manufac ID. We do not have access to this with soundcard.h
334 Does not seem to be a problem, because in mmsystem.h only
335 Microsoft's ID is listed */
336 MidiInDev
[i
].caps
.wMid
= 0x00FF;
337 MidiInDev
[i
].caps
.wPid
= 0x0001; /* FIXME Product ID */
338 /* Product Version. We simply say "1" */
339 MidiInDev
[i
].caps
.vDriverVersion
= 0x001;
341 static const WCHAR fmt
[] = {'W','i','n','e',' ','O','S','S',' ','M','i','d','i',' ','I','n',' ','#','%','d',' ','d','i','s','a','b','l','e','d',0};
342 wsprintfW(MidiInDev
[i
].caps
.szPname
, fmt
, numsynthdevs
+ i
);
343 MidiInDev
[i
].state
= -1;
345 MultiByteToWideChar(CP_UNIXCP
, 0, minfo
.name
, -1,
346 MidiInDev
[i
].caps
.szPname
,
347 sizeof(MidiInDev
[i
].caps
.szPname
) / sizeof(WCHAR
));
348 MidiInDev
[i
].state
= 0;
350 MidiInDev
[i
].caps
.dwSupport
= 0; /* mandatory with MIDIINCAPS */
352 TRACE("OSS info: midi[%d] dev-type=%d capa=%lx\n"
353 "\tMidiOut[%d] name='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%d\n"
354 "\tMidiIn [%d] name='%s' support=%d\n",
355 i
, minfo
.dev_type
, (long)minfo
.capabilities
,
356 numsynthdevs
+ i
, wine_dbgstr_w(MidiOutDev
[numsynthdevs
+ i
].caps
.szPname
),
357 MidiOutDev
[numsynthdevs
+ i
].caps
.wTechnology
,
358 MidiOutDev
[numsynthdevs
+ i
].caps
.wVoices
, MidiOutDev
[numsynthdevs
+ i
].caps
.wNotes
,
359 MidiOutDev
[numsynthdevs
+ i
].caps
.wChannelMask
, MidiOutDev
[numsynthdevs
+ i
].caps
.dwSupport
,
360 i
, wine_dbgstr_w(MidiInDev
[i
].caps
.szPname
), MidiInDev
[i
].caps
.dwSupport
);
364 /* windows does not seem to differentiate Synth from MIDI devices */
365 MODM_NumFMSynthDevs
= numsynthdevs
;
366 MODM_NumDevs
= numsynthdevs
+ nummididevs
;
368 MIDM_NumDevs
= nummididevs
;
370 /* close file and exit */
376 /**************************************************************************
377 * OSS_MidiExit [internal]
379 * Release the MIDI devices information variables
381 static LRESULT
OSS_MidiExit(void)
383 TRACE("(%i)\n", MIDI_loadcount
);
385 if (--MIDI_loadcount
)
388 ZeroMemory(MidiInDev
, sizeof(MidiInDev
));
389 ZeroMemory(MidiOutDev
, sizeof(MidiOutDev
));
392 MODM_NumFMSynthDevs
= 0;
398 /**************************************************************************
399 * MIDI_NotifyClient [internal]
401 static void MIDI_NotifyClient(UINT wDevID
, WORD wMsg
,
402 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
404 DWORD_PTR dwCallBack
;
407 DWORD_PTR dwInstance
;
409 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",
410 wDevID
, wMsg
, dwParam1
, dwParam2
);
417 if (wDevID
> MODM_NumDevs
) return;
419 dwCallBack
= MidiOutDev
[wDevID
].midiDesc
.dwCallback
;
420 uFlags
= MidiOutDev
[wDevID
].wFlags
;
421 hDev
= MidiOutDev
[wDevID
].midiDesc
.hMidi
;
422 dwInstance
= MidiOutDev
[wDevID
].midiDesc
.dwInstance
;
432 if (wDevID
> MIDM_NumDevs
) return;
434 dwCallBack
= MidiInDev
[wDevID
].midiDesc
.dwCallback
;
435 uFlags
= MidiInDev
[wDevID
].wFlags
;
436 hDev
= MidiInDev
[wDevID
].midiDesc
.hMidi
;
437 dwInstance
= MidiInDev
[wDevID
].midiDesc
.dwInstance
;
440 ERR("Unsupported MSW-MIDI message %u\n", wMsg
);
444 DriverCallback(dwCallBack
, uFlags
, hDev
, wMsg
, dwInstance
, dwParam1
, dwParam2
);
447 static int midi_warn
= 1;
448 /**************************************************************************
449 * midiOpenSeq [internal]
451 static int midiOpenSeq(void)
453 if (numOpenMidiSeq
== 0) {
455 device
=getenv("MIDIDEV");
456 if (!device
) device
="/dev/sequencer";
457 midiSeqFD
= open(device
, O_RDWR
, 0);
458 if (midiSeqFD
== -1) {
461 WARN("Can't open MIDI device '%s' ! (%s). If your "
462 "program needs this (probably not): %s\n",
463 device
, strerror(errno
),
465 "create it ! (\"man MAKEDEV\" ?)" :
467 "load MIDI sequencer kernel driver !" :
469 "grant access ! (\"man chmod\")" : ""
476 if (fcntl(midiSeqFD
, F_SETFL
, O_NONBLOCK
) < 0) {
477 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno
, strerror(errno
));
483 fcntl(midiSeqFD
, F_SETFD
, 1); /* set close on exec flag */
484 ioctl(midiSeqFD
, SNDCTL_SEQ_RESET
);
490 /**************************************************************************
491 * midiCloseSeq [internal]
493 static int midiCloseSeq(void)
495 if (--numOpenMidiSeq
== 0) {
502 /* FIXME: this is a bad idea, it's even not static... */
505 /* FIXME: this is not reentrant, not static - because of global variable
508 /**************************************************************************
509 * seqbuf_dump [internal]
511 * Used by SEQ_DUMPBUF to flush the buffer.
514 void seqbuf_dump(void)
517 if (write(midiSeqFD
, _seqbuf
, _seqbufptr
) == -1) {
518 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
519 midiSeqFD
, errno
, strerror(errno
));
522 * in any case buffer is lost so that if many errors occur the buffer
529 /**************************************************************************
530 * midReceiveChar [internal]
532 static void midReceiveChar(WORD wDevID
, unsigned char value
, DWORD dwTime
)
536 TRACE("Adding %02xh to %d[%d]\n", value
, wDevID
, MidiInDev
[wDevID
].incLen
);
538 if (wDevID
>= MIDM_NumDevs
) {
542 if (MidiInDev
[wDevID
].state
<= 0) {
543 TRACE("disabled or input not started, thrown away\n");
547 if (MidiInDev
[wDevID
].state
& 2) { /* system exclusive */
551 EnterCriticalSection(&crit_sect
);
552 if ((lpMidiHdr
= MidiInDev
[wDevID
].lpQueueHdr
) != NULL
) {
553 LPBYTE lpData
= (LPBYTE
) lpMidiHdr
->lpData
;
555 lpData
[lpMidiHdr
->dwBytesRecorded
++] = value
;
556 if (lpMidiHdr
->dwBytesRecorded
== lpMidiHdr
->dwBufferLength
) {
560 if (value
== 0xF7) { /* then end */
561 MidiInDev
[wDevID
].state
&= ~2;
564 if (sbfb
&& lpMidiHdr
!= NULL
) {
565 lpMidiHdr
= MidiInDev
[wDevID
].lpQueueHdr
;
566 MidiInDev
[wDevID
].lpQueueHdr
= lpMidiHdr
->lpNext
;
567 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
568 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
569 MIDI_NotifyClient(wDevID
, MIM_LONGDATA
, (DWORD_PTR
)lpMidiHdr
, dwTime
);
571 LeaveCriticalSection(&crit_sect
);
575 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
576 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
578 if (!IS_CMD(value
) && MidiInDev
[wDevID
].incLen
== 0) { /* try to reuse old cmd */
579 if (IS_CMD(MidiInDev
[wDevID
].incPrev
) && !IS_SYS_CMD(MidiInDev
[wDevID
].incPrev
)) {
580 MidiInDev
[wDevID
].incoming
[0] = MidiInDev
[wDevID
].incPrev
;
581 MidiInDev
[wDevID
].incLen
= 1;
582 TRACE("Reusing old command %02xh\n", MidiInDev
[wDevID
].incPrev
);
584 FIXME("error for midi-in, should generate MIM_ERROR notification:"
585 " prev=%02Xh, incLen=%02Xh\n",
586 MidiInDev
[wDevID
].incPrev
, MidiInDev
[wDevID
].incLen
);
590 MidiInDev
[wDevID
].incoming
[(int)(MidiInDev
[wDevID
].incLen
++)] = value
;
591 if (MidiInDev
[wDevID
].incLen
== 1 && !IS_SYS_CMD(MidiInDev
[wDevID
].incoming
[0])) {
592 /* store new cmd, just in case */
593 MidiInDev
[wDevID
].incPrev
= MidiInDev
[wDevID
].incoming
[0];
599 switch (MidiInDev
[wDevID
].incoming
[0] & 0xF0) {
602 case MIDI_KEY_PRESSURE
:
603 case MIDI_CTL_CHANGE
:
604 case MIDI_PITCH_BEND
:
605 if (MidiInDev
[wDevID
].incLen
== 3) {
606 toSend
= (MidiInDev
[wDevID
].incoming
[2] << 16) |
607 (MidiInDev
[wDevID
].incoming
[1] << 8) |
608 (MidiInDev
[wDevID
].incoming
[0] << 0);
611 case MIDI_PGM_CHANGE
:
612 case MIDI_CHN_PRESSURE
:
613 if (MidiInDev
[wDevID
].incLen
== 2) {
614 toSend
= (MidiInDev
[wDevID
].incoming
[1] << 8) |
615 (MidiInDev
[wDevID
].incoming
[0] << 0);
618 case MIDI_SYSTEM_PREFIX
:
619 if (MidiInDev
[wDevID
].incoming
[0] == 0xF0) {
620 MidiInDev
[wDevID
].state
|= 2;
621 MidiInDev
[wDevID
].incLen
= 0;
623 if (MidiInDev
[wDevID
].incLen
== 1) {
624 toSend
= (MidiInDev
[wDevID
].incoming
[0] << 0);
629 WARN("This shouldn't happen (%02X)\n", MidiInDev
[wDevID
].incoming
[0]);
632 TRACE("Sending event %08x\n", toSend
);
633 MidiInDev
[wDevID
].incLen
= 0;
634 dwTime
-= MidiInDev
[wDevID
].startTime
;
635 MIDI_NotifyClient(wDevID
, MIM_DATA
, toSend
, dwTime
);
639 static DWORD WINAPI
midRecThread(LPVOID arg
)
641 unsigned char buffer
[256];
646 TRACE("Thread startup\n");
652 TRACE("Thread loop\n");
654 /* Check if an event is present */
655 if (poll(&pfd
, 1, 250) <= 0)
658 len
= read(midiSeqFD
, buffer
, sizeof(buffer
));
659 TRACE("Received %d bytes\n", len
);
661 if (len
< 0) continue;
662 if ((len
% 4) != 0) {
663 WARN("Bad length %d, errno %d (%s)\n", len
, errno
, strerror(errno
));
667 dwTime
= GetTickCount();
669 for (idx
= 0; idx
< len
; ) {
670 if (buffer
[idx
] & 0x80) {
672 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
673 buffer
[idx
+ 0], buffer
[idx
+ 1],
674 buffer
[idx
+ 2], buffer
[idx
+ 3],
675 buffer
[idx
+ 4], buffer
[idx
+ 5],
676 buffer
[idx
+ 6], buffer
[idx
+ 7]);
679 switch (buffer
[idx
+ 0]) {
684 midReceiveChar(buffer
[idx
+ 2], buffer
[idx
+ 1], dwTime
);
687 TRACE("Unsupported event %d\n", buffer
[idx
+ 0]);
697 /**************************************************************************
698 * midGetDevCaps [internal]
700 static DWORD
midGetDevCaps(WORD wDevID
, LPMIDIINCAPSW lpCaps
, DWORD dwSize
)
702 TRACE("(%04X, %p, %08X);\n", wDevID
, lpCaps
, dwSize
);
704 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
705 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
707 memcpy(lpCaps
, &MidiInDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
709 return MMSYSERR_NOERROR
;
712 /**************************************************************************
715 static DWORD
midOpen(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
717 TRACE("(%04X, %p, %08X);\n", wDevID
, lpDesc
, dwFlags
);
719 if (lpDesc
== NULL
) {
720 WARN("Invalid Parameter !\n");
721 return MMSYSERR_INVALPARAM
;
725 * how to check that content of lpDesc is correct ?
727 if (wDevID
>= MIDM_NumDevs
) {
728 WARN("wDevID too large (%u) !\n", wDevID
);
729 return MMSYSERR_BADDEVICEID
;
731 if (MidiInDev
[wDevID
].state
== -1) {
732 WARN("device disabled\n");
733 return MIDIERR_NODEVICE
;
735 if (MidiInDev
[wDevID
].midiDesc
.hMidi
!= 0) {
736 WARN("device already open !\n");
737 return MMSYSERR_ALLOCATED
;
739 if ((dwFlags
& MIDI_IO_STATUS
) != 0) {
740 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
741 dwFlags
&= ~MIDI_IO_STATUS
;
743 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
744 FIXME("Bad dwFlags\n");
745 return MMSYSERR_INVALFLAG
;
748 if (midiOpenSeq() < 0) {
749 return MMSYSERR_ERROR
;
752 if (numStartedMidiIn
++ == 0) {
754 hThread
= CreateThread(NULL
, 0, midRecThread
, NULL
, 0, NULL
);
756 numStartedMidiIn
= 0;
757 WARN("Couldn't create thread for midi-in\n");
759 return MMSYSERR_ERROR
;
761 SetThreadPriority(hThread
, THREAD_PRIORITY_TIME_CRITICAL
);
762 TRACE("Created thread for midi-in\n");
765 MidiInDev
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
767 MidiInDev
[wDevID
].lpQueueHdr
= NULL
;
768 MidiInDev
[wDevID
].dwTotalPlayed
= 0;
769 MidiInDev
[wDevID
].bufsize
= 0x3FFF;
770 MidiInDev
[wDevID
].midiDesc
= *lpDesc
;
771 MidiInDev
[wDevID
].state
= 0;
772 MidiInDev
[wDevID
].incLen
= 0;
773 MidiInDev
[wDevID
].startTime
= 0;
775 MIDI_NotifyClient(wDevID
, MIM_OPEN
, 0L, 0L);
776 return MMSYSERR_NOERROR
;
779 /**************************************************************************
780 * midClose [internal]
782 static DWORD
midClose(WORD wDevID
)
784 int ret
= MMSYSERR_NOERROR
;
786 TRACE("(%04X);\n", wDevID
);
788 if (wDevID
>= MIDM_NumDevs
) {
789 WARN("wDevID too big (%u) !\n", wDevID
);
790 return MMSYSERR_BADDEVICEID
;
792 if (MidiInDev
[wDevID
].midiDesc
.hMidi
== 0) {
793 WARN("device not opened !\n");
794 return MMSYSERR_ERROR
;
796 if (MidiInDev
[wDevID
].lpQueueHdr
!= 0) {
797 return MIDIERR_STILLPLAYING
;
800 if (midiSeqFD
== -1) {
802 return MMSYSERR_ERROR
;
804 if (--numStartedMidiIn
== 0) {
805 TRACE("Stopping thread for midi-in\n");
807 if (WaitForSingleObject(hThread
, 5000) != WAIT_OBJECT_0
) {
808 WARN("Thread end not signaled, force termination\n");
809 TerminateThread(hThread
, 0);
811 TRACE("Stopped thread for midi-in\n");
815 MidiInDev
[wDevID
].bufsize
= 0;
816 MIDI_NotifyClient(wDevID
, MIM_CLOSE
, 0L, 0L);
817 MidiInDev
[wDevID
].midiDesc
.hMidi
= 0;
821 /**************************************************************************
822 * midAddBuffer [internal]
824 static DWORD
midAddBuffer(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
826 TRACE("(%04X, %p, %d);\n", wDevID
, lpMidiHdr
, dwSize
);
828 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
829 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
831 if (lpMidiHdr
== NULL
) return MMSYSERR_INVALPARAM
;
832 if (dwSize
< offsetof(MIDIHDR
,dwOffset
)) return MMSYSERR_INVALPARAM
;
833 if (lpMidiHdr
->dwBufferLength
== 0) return MMSYSERR_INVALPARAM
;
834 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) return MIDIERR_STILLPLAYING
;
835 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) return MIDIERR_UNPREPARED
;
837 EnterCriticalSection(&crit_sect
);
838 lpMidiHdr
->dwFlags
&= ~WHDR_DONE
;
839 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
840 lpMidiHdr
->dwBytesRecorded
= 0;
841 lpMidiHdr
->lpNext
= 0;
842 if (MidiInDev
[wDevID
].lpQueueHdr
== 0) {
843 MidiInDev
[wDevID
].lpQueueHdr
= lpMidiHdr
;
847 for (ptr
= MidiInDev
[wDevID
].lpQueueHdr
;
850 ptr
->lpNext
= lpMidiHdr
;
852 LeaveCriticalSection(&crit_sect
);
854 return MMSYSERR_NOERROR
;
857 /**************************************************************************
858 * midPrepare [internal]
860 static DWORD
midPrepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
862 TRACE("(%04X, %p, %d);\n", wDevID
, lpMidiHdr
, dwSize
);
864 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
865 return MMSYSERR_INVALPARAM
;
866 if (lpMidiHdr
->dwFlags
& MHDR_PREPARED
)
867 return MMSYSERR_NOERROR
;
869 lpMidiHdr
->lpNext
= 0;
870 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
871 lpMidiHdr
->dwFlags
&= ~(MHDR_DONE
|MHDR_INQUEUE
); /* flags cleared since w2k */
873 return MMSYSERR_NOERROR
;
876 /**************************************************************************
877 * midUnprepare [internal]
879 static DWORD
midUnprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
881 TRACE("(%04X, %p, %d);\n", wDevID
, lpMidiHdr
, dwSize
);
883 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
884 return MMSYSERR_INVALPARAM
;
885 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
886 return MMSYSERR_NOERROR
;
887 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
888 return MIDIERR_STILLPLAYING
;
890 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
892 return MMSYSERR_NOERROR
;
895 /**************************************************************************
896 * midReset [internal]
898 static DWORD
midReset(WORD wDevID
)
900 DWORD dwTime
= GetTickCount();
902 TRACE("(%04X);\n", wDevID
);
904 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
905 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
907 EnterCriticalSection(&crit_sect
);
908 while (MidiInDev
[wDevID
].lpQueueHdr
) {
909 LPMIDIHDR lpMidiHdr
= MidiInDev
[wDevID
].lpQueueHdr
;
910 MidiInDev
[wDevID
].lpQueueHdr
= lpMidiHdr
->lpNext
;
911 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
912 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
913 MIDI_NotifyClient(wDevID
, MIM_LONGDATA
, (DWORD_PTR
)lpMidiHdr
, dwTime
);
915 LeaveCriticalSection(&crit_sect
);
917 return MMSYSERR_NOERROR
;
920 /**************************************************************************
921 * midStart [internal]
923 static DWORD
midStart(WORD wDevID
)
925 TRACE("(%04X);\n", wDevID
);
927 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
928 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
930 MidiInDev
[wDevID
].state
= 1;
931 MidiInDev
[wDevID
].startTime
= GetTickCount();
932 return MMSYSERR_NOERROR
;
935 /**************************************************************************
938 static DWORD
midStop(WORD wDevID
)
940 TRACE("(%04X);\n", wDevID
);
942 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
943 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
945 MidiInDev
[wDevID
].state
= 0;
946 return MMSYSERR_NOERROR
;
949 /*-----------------------------------------------------------------------*/
951 typedef struct sVoice
{
952 int note
; /* 0 means not used */
954 unsigned cntMark
: 30,
957 #define sVS_PLAYING 1
958 #define sVS_SUSTAINED 2
961 typedef struct sChannel
{
967 int bank
; /* CTL_BANK_SELECT */
968 int volume
; /* CTL_MAIN_VOLUME */
969 int balance
; /* CTL_BALANCE */
970 int expression
; /* CTL_EXPRESSION */
971 int sustain
; /* CTL_SUSTAIN */
973 unsigned char nrgPmtMSB
; /* Non register Parameters */
974 unsigned char nrgPmtLSB
;
975 unsigned char regPmtMSB
; /* Non register Parameters */
976 unsigned char regPmtLSB
;
979 typedef struct sFMextra
{
982 sChannel channel
[16]; /* MIDI has only 16 channels */
983 sVoice voice
[1]; /* dyn allocated according to sound card */
984 /* do not append fields below voice[1] since the size of this structure
985 * depends on the number of available voices on the FM synth...
989 extern const unsigned char midiFMInstrumentPatches
[16 * 128];
990 extern const unsigned char midiFMDrumsPatches
[16 * 128];
992 /**************************************************************************
993 * modFMLoad [internal]
995 static int modFMLoad(int dev
)
998 struct sbi_instrument sbi
;
1003 memset(sbi
.operators
+ 16, 0, 16);
1004 for (i
= 0; i
< 128; i
++) {
1006 memcpy(sbi
.operators
, midiFMInstrumentPatches
+ i
* 16, 16);
1008 if (write(midiSeqFD
, &sbi
, sizeof(sbi
)) == -1) {
1009 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi
.channel
, errno
, strerror(errno
));
1013 for (i
= 0; i
< 128; i
++) {
1014 sbi
.channel
= 128 + i
;
1015 memcpy(sbi
.operators
, midiFMDrumsPatches
+ i
* 16, 16);
1017 if (write(midiSeqFD
, &sbi
, sizeof(sbi
)) == -1) {
1018 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi
.channel
, errno
, strerror(errno
));
1025 /**************************************************************************
1026 * modFMReset [internal]
1028 static void modFMReset(WORD wDevID
)
1030 sFMextra
* extra
= MidiOutDev
[wDevID
].lpExtra
;
1031 sVoice
* voice
= extra
->voice
;
1032 sChannel
* channel
= extra
->channel
;
1035 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1036 if (voice
[i
].status
!= sVS_UNUSED
) {
1037 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1039 SEQ_KEY_PRESSURE(wDevID
, i
, 127, 0);
1040 SEQ_CONTROL(wDevID
, i
, SEQ_VOLMODE
, VOL_METHOD_LINEAR
);
1042 voice
[i
].channel
= -1;
1043 voice
[i
].cntMark
= 0;
1044 voice
[i
].status
= sVS_UNUSED
;
1046 for (i
= 0; i
< 16; i
++) {
1047 channel
[i
].program
= 0;
1048 channel
[i
].bender
= 8192;
1049 channel
[i
].benderRange
= 2;
1050 channel
[i
].bank
= 0;
1051 channel
[i
].volume
= 127;
1052 channel
[i
].balance
= 64;
1053 channel
[i
].expression
= 0;
1054 channel
[i
].sustain
= 0;
1057 extra
->drumSetMask
= 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
1061 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
1063 /**************************************************************************
1064 * modGetDevCaps [internal]
1066 static DWORD
modGetDevCaps(WORD wDevID
, LPMIDIOUTCAPSW lpCaps
, DWORD dwSize
)
1068 TRACE("(%04X, %p, %08X);\n", wDevID
, lpCaps
, dwSize
);
1070 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1071 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
1073 memcpy(lpCaps
, &MidiOutDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
1075 return MMSYSERR_NOERROR
;
1078 /**************************************************************************
1079 * modOpen [internal]
1081 static DWORD
modOpen(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
1083 TRACE("(%04X, %p, %08X);\n", wDevID
, lpDesc
, dwFlags
);
1084 if (lpDesc
== NULL
) {
1085 WARN("Invalid Parameter !\n");
1086 return MMSYSERR_INVALPARAM
;
1088 if (wDevID
>= MODM_NumDevs
) {
1089 TRACE("MAX_MIDIOUTDRV reached !\n");
1090 return MMSYSERR_BADDEVICEID
;
1092 if (MidiOutDev
[wDevID
].midiDesc
.hMidi
!= 0) {
1093 WARN("device already open !\n");
1094 return MMSYSERR_ALLOCATED
;
1096 if (!MidiOutDev
[wDevID
].bEnabled
) {
1097 WARN("device disabled !\n");
1098 return MIDIERR_NODEVICE
;
1100 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
1101 WARN("bad dwFlags\n");
1102 return MMSYSERR_INVALFLAG
;
1105 MidiOutDev
[wDevID
].lpExtra
= 0;
1107 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1112 extra
= HeapAlloc(GetProcessHeap(), 0,
1113 offsetof(struct sFMextra
, voice
[MidiOutDev
[wDevID
].caps
.wVoices
]));
1116 WARN("can't alloc extra data !\n");
1117 return MMSYSERR_NOMEM
;
1119 MidiOutDev
[wDevID
].lpExtra
= extra
;
1120 if (midiOpenSeq() < 0) {
1121 MidiOutDev
[wDevID
].lpExtra
= 0;
1122 HeapFree(GetProcessHeap(), 0, extra
);
1123 return MMSYSERR_ERROR
;
1125 if (modFMLoad(wDevID
) < 0) {
1127 MidiOutDev
[wDevID
].lpExtra
= 0;
1128 HeapFree(GetProcessHeap(), 0, extra
);
1129 return MMSYSERR_ERROR
;
1136 if (midiOpenSeq() < 0) {
1137 return MMSYSERR_ALLOCATED
;
1141 WARN("Technology not supported (yet) %d !\n",
1142 MidiOutDev
[wDevID
].caps
.wTechnology
);
1143 return MMSYSERR_NOTENABLED
;
1146 MidiOutDev
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1148 MidiOutDev
[wDevID
].lpQueueHdr
= NULL
;
1149 MidiOutDev
[wDevID
].dwTotalPlayed
= 0;
1150 MidiOutDev
[wDevID
].bufsize
= 0x3FFF;
1151 MidiOutDev
[wDevID
].midiDesc
= *lpDesc
;
1153 MIDI_NotifyClient(wDevID
, MOM_OPEN
, 0L, 0L);
1154 TRACE("Successful !\n");
1155 return MMSYSERR_NOERROR
;
1159 /**************************************************************************
1160 * modClose [internal]
1162 static DWORD
modClose(WORD wDevID
)
1164 int ret
= MMSYSERR_NOERROR
;
1166 TRACE("(%04X);\n", wDevID
);
1168 if (MidiOutDev
[wDevID
].midiDesc
.hMidi
== 0) {
1169 WARN("device not opened !\n");
1170 return MMSYSERR_ERROR
;
1172 /* FIXME: should test that no pending buffer is still in the queue for
1175 if (midiSeqFD
== -1) {
1176 WARN("can't close !\n");
1177 return MMSYSERR_ERROR
;
1180 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1186 WARN("Technology not supported (yet) %d !\n",
1187 MidiOutDev
[wDevID
].caps
.wTechnology
);
1188 return MMSYSERR_NOTENABLED
;
1191 HeapFree(GetProcessHeap(), 0, MidiOutDev
[wDevID
].lpExtra
);
1192 MidiOutDev
[wDevID
].lpExtra
= 0;
1194 MidiOutDev
[wDevID
].bufsize
= 0;
1195 MIDI_NotifyClient(wDevID
, MOM_CLOSE
, 0L, 0L);
1196 MidiOutDev
[wDevID
].midiDesc
.hMidi
= 0;
1200 /**************************************************************************
1201 * modData [internal]
1203 static DWORD
modData(WORD wDevID
, DWORD dwParam
)
1205 WORD evt
= LOBYTE(LOWORD(dwParam
));
1206 WORD d1
= HIBYTE(LOWORD(dwParam
));
1207 WORD d2
= LOBYTE(HIWORD(dwParam
));
1209 TRACE("(%04X, %08X);\n", wDevID
, dwParam
);
1211 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1212 if (!MidiOutDev
[wDevID
].bEnabled
) return MIDIERR_NODEVICE
;
1214 if (midiSeqFD
== -1) {
1215 WARN("can't play !\n");
1216 return MIDIERR_NODEVICE
;
1218 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1221 * - chorus depth controller is not used
1224 sFMextra
* extra
= MidiOutDev
[wDevID
].lpExtra
;
1225 sVoice
* voice
= extra
->voice
;
1226 sChannel
* channel
= extra
->channel
;
1227 int chn
= (evt
& 0x0F);
1230 switch (evt
& 0xF0) {
1232 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1233 /* don't stop sustained notes */
1234 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1235 voice
[i
].status
= sVS_UNUSED
;
1236 SEQ_STOP_NOTE(wDevID
, i
, d1
, d2
);
1241 if (d2
== 0) { /* note off if velocity == 0 */
1242 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1243 /* don't stop sustained notes */
1244 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1245 voice
[i
].status
= sVS_UNUSED
;
1246 SEQ_STOP_NOTE(wDevID
, i
, d1
, 64);
1251 /* finding out in this order :
1253 * - if replaying the same note on the same channel
1254 * - the older voice (LRU)
1256 for (i
= nv
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1257 if (voice
[i
].status
== sVS_UNUSED
||
1258 (voice
[i
].note
== d1
&& voice
[i
].channel
== chn
)) {
1262 if (voice
[i
].cntMark
< voice
[0].cntMark
) {
1267 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1268 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1269 nv
, channel
[chn
].program
,
1270 channel
[chn
].balance
,
1271 channel
[chn
].volume
,
1272 channel
[chn
].bender
, d1
, d2
);
1274 SEQ_SET_PATCH(wDevID
, nv
, IS_DRUM_CHANNEL(extra
, chn
) ?
1275 (128 + d1
) : channel
[chn
].program
);
1276 SEQ_BENDER_RANGE(wDevID
, nv
, channel
[chn
].benderRange
* 100);
1277 SEQ_BENDER(wDevID
, nv
, channel
[chn
].bender
);
1278 SEQ_CONTROL(wDevID
, nv
, CTL_PAN
, channel
[chn
].balance
);
1279 SEQ_CONTROL(wDevID
, nv
, CTL_EXPRESSION
, channel
[chn
].expression
);
1281 /* FIXME: does not really seem to work on my SB card and
1282 * screws everything up... so lay it down
1284 SEQ_CONTROL(wDevID
, nv
, CTL_MAIN_VOLUME
, channel
[chn
].volume
);
1286 SEQ_START_NOTE(wDevID
, nv
, d1
, d2
);
1287 voice
[nv
].status
= channel
[chn
].sustain
? sVS_SUSTAINED
: sVS_PLAYING
;
1288 voice
[nv
].note
= d1
;
1289 voice
[nv
].channel
= chn
;
1290 voice
[nv
].cntMark
= extra
->counter
++;
1292 case MIDI_KEY_PRESSURE
:
1293 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1294 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1295 SEQ_KEY_PRESSURE(wDevID
, i
, d1
, d2
);
1299 case MIDI_CTL_CHANGE
:
1301 case CTL_BANK_SELECT
: channel
[chn
].bank
= d2
; break;
1302 case CTL_MAIN_VOLUME
: channel
[chn
].volume
= d2
; break;
1303 case CTL_PAN
: channel
[chn
].balance
= d2
; break;
1304 case CTL_EXPRESSION
: channel
[chn
].expression
= d2
; break;
1305 case CTL_SUSTAIN
: channel
[chn
].sustain
= d2
;
1307 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1308 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
) {
1309 voice
[i
].status
= sVS_SUSTAINED
;
1313 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1314 if (voice
[i
].status
== sVS_SUSTAINED
&& voice
[i
].channel
== chn
) {
1315 voice
[i
].status
= sVS_UNUSED
;
1316 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1321 case CTL_NONREG_PARM_NUM_LSB
: channel
[chn
].nrgPmtLSB
= d2
; break;
1322 case CTL_NONREG_PARM_NUM_MSB
: channel
[chn
].nrgPmtMSB
= d2
; break;
1323 case CTL_REGIST_PARM_NUM_LSB
: channel
[chn
].regPmtLSB
= d2
; break;
1324 case CTL_REGIST_PARM_NUM_MSB
: channel
[chn
].regPmtMSB
= d2
; break;
1325 case CTL_DATA_ENTRY
:
1326 switch ((channel
[chn
].regPmtMSB
<< 8) | channel
[chn
].regPmtLSB
) {
1328 if (channel
[chn
].benderRange
!= d2
) {
1329 channel
[chn
].benderRange
= d2
;
1330 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1331 if (voice
[i
].channel
== chn
) {
1332 SEQ_BENDER_RANGE(wDevID
, i
, channel
[chn
].benderRange
);
1339 channel
[chn
].benderRange
= 2;
1340 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1341 if (voice
[i
].channel
== chn
) {
1342 SEQ_BENDER_RANGE(wDevID
, i
, channel
[chn
].benderRange
);
1347 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1348 channel
[chn
].regPmtMSB
, channel
[chn
].regPmtLSB
,
1349 channel
[chn
].nrgPmtMSB
, channel
[chn
].nrgPmtLSB
,
1355 case 0x78: /* all sounds off */
1356 /* FIXME: I don't know if I have to take care of the channel
1357 * for this control ?
1359 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1360 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
) {
1361 voice
[i
].status
= sVS_UNUSED
;
1362 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1366 case 0x7B: /* all notes off */
1367 /* FIXME: I don't know if I have to take care of the channel
1368 * for this control ?
1370 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1371 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
) {
1372 voice
[i
].status
= sVS_UNUSED
;
1373 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1378 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1383 case MIDI_PGM_CHANGE
:
1384 channel
[chn
].program
= d1
;
1386 case MIDI_CHN_PRESSURE
:
1387 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1388 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
) {
1389 SEQ_KEY_PRESSURE(wDevID
, i
, voice
[i
].note
, d1
);
1393 case MIDI_PITCH_BEND
:
1394 channel
[chn
].bender
= (d2
<< 7) + d1
;
1395 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1396 if (voice
[i
].channel
== chn
) {
1397 SEQ_BENDER(wDevID
, i
, channel
[chn
].bender
);
1401 case MIDI_SYSTEM_PREFIX
:
1402 switch (evt
& 0x0F) {
1403 case 0x0F: /* Reset */
1407 WARN("Unsupported (yet) system event %02x\n", evt
& 0x0F);
1411 WARN("Internal error, shouldn't happen (event=%08x)\n", evt
& 0xF0);
1412 return MMSYSERR_NOTENABLED
;
1418 int dev
= wDevID
- MODM_NumFMSynthDevs
;
1420 WARN("Internal error on devID (%u) !\n", wDevID
);
1421 return MIDIERR_NODEVICE
;
1424 switch (evt
& 0xF0) {
1427 case MIDI_KEY_PRESSURE
:
1428 case MIDI_CTL_CHANGE
:
1429 case MIDI_PITCH_BEND
:
1430 SEQ_MIDIOUT(dev
, evt
);
1431 SEQ_MIDIOUT(dev
, d1
);
1432 SEQ_MIDIOUT(dev
, d2
);
1434 case MIDI_PGM_CHANGE
:
1435 case MIDI_CHN_PRESSURE
:
1436 SEQ_MIDIOUT(dev
, evt
);
1437 SEQ_MIDIOUT(dev
, d1
);
1439 case MIDI_SYSTEM_PREFIX
:
1440 switch (evt
& 0x0F) {
1441 case 0x00: /* System Exclusive, don't do it on modData,
1442 * should require modLongData*/
1443 case 0x04: /* Undefined. */
1444 case 0x05: /* Undefined. */
1445 case 0x07: /* End of Exclusive. */
1446 case 0x09: /* Undefined. */
1447 case 0x0D: /* Undefined. */
1449 case 0x06: /* Tune Request */
1450 case 0x08: /* Timing Clock. */
1451 case 0x0A: /* Start. */
1452 case 0x0B: /* Continue */
1453 case 0x0C: /* Stop */
1454 case 0x0E: /* Active Sensing. */
1455 SEQ_MIDIOUT(dev
, evt
);
1457 case 0x0F: /* Reset */
1458 /* SEQ_MIDIOUT(dev, evt);
1459 this other way may be better */
1460 SEQ_MIDIOUT(dev
, MIDI_SYSTEM_PREFIX
);
1461 SEQ_MIDIOUT(dev
, 0x7e);
1462 SEQ_MIDIOUT(dev
, 0x7f);
1463 SEQ_MIDIOUT(dev
, 0x09);
1464 SEQ_MIDIOUT(dev
, 0x01);
1465 SEQ_MIDIOUT(dev
, 0xf7);
1467 case 0x01: /* MTC Quarter frame */
1468 case 0x03: /* Song Select. */
1469 SEQ_MIDIOUT(dev
, evt
);
1470 SEQ_MIDIOUT(dev
, d1
);
1471 case 0x02: /* Song Position Pointer. */
1472 SEQ_MIDIOUT(dev
, evt
);
1473 SEQ_MIDIOUT(dev
, d1
);
1474 SEQ_MIDIOUT(dev
, d2
);
1481 WARN("Technology not supported (yet) %d !\n",
1482 MidiOutDev
[wDevID
].caps
.wTechnology
);
1483 return MMSYSERR_NOTENABLED
;
1488 return MMSYSERR_NOERROR
;
1491 /**************************************************************************
1492 * modLongData [internal]
1494 static DWORD
modLongData(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1499 TRACE("(%04X, %p, %08X);\n", wDevID
, lpMidiHdr
, dwSize
);
1501 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
1502 * but it seems to be used only for midi input.
1503 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
1506 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1507 if (!MidiOutDev
[wDevID
].bEnabled
) return MIDIERR_NODEVICE
;
1509 if (midiSeqFD
== -1) {
1510 WARN("can't play !\n");
1511 return MIDIERR_NODEVICE
;
1514 lpData
= (LPBYTE
) lpMidiHdr
->lpData
;
1517 return MIDIERR_UNPREPARED
;
1518 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
1519 return MIDIERR_UNPREPARED
;
1520 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
1521 return MIDIERR_STILLPLAYING
;
1522 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
1523 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
1525 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1526 * data, or can it also contain raw MIDI data, to be split up and sent to
1528 * If the latter is true, then the following WARNing will fire up
1530 if (lpData
[0] != 0xF0 || lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
1531 WARN("The allegedly system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1534 TRACE("dwBufferLength=%u !\n", lpMidiHdr
->dwBufferLength
);
1535 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1536 lpData
[0], lpData
[1], lpData
[2], lpData
[lpMidiHdr
->dwBufferLength
-3],
1537 lpData
[lpMidiHdr
->dwBufferLength
-2], lpData
[lpMidiHdr
->dwBufferLength
-1]);
1539 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1541 /* FIXME: I don't think there is much to do here */
1544 if (lpData
[0] != 0xF0) {
1545 /* Send end of System Exclusive */
1546 SEQ_MIDIOUT(wDevID
- MODM_NumFMSynthDevs
, 0xF0);
1547 WARN("Adding missing 0xF0 marker at the beginning of "
1548 "system exclusive byte stream\n");
1550 for (count
= 0; count
< lpMidiHdr
->dwBufferLength
; count
++) {
1551 SEQ_MIDIOUT(wDevID
- MODM_NumFMSynthDevs
, lpData
[count
]);
1553 if (lpData
[count
- 1] != 0xF7) {
1554 /* Send end of System Exclusive */
1555 SEQ_MIDIOUT(wDevID
- MODM_NumFMSynthDevs
, 0xF7);
1556 WARN("Adding missing 0xF7 marker at the end of "
1557 "system exclusive byte stream\n");
1562 WARN("Technology not supported (yet) %d !\n",
1563 MidiOutDev
[wDevID
].caps
.wTechnology
);
1564 return MMSYSERR_NOTENABLED
;
1567 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
1568 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
1569 MIDI_NotifyClient(wDevID
, MOM_DONE
, (DWORD_PTR
)lpMidiHdr
, 0L);
1570 return MMSYSERR_NOERROR
;
1573 /**************************************************************************
1574 * modPrepare [internal]
1576 static DWORD
modPrepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1578 TRACE("(%04X, %p, %d);\n", wDevID
, lpMidiHdr
, dwSize
);
1580 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
1581 return MMSYSERR_INVALPARAM
;
1582 if (lpMidiHdr
->dwFlags
& MHDR_PREPARED
)
1583 return MMSYSERR_NOERROR
;
1585 lpMidiHdr
->lpNext
= 0;
1586 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
1587 lpMidiHdr
->dwFlags
&= ~(MHDR_DONE
|MHDR_INQUEUE
); /* flags cleared since w2k */
1588 return MMSYSERR_NOERROR
;
1591 /**************************************************************************
1592 * modUnprepare [internal]
1594 static DWORD
modUnprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1596 TRACE("(%04X, %p, %d);\n", wDevID
, lpMidiHdr
, dwSize
);
1598 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 || lpMidiHdr
->lpData
== 0)
1599 return MMSYSERR_INVALPARAM
;
1600 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
1601 return MMSYSERR_NOERROR
;
1602 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
1603 return MIDIERR_STILLPLAYING
;
1604 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
1605 return MMSYSERR_NOERROR
;
1608 /**************************************************************************
1609 * modGetVolume [internal]
1611 static DWORD
modGetVolume(WORD wDevID
, DWORD
* lpdwVolume
)
1613 if (!lpdwVolume
) return MMSYSERR_INVALPARAM
;
1614 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1615 *lpdwVolume
= 0xFFFFFFFF;
1616 return (MidiOutDev
[wDevID
].caps
.dwSupport
& MIDICAPS_VOLUME
) ? 0 : MMSYSERR_NOTSUPPORTED
;
1619 /**************************************************************************
1620 * modReset [internal]
1622 static DWORD
modReset(WORD wDevID
)
1626 TRACE("(%04X);\n", wDevID
);
1628 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1629 if (!MidiOutDev
[wDevID
].bEnabled
) return MIDIERR_NODEVICE
;
1631 /* stop all notes */
1632 /* FIXME: check if 0x78B0 is channel dependent or not. I coded it so that
1633 * it's channel dependent...
1635 for (chn
= 0; chn
< 16; chn
++) {
1636 /* turn off every note */
1637 modData(wDevID
, 0x7800 | MIDI_CTL_CHANGE
| chn
);
1638 /* remove sustain on all channels */
1639 modData(wDevID
, (CTL_SUSTAIN
<< 8) | MIDI_CTL_CHANGE
| chn
);
1641 /* FIXME: the LongData buffers must also be returned to the app */
1642 return MMSYSERR_NOERROR
;
1645 #endif /* HAVE_OSS_MIDI */
1647 /*======================================================================*
1648 * MIDI entry points *
1649 *======================================================================*/
1651 /**************************************************************************
1652 * midMessage (WINEOSS.@)
1654 DWORD WINAPI
OSS_midMessage(UINT wDevID
, UINT wMsg
, DWORD_PTR dwUser
,
1655 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
1657 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1658 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1660 #ifdef HAVE_OSS_MIDI
1662 return OSS_MidiInit();
1664 return OSS_MidiExit();
1667 /* FIXME: Pretend this is supported */
1670 return midOpen(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1672 return midClose(wDevID
);
1673 case MIDM_ADDBUFFER
:
1674 return midAddBuffer(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1676 return midPrepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1677 case MIDM_UNPREPARE
:
1678 return midUnprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1679 case MIDM_GETDEVCAPS
:
1680 return midGetDevCaps(wDevID
, (LPMIDIINCAPSW
)dwParam1
,dwParam2
);
1681 case MIDM_GETNUMDEVS
:
1682 return MIDM_NumDevs
;
1684 return midReset(wDevID
);
1686 return midStart(wDevID
);
1688 return midStop(wDevID
);
1691 case MIDM_GETNUMDEVS
:
1695 TRACE("Unsupported message\n");
1697 return MMSYSERR_NOTSUPPORTED
;
1700 /**************************************************************************
1701 * modMessage (WINEOSS.@)
1703 DWORD WINAPI
OSS_modMessage(UINT wDevID
, UINT wMsg
, DWORD_PTR dwUser
,
1704 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
1706 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1707 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1710 #ifdef HAVE_OSS_MIDI
1712 return OSS_MidiInit();
1714 return OSS_MidiExit();
1717 /* FIXME: Pretend this is supported */
1720 return modOpen(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1722 return modClose(wDevID
);
1724 return modData(wDevID
, dwParam1
);
1726 return modLongData(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1728 return modPrepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1729 case MODM_UNPREPARE
:
1730 return modUnprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1731 case MODM_GETDEVCAPS
:
1732 return modGetDevCaps(wDevID
, (LPMIDIOUTCAPSW
)dwParam1
, dwParam2
);
1733 case MODM_GETNUMDEVS
:
1734 return MODM_NumDevs
;
1735 case MODM_GETVOLUME
:
1736 return modGetVolume(wDevID
, (DWORD
*)dwParam1
);
1737 case MODM_SETVOLUME
:
1740 return modReset(wDevID
);
1743 case MODM_GETNUMDEVS
:
1747 TRACE("Unsupported message\n");
1749 return MMSYSERR_NOTSUPPORTED
;
1752 /**************************************************************************
1753 * DriverProc (WINEOSS.1)
1755 LRESULT CALLBACK
OSS_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1756 LPARAM dwParam1
, LPARAM dwParam2
)
1758 TRACE("(%08lX, %p, %08X, %08lX, %08lX)\n",
1759 dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1768 case DRV_QUERYCONFIGURE
: