Home | History | Annotate | Line # | Download | only in dev
midisyn.c revision 1.17.2.14
      1  1.17.2.14      chap /*	$NetBSD: midisyn.c,v 1.17.2.14 2006/06/08 04:55:22 chap Exp $	*/
      2        1.1  augustss 
      3        1.1  augustss /*
      4        1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5        1.1  augustss  * All rights reserved.
      6        1.1  augustss  *
      7        1.5  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8       1.13    keihan  * by Lennart Augustsson (augustss (at) NetBSD.org).
      9        1.1  augustss  *
     10        1.1  augustss  * Redistribution and use in source and binary forms, with or without
     11        1.1  augustss  * modification, are permitted provided that the following conditions
     12        1.1  augustss  * are met:
     13        1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     14        1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     15        1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     17        1.1  augustss  *    documentation and/or other materials provided with the distribution.
     18        1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     19        1.1  augustss  *    must display the following acknowledgement:
     20        1.1  augustss  *        This product includes software developed by the NetBSD
     21        1.1  augustss  *        Foundation, Inc. and its contributors.
     22        1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23        1.1  augustss  *    contributors may be used to endorse or promote products derived
     24        1.1  augustss  *    from this software without specific prior written permission.
     25        1.1  augustss  *
     26        1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27        1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28        1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29        1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30        1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31        1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32        1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33        1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34        1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35        1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36        1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     37        1.1  augustss  */
     38        1.9     lukem 
     39        1.9     lukem #include <sys/cdefs.h>
     40  1.17.2.14      chap __KERNEL_RCSID(0, "$NetBSD: midisyn.c,v 1.17.2.14 2006/06/08 04:55:22 chap Exp $");
     41        1.1  augustss 
     42        1.1  augustss #include <sys/param.h>
     43        1.1  augustss #include <sys/ioctl.h>
     44        1.1  augustss #include <sys/fcntl.h>
     45        1.1  augustss #include <sys/vnode.h>
     46        1.1  augustss #include <sys/select.h>
     47        1.1  augustss #include <sys/proc.h>
     48        1.1  augustss #include <sys/malloc.h>
     49        1.1  augustss #include <sys/systm.h>
     50        1.1  augustss #include <sys/syslog.h>
     51        1.1  augustss #include <sys/kernel.h>
     52        1.1  augustss #include <sys/audioio.h>
     53        1.1  augustss #include <sys/midiio.h>
     54        1.1  augustss #include <sys/device.h>
     55        1.1  augustss 
     56        1.1  augustss #include <dev/audio_if.h>
     57        1.3  augustss #include <dev/midi_if.h>
     58        1.1  augustss #include <dev/midivar.h>
     59        1.1  augustss #include <dev/midisynvar.h>
     60        1.1  augustss 
     61        1.1  augustss #ifdef AUDIO_DEBUG
     62        1.1  augustss #define DPRINTF(x)	if (midisyndebug) printf x
     63        1.1  augustss #define DPRINTFN(n,x)	if (midisyndebug >= (n)) printf x
     64        1.1  augustss int	midisyndebug = 0;
     65        1.1  augustss #else
     66        1.1  augustss #define DPRINTF(x)
     67        1.1  augustss #define DPRINTFN(n,x)
     68        1.1  augustss #endif
     69        1.1  augustss 
     70        1.8  augustss int	midisyn_findvoice(midisyn *, int, int);
     71        1.8  augustss void	midisyn_freevoice(midisyn *, int);
     72  1.17.2.14      chap int	midisyn_allocvoice(midisyn *, uint_fast8_t, uint_fast8_t);
     73        1.8  augustss u_int32_t midisyn_note_to_freq(int);
     74        1.8  augustss u_int32_t midisyn_finetune(u_int32_t, int, int, int);
     75        1.8  augustss 
     76  1.17.2.12      chap static midictl_notify midisyn_notify;
     77  1.17.2.12      chap 
     78  1.17.2.10      chap int	midisyn_open(void *, int,
     79        1.8  augustss 		     void (*iintr)(void *, int),
     80        1.8  augustss 		     void (*ointr)(void *), void *arg);
     81        1.8  augustss void	midisyn_close(void *);
     82   1.17.2.2      chap int	midisyn_sysrt(void *, int);
     83        1.8  augustss void	midisyn_getinfo(void *, struct midi_info *);
     84  1.17.2.10      chap int	midisyn_ioctl(void *, u_long, caddr_t, int, struct lwp *);
     85        1.1  augustss 
     86  1.17.2.10      chap const struct midi_hw_if midisyn_hw_if = {
     87        1.1  augustss 	midisyn_open,
     88        1.1  augustss 	midisyn_close,
     89   1.17.2.2      chap 	midisyn_sysrt,
     90        1.1  augustss 	midisyn_getinfo,
     91        1.1  augustss 	midisyn_ioctl,
     92        1.1  augustss };
     93        1.1  augustss 
     94   1.17.2.2      chap int	midisyn_channelmsg(void *, int, int, u_char *, int);
     95   1.17.2.2      chap int	midisyn_commonmsg(void *, int, u_char *, int);
     96   1.17.2.2      chap int	midisyn_sysex(void *, u_char *, int);
     97   1.17.2.2      chap 
     98   1.17.2.2      chap struct midi_hw_if_ext midisyn_hw_if_ext = {
     99   1.17.2.2      chap 	.channel = midisyn_channelmsg,
    100   1.17.2.2      chap 	.common  = midisyn_commonmsg,
    101   1.17.2.2      chap 	.sysex   = midisyn_sysex,
    102   1.17.2.2      chap };
    103        1.1  augustss 
    104        1.1  augustss int
    105        1.8  augustss midisyn_open(void *addr, int flags, void (*iintr)(void *, int),
    106        1.8  augustss 	     void (*ointr)(void *), void *arg)
    107        1.1  augustss {
    108        1.1  augustss 	midisyn *ms = addr;
    109        1.1  augustss 
    110        1.1  augustss 	DPRINTF(("midisyn_open: ms=%p ms->mets=%p\n", ms, ms->mets));
    111  1.17.2.12      chap 
    112  1.17.2.12      chap 	midictl_open(&ms->ctl);
    113  1.17.2.12      chap 
    114        1.1  augustss 	if (ms->mets->open)
    115        1.1  augustss 		return (ms->mets->open(ms, flags));
    116        1.1  augustss 	else
    117        1.1  augustss 		return (0);
    118        1.1  augustss }
    119        1.1  augustss 
    120        1.1  augustss void
    121        1.8  augustss midisyn_close(void *addr)
    122        1.1  augustss {
    123        1.1  augustss 	midisyn *ms = addr;
    124        1.4   mycroft 	struct midisyn_methods *fs;
    125        1.4   mycroft 	int v;
    126        1.1  augustss 
    127        1.1  augustss 	DPRINTF(("midisyn_close: ms=%p ms->mets=%p\n", ms, ms->mets));
    128        1.4   mycroft 	fs = ms->mets;
    129        1.4   mycroft 	for (v = 0; v < ms->nvoice; v++)
    130        1.4   mycroft 		if (ms->voices[v].inuse) {
    131        1.4   mycroft 			fs->noteoff(ms, v, 0, 0);
    132        1.4   mycroft 			midisyn_freevoice(ms, v);
    133        1.4   mycroft 		}
    134        1.4   mycroft 	if (fs->close)
    135        1.4   mycroft 		fs->close(ms);
    136  1.17.2.12      chap 
    137  1.17.2.12      chap 	midictl_close(&ms->ctl);
    138        1.1  augustss }
    139        1.1  augustss 
    140        1.1  augustss void
    141        1.8  augustss midisyn_getinfo(void *addr, struct midi_info *mi)
    142        1.1  augustss {
    143        1.1  augustss 	midisyn *ms = addr;
    144        1.1  augustss 
    145        1.1  augustss 	mi->name = ms->name;
    146  1.17.2.11      chap 	/*
    147  1.17.2.11      chap 	 * I was going to add a property here to suppress midi(4)'s warning
    148  1.17.2.11      chap 	 * about an output device that uses no transmit interrupt, on the
    149  1.17.2.11      chap 	 * assumption that as an onboard synth we handle "output" internally
    150  1.17.2.11      chap 	 * with nothing like the 320 us per byte busy wait of a dumb UART.
    151  1.17.2.11      chap 	 * Then I noticed that opl (at least as currently implemented) seems
    152  1.17.2.11      chap 	 * to need 40 us busy wait to set each register on an OPL2, and sets
    153  1.17.2.11      chap 	 * about 21 registers for every note-on. (Half of that is patch loading
    154  1.17.2.11      chap 	 * and could probably be reduced by different management of voices and
    155  1.17.2.11      chap 	 * patches.) For now I won't bother suppressing that warning....
    156  1.17.2.11      chap 	 */
    157        1.1  augustss 	mi->props = 0;
    158   1.17.2.2      chap 
    159   1.17.2.2      chap 	midi_register_hw_if_ext(&midisyn_hw_if_ext);
    160        1.1  augustss }
    161        1.1  augustss 
    162        1.1  augustss int
    163  1.17.2.10      chap midisyn_ioctl(void *maddr, u_long cmd, caddr_t addr, int flag, struct lwp *l)
    164        1.1  augustss {
    165        1.1  augustss 	midisyn *ms = maddr;
    166        1.1  augustss 
    167        1.1  augustss 	if (ms->mets->ioctl)
    168  1.17.2.10      chap 		return (ms->mets->ioctl(ms, cmd, addr, flag, l));
    169        1.1  augustss 	else
    170        1.1  augustss 		return (EINVAL);
    171        1.1  augustss }
    172        1.1  augustss 
    173        1.1  augustss int
    174        1.8  augustss midisyn_findvoice(midisyn *ms, int chan, int note)
    175        1.1  augustss {
    176        1.1  augustss 	u_int cn;
    177        1.1  augustss 	int v;
    178        1.1  augustss 
    179        1.1  augustss 	if (!(ms->flags & MS_DOALLOC))
    180        1.1  augustss 		return (chan);
    181        1.3  augustss 	cn = MS_CHANNOTE(chan, note);
    182        1.1  augustss 	for (v = 0; v < ms->nvoice; v++)
    183        1.3  augustss 		if (ms->voices[v].chan_note == cn && ms->voices[v].inuse)
    184        1.1  augustss 			return (v);
    185        1.1  augustss 	return (-1);
    186        1.1  augustss }
    187        1.1  augustss 
    188        1.1  augustss void
    189        1.8  augustss midisyn_attach(struct midi_softc *sc, midisyn *ms)
    190        1.1  augustss {
    191        1.1  augustss 	if (ms->flags & MS_DOALLOC) {
    192  1.17.2.10      chap 		ms->voices = malloc(ms->nvoice * sizeof (struct voice),
    193       1.10   tsutsui 				    M_DEVBUF, M_WAITOK|M_ZERO);
    194        1.1  augustss 		ms->seqno = 1;
    195        1.1  augustss 		if (ms->mets->allocv == 0)
    196        1.1  augustss 			ms->mets->allocv = &midisyn_allocvoice;
    197        1.1  augustss 	}
    198  1.17.2.12      chap 
    199  1.17.2.12      chap 	ms->ctl = (midictl) {
    200  1.17.2.12      chap 		.base_channel = 16,
    201  1.17.2.12      chap 		.cookie = ms,
    202  1.17.2.12      chap 		.notify = midisyn_notify
    203  1.17.2.12      chap 	};
    204  1.17.2.12      chap 
    205        1.1  augustss 	sc->hw_if = &midisyn_hw_if;
    206        1.1  augustss 	sc->hw_hdl = ms;
    207        1.1  augustss 	DPRINTF(("midisyn_attach: ms=%p\n", sc->hw_hdl));
    208        1.1  augustss }
    209        1.1  augustss 
    210        1.1  augustss void
    211        1.8  augustss midisyn_freevoice(midisyn *ms, int voice)
    212        1.1  augustss {
    213        1.1  augustss 	if (!(ms->flags & MS_DOALLOC))
    214        1.1  augustss 		return;
    215        1.3  augustss 	ms->voices[voice].inuse = 0;
    216        1.1  augustss }
    217        1.1  augustss 
    218        1.1  augustss int
    219  1.17.2.14      chap midisyn_allocvoice(midisyn *ms, uint_fast8_t chan, uint_fast8_t note)
    220        1.1  augustss {
    221        1.1  augustss 	int bestv, v;
    222        1.1  augustss 	u_int bestseq, s;
    223        1.1  augustss 
    224        1.1  augustss 	/* Find a free voice, or if no free voice is found the oldest. */
    225        1.1  augustss 	bestv = 0;
    226        1.3  augustss 	bestseq = ms->voices[0].seqno + (ms->voices[0].inuse ? 0x40000000 : 0);
    227        1.1  augustss 	for (v = 1; v < ms->nvoice; v++) {
    228        1.1  augustss 		s = ms->voices[v].seqno;
    229        1.3  augustss 		if (ms->voices[v].inuse)
    230        1.3  augustss 			s += 0x40000000;
    231        1.1  augustss 		if (s < bestseq) {
    232        1.1  augustss 			bestseq = s;
    233        1.1  augustss 			bestv = v;
    234        1.1  augustss 		}
    235        1.1  augustss 	}
    236        1.3  augustss 	DPRINTFN(10,("midisyn_allocvoice: v=%d seq=%d cn=%x inuse=%d\n",
    237  1.17.2.10      chap 		     bestv, ms->voices[bestv].seqno,
    238        1.3  augustss 		     ms->voices[bestv].chan_note,
    239        1.3  augustss 		     ms->voices[bestv].inuse));
    240        1.3  augustss #ifdef AUDIO_DEBUG
    241        1.3  augustss 	if (ms->voices[bestv].inuse)
    242  1.17.2.10      chap 		DPRINTFN(1,("midisyn_allocvoice: steal %x\n",
    243        1.3  augustss 			    ms->voices[bestv].chan_note));
    244        1.3  augustss #endif
    245        1.3  augustss 	ms->voices[bestv].chan_note = MS_CHANNOTE(chan, note);
    246        1.1  augustss 	ms->voices[bestv].seqno = ms->seqno++;
    247        1.3  augustss 	ms->voices[bestv].inuse = 1;
    248        1.1  augustss 	return (bestv);
    249        1.1  augustss }
    250        1.1  augustss 
    251        1.1  augustss int
    252   1.17.2.2      chap midisyn_sysrt(void *addr, int b)
    253   1.17.2.2      chap {
    254   1.17.2.2      chap 	return 0;
    255   1.17.2.2      chap }
    256   1.17.2.2      chap 
    257   1.17.2.2      chap int midisyn_channelmsg(void *addr, int status, int chan, u_char *buf, int len)
    258        1.1  augustss {
    259        1.1  augustss 	midisyn *ms = addr;
    260        1.1  augustss 	int voice = 0;		/* initialize to keep gcc quiet */
    261        1.1  augustss 	struct midisyn_methods *fs;
    262        1.1  augustss 	u_int32_t note, vel;
    263        1.1  augustss 
    264   1.17.2.2      chap 	DPRINTF(("midisyn_channelmsg: ms=%p status=%#02x chan=%d\n",
    265   1.17.2.2      chap 	       ms, status, chan));
    266        1.1  augustss 	fs = ms->mets;
    267   1.17.2.2      chap 	note = buf[1];
    268        1.1  augustss 	if (ms->flags & MS_FREQXLATE)
    269        1.1  augustss 		note = midisyn_note_to_freq(note);
    270   1.17.2.2      chap 	vel = buf[2];
    271   1.17.2.2      chap 
    272   1.17.2.2      chap 	switch (status) {
    273        1.1  augustss 	case MIDI_NOTEOFF:
    274  1.17.2.13      chap 		/*
    275  1.17.2.13      chap 		 * for a device that leaves voice allocation to us--and that's
    276  1.17.2.13      chap 		 * all of 'em at the moment--the voice and release velocity
    277  1.17.2.13      chap 		 * should be the only necessary arguments to noteoff. what use
    278  1.17.2.13      chap 		 * are they making of note? checking... None. Cool.
    279  1.17.2.13      chap 		 */
    280   1.17.2.2      chap 		voice = midisyn_findvoice(ms, chan, buf[1]);
    281        1.1  augustss 		if (voice >= 0) {
    282        1.1  augustss 			fs->noteoff(ms, voice, note, vel);
    283        1.1  augustss 			midisyn_freevoice(ms, voice);
    284        1.1  augustss 		}
    285        1.1  augustss 		break;
    286        1.1  augustss 	case MIDI_NOTEON:
    287  1.17.2.13      chap 		/*
    288  1.17.2.13      chap 		 * opl combines vel with a 'mainvol' that has no setter
    289  1.17.2.13      chap 		 * anywhere. pcppi punts volume entirely. cms uses vel alone.
    290  1.17.2.13      chap 		 * what's called for here, given current drivers, is an i/f
    291  1.17.2.13      chap 		 * where midisyn computes a volume from vel*volume*expression*
    292  1.17.2.13      chap 		 * mastervolume and passes that result as a single arg. It can
    293  1.17.2.13      chap 		 * evolve later to support drivers that expose some of those
    294  1.17.2.13      chap 		 * bits separately (e.g. a driver could expose a mixer register
    295  1.17.2.13      chap 		 * on its sound card and use that for mastervolume).
    296  1.17.2.13      chap 		 */
    297   1.17.2.2      chap 		voice = fs->allocv(ms, chan, buf[1]);
    298        1.1  augustss 		fs->noteon(ms, voice, note, vel);
    299        1.1  augustss 		break;
    300        1.1  augustss 	case MIDI_KEY_PRESSURE:
    301  1.17.2.13      chap 		/*
    302  1.17.2.13      chap 		 * unimplemented by the existing drivers. if we are doing
    303  1.17.2.13      chap 		 * voice allocation, find the voice that corresponds to this
    304  1.17.2.13      chap 		 * chan/note and define a method that passes the voice and
    305  1.17.2.13      chap 		 * pressure to the driver ... not the note, /it/ doesn't matter.
    306  1.17.2.13      chap 		 * For a driver that does its own allocation, a different
    307  1.17.2.13      chap 		 * method may be needed passing pressure, chan, note so it can
    308  1.17.2.13      chap 		 * find the right voice on its own. Be sure that whatever is
    309  1.17.2.13      chap 		 * done here is undone when midisyn_notify sees MIDICTL_RESET.
    310  1.17.2.13      chap 		 */
    311        1.1  augustss 		break;
    312        1.1  augustss 	case MIDI_CTL_CHANGE:
    313  1.17.2.12      chap 		midictl_change(&ms->ctl, chan, buf+1);
    314        1.1  augustss 		break;
    315        1.1  augustss 	case MIDI_PGM_CHANGE:
    316        1.1  augustss 		if (fs->pgmchg)
    317   1.17.2.2      chap 			fs->pgmchg(ms, chan, buf[1]);
    318        1.1  augustss 		break;
    319        1.1  augustss 	case MIDI_CHN_PRESSURE:
    320  1.17.2.13      chap 		/*
    321  1.17.2.13      chap 		 * unimplemented by the existing drivers. if driver exposes no
    322  1.17.2.13      chap 		 * distinct method, can use KEY_PRESSURE method for each voice
    323  1.17.2.13      chap 		 * on channel. Be sure that whatever is
    324  1.17.2.13      chap 		 * done here is undone when midisyn_notify sees MIDICTL_RESET.
    325  1.17.2.13      chap 		 */
    326        1.1  augustss 		break;
    327        1.1  augustss 	case MIDI_PITCH_BEND:
    328  1.17.2.13      chap 		/*
    329  1.17.2.13      chap 		 * unimplemented by existing drivers. it is good that most
    330  1.17.2.13      chap 		 * existing drivers take a /frequency/ rather than a /note no/
    331  1.17.2.13      chap 		 * for control; midisyn should use this event to update some
    332  1.17.2.13      chap 		 * internal state and use it (along with tuning) to derive the
    333  1.17.2.13      chap 		 * frequency passed to the driver for any note on. A driver that
    334  1.17.2.13      chap 		 * can change freq of a sounding voice should expose a method
    335  1.17.2.13      chap 		 * for that, i.e. m(voice,newfreq) and we should call that too,
    336  1.17.2.13      chap 		 * for each voice on the affected channel, when a pitch bend
    337  1.17.2.13      chap 		 * change is received. Note RPN 0 must be used in scaling the
    338  1.17.2.13      chap 		 * pitch bend. Be sure that whatever is
    339  1.17.2.13      chap 		 * done here is undone when midisyn_notify sees MIDICTL_RESET.
    340  1.17.2.13      chap 		 */
    341        1.1  augustss 		break;
    342        1.1  augustss 	}
    343   1.17.2.2      chap 	return 0;
    344   1.17.2.2      chap }
    345   1.17.2.2      chap 
    346   1.17.2.2      chap int midisyn_commonmsg(void *addr, int status, u_char *buf, int len)
    347   1.17.2.2      chap {
    348   1.17.2.2      chap 	return 0;
    349   1.17.2.2      chap }
    350   1.17.2.2      chap 
    351   1.17.2.2      chap int midisyn_sysex(void *addr, u_char *buf, int len)
    352   1.17.2.2      chap {
    353  1.17.2.13      chap 	/*
    354  1.17.2.13      chap 	 * unimplemented by existing drivers. it is surely more sensible
    355  1.17.2.13      chap 	 * to do some parsing of well-defined sysex messages here, either
    356  1.17.2.13      chap 	 * handling them internally or calling specific methods on the
    357  1.17.2.13      chap 	 * driver after parsing out the details, than to ask every driver
    358  1.17.2.13      chap 	 * to deal with sysex messages poked at it a byte at a time.
    359  1.17.2.13      chap 	 */
    360   1.17.2.2      chap 	return 0;
    361        1.1  augustss }
    362        1.1  augustss 
    363        1.1  augustss /*
    364        1.1  augustss  * Convert a MIDI note to the corresponding frequency.
    365        1.1  augustss  * The frequency is scaled by 2^16.
    366        1.1  augustss  */
    367        1.1  augustss u_int32_t
    368        1.8  augustss midisyn_note_to_freq(int note)
    369        1.1  augustss {
    370        1.1  augustss 	int o, n, f;
    371        1.1  augustss #define BASE_OCTAVE 5
    372        1.7  jdolecek 	static const u_int32_t notes[] = {
    373        1.1  augustss 		17145893, 18165441, 19245614, 20390018, 21602472, 22887021,
    374        1.1  augustss 		24247954, 25689813, 27217409, 28835840, 30550508, 32367136
    375        1.1  augustss 	};
    376        1.1  augustss 
    377        1.1  augustss 
    378        1.1  augustss 	o = note / 12;
    379        1.1  augustss 	n = note % 12;
    380        1.1  augustss 
    381        1.1  augustss 	f = notes[n];
    382        1.1  augustss 
    383        1.1  augustss 	if (o < BASE_OCTAVE)
    384        1.1  augustss 		f >>= (BASE_OCTAVE - o);
    385        1.1  augustss 	else if (o > BASE_OCTAVE)
    386        1.1  augustss 		f <<= (o - BASE_OCTAVE);
    387        1.1  augustss 	return (f);
    388        1.3  augustss }
    389        1.3  augustss 
    390        1.3  augustss u_int32_t
    391        1.8  augustss midisyn_finetune(u_int32_t base_freq, int bend, int range, int vibrato_cents)
    392        1.3  augustss {
    393  1.17.2.10      chap 	static const u_int16_t semitone_tuning[24] =
    394        1.3  augustss 	{
    395  1.17.2.10      chap /*   0 */ 10000, 10595, 11225, 11892, 12599, 13348, 14142, 14983,
    396  1.17.2.10      chap /*   8 */ 15874, 16818, 17818, 18877, 20000, 21189, 22449, 23784,
    397        1.3  augustss /*  16 */ 25198, 26697, 28284, 29966, 31748, 33636, 35636, 37755
    398        1.3  augustss 	};
    399        1.7  jdolecek 	static const u_int16_t cent_tuning[100] =
    400        1.3  augustss 	{
    401  1.17.2.10      chap /*   0 */ 10000, 10006, 10012, 10017, 10023, 10029, 10035, 10041,
    402  1.17.2.10      chap /*   8 */ 10046, 10052, 10058, 10064, 10070, 10075, 10081, 10087,
    403  1.17.2.10      chap /*  16 */ 10093, 10099, 10105, 10110, 10116, 10122, 10128, 10134,
    404  1.17.2.10      chap /*  24 */ 10140, 10145, 10151, 10157, 10163, 10169, 10175, 10181,
    405  1.17.2.10      chap /*  32 */ 10187, 10192, 10198, 10204, 10210, 10216, 10222, 10228,
    406  1.17.2.10      chap /*  40 */ 10234, 10240, 10246, 10251, 10257, 10263, 10269, 10275,
    407  1.17.2.10      chap /*  48 */ 10281, 10287, 10293, 10299, 10305, 10311, 10317, 10323,
    408  1.17.2.10      chap /*  56 */ 10329, 10335, 10341, 10347, 10353, 10359, 10365, 10371,
    409  1.17.2.10      chap /*  64 */ 10377, 10383, 10389, 10395, 10401, 10407, 10413, 10419,
    410  1.17.2.10      chap /*  72 */ 10425, 10431, 10437, 10443, 10449, 10455, 10461, 10467,
    411  1.17.2.10      chap /*  80 */ 10473, 10479, 10485, 10491, 10497, 10503, 10509, 10515,
    412  1.17.2.10      chap /*  88 */ 10521, 10528, 10534, 10540, 10546, 10552, 10558, 10564,
    413        1.3  augustss /*  96 */ 10570, 10576, 10582, 10589
    414        1.3  augustss 	};
    415        1.3  augustss 	u_int32_t amount;
    416        1.3  augustss 	int negative, semitones, cents, multiplier;
    417        1.3  augustss 
    418        1.3  augustss 	if (range == 0)
    419        1.3  augustss 		return base_freq;
    420        1.3  augustss 
    421        1.3  augustss 	if (base_freq == 0)
    422        1.3  augustss 		return base_freq;
    423        1.3  augustss 
    424        1.3  augustss 	if (range >= 8192)
    425        1.3  augustss 		range = 8192;
    426        1.3  augustss 
    427        1.3  augustss 	bend = bend * range / 8192;
    428        1.3  augustss 	bend += vibrato_cents;
    429        1.3  augustss 
    430        1.3  augustss 	if (bend == 0)
    431        1.3  augustss 		return base_freq;
    432        1.3  augustss 
    433        1.3  augustss 	if (bend < 0) {
    434        1.3  augustss 		bend = -bend;
    435        1.3  augustss 		negative = 1;
    436  1.17.2.10      chap 	} else
    437        1.3  augustss 		negative = 0;
    438        1.3  augustss 
    439        1.3  augustss 	if (bend > range)
    440        1.3  augustss 		bend = range;
    441        1.3  augustss 
    442        1.3  augustss 	multiplier = 1;
    443        1.3  augustss 	while (bend > 2399) {
    444        1.3  augustss 		multiplier *= 4;
    445        1.3  augustss 		bend -= 2400;
    446        1.3  augustss 	}
    447        1.3  augustss 
    448        1.3  augustss 	semitones = bend / 100;
    449        1.3  augustss 	cents = bend % 100;
    450        1.3  augustss 
    451        1.3  augustss 	amount = semitone_tuning[semitones] * multiplier * cent_tuning[cents]
    452        1.3  augustss 		/ 10000;
    453        1.3  augustss 
    454        1.3  augustss 	if (negative)
    455        1.3  augustss 		return (base_freq * 10000 / amount);	/* Bend down */
    456        1.3  augustss 	else
    457        1.3  augustss 		return (base_freq * amount / 10000);	/* Bend up */
    458        1.1  augustss }
    459        1.1  augustss 
    460  1.17.2.12      chap static void
    461  1.17.2.12      chap midisyn_notify(void *cookie, midictl_evt evt,
    462  1.17.2.12      chap                uint_fast8_t chan, uint_fast16_t key)
    463  1.17.2.12      chap {
    464  1.17.2.12      chap }
    465  1.17.2.14      chap 
    466  1.17.2.14      chap int16_t
    467  1.17.2.14      chap midisyn_vol2cB(uint_fast16_t vol)
    468  1.17.2.14      chap {
    469  1.17.2.14      chap 	int16_t cB = 0;
    470  1.17.2.14      chap 	int32_t v;
    471  1.17.2.14      chap 
    472  1.17.2.14      chap 	if ( 0 == vol )
    473  1.17.2.14      chap 		return INT16_MIN;
    474  1.17.2.14      chap 	/*
    475  1.17.2.14      chap 	 * Adjust vol to fall in the range 8192..16383. Each doubling is
    476  1.17.2.14      chap 	 * worth 12 dB.
    477  1.17.2.14      chap 	 */
    478  1.17.2.14      chap 	while ( vol < 8192 ) {
    479  1.17.2.14      chap 		vol <<= 1;
    480  1.17.2.14      chap 		cB -= 120;
    481  1.17.2.14      chap 	}
    482  1.17.2.14      chap 	v = vol; /* ensure evaluation in signed 32 bit below */
    483  1.17.2.14      chap 	/*
    484  1.17.2.14      chap 	 * The GM vol-to-dB formula is dB = 40 log ( v / 127 ) for 7-bit v.
    485  1.17.2.14      chap 	 * The vol and expression controllers are in 14-bit space so the
    486  1.17.2.14      chap 	 * equivalent is 40 log ( v / 16256 ) - that is, MSB 127 LSB 0 because
    487  1.17.2.14      chap 	 * the LSB is commonly unused. MSB 127 LSB 127 would then be a tiny
    488  1.17.2.14      chap 	 * bit over.
    489  1.17.2.14      chap 	 * 1 dB resolution is a little coarser than we'd like, so let's shoot
    490  1.17.2.14      chap 	 * for centibels, i.e. 400 log ( v / 16256 ), and shift everything left
    491  1.17.2.14      chap 	 * as far as will fit in 32 bits, which turns out to be a shift of 22.
    492  1.17.2.14      chap 	 * This minimax polynomial approximation is good to about a centibel
    493  1.17.2.14      chap 	 * on the range 8192..16256, a shade worse (1.4 or so) above that.
    494  1.17.2.14      chap 	 * 26385/10166 is the 6th convergent of the coefficient for v^2.
    495  1.17.2.14      chap 	 */
    496  1.17.2.14      chap 	cB += ( v * ( 124828 - ( v * 26385 ) / 10166 ) - 1347349038 ) >> 22;
    497  1.17.2.14      chap 	return cB;
    498  1.17.2.14      chap }
    499  1.17.2.14      chap 
    500  1.17.2.14      chap uint32_t
    501  1.17.2.14      chap midisyn_mt2hz18(uint32_t mt)
    502  1.17.2.14      chap {
    503  1.17.2.14      chap 	int64_t t64a, t64b;
    504  1.17.2.14      chap 	uint_fast8_t shift;
    505  1.17.2.14      chap 
    506  1.17.2.14      chap 	/*
    507  1.17.2.14      chap 	 * Scale from the logarithmic MIDI-Tuning units to Hz<<18. Uses the
    508  1.17.2.14      chap 	 * continued-fraction form of a 2/2 rational function derived to
    509  1.17.2.14      chap 	 * cover the highest octave (mt 1900544..2097151 or 74.00.00..7f.7f.7f
    510  1.17.2.14      chap 	 * in RP-012-speak, the dotted bits are 7 wide) to produce Hz shifted
    511  1.17.2.14      chap 	 * left just as far as the maximum Hz will fit in a uint32, which
    512  1.17.2.14      chap 	 * turns out to be 18. Just shift off the result for lower octaves.
    513  1.17.2.14      chap 	 * Fit is within 1/4 MIDI tuning unit throughout (disclaimer: the
    514  1.17.2.14      chap 	 * comparison relied on the double-precision log in libm).
    515  1.17.2.14      chap 	 *
    516  1.17.2.14      chap 	 */
    517  1.17.2.14      chap 
    518  1.17.2.14      chap 	if ( 0 == mt )
    519  1.17.2.14      chap 		return 2143236;
    520  1.17.2.14      chap 
    521  1.17.2.14      chap 	for ( shift = 0; mt < 1900544; ++ shift )
    522  1.17.2.14      chap 		mt += 196608; /* 12 << 14 */
    523  1.17.2.14      chap 
    524  1.17.2.14      chap 	if ( 1998848 == mt )
    525  1.17.2.14      chap 		return UINT32_C(2463438621) >> shift;
    526  1.17.2.14      chap 
    527  1.17.2.14      chap 	t64a  = 0x5a1a0ee4; /* INT64_C(967879298788) gcc333: spurious warning */
    528  1.17.2.14      chap 	t64a |= (int64_t)0xe1 << 32;
    529  1.17.2.14      chap 	t64a /= (int32_t)mt - 1998848;
    530  1.17.2.14      chap 	t64a += (int32_t)mt - 3704981;
    531  1.17.2.14      chap 	t64b  = 0x6763759d; /* INT64_C(8405905567872413) and here too */
    532  1.17.2.14      chap 	t64b |= (int64_t)0x1ddd20 << 32;
    533  1.17.2.14      chap 	t64b /= t64a;
    534  1.17.2.14      chap 	t64b += UINT32_C(2463438619);
    535  1.17.2.14      chap 	return (uint32_t)t64b >> shift;
    536  1.17.2.14      chap }
    537