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