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
20 #include <sys/ioctl.h>
28 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(midi
);
34 #define MIDI_SEQ "/dev/sequencer"
39 MIDIOPENDESC midiDesc
;
43 unsigned char incoming
[3];
44 unsigned char incPrev
;
52 MIDIOPENDESC midiDesc
;
56 void* lpExtra
; /* according to port type (MIDI, FM...), extra data when needed */
59 static WINE_MIDIIN MidiInDev
[MAX_MIDIINDRV
];
60 static WINE_MIDIOUT MidiOutDev
[MAX_MIDIOUTDRV
];
62 /* this is the total number of MIDI out devices found */
63 static int MODM_NUMDEVS
= 0;
64 /* this is the number of FM synthetizers (index from 0 to
65 NUMFMSYNTHDEVS - 1) */
66 static int MODM_NUMFMSYNTHDEVS
= 0;
67 /* this is the number of Midi ports (index from NUMFMSYNTHDEVS to
68 NUMFMSYNTHDEVS + NUMMIDIDEVS - 1) */
69 static int MODM_NUMMIDIDEVS
= 0;
71 /* this is the total number of MIDI out devices found */
72 static int MIDM_NUMDEVS
= 0;
74 static int midiSeqFD
= -1;
75 static int numOpenMidiSeq
= 0;
76 static UINT midiInTimerID
= 0;
77 static int numStartedMidiIn
= 0;
79 /* this structure holds pointers with information for each MIDI
82 static LPMIDIOUTCAPSA midiOutDevices
[MAX_MIDIOUTDRV
];
84 /* this structure holds pointers with information for each MIDI
87 static LPMIDIINCAPSA midiInDevices
[MAX_MIDIINDRV
];
90 * FIXME : all tests on device ID for midXXX and modYYY are made against
91 * MAX_MIDIxxDRV (when they are made) but should be done against the actual
92 * number of midi devices found...
95 /*======================================================================*
96 * Low level MIDI implementation *
97 *======================================================================*/
99 static int midiOpenSeq(void);
100 static int midiCloseSeq(void);
102 /**************************************************************************
103 * unixToWindowsDeviceType [internal]
105 * return the Windows equivalent to a Unix Device Type
108 static int MIDI_UnixToWindowsDeviceType(int type
)
110 /* MOD_MIDIPORT output port
111 * MOD_SYNTH generic internal synth
112 * MOD_SQSYNTH square wave internal synth
113 * MOD_FMSYNTH FM internal synth
114 * MOD_MAPPER MIDI mapper
117 /* FIXME Is this really the correct equivalence from UNIX to
118 Windows Sound type */
121 case SYNTH_TYPE_FM
: return MOD_FMSYNTH
;
122 case SYNTH_TYPE_SAMPLE
: return MOD_SYNTH
;
123 case SYNTH_TYPE_MIDI
: return MOD_MIDIPORT
;
125 ERR("Cannot determine the type of this midi device. "
126 "Assuming FM Synth\n");
132 /**************************************************************************
133 * OSS_MidiInit [internal]
135 * Initializes the MIDI devices information variables
137 BOOL
OSS_MidiInit(void)
139 int i
, status
, numsynthdevs
= 255, nummididevs
= 255;
140 struct synth_info sinfo
;
141 struct midi_info minfo
;
142 static BOOL bInitDone
= FALSE
;
147 TRACE("Initializing the MIDI variables.\n");
150 /* try to open device */
151 if (midiOpenSeq() == -1) {
155 /* find how many Synth devices are there in the system */
156 status
= ioctl(midiSeqFD
, SNDCTL_SEQ_NRSYNTHS
, &numsynthdevs
);
159 ERR("ioctl for nr synth failed.\n");
164 if (numsynthdevs
> MAX_MIDIOUTDRV
) {
165 ERR("MAX_MIDIOUTDRV (%d) was enough for the number of devices (%d). "
166 "Some FM devices will not be available.\n",MAX_MIDIOUTDRV
,numsynthdevs
);
167 numsynthdevs
= MAX_MIDIOUTDRV
;
170 for (i
= 0; i
< numsynthdevs
; i
++) {
171 LPMIDIOUTCAPSA tmplpCaps
;
174 status
= ioctl(midiSeqFD
, SNDCTL_SYNTH_INFO
, &sinfo
);
176 ERR("ioctl for synth info failed.\n");
181 tmplpCaps
= HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA
));
184 /* We also have the information sinfo.synth_subtype, not used here
187 /* Manufac ID. We do not have access to this with soundcard.h
188 * Does not seem to be a problem, because in mmsystem.h only
189 * Microsoft's ID is listed.
191 tmplpCaps
->wMid
= 0x00FF;
192 tmplpCaps
->wPid
= 0x0001; /* FIXME Product ID */
193 /* Product Version. We simply say "1" */
194 tmplpCaps
->vDriverVersion
= 0x001;
195 strcpy(tmplpCaps
->szPname
, sinfo
.name
);
197 tmplpCaps
->wTechnology
= MIDI_UnixToWindowsDeviceType(sinfo
.synth_type
);
198 tmplpCaps
->wVoices
= sinfo
.nr_voices
;
200 /* FIXME Is it possible to know the maximum
201 * number of simultaneous notes of a soundcard ?
202 * I believe we don't have this information, but
203 * it's probably equal or more than wVoices
205 tmplpCaps
->wNotes
= sinfo
.nr_voices
;
207 /* FIXME Do we have this information?
208 * Assuming the soundcards can handle
209 * MIDICAPS_VOLUME and MIDICAPS_LRVOLUME but
210 * not MIDICAPS_CACHE.
212 tmplpCaps
->dwSupport
= MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
;
214 midiOutDevices
[i
] = tmplpCaps
;
216 if (sinfo
.capabilities
& SYNTH_CAP_INPUT
) {
217 FIXME("Synthesizer support MIDI in. Not supported yet (please report)\n");
220 TRACE("name='%s', techn=%d voices=%d notes=%d support=%ld\n",
221 tmplpCaps
->szPname
, tmplpCaps
->wTechnology
,
222 tmplpCaps
->wVoices
, tmplpCaps
->wNotes
, tmplpCaps
->dwSupport
);
223 TRACE("OSS info: synth subtype=%d capa=%lx\n",
224 sinfo
.synth_subtype
, (long)sinfo
.capabilities
);
227 /* find how many MIDI devices are there in the system */
228 status
= ioctl(midiSeqFD
, SNDCTL_SEQ_NRMIDIS
, &nummididevs
);
230 ERR("ioctl on nr midi failed.\n");
235 /* FIXME: the two restrictions below could be loosen in some cases */
236 if (numsynthdevs
+ nummididevs
> MAX_MIDIOUTDRV
) {
237 ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
238 "Some MIDI devices will not be available.\n");
239 nummididevs
= MAX_MIDIOUTDRV
- numsynthdevs
;
242 if (nummididevs
> MAX_MIDIINDRV
) {
243 ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
244 "Some MIDI devices will not be available.\n",MAX_MIDIINDRV
,nummididevs
);
245 nummididevs
= MAX_MIDIINDRV
;
248 for (i
= 0; i
< nummididevs
; i
++) {
249 LPMIDIOUTCAPSA tmplpOutCaps
;
250 LPMIDIINCAPSA tmplpInCaps
;
253 status
= ioctl(midiSeqFD
, SNDCTL_MIDI_INFO
, &minfo
);
255 ERR("ioctl on midi info for device %d failed.\n", i
);
260 tmplpOutCaps
= HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA
));
263 /* This whole part is somewhat obscure to me. I'll keep trying to dig
264 info about it. If you happen to know, please tell us. The very
265 descritive minfo.dev_type was not used here.
267 /* Manufac ID. We do not have access to this with soundcard.h
268 Does not seem to be a problem, because in mmsystem.h only
269 Microsoft's ID is listed */
270 tmplpOutCaps
->wMid
= 0x00FF;
271 tmplpOutCaps
->wPid
= 0x0001; /* FIXME Product ID */
272 /* Product Version. We simply say "1" */
273 tmplpOutCaps
->vDriverVersion
= 0x001;
274 strcpy(tmplpOutCaps
->szPname
, minfo
.name
);
276 tmplpOutCaps
->wTechnology
= MOD_MIDIPORT
; /* FIXME Is this right? */
277 /* Does it make any difference? */
278 tmplpOutCaps
->wVoices
= 16;
279 /* Does it make any difference? */
280 tmplpOutCaps
->wNotes
= 16;
281 /* FIXME Does it make any difference? */
282 tmplpOutCaps
->dwSupport
= MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
;
284 midiOutDevices
[numsynthdevs
+ i
] = tmplpOutCaps
;
286 tmplpInCaps
= HeapAlloc(GetProcessHeap(), 0, sizeof(MIDIOUTCAPSA
));
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 tmplpInCaps
->wMid
= 0x00FF;
297 tmplpInCaps
->wPid
= 0x0001; /* FIXME Product ID */
298 /* Product Version. We simply say "1" */
299 tmplpInCaps
->vDriverVersion
= 0x001;
300 strcpy(tmplpInCaps
->szPname
, minfo
.name
);
302 /* FIXME : could we get better information than that ? */
303 tmplpInCaps
->dwSupport
= MIDICAPS_VOLUME
|MIDICAPS_LRVOLUME
;
305 midiInDevices
[i
] = tmplpInCaps
;
307 TRACE("name='%s' techn=%d voices=%d notes=%d support=%ld\n",
308 tmplpOutCaps
->szPname
, tmplpOutCaps
->wTechnology
, tmplpOutCaps
->wVoices
,
309 tmplpOutCaps
->wNotes
, tmplpOutCaps
->dwSupport
);
310 TRACE("OSS info: midi dev-type=%d, capa=%lx\n",
311 minfo
.dev_type
, (long)minfo
.capabilities
);
314 /* windows does not seem to differentiate Synth from MIDI devices */
315 MODM_NUMFMSYNTHDEVS
= numsynthdevs
;
316 MODM_NUMMIDIDEVS
= nummididevs
;
317 MODM_NUMDEVS
= numsynthdevs
+ nummididevs
;
319 MIDM_NUMDEVS
= nummididevs
;
321 /* close file and exit */
327 /**************************************************************************
328 * MIDI_NotifyClient [internal]
330 static DWORD
MIDI_NotifyClient(UINT wDevID
, WORD wMsg
,
331 DWORD dwParam1
, DWORD dwParam2
)
338 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",
339 wDevID
, wMsg
, dwParam1
, dwParam2
);
345 if (wDevID
> MAX_MIDIOUTDRV
)
346 return MCIERR_INTERNAL
;
348 dwCallBack
= MidiOutDev
[wDevID
].midiDesc
.dwCallback
;
349 uFlags
= MidiOutDev
[wDevID
].wFlags
;
350 hDev
= MidiOutDev
[wDevID
].midiDesc
.hMidi
;
351 dwInstance
= MidiOutDev
[wDevID
].midiDesc
.dwInstance
;
358 if (wDevID
> MAX_MIDIINDRV
)
359 return MCIERR_INTERNAL
;
361 dwCallBack
= MidiInDev
[wDevID
].midiDesc
.dwCallback
;
362 uFlags
= MidiInDev
[wDevID
].wFlags
;
363 hDev
= MidiInDev
[wDevID
].midiDesc
.hMidi
;
364 dwInstance
= MidiInDev
[wDevID
].midiDesc
.dwInstance
;
367 WARN("Unsupported MSW-MIDI message %u\n", wMsg
);
368 return MCIERR_INTERNAL
;
371 return DriverCallback(dwCallBack
, uFlags
, hDev
, wMsg
, dwInstance
, dwParam1
, dwParam2
) ?
375 static int midi_warn
= 1;
376 /**************************************************************************
377 * midiOpenSeq [internal]
379 static int midiOpenSeq(void)
381 if (numOpenMidiSeq
== 0) {
382 midiSeqFD
= open(MIDI_SEQ
, O_RDWR
, 0);
383 if (midiSeqFD
== -1) {
386 WARN("Can't open MIDI device '%s' ! (%s). If your "
387 "program needs this (probably not): %s\n",
388 MIDI_SEQ
, strerror(errno
),
390 "create it ! (\"man MAKEDEV\" ?)" :
392 "load MIDI sequencer kernel driver !" :
394 "grant access ! (\"man chmod\")" : ""
400 if (fcntl(midiSeqFD
, F_SETFL
, O_NONBLOCK
) < 0) {
401 WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno
, strerror(errno
));
406 fcntl(midiSeqFD
, F_SETFD
, 1); /* set close on exec flag */
407 ioctl(midiSeqFD
, SNDCTL_SEQ_RESET
);
413 /**************************************************************************
414 * midiCloseSeq [internal]
416 static int midiCloseSeq(void)
418 if (--numOpenMidiSeq
== 0) {
425 /* FIXME: this is a bad idea, it's even not static... */
428 /* FIXME: this is not reentrant, not static - because of global variable
431 /**************************************************************************
432 * seqbuf_dump [internal]
434 void seqbuf_dump(void)
437 if (write(midiSeqFD
, _seqbuf
, _seqbufptr
) == -1) {
438 WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
439 midiSeqFD
, errno
, strerror(errno
));
442 * in any case buffer is lost so that if many errors occur the buffer
449 static void midReceiveChar(WORD wDevID
, unsigned char value
, DWORD dwTime
)
453 TRACE("Adding %02xh to %d[%d]\n", value
, wDevID
, MidiInDev
[wDevID
].incLen
);
455 if (wDevID
>= MAX_MIDIINDRV
) {
459 if (MidiInDev
[wDevID
].state
== 0) {
460 TRACE("input not started, thrown away\n");
464 if (MidiInDev
[wDevID
].state
& 2) { /* system exclusive */
465 LPMIDIHDR lpMidiHdr
= MidiInDev
[wDevID
].lpQueueHdr
;
469 LPBYTE lpData
= lpMidiHdr
->lpData
;
471 lpData
[lpMidiHdr
->dwBytesRecorded
++] = value
;
472 if (lpMidiHdr
->dwBytesRecorded
== lpMidiHdr
->dwBufferLength
) {
476 if (value
== 0xF7) { /* then end */
477 MidiInDev
[wDevID
].state
&= ~2;
480 if (sbfb
&& lpMidiHdr
!= NULL
) {
481 lpMidiHdr
= MidiInDev
[wDevID
].lpQueueHdr
;
482 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
483 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
484 MidiInDev
[wDevID
].lpQueueHdr
= (LPMIDIHDR
)lpMidiHdr
->lpNext
;
485 if (MIDI_NotifyClient(wDevID
, MIM_LONGDATA
, (DWORD
)lpMidiHdr
, dwTime
) != MMSYSERR_NOERROR
) {
486 WARN("Couldn't notify client\n");
492 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
493 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
495 if (!IS_CMD(value
) && MidiInDev
[wDevID
].incLen
== 0) { /* try to reuse old cmd */
496 if (IS_CMD(MidiInDev
[wDevID
].incPrev
) && !IS_SYS_CMD(MidiInDev
[wDevID
].incPrev
)) {
497 MidiInDev
[wDevID
].incoming
[0] = MidiInDev
[wDevID
].incPrev
;
498 MidiInDev
[wDevID
].incLen
= 1;
499 TRACE("Reusing old command %02xh\n", MidiInDev
[wDevID
].incPrev
);
501 FIXME("error for midi-in, should generate MIM_ERROR notification:"
502 " prev=%02Xh, incLen=%02Xh\n",
503 MidiInDev
[wDevID
].incPrev
, MidiInDev
[wDevID
].incLen
);
507 MidiInDev
[wDevID
].incoming
[(int)(MidiInDev
[wDevID
].incLen
++)] = value
;
508 if (MidiInDev
[wDevID
].incLen
== 1 && !IS_SYS_CMD(MidiInDev
[wDevID
].incoming
[0])) {
509 /* store new cmd, just in case */
510 MidiInDev
[wDevID
].incPrev
= MidiInDev
[wDevID
].incoming
[0];
516 switch (MidiInDev
[wDevID
].incoming
[0] & 0xF0) {
519 case MIDI_KEY_PRESSURE
:
520 case MIDI_CTL_CHANGE
:
521 case MIDI_PITCH_BEND
:
522 if (MidiInDev
[wDevID
].incLen
== 3) {
523 toSend
= (MidiInDev
[wDevID
].incoming
[2] << 16) |
524 (MidiInDev
[wDevID
].incoming
[1] << 8) |
525 (MidiInDev
[wDevID
].incoming
[0] << 0);
528 case MIDI_PGM_CHANGE
:
529 case MIDI_CHN_PRESSURE
:
530 if (MidiInDev
[wDevID
].incLen
== 2) {
531 toSend
= (MidiInDev
[wDevID
].incoming
[1] << 8) |
532 (MidiInDev
[wDevID
].incoming
[0] << 0);
535 case MIDI_SYSTEM_PREFIX
:
536 if (MidiInDev
[wDevID
].incoming
[0] == 0xF0) {
537 MidiInDev
[wDevID
].state
|= 2;
538 MidiInDev
[wDevID
].incLen
= 0;
540 if (MidiInDev
[wDevID
].incLen
== 1) {
541 toSend
= (MidiInDev
[wDevID
].incoming
[0] << 0);
546 WARN("This shouldn't happen (%02X)\n", MidiInDev
[wDevID
].incoming
[0]);
549 TRACE("Sending event %08lx\n", toSend
);
550 MidiInDev
[wDevID
].incLen
= 0;
551 dwTime
-= MidiInDev
[wDevID
].startTime
;
552 if (MIDI_NotifyClient(wDevID
, MIM_DATA
, toSend
, dwTime
) != MMSYSERR_NOERROR
) {
553 WARN("Couldn't notify client\n");
558 static VOID WINAPI
midTimeCallback(HWND hwnd
, UINT msg
, UINT id
, DWORD dwTime
)
560 unsigned char buffer
[256];
563 TRACE("(%04X, %d, %d, %lu)\n", hwnd
, msg
, id
, dwTime
);
565 len
= read(midiSeqFD
, buffer
, sizeof(buffer
));
568 if ((len
% 4) != 0) {
569 WARN("Bad length %d, errno %d (%s)\n", len
, errno
, strerror(errno
));
573 for (idx
= 0; idx
< len
; ) {
574 if (buffer
[idx
] & 0x80) {
576 "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
577 buffer
[idx
+ 0], buffer
[idx
+ 1],
578 buffer
[idx
+ 2], buffer
[idx
+ 3],
579 buffer
[idx
+ 4], buffer
[idx
+ 5],
580 buffer
[idx
+ 6], buffer
[idx
+ 7]);
583 switch (buffer
[idx
+ 0]) {
588 midReceiveChar(buffer
[idx
+ 2], buffer
[idx
+ 1], dwTime
);
591 TRACE("Unsupported event %d\n", buffer
[idx
+ 0]);
599 /**************************************************************************
600 * midGetDevCaps [internal]
602 static DWORD
midGetDevCaps(WORD wDevID
, LPMIDIINCAPSA lpCaps
, DWORD dwSize
)
604 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpCaps
, dwSize
);
606 if (wDevID
>= MIDM_NUMDEVS
) return MMSYSERR_BADDEVICEID
;
607 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
609 memcpy(lpCaps
, midiInDevices
[wDevID
], min(dwSize
, sizeof(*lpCaps
)));
611 return MMSYSERR_NOERROR
;
614 /**************************************************************************
617 static DWORD
midOpen(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
619 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpDesc
, dwFlags
);
621 if (lpDesc
== NULL
) {
622 WARN("Invalid Parameter !\n");
623 return MMSYSERR_INVALPARAM
;
627 * how to check that content of lpDesc is correct ?
629 if (wDevID
>= MAX_MIDIINDRV
) {
630 WARN("wDevID too large (%u) !\n", wDevID
);
631 return MMSYSERR_BADDEVICEID
;
633 if (MidiInDev
[wDevID
].midiDesc
.hMidi
!= 0) {
634 WARN("device already open !\n");
635 return MMSYSERR_ALLOCATED
;
637 if ((dwFlags
& MIDI_IO_STATUS
) != 0) {
638 WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
639 dwFlags
&= ~MIDI_IO_STATUS
;
641 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
642 FIXME("Bad dwFlags\n");
643 return MMSYSERR_INVALFLAG
;
646 if (midiOpenSeq() < 0) {
647 return MMSYSERR_ERROR
;
650 if (numStartedMidiIn
++ == 0) {
651 midiInTimerID
= SetTimer(0, 0, 250, midTimeCallback
);
652 if (!midiInTimerID
) {
653 numStartedMidiIn
= 0;
654 WARN("Couldn't start timer for midi-in\n");
656 return MMSYSERR_ERROR
;
658 TRACE("Starting timer (%u) for midi-in\n", midiInTimerID
);
661 MidiInDev
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
663 MidiInDev
[wDevID
].lpQueueHdr
= NULL
;
664 MidiInDev
[wDevID
].dwTotalPlayed
= 0;
665 MidiInDev
[wDevID
].bufsize
= 0x3FFF;
666 MidiInDev
[wDevID
].midiDesc
= *lpDesc
;
667 MidiInDev
[wDevID
].state
= 0;
668 MidiInDev
[wDevID
].incLen
= 0;
669 MidiInDev
[wDevID
].startTime
= 0;
671 if (MIDI_NotifyClient(wDevID
, MIM_OPEN
, 0L, 0L) != MMSYSERR_NOERROR
) {
672 WARN("can't notify client !\n");
673 return MMSYSERR_INVALPARAM
;
675 return MMSYSERR_NOERROR
;
678 /**************************************************************************
679 * midClose [internal]
681 static DWORD
midClose(WORD wDevID
)
683 int ret
= MMSYSERR_NOERROR
;
685 TRACE("(%04X);\n", wDevID
);
687 if (wDevID
>= MAX_MIDIINDRV
) {
688 WARN("wDevID too big (%u) !\n", wDevID
);
689 return MMSYSERR_BADDEVICEID
;
691 if (MidiInDev
[wDevID
].midiDesc
.hMidi
== 0) {
692 WARN("device not opened !\n");
693 return MMSYSERR_ERROR
;
695 if (MidiInDev
[wDevID
].lpQueueHdr
!= 0) {
696 return MIDIERR_STILLPLAYING
;
699 if (midiSeqFD
== -1) {
701 return MMSYSERR_ERROR
;
703 if (--numStartedMidiIn
== 0) {
704 TRACE("Stopping timer for midi-in\n");
705 if (!KillTimer(0, midiInTimerID
)) {
706 WARN("Couldn't stop timer for midi-in\n");
712 MidiInDev
[wDevID
].bufsize
= 0;
713 if (MIDI_NotifyClient(wDevID
, MIM_CLOSE
, 0L, 0L) != MMSYSERR_NOERROR
) {
714 WARN("can't notify client !\n");
715 ret
= MMSYSERR_INVALPARAM
;
717 MidiInDev
[wDevID
].midiDesc
.hMidi
= 0;
721 /**************************************************************************
722 * midAddBuffer [internal]
724 static DWORD
midAddBuffer(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
726 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
728 if (lpMidiHdr
== NULL
) return MMSYSERR_INVALPARAM
;
729 if (sizeof(MIDIHDR
) > dwSize
) return MMSYSERR_INVALPARAM
;
730 if (lpMidiHdr
->dwBufferLength
== 0) return MMSYSERR_INVALPARAM
;
731 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) return MIDIERR_STILLPLAYING
;
732 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) return MIDIERR_UNPREPARED
;
734 if (MidiInDev
[wDevID
].lpQueueHdr
== 0) {
735 MidiInDev
[wDevID
].lpQueueHdr
= lpMidiHdr
;
739 for (ptr
= MidiInDev
[wDevID
].lpQueueHdr
;
741 ptr
= (LPMIDIHDR
)ptr
->lpNext
);
742 ptr
->lpNext
= (struct midihdr_tag
*)lpMidiHdr
;
744 return MMSYSERR_NOERROR
;
747 /**************************************************************************
748 * midPrepare [internal]
750 static DWORD
midPrepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
752 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
754 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0 ||
755 lpMidiHdr
->lpData
== 0 || lpMidiHdr
->dwFlags
!= 0 ||
756 lpMidiHdr
->dwBufferLength
>= 0x10000ul
)
757 return MMSYSERR_INVALPARAM
;
759 lpMidiHdr
->lpNext
= 0;
760 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
761 lpMidiHdr
->dwBytesRecorded
= 0;
763 return MMSYSERR_NOERROR
;
766 /**************************************************************************
767 * midUnprepare [internal]
769 static DWORD
midUnprepare(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
->dwBufferLength
>= 0x10000ul
)
775 return MMSYSERR_INVALPARAM
;
777 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
)) return MIDIERR_UNPREPARED
;
778 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) return MIDIERR_STILLPLAYING
;
780 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
782 return MMSYSERR_NOERROR
;
785 /**************************************************************************
786 * midReset [internal]
788 static DWORD
midReset(WORD wDevID
)
790 DWORD dwTime
= GetTickCount();
792 TRACE("(%04X);\n", wDevID
);
794 while (MidiInDev
[wDevID
].lpQueueHdr
) {
795 MidiInDev
[wDevID
].lpQueueHdr
->dwFlags
&= ~MHDR_INQUEUE
;
796 MidiInDev
[wDevID
].lpQueueHdr
->dwFlags
|= MHDR_DONE
;
797 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
798 if (MIDI_NotifyClient(wDevID
, MIM_LONGDATA
,
799 (DWORD
)MidiInDev
[wDevID
].lpQueueHdr
, dwTime
) != MMSYSERR_NOERROR
) {
800 WARN("Couldn't notify client\n");
802 MidiInDev
[wDevID
].lpQueueHdr
= (LPMIDIHDR
)MidiInDev
[wDevID
].lpQueueHdr
->lpNext
;
805 return MMSYSERR_NOERROR
;
809 /**************************************************************************
810 * midStart [internal]
812 static DWORD
midStart(WORD wDevID
)
814 TRACE("(%04X);\n", wDevID
);
816 /* FIXME : should test value of wDevID */
818 MidiInDev
[wDevID
].state
= 1;
819 MidiInDev
[wDevID
].startTime
= GetTickCount();
820 return MMSYSERR_NOERROR
;
823 /**************************************************************************
826 static DWORD
midStop(WORD wDevID
)
828 TRACE("(%04X);\n", wDevID
);
830 /* FIXME : should test value of wDevID */
831 MidiInDev
[wDevID
].state
= 0;
832 return MMSYSERR_NOERROR
;
835 /*-----------------------------------------------------------------------*/
837 typedef struct sVoice
{
838 int note
; /* 0 means not used */
840 unsigned cntMark
: 30,
843 #define sVS_PLAYING 1
844 #define sVS_SUSTAINED 2
847 typedef struct sChannel
{
853 int bank
; /* CTL_BANK_SELECT */
854 int volume
; /* CTL_MAIN_VOLUME */
855 int balance
; /* CTL_BALANCE */
856 int expression
; /* CTL_EXPRESSION */
857 int sustain
; /* CTL_SUSTAIN */
859 unsigned char nrgPmtMSB
; /* Non register Parameters */
860 unsigned char nrgPmtLSB
;
861 unsigned char regPmtMSB
; /* Non register Parameters */
862 unsigned char regPmtLSB
;
865 typedef struct sFMextra
{
868 sChannel channel
[16]; /* MIDI has only 16 channels */
869 sVoice voice
[1]; /* dyn allocated according to sound card */
870 /* do not append fields below voice[1] since the size of this structure
871 * depends on the number of available voices on the FM synth...
875 extern unsigned char midiFMInstrumentPatches
[16 * 128];
876 extern unsigned char midiFMDrumsPatches
[16 * 128];
878 /**************************************************************************
879 * modFMLoad [internal]
881 static int modFMLoad(int dev
)
884 struct sbi_instrument sbi
;
889 memset(sbi
.operators
+ 16, 0, 16);
890 for (i
= 0; i
< 128; i
++) {
892 memcpy(sbi
.operators
, midiFMInstrumentPatches
+ i
* 16, 16);
894 if (write(midiSeqFD
, (char*)&sbi
, sizeof(sbi
)) == -1) {
895 WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi
.channel
, errno
, strerror(errno
));
899 for (i
= 0; i
< 128; i
++) {
900 sbi
.channel
= 128 + i
;
901 memcpy(sbi
.operators
, midiFMDrumsPatches
+ i
* 16, 16);
903 if (write(midiSeqFD
, (char*)&sbi
, sizeof(sbi
)) == -1) {
904 WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi
.channel
, errno
, strerror(errno
));
911 /**************************************************************************
912 * modFMReset [internal]
914 static void modFMReset(WORD wDevID
)
916 sFMextra
* extra
= (sFMextra
*)MidiOutDev
[wDevID
].lpExtra
;
917 sVoice
* voice
= extra
->voice
;
918 sChannel
* channel
= extra
->channel
;
921 for (i
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
922 if (voice
[i
].status
!= sVS_UNUSED
) {
923 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
925 SEQ_KEY_PRESSURE(wDevID
, i
, 127, 0);
926 SEQ_CONTROL(wDevID
, i
, SEQ_VOLMODE
, VOL_METHOD_LINEAR
);
928 voice
[i
].channel
= -1;
929 voice
[i
].cntMark
= 0;
930 voice
[i
].status
= sVS_UNUSED
;
932 for (i
= 0; i
< 16; i
++) {
933 channel
[i
].program
= 0;
934 channel
[i
].bender
= 8192;
935 channel
[i
].benderRange
= 2;
937 channel
[i
].volume
= 127;
938 channel
[i
].balance
= 64;
939 channel
[i
].expression
= 0;
940 channel
[i
].sustain
= 0;
943 extra
->drumSetMask
= 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
947 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
949 /**************************************************************************
950 * modGetDevCaps [internal]
952 static DWORD
modGetDevCaps(WORD wDevID
, LPMIDIOUTCAPSA lpCaps
, DWORD dwSize
)
954 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpCaps
, dwSize
);
956 if (wDevID
>= MODM_NUMDEVS
) return MMSYSERR_BADDEVICEID
;
957 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
959 memcpy(lpCaps
, midiOutDevices
[wDevID
], min(dwSize
, sizeof(*lpCaps
)));
961 return MMSYSERR_NOERROR
;
964 /**************************************************************************
967 static DWORD
modOpen(WORD wDevID
, LPMIDIOPENDESC lpDesc
, DWORD dwFlags
)
969 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpDesc
, dwFlags
);
970 if (lpDesc
== NULL
) {
971 WARN("Invalid Parameter !\n");
972 return MMSYSERR_INVALPARAM
;
974 if (wDevID
>= MAX_MIDIOUTDRV
) {
975 TRACE("MAX_MIDIOUTDRV reached !\n");
976 return MMSYSERR_BADDEVICEID
;
978 if (MidiOutDev
[wDevID
].midiDesc
.hMidi
!= 0) {
979 WARN("device already open !\n");
980 return MMSYSERR_ALLOCATED
;
982 if ((dwFlags
& ~CALLBACK_TYPEMASK
) != 0) {
983 WARN("bad dwFlags\n");
984 return MMSYSERR_INVALFLAG
;
986 if (midiOutDevices
[wDevID
] == NULL
) {
987 TRACE("un-allocated wDevID\n");
988 return MMSYSERR_BADDEVICEID
;
991 MidiOutDev
[wDevID
].lpExtra
= 0;
993 switch (midiOutDevices
[wDevID
]->wTechnology
) {
996 void* extra
= HeapAlloc(GetProcessHeap(), 0,
997 sizeof(struct sFMextra
) +
998 sizeof(struct sVoice
) * (midiOutDevices
[wDevID
]->wVoices
- 1));
1001 WARN("can't alloc extra data !\n");
1002 return MMSYSERR_NOMEM
;
1004 MidiOutDev
[wDevID
].lpExtra
= extra
;
1005 if (midiOpenSeq() < 0) {
1006 MidiOutDev
[wDevID
].lpExtra
= 0;
1007 HeapFree(GetProcessHeap(), 0, extra
);
1008 return MMSYSERR_ERROR
;
1010 if (modFMLoad(wDevID
) < 0) {
1012 MidiOutDev
[wDevID
].lpExtra
= 0;
1013 HeapFree(GetProcessHeap(), 0, extra
);
1014 return MMSYSERR_ERROR
;
1020 if (midiOpenSeq() < 0) {
1021 return MMSYSERR_ALLOCATED
;
1025 WARN("Technology not supported (yet) %d !\n",
1026 midiOutDevices
[wDevID
]->wTechnology
);
1027 return MMSYSERR_NOTENABLED
;
1030 MidiOutDev
[wDevID
].wFlags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
1032 MidiOutDev
[wDevID
].lpQueueHdr
= NULL
;
1033 MidiOutDev
[wDevID
].dwTotalPlayed
= 0;
1034 MidiOutDev
[wDevID
].bufsize
= 0x3FFF;
1035 MidiOutDev
[wDevID
].midiDesc
= *lpDesc
;
1037 if (MIDI_NotifyClient(wDevID
, MOM_OPEN
, 0L, 0L) != MMSYSERR_NOERROR
) {
1038 WARN("can't notify client !\n");
1039 return MMSYSERR_INVALPARAM
;
1041 TRACE("Successful !\n");
1042 return MMSYSERR_NOERROR
;
1046 /**************************************************************************
1047 * modClose [internal]
1049 static DWORD
modClose(WORD wDevID
)
1051 int ret
= MMSYSERR_NOERROR
;
1053 TRACE("(%04X);\n", wDevID
);
1055 if (MidiOutDev
[wDevID
].midiDesc
.hMidi
== 0) {
1056 WARN("device not opened !\n");
1057 return MMSYSERR_ERROR
;
1059 /* FIXME: should test that no pending buffer is still in the queue for
1062 if (midiSeqFD
== -1) {
1063 WARN("can't close !\n");
1064 return MMSYSERR_ERROR
;
1067 switch (midiOutDevices
[wDevID
]->wTechnology
) {
1073 WARN("Technology not supported (yet) %d !\n",
1074 midiOutDevices
[wDevID
]->wTechnology
);
1075 return MMSYSERR_NOTENABLED
;
1078 if (MidiOutDev
[wDevID
].lpExtra
!= 0) {
1079 HeapFree(GetProcessHeap(), 0, MidiOutDev
[wDevID
].lpExtra
);
1080 MidiOutDev
[wDevID
].lpExtra
= 0;
1083 MidiOutDev
[wDevID
].bufsize
= 0;
1084 if (MIDI_NotifyClient(wDevID
, MOM_CLOSE
, 0L, 0L) != MMSYSERR_NOERROR
) {
1085 WARN("can't notify client !\n");
1086 ret
= MMSYSERR_INVALPARAM
;
1088 MidiOutDev
[wDevID
].midiDesc
.hMidi
= 0;
1092 /**************************************************************************
1093 * modData [internal]
1095 static DWORD
modData(WORD wDevID
, DWORD dwParam
)
1097 WORD evt
= LOBYTE(LOWORD(dwParam
));
1098 WORD d1
= HIBYTE(LOWORD(dwParam
));
1099 WORD d2
= LOBYTE(HIWORD(dwParam
));
1101 TRACE("(%04X, %08lX);\n", wDevID
, dwParam
);
1103 if (midiSeqFD
== -1) {
1104 WARN("can't play !\n");
1105 return MIDIERR_NODEVICE
;
1107 switch (midiOutDevices
[wDevID
]->wTechnology
) {
1110 * - chorus depth controller is not used
1113 sFMextra
* extra
= (sFMextra
*)MidiOutDev
[wDevID
].lpExtra
;
1114 sVoice
* voice
= extra
->voice
;
1115 sChannel
* channel
= extra
->channel
;
1116 int chn
= (evt
& 0x0F);
1119 switch (evt
& 0xF0) {
1121 for (i
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
1122 /* don't stop sustained notes */
1123 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1124 voice
[i
].status
= sVS_UNUSED
;
1125 SEQ_STOP_NOTE(wDevID
, i
, d1
, d2
);
1130 if (d2
== 0) { /* note off if velocity == 0 */
1131 for (i
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
1132 /* don't stop sustained notes */
1133 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1134 voice
[i
].status
= sVS_UNUSED
;
1135 SEQ_STOP_NOTE(wDevID
, i
, d1
, 64);
1140 /* finding out in this order :
1142 * - if replaying the same note on the same channel
1143 * - the older voice (LRU)
1145 for (i
= nv
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
1146 if (voice
[i
].status
== sVS_UNUSED
||
1147 (voice
[i
].note
== d1
&& voice
[i
].channel
== chn
)) {
1151 if (voice
[i
].cntMark
< voice
[0].cntMark
) {
1156 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1157 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1158 nv
, channel
[chn
].program
,
1159 channel
[chn
].balance
,
1160 channel
[chn
].volume
,
1161 channel
[chn
].bender
, d1
, d2
);
1163 SEQ_SET_PATCH(wDevID
, nv
, IS_DRUM_CHANNEL(extra
, chn
) ?
1164 (128 + d1
) : channel
[chn
].program
);
1165 SEQ_BENDER_RANGE(wDevID
, nv
, channel
[chn
].benderRange
* 100);
1166 SEQ_BENDER(wDevID
, nv
, channel
[chn
].bender
);
1167 SEQ_CONTROL(wDevID
, nv
, CTL_PAN
, channel
[chn
].balance
);
1168 SEQ_CONTROL(wDevID
, nv
, CTL_EXPRESSION
, channel
[chn
].expression
);
1170 /* FIXME: does not really seem to work on my SB card and
1171 * screws everything up... so lay it down
1173 SEQ_CONTROL(wDevID
, nv
, CTL_MAIN_VOLUME
, channel
[chn
].volume
);
1175 SEQ_START_NOTE(wDevID
, nv
, d1
, d2
);
1176 voice
[nv
].status
= channel
[chn
].sustain
? sVS_SUSTAINED
: sVS_PLAYING
;
1177 voice
[nv
].note
= d1
;
1178 voice
[nv
].channel
= chn
;
1179 voice
[nv
].cntMark
= extra
->counter
++;
1181 case MIDI_KEY_PRESSURE
:
1182 for (i
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
1183 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
&& voice
[i
].note
== d1
) {
1184 SEQ_KEY_PRESSURE(wDevID
, i
, d1
, d2
);
1188 case MIDI_CTL_CHANGE
:
1190 case CTL_BANK_SELECT
: channel
[chn
].bank
= d2
; break;
1191 case CTL_MAIN_VOLUME
: channel
[chn
].volume
= d2
; break;
1192 case CTL_PAN
: channel
[chn
].balance
= d2
; break;
1193 case CTL_EXPRESSION
: channel
[chn
].expression
= d2
; break;
1194 case CTL_SUSTAIN
: channel
[chn
].sustain
= d2
;
1196 for (i
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
1197 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
) {
1198 voice
[i
].status
= sVS_SUSTAINED
;
1202 for (i
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
1203 if (voice
[i
].status
== sVS_SUSTAINED
&& voice
[i
].channel
== chn
) {
1204 voice
[i
].status
= sVS_UNUSED
;
1205 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1210 case CTL_NONREG_PARM_NUM_LSB
: channel
[chn
].nrgPmtLSB
= d2
; break;
1211 case CTL_NONREG_PARM_NUM_MSB
: channel
[chn
].nrgPmtMSB
= d2
; break;
1212 case CTL_REGIST_PARM_NUM_LSB
: channel
[chn
].regPmtLSB
= d2
; break;
1213 case CTL_REGIST_PARM_NUM_MSB
: channel
[chn
].regPmtMSB
= d2
; break;
1214 case CTL_DATA_ENTRY
:
1215 switch ((channel
[chn
].regPmtMSB
<< 8) | channel
[chn
].regPmtLSB
) {
1217 if (channel
[chn
].benderRange
!= d2
) {
1218 channel
[chn
].benderRange
= d2
;
1219 for (i
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
1220 if (voice
[i
].channel
== chn
) {
1221 SEQ_BENDER_RANGE(wDevID
, i
, channel
[chn
].benderRange
);
1228 channel
[chn
].benderRange
= 2;
1229 for (i
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
1230 if (voice
[i
].channel
== chn
) {
1231 SEQ_BENDER_RANGE(wDevID
, i
, channel
[chn
].benderRange
);
1236 TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1237 channel
[chn
].regPmtMSB
, channel
[chn
].regPmtLSB
,
1238 channel
[chn
].nrgPmtMSB
, channel
[chn
].nrgPmtLSB
,
1244 case 0x78: /* all sounds off */
1245 /* FIXME: I don't know if I have to take care of the channel
1246 * for this control ?
1248 for (i
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
1249 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
) {
1250 voice
[i
].status
= sVS_UNUSED
;
1251 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1255 case 0x7B: /* all notes off */
1256 /* FIXME: I don't know if I have to take care of the channel
1257 * for this control ?
1259 for (i
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
1260 if (voice
[i
].status
== sVS_PLAYING
&& voice
[i
].channel
== chn
) {
1261 voice
[i
].status
= sVS_UNUSED
;
1262 SEQ_STOP_NOTE(wDevID
, i
, voice
[i
].note
, 64);
1267 TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1272 case MIDI_PGM_CHANGE
:
1273 channel
[chn
].program
= d1
;
1275 case MIDI_CHN_PRESSURE
:
1276 for (i
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
1277 if (voice
[i
].status
!= sVS_UNUSED
&& voice
[i
].channel
== chn
) {
1278 SEQ_KEY_PRESSURE(wDevID
, i
, voice
[i
].note
, d1
);
1282 case MIDI_PITCH_BEND
:
1283 channel
[chn
].bender
= (d2
<< 7) + d1
;
1284 for (i
= 0; i
< midiOutDevices
[wDevID
]->wVoices
; i
++) {
1285 if (voice
[i
].channel
== chn
) {
1286 SEQ_BENDER(wDevID
, i
, channel
[chn
].bender
);
1290 case MIDI_SYSTEM_PREFIX
:
1291 switch (evt
& 0x0F) {
1292 case 0x0F: /* Reset */
1296 WARN("Unsupported (yet) system event %02x\n", evt
& 0x0F);
1300 WARN("Internal error, shouldn't happen (event=%08x)\n", evt
& 0xF0);
1301 return MMSYSERR_NOTENABLED
;
1307 int dev
= wDevID
- MODM_NUMFMSYNTHDEVS
;
1309 WARN("Internal error on devID (%u) !\n", wDevID
);
1310 return MIDIERR_NODEVICE
;
1313 switch (evt
& 0xF0) {
1316 case MIDI_KEY_PRESSURE
:
1317 case MIDI_CTL_CHANGE
:
1318 case MIDI_PITCH_BEND
:
1319 SEQ_MIDIOUT(dev
, evt
);
1320 SEQ_MIDIOUT(dev
, d1
);
1321 SEQ_MIDIOUT(dev
, d2
);
1323 case MIDI_PGM_CHANGE
:
1324 case MIDI_CHN_PRESSURE
:
1325 SEQ_MIDIOUT(dev
, evt
);
1326 SEQ_MIDIOUT(dev
, d1
);
1328 case MIDI_SYSTEM_PREFIX
:
1329 switch (evt
& 0x0F) {
1330 case 0x00: /* System Exclusive, don't do it on modData,
1331 * should require modLongData*/
1332 case 0x01: /* Undefined */
1333 case 0x04: /* Undefined. */
1334 case 0x05: /* Undefined. */
1335 case 0x07: /* End of Exclusive. */
1336 case 0x09: /* Undefined. */
1337 case 0x0D: /* Undefined. */
1339 case 0x06: /* Tune Request */
1340 case 0x08: /* Timing Clock. */
1341 case 0x0A: /* Start. */
1342 case 0x0B: /* Continue */
1343 case 0x0C: /* Stop */
1344 case 0x0E: /* Active Sensing. */
1345 SEQ_MIDIOUT(dev
, evt
);
1347 case 0x0F: /* Reset */
1348 /* SEQ_MIDIOUT(dev, evt);
1349 this other way may be better */
1350 SEQ_MIDIOUT(dev
, MIDI_SYSTEM_PREFIX
);
1351 SEQ_MIDIOUT(dev
, 0x7e);
1352 SEQ_MIDIOUT(dev
, 0x7f);
1353 SEQ_MIDIOUT(dev
, 0x09);
1354 SEQ_MIDIOUT(dev
, 0x01);
1355 SEQ_MIDIOUT(dev
, 0xf7);
1357 case 0x03: /* Song Select. */
1358 SEQ_MIDIOUT(dev
, evt
);
1359 SEQ_MIDIOUT(dev
, d1
);
1360 case 0x02: /* Song Position Pointer. */
1361 SEQ_MIDIOUT(dev
, evt
);
1362 SEQ_MIDIOUT(dev
, d1
);
1363 SEQ_MIDIOUT(dev
, d2
);
1370 WARN("Technology not supported (yet) %d !\n",
1371 midiOutDevices
[wDevID
]->wTechnology
);
1372 return MMSYSERR_NOTENABLED
;
1377 return MMSYSERR_NOERROR
;
1380 /**************************************************************************
1381 * modLongData [internal]
1383 static DWORD
modLongData(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1388 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
1390 if (midiSeqFD
== -1) {
1391 WARN("can't play !\n");
1392 return MIDIERR_NODEVICE
;
1395 lpData
= lpMidiHdr
->lpData
;
1398 return MIDIERR_UNPREPARED
;
1399 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
1400 return MIDIERR_UNPREPARED
;
1401 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
1402 return MIDIERR_STILLPLAYING
;
1403 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
1404 lpMidiHdr
->dwFlags
|= MHDR_INQUEUE
;
1406 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1407 * data, or can it also contain raw MIDI data, to be split up and sent to
1409 * If the latest is true, then the following WARNing will fire up
1411 if (lpData
[0] != 0xF0 || lpData
[lpMidiHdr
->dwBufferLength
- 1] != 0xF7) {
1412 WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1415 TRACE("dwBufferLength=%lu !\n", lpMidiHdr
->dwBufferLength
);
1416 TRACE(" %02X %02X %02X ... %02X %02X %02X\n",
1417 lpData
[0], lpData
[1], lpData
[2], lpData
[lpMidiHdr
->dwBufferLength
-3],
1418 lpData
[lpMidiHdr
->dwBufferLength
-2], lpData
[lpMidiHdr
->dwBufferLength
-1]);
1420 switch (midiOutDevices
[wDevID
]->wTechnology
) {
1422 /* FIXME: I don't think there is much to do here */
1425 if (lpData
[0] != 0xF0) {
1426 /* Send end of System Exclusive */
1427 SEQ_MIDIOUT(wDevID
- MODM_NUMFMSYNTHDEVS
, 0xF0);
1428 WARN("Adding missing 0xF0 marker at the beginning of "
1429 "system exclusive byte stream\n");
1431 for (count
= 0; count
< lpMidiHdr
->dwBytesRecorded
; count
++) {
1432 SEQ_MIDIOUT(wDevID
- MODM_NUMFMSYNTHDEVS
, lpData
[count
]);
1434 if (lpData
[count
- 1] != 0xF7) {
1435 /* Send end of System Exclusive */
1436 SEQ_MIDIOUT(wDevID
- MODM_NUMFMSYNTHDEVS
, 0xF7);
1437 WARN("Adding missing 0xF7 marker at the end of "
1438 "system exclusive byte stream\n");
1443 WARN("Technology not supported (yet) %d !\n",
1444 midiOutDevices
[wDevID
]->wTechnology
);
1445 return MMSYSERR_NOTENABLED
;
1448 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
1449 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
1450 if (MIDI_NotifyClient(wDevID
, MOM_DONE
, (DWORD
)lpMidiHdr
, 0L) != MMSYSERR_NOERROR
) {
1451 WARN("can't notify client !\n");
1452 return MMSYSERR_INVALPARAM
;
1454 return MMSYSERR_NOERROR
;
1457 /**************************************************************************
1458 * modPrepare [internal]
1460 static DWORD
modPrepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1462 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
1464 if (midiSeqFD
== -1) {
1465 WARN("can't prepare !\n");
1466 return MMSYSERR_NOTENABLED
;
1469 /* MS doc says taht dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
1470 * asks to prepare MIDIHDR which dwFlags != 0.
1471 * So at least check for the inqueue flag
1473 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0 ||
1474 lpMidiHdr
->lpData
== 0 || (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
) != 0 ||
1475 lpMidiHdr
->dwBufferLength
>= 0x10000ul
) {
1476 WARN("%p %p %08lx %d/%ld\n", lpMidiHdr
, lpMidiHdr
->lpData
,
1477 lpMidiHdr
->dwFlags
, sizeof(MIDIHDR
), dwSize
);
1478 return MMSYSERR_INVALPARAM
;
1481 lpMidiHdr
->lpNext
= 0;
1482 lpMidiHdr
->dwFlags
|= MHDR_PREPARED
;
1483 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
1484 return MMSYSERR_NOERROR
;
1487 /**************************************************************************
1488 * modUnprepare [internal]
1490 static DWORD
modUnprepare(WORD wDevID
, LPMIDIHDR lpMidiHdr
, DWORD dwSize
)
1492 TRACE("(%04X, %p, %08lX);\n", wDevID
, lpMidiHdr
, dwSize
);
1494 if (midiSeqFD
== -1) {
1495 WARN("can't unprepare !\n");
1496 return MMSYSERR_NOTENABLED
;
1499 if (dwSize
< sizeof(MIDIHDR
) || lpMidiHdr
== 0)
1500 return MMSYSERR_INVALPARAM
;
1501 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
1502 return MIDIERR_STILLPLAYING
;
1503 lpMidiHdr
->dwFlags
&= ~MHDR_PREPARED
;
1504 return MMSYSERR_NOERROR
;
1507 /**************************************************************************
1508 * modReset [internal]
1510 static DWORD
modReset(WORD wDevID
)
1514 TRACE("(%04X);\n", wDevID
);
1516 /* stop all notes */
1517 /* FIXME: check if 0x78B0 is channel dependant or not. I coded it so that
1518 * it's channel dependent...
1520 for (chn
= 0; chn
< 16; chn
++) {
1521 /* turn off every note */
1522 modData(wDevID
, 0x7800 | MIDI_CTL_CHANGE
| chn
);
1523 /* remove sustain on all channels */
1524 modData(wDevID
, (CTL_SUSTAIN
<< 8) | MIDI_CTL_CHANGE
| chn
);
1526 /* FIXME: the LongData buffers must also be returned to the app */
1527 return MMSYSERR_NOERROR
;
1530 #endif /* HAVE_OSS_MIDI */
1532 /*======================================================================*
1533 * MIDI entry points *
1534 *======================================================================*/
1536 /**************************************************************************
1537 * midMessage (WINEOSS.4)
1539 DWORD WINAPI
OSS_midMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1540 DWORD dwParam1
, DWORD dwParam2
)
1542 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1543 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1545 #ifdef HAVE_OSS_MIDI
1549 /* FIXME: Pretend this is supported */
1552 return midOpen(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1554 return midClose(wDevID
);
1555 case MIDM_ADDBUFFER
:
1556 return midAddBuffer(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1558 return midPrepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1559 case MIDM_UNPREPARE
:
1560 return midUnprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1561 case MIDM_GETDEVCAPS
:
1562 return midGetDevCaps(wDevID
, (LPMIDIINCAPSA
)dwParam1
,dwParam2
);
1563 case MIDM_GETNUMDEVS
:
1564 return MIDM_NUMDEVS
;
1566 return midReset(wDevID
);
1568 return midStart(wDevID
);
1570 return midStop(wDevID
);
1573 TRACE("Unsupported message\n");
1575 return MMSYSERR_NOTSUPPORTED
;
1578 /**************************************************************************
1579 * modMessage (WINEOSS.5)
1581 DWORD WINAPI
OSS_modMessage(UINT wDevID
, UINT wMsg
, DWORD dwUser
,
1582 DWORD dwParam1
, DWORD dwParam2
)
1584 TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1585 wDevID
, wMsg
, dwUser
, dwParam1
, dwParam2
);
1588 #ifdef HAVE_OSS_MIDI
1593 /* FIXME: Pretend this is supported */
1596 return modOpen(wDevID
, (LPMIDIOPENDESC
)dwParam1
, dwParam2
);
1598 return modClose(wDevID
);
1600 return modData(wDevID
, dwParam1
);
1602 return modLongData(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1604 return modPrepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1605 case MODM_UNPREPARE
:
1606 return modUnprepare(wDevID
, (LPMIDIHDR
)dwParam1
, dwParam2
);
1607 case MODM_GETDEVCAPS
:
1608 return modGetDevCaps(wDevID
, (LPMIDIOUTCAPSA
)dwParam1
, dwParam2
);
1609 case MODM_GETNUMDEVS
:
1610 return MODM_NUMDEVS
;
1611 case MODM_GETVOLUME
:
1613 case MODM_SETVOLUME
:
1616 return modReset(wDevID
);
1619 TRACE("Unsupported message\n");
1621 return MMSYSERR_NOTSUPPORTED
;
1624 /*-----------------------------------------------------------------------*/