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