midisyn.c revision 1.4
1/*	$NetBSD: midisyn.c,v 1.4 1998/09/13 04:41:34 mycroft 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/midi_if.h>
55#include <dev/midivar.h>
56#include <dev/midisynvar.h>
57
58#ifdef AUDIO_DEBUG
59#define DPRINTF(x)	if (midisyndebug) printf x
60#define DPRINTFN(n,x)	if (midisyndebug >= (n)) printf x
61int	midisyndebug = 0;
62#else
63#define DPRINTF(x)
64#define DPRINTFN(n,x)
65#endif
66
67int	midisyn_findvoice __P((midisyn *, int, int));
68void	midisyn_freevoice __P((midisyn *, int));
69int	midisyn_allocvoice __P((midisyn *, u_int32_t, u_int32_t));
70u_int32_t midisyn_note_to_freq __P((int));
71u_int32_t midisyn_finetune __P((u_int32_t, int, int, int));
72
73int	midisyn_open __P((void *, int,
74			  void (*iintr)__P((void *, int)),
75			  void (*ointr)__P((void *)), void *arg));
76void	midisyn_close __P((void *));
77int	midisyn_output __P((void *, int));
78void	midisyn_getinfo __P((void *, struct midi_info *));
79int	midisyn_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
80
81struct midi_hw_if midisyn_hw_if = {
82	midisyn_open,
83	midisyn_close,
84	midisyn_output,
85	midisyn_getinfo,
86	midisyn_ioctl,
87};
88
89static int midi_lengths[] = { 3,3,3,3,2,2,3,1 };
90/* Number of bytes in a MIDI command, including status */
91#define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7])
92
93int
94midisyn_open(addr, flags, iintr, ointr, arg)
95	void *addr;
96	int flags;
97	void (*iintr)__P((void *, int));
98	void (*ointr)__P((void *));
99	void *arg;
100{
101	midisyn *ms = addr;
102
103	DPRINTF(("midisyn_open: ms=%p ms->mets=%p\n", ms, ms->mets));
104	if (ms->mets->open)
105		return (ms->mets->open(ms, flags));
106	else
107		return (0);
108}
109
110void
111midisyn_close(addr)
112	void *addr;
113{
114	midisyn *ms = addr;
115	struct midisyn_methods *fs;
116	int v;
117
118	DPRINTF(("midisyn_close: ms=%p ms->mets=%p\n", ms, ms->mets));
119	fs = ms->mets;
120	for (v = 0; v < ms->nvoice; v++)
121		if (ms->voices[v].inuse) {
122			fs->noteoff(ms, v, 0, 0);
123			midisyn_freevoice(ms, v);
124		}
125	if (fs->close)
126		fs->close(ms);
127}
128
129void
130midisyn_getinfo(addr, mi)
131	void *addr;
132	struct midi_info *mi;
133{
134	midisyn *ms = addr;
135
136	mi->name = ms->name;
137	mi->props = 0;
138}
139
140int
141midisyn_ioctl(maddr, cmd, addr, flag, p)
142	void *maddr;
143	u_long cmd;
144	caddr_t addr;
145	int flag;
146	struct proc *p;
147{
148	midisyn *ms = maddr;
149
150	if (ms->mets->ioctl)
151		return (ms->mets->ioctl(ms, cmd, addr, flag, p));
152	else
153		return (EINVAL);
154}
155
156int
157midisyn_findvoice(ms, chan, note)
158	midisyn *ms;
159	int chan, note;
160{
161	u_int cn;
162	int v;
163
164	if (!(ms->flags & MS_DOALLOC))
165		return (chan);
166	cn = MS_CHANNOTE(chan, note);
167	for (v = 0; v < ms->nvoice; v++)
168		if (ms->voices[v].chan_note == cn && ms->voices[v].inuse)
169			return (v);
170	return (-1);
171}
172
173void
174midisyn_attach(sc, ms)
175	struct midi_softc *sc;
176	midisyn *ms;
177{
178	if (ms->flags & MS_DOALLOC) {
179		ms->voices = malloc(ms->nvoice * sizeof (struct voice),
180				    M_DEVBUF, M_WAITOK);
181		memset(ms->voices, 0, ms->nvoice * sizeof (struct voice));
182		ms->seqno = 1;
183		if (ms->mets->allocv == 0)
184			ms->mets->allocv = &midisyn_allocvoice;
185	}
186	sc->hw_if = &midisyn_hw_if;
187	sc->hw_hdl = ms;
188	DPRINTF(("midisyn_attach: ms=%p\n", sc->hw_hdl));
189}
190
191void
192midisyn_freevoice(ms, voice)
193	midisyn *ms;
194	int voice;
195{
196	if (!(ms->flags & MS_DOALLOC))
197		return;
198	ms->voices[voice].inuse = 0;
199}
200
201int
202midisyn_allocvoice(ms, chan, note)
203	midisyn *ms;
204	u_int32_t chan, note;
205{
206	int bestv, v;
207	u_int bestseq, s;
208
209	if (!(ms->flags & MS_DOALLOC))
210		return (chan);
211	/* Find a free voice, or if no free voice is found the oldest. */
212	bestv = 0;
213	bestseq = ms->voices[0].seqno + (ms->voices[0].inuse ? 0x40000000 : 0);
214	for (v = 1; v < ms->nvoice; v++) {
215		s = ms->voices[v].seqno;
216		if (ms->voices[v].inuse)
217			s += 0x40000000;
218		if (s < bestseq) {
219			bestseq = s;
220			bestv = v;
221		}
222	}
223	DPRINTFN(10,("midisyn_allocvoice: v=%d seq=%d cn=%x inuse=%d\n",
224		     bestv, ms->voices[bestv].seqno,
225		     ms->voices[bestv].chan_note,
226		     ms->voices[bestv].inuse));
227#ifdef AUDIO_DEBUG
228	if (ms->voices[bestv].inuse)
229		DPRINTFN(1,("midisyn_allocvoice: steal %x\n",
230			    ms->voices[bestv].chan_note));
231#endif
232	ms->voices[bestv].chan_note = MS_CHANNOTE(chan, note);
233	ms->voices[bestv].seqno = ms->seqno++;
234	ms->voices[bestv].inuse = 1;
235	return (bestv);
236}
237
238int
239midisyn_output(addr, b)
240	void *addr;
241	int b;
242{
243	midisyn *ms = addr;
244	u_int8_t status, chan;
245	int voice = 0;		/* initialize to keep gcc quiet */
246	struct midisyn_methods *fs;
247	u_int32_t note, vel;
248
249	DPRINTF(("midisyn_output: ms=%p b=0x%02x\n", ms, b));
250	fs = ms->mets;
251	if (ms->pos < 0) {
252		/* Doing SYSEX */
253		DPRINTF(("midisyn_output: sysex 0x%02x\n", b));
254		if (fs->sysex)
255			fs->sysex(ms, b);
256		if (b == MIDI_SYSEX_END)
257			ms->pos = 0;
258		return (0);
259	}
260	if (ms->pos == 0 && !MIDI_IS_STATUS(b))
261		ms->pos++;	/* repeat last status byte */
262	ms->buf[ms->pos++] = b;
263	status = ms->buf[0];
264	if (ms->pos < MIDI_LENGTH(status))
265		return (0);
266	/* Decode the MIDI command */
267	chan = MIDI_GET_CHAN(status);
268	note = ms->buf[1];
269	if (ms->flags & MS_FREQXLATE)
270		note = midisyn_note_to_freq(note);
271	vel = ms->buf[2];
272	switch (MIDI_GET_STATUS(status)) {
273	case MIDI_NOTEOFF:
274		voice = midisyn_findvoice(ms, chan, ms->buf[1]);
275		if (voice >= 0) {
276			fs->noteoff(ms, voice, note, vel);
277			midisyn_freevoice(ms, voice);
278		}
279		break;
280	case MIDI_NOTEON:
281		voice = fs->allocv(ms, chan, ms->buf[1]);
282		fs->noteon(ms, voice, note, vel);
283		break;
284	case MIDI_KEY_PRESSURE:
285		if (fs->keypres) {
286			voice = midisyn_findvoice(ms, voice, ms->buf[1]);
287			if (voice >= 0)
288				fs->keypres(ms, voice, note, vel);
289		}
290		break;
291	case MIDI_CTL_CHANGE:
292		if (fs->ctlchg)
293			fs->ctlchg(ms, chan, ms->buf[1], vel);
294		break;
295	case MIDI_PGM_CHANGE:
296		if (fs->pgmchg)
297			fs->pgmchg(ms, chan, ms->buf[1]);
298		break;
299	case MIDI_CHN_PRESSURE:
300		if (fs->chnpres) {
301			voice = midisyn_findvoice(ms, chan, ms->buf[1]);
302			if (voice >= 0)
303				fs->chnpres(ms, voice, note);
304		}
305		break;
306	case MIDI_PITCH_BEND:
307		if (fs->pitchb) {
308			voice = midisyn_findvoice(ms, chan, ms->buf[1]);
309			if (voice >= 0)
310				fs->pitchb(ms, chan, note, vel);
311		}
312		break;
313	case MIDI_SYSTEM_PREFIX:
314		if (fs->sysex)
315			fs->sysex(ms, status);
316		ms->pos = -1;
317		return (0);
318	}
319	ms->pos = 0;
320	return (0);
321}
322
323/*
324 * Convert a MIDI note to the corresponding frequency.
325 * The frequency is scaled by 2^16.
326 */
327u_int32_t
328midisyn_note_to_freq(note)
329	int note;
330{
331	int o, n, f;
332#define BASE_OCTAVE 5
333	static u_int32_t notes[] = {
334		17145893, 18165441, 19245614, 20390018, 21602472, 22887021,
335		24247954, 25689813, 27217409, 28835840, 30550508, 32367136
336	};
337
338
339	o = note / 12;
340	n = note % 12;
341
342	f = notes[n];
343
344	if (o < BASE_OCTAVE)
345		f >>= (BASE_OCTAVE - o);
346	else if (o > BASE_OCTAVE)
347		f <<= (o - BASE_OCTAVE);
348	return (f);
349}
350
351u_int32_t
352midisyn_finetune(base_freq, bend, range, vibrato_cents)
353	u_int32_t base_freq;
354	int bend;
355	int range;
356	int vibrato_cents;
357{
358	static u_int16_t semitone_tuning[24] =
359	{
360/*   0 */ 10000, 10595, 11225, 11892, 12599, 13348, 14142, 14983,
361/*   8 */ 15874, 16818, 17818, 18877, 20000, 21189, 22449, 23784,
362/*  16 */ 25198, 26697, 28284, 29966, 31748, 33636, 35636, 37755
363	};
364	static u_int16_t cent_tuning[100] =
365	{
366/*   0 */ 10000, 10006, 10012, 10017, 10023, 10029, 10035, 10041,
367/*   8 */ 10046, 10052, 10058, 10064, 10070, 10075, 10081, 10087,
368/*  16 */ 10093, 10099, 10105, 10110, 10116, 10122, 10128, 10134,
369/*  24 */ 10140, 10145, 10151, 10157, 10163, 10169, 10175, 10181,
370/*  32 */ 10187, 10192, 10198, 10204, 10210, 10216, 10222, 10228,
371/*  40 */ 10234, 10240, 10246, 10251, 10257, 10263, 10269, 10275,
372/*  48 */ 10281, 10287, 10293, 10299, 10305, 10311, 10317, 10323,
373/*  56 */ 10329, 10335, 10341, 10347, 10353, 10359, 10365, 10371,
374/*  64 */ 10377, 10383, 10389, 10395, 10401, 10407, 10413, 10419,
375/*  72 */ 10425, 10431, 10437, 10443, 10449, 10455, 10461, 10467,
376/*  80 */ 10473, 10479, 10485, 10491, 10497, 10503, 10509, 10515,
377/*  88 */ 10521, 10528, 10534, 10540, 10546, 10552, 10558, 10564,
378/*  96 */ 10570, 10576, 10582, 10589
379	};
380	u_int32_t amount;
381	int negative, semitones, cents, multiplier;
382
383	if (range == 0)
384		return base_freq;
385
386	if (base_freq == 0)
387		return base_freq;
388
389	if (range >= 8192)
390		range = 8192;
391
392	bend = bend * range / 8192;
393	bend += vibrato_cents;
394
395	if (bend == 0)
396		return base_freq;
397
398	if (bend < 0) {
399		bend = -bend;
400		negative = 1;
401	} else
402		negative = 0;
403
404	if (bend > range)
405		bend = range;
406
407	multiplier = 1;
408	while (bend > 2399) {
409		multiplier *= 4;
410		bend -= 2400;
411	}
412
413	semitones = bend / 100;
414	if (semitones > 99)
415		semitones = 99;
416	cents = bend % 100;
417
418	amount = semitone_tuning[semitones] * multiplier * cent_tuning[cents]
419		/ 10000;
420
421	if (negative)
422		return (base_freq * 10000 / amount);	/* Bend down */
423	else
424		return (base_freq * amount / 10000);	/* Bend up */
425}
426
427