midisyn.c revision 1.1
1/*	$NetBSD: midisyn.c,v 1.1 1998/08/12 18:14:01 augustss Exp $	*/
2
3/*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Author: Lennart Augustsson
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *        This product includes software developed by the NetBSD
20 *        Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 *    contributors may be used to endorse or promote products derived
23 *    from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <sys/param.h>
39#include <sys/ioctl.h>
40#include <sys/fcntl.h>
41#include <sys/vnode.h>
42#include <sys/select.h>
43#include <sys/proc.h>
44#include <sys/malloc.h>
45#include <sys/systm.h>
46#include <sys/syslog.h>
47#include <sys/kernel.h>
48#include <sys/conf.h>
49#include <sys/audioio.h>
50#include <sys/midiio.h>
51#include <sys/device.h>
52
53#include <dev/audio_if.h>
54#include <dev/midivar.h>
55#include <dev/midisynvar.h>
56
57#ifdef AUDIO_DEBUG
58#define DPRINTF(x)	if (midisyndebug) printf x
59#define DPRINTFN(n,x)	if (midisyndebug >= (n)) printf x
60int	midisyndebug = 0;
61#else
62#define DPRINTF(x)
63#define DPRINTFN(n,x)
64#endif
65
66int	midisyn_findvoice __P((midisyn *, int, int));
67void	midisyn_freevoice __P((midisyn *, int));
68int	midisyn_allocvoice __P((midisyn *, u_int32_t, u_int32_t));
69u_int32_t midisyn_note_to_freq __P((int));
70
71int	midisyn_open __P((void *, int,
72			  void (*iintr)__P((void *, int)),
73			  void (*ointr)__P((void *)), void *arg));
74void	midisyn_close __P((void *));
75int	midisyn_output __P((void *, int));
76void	midisyn_getinfo __P((void *, struct midi_info *));
77int	midisyn_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
78
79struct midi_hw_if midisyn_hw_if = {
80	midisyn_open,
81	midisyn_close,
82	midisyn_output,
83	midisyn_getinfo,
84	midisyn_ioctl,
85};
86
87static int midi_lengths[] = { 3,3,3,3,2,2,3,1 };
88/* Number of bytes in a MIDI command, including status */
89#define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7])
90
91int
92midisyn_open(addr, flags, iintr, ointr, arg)
93	void *addr;
94	int flags;
95	void (*iintr)__P((void *, int));
96	void (*ointr)__P((void *));
97	void *arg;
98{
99	midisyn *ms = addr;
100
101	DPRINTF(("midisyn_open: ms=%p ms->mets=%p\n", ms, ms->mets));
102	if (ms->mets->open)
103		return (ms->mets->open(ms, flags));
104	else
105		return (0);
106}
107
108void
109midisyn_close(addr)
110	void *addr;
111{
112	midisyn *ms = addr;
113
114	DPRINTF(("midisyn_close: ms=%p ms->mets=%p\n", ms, ms->mets));
115	if (ms->mets->close)
116		ms->mets->close(ms);
117}
118
119void
120midisyn_getinfo(addr, mi)
121	void *addr;
122	struct midi_info *mi;
123{
124	midisyn *ms = addr;
125
126	mi->name = ms->name;
127	mi->props = 0;
128}
129
130int
131midisyn_ioctl(maddr, cmd, addr, flag, p)
132	void *maddr;
133	u_long cmd;
134	caddr_t addr;
135	int flag;
136	struct proc *p;
137{
138	midisyn *ms = maddr;
139
140	if (ms->mets->ioctl)
141		return (ms->mets->ioctl(ms, cmd, addr, flag, p));
142	else
143		return (EINVAL);
144}
145
146int
147midisyn_findvoice(ms, chan, note)
148	midisyn *ms;
149	int chan, note;
150{
151	u_int cn;
152	int v;
153
154	if (!(ms->flags & MS_DOALLOC))
155		return (chan);
156	cn = CHANNOTE(chan, note);
157	for (v = 0; v < ms->nvoice; v++)
158		if (ms->voices[v].chan_note == cn)
159			return (v);
160	return (-1);
161}
162
163void
164midisyn_attach(sc, ms, parent)
165	struct midi_softc *sc;
166	midisyn *ms;
167	struct device *parent;
168{
169	if (ms->flags & MS_DOALLOC) {
170		ms->voices = malloc(ms->nvoice * sizeof (struct voice),
171				    M_DEVBUF, M_WAITOK);
172		memset(ms->voices, 0, ms->nvoice * sizeof (struct voice));
173		ms->seqno = 1;
174		if (ms->mets->allocv == 0)
175			ms->mets->allocv = &midisyn_allocvoice;
176	}
177	sc->hw_if = &midisyn_hw_if;
178	sc->hw_hdl = ms;
179	DPRINTF(("midisyn_attach: ms=%p\n", sc->hw_hdl));
180	midi_attach(sc, parent);
181}
182
183void
184midisyn_freevoice(ms, voice)
185	midisyn *ms;
186	int voice;
187{
188	if (!(ms->flags & MS_DOALLOC))
189		return;
190	ms->voices[voice].chan_note = ~0;
191}
192
193int
194midisyn_allocvoice(ms, chan, note)
195	midisyn *ms;
196	u_int32_t chan, note;
197{
198	int bestv, v;
199	u_int bestseq, s;
200
201	if (!(ms->flags & MS_DOALLOC))
202		return (chan);
203	/* Find a free voice, or if no free voice is found the oldest. */
204	bestv = 0;
205	bestseq = ms->voices[0].seqno;
206	for (v = 1; v < ms->nvoice; v++) {
207		s = ms->voices[v].seqno;
208		if (s < bestseq) {
209			bestseq = s;
210			bestv = v;
211		}
212	}
213	ms->voices[bestv].chan_note = CHANNOTE(chan, note);
214	ms->voices[bestv].seqno = ms->seqno++;
215	return (bestv);
216}
217
218int
219midisyn_output(addr, b)
220	void *addr;
221	int b;
222{
223	midisyn *ms = addr;
224	u_int8_t status, chan;
225	int voice = 0;		/* initialize to keep gcc quiet */
226	struct midisyn_methods *fs;
227	u_int32_t note, vel;
228
229	DPRINTF(("midisyn_output: ms=%p ms->mets=%p\n", ms, ms->mets));
230	fs = ms->mets;
231	if (ms->pos < 0) {
232		/* Doing SYSEX */
233		if (fs->sysex)
234			fs->sysex(ms, b);
235		if (b == MIDI_SYSEX_END)
236			ms->pos = 0;
237		return (0);
238	}
239	if (ms->pos == 0 && !MIDI_IS_STATUS(b))
240		ms->pos++;	/* repeat last status byte */
241	ms->buf[ms->pos++] = b;
242	status = ms->buf[0];
243	if (ms->pos < MIDI_LENGTH(status))
244		return (0);
245	/* Decode the MIDI command */
246	chan = MIDI_GET_CHAN(status);
247	note = ms->buf[1];
248	if (ms->flags & MS_FREQXLATE)
249		note = midisyn_note_to_freq(note);
250	vel = ms->buf[2];
251	switch (MIDI_GET_STATUS(status)) {
252	case MIDI_NOTEOFF:
253		voice = midisyn_findvoice(ms, chan, ms->buf[1]);
254		if (voice >= 0) {
255			fs->noteoff(ms, voice, note, vel);
256			midisyn_freevoice(ms, voice);
257		}
258		break;
259	case MIDI_NOTEON:
260		voice = fs->allocv(ms, chan, ms->buf[1]);
261		fs->noteon(ms, voice, note, vel);
262		break;
263	case MIDI_KEY_PRESSURE:
264		if (fs->keypres) {
265			voice = midisyn_findvoice(ms, voice, ms->buf[1]);
266			if (voice >= 0)
267				fs->keypres(ms, voice, note, vel);
268		}
269		break;
270	case MIDI_CTL_CHANGE:
271		if (fs->ctlchg)
272			fs->ctlchg(ms, chan, ms->buf[1], vel);
273		break;
274	case MIDI_PGM_CHANGE:
275		if (fs->pgmchg)
276			fs->pgmchg(ms, chan, ms->buf[1]);
277		break;
278	case MIDI_CHN_PRESSURE:
279		if (fs->chnpres) {
280			voice = midisyn_findvoice(ms, chan, ms->buf[1]);
281			if (voice >= 0)
282				fs->chnpres(ms, voice, note);
283		}
284		break;
285	case MIDI_PITCH_BEND:
286		if (fs->pitchb) {
287			voice = midisyn_findvoice(ms, chan, ms->buf[1]);
288			if (voice >= 0)
289				fs->pitchb(ms, chan, note, vel);
290		}
291		break;
292	case MIDI_SYSTEM_PREFIX:
293		if (fs->sysex)
294			fs->sysex(ms, status);
295		ms->pos = -1;
296		return (0);
297	}
298	ms->pos = 0;
299	return (0);
300}
301
302/*
303 * Convert a MIDI note to the corresponding frequency.
304 * The frequency is scaled by 2^16.
305 */
306u_int32_t
307midisyn_note_to_freq(note)
308	int note;
309{
310	int o, n, f;
311#define BASE_OCTAVE 5
312	static u_int32_t notes[] = {
313		17145893, 18165441, 19245614, 20390018, 21602472, 22887021,
314		24247954, 25689813, 27217409, 28835840, 30550508, 32367136
315	};
316
317
318	o = note / 12;
319	n = note % 12;
320
321	f = notes[n];
322
323	if (o < BASE_OCTAVE)
324		f >>= (BASE_OCTAVE - o);
325	else if (o > BASE_OCTAVE)
326		f <<= (o - BASE_OCTAVE);
327	return (f);
328}
329
330