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
)
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 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
567 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
568 MidiInDev
[wDevID
].lpQueueHdr
= lpMidiHdr
->lpNext
;
569 MIDI_NotifyClient(wDevID
, MIM_LONGDATA
, (DWORD_PTR
)lpMidiHdr
, dwTime
);
571 LeaveCriticalSection(&crit_sect
);
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, %08X);\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, %08X);\n", wDevID
, lpMidiHdr
, dwSize
);
864 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 ||
865 lpMidiHdr
->lpData
== 0 || (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) != 0)
866 return MMSYSERR_INVALPARAM
;
868 lpMidiHdr
->lpNext
= 0;
869 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
870 lpMidiHdr
->dwBytesRecorded
= 0;
872 return MMSYSERR_NOERROR
;
875 /**************************************************************************
876 * midUnprepare [internal]
878 static DWORD
midUnprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
880 TRACE("(%04X, %p, %08X);\n", wDevID
, lpMidiHdr
, dwSize
);
882 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
883 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
885 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 ||
886 lpMidiHdr
->lpData
== 0)
887 return MMSYSERR_INVALPARAM
;
889 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) return MIDIERR_UNPREPARED
;
890 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) return MIDIERR_STILLPLAYING
;
892 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
894 return MMSYSERR_NOERROR
;
897 /**************************************************************************
898 * midReset [internal]
900 static DWORD
midReset(WORD wDevID
)
902 DWORD dwTime
= GetTickCount();
904 TRACE("(%04X);\n", wDevID
);
906 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
907 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
909 EnterCriticalSection(&crit_sect
);
910 while (MidiInDev
[wDevID
].lpQueueHdr
) {
911 MidiInDev
[wDevID
].lpQueueHdr
->dwFlags
&= ~MHDR_INQUEUE
;
912 MidiInDev
[wDevID
].lpQueueHdr
->dwFlags
|= MHDR_DONE
;
913 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
914 MIDI_NotifyClient(wDevID
, MIM_LONGDATA
,
915 (DWORD_PTR
)MidiInDev
[wDevID
].lpQueueHdr
, dwTime
);
916 MidiInDev
[wDevID
].lpQueueHdr
= MidiInDev
[wDevID
].lpQueueHdr
->lpNext
;
918 LeaveCriticalSection(&crit_sect
);
920 return MMSYSERR_NOERROR
;
924 /**************************************************************************
925 * midStart [internal]
927 static DWORD
midStart(WORD wDevID
)
929 TRACE("(%04X);\n", wDevID
);
931 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
932 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
934 MidiInDev
[wDevID
].state
= 1;
935 MidiInDev
[wDevID
].startTime
= GetTickCount();
936 return MMSYSERR_NOERROR
;
939 /**************************************************************************
942 static DWORD
midStop(WORD wDevID
)
944 TRACE("(%04X);\n", wDevID
);
946 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
947 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
949 MidiInDev
[wDevID
].state
= 0;
950 return MMSYSERR_NOERROR
;
953 /*-----------------------------------------------------------------------*/
955 typedef struct sVoice
{
956 int note
; /* 0 means not used */
958 unsigned cntMark
: 30,
961 #define sVS_PLAYING 1
962 #define sVS_SUSTAINED 2
965 typedef struct sChannel
{
971 int bank
; /* CTL_BANK_SELECT */
972 int volume
; /* CTL_MAIN_VOLUME */
973 int balance
; /* CTL_BALANCE */
974 int expression
; /* CTL_EXPRESSION */
975 int sustain
; /* CTL_SUSTAIN */
977 unsigned char nrgPmtMSB
; /* Non register Parameters */
978 unsigned char nrgPmtLSB
;
979 unsigned char regPmtMSB
; /* Non register Parameters */
980 unsigned char regPmtLSB
;
983 typedef struct sFMextra
{
986 sChannel channel
[16]; /* MIDI has only 16 channels */
987 sVoice voice
[1]; /* dyn allocated according to sound card */
988 /* do not append fields below voice[1] since the size of this structure
989 * depends on the number of available voices on the FM synth...
993 extern const unsigned char midiFMInstrumentPatches
[16 * 128];
994 extern const unsigned char midiFMDrumsPatches
[16 * 128];
996 /**************************************************************************
997 * modFMLoad [internal]
999 static int modFMLoad(int dev
)
1002 struct sbi_instrument sbi
;
1007 memset(sbi
.operators
+ 16, 0, 16);
1008 for (i
= 0; i
< 128; i
++) {
1010 memcpy(sbi
.operators
, midiFMInstrumentPatches
+ i
* 16, 16);
1012 if (write(midiSeqFD
, &sbi
, sizeof(sbi
)) == -1) {
1013 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi
.channel
, errno
, strerror(errno
));
1017 for (i
= 0; i
< 128; i
++) {
1018 sbi
.channel
= 128 + i
;
1019 memcpy(sbi
.operators
, midiFMDrumsPatches
+ i
* 16, 16);
1021 if (write(midiSeqFD
, &sbi
, sizeof(sbi
)) == -1) {
1022 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi
.channel
, errno
, strerror(errno
));
1029 /**************************************************************************
1030 * modFMReset [internal]
1032 static void modFMReset(WORD wDevID
)
1034 sFMextra
* extra
= MidiOutDev
[wDevID
].lpExtra
;
1035 sVoice
* voice
= extra
->voice
;
1036 sChannel
* channel
= extra
->channel
;
1039 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1040 if (voice
[i
].status
!= sVS_UNUSED
) {
1041 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1043 SEQ_KEY_PRESSURE(wDevID
, i
, 127, 0);
1044 SEQ_CONTROL(wDevID
, i
, SEQ_VOLMODE
, VOL_METHOD_LINEAR
);
1046 voice
[i
].channel
= -1;
1047 voice
[i
].cntMark
= 0;
1048 voice
[i
].status
= sVS_UNUSED
;
1050 for (i
= 0; i
< 16; i
++) {
1051 channel
[i
].program
= 0;
1052 channel
[i
].bender
= 8192;
1053 channel
[i
].benderRange
= 2;
1054 channel
[i
].bank
= 0;
1055 channel
[i
].volume
= 127;
1056 channel
[i
].balance
= 64;
1057 channel
[i
].expression
= 0;
1058 channel
[i
].sustain
= 0;
1061 extra
->drumSetMask
= 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
1065 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
1067 /**************************************************************************
1068 * modGetDevCaps [internal]
1070 static DWORD
modGetDevCaps(WORD wDevID
, LPMIDIOUTCAPSW lpCaps
, DWORD dwSize
)
1072 TRACE("(%04X, %p, %08X);\n", wDevID
, lpCaps
, dwSize
);
1074 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1075 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
1077 memcpy(lpCaps
, &MidiOutDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
1079 return MMSYSERR_NOERROR
;
1082 /**************************************************************************
1083 * modOpen [internal]
1085 static DWORD
modOpen(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
1087 TRACE("(%04X, %p, %08X);\n", wDevID
, lpDesc
, dwFlags
);
1088 if (lpDesc
== NULL
) {
1089 WARN("Invalid Parameter !\n");
1090 return MMSYSERR_INVALPARAM
;
1092 if (wDevID
>= MODM_NumDevs
) {
1093 TRACE("MAX_MIDIOUTDRV reached !\n");
1094 return MMSYSERR_BADDEVICEID
;
1096 if (MidiOutDev
[wDevID
].midiDesc
.hMidi
!= 0) {
1097 WARN("device already open !\n");
1098 return MMSYSERR_ALLOCATED
;
1100 if (!MidiOutDev
[wDevID
].bEnabled
) {
1101 WARN("device disabled !\n");
1102 return MIDIERR_NODEVICE
;
1104 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
1105 WARN("bad dwFlags\n");
1106 return MMSYSERR_INVALFLAG
;
1108 if (!MidiOutDev
[wDevID
].bEnabled
) {
1109 TRACE("disabled wDevID\n");
1110 return MMSYSERR_NOTENABLED
;
1113 MidiOutDev
[wDevID
].lpExtra
= 0;
1115 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1120 extra
= HeapAlloc(GetProcessHeap(), 0,
1121 sizeof(struct sFMextra
) +
1122 sizeof(struct sVoice
) * (MidiOutDev
[wDevID
].caps
.wVoices
- 1));
1125 WARN("can't alloc extra data !\n");
1126 return MMSYSERR_NOMEM
;
1128 MidiOutDev
[wDevID
].lpExtra
= extra
;
1129 if (midiOpenSeq() < 0) {
1130 MidiOutDev
[wDevID
].lpExtra
= 0;
1131 HeapFree(GetProcessHeap(), 0, extra
);
1132 return MMSYSERR_ERROR
;
1134 if (modFMLoad(wDevID
) < 0) {
1136 MidiOutDev
[wDevID
].lpExtra
= 0;
1137 HeapFree(GetProcessHeap(), 0, extra
);
1138 return MMSYSERR_ERROR
;
1145 if (midiOpenSeq() < 0) {
1146 return MMSYSERR_ALLOCATED
;
1150 WARN("Technology not supported (yet) %d !\n",
1151 MidiOutDev
[wDevID
].caps
.wTechnology
);
1152 return MMSYSERR_NOTENABLED
;
1155 MidiOutDev
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1157 MidiOutDev
[wDevID
].lpQueueHdr
= NULL
;
1158 MidiOutDev
[wDevID
].dwTotalPlayed
= 0;
1159 MidiOutDev
[wDevID
].bufsize
= 0x3FFF;
1160 MidiOutDev
[wDevID
].midiDesc
= *lpDesc
;
1162 MIDI_NotifyClient(wDevID
, MOM_OPEN
, 0L, 0L);
1163 TRACE("Successful !\n");
1164 return MMSYSERR_NOERROR
;
1168 /**************************************************************************
1169 * modClose [internal]
1171 static DWORD
modClose(WORD wDevID
)
1173 int ret
= MMSYSERR_NOERROR
;
1175 TRACE("(%04X);\n", wDevID
);
1177 if (MidiOutDev
[wDevID
].midiDesc
.hMidi
== 0) {
1178 WARN("device not opened !\n");
1179 return MMSYSERR_ERROR
;
1181 /* FIXME: should test that no pending buffer is still in the queue for
1184 if (midiSeqFD
== -1) {
1185 WARN("can't close !\n");
1186 return MMSYSERR_ERROR
;
1189 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1195 WARN("Technology not supported (yet) %d !\n",
1196 MidiOutDev
[wDevID
].caps
.wTechnology
);
1197 return MMSYSERR_NOTENABLED
;
1200 HeapFree(GetProcessHeap(), 0, MidiOutDev
[wDevID
].lpExtra
);
1201 MidiOutDev
[wDevID
].lpExtra
= 0;
1203 MidiOutDev
[wDevID
].bufsize
= 0;
1204 MIDI_NotifyClient(wDevID
, MOM_CLOSE
, 0L, 0L);
1205 MidiOutDev
[wDevID
].midiDesc
.hMidi
= 0;
1209 /**************************************************************************
1210 * modData [internal]
1212 static DWORD
modData(WORD wDevID
, DWORD dwParam
)
1214 WORD evt
= LOBYTE(LOWORD(dwParam
));
1215 WORD d1
= HIBYTE(LOWORD(dwParam
));
1216 WORD d2
= LOBYTE(HIWORD(dwParam
));
1218 TRACE("(%04X, %08X);\n", wDevID
, dwParam
);
1220 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1221 if (!MidiOutDev
[wDevID
].bEnabled
) return MIDIERR_NODEVICE
;
1223 if (midiSeqFD
== -1) {
1224 WARN("can't play !\n");
1225 return MIDIERR_NODEVICE
;
1227 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1230 * - chorus depth controller is not used
1233 sFMextra
* extra
= MidiOutDev
[wDevID
].lpExtra
;
1234 sVoice
* voice
= extra
->voice
;
1235 sChannel
* channel
= extra
->channel
;
1236 int chn
= (evt
& 0x0F);
1239 switch (evt
& 0xF0) {
1241 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1242 /* don't stop sustained notes */
1243 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1244 voice
[i
].status
= sVS_UNUSED
;
1245 SEQ_STOP_NOTE(wDevID
, i
, d1
, d2
);
1250 if (d2
== 0) { /* note off if velocity == 0 */
1251 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1252 /* don't stop sustained notes */
1253 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1254 voice
[i
].status
= sVS_UNUSED
;
1255 SEQ_STOP_NOTE(wDevID
, i
, d1
, 64);
1260 /* finding out in this order :
1262 * - if replaying the same note on the same channel
1263 * - the older voice (LRU)
1265 for (i
= nv
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1266 if (voice
[i
].status
== sVS_UNUSED
||
1267 (voice
[i
].note
== d1
&& voice
[i
].channel
== chn
)) {
1271 if (voice
[i
].cntMark
< voice
[0].cntMark
) {
1276 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1277 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1278 nv
, channel
[chn
].program
,
1279 channel
[chn
].balance
,
1280 channel
[chn
].volume
,
1281 channel
[chn
].bender
, d1
, d2
);
1283 SEQ_SET_PATCH(wDevID
, nv
, IS_DRUM_CHANNEL(extra
, chn
) ?
1284 (128 + d1
) : channel
[chn
].program
);
1285 SEQ_BENDER_RANGE(wDevID
, nv
, channel
[chn
].benderRange
* 100);
1286 SEQ_BENDER(wDevID
, nv
, channel
[chn
].bender
);
1287 SEQ_CONTROL(wDevID
, nv
, CTL_PAN
, channel
[chn
].balance
);
1288 SEQ_CONTROL(wDevID
, nv
, CTL_EXPRESSION
, channel
[chn
].expression
);
1290 /* FIXME: does not really seem to work on my SB card and
1291 * screws everything up... so lay it down
1293 SEQ_CONTROL(wDevID
, nv
, CTL_MAIN_VOLUME
, channel
[chn
].volume
);
1295 SEQ_START_NOTE(wDevID
, nv
, d1
, d2
);
1296 voice
[nv
].status
= channel
[chn
].sustain
? sVS_SUSTAINED
: sVS_PLAYING
;
1297 voice
[nv
].note
= d1
;
1298 voice
[nv
].channel
= chn
;
1299 voice
[nv
].cntMark
= extra
->counter
++;
1301 case MIDI_KEY_PRESSURE
:
1302 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1303 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1304 SEQ_KEY_PRESSURE(wDevID
, i
, d1
, d2
);
1308 case MIDI_CTL_CHANGE
:
1310 case CTL_BANK_SELECT
: channel
[chn
].bank
= d2
; break;
1311 case CTL_MAIN_VOLUME
: channel
[chn
].volume
= d2
; break;
1312 case CTL_PAN
: channel
[chn
].balance
= d2
; break;
1313 case CTL_EXPRESSION
: channel
[chn
].expression
= d2
; break;
1314 case CTL_SUSTAIN
: channel
[chn
].sustain
= d2
;
1316 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1317 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
) {
1318 voice
[i
].status
= sVS_SUSTAINED
;
1322 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1323 if (voice
[i
].status
== sVS_SUSTAINED
&& voice
[i
].channel
== chn
) {
1324 voice
[i
].status
= sVS_UNUSED
;
1325 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1330 case CTL_NONREG_PARM_NUM_LSB
: channel
[chn
].nrgPmtLSB
= d2
; break;
1331 case CTL_NONREG_PARM_NUM_MSB
: channel
[chn
].nrgPmtMSB
= d2
; break;
1332 case CTL_REGIST_PARM_NUM_LSB
: channel
[chn
].regPmtLSB
= d2
; break;
1333 case CTL_REGIST_PARM_NUM_MSB
: channel
[chn
].regPmtMSB
= d2
; break;
1334 case CTL_DATA_ENTRY
:
1335 switch ((channel
[chn
].regPmtMSB
<< 8) | channel
[chn
].regPmtLSB
) {
1337 if (channel
[chn
].benderRange
!= d2
) {
1338 channel
[chn
].benderRange
= d2
;
1339 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1340 if (voice
[i
].channel
== chn
) {
1341 SEQ_BENDER_RANGE(wDevID
, i
, channel
[chn
].benderRange
);
1348 channel
[chn
].benderRange
= 2;
1349 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1350 if (voice
[i
].channel
== chn
) {
1351 SEQ_BENDER_RANGE(wDevID
, i
, channel
[chn
].benderRange
);
1356 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1357 channel
[chn
].regPmtMSB
, channel
[chn
].regPmtLSB
,
1358 channel
[chn
].nrgPmtMSB
, channel
[chn
].nrgPmtLSB
,
1364 case 0x78: /* all sounds off */
1365 /* FIXME: I don't know if I have to take care of the channel
1366 * for this control ?
1368 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1369 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
) {
1370 voice
[i
].status
= sVS_UNUSED
;
1371 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1375 case 0x7B: /* all notes off */
1376 /* FIXME: I don't know if I have to take care of the channel
1377 * for this control ?
1379 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1380 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
) {
1381 voice
[i
].status
= sVS_UNUSED
;
1382 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1387 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1392 case MIDI_PGM_CHANGE
:
1393 channel
[chn
].program
= d1
;
1395 case MIDI_CHN_PRESSURE
:
1396 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1397 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
) {
1398 SEQ_KEY_PRESSURE(wDevID
, i
, voice
[i
].note
, d1
);
1402 case MIDI_PITCH_BEND
:
1403 channel
[chn
].bender
= (d2
<< 7) + d1
;
1404 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1405 if (voice
[i
].channel
== chn
) {
1406 SEQ_BENDER(wDevID
, i
, channel
[chn
].bender
);
1410 case MIDI_SYSTEM_PREFIX
:
1411 switch (evt
& 0x0F) {
1412 case 0x0F: /* Reset */
1416 WARN("Unsupported (yet) system event %02x\n", evt
& 0x0F);
1420 WARN("Internal error, shouldn't happen (event=%08x)\n", evt
& 0xF0);
1421 return MMSYSERR_NOTENABLED
;
1427 int dev
= wDevID
- MODM_NumFMSynthDevs
;
1429 WARN("Internal error on devID (%u) !\n", wDevID
);
1430 return MIDIERR_NODEVICE
;
1433 switch (evt
& 0xF0) {
1436 case MIDI_KEY_PRESSURE
:
1437 case MIDI_CTL_CHANGE
:
1438 case MIDI_PITCH_BEND
:
1439 SEQ_MIDIOUT(dev
, evt
);
1440 SEQ_MIDIOUT(dev
, d1
);
1441 SEQ_MIDIOUT(dev
, d2
);
1443 case MIDI_PGM_CHANGE
:
1444 case MIDI_CHN_PRESSURE
:
1445 SEQ_MIDIOUT(dev
, evt
);
1446 SEQ_MIDIOUT(dev
, d1
);
1448 case MIDI_SYSTEM_PREFIX
:
1449 switch (evt
& 0x0F) {
1450 case 0x00: /* System Exclusive, don't do it on modData,
1451 * should require modLongData*/
1452 case 0x01: /* Undefined */
1453 case 0x04: /* Undefined. */
1454 case 0x05: /* Undefined. */
1455 case 0x07: /* End of Exclusive. */
1456 case 0x09: /* Undefined. */
1457 case 0x0D: /* Undefined. */
1459 case 0x06: /* Tune Request */
1460 case 0x08: /* Timing Clock. */
1461 case 0x0A: /* Start. */
1462 case 0x0B: /* Continue */
1463 case 0x0C: /* Stop */
1464 case 0x0E: /* Active Sensing. */
1465 SEQ_MIDIOUT(dev
, evt
);
1467 case 0x0F: /* Reset */
1468 /* SEQ_MIDIOUT(dev, evt);
1469 this other way may be better */
1470 SEQ_MIDIOUT(dev
, MIDI_SYSTEM_PREFIX
);
1471 SEQ_MIDIOUT(dev
, 0x7e);
1472 SEQ_MIDIOUT(dev
, 0x7f);
1473 SEQ_MIDIOUT(dev
, 0x09);
1474 SEQ_MIDIOUT(dev
, 0x01);
1475 SEQ_MIDIOUT(dev
, 0xf7);
1477 case 0x03: /* Song Select. */
1478 SEQ_MIDIOUT(dev
, evt
);
1479 SEQ_MIDIOUT(dev
, d1
);
1480 case 0x02: /* Song Position Pointer. */
1481 SEQ_MIDIOUT(dev
, evt
);
1482 SEQ_MIDIOUT(dev
, d1
);
1483 SEQ_MIDIOUT(dev
, d2
);
1490 WARN("Technology not supported (yet) %d !\n",
1491 MidiOutDev
[wDevID
].caps
.wTechnology
);
1492 return MMSYSERR_NOTENABLED
;
1497 return MMSYSERR_NOERROR
;
1500 /**************************************************************************
1501 * modLongData [internal]
1503 static DWORD
modLongData(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1508 TRACE("(%04X, %p, %08X);\n", wDevID
, lpMidiHdr
, dwSize
);
1510 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
1511 * but it seems to be used only for midi input.
1512 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
1515 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1516 if (!MidiOutDev
[wDevID
].bEnabled
) return MIDIERR_NODEVICE
;
1518 if (midiSeqFD
== -1) {
1519 WARN("can't play !\n");
1520 return MIDIERR_NODEVICE
;
1523 lpData
= (LPBYTE
) lpMidiHdr
->lpData
;
1526 return MIDIERR_UNPREPARED
;
1527 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
1528 return MIDIERR_UNPREPARED
;
1529 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
1530 return MIDIERR_STILLPLAYING
;
1531 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
1532 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
1534 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1535 * data, or can it also contain raw MIDI data, to be split up and sent to
1537 * If the latest is true, then the following WARNing will fire up
1539 if (lpData
[0] != 0xF0 || lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
1540 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1543 TRACE("dwBufferLength=%u !\n", lpMidiHdr
->dwBufferLength
);
1544 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1545 lpData
[0], lpData
[1], lpData
[2], lpData
[lpMidiHdr
->dwBufferLength
-3],
1546 lpData
[lpMidiHdr
->dwBufferLength
-2], lpData
[lpMidiHdr
->dwBufferLength
-1]);
1548 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1550 /* FIXME: I don't think there is much to do here */
1553 if (lpData
[0] != 0xF0) {
1554 /* Send end of System Exclusive */
1555 SEQ_MIDIOUT(wDevID
- MODM_NumFMSynthDevs
, 0xF0);
1556 WARN("Adding missing 0xF0 marker at the beginning of "
1557 "system exclusive byte stream\n");
1559 for (count
= 0; count
< lpMidiHdr
->dwBufferLength
; count
++) {
1560 SEQ_MIDIOUT(wDevID
- MODM_NumFMSynthDevs
, lpData
[count
]);
1562 if (lpData
[count
- 1] != 0xF7) {
1563 /* Send end of System Exclusive */
1564 SEQ_MIDIOUT(wDevID
- MODM_NumFMSynthDevs
, 0xF7);
1565 WARN("Adding missing 0xF7 marker at the end of "
1566 "system exclusive byte stream\n");
1571 WARN("Technology not supported (yet) %d !\n",
1572 MidiOutDev
[wDevID
].caps
.wTechnology
);
1573 return MMSYSERR_NOTENABLED
;
1576 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
1577 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
1578 MIDI_NotifyClient(wDevID
, MOM_DONE
, (DWORD_PTR
)lpMidiHdr
, 0L);
1579 return MMSYSERR_NOERROR
;
1582 /**************************************************************************
1583 * modPrepare [internal]
1585 static DWORD
modPrepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1587 TRACE("(%04X, %p, %08X);\n", wDevID
, lpMidiHdr
, dwSize
);
1589 if (midiSeqFD
== -1) {
1590 WARN("can't prepare !\n");
1591 return MMSYSERR_NOTENABLED
;
1594 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
1595 * asks to prepare MIDIHDR which dwFlags != 0.
1596 * So at least check for the inqueue flag
1598 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0 ||
1599 lpMidiHdr
->lpData
== 0 || (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) != 0) {
1600 WARN("%p %p %08x %d\n", lpMidiHdr
, lpMidiHdr
? lpMidiHdr
->lpData
: NULL
,
1601 lpMidiHdr
? lpMidiHdr
->dwFlags
: 0, dwSize
);
1602 return MMSYSERR_INVALPARAM
;
1605 lpMidiHdr
->lpNext
= 0;
1606 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
1607 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
1608 return MMSYSERR_NOERROR
;
1611 /**************************************************************************
1612 * modUnprepare [internal]
1614 static DWORD
modUnprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1616 TRACE("(%04X, %p, %08X);\n", wDevID
, lpMidiHdr
, dwSize
);
1618 if (midiSeqFD
== -1) {
1619 WARN("can't unprepare !\n");
1620 return MMSYSERR_NOTENABLED
;
1623 if (dwSize
< offsetof(MIDIHDR
,dwOffset
) || lpMidiHdr
== 0)
1624 return MMSYSERR_INVALPARAM
;
1625 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
1626 return MIDIERR_STILLPLAYING
;
1627 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
1628 return MMSYSERR_NOERROR
;
1631 /**************************************************************************
1632 * modGetVolume [internal]
1634 static DWORD
modGetVolume(WORD wDevID
, DWORD
* lpdwVolume
)
1636 if (!lpdwVolume
) return MMSYSERR_INVALPARAM
;
1637 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1638 *lpdwVolume
= 0xFFFFFFFF;
1639 return (MidiOutDev
[wDevID
].caps
.dwSupport
& MIDICAPS_VOLUME
) ? 0 : MMSYSERR_NOTSUPPORTED
;
1642 /**************************************************************************
1643 * modReset [internal]
1645 static DWORD
modReset(WORD wDevID
)
1649 TRACE("(%04X);\n", wDevID
);
1651 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1652 if (!MidiOutDev
[wDevID
].bEnabled
) return MIDIERR_NODEVICE
;
1654 /* stop all notes */
1655 /* FIXME: check if 0x78B0 is channel dependent or not. I coded it so that
1656 * it's channel dependent...
1658 for (chn
= 0; chn
< 16; chn
++) {
1659 /* turn off every note */
1660 modData(wDevID
, 0x7800 | MIDI_CTL_CHANGE
| chn
);
1661 /* remove sustain on all channels */
1662 modData(wDevID
, (CTL_SUSTAIN
<< 8) | MIDI_CTL_CHANGE
| chn
);
1664 /* FIXME: the LongData buffers must also be returned to the app */
1665 return MMSYSERR_NOERROR
;
1668 #endif /* HAVE_OSS_MIDI */
1670 /*======================================================================*
1671 * MIDI entry points *
1672 *======================================================================*/
1674 /**************************************************************************
1675 * midMessage (WINEOSS.@)
1677 DWORD WINAPI
OSS_midMessage(UINT wDevID
, UINT wMsg
, DWORD_PTR dwUser
,
1678 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
1680 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1681 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1683 #ifdef HAVE_OSS_MIDI
1685 return OSS_MidiInit();
1687 return OSS_MidiExit();
1690 /* FIXME: Pretend this is supported */
1693 return midOpen(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1695 return midClose(wDevID
);
1696 case MIDM_ADDBUFFER
:
1697 return midAddBuffer(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1699 return midPrepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1700 case MIDM_UNPREPARE
:
1701 return midUnprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1702 case MIDM_GETDEVCAPS
:
1703 return midGetDevCaps(wDevID
, (LPMIDIINCAPSW
)dwParam1
,dwParam2
);
1704 case MIDM_GETNUMDEVS
:
1705 return MIDM_NumDevs
;
1707 return midReset(wDevID
);
1709 return midStart(wDevID
);
1711 return midStop(wDevID
);
1714 case MIDM_GETNUMDEVS
:
1718 TRACE("Unsupported message\n");
1720 return MMSYSERR_NOTSUPPORTED
;
1723 /**************************************************************************
1724 * modMessage (WINEOSS.@)
1726 DWORD WINAPI
OSS_modMessage(UINT wDevID
, UINT wMsg
, DWORD_PTR dwUser
,
1727 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
1729 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1730 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1733 #ifdef HAVE_OSS_MIDI
1735 return OSS_MidiInit();
1737 return OSS_MidiExit();
1740 /* FIXME: Pretend this is supported */
1743 return modOpen(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1745 return modClose(wDevID
);
1747 return modData(wDevID
, dwParam1
);
1749 return modLongData(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1751 return modPrepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1752 case MODM_UNPREPARE
:
1753 return modUnprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1754 case MODM_GETDEVCAPS
:
1755 return modGetDevCaps(wDevID
, (LPMIDIOUTCAPSW
)dwParam1
, dwParam2
);
1756 case MODM_GETNUMDEVS
:
1757 return MODM_NumDevs
;
1758 case MODM_GETVOLUME
:
1759 return modGetVolume(wDevID
, (DWORD
*)dwParam1
);
1760 case MODM_SETVOLUME
:
1763 return modReset(wDevID
);
1766 case MODM_GETNUMDEVS
:
1770 TRACE("Unsupported message\n");
1772 return MMSYSERR_NOTSUPPORTED
;
1775 /**************************************************************************
1776 * DriverProc (WINEOSS.1)
1778 LRESULT CALLBACK
OSS_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1779 LPARAM dwParam1
, LPARAM dwParam2
)
1781 TRACE("(%08lX, %p, %08X, %08lX, %08lX)\n",
1782 dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1791 case DRV_QUERYCONFIGURE
: