Home | History | Annotate | Line # | Download | only in dev
midisyn.c revision 1.8
      1 /*	$NetBSD: midisyn.c,v 1.8 2001/10/02 22:41:22 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (augustss (at) netbsd.org).
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/fcntl.h>
     42 #include <sys/vnode.h>
     43 #include <sys/select.h>
     44 #include <sys/proc.h>
     45 #include <sys/malloc.h>
     46 #include <sys/systm.h>
     47 #include <sys/syslog.h>
     48 #include <sys/kernel.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
     61 int	midisyndebug = 0;
     62 #else
     63 #define DPRINTF(x)
     64 #define DPRINTFN(n,x)
     65 #endif
     66 
     67 int	midisyn_findvoice(midisyn *, int, int);
     68 void	midisyn_freevoice(midisyn *, int);
     69 int	midisyn_allocvoice(midisyn *, u_int32_t, u_int32_t);
     70 u_int32_t midisyn_note_to_freq(int);
     71 u_int32_t midisyn_finetune(u_int32_t, int, int, int);
     72 
     73 int	midisyn_open(void *, int,
     74 		     void (*iintr)(void *, int),
     75 		     void (*ointr)(void *), void *arg);
     76 void	midisyn_close(void *);
     77 int	midisyn_output(void *, int);
     78 void	midisyn_getinfo(void *, struct midi_info *);
     79 int	midisyn_ioctl(void *, u_long, caddr_t, int, struct proc *);
     80 
     81 struct midi_hw_if midisyn_hw_if = {
     82 	midisyn_open,
     83 	midisyn_close,
     84 	midisyn_output,
     85 	midisyn_getinfo,
     86 	midisyn_ioctl,
     87 };
     88 
     89 static const 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 
     93 int
     94 midisyn_open(void *addr, int flags, void (*iintr)(void *, int),
     95 	     void (*ointr)(void *), void *arg)
     96 {
     97 	midisyn *ms = addr;
     98 
     99 	DPRINTF(("midisyn_open: ms=%p ms->mets=%p\n", ms, ms->mets));
    100 	if (ms->mets->open)
    101 		return (ms->mets->open(ms, flags));
    102 	else
    103 		return (0);
    104 }
    105 
    106 void
    107 midisyn_close(void *addr)
    108 {
    109 	midisyn *ms = addr;
    110 	struct midisyn_methods *fs;
    111 	int v;
    112 
    113 	DPRINTF(("midisyn_close: ms=%p ms->mets=%p\n", ms, ms->mets));
    114 	fs = ms->mets;
    115 	for (v = 0; v < ms->nvoice; v++)
    116 		if (ms->voices[v].inuse) {
    117 			fs->noteoff(ms, v, 0, 0);
    118 			midisyn_freevoice(ms, v);
    119 		}
    120 	if (fs->close)
    121 		fs->close(ms);
    122 }
    123 
    124 void
    125 midisyn_getinfo(void *addr, struct midi_info *mi)
    126 {
    127 	midisyn *ms = addr;
    128 
    129 	mi->name = ms->name;
    130 	mi->props = 0;
    131 }
    132 
    133 int
    134 midisyn_ioctl(void *maddr, u_long cmd, caddr_t addr, int flag, struct proc *p)
    135 {
    136 	midisyn *ms = maddr;
    137 
    138 	if (ms->mets->ioctl)
    139 		return (ms->mets->ioctl(ms, cmd, addr, flag, p));
    140 	else
    141 		return (EINVAL);
    142 }
    143 
    144 int
    145 midisyn_findvoice(midisyn *ms, int chan, int note)
    146 {
    147 	u_int cn;
    148 	int v;
    149 
    150 	if (!(ms->flags & MS_DOALLOC))
    151 		return (chan);
    152 	cn = MS_CHANNOTE(chan, note);
    153 	for (v = 0; v < ms->nvoice; v++)
    154 		if (ms->voices[v].chan_note == cn && ms->voices[v].inuse)
    155 			return (v);
    156 	return (-1);
    157 }
    158 
    159 void
    160 midisyn_attach(struct midi_softc *sc, midisyn *ms)
    161 {
    162 	if (ms->flags & MS_DOALLOC) {
    163 		ms->voices = malloc(ms->nvoice * sizeof (struct voice),
    164 				    M_DEVBUF, M_WAITOK);
    165 		memset(ms->voices, 0, ms->nvoice * sizeof (struct voice));
    166 		ms->seqno = 1;
    167 		if (ms->mets->allocv == 0)
    168 			ms->mets->allocv = &midisyn_allocvoice;
    169 	}
    170 	sc->hw_if = &midisyn_hw_if;
    171 	sc->hw_hdl = ms;
    172 	DPRINTF(("midisyn_attach: ms=%p\n", sc->hw_hdl));
    173 }
    174 
    175 void
    176 midisyn_freevoice(midisyn *ms, int voice)
    177 {
    178 	if (!(ms->flags & MS_DOALLOC))
    179 		return;
    180 	ms->voices[voice].inuse = 0;
    181 }
    182 
    183 int
    184 midisyn_allocvoice(midisyn *ms, u_int32_t chan, u_int32_t note)
    185 {
    186 	int bestv, v;
    187 	u_int bestseq, s;
    188 
    189 	if (!(ms->flags & MS_DOALLOC))
    190 		return (chan);
    191 	/* Find a free voice, or if no free voice is found the oldest. */
    192 	bestv = 0;
    193 	bestseq = ms->voices[0].seqno + (ms->voices[0].inuse ? 0x40000000 : 0);
    194 	for (v = 1; v < ms->nvoice; v++) {
    195 		s = ms->voices[v].seqno;
    196 		if (ms->voices[v].inuse)
    197 			s += 0x40000000;
    198 		if (s < bestseq) {
    199 			bestseq = s;
    200 			bestv = v;
    201 		}
    202 	}
    203 	DPRINTFN(10,("midisyn_allocvoice: v=%d seq=%d cn=%x inuse=%d\n",
    204 		     bestv, ms->voices[bestv].seqno,
    205 		     ms->voices[bestv].chan_note,
    206 		     ms->voices[bestv].inuse));
    207 #ifdef AUDIO_DEBUG
    208 	if (ms->voices[bestv].inuse)
    209 		DPRINTFN(1,("midisyn_allocvoice: steal %x\n",
    210 			    ms->voices[bestv].chan_note));
    211 #endif
    212 	ms->voices[bestv].chan_note = MS_CHANNOTE(chan, note);
    213 	ms->voices[bestv].seqno = ms->seqno++;
    214 	ms->voices[bestv].inuse = 1;
    215 	return (bestv);
    216 }
    217 
    218 int
    219 midisyn_output(void *addr, int b)
    220 {
    221 	midisyn *ms = addr;
    222 	u_int8_t status, chan;
    223 	int voice = 0;		/* initialize to keep gcc quiet */
    224 	struct midisyn_methods *fs;
    225 	u_int32_t note, vel;
    226 
    227 	DPRINTF(("midisyn_output: ms=%p b=0x%02x\n", ms, b));
    228 	fs = ms->mets;
    229 	if (ms->pos < 0) {
    230 		/* Doing SYSEX */
    231 		DPRINTF(("midisyn_output: sysex 0x%02x\n", b));
    232 		if (fs->sysex)
    233 			fs->sysex(ms, b);
    234 		if (b == MIDI_SYSEX_END)
    235 			ms->pos = 0;
    236 		return (0);
    237 	}
    238 	if (ms->pos == 0 && !MIDI_IS_STATUS(b))
    239 		ms->pos++;	/* repeat last status byte */
    240 	ms->buf[ms->pos++] = b;
    241 	status = ms->buf[0];
    242 	if (ms->pos < MIDI_LENGTH(status))
    243 		return (0);
    244 	/* Decode the MIDI command */
    245 	chan = MIDI_GET_CHAN(status);
    246 	note = ms->buf[1];
    247 	if (ms->flags & MS_FREQXLATE)
    248 		note = midisyn_note_to_freq(note);
    249 	vel = ms->buf[2];
    250 	switch (MIDI_GET_STATUS(status)) {
    251 	case MIDI_NOTEOFF:
    252 		voice = midisyn_findvoice(ms, chan, ms->buf[1]);
    253 		if (voice >= 0) {
    254 			fs->noteoff(ms, voice, note, vel);
    255 			midisyn_freevoice(ms, voice);
    256 		}
    257 		break;
    258 	case MIDI_NOTEON:
    259 		voice = fs->allocv(ms, chan, ms->buf[1]);
    260 		fs->noteon(ms, voice, note, vel);
    261 		break;
    262 	case MIDI_KEY_PRESSURE:
    263 		if (fs->keypres) {
    264 			voice = midisyn_findvoice(ms, voice, ms->buf[1]);
    265 			if (voice >= 0)
    266 				fs->keypres(ms, voice, note, vel);
    267 		}
    268 		break;
    269 	case MIDI_CTL_CHANGE:
    270 		if (fs->ctlchg)
    271 			fs->ctlchg(ms, chan, ms->buf[1], vel);
    272 		break;
    273 	case MIDI_PGM_CHANGE:
    274 		if (fs->pgmchg)
    275 			fs->pgmchg(ms, chan, ms->buf[1]);
    276 		break;
    277 	case MIDI_CHN_PRESSURE:
    278 		if (fs->chnpres) {
    279 			voice = midisyn_findvoice(ms, chan, ms->buf[1]);
    280 			if (voice >= 0)
    281 				fs->chnpres(ms, voice, note);
    282 		}
    283 		break;
    284 	case MIDI_PITCH_BEND:
    285 		if (fs->pitchb) {
    286 			voice = midisyn_findvoice(ms, chan, ms->buf[1]);
    287 			if (voice >= 0)
    288 				fs->pitchb(ms, chan, note, vel);
    289 		}
    290 		break;
    291 	case MIDI_SYSTEM_PREFIX:
    292 		if (fs->sysex)
    293 			fs->sysex(ms, status);
    294 		ms->pos = -1;
    295 		return (0);
    296 	}
    297 	ms->pos = 0;
    298 	return (0);
    299 }
    300 
    301 /*
    302  * Convert a MIDI note to the corresponding frequency.
    303  * The frequency is scaled by 2^16.
    304  */
    305 u_int32_t
    306 midisyn_note_to_freq(int note)
    307 {
    308 	int o, n, f;
    309 #define BASE_OCTAVE 5
    310 	static const u_int32_t notes[] = {
    311 		17145893, 18165441, 19245614, 20390018, 21602472, 22887021,
    312 		24247954, 25689813, 27217409, 28835840, 30550508, 32367136
    313 	};
    314 
    315 
    316 	o = note / 12;
    317 	n = note % 12;
    318 
    319 	f = notes[n];
    320 
    321 	if (o < BASE_OCTAVE)
    322 		f >>= (BASE_OCTAVE - o);
    323 	else if (o > BASE_OCTAVE)
    324 		f <<= (o - BASE_OCTAVE);
    325 	return (f);
    326 }
    327 
    328 u_int32_t
    329 midisyn_finetune(u_int32_t base_freq, int bend, int range, int vibrato_cents)
    330 {
    331 	static const u_int16_t semitone_tuning[24] =
    332 	{
    333 /*   0 */ 10000, 10595, 11225, 11892, 12599, 13348, 14142, 14983,
    334 /*   8 */ 15874, 16818, 17818, 18877, 20000, 21189, 22449, 23784,
    335 /*  16 */ 25198, 26697, 28284, 29966, 31748, 33636, 35636, 37755
    336 	};
    337 	static const u_int16_t cent_tuning[100] =
    338 	{
    339 /*   0 */ 10000, 10006, 10012, 10017, 10023, 10029, 10035, 10041,
    340 /*   8 */ 10046, 10052, 10058, 10064, 10070, 10075, 10081, 10087,
    341 /*  16 */ 10093, 10099, 10105, 10110, 10116, 10122, 10128, 10134,
    342 /*  24 */ 10140, 10145, 10151, 10157, 10163, 10169, 10175, 10181,
    343 /*  32 */ 10187, 10192, 10198, 10204, 10210, 10216, 10222, 10228,
    344 /*  40 */ 10234, 10240, 10246, 10251, 10257, 10263, 10269, 10275,
    345 /*  48 */ 10281, 10287, 10293, 10299, 10305, 10311, 10317, 10323,
    346 /*  56 */ 10329, 10335, 10341, 10347, 10353, 10359, 10365, 10371,
    347 /*  64 */ 10377, 10383, 10389, 10395, 10401, 10407, 10413, 10419,
    348 /*  72 */ 10425, 10431, 10437, 10443, 10449, 10455, 10461, 10467,
    349 /*  80 */ 10473, 10479, 10485, 10491, 10497, 10503, 10509, 10515,
    350 /*  88 */ 10521, 10528, 10534, 10540, 10546, 10552, 10558, 10564,
    351 /*  96 */ 10570, 10576, 10582, 10589
    352 	};
    353 	u_int32_t amount;
    354 	int negative, semitones, cents, multiplier;
    355 
    356 	if (range == 0)
    357 		return base_freq;
    358 
    359 	if (base_freq == 0)
    360 		return base_freq;
    361 
    362 	if (range >= 8192)
    363 		range = 8192;
    364 
    365 	bend = bend * range / 8192;
    366 	bend += vibrato_cents;
    367 
    368 	if (bend == 0)
    369 		return base_freq;
    370 
    371 	if (bend < 0) {
    372 		bend = -bend;
    373 		negative = 1;
    374 	} else
    375 		negative = 0;
    376 
    377 	if (bend > range)
    378 		bend = range;
    379 
    380 	multiplier = 1;
    381 	while (bend > 2399) {
    382 		multiplier *= 4;
    383 		bend -= 2400;
    384 	}
    385 
    386 	semitones = bend / 100;
    387 	if (semitones > 99)
    388 		semitones = 99;
    389 	cents = bend % 100;
    390 
    391 	amount = semitone_tuning[semitones] * multiplier * cent_tuning[cents]
    392 		/ 10000;
    393 
    394 	if (negative)
    395 		return (base_freq * 10000 / amount);	/* Bend down */
    396 	else
    397 		return (base_freq * amount / 10000);	/* Bend up */
    398 }
    399 
    400