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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
53 #ifdef HAVE_SYS_IOCTL_H
54 # include <sys/ioctl.h>
59 #ifdef HAVE_SYS_POLL_H
70 #include "wine/unicode.h"
71 #include "wine/debug.h"
73 WINE_DEFAULT_DEBUG_CHANNEL(midi
);
75 #ifdef SNDCTL_SEQ_NRMIDIS
81 #define MIDI_SEQ "/dev/sequencer"
84 int state
; /* -1 disabled, 0 is no recording started, 1 in recording, bit 2 set if in sys exclusive recording */
86 MIDIOPENDESC midiDesc
;
90 unsigned char incoming
[3];
91 unsigned char incPrev
;
100 MIDIOPENDESC midiDesc
;
102 LPMIDIHDR lpQueueHdr
;
104 void* lpExtra
; /* according to port type (MIDI, FM...), extra data when needed */
108 static WINE_MIDIIN MidiInDev
[MAX_MIDIINDRV
];
109 static WINE_MIDIOUT MidiOutDev
[MAX_MIDIOUTDRV
];
111 /* this is the total number of MIDI out devices found (synth and port) */
112 static int MODM_NumDevs
= 0;
113 /* this is the number of FM synthetizers (index from 0 to NUMFMSYNTHDEVS - 1) */
114 static int MODM_NumFMSynthDevs
= 0;
115 /* the Midi ports have index from NUMFMSYNTHDEVS to NumDevs - 1 */
117 /* this is the total number of MIDI out devices found */
118 static int MIDM_NumDevs
= 0;
120 static int midiSeqFD
= -1;
121 static int numOpenMidiSeq
= 0;
122 static int numStartedMidiIn
= 0;
124 static CRITICAL_SECTION crit_sect
; /* protects all MidiIn buffers queues */
125 static CRITICAL_SECTION_DEBUG critsect_debug
=
128 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
129 0, 0, { (DWORD_PTR
)(__FILE__
": crit_sect") }
131 static CRITICAL_SECTION crit_sect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
133 static int end_thread
;
134 static HANDLE hThread
;
136 /*======================================================================*
137 * Low level MIDI implementation *
138 *======================================================================*/
140 static int midiOpenSeq(void);
141 static int midiCloseSeq(void);
143 /**************************************************************************
144 * MIDI_unixToWindowsDeviceType [internal]
146 * return the Windows equivalent to a Unix Device Type
149 static int MIDI_UnixToWindowsDeviceType(int type
)
151 /* MOD_MIDIPORT output port
152 * MOD_SYNTH generic internal synth
153 * MOD_SQSYNTH square wave internal synth
154 * MOD_FMSYNTH FM internal synth
155 * MOD_MAPPER MIDI mapper
156 * MOD_WAVETABLE hardware watetable internal synth
157 * MOD_SWSYNTH software internal synth
160 /* FIXME Is this really the correct equivalence from UNIX to
161 Windows Sound type */
164 case SYNTH_TYPE_FM
: return MOD_FMSYNTH
;
165 case SYNTH_TYPE_SAMPLE
: return MOD_SYNTH
;
166 case SYNTH_TYPE_MIDI
: return MOD_MIDIPORT
;
168 ERR("Cannot determine the type of this midi device. "
169 "Assuming FM Synth\n");
174 /**************************************************************************
175 * OSS_MidiInit [internal]
177 * Initializes the MIDI devices information variables
179 LRESULT
OSS_MidiInit(void)
181 int i
, status
, numsynthdevs
= 255, nummididevs
= 255;
182 struct synth_info sinfo
;
183 struct midi_info minfo
;
184 static BOOL bInitDone
= FALSE
;
189 TRACE("Initializing the MIDI variables.\n");
192 /* try to open device */
193 if (midiOpenSeq() == -1) {
197 /* find how many Synth devices are there in the system */
198 status
= ioctl(midiSeqFD
, SNDCTL_SEQ_NRSYNTHS
, &numsynthdevs
);
201 ERR("ioctl for nr synth failed.\n");
206 if (numsynthdevs
> MAX_MIDIOUTDRV
) {
207 ERR("MAX_MIDIOUTDRV (%d) was enough for the number of devices (%d). "
208 "Some FM devices will not be available.\n",MAX_MIDIOUTDRV
,numsynthdevs
);
209 numsynthdevs
= MAX_MIDIOUTDRV
;
212 for (i
= 0; i
< numsynthdevs
; i
++) {
213 /* Manufac ID. We do not have access to this with soundcard.h
214 * Does not seem to be a problem, because in mmsystem.h only
215 * Microsoft's ID is listed.
217 MidiOutDev
[i
].caps
.wMid
= 0x00FF;
218 MidiOutDev
[i
].caps
.wPid
= 0x0001; /* FIXME Product ID */
219 /* Product Version. We simply say "1" */
220 MidiOutDev
[i
].caps
.vDriverVersion
= 0x001;
221 MidiOutDev
[i
].caps
.wChannelMask
= 0xFFFF;
223 /* FIXME Do we have this information?
224 * Assuming the soundcards can handle
225 * MIDICAPS_VOLUME and MIDICAPS_LRVOLUME but
226 * not MIDICAPS_CACHE.
228 MidiOutDev
[i
].caps
.dwSupport
= MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
;
231 status
= ioctl(midiSeqFD
, SNDCTL_SYNTH_INFO
, &sinfo
);
233 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};
234 ERR("ioctl for synth info failed on %d, disabling it.\n", i
);
236 wsprintfW(MidiOutDev
[i
].caps
.szPname
, fmt
, i
);
238 MidiOutDev
[i
].caps
.wTechnology
= MOD_MIDIPORT
;
239 MidiOutDev
[i
].caps
.wVoices
= 16;
240 MidiOutDev
[i
].caps
.wNotes
= 16;
241 MidiOutDev
[i
].bEnabled
= FALSE
;
243 MultiByteToWideChar( CP_ACP
, 0, sinfo
.name
, -1,
244 MidiOutDev
[i
].caps
.szPname
,
245 sizeof(MidiOutDev
[i
].caps
.szPname
)/sizeof(WCHAR
) );
247 MidiOutDev
[i
].caps
.wTechnology
= MIDI_UnixToWindowsDeviceType(sinfo
.synth_type
);
248 MidiOutDev
[i
].caps
.wVoices
= sinfo
.nr_voices
;
250 /* FIXME Is it possible to know the maximum
251 * number of simultaneous notes of a soundcard ?
252 * I believe we don't have this information, but
253 * it's probably equal or more than wVoices
255 MidiOutDev
[i
].caps
.wNotes
= sinfo
.nr_voices
;
256 MidiOutDev
[i
].bEnabled
= TRUE
;
259 /* We also have the information sinfo.synth_subtype, not used here
262 if (sinfo
.capabilities
& SYNTH_CAP_INPUT
) {
263 FIXME("Synthesizer support MIDI in. Not supported yet (please report)\n");
266 TRACE("SynthOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%ld\n"
267 "\tOSS info: synth subtype=%d capa=%lx\n",
268 i
, wine_dbgstr_w(MidiOutDev
[i
].caps
.szPname
),
269 MidiOutDev
[i
].caps
.wTechnology
,
270 MidiOutDev
[i
].caps
.wVoices
, MidiOutDev
[i
].caps
.wNotes
,
271 MidiOutDev
[i
].caps
.wChannelMask
, MidiOutDev
[i
].caps
.dwSupport
,
272 sinfo
.synth_subtype
, (long)sinfo
.capabilities
);
275 /* find how many MIDI devices are there in the system */
276 status
= ioctl(midiSeqFD
, SNDCTL_SEQ_NRMIDIS
, &nummididevs
);
278 ERR("ioctl on nr midi failed.\n");
283 /* FIXME: the two restrictions below could be loosened in some cases */
284 if (numsynthdevs
+ nummididevs
> MAX_MIDIOUTDRV
) {
285 ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
286 "Some MIDI devices will not be available.\n");
287 nummididevs
= MAX_MIDIOUTDRV
- numsynthdevs
;
290 if (nummididevs
> MAX_MIDIINDRV
) {
291 ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
292 "Some MIDI devices will not be available.\n",MAX_MIDIINDRV
,nummididevs
);
293 nummididevs
= MAX_MIDIINDRV
;
296 for (i
= 0; i
< nummididevs
; i
++) {
298 status
= ioctl(midiSeqFD
, SNDCTL_MIDI_INFO
, &minfo
);
299 if (status
== -1) WARN("ioctl on midi info for device %d failed.\n", i
);
301 /* This whole part is somewhat obscure to me. I'll keep trying to dig
302 info about it. If you happen to know, please tell us. The very
303 descritive minfo.dev_type was not used here.
305 /* Manufac ID. We do not have access to this with soundcard.h
306 Does not seem to be a problem, because in mmsystem.h only
307 Microsoft's ID is listed */
308 MidiOutDev
[numsynthdevs
+ i
].caps
.wMid
= 0x00FF;
309 MidiOutDev
[numsynthdevs
+ i
].caps
.wPid
= 0x0001; /* FIXME Product ID */
310 /* Product Version. We simply say "1" */
311 MidiOutDev
[numsynthdevs
+ i
].caps
.vDriverVersion
= 0x001;
313 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};
314 wsprintfW(MidiOutDev
[numsynthdevs
+ i
].caps
.szPname
, fmt
, numsynthdevs
+ i
);
315 MidiOutDev
[numsynthdevs
+ i
].bEnabled
= FALSE
;
317 MultiByteToWideChar(CP_ACP
, 0, minfo
.name
, -1,
318 MidiOutDev
[numsynthdevs
+ i
].caps
.szPname
,
319 sizeof(MidiOutDev
[numsynthdevs
+ i
].caps
.szPname
) / sizeof(WCHAR
));
320 MidiOutDev
[numsynthdevs
+ i
].bEnabled
= TRUE
;
322 MidiOutDev
[numsynthdevs
+ i
].caps
.wTechnology
= MOD_MIDIPORT
; /* FIXME Is this right? */
323 /* Does it make any difference? */
324 MidiOutDev
[numsynthdevs
+ i
].caps
.wVoices
= 16;
325 /* Does it make any difference? */
326 MidiOutDev
[numsynthdevs
+ i
].caps
.wNotes
= 16;
327 MidiOutDev
[numsynthdevs
+ i
].caps
.wChannelMask
= 0xFFFF;
329 /* FIXME Does it make any difference? */
330 MidiOutDev
[numsynthdevs
+ i
].caps
.dwSupport
= MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
;
332 /* This whole part is somewhat obscure to me. I'll keep trying to dig
333 info about it. If you happen to know, please tell us. The very
334 descritive minfo.dev_type was not used here.
336 /* Manufac ID. We do not have access to this with soundcard.h
337 Does not seem to be a problem, because in mmsystem.h only
338 Microsoft's ID is listed */
339 MidiInDev
[i
].caps
.wMid
= 0x00FF;
340 MidiInDev
[i
].caps
.wPid
= 0x0001; /* FIXME Product ID */
341 /* Product Version. We simply say "1" */
342 MidiInDev
[i
].caps
.vDriverVersion
= 0x001;
344 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};
345 wsprintfW(MidiInDev
[i
].caps
.szPname
, fmt
, numsynthdevs
+ i
);
346 MidiInDev
[i
].state
= -1;
348 MultiByteToWideChar(CP_ACP
, 0, minfo
.name
, -1,
349 MidiInDev
[i
].caps
.szPname
,
350 sizeof(MidiInDev
[i
].caps
.szPname
) / sizeof(WCHAR
));
351 MidiInDev
[i
].state
= 0;
353 /* FIXME : could we get better information than that ? */
354 MidiInDev
[i
].caps
.dwSupport
= MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
;
356 TRACE("MidiOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%ld\n"
357 "MidiIn [%d]\tname='%s' support=%ld\n"
358 "\tOSS info: midi dev-type=%d, capa=%lx\n",
359 i
, wine_dbgstr_w(MidiOutDev
[numsynthdevs
+ i
].caps
.szPname
),
360 MidiOutDev
[numsynthdevs
+ i
].caps
.wTechnology
,
361 MidiOutDev
[numsynthdevs
+ i
].caps
.wVoices
, MidiOutDev
[numsynthdevs
+ i
].caps
.wNotes
,
362 MidiOutDev
[numsynthdevs
+ i
].caps
.wChannelMask
, MidiOutDev
[numsynthdevs
+ i
].caps
.dwSupport
,
363 i
, wine_dbgstr_w(MidiInDev
[i
].caps
.szPname
), MidiInDev
[i
].caps
.dwSupport
,
364 minfo
.dev_type
, (long)minfo
.capabilities
);
368 /* windows does not seem to differentiate Synth from MIDI devices */
369 MODM_NumFMSynthDevs
= numsynthdevs
;
370 MODM_NumDevs
= numsynthdevs
+ nummididevs
;
372 MIDM_NumDevs
= nummididevs
;
374 /* close file and exit */
380 /**************************************************************************
381 * OSS_MidiExit [internal]
383 * Release the MIDI devices information variables
385 LRESULT
OSS_MidiExit(void)
389 ZeroMemory(MidiInDev
, sizeof(MidiInDev
));
390 ZeroMemory(MidiOutDev
, sizeof(MidiOutDev
));
393 MODM_NumFMSynthDevs
= 0;
399 /**************************************************************************
400 * MIDI_NotifyClient [internal]
402 static DWORD
MIDI_NotifyClient(UINT wDevID
, WORD wMsg
,
403 DWORD dwParam1
, DWORD dwParam2
)
410 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",
411 wDevID
, wMsg
, dwParam1
, dwParam2
);
418 if (wDevID
> MODM_NumDevs
)
419 return MMSYSERR_BADDEVICEID
;
421 dwCallBack
= MidiOutDev
[wDevID
].midiDesc
.dwCallback
;
422 uFlags
= MidiOutDev
[wDevID
].wFlags
;
423 hDev
= MidiOutDev
[wDevID
].midiDesc
.hMidi
;
424 dwInstance
= MidiOutDev
[wDevID
].midiDesc
.dwInstance
;
434 if (wDevID
> MIDM_NumDevs
)
435 return MMSYSERR_BADDEVICEID
;
437 dwCallBack
= MidiInDev
[wDevID
].midiDesc
.dwCallback
;
438 uFlags
= MidiInDev
[wDevID
].wFlags
;
439 hDev
= MidiInDev
[wDevID
].midiDesc
.hMidi
;
440 dwInstance
= MidiInDev
[wDevID
].midiDesc
.dwInstance
;
443 WARN("Unsupported MSW-MIDI message %u\n", wMsg
);
444 return MMSYSERR_ERROR
;
447 return DriverCallback(dwCallBack
, uFlags
, hDev
, wMsg
, dwInstance
, dwParam1
, dwParam2
) ?
451 static int midi_warn
= 1;
452 /**************************************************************************
453 * midiOpenSeq [internal]
455 static int midiOpenSeq(void)
457 if (numOpenMidiSeq
== 0) {
458 midiSeqFD
= open(MIDI_SEQ
, O_RDWR
, 0);
459 if (midiSeqFD
== -1) {
462 WARN("Can't open MIDI device '%s' ! (%s). If your "
463 "program needs this (probably not): %s\n",
464 MIDI_SEQ
, strerror(errno
),
466 "create it ! (\"man MAKEDEV\" ?)" :
468 "load MIDI sequencer kernel driver !" :
470 "grant access ! (\"man chmod\")" : ""
477 if (fcntl(midiSeqFD
, F_SETFL
, O_NONBLOCK
) < 0) {
478 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno
, strerror(errno
));
484 fcntl(midiSeqFD
, F_SETFD
, 1); /* set close on exec flag */
485 ioctl(midiSeqFD
, SNDCTL_SEQ_RESET
);
491 /**************************************************************************
492 * midiCloseSeq [internal]
494 static int midiCloseSeq(void)
496 if (--numOpenMidiSeq
== 0) {
503 /* FIXME: this is a bad idea, it's even not static... */
506 /* FIXME: this is not reentrant, not static - because of global variable
509 /**************************************************************************
510 * seqbuf_dump [internal]
512 * Used by SEQ_DUMPBUF to flush the buffer.
515 void seqbuf_dump(void)
518 if (write(midiSeqFD
, _seqbuf
, _seqbufptr
) == -1) {
519 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
520 midiSeqFD
, errno
, strerror(errno
));
523 * in any case buffer is lost so that if many errors occur the buffer
530 /**************************************************************************
531 * midReceiveChar [internal]
533 static void midReceiveChar(WORD wDevID
, unsigned char value
, DWORD dwTime
)
537 TRACE("Adding %02xh to %d[%d]\n", value
, wDevID
, MidiInDev
[wDevID
].incLen
);
539 if (wDevID
>= MIDM_NumDevs
) {
543 if (MidiInDev
[wDevID
].state
<= 0) {
544 TRACE("disabled or input not started, thrown away\n");
548 if (MidiInDev
[wDevID
].state
& 2) { /* system exclusive */
552 EnterCriticalSection(&crit_sect
);
553 if ((lpMidiHdr
= MidiInDev
[wDevID
].lpQueueHdr
) != NULL
) {
554 LPBYTE lpData
= (LPBYTE
) lpMidiHdr
->lpData
;
556 lpData
[lpMidiHdr
->dwBytesRecorded
++] = value
;
557 if (lpMidiHdr
->dwBytesRecorded
== lpMidiHdr
->dwBufferLength
) {
561 if (value
== 0xF7) { /* then end */
562 MidiInDev
[wDevID
].state
&= ~2;
565 if (sbfb
&& lpMidiHdr
!= NULL
) {
566 lpMidiHdr
= MidiInDev
[wDevID
].lpQueueHdr
;
567 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
568 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
569 MidiInDev
[wDevID
].lpQueueHdr
= (LPMIDIHDR
)lpMidiHdr
->lpNext
;
570 if (MIDI_NotifyClient(wDevID
, MIM_LONGDATA
, (DWORD
)lpMidiHdr
, dwTime
) != MMSYSERR_NOERROR
) {
571 WARN("Couldn't notify client\n");
574 LeaveCriticalSection(&crit_sect
);
578 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
579 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
581 if (!IS_CMD(value
) && MidiInDev
[wDevID
].incLen
== 0) { /* try to reuse old cmd */
582 if (IS_CMD(MidiInDev
[wDevID
].incPrev
) && !IS_SYS_CMD(MidiInDev
[wDevID
].incPrev
)) {
583 MidiInDev
[wDevID
].incoming
[0] = MidiInDev
[wDevID
].incPrev
;
584 MidiInDev
[wDevID
].incLen
= 1;
585 TRACE("Reusing old command %02xh\n", MidiInDev
[wDevID
].incPrev
);
587 FIXME("error for midi-in, should generate MIM_ERROR notification:"
588 " prev=%02Xh, incLen=%02Xh\n",
589 MidiInDev
[wDevID
].incPrev
, MidiInDev
[wDevID
].incLen
);
593 MidiInDev
[wDevID
].incoming
[(int)(MidiInDev
[wDevID
].incLen
++)] = value
;
594 if (MidiInDev
[wDevID
].incLen
== 1 && !IS_SYS_CMD(MidiInDev
[wDevID
].incoming
[0])) {
595 /* store new cmd, just in case */
596 MidiInDev
[wDevID
].incPrev
= MidiInDev
[wDevID
].incoming
[0];
602 switch (MidiInDev
[wDevID
].incoming
[0] & 0xF0) {
605 case MIDI_KEY_PRESSURE
:
606 case MIDI_CTL_CHANGE
:
607 case MIDI_PITCH_BEND
:
608 if (MidiInDev
[wDevID
].incLen
== 3) {
609 toSend
= (MidiInDev
[wDevID
].incoming
[2] << 16) |
610 (MidiInDev
[wDevID
].incoming
[1] << 8) |
611 (MidiInDev
[wDevID
].incoming
[0] << 0);
614 case MIDI_PGM_CHANGE
:
615 case MIDI_CHN_PRESSURE
:
616 if (MidiInDev
[wDevID
].incLen
== 2) {
617 toSend
= (MidiInDev
[wDevID
].incoming
[1] << 8) |
618 (MidiInDev
[wDevID
].incoming
[0] << 0);
621 case MIDI_SYSTEM_PREFIX
:
622 if (MidiInDev
[wDevID
].incoming
[0] == 0xF0) {
623 MidiInDev
[wDevID
].state
|= 2;
624 MidiInDev
[wDevID
].incLen
= 0;
626 if (MidiInDev
[wDevID
].incLen
== 1) {
627 toSend
= (MidiInDev
[wDevID
].incoming
[0] << 0);
632 WARN("This shouldn't happen (%02X)\n", MidiInDev
[wDevID
].incoming
[0]);
635 TRACE("Sending event %08lx\n", toSend
);
636 MidiInDev
[wDevID
].incLen
= 0;
637 dwTime
-= MidiInDev
[wDevID
].startTime
;
638 if (MIDI_NotifyClient(wDevID
, MIM_DATA
, toSend
, dwTime
) != MMSYSERR_NOERROR
) {
639 WARN("Couldn't notify client\n");
644 static DWORD WINAPI
midRecThread(LPVOID arg
)
646 unsigned char buffer
[256];
651 TRACE("Thread startup\n");
657 TRACE("Thread loop\n");
659 /* Check if an event is present */
660 if (poll(&pfd
, 1, 250) <= 0)
663 len
= read(midiSeqFD
, buffer
, sizeof(buffer
));
664 TRACE("Reveived %d bytes\n", len
);
666 if (len
< 0) continue;
667 if ((len
% 4) != 0) {
668 WARN("Bad length %d, errno %d (%s)\n", len
, errno
, strerror(errno
));
672 dwTime
= GetTickCount();
674 for (idx
= 0; idx
< len
; ) {
675 if (buffer
[idx
] & 0x80) {
677 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
678 buffer
[idx
+ 0], buffer
[idx
+ 1],
679 buffer
[idx
+ 2], buffer
[idx
+ 3],
680 buffer
[idx
+ 4], buffer
[idx
+ 5],
681 buffer
[idx
+ 6], buffer
[idx
+ 7]);
684 switch (buffer
[idx
+ 0]) {
689 midReceiveChar(buffer
[idx
+ 2], buffer
[idx
+ 1], dwTime
);
692 TRACE("Unsupported event %d\n", buffer
[idx
+ 0]);
702 /**************************************************************************
703 * midGetDevCaps [internal]
705 static DWORD
midGetDevCaps(WORD wDevID
, LPMIDIINCAPSW lpCaps
, DWORD dwSize
)
707 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpCaps
, dwSize
);
709 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
710 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
712 memcpy(lpCaps
, &MidiInDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
714 return MMSYSERR_NOERROR
;
717 /**************************************************************************
720 static DWORD
midOpen(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
722 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpDesc
, dwFlags
);
724 if (lpDesc
== NULL
) {
725 WARN("Invalid Parameter !\n");
726 return MMSYSERR_INVALPARAM
;
730 * how to check that content of lpDesc is correct ?
732 if (wDevID
>= MIDM_NumDevs
) {
733 WARN("wDevID too large (%u) !\n", wDevID
);
734 return MMSYSERR_BADDEVICEID
;
736 if (MidiInDev
[wDevID
].state
== -1) {
737 WARN("device disabled\n");
738 return MIDIERR_NODEVICE
;
740 if (MidiInDev
[wDevID
].midiDesc
.hMidi
!= 0) {
741 WARN("device already open !\n");
742 return MMSYSERR_ALLOCATED
;
744 if ((dwFlags
& MIDI_IO_STATUS
) != 0) {
745 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
746 dwFlags
&= ~MIDI_IO_STATUS
;
748 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
749 FIXME("Bad dwFlags\n");
750 return MMSYSERR_INVALFLAG
;
753 if (midiOpenSeq() < 0) {
754 return MMSYSERR_ERROR
;
757 if (numStartedMidiIn
++ == 0) {
759 hThread
= CreateThread(NULL
, 0, midRecThread
, NULL
, 0, NULL
);
761 numStartedMidiIn
= 0;
762 WARN("Couldn't create thread for midi-in\n");
764 return MMSYSERR_ERROR
;
766 SetThreadPriority(hThread
, THREAD_PRIORITY_TIME_CRITICAL
);
767 TRACE("Created thread for midi-in\n");
770 MidiInDev
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
772 MidiInDev
[wDevID
].lpQueueHdr
= NULL
;
773 MidiInDev
[wDevID
].dwTotalPlayed
= 0;
774 MidiInDev
[wDevID
].bufsize
= 0x3FFF;
775 MidiInDev
[wDevID
].midiDesc
= *lpDesc
;
776 MidiInDev
[wDevID
].state
= 0;
777 MidiInDev
[wDevID
].incLen
= 0;
778 MidiInDev
[wDevID
].startTime
= 0;
780 if (MIDI_NotifyClient(wDevID
, MIM_OPEN
, 0L, 0L) != MMSYSERR_NOERROR
) {
781 WARN("can't notify client !\n");
782 return MMSYSERR_INVALPARAM
;
784 return MMSYSERR_NOERROR
;
787 /**************************************************************************
788 * midClose [internal]
790 static DWORD
midClose(WORD wDevID
)
792 int ret
= MMSYSERR_NOERROR
;
794 TRACE("(%04X);\n", wDevID
);
796 if (wDevID
>= MIDM_NumDevs
) {
797 WARN("wDevID too big (%u) !\n", wDevID
);
798 return MMSYSERR_BADDEVICEID
;
800 if (MidiInDev
[wDevID
].midiDesc
.hMidi
== 0) {
801 WARN("device not opened !\n");
802 return MMSYSERR_ERROR
;
804 if (MidiInDev
[wDevID
].lpQueueHdr
!= 0) {
805 return MIDIERR_STILLPLAYING
;
808 if (midiSeqFD
== -1) {
810 return MMSYSERR_ERROR
;
812 if (--numStartedMidiIn
== 0) {
813 TRACE("Stopping thread for midi-in\n");
815 if (WaitForSingleObject(hThread
, 5000) != WAIT_OBJECT_0
) {
816 WARN("Thread end not signaled, force termination\n");
817 TerminateThread(hThread
, 0);
819 TRACE("Stopped thread for midi-in\n");
823 MidiInDev
[wDevID
].bufsize
= 0;
824 if (MIDI_NotifyClient(wDevID
, MIM_CLOSE
, 0L, 0L) != MMSYSERR_NOERROR
) {
825 WARN("can't notify client !\n");
826 ret
= MMSYSERR_INVALPARAM
;
828 MidiInDev
[wDevID
].midiDesc
.hMidi
= 0;
832 /**************************************************************************
833 * midAddBuffer [internal]
835 static DWORD
midAddBuffer(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
837 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
839 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
840 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
842 if (lpMidiHdr
== NULL
) return MMSYSERR_INVALPARAM
;
843 if (sizeof(MIDIHDR
) > dwSize
) return MMSYSERR_INVALPARAM
;
844 if (lpMidiHdr
->dwBufferLength
== 0) return MMSYSERR_INVALPARAM
;
845 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) return MIDIERR_STILLPLAYING
;
846 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) return MIDIERR_UNPREPARED
;
848 EnterCriticalSection(&crit_sect
);
849 if (MidiInDev
[wDevID
].lpQueueHdr
== 0) {
850 MidiInDev
[wDevID
].lpQueueHdr
= lpMidiHdr
;
854 for (ptr
= MidiInDev
[wDevID
].lpQueueHdr
;
856 ptr
= (LPMIDIHDR
)ptr
->lpNext
);
857 ptr
->lpNext
= (struct midihdr_tag
*)lpMidiHdr
;
859 LeaveCriticalSection(&crit_sect
);
861 return MMSYSERR_NOERROR
;
864 /**************************************************************************
865 * midPrepare [internal]
867 static DWORD
midPrepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
869 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
871 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0 ||
872 lpMidiHdr
->lpData
== 0 || (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) != 0 ||
873 lpMidiHdr
->dwBufferLength
>= 0x10000ul
)
874 return MMSYSERR_INVALPARAM
;
876 lpMidiHdr
->lpNext
= 0;
877 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
878 lpMidiHdr
->dwBytesRecorded
= 0;
880 return MMSYSERR_NOERROR
;
883 /**************************************************************************
884 * midUnprepare [internal]
886 static DWORD
midUnprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
888 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
890 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
891 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
893 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0 ||
894 lpMidiHdr
->lpData
== 0 || lpMidiHdr
->dwBufferLength
>= 0x10000ul
)
895 return MMSYSERR_INVALPARAM
;
897 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) return MIDIERR_UNPREPARED
;
898 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) return MIDIERR_STILLPLAYING
;
900 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
902 return MMSYSERR_NOERROR
;
905 /**************************************************************************
906 * midReset [internal]
908 static DWORD
midReset(WORD wDevID
)
910 DWORD dwTime
= GetTickCount();
912 TRACE("(%04X);\n", wDevID
);
914 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
915 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
917 EnterCriticalSection(&crit_sect
);
918 while (MidiInDev
[wDevID
].lpQueueHdr
) {
919 MidiInDev
[wDevID
].lpQueueHdr
->dwFlags
&= ~MHDR_INQUEUE
;
920 MidiInDev
[wDevID
].lpQueueHdr
->dwFlags
|= MHDR_DONE
;
921 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
922 if (MIDI_NotifyClient(wDevID
, MIM_LONGDATA
,
923 (DWORD
)MidiInDev
[wDevID
].lpQueueHdr
, dwTime
) != MMSYSERR_NOERROR
) {
924 WARN("Couldn't notify client\n");
926 MidiInDev
[wDevID
].lpQueueHdr
= (LPMIDIHDR
)MidiInDev
[wDevID
].lpQueueHdr
->lpNext
;
928 LeaveCriticalSection(&crit_sect
);
930 return MMSYSERR_NOERROR
;
934 /**************************************************************************
935 * midStart [internal]
937 static DWORD
midStart(WORD wDevID
)
939 TRACE("(%04X);\n", wDevID
);
941 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
942 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
944 MidiInDev
[wDevID
].state
= 1;
945 MidiInDev
[wDevID
].startTime
= GetTickCount();
946 return MMSYSERR_NOERROR
;
949 /**************************************************************************
952 static DWORD
midStop(WORD wDevID
)
954 TRACE("(%04X);\n", wDevID
);
956 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
957 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
959 MidiInDev
[wDevID
].state
= 0;
960 return MMSYSERR_NOERROR
;
963 /*-----------------------------------------------------------------------*/
965 typedef struct sVoice
{
966 int note
; /* 0 means not used */
968 unsigned cntMark
: 30,
971 #define sVS_PLAYING 1
972 #define sVS_SUSTAINED 2
975 typedef struct sChannel
{
981 int bank
; /* CTL_BANK_SELECT */
982 int volume
; /* CTL_MAIN_VOLUME */
983 int balance
; /* CTL_BALANCE */
984 int expression
; /* CTL_EXPRESSION */
985 int sustain
; /* CTL_SUSTAIN */
987 unsigned char nrgPmtMSB
; /* Non register Parameters */
988 unsigned char nrgPmtLSB
;
989 unsigned char regPmtMSB
; /* Non register Parameters */
990 unsigned char regPmtLSB
;
993 typedef struct sFMextra
{
996 sChannel channel
[16]; /* MIDI has only 16 channels */
997 sVoice voice
[1]; /* dyn allocated according to sound card */
998 /* do not append fields below voice[1] since the size of this structure
999 * depends on the number of available voices on the FM synth...
1003 extern unsigned char midiFMInstrumentPatches
[16 * 128];
1004 extern unsigned char midiFMDrumsPatches
[16 * 128];
1006 /**************************************************************************
1007 * modFMLoad [internal]
1009 static int modFMLoad(int dev
)
1012 struct sbi_instrument sbi
;
1017 memset(sbi
.operators
+ 16, 0, 16);
1018 for (i
= 0; i
< 128; i
++) {
1020 memcpy(sbi
.operators
, midiFMInstrumentPatches
+ i
* 16, 16);
1022 if (write(midiSeqFD
, (char*)&sbi
, sizeof(sbi
)) == -1) {
1023 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi
.channel
, errno
, strerror(errno
));
1027 for (i
= 0; i
< 128; i
++) {
1028 sbi
.channel
= 128 + i
;
1029 memcpy(sbi
.operators
, midiFMDrumsPatches
+ i
* 16, 16);
1031 if (write(midiSeqFD
, (char*)&sbi
, sizeof(sbi
)) == -1) {
1032 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi
.channel
, errno
, strerror(errno
));
1039 /**************************************************************************
1040 * modFMReset [internal]
1042 static void modFMReset(WORD wDevID
)
1044 sFMextra
* extra
= (sFMextra
*)MidiOutDev
[wDevID
].lpExtra
;
1045 sVoice
* voice
= extra
->voice
;
1046 sChannel
* channel
= extra
->channel
;
1049 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1050 if (voice
[i
].status
!= sVS_UNUSED
) {
1051 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1053 SEQ_KEY_PRESSURE(wDevID
, i
, 127, 0);
1054 SEQ_CONTROL(wDevID
, i
, SEQ_VOLMODE
, VOL_METHOD_LINEAR
);
1056 voice
[i
].channel
= -1;
1057 voice
[i
].cntMark
= 0;
1058 voice
[i
].status
= sVS_UNUSED
;
1060 for (i
= 0; i
< 16; i
++) {
1061 channel
[i
].program
= 0;
1062 channel
[i
].bender
= 8192;
1063 channel
[i
].benderRange
= 2;
1064 channel
[i
].bank
= 0;
1065 channel
[i
].volume
= 127;
1066 channel
[i
].balance
= 64;
1067 channel
[i
].expression
= 0;
1068 channel
[i
].sustain
= 0;
1071 extra
->drumSetMask
= 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
1075 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
1077 /**************************************************************************
1078 * modGetDevCaps [internal]
1080 static DWORD
modGetDevCaps(WORD wDevID
, LPMIDIOUTCAPSW lpCaps
, DWORD dwSize
)
1082 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpCaps
, dwSize
);
1084 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1085 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
1087 memcpy(lpCaps
, &MidiOutDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
1089 return MMSYSERR_NOERROR
;
1092 /**************************************************************************
1093 * modOpen [internal]
1095 static DWORD
modOpen(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
1097 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpDesc
, dwFlags
);
1098 if (lpDesc
== NULL
) {
1099 WARN("Invalid Parameter !\n");
1100 return MMSYSERR_INVALPARAM
;
1102 if (wDevID
>= MODM_NumDevs
) {
1103 TRACE("MAX_MIDIOUTDRV reached !\n");
1104 return MMSYSERR_BADDEVICEID
;
1106 if (MidiOutDev
[wDevID
].midiDesc
.hMidi
!= 0) {
1107 WARN("device already open !\n");
1108 return MMSYSERR_ALLOCATED
;
1110 if (!MidiOutDev
[wDevID
].bEnabled
) {
1111 WARN("device disabled !\n");
1112 return MIDIERR_NODEVICE
;
1114 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
1115 WARN("bad dwFlags\n");
1116 return MMSYSERR_INVALFLAG
;
1118 if (!MidiOutDev
[wDevID
].bEnabled
) {
1119 TRACE("disabled wDevID\n");
1120 return MMSYSERR_NOTENABLED
;
1123 MidiOutDev
[wDevID
].lpExtra
= 0;
1125 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1130 extra
= HeapAlloc(GetProcessHeap(), 0,
1131 sizeof(struct sFMextra
) +
1132 sizeof(struct sVoice
) * (MidiOutDev
[wDevID
].caps
.wVoices
- 1));
1135 WARN("can't alloc extra data !\n");
1136 return MMSYSERR_NOMEM
;
1138 MidiOutDev
[wDevID
].lpExtra
= extra
;
1139 if (midiOpenSeq() < 0) {
1140 MidiOutDev
[wDevID
].lpExtra
= 0;
1141 HeapFree(GetProcessHeap(), 0, extra
);
1142 return MMSYSERR_ERROR
;
1144 if (modFMLoad(wDevID
) < 0) {
1146 MidiOutDev
[wDevID
].lpExtra
= 0;
1147 HeapFree(GetProcessHeap(), 0, extra
);
1148 return MMSYSERR_ERROR
;
1155 if (midiOpenSeq() < 0) {
1156 return MMSYSERR_ALLOCATED
;
1160 WARN("Technology not supported (yet) %d !\n",
1161 MidiOutDev
[wDevID
].caps
.wTechnology
);
1162 return MMSYSERR_NOTENABLED
;
1165 MidiOutDev
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1167 MidiOutDev
[wDevID
].lpQueueHdr
= NULL
;
1168 MidiOutDev
[wDevID
].dwTotalPlayed
= 0;
1169 MidiOutDev
[wDevID
].bufsize
= 0x3FFF;
1170 MidiOutDev
[wDevID
].midiDesc
= *lpDesc
;
1172 if (MIDI_NotifyClient(wDevID
, MOM_OPEN
, 0L, 0L) != MMSYSERR_NOERROR
) {
1173 WARN("can't notify client !\n");
1174 return MMSYSERR_INVALPARAM
;
1176 TRACE("Successful !\n");
1177 return MMSYSERR_NOERROR
;
1181 /**************************************************************************
1182 * modClose [internal]
1184 static DWORD
modClose(WORD wDevID
)
1186 int ret
= MMSYSERR_NOERROR
;
1188 TRACE("(%04X);\n", wDevID
);
1190 if (MidiOutDev
[wDevID
].midiDesc
.hMidi
== 0) {
1191 WARN("device not opened !\n");
1192 return MMSYSERR_ERROR
;
1194 /* FIXME: should test that no pending buffer is still in the queue for
1197 if (midiSeqFD
== -1) {
1198 WARN("can't close !\n");
1199 return MMSYSERR_ERROR
;
1202 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1208 WARN("Technology not supported (yet) %d !\n",
1209 MidiOutDev
[wDevID
].caps
.wTechnology
);
1210 return MMSYSERR_NOTENABLED
;
1213 HeapFree(GetProcessHeap(), 0, MidiOutDev
[wDevID
].lpExtra
);
1214 MidiOutDev
[wDevID
].lpExtra
= 0;
1216 MidiOutDev
[wDevID
].bufsize
= 0;
1217 if (MIDI_NotifyClient(wDevID
, MOM_CLOSE
, 0L, 0L) != MMSYSERR_NOERROR
) {
1218 WARN("can't notify client !\n");
1219 ret
= MMSYSERR_INVALPARAM
;
1221 MidiOutDev
[wDevID
].midiDesc
.hMidi
= 0;
1225 /**************************************************************************
1226 * modData [internal]
1228 static DWORD
modData(WORD wDevID
, DWORD dwParam
)
1230 WORD evt
= LOBYTE(LOWORD(dwParam
));
1231 WORD d1
= HIBYTE(LOWORD(dwParam
));
1232 WORD d2
= LOBYTE(HIWORD(dwParam
));
1234 TRACE("(%04X, %08lX);\n", wDevID
, dwParam
);
1236 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1237 if (!MidiOutDev
[wDevID
].bEnabled
) return MIDIERR_NODEVICE
;
1239 if (midiSeqFD
== -1) {
1240 WARN("can't play !\n");
1241 return MIDIERR_NODEVICE
;
1243 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1246 * - chorus depth controller is not used
1249 sFMextra
* extra
= (sFMextra
*)MidiOutDev
[wDevID
].lpExtra
;
1250 sVoice
* voice
= extra
->voice
;
1251 sChannel
* channel
= extra
->channel
;
1252 int chn
= (evt
& 0x0F);
1255 switch (evt
& 0xF0) {
1257 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1258 /* don't stop sustained notes */
1259 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1260 voice
[i
].status
= sVS_UNUSED
;
1261 SEQ_STOP_NOTE(wDevID
, i
, d1
, d2
);
1266 if (d2
== 0) { /* note off if velocity == 0 */
1267 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1268 /* don't stop sustained notes */
1269 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1270 voice
[i
].status
= sVS_UNUSED
;
1271 SEQ_STOP_NOTE(wDevID
, i
, d1
, 64);
1276 /* finding out in this order :
1278 * - if replaying the same note on the same channel
1279 * - the older voice (LRU)
1281 for (i
= nv
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1282 if (voice
[i
].status
== sVS_UNUSED
||
1283 (voice
[i
].note
== d1
&& voice
[i
].channel
== chn
)) {
1287 if (voice
[i
].cntMark
< voice
[0].cntMark
) {
1292 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1293 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1294 nv
, channel
[chn
].program
,
1295 channel
[chn
].balance
,
1296 channel
[chn
].volume
,
1297 channel
[chn
].bender
, d1
, d2
);
1299 SEQ_SET_PATCH(wDevID
, nv
, IS_DRUM_CHANNEL(extra
, chn
) ?
1300 (128 + d1
) : channel
[chn
].program
);
1301 SEQ_BENDER_RANGE(wDevID
, nv
, channel
[chn
].benderRange
* 100);
1302 SEQ_BENDER(wDevID
, nv
, channel
[chn
].bender
);
1303 SEQ_CONTROL(wDevID
, nv
, CTL_PAN
, channel
[chn
].balance
);
1304 SEQ_CONTROL(wDevID
, nv
, CTL_EXPRESSION
, channel
[chn
].expression
);
1306 /* FIXME: does not really seem to work on my SB card and
1307 * screws everything up... so lay it down
1309 SEQ_CONTROL(wDevID
, nv
, CTL_MAIN_VOLUME
, channel
[chn
].volume
);
1311 SEQ_START_NOTE(wDevID
, nv
, d1
, d2
);
1312 voice
[nv
].status
= channel
[chn
].sustain
? sVS_SUSTAINED
: sVS_PLAYING
;
1313 voice
[nv
].note
= d1
;
1314 voice
[nv
].channel
= chn
;
1315 voice
[nv
].cntMark
= extra
->counter
++;
1317 case MIDI_KEY_PRESSURE
:
1318 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1319 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1320 SEQ_KEY_PRESSURE(wDevID
, i
, d1
, d2
);
1324 case MIDI_CTL_CHANGE
:
1326 case CTL_BANK_SELECT
: channel
[chn
].bank
= d2
; break;
1327 case CTL_MAIN_VOLUME
: channel
[chn
].volume
= d2
; break;
1328 case CTL_PAN
: channel
[chn
].balance
= d2
; break;
1329 case CTL_EXPRESSION
: channel
[chn
].expression
= d2
; break;
1330 case CTL_SUSTAIN
: channel
[chn
].sustain
= d2
;
1332 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1333 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
) {
1334 voice
[i
].status
= sVS_SUSTAINED
;
1338 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1339 if (voice
[i
].status
== sVS_SUSTAINED
&& voice
[i
].channel
== chn
) {
1340 voice
[i
].status
= sVS_UNUSED
;
1341 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1346 case CTL_NONREG_PARM_NUM_LSB
: channel
[chn
].nrgPmtLSB
= d2
; break;
1347 case CTL_NONREG_PARM_NUM_MSB
: channel
[chn
].nrgPmtMSB
= d2
; break;
1348 case CTL_REGIST_PARM_NUM_LSB
: channel
[chn
].regPmtLSB
= d2
; break;
1349 case CTL_REGIST_PARM_NUM_MSB
: channel
[chn
].regPmtMSB
= d2
; break;
1350 case CTL_DATA_ENTRY
:
1351 switch ((channel
[chn
].regPmtMSB
<< 8) | channel
[chn
].regPmtLSB
) {
1353 if (channel
[chn
].benderRange
!= d2
) {
1354 channel
[chn
].benderRange
= d2
;
1355 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1356 if (voice
[i
].channel
== chn
) {
1357 SEQ_BENDER_RANGE(wDevID
, i
, channel
[chn
].benderRange
);
1364 channel
[chn
].benderRange
= 2;
1365 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1366 if (voice
[i
].channel
== chn
) {
1367 SEQ_BENDER_RANGE(wDevID
, i
, channel
[chn
].benderRange
);
1372 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1373 channel
[chn
].regPmtMSB
, channel
[chn
].regPmtLSB
,
1374 channel
[chn
].nrgPmtMSB
, channel
[chn
].nrgPmtLSB
,
1380 case 0x78: /* all sounds off */
1381 /* FIXME: I don't know if I have to take care of the channel
1382 * for this control ?
1384 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1385 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
) {
1386 voice
[i
].status
= sVS_UNUSED
;
1387 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1391 case 0x7B: /* all notes off */
1392 /* FIXME: I don't know if I have to take care of the channel
1393 * for this control ?
1395 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1396 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
) {
1397 voice
[i
].status
= sVS_UNUSED
;
1398 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1403 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1408 case MIDI_PGM_CHANGE
:
1409 channel
[chn
].program
= d1
;
1411 case MIDI_CHN_PRESSURE
:
1412 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1413 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
) {
1414 SEQ_KEY_PRESSURE(wDevID
, i
, voice
[i
].note
, d1
);
1418 case MIDI_PITCH_BEND
:
1419 channel
[chn
].bender
= (d2
<< 7) + d1
;
1420 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1421 if (voice
[i
].channel
== chn
) {
1422 SEQ_BENDER(wDevID
, i
, channel
[chn
].bender
);
1426 case MIDI_SYSTEM_PREFIX
:
1427 switch (evt
& 0x0F) {
1428 case 0x0F: /* Reset */
1432 WARN("Unsupported (yet) system event %02x\n", evt
& 0x0F);
1436 WARN("Internal error, shouldn't happen (event=%08x)\n", evt
& 0xF0);
1437 return MMSYSERR_NOTENABLED
;
1443 int dev
= wDevID
- MODM_NumFMSynthDevs
;
1445 WARN("Internal error on devID (%u) !\n", wDevID
);
1446 return MIDIERR_NODEVICE
;
1449 switch (evt
& 0xF0) {
1452 case MIDI_KEY_PRESSURE
:
1453 case MIDI_CTL_CHANGE
:
1454 case MIDI_PITCH_BEND
:
1455 SEQ_MIDIOUT(dev
, evt
);
1456 SEQ_MIDIOUT(dev
, d1
);
1457 SEQ_MIDIOUT(dev
, d2
);
1459 case MIDI_PGM_CHANGE
:
1460 case MIDI_CHN_PRESSURE
:
1461 SEQ_MIDIOUT(dev
, evt
);
1462 SEQ_MIDIOUT(dev
, d1
);
1464 case MIDI_SYSTEM_PREFIX
:
1465 switch (evt
& 0x0F) {
1466 case 0x00: /* System Exclusive, don't do it on modData,
1467 * should require modLongData*/
1468 case 0x01: /* Undefined */
1469 case 0x04: /* Undefined. */
1470 case 0x05: /* Undefined. */
1471 case 0x07: /* End of Exclusive. */
1472 case 0x09: /* Undefined. */
1473 case 0x0D: /* Undefined. */
1475 case 0x06: /* Tune Request */
1476 case 0x08: /* Timing Clock. */
1477 case 0x0A: /* Start. */
1478 case 0x0B: /* Continue */
1479 case 0x0C: /* Stop */
1480 case 0x0E: /* Active Sensing. */
1481 SEQ_MIDIOUT(dev
, evt
);
1483 case 0x0F: /* Reset */
1484 /* SEQ_MIDIOUT(dev, evt);
1485 this other way may be better */
1486 SEQ_MIDIOUT(dev
, MIDI_SYSTEM_PREFIX
);
1487 SEQ_MIDIOUT(dev
, 0x7e);
1488 SEQ_MIDIOUT(dev
, 0x7f);
1489 SEQ_MIDIOUT(dev
, 0x09);
1490 SEQ_MIDIOUT(dev
, 0x01);
1491 SEQ_MIDIOUT(dev
, 0xf7);
1493 case 0x03: /* Song Select. */
1494 SEQ_MIDIOUT(dev
, evt
);
1495 SEQ_MIDIOUT(dev
, d1
);
1496 case 0x02: /* Song Position Pointer. */
1497 SEQ_MIDIOUT(dev
, evt
);
1498 SEQ_MIDIOUT(dev
, d1
);
1499 SEQ_MIDIOUT(dev
, d2
);
1506 WARN("Technology not supported (yet) %d !\n",
1507 MidiOutDev
[wDevID
].caps
.wTechnology
);
1508 return MMSYSERR_NOTENABLED
;
1513 return MMSYSERR_NOERROR
;
1516 /**************************************************************************
1517 * modLongData [internal]
1519 static DWORD
modLongData(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1524 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
1526 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
1527 * but it seems to be used only for midi input.
1528 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
1531 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1532 if (!MidiOutDev
[wDevID
].bEnabled
) return MIDIERR_NODEVICE
;
1534 if (midiSeqFD
== -1) {
1535 WARN("can't play !\n");
1536 return MIDIERR_NODEVICE
;
1539 lpData
= (LPBYTE
) lpMidiHdr
->lpData
;
1542 return MIDIERR_UNPREPARED
;
1543 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
1544 return MIDIERR_UNPREPARED
;
1545 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
1546 return MIDIERR_STILLPLAYING
;
1547 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
1548 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
1550 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1551 * data, or can it also contain raw MIDI data, to be split up and sent to
1553 * If the latest is true, then the following WARNing will fire up
1555 if (lpData
[0] != 0xF0 || lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
1556 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1559 TRACE("dwBufferLength=%lu !\n", lpMidiHdr
->dwBufferLength
);
1560 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1561 lpData
[0], lpData
[1], lpData
[2], lpData
[lpMidiHdr
->dwBufferLength
-3],
1562 lpData
[lpMidiHdr
->dwBufferLength
-2], lpData
[lpMidiHdr
->dwBufferLength
-1]);
1564 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1566 /* FIXME: I don't think there is much to do here */
1569 if (lpData
[0] != 0xF0) {
1570 /* Send end of System Exclusive */
1571 SEQ_MIDIOUT(wDevID
- MODM_NumFMSynthDevs
, 0xF0);
1572 WARN("Adding missing 0xF0 marker at the beginning of "
1573 "system exclusive byte stream\n");
1575 for (count
= 0; count
< lpMidiHdr
->dwBufferLength
; count
++) {
1576 SEQ_MIDIOUT(wDevID
- MODM_NumFMSynthDevs
, lpData
[count
]);
1578 if (lpData
[count
- 1] != 0xF7) {
1579 /* Send end of System Exclusive */
1580 SEQ_MIDIOUT(wDevID
- MODM_NumFMSynthDevs
, 0xF7);
1581 WARN("Adding missing 0xF7 marker at the end of "
1582 "system exclusive byte stream\n");
1587 WARN("Technology not supported (yet) %d !\n",
1588 MidiOutDev
[wDevID
].caps
.wTechnology
);
1589 return MMSYSERR_NOTENABLED
;
1592 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
1593 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
1594 if (MIDI_NotifyClient(wDevID
, MOM_DONE
, (DWORD
)lpMidiHdr
, 0L) != MMSYSERR_NOERROR
) {
1595 WARN("can't notify client !\n");
1596 return MMSYSERR_INVALPARAM
;
1598 return MMSYSERR_NOERROR
;
1601 /**************************************************************************
1602 * modPrepare [internal]
1604 static DWORD
modPrepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1606 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
1608 if (midiSeqFD
== -1) {
1609 WARN("can't prepare !\n");
1610 return MMSYSERR_NOTENABLED
;
1613 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
1614 * asks to prepare MIDIHDR which dwFlags != 0.
1615 * So at least check for the inqueue flag
1617 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0 ||
1618 lpMidiHdr
->lpData
== 0 || (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) != 0 ||
1619 lpMidiHdr
->dwBufferLength
>= 0x10000ul
) {
1620 WARN("%p %p %08lx %d/%ld\n", lpMidiHdr
, lpMidiHdr
->lpData
,
1621 lpMidiHdr
->dwFlags
, sizeof(MIDIHDR
), dwSize
);
1622 return MMSYSERR_INVALPARAM
;
1625 lpMidiHdr
->lpNext
= 0;
1626 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
1627 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
1628 return MMSYSERR_NOERROR
;
1631 /**************************************************************************
1632 * modUnprepare [internal]
1634 static DWORD
modUnprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1636 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
1638 if (midiSeqFD
== -1) {
1639 WARN("can't unprepare !\n");
1640 return MMSYSERR_NOTENABLED
;
1643 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0)
1644 return MMSYSERR_INVALPARAM
;
1645 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
1646 return MIDIERR_STILLPLAYING
;
1647 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
1648 return MMSYSERR_NOERROR
;
1651 /**************************************************************************
1652 * modReset [internal]
1654 static DWORD
modReset(WORD wDevID
)
1658 TRACE("(%04X);\n", wDevID
);
1660 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1661 if (!MidiOutDev
[wDevID
].bEnabled
) return MIDIERR_NODEVICE
;
1663 /* stop all notes */
1664 /* FIXME: check if 0x78B0 is channel dependent or not. I coded it so that
1665 * it's channel dependent...
1667 for (chn
= 0; chn
< 16; chn
++) {
1668 /* turn off every note */
1669 modData(wDevID
, 0x7800 | MIDI_CTL_CHANGE
| chn
);
1670 /* remove sustain on all channels */
1671 modData(wDevID
, (CTL_SUSTAIN
<< 8) | MIDI_CTL_CHANGE
| chn
);
1673 /* FIXME: the LongData buffers must also be returned to the app */
1674 return MMSYSERR_NOERROR
;
1677 #else /* HAVE_OSS_MIDI */
1679 LRESULT
OSS_MidiInit(void)
1684 #endif /* HAVE_OSS_MIDI */
1686 /*======================================================================*
1687 * MIDI entry points *
1688 *======================================================================*/
1690 /**************************************************************************
1691 * midMessage (WINEOSS.4)
1693 DWORD WINAPI
OSS_midMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1694 DWORD dwParam1
, DWORD dwParam2
)
1696 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1697 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1699 #ifdef HAVE_OSS_MIDI
1704 /* FIXME: Pretend this is supported */
1707 return midOpen(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1709 return midClose(wDevID
);
1710 case MIDM_ADDBUFFER
:
1711 return midAddBuffer(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1713 return midPrepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1714 case MIDM_UNPREPARE
:
1715 return midUnprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1716 case MIDM_GETDEVCAPS
:
1717 return midGetDevCaps(wDevID
, (LPMIDIINCAPSW
)dwParam1
,dwParam2
);
1718 case MIDM_GETNUMDEVS
:
1719 return MIDM_NumDevs
;
1721 return midReset(wDevID
);
1723 return midStart(wDevID
);
1725 return midStop(wDevID
);
1728 TRACE("Unsupported message\n");
1730 return MMSYSERR_NOTSUPPORTED
;
1733 /**************************************************************************
1734 * modMessage (WINEOSS.5)
1736 DWORD WINAPI
OSS_modMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1737 DWORD dwParam1
, DWORD dwParam2
)
1739 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1740 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1743 #ifdef HAVE_OSS_MIDI
1748 /* FIXME: Pretend this is supported */
1751 return modOpen(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1753 return modClose(wDevID
);
1755 return modData(wDevID
, dwParam1
);
1757 return modLongData(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1759 return modPrepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1760 case MODM_UNPREPARE
:
1761 return modUnprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1762 case MODM_GETDEVCAPS
:
1763 return modGetDevCaps(wDevID
, (LPMIDIOUTCAPSW
)dwParam1
, dwParam2
);
1764 case MODM_GETNUMDEVS
:
1765 return MODM_NumDevs
;
1766 case MODM_GETVOLUME
:
1768 case MODM_SETVOLUME
:
1771 return modReset(wDevID
);
1774 TRACE("Unsupported message\n");
1776 return MMSYSERR_NOTSUPPORTED
;
1779 /*-----------------------------------------------------------------------*/