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
39 #ifdef HAVE_SYS_IOCTL_H
40 # include <sys/ioctl.h>
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(midi
);
55 #define MIDI_SEQ "/dev/sequencer"
58 int state
; /* -1 disabled, 0 is no recording started, 1 in recording, bit 2 set if in sys exclusive recording */
60 MIDIOPENDESC midiDesc
;
64 unsigned char incoming
[3];
65 unsigned char incPrev
;
74 MIDIOPENDESC midiDesc
;
78 void* lpExtra
; /* according to port type (MIDI, FM...), extra data when needed */
82 static WINE_MIDIIN MidiInDev
[MAX_MIDIINDRV
];
83 static WINE_MIDIOUT MidiOutDev
[MAX_MIDIOUTDRV
];
85 /* this is the total number of MIDI out devices found (synth and port) */
86 static int MODM_NumDevs
= 0;
87 /* this is the number of FM synthetizers (index from 0 to NUMFMSYNTHDEVS - 1) */
88 static int MODM_NumFMSynthDevs
= 0;
89 /* the Midi ports have index from NUMFMSYNTHDEVS to NumDevs - 1 */
91 /* this is the total number of MIDI out devices found */
92 static int MIDM_NumDevs
= 0;
94 static int midiSeqFD
= -1;
95 static int numOpenMidiSeq
= 0;
96 static UINT midiInTimerID
= 0;
97 static int numStartedMidiIn
= 0;
99 /*======================================================================*
100 * Low level MIDI implementation *
101 *======================================================================*/
103 static int midiOpenSeq(void);
104 static int midiCloseSeq(void);
106 /**************************************************************************
107 * MIDI_unixToWindowsDeviceType [internal]
109 * return the Windows equivalent to a Unix Device Type
112 static int MIDI_UnixToWindowsDeviceType(int type
)
114 /* MOD_MIDIPORT output port
115 * MOD_SYNTH generic internal synth
116 * MOD_SQSYNTH square wave internal synth
117 * MOD_FMSYNTH FM internal synth
118 * MOD_MAPPER MIDI mapper
119 * MOD_WAVETABLE hardware watetable internal synth
120 * MOD_SWSYNTH software internal synth
123 /* FIXME Is this really the correct equivalence from UNIX to
124 Windows Sound type */
127 case SYNTH_TYPE_FM
: return MOD_FMSYNTH
;
128 case SYNTH_TYPE_SAMPLE
: return MOD_SYNTH
;
129 case SYNTH_TYPE_MIDI
: return MOD_MIDIPORT
;
131 ERR("Cannot determine the type of this midi device. "
132 "Assuming FM Synth\n");
138 /**************************************************************************
139 * OSS_MidiInit [internal]
141 * Initializes the MIDI devices information variables
143 BOOL
OSS_MidiInit(void)
145 int i
, status
, numsynthdevs
= 255, nummididevs
= 255;
146 struct synth_info sinfo
;
147 struct midi_info minfo
;
148 static BOOL bInitDone
= FALSE
;
153 TRACE("Initializing the MIDI variables.\n");
156 /* try to open device */
157 if (midiOpenSeq() == -1) {
161 /* find how many Synth devices are there in the system */
162 status
= ioctl(midiSeqFD
, SNDCTL_SEQ_NRSYNTHS
, &numsynthdevs
);
165 ERR("ioctl for nr synth failed.\n");
170 if (numsynthdevs
> MAX_MIDIOUTDRV
) {
171 ERR("MAX_MIDIOUTDRV (%d) was enough for the number of devices (%d). "
172 "Some FM devices will not be available.\n",MAX_MIDIOUTDRV
,numsynthdevs
);
173 numsynthdevs
= MAX_MIDIOUTDRV
;
176 for (i
= 0; i
< numsynthdevs
; i
++) {
177 /* Manufac ID. We do not have access to this with soundcard.h
178 * Does not seem to be a problem, because in mmsystem.h only
179 * Microsoft's ID is listed.
181 MidiOutDev
[i
].caps
.wMid
= 0x00FF;
182 MidiOutDev
[i
].caps
.wPid
= 0x0001; /* FIXME Product ID */
183 /* Product Version. We simply say "1" */
184 MidiOutDev
[i
].caps
.vDriverVersion
= 0x001;
185 MidiOutDev
[i
].caps
.wChannelMask
= 0xFFFF;
187 /* FIXME Do we have this information?
188 * Assuming the soundcards can handle
189 * MIDICAPS_VOLUME and MIDICAPS_LRVOLUME but
190 * not MIDICAPS_CACHE.
192 MidiOutDev
[i
].caps
.dwSupport
= MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
;
195 status
= ioctl(midiSeqFD
, SNDCTL_SYNTH_INFO
, &sinfo
);
197 ERR("ioctl for synth info failed on %d, disabling it.\n", i
);
199 sprintf(MidiOutDev
[i
].caps
.szPname
, "Wine OSS Midi Out (#%d) - disabled", i
);
201 MidiOutDev
[i
].caps
.wTechnology
= MOD_MIDIPORT
;
202 MidiOutDev
[i
].caps
.wVoices
= 16;
203 MidiOutDev
[i
].caps
.wNotes
= 16;
204 MidiOutDev
[i
].bEnabled
= FALSE
;
206 strcpy(MidiOutDev
[i
].caps
.szPname
, sinfo
.name
);
208 MidiOutDev
[i
].caps
.wTechnology
= MIDI_UnixToWindowsDeviceType(sinfo
.synth_type
);
209 MidiOutDev
[i
].caps
.wVoices
= sinfo
.nr_voices
;
211 /* FIXME Is it possible to know the maximum
212 * number of simultaneous notes of a soundcard ?
213 * I believe we don't have this information, but
214 * it's probably equal or more than wVoices
216 MidiOutDev
[i
].caps
.wNotes
= sinfo
.nr_voices
;
217 MidiOutDev
[i
].bEnabled
= TRUE
;
220 /* We also have the information sinfo.synth_subtype, not used here
223 if (sinfo
.capabilities
& SYNTH_CAP_INPUT
) {
224 FIXME("Synthesizer support MIDI in. Not supported yet (please report)\n");
227 TRACE("SynthOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%ld\n"
228 "\tOSS info: synth subtype=%d capa=%lx\n",
229 i
, MidiOutDev
[i
].caps
.szPname
, MidiOutDev
[i
].caps
.wTechnology
,
230 MidiOutDev
[i
].caps
.wVoices
, MidiOutDev
[i
].caps
.wNotes
,
231 MidiOutDev
[i
].caps
.wChannelMask
, MidiOutDev
[i
].caps
.dwSupport
,
232 sinfo
.synth_subtype
, (long)sinfo
.capabilities
);
235 /* find how many MIDI devices are there in the system */
236 status
= ioctl(midiSeqFD
, SNDCTL_SEQ_NRMIDIS
, &nummididevs
);
238 ERR("ioctl on nr midi failed.\n");
243 /* FIXME: the two restrictions below could be loosen in some cases */
244 if (numsynthdevs
+ nummididevs
> MAX_MIDIOUTDRV
) {
245 ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
246 "Some MIDI devices will not be available.\n");
247 nummididevs
= MAX_MIDIOUTDRV
- numsynthdevs
;
250 if (nummididevs
> MAX_MIDIINDRV
) {
251 ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
252 "Some MIDI devices will not be available.\n",MAX_MIDIINDRV
,nummididevs
);
253 nummididevs
= MAX_MIDIINDRV
;
256 for (i
= 0; i
< nummididevs
; i
++) {
258 status
= ioctl(midiSeqFD
, SNDCTL_MIDI_INFO
, &minfo
);
259 if (status
== -1) WARN("ioctl on midi info for device %d failed.\n", i
);
261 /* This whole part is somewhat obscure to me. I'll keep trying to dig
262 info about it. If you happen to know, please tell us. The very
263 descritive minfo.dev_type was not used here.
265 /* Manufac ID. We do not have access to this with soundcard.h
266 Does not seem to be a problem, because in mmsystem.h only
267 Microsoft's ID is listed */
268 MidiOutDev
[numsynthdevs
+ i
].caps
.wMid
= 0x00FF;
269 MidiOutDev
[numsynthdevs
+ i
].caps
.wPid
= 0x0001; /* FIXME Product ID */
270 /* Product Version. We simply say "1" */
271 MidiOutDev
[numsynthdevs
+ i
].caps
.vDriverVersion
= 0x001;
273 sprintf(MidiOutDev
[numsynthdevs
+ i
].caps
.szPname
, "Wine OSS Midi Out (#%d) - disabled", numsynthdevs
+ i
);
274 MidiOutDev
[numsynthdevs
+ i
].bEnabled
= FALSE
;
276 strcpy(MidiOutDev
[numsynthdevs
+ i
].caps
.szPname
, minfo
.name
);
277 MidiOutDev
[numsynthdevs
+ i
].bEnabled
= TRUE
;
279 MidiOutDev
[numsynthdevs
+ i
].caps
.wTechnology
= MOD_MIDIPORT
; /* FIXME Is this right? */
280 /* Does it make any difference? */
281 MidiOutDev
[numsynthdevs
+ i
].caps
.wVoices
= 16;
282 /* Does it make any difference? */
283 MidiOutDev
[numsynthdevs
+ i
].caps
.wNotes
= 16;
284 MidiOutDev
[numsynthdevs
+ i
].caps
.wChannelMask
= 0xFFFF;
286 /* FIXME Does it make any difference? */
287 MidiOutDev
[numsynthdevs
+ i
].caps
.dwSupport
= MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
;
289 /* This whole part is somewhat obscure to me. I'll keep trying to dig
290 info about it. If you happen to know, please tell us. The very
291 descritive minfo.dev_type was not used here.
293 /* Manufac ID. We do not have access to this with soundcard.h
294 Does not seem to be a problem, because in mmsystem.h only
295 Microsoft's ID is listed */
296 MidiInDev
[i
].caps
.wMid
= 0x00FF;
297 MidiInDev
[i
].caps
.wPid
= 0x0001; /* FIXME Product ID */
298 /* Product Version. We simply say "1" */
299 MidiInDev
[i
].caps
.vDriverVersion
= 0x001;
301 sprintf(MidiInDev
[i
].caps
.szPname
, "Wine OSS Midi In (#%d) - disabled", numsynthdevs
+ i
);
302 MidiInDev
[i
].state
= -1;
304 strcpy(MidiInDev
[i
].caps
.szPname
, minfo
.name
);
305 MidiInDev
[i
].state
= 0;
307 /* FIXME : could we get better information than that ? */
308 MidiInDev
[i
].caps
.dwSupport
= MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
;
310 TRACE("MidiOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%ld\n"
311 "MidiIn [%d]\tname='%s' support=%ld\n"
312 "\tOSS info: midi dev-type=%d, capa=%lx\n",
313 i
, MidiOutDev
[numsynthdevs
+ i
].caps
.szPname
, MidiOutDev
[numsynthdevs
+ i
].caps
.wTechnology
,
314 MidiOutDev
[numsynthdevs
+ i
].caps
.wVoices
, MidiOutDev
[numsynthdevs
+ i
].caps
.wNotes
,
315 MidiOutDev
[numsynthdevs
+ i
].caps
.wChannelMask
, MidiOutDev
[numsynthdevs
+ i
].caps
.dwSupport
,
316 i
, MidiInDev
[i
].caps
.szPname
, MidiInDev
[i
].caps
.dwSupport
,
317 minfo
.dev_type
, (long)minfo
.capabilities
);
321 /* windows does not seem to differentiate Synth from MIDI devices */
322 MODM_NumFMSynthDevs
= numsynthdevs
;
323 MODM_NumDevs
= numsynthdevs
+ nummididevs
;
325 MIDM_NumDevs
= nummididevs
;
327 /* close file and exit */
333 /**************************************************************************
334 * MIDI_NotifyClient [internal]
336 static DWORD
MIDI_NotifyClient(UINT wDevID
, WORD wMsg
,
337 DWORD dwParam1
, DWORD dwParam2
)
344 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",
345 wDevID
, wMsg
, dwParam1
, dwParam2
);
351 if (wDevID
> MODM_NumDevs
)
352 return MMSYSERR_BADDEVICEID
;
354 dwCallBack
= MidiOutDev
[wDevID
].midiDesc
.dwCallback
;
355 uFlags
= MidiOutDev
[wDevID
].wFlags
;
356 hDev
= MidiOutDev
[wDevID
].midiDesc
.hMidi
;
357 dwInstance
= MidiOutDev
[wDevID
].midiDesc
.dwInstance
;
364 if (wDevID
> MIDM_NumDevs
)
365 return MMSYSERR_BADDEVICEID
;
367 dwCallBack
= MidiInDev
[wDevID
].midiDesc
.dwCallback
;
368 uFlags
= MidiInDev
[wDevID
].wFlags
;
369 hDev
= MidiInDev
[wDevID
].midiDesc
.hMidi
;
370 dwInstance
= MidiInDev
[wDevID
].midiDesc
.dwInstance
;
373 WARN("Unsupported MSW-MIDI message %u\n", wMsg
);
374 return MMSYSERR_ERROR
;
377 return DriverCallback(dwCallBack
, uFlags
, hDev
, wMsg
, dwInstance
, dwParam1
, dwParam2
) ?
381 static int midi_warn
= 1;
382 /**************************************************************************
383 * midiOpenSeq [internal]
385 static int midiOpenSeq(void)
387 if (numOpenMidiSeq
== 0) {
388 midiSeqFD
= open(MIDI_SEQ
, O_RDWR
, 0);
389 if (midiSeqFD
== -1) {
392 WARN("Can't open MIDI device '%s' ! (%s). If your "
393 "program needs this (probably not): %s\n",
394 MIDI_SEQ
, strerror(errno
),
396 "create it ! (\"man MAKEDEV\" ?)" :
398 "load MIDI sequencer kernel driver !" :
400 "grant access ! (\"man chmod\")" : ""
406 if (fcntl(midiSeqFD
, F_SETFL
, O_NONBLOCK
) < 0) {
407 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno
, strerror(errno
));
412 fcntl(midiSeqFD
, F_SETFD
, 1); /* set close on exec flag */
413 ioctl(midiSeqFD
, SNDCTL_SEQ_RESET
);
419 /**************************************************************************
420 * midiCloseSeq [internal]
422 static int midiCloseSeq(void)
424 if (--numOpenMidiSeq
== 0) {
431 /* FIXME: this is a bad idea, it's even not static... */
434 /* FIXME: this is not reentrant, not static - because of global variable
437 /**************************************************************************
438 * seqbuf_dump [internal]
440 * Used by SEQ_DUMPBUF to flush the buffer.
443 void seqbuf_dump(void)
446 if (write(midiSeqFD
, _seqbuf
, _seqbufptr
) == -1) {
447 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
448 midiSeqFD
, errno
, strerror(errno
));
451 * in any case buffer is lost so that if many errors occur the buffer
458 /**************************************************************************
459 * midReceiveChar [internal]
461 static void midReceiveChar(WORD wDevID
, unsigned char value
, DWORD dwTime
)
465 TRACE("Adding %02xh to %d[%d]\n", value
, wDevID
, MidiInDev
[wDevID
].incLen
);
467 if (wDevID
>= MIDM_NumDevs
) {
471 if (MidiInDev
[wDevID
].state
<= 0) {
472 TRACE("disabled or input not started, thrown away\n");
476 if (MidiInDev
[wDevID
].state
& 2) { /* system exclusive */
477 LPMIDIHDR lpMidiHdr
= MidiInDev
[wDevID
].lpQueueHdr
;
481 LPBYTE lpData
= lpMidiHdr
->lpData
;
483 lpData
[lpMidiHdr
->dwBytesRecorded
++] = value
;
484 if (lpMidiHdr
->dwBytesRecorded
== lpMidiHdr
->dwBufferLength
) {
488 if (value
== 0xF7) { /* then end */
489 MidiInDev
[wDevID
].state
&= ~2;
492 if (sbfb
&& lpMidiHdr
!= NULL
) {
493 lpMidiHdr
= MidiInDev
[wDevID
].lpQueueHdr
;
494 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
495 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
496 MidiInDev
[wDevID
].lpQueueHdr
= (LPMIDIHDR
)lpMidiHdr
->lpNext
;
497 if (MIDI_NotifyClient(wDevID
, MIM_LONGDATA
, (DWORD
)lpMidiHdr
, dwTime
) != MMSYSERR_NOERROR
) {
498 WARN("Couldn't notify client\n");
504 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
505 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
507 if (!IS_CMD(value
) && MidiInDev
[wDevID
].incLen
== 0) { /* try to reuse old cmd */
508 if (IS_CMD(MidiInDev
[wDevID
].incPrev
) && !IS_SYS_CMD(MidiInDev
[wDevID
].incPrev
)) {
509 MidiInDev
[wDevID
].incoming
[0] = MidiInDev
[wDevID
].incPrev
;
510 MidiInDev
[wDevID
].incLen
= 1;
511 TRACE("Reusing old command %02xh\n", MidiInDev
[wDevID
].incPrev
);
513 FIXME("error for midi-in, should generate MIM_ERROR notification:"
514 " prev=%02Xh, incLen=%02Xh\n",
515 MidiInDev
[wDevID
].incPrev
, MidiInDev
[wDevID
].incLen
);
519 MidiInDev
[wDevID
].incoming
[(int)(MidiInDev
[wDevID
].incLen
++)] = value
;
520 if (MidiInDev
[wDevID
].incLen
== 1 && !IS_SYS_CMD(MidiInDev
[wDevID
].incoming
[0])) {
521 /* store new cmd, just in case */
522 MidiInDev
[wDevID
].incPrev
= MidiInDev
[wDevID
].incoming
[0];
528 switch (MidiInDev
[wDevID
].incoming
[0] & 0xF0) {
531 case MIDI_KEY_PRESSURE
:
532 case MIDI_CTL_CHANGE
:
533 case MIDI_PITCH_BEND
:
534 if (MidiInDev
[wDevID
].incLen
== 3) {
535 toSend
= (MidiInDev
[wDevID
].incoming
[2] << 16) |
536 (MidiInDev
[wDevID
].incoming
[1] << 8) |
537 (MidiInDev
[wDevID
].incoming
[0] << 0);
540 case MIDI_PGM_CHANGE
:
541 case MIDI_CHN_PRESSURE
:
542 if (MidiInDev
[wDevID
].incLen
== 2) {
543 toSend
= (MidiInDev
[wDevID
].incoming
[1] << 8) |
544 (MidiInDev
[wDevID
].incoming
[0] << 0);
547 case MIDI_SYSTEM_PREFIX
:
548 if (MidiInDev
[wDevID
].incoming
[0] == 0xF0) {
549 MidiInDev
[wDevID
].state
|= 2;
550 MidiInDev
[wDevID
].incLen
= 0;
552 if (MidiInDev
[wDevID
].incLen
== 1) {
553 toSend
= (MidiInDev
[wDevID
].incoming
[0] << 0);
558 WARN("This shouldn't happen (%02X)\n", MidiInDev
[wDevID
].incoming
[0]);
561 TRACE("Sending event %08lx\n", toSend
);
562 MidiInDev
[wDevID
].incLen
= 0;
563 dwTime
-= MidiInDev
[wDevID
].startTime
;
564 if (MIDI_NotifyClient(wDevID
, MIM_DATA
, toSend
, dwTime
) != MMSYSERR_NOERROR
) {
565 WARN("Couldn't notify client\n");
570 static VOID WINAPI
midTimeCallback(HWND hwnd
, UINT msg
, UINT id
, DWORD dwTime
)
572 unsigned char buffer
[256];
575 TRACE("(%p, %d, %d, %lu)\n", hwnd
, msg
, id
, dwTime
);
577 len
= read(midiSeqFD
, buffer
, sizeof(buffer
));
580 if ((len
% 4) != 0) {
581 WARN("Bad length %d, errno %d (%s)\n", len
, errno
, strerror(errno
));
585 for (idx
= 0; idx
< len
; ) {
586 if (buffer
[idx
] & 0x80) {
588 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
589 buffer
[idx
+ 0], buffer
[idx
+ 1],
590 buffer
[idx
+ 2], buffer
[idx
+ 3],
591 buffer
[idx
+ 4], buffer
[idx
+ 5],
592 buffer
[idx
+ 6], buffer
[idx
+ 7]);
595 switch (buffer
[idx
+ 0]) {
600 midReceiveChar(buffer
[idx
+ 2], buffer
[idx
+ 1], dwTime
);
603 TRACE("Unsupported event %d\n", buffer
[idx
+ 0]);
611 /**************************************************************************
612 * midGetDevCaps [internal]
614 static DWORD
midGetDevCaps(WORD wDevID
, LPMIDIINCAPSA lpCaps
, DWORD dwSize
)
616 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpCaps
, dwSize
);
618 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
619 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
621 memcpy(lpCaps
, &MidiInDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
623 return MMSYSERR_NOERROR
;
626 /**************************************************************************
629 static DWORD
midOpen(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
631 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpDesc
, dwFlags
);
633 if (lpDesc
== NULL
) {
634 WARN("Invalid Parameter !\n");
635 return MMSYSERR_INVALPARAM
;
639 * how to check that content of lpDesc is correct ?
641 if (wDevID
>= MIDM_NumDevs
) {
642 WARN("wDevID too large (%u) !\n", wDevID
);
643 return MMSYSERR_BADDEVICEID
;
645 if (MidiInDev
[wDevID
].state
== -1) {
646 WARN("device disabled\n");
647 return MIDIERR_NODEVICE
;
649 if (MidiInDev
[wDevID
].midiDesc
.hMidi
!= 0) {
650 WARN("device already open !\n");
651 return MMSYSERR_ALLOCATED
;
653 if ((dwFlags
& MIDI_IO_STATUS
) != 0) {
654 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
655 dwFlags
&= ~MIDI_IO_STATUS
;
657 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
658 FIXME("Bad dwFlags\n");
659 return MMSYSERR_INVALFLAG
;
662 if (midiOpenSeq() < 0) {
663 return MMSYSERR_ERROR
;
666 if (numStartedMidiIn
++ == 0) {
667 midiInTimerID
= SetTimer(0, 0, 250, midTimeCallback
);
668 if (!midiInTimerID
) {
669 numStartedMidiIn
= 0;
670 WARN("Couldn't start timer for midi-in\n");
672 return MMSYSERR_ERROR
;
674 TRACE("Starting timer (%u) for midi-in\n", midiInTimerID
);
677 MidiInDev
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
679 MidiInDev
[wDevID
].lpQueueHdr
= NULL
;
680 MidiInDev
[wDevID
].dwTotalPlayed
= 0;
681 MidiInDev
[wDevID
].bufsize
= 0x3FFF;
682 MidiInDev
[wDevID
].midiDesc
= *lpDesc
;
683 MidiInDev
[wDevID
].state
= 0;
684 MidiInDev
[wDevID
].incLen
= 0;
685 MidiInDev
[wDevID
].startTime
= 0;
687 if (MIDI_NotifyClient(wDevID
, MIM_OPEN
, 0L, 0L) != MMSYSERR_NOERROR
) {
688 WARN("can't notify client !\n");
689 return MMSYSERR_INVALPARAM
;
691 return MMSYSERR_NOERROR
;
694 /**************************************************************************
695 * midClose [internal]
697 static DWORD
midClose(WORD wDevID
)
699 int ret
= MMSYSERR_NOERROR
;
701 TRACE("(%04X);\n", wDevID
);
703 if (wDevID
>= MIDM_NumDevs
) {
704 WARN("wDevID too big (%u) !\n", wDevID
);
705 return MMSYSERR_BADDEVICEID
;
707 if (MidiInDev
[wDevID
].midiDesc
.hMidi
== 0) {
708 WARN("device not opened !\n");
709 return MMSYSERR_ERROR
;
711 if (MidiInDev
[wDevID
].lpQueueHdr
!= 0) {
712 return MIDIERR_STILLPLAYING
;
715 if (midiSeqFD
== -1) {
717 return MMSYSERR_ERROR
;
719 if (--numStartedMidiIn
== 0) {
720 TRACE("Stopping timer for midi-in\n");
721 if (!KillTimer(0, midiInTimerID
)) {
722 WARN("Couldn't stop timer for midi-in\n");
728 MidiInDev
[wDevID
].bufsize
= 0;
729 if (MIDI_NotifyClient(wDevID
, MIM_CLOSE
, 0L, 0L) != MMSYSERR_NOERROR
) {
730 WARN("can't notify client !\n");
731 ret
= MMSYSERR_INVALPARAM
;
733 MidiInDev
[wDevID
].midiDesc
.hMidi
= 0;
737 /**************************************************************************
738 * midAddBuffer [internal]
740 static DWORD
midAddBuffer(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
742 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
744 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
745 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
747 if (lpMidiHdr
== NULL
) return MMSYSERR_INVALPARAM
;
748 if (sizeof(MIDIHDR
) > dwSize
) return MMSYSERR_INVALPARAM
;
749 if (lpMidiHdr
->dwBufferLength
== 0) return MMSYSERR_INVALPARAM
;
750 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) return MIDIERR_STILLPLAYING
;
751 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) return MIDIERR_UNPREPARED
;
753 if (MidiInDev
[wDevID
].lpQueueHdr
== 0) {
754 MidiInDev
[wDevID
].lpQueueHdr
= lpMidiHdr
;
758 for (ptr
= MidiInDev
[wDevID
].lpQueueHdr
;
760 ptr
= (LPMIDIHDR
)ptr
->lpNext
);
761 ptr
->lpNext
= (struct midihdr_tag
*)lpMidiHdr
;
763 return MMSYSERR_NOERROR
;
766 /**************************************************************************
767 * midPrepare [internal]
769 static DWORD
midPrepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
771 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
773 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0 ||
774 lpMidiHdr
->lpData
== 0 || lpMidiHdr
->dwFlags
!= 0 ||
775 lpMidiHdr
->dwBufferLength
>= 0x10000ul
)
776 return MMSYSERR_INVALPARAM
;
778 lpMidiHdr
->lpNext
= 0;
779 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
780 lpMidiHdr
->dwBytesRecorded
= 0;
782 return MMSYSERR_NOERROR
;
785 /**************************************************************************
786 * midUnprepare [internal]
788 static DWORD
midUnprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
790 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
792 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
793 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
795 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0 ||
796 lpMidiHdr
->lpData
== 0 || lpMidiHdr
->dwBufferLength
>= 0x10000ul
)
797 return MMSYSERR_INVALPARAM
;
799 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) return MIDIERR_UNPREPARED
;
800 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) return MIDIERR_STILLPLAYING
;
802 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
804 return MMSYSERR_NOERROR
;
807 /**************************************************************************
808 * midReset [internal]
810 static DWORD
midReset(WORD wDevID
)
812 DWORD dwTime
= GetTickCount();
814 TRACE("(%04X);\n", wDevID
);
816 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
817 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
819 while (MidiInDev
[wDevID
].lpQueueHdr
) {
820 MidiInDev
[wDevID
].lpQueueHdr
->dwFlags
&= ~MHDR_INQUEUE
;
821 MidiInDev
[wDevID
].lpQueueHdr
->dwFlags
|= MHDR_DONE
;
822 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
823 if (MIDI_NotifyClient(wDevID
, MIM_LONGDATA
,
824 (DWORD
)MidiInDev
[wDevID
].lpQueueHdr
, dwTime
) != MMSYSERR_NOERROR
) {
825 WARN("Couldn't notify client\n");
827 MidiInDev
[wDevID
].lpQueueHdr
= (LPMIDIHDR
)MidiInDev
[wDevID
].lpQueueHdr
->lpNext
;
830 return MMSYSERR_NOERROR
;
834 /**************************************************************************
835 * midStart [internal]
837 static DWORD
midStart(WORD wDevID
)
839 TRACE("(%04X);\n", wDevID
);
841 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
842 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
844 MidiInDev
[wDevID
].state
= 1;
845 MidiInDev
[wDevID
].startTime
= GetTickCount();
846 return MMSYSERR_NOERROR
;
849 /**************************************************************************
852 static DWORD
midStop(WORD wDevID
)
854 TRACE("(%04X);\n", wDevID
);
856 if (wDevID
>= MIDM_NumDevs
) return MMSYSERR_BADDEVICEID
;
857 if (MidiInDev
[wDevID
].state
== -1) return MIDIERR_NODEVICE
;
859 MidiInDev
[wDevID
].state
= 0;
860 return MMSYSERR_NOERROR
;
863 /*-----------------------------------------------------------------------*/
865 typedef struct sVoice
{
866 int note
; /* 0 means not used */
868 unsigned cntMark
: 30,
871 #define sVS_PLAYING 1
872 #define sVS_SUSTAINED 2
875 typedef struct sChannel
{
881 int bank
; /* CTL_BANK_SELECT */
882 int volume
; /* CTL_MAIN_VOLUME */
883 int balance
; /* CTL_BALANCE */
884 int expression
; /* CTL_EXPRESSION */
885 int sustain
; /* CTL_SUSTAIN */
887 unsigned char nrgPmtMSB
; /* Non register Parameters */
888 unsigned char nrgPmtLSB
;
889 unsigned char regPmtMSB
; /* Non register Parameters */
890 unsigned char regPmtLSB
;
893 typedef struct sFMextra
{
896 sChannel channel
[16]; /* MIDI has only 16 channels */
897 sVoice voice
[1]; /* dyn allocated according to sound card */
898 /* do not append fields below voice[1] since the size of this structure
899 * depends on the number of available voices on the FM synth...
903 extern unsigned char midiFMInstrumentPatches
[16 * 128];
904 extern unsigned char midiFMDrumsPatches
[16 * 128];
906 /**************************************************************************
907 * modFMLoad [internal]
909 static int modFMLoad(int dev
)
912 struct sbi_instrument sbi
;
917 memset(sbi
.operators
+ 16, 0, 16);
918 for (i
= 0; i
< 128; i
++) {
920 memcpy(sbi
.operators
, midiFMInstrumentPatches
+ i
* 16, 16);
922 if (write(midiSeqFD
, (char*)&sbi
, sizeof(sbi
)) == -1) {
923 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi
.channel
, errno
, strerror(errno
));
927 for (i
= 0; i
< 128; i
++) {
928 sbi
.channel
= 128 + i
;
929 memcpy(sbi
.operators
, midiFMDrumsPatches
+ i
* 16, 16);
931 if (write(midiSeqFD
, (char*)&sbi
, sizeof(sbi
)) == -1) {
932 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi
.channel
, errno
, strerror(errno
));
939 /**************************************************************************
940 * modFMReset [internal]
942 static void modFMReset(WORD wDevID
)
944 sFMextra
* extra
= (sFMextra
*)MidiOutDev
[wDevID
].lpExtra
;
945 sVoice
* voice
= extra
->voice
;
946 sChannel
* channel
= extra
->channel
;
949 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
950 if (voice
[i
].status
!= sVS_UNUSED
) {
951 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
953 SEQ_KEY_PRESSURE(wDevID
, i
, 127, 0);
954 SEQ_CONTROL(wDevID
, i
, SEQ_VOLMODE
, VOL_METHOD_LINEAR
);
956 voice
[i
].channel
= -1;
957 voice
[i
].cntMark
= 0;
958 voice
[i
].status
= sVS_UNUSED
;
960 for (i
= 0; i
< 16; i
++) {
961 channel
[i
].program
= 0;
962 channel
[i
].bender
= 8192;
963 channel
[i
].benderRange
= 2;
965 channel
[i
].volume
= 127;
966 channel
[i
].balance
= 64;
967 channel
[i
].expression
= 0;
968 channel
[i
].sustain
= 0;
971 extra
->drumSetMask
= 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
975 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
977 /**************************************************************************
978 * modGetDevCaps [internal]
980 static DWORD
modGetDevCaps(WORD wDevID
, LPMIDIOUTCAPSA lpCaps
, DWORD dwSize
)
982 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpCaps
, dwSize
);
984 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
985 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
987 memcpy(lpCaps
, &MidiOutDev
[wDevID
].caps
, min(dwSize
, sizeof(*lpCaps
)));
989 return MMSYSERR_NOERROR
;
992 /**************************************************************************
995 static DWORD
modOpen(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
997 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpDesc
, dwFlags
);
998 if (lpDesc
== NULL
) {
999 WARN("Invalid Parameter !\n");
1000 return MMSYSERR_INVALPARAM
;
1002 if (wDevID
>= MODM_NumDevs
) {
1003 TRACE("MAX_MIDIOUTDRV reached !\n");
1004 return MMSYSERR_BADDEVICEID
;
1006 if (MidiOutDev
[wDevID
].midiDesc
.hMidi
!= 0) {
1007 WARN("device already open !\n");
1008 return MMSYSERR_ALLOCATED
;
1010 if (!MidiOutDev
[wDevID
].bEnabled
) {
1011 WARN("device disabled !\n");
1012 return MIDIERR_NODEVICE
;
1014 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
1015 WARN("bad dwFlags\n");
1016 return MMSYSERR_INVALFLAG
;
1018 if (!MidiOutDev
[wDevID
].bEnabled
) {
1019 TRACE("disabled wDevID\n");
1020 return MMSYSERR_NOTENABLED
;
1023 MidiOutDev
[wDevID
].lpExtra
= 0;
1025 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1030 extra
= HeapAlloc(GetProcessHeap(), 0,
1031 sizeof(struct sFMextra
) +
1032 sizeof(struct sVoice
) * (MidiOutDev
[wDevID
].caps
.wVoices
- 1));
1035 WARN("can't alloc extra data !\n");
1036 return MMSYSERR_NOMEM
;
1038 MidiOutDev
[wDevID
].lpExtra
= extra
;
1039 if (midiOpenSeq() < 0) {
1040 MidiOutDev
[wDevID
].lpExtra
= 0;
1041 HeapFree(GetProcessHeap(), 0, extra
);
1042 return MMSYSERR_ERROR
;
1044 if (modFMLoad(wDevID
) < 0) {
1046 MidiOutDev
[wDevID
].lpExtra
= 0;
1047 HeapFree(GetProcessHeap(), 0, extra
);
1048 return MMSYSERR_ERROR
;
1055 if (midiOpenSeq() < 0) {
1056 return MMSYSERR_ALLOCATED
;
1060 WARN("Technology not supported (yet) %d !\n",
1061 MidiOutDev
[wDevID
].caps
.wTechnology
);
1062 return MMSYSERR_NOTENABLED
;
1065 MidiOutDev
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1067 MidiOutDev
[wDevID
].lpQueueHdr
= NULL
;
1068 MidiOutDev
[wDevID
].dwTotalPlayed
= 0;
1069 MidiOutDev
[wDevID
].bufsize
= 0x3FFF;
1070 MidiOutDev
[wDevID
].midiDesc
= *lpDesc
;
1072 if (MIDI_NotifyClient(wDevID
, MOM_OPEN
, 0L, 0L) != MMSYSERR_NOERROR
) {
1073 WARN("can't notify client !\n");
1074 return MMSYSERR_INVALPARAM
;
1076 TRACE("Successful !\n");
1077 return MMSYSERR_NOERROR
;
1081 /**************************************************************************
1082 * modClose [internal]
1084 static DWORD
modClose(WORD wDevID
)
1086 int ret
= MMSYSERR_NOERROR
;
1088 TRACE("(%04X);\n", wDevID
);
1090 if (MidiOutDev
[wDevID
].midiDesc
.hMidi
== 0) {
1091 WARN("device not opened !\n");
1092 return MMSYSERR_ERROR
;
1094 /* FIXME: should test that no pending buffer is still in the queue for
1097 if (midiSeqFD
== -1) {
1098 WARN("can't close !\n");
1099 return MMSYSERR_ERROR
;
1102 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1108 WARN("Technology not supported (yet) %d !\n",
1109 MidiOutDev
[wDevID
].caps
.wTechnology
);
1110 return MMSYSERR_NOTENABLED
;
1113 if (MidiOutDev
[wDevID
].lpExtra
!= 0) {
1114 HeapFree(GetProcessHeap(), 0, MidiOutDev
[wDevID
].lpExtra
);
1115 MidiOutDev
[wDevID
].lpExtra
= 0;
1118 MidiOutDev
[wDevID
].bufsize
= 0;
1119 if (MIDI_NotifyClient(wDevID
, MOM_CLOSE
, 0L, 0L) != MMSYSERR_NOERROR
) {
1120 WARN("can't notify client !\n");
1121 ret
= MMSYSERR_INVALPARAM
;
1123 MidiOutDev
[wDevID
].midiDesc
.hMidi
= 0;
1127 /**************************************************************************
1128 * modData [internal]
1130 static DWORD
modData(WORD wDevID
, DWORD dwParam
)
1132 WORD evt
= LOBYTE(LOWORD(dwParam
));
1133 WORD d1
= HIBYTE(LOWORD(dwParam
));
1134 WORD d2
= LOBYTE(HIWORD(dwParam
));
1136 TRACE("(%04X, %08lX);\n", wDevID
, dwParam
);
1138 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1139 if (!MidiOutDev
[wDevID
].bEnabled
) return MIDIERR_NODEVICE
;
1141 if (midiSeqFD
== -1) {
1142 WARN("can't play !\n");
1143 return MIDIERR_NODEVICE
;
1145 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1148 * - chorus depth controller is not used
1151 sFMextra
* extra
= (sFMextra
*)MidiOutDev
[wDevID
].lpExtra
;
1152 sVoice
* voice
= extra
->voice
;
1153 sChannel
* channel
= extra
->channel
;
1154 int chn
= (evt
& 0x0F);
1157 switch (evt
& 0xF0) {
1159 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1160 /* don't stop sustained notes */
1161 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1162 voice
[i
].status
= sVS_UNUSED
;
1163 SEQ_STOP_NOTE(wDevID
, i
, d1
, d2
);
1168 if (d2
== 0) { /* note off if velocity == 0 */
1169 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1170 /* don't stop sustained notes */
1171 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1172 voice
[i
].status
= sVS_UNUSED
;
1173 SEQ_STOP_NOTE(wDevID
, i
, d1
, 64);
1178 /* finding out in this order :
1180 * - if replaying the same note on the same channel
1181 * - the older voice (LRU)
1183 for (i
= nv
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1184 if (voice
[i
].status
== sVS_UNUSED
||
1185 (voice
[i
].note
== d1
&& voice
[i
].channel
== chn
)) {
1189 if (voice
[i
].cntMark
< voice
[0].cntMark
) {
1194 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1195 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1196 nv
, channel
[chn
].program
,
1197 channel
[chn
].balance
,
1198 channel
[chn
].volume
,
1199 channel
[chn
].bender
, d1
, d2
);
1201 SEQ_SET_PATCH(wDevID
, nv
, IS_DRUM_CHANNEL(extra
, chn
) ?
1202 (128 + d1
) : channel
[chn
].program
);
1203 SEQ_BENDER_RANGE(wDevID
, nv
, channel
[chn
].benderRange
* 100);
1204 SEQ_BENDER(wDevID
, nv
, channel
[chn
].bender
);
1205 SEQ_CONTROL(wDevID
, nv
, CTL_PAN
, channel
[chn
].balance
);
1206 SEQ_CONTROL(wDevID
, nv
, CTL_EXPRESSION
, channel
[chn
].expression
);
1208 /* FIXME: does not really seem to work on my SB card and
1209 * screws everything up... so lay it down
1211 SEQ_CONTROL(wDevID
, nv
, CTL_MAIN_VOLUME
, channel
[chn
].volume
);
1213 SEQ_START_NOTE(wDevID
, nv
, d1
, d2
);
1214 voice
[nv
].status
= channel
[chn
].sustain
? sVS_SUSTAINED
: sVS_PLAYING
;
1215 voice
[nv
].note
= d1
;
1216 voice
[nv
].channel
= chn
;
1217 voice
[nv
].cntMark
= extra
->counter
++;
1219 case MIDI_KEY_PRESSURE
:
1220 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1221 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1222 SEQ_KEY_PRESSURE(wDevID
, i
, d1
, d2
);
1226 case MIDI_CTL_CHANGE
:
1228 case CTL_BANK_SELECT
: channel
[chn
].bank
= d2
; break;
1229 case CTL_MAIN_VOLUME
: channel
[chn
].volume
= d2
; break;
1230 case CTL_PAN
: channel
[chn
].balance
= d2
; break;
1231 case CTL_EXPRESSION
: channel
[chn
].expression
= d2
; break;
1232 case CTL_SUSTAIN
: channel
[chn
].sustain
= d2
;
1234 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1235 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
) {
1236 voice
[i
].status
= sVS_SUSTAINED
;
1240 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1241 if (voice
[i
].status
== sVS_SUSTAINED
&& voice
[i
].channel
== chn
) {
1242 voice
[i
].status
= sVS_UNUSED
;
1243 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1248 case CTL_NONREG_PARM_NUM_LSB
: channel
[chn
].nrgPmtLSB
= d2
; break;
1249 case CTL_NONREG_PARM_NUM_MSB
: channel
[chn
].nrgPmtMSB
= d2
; break;
1250 case CTL_REGIST_PARM_NUM_LSB
: channel
[chn
].regPmtLSB
= d2
; break;
1251 case CTL_REGIST_PARM_NUM_MSB
: channel
[chn
].regPmtMSB
= d2
; break;
1252 case CTL_DATA_ENTRY
:
1253 switch ((channel
[chn
].regPmtMSB
<< 8) | channel
[chn
].regPmtLSB
) {
1255 if (channel
[chn
].benderRange
!= d2
) {
1256 channel
[chn
].benderRange
= d2
;
1257 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1258 if (voice
[i
].channel
== chn
) {
1259 SEQ_BENDER_RANGE(wDevID
, i
, channel
[chn
].benderRange
);
1266 channel
[chn
].benderRange
= 2;
1267 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1268 if (voice
[i
].channel
== chn
) {
1269 SEQ_BENDER_RANGE(wDevID
, i
, channel
[chn
].benderRange
);
1274 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1275 channel
[chn
].regPmtMSB
, channel
[chn
].regPmtLSB
,
1276 channel
[chn
].nrgPmtMSB
, channel
[chn
].nrgPmtLSB
,
1282 case 0x78: /* all sounds off */
1283 /* FIXME: I don't know if I have to take care of the channel
1284 * for this control ?
1286 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1287 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
) {
1288 voice
[i
].status
= sVS_UNUSED
;
1289 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1293 case 0x7B: /* all notes off */
1294 /* FIXME: I don't know if I have to take care of the channel
1295 * for this control ?
1297 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1298 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
) {
1299 voice
[i
].status
= sVS_UNUSED
;
1300 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1305 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1310 case MIDI_PGM_CHANGE
:
1311 channel
[chn
].program
= d1
;
1313 case MIDI_CHN_PRESSURE
:
1314 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1315 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
) {
1316 SEQ_KEY_PRESSURE(wDevID
, i
, voice
[i
].note
, d1
);
1320 case MIDI_PITCH_BEND
:
1321 channel
[chn
].bender
= (d2
<< 7) + d1
;
1322 for (i
= 0; i
< MidiOutDev
[wDevID
].caps
.wVoices
; i
++) {
1323 if (voice
[i
].channel
== chn
) {
1324 SEQ_BENDER(wDevID
, i
, channel
[chn
].bender
);
1328 case MIDI_SYSTEM_PREFIX
:
1329 switch (evt
& 0x0F) {
1330 case 0x0F: /* Reset */
1334 WARN("Unsupported (yet) system event %02x\n", evt
& 0x0F);
1338 WARN("Internal error, shouldn't happen (event=%08x)\n", evt
& 0xF0);
1339 return MMSYSERR_NOTENABLED
;
1345 int dev
= wDevID
- MODM_NumFMSynthDevs
;
1347 WARN("Internal error on devID (%u) !\n", wDevID
);
1348 return MIDIERR_NODEVICE
;
1351 switch (evt
& 0xF0) {
1354 case MIDI_KEY_PRESSURE
:
1355 case MIDI_CTL_CHANGE
:
1356 case MIDI_PITCH_BEND
:
1357 SEQ_MIDIOUT(dev
, evt
);
1358 SEQ_MIDIOUT(dev
, d1
);
1359 SEQ_MIDIOUT(dev
, d2
);
1361 case MIDI_PGM_CHANGE
:
1362 case MIDI_CHN_PRESSURE
:
1363 SEQ_MIDIOUT(dev
, evt
);
1364 SEQ_MIDIOUT(dev
, d1
);
1366 case MIDI_SYSTEM_PREFIX
:
1367 switch (evt
& 0x0F) {
1368 case 0x00: /* System Exclusive, don't do it on modData,
1369 * should require modLongData*/
1370 case 0x01: /* Undefined */
1371 case 0x04: /* Undefined. */
1372 case 0x05: /* Undefined. */
1373 case 0x07: /* End of Exclusive. */
1374 case 0x09: /* Undefined. */
1375 case 0x0D: /* Undefined. */
1377 case 0x06: /* Tune Request */
1378 case 0x08: /* Timing Clock. */
1379 case 0x0A: /* Start. */
1380 case 0x0B: /* Continue */
1381 case 0x0C: /* Stop */
1382 case 0x0E: /* Active Sensing. */
1383 SEQ_MIDIOUT(dev
, evt
);
1385 case 0x0F: /* Reset */
1386 /* SEQ_MIDIOUT(dev, evt);
1387 this other way may be better */
1388 SEQ_MIDIOUT(dev
, MIDI_SYSTEM_PREFIX
);
1389 SEQ_MIDIOUT(dev
, 0x7e);
1390 SEQ_MIDIOUT(dev
, 0x7f);
1391 SEQ_MIDIOUT(dev
, 0x09);
1392 SEQ_MIDIOUT(dev
, 0x01);
1393 SEQ_MIDIOUT(dev
, 0xf7);
1395 case 0x03: /* Song Select. */
1396 SEQ_MIDIOUT(dev
, evt
);
1397 SEQ_MIDIOUT(dev
, d1
);
1398 case 0x02: /* Song Position Pointer. */
1399 SEQ_MIDIOUT(dev
, evt
);
1400 SEQ_MIDIOUT(dev
, d1
);
1401 SEQ_MIDIOUT(dev
, d2
);
1408 WARN("Technology not supported (yet) %d !\n",
1409 MidiOutDev
[wDevID
].caps
.wTechnology
);
1410 return MMSYSERR_NOTENABLED
;
1415 return MMSYSERR_NOERROR
;
1418 /**************************************************************************
1419 * modLongData [internal]
1421 static DWORD
modLongData(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1426 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
1428 /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
1429 * but it seems to be used only for midi input.
1430 * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
1433 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1434 if (!MidiOutDev
[wDevID
].bEnabled
) return MIDIERR_NODEVICE
;
1436 if (midiSeqFD
== -1) {
1437 WARN("can't play !\n");
1438 return MIDIERR_NODEVICE
;
1441 lpData
= lpMidiHdr
->lpData
;
1444 return MIDIERR_UNPREPARED
;
1445 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
1446 return MIDIERR_UNPREPARED
;
1447 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
1448 return MIDIERR_STILLPLAYING
;
1449 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
1450 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
1452 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1453 * data, or can it also contain raw MIDI data, to be split up and sent to
1455 * If the latest is true, then the following WARNing will fire up
1457 if (lpData
[0] != 0xF0 || lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
1458 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1461 TRACE("dwBufferLength=%lu !\n", lpMidiHdr
->dwBufferLength
);
1462 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1463 lpData
[0], lpData
[1], lpData
[2], lpData
[lpMidiHdr
->dwBufferLength
-3],
1464 lpData
[lpMidiHdr
->dwBufferLength
-2], lpData
[lpMidiHdr
->dwBufferLength
-1]);
1466 switch (MidiOutDev
[wDevID
].caps
.wTechnology
) {
1468 /* FIXME: I don't think there is much to do here */
1471 if (lpData
[0] != 0xF0) {
1472 /* Send end of System Exclusive */
1473 SEQ_MIDIOUT(wDevID
- MODM_NumFMSynthDevs
, 0xF0);
1474 WARN("Adding missing 0xF0 marker at the beginning of "
1475 "system exclusive byte stream\n");
1477 for (count
= 0; count
< lpMidiHdr
->dwBufferLength
; count
++) {
1478 SEQ_MIDIOUT(wDevID
- MODM_NumFMSynthDevs
, lpData
[count
]);
1480 if (lpData
[count
- 1] != 0xF7) {
1481 /* Send end of System Exclusive */
1482 SEQ_MIDIOUT(wDevID
- MODM_NumFMSynthDevs
, 0xF7);
1483 WARN("Adding missing 0xF7 marker at the end of "
1484 "system exclusive byte stream\n");
1489 WARN("Technology not supported (yet) %d !\n",
1490 MidiOutDev
[wDevID
].caps
.wTechnology
);
1491 return MMSYSERR_NOTENABLED
;
1494 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
1495 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
1496 if (MIDI_NotifyClient(wDevID
, MOM_DONE
, (DWORD
)lpMidiHdr
, 0L) != MMSYSERR_NOERROR
) {
1497 WARN("can't notify client !\n");
1498 return MMSYSERR_INVALPARAM
;
1500 return MMSYSERR_NOERROR
;
1503 /**************************************************************************
1504 * modPrepare [internal]
1506 static DWORD
modPrepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1508 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
1510 if (midiSeqFD
== -1) {
1511 WARN("can't prepare !\n");
1512 return MMSYSERR_NOTENABLED
;
1515 /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
1516 * asks to prepare MIDIHDR which dwFlags != 0.
1517 * So at least check for the inqueue flag
1519 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0 ||
1520 lpMidiHdr
->lpData
== 0 || (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) != 0 ||
1521 lpMidiHdr
->dwBufferLength
>= 0x10000ul
) {
1522 WARN("%p %p %08lx %d/%ld\n", lpMidiHdr
, lpMidiHdr
->lpData
,
1523 lpMidiHdr
->dwFlags
, sizeof(MIDIHDR
), dwSize
);
1524 return MMSYSERR_INVALPARAM
;
1527 lpMidiHdr
->lpNext
= 0;
1528 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
1529 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
1530 return MMSYSERR_NOERROR
;
1533 /**************************************************************************
1534 * modUnprepare [internal]
1536 static DWORD
modUnprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1538 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
1540 if (midiSeqFD
== -1) {
1541 WARN("can't unprepare !\n");
1542 return MMSYSERR_NOTENABLED
;
1545 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0)
1546 return MMSYSERR_INVALPARAM
;
1547 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
1548 return MIDIERR_STILLPLAYING
;
1549 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
1550 return MMSYSERR_NOERROR
;
1553 /**************************************************************************
1554 * modReset [internal]
1556 static DWORD
modReset(WORD wDevID
)
1560 TRACE("(%04X);\n", wDevID
);
1562 if (wDevID
>= MODM_NumDevs
) return MMSYSERR_BADDEVICEID
;
1563 if (!MidiOutDev
[wDevID
].bEnabled
) return MIDIERR_NODEVICE
;
1565 /* stop all notes */
1566 /* FIXME: check if 0x78B0 is channel dependent or not. I coded it so that
1567 * it's channel dependent...
1569 for (chn
= 0; chn
< 16; chn
++) {
1570 /* turn off every note */
1571 modData(wDevID
, 0x7800 | MIDI_CTL_CHANGE
| chn
);
1572 /* remove sustain on all channels */
1573 modData(wDevID
, (CTL_SUSTAIN
<< 8) | MIDI_CTL_CHANGE
| chn
);
1575 /* FIXME: the LongData buffers must also be returned to the app */
1576 return MMSYSERR_NOERROR
;
1579 #endif /* HAVE_OSS_MIDI */
1581 /*======================================================================*
1582 * MIDI entry points *
1583 *======================================================================*/
1585 /**************************************************************************
1586 * midMessage (WINEOSS.4)
1588 DWORD WINAPI
OSS_midMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1589 DWORD dwParam1
, DWORD dwParam2
)
1591 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1592 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1594 #ifdef HAVE_OSS_MIDI
1598 /* FIXME: Pretend this is supported */
1601 return midOpen(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1603 return midClose(wDevID
);
1604 case MIDM_ADDBUFFER
:
1605 return midAddBuffer(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1607 return midPrepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1608 case MIDM_UNPREPARE
:
1609 return midUnprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1610 case MIDM_GETDEVCAPS
:
1611 return midGetDevCaps(wDevID
, (LPMIDIINCAPSA
)dwParam1
,dwParam2
);
1612 case MIDM_GETNUMDEVS
:
1613 return MIDM_NumDevs
;
1615 return midReset(wDevID
);
1617 return midStart(wDevID
);
1619 return midStop(wDevID
);
1622 TRACE("Unsupported message\n");
1624 return MMSYSERR_NOTSUPPORTED
;
1627 /**************************************************************************
1628 * modMessage (WINEOSS.5)
1630 DWORD WINAPI
OSS_modMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1631 DWORD dwParam1
, DWORD dwParam2
)
1633 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1634 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1637 #ifdef HAVE_OSS_MIDI
1642 /* FIXME: Pretend this is supported */
1645 return modOpen(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1647 return modClose(wDevID
);
1649 return modData(wDevID
, dwParam1
);
1651 return modLongData(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1653 return modPrepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1654 case MODM_UNPREPARE
:
1655 return modUnprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1656 case MODM_GETDEVCAPS
:
1657 return modGetDevCaps(wDevID
, (LPMIDIOUTCAPSA
)dwParam1
, dwParam2
);
1658 case MODM_GETNUMDEVS
:
1659 return MODM_NumDevs
;
1660 case MODM_GETVOLUME
:
1662 case MODM_SETVOLUME
:
1665 return modReset(wDevID
);
1668 TRACE("Unsupported message\n");
1670 return MMSYSERR_NOTSUPPORTED
;
1673 /*-----------------------------------------------------------------------*/