Home | History | Annotate | Line # | Download | only in dev
midisyn.c revision 1.17.2.5
      1 /*	$NetBSD: midisyn.c,v 1.17.2.5 2006/05/20 03:27:31 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.5 2006/05/20 03:27:31 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 proc *);
     83 
     84 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 	mi->props = 0;
    140 
    141 	midi_register_hw_if_ext(&midisyn_hw_if_ext);
    142 }
    143 
    144 int
    145 midisyn_ioctl(void *maddr, u_long cmd, caddr_t addr, int flag, struct proc *p)
    146 {
    147 	midisyn *ms = maddr;
    148 
    149 	if (ms->mets->ioctl)
    150 		return (ms->mets->ioctl(ms, cmd, addr, flag, p));
    151 	else
    152 		return (EINVAL);
    153 }
    154 
    155 int
    156 midisyn_findvoice(midisyn *ms, int chan, int note)
    157 {
    158 	u_int cn;
    159 	int v;
    160 
    161 	if (!(ms->flags & MS_DOALLOC))
    162 		return (chan);
    163 	cn = MS_CHANNOTE(chan, note);
    164 	for (v = 0; v < ms->nvoice; v++)
    165 		if (ms->voices[v].chan_note == cn && ms->voices[v].inuse)
    166 			return (v);
    167 	return (-1);
    168 }
    169 
    170 void
    171 midisyn_attach(struct midi_softc *sc, midisyn *ms)
    172 {
    173 	if (ms->flags & MS_DOALLOC) {
    174 		ms->voices = malloc(ms->nvoice * sizeof (struct voice),
    175 				    M_DEVBUF, M_WAITOK|M_ZERO);
    176 		ms->seqno = 1;
    177 		if (ms->mets->allocv == 0)
    178 			ms->mets->allocv = &midisyn_allocvoice;
    179 	}
    180 	sc->hw_if = &midisyn_hw_if;
    181 	sc->hw_hdl = ms;
    182 	DPRINTF(("midisyn_attach: ms=%p\n", sc->hw_hdl));
    183 }
    184 
    185 void
    186 midisyn_freevoice(midisyn *ms, int voice)
    187 {
    188 	if (!(ms->flags & MS_DOALLOC))
    189 		return;
    190 	ms->voices[voice].inuse = 0;
    191 }
    192 
    193 int
    194 midisyn_allocvoice(midisyn *ms, u_int32_t chan, u_int32_t note)
    195 {
    196 	int bestv, v;
    197 	u_int bestseq, s;
    198 
    199 	if (!(ms->flags & MS_DOALLOC))
    200 		return (chan);
    201 	/* Find a free voice, or if no free voice is found the oldest. */
    202 	bestv = 0;
    203 	bestseq = ms->voices[0].seqno + (ms->voices[0].inuse ? 0x40000000 : 0);
    204 	for (v = 1; v < ms->nvoice; v++) {
    205 		s = ms->voices[v].seqno;
    206 		if (ms->voices[v].inuse)
    207 			s += 0x40000000;
    208 		if (s < bestseq) {
    209 			bestseq = s;
    210 			bestv = v;
    211 		}
    212 	}
    213 	DPRINTFN(10,("midisyn_allocvoice: v=%d seq=%d cn=%x inuse=%d\n",
    214 		     bestv, ms->voices[bestv].seqno,
    215 		     ms->voices[bestv].chan_note,
    216 		     ms->voices[bestv].inuse));
    217 #ifdef AUDIO_DEBUG
    218 	if (ms->voices[bestv].inuse)
    219 		DPRINTFN(1,("midisyn_allocvoice: steal %x\n",
    220 			    ms->voices[bestv].chan_note));
    221 #endif
    222 	ms->voices[bestv].chan_note = MS_CHANNOTE(chan, note);
    223 	ms->voices[bestv].seqno = ms->seqno++;
    224 	ms->voices[bestv].inuse = 1;
    225 	return (bestv);
    226 }
    227 
    228 int
    229 midisyn_sysrt(void *addr, int b)
    230 {
    231 	return 0;
    232 }
    233 
    234 int midisyn_channelmsg(void *addr, int status, int chan, u_char *buf, int len)
    235 {
    236 	midisyn *ms = addr;
    237 	int voice = 0;		/* initialize to keep gcc quiet */
    238 	struct midisyn_methods *fs;
    239 	u_int32_t note, vel;
    240 
    241 	DPRINTF(("midisyn_channelmsg: ms=%p status=%#02x chan=%d\n",
    242 	       ms, status, chan));
    243 	fs = ms->mets;
    244 	note = buf[1];
    245 	if (ms->flags & MS_FREQXLATE)
    246 		note = midisyn_note_to_freq(note);
    247 	vel = buf[2];
    248 
    249 	switch (status) {
    250 	case MIDI_NOTEOFF:
    251 		voice = midisyn_findvoice(ms, chan, buf[1]);
    252 		if (voice >= 0) {
    253 			fs->noteoff(ms, voice, note, vel);
    254 			midisyn_freevoice(ms, voice);
    255 		}
    256 		break;
    257 	case MIDI_NOTEON:
    258 		voice = fs->allocv(ms, chan, buf[1]);
    259 		fs->noteon(ms, voice, note, vel);
    260 		break;
    261 	case MIDI_KEY_PRESSURE:
    262 		if (fs->keypres) {
    263 			voice = midisyn_findvoice(ms, voice, buf[1]);
    264 			if (voice >= 0)
    265 				fs->keypres(ms, voice, note, vel);
    266 		}
    267 		break;
    268 	case MIDI_CTL_CHANGE:
    269 		if (fs->ctlchg)
    270 			fs->ctlchg(ms, chan, buf[1], vel);
    271 		break;
    272 	case MIDI_PGM_CHANGE:
    273 		if (fs->pgmchg)
    274 			fs->pgmchg(ms, chan, buf[1]);
    275 		break;
    276 	case MIDI_CHN_PRESSURE:
    277 		if (fs->chnpres) {
    278 			voice = midisyn_findvoice(ms, chan, buf[1]);
    279 			if (voice >= 0) /* XXX should do all voices on chan */
    280 				fs->chnpres(ms, voice, note);
    281 		}
    282 		break;
    283 	case MIDI_PITCH_BEND:
    284 		if (fs->pitchb) {
    285 			voice = midisyn_findvoice(ms, chan, buf[1]);
    286 			if (voice >= 0) /* XXX buf1-2 are a 14-bit bend value */
    287 				fs->pitchb(ms, chan, note, vel);
    288 		}
    289 		break;
    290 	}
    291 	return 0;
    292 }
    293 
    294 int midisyn_commonmsg(void *addr, int status, u_char *buf, int len)
    295 {
    296 	return 0;
    297 }
    298 
    299 int midisyn_sysex(void *addr, u_char *buf, int len)
    300 {
    301 	midisyn *ms = addr;
    302 	if (ms->mets->sysex) {
    303 		while ( len --> 0 )
    304 			ms->mets->sysex(ms, *buf++);
    305 	}
    306 	return 0;
    307 }
    308 
    309 /*
    310  * Convert a MIDI note to the corresponding frequency.
    311  * The frequency is scaled by 2^16.
    312  */
    313 u_int32_t
    314 midisyn_note_to_freq(int note)
    315 {
    316 	int o, n, f;
    317 #define BASE_OCTAVE 5
    318 	static const u_int32_t notes[] = {
    319 		17145893, 18165441, 19245614, 20390018, 21602472, 22887021,
    320 		24247954, 25689813, 27217409, 28835840, 30550508, 32367136
    321 	};
    322 
    323 
    324 	o = note / 12;
    325 	n = note % 12;
    326 
    327 	f = notes[n];
    328 
    329 	if (o < BASE_OCTAVE)
    330 		f >>= (BASE_OCTAVE - o);
    331 	else if (o > BASE_OCTAVE)
    332 		f <<= (o - BASE_OCTAVE);
    333 	return (f);
    334 }
    335 
    336 u_int32_t
    337 midisyn_finetune(u_int32_t base_freq, int bend, int range, int vibrato_cents)
    338 {
    339 	static const u_int16_t semitone_tuning[24] =
    340 	{
    341 /*   0 */ 10000, 10595, 11225, 11892, 12599, 13348, 14142, 14983,
    342 /*   8 */ 15874, 16818, 17818, 18877, 20000, 21189, 22449, 23784,
    343 /*  16 */ 25198, 26697, 28284, 29966, 31748, 33636, 35636, 37755
    344 	};
    345 	static const u_int16_t cent_tuning[100] =
    346 	{
    347 /*   0 */ 10000, 10006, 10012, 10017, 10023, 10029, 10035, 10041,
    348 /*   8 */ 10046, 10052, 10058, 10064, 10070, 10075, 10081, 10087,
    349 /*  16 */ 10093, 10099, 10105, 10110, 10116, 10122, 10128, 10134,
    350 /*  24 */ 10140, 10145, 10151, 10157, 10163, 10169, 10175, 10181,
    351 /*  32 */ 10187, 10192, 10198, 10204, 10210, 10216, 10222, 10228,
    352 /*  40 */ 10234, 10240, 10246, 10251, 10257, 10263, 10269, 10275,
    353 /*  48 */ 10281, 10287, 10293, 10299, 10305, 10311, 10317, 10323,
    354 /*  56 */ 10329, 10335, 10341, 10347, 10353, 10359, 10365, 10371,
    355 /*  64 */ 10377, 10383, 10389, 10395, 10401, 10407, 10413, 10419,
    356 /*  72 */ 10425, 10431, 10437, 10443, 10449, 10455, 10461, 10467,
    357 /*  80 */ 10473, 10479, 10485, 10491, 10497, 10503, 10509, 10515,
    358 /*  88 */ 10521, 10528, 10534, 10540, 10546, 10552, 10558, 10564,
    359 /*  96 */ 10570, 10576, 10582, 10589
    360 	};
    361 	u_int32_t amount;
    362 	int negative, semitones, cents, multiplier;
    363 
    364 	if (range == 0)
    365 		return base_freq;
    366 
    367 	if (base_freq == 0)
    368 		return base_freq;
    369 
    370 	if (range >= 8192)
    371 		range = 8192;
    372 
    373 	bend = bend * range / 8192;
    374 	bend += vibrato_cents;
    375 
    376 	if (bend == 0)
    377 		return base_freq;
    378 
    379 	if (bend < 0) {
    380 		bend = -bend;
    381 		negative = 1;
    382 	} else
    383 		negative = 0;
    384 
    385 	if (bend > range)
    386 		bend = range;
    387 
    388 	multiplier = 1;
    389 	while (bend > 2399) {
    390 		multiplier *= 4;
    391 		bend -= 2400;
    392 	}
    393 
    394 	semitones = bend / 100;
    395 	if (semitones > 99)
    396 		semitones = 99;
    397 	cents = bend % 100;
    398 
    399 	amount = semitone_tuning[semitones] * multiplier * cent_tuning[cents]
    400 		/ 10000;
    401 
    402 	if (negative)
    403 		return (base_freq * 10000 / amount);	/* Bend down */
    404 	else
    405 		return (base_freq * amount / 10000);	/* Bend up */
    406 }
    407 
    408