Home | History | Annotate | Line # | Download | only in dev
midisyn.c revision 1.15.4.3
      1  1.15.4.3      yamt /*	$NetBSD: midisyn.c,v 1.15.4.3 2007/09/03 14:33:15 yamt 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.15.4.3      yamt __KERNEL_RCSID(0, "$NetBSD: midisyn.c,v 1.15.4.3 2007/09/03 14:33:15 yamt 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.15.4.2      yamt uint_fast16_t	midisyn_allocvoice(midisyn *, uint_fast8_t, uint_fast8_t);
     73  1.15.4.2      yamt static void	midisyn_attackv_vel(midisyn *, uint_fast16_t, midipitch_t,
     74  1.15.4.2      yamt                                     int16_t, uint_fast8_t);
     75  1.15.4.2      yamt 
     76  1.15.4.2      yamt static midictl_notify midisyn_notify;
     77  1.15.4.2      yamt 
     78  1.15.4.2      yamt static midipitch_t midisyn_clamp_pitch(midipitch_t);
     79  1.15.4.2      yamt static int16_t midisyn_adj_level(midisyn *, uint_fast8_t);
     80  1.15.4.2      yamt static midipitch_t midisyn_adj_pitch(midisyn *, uint_fast8_t);
     81  1.15.4.2      yamt static void midisyn_chan_releasev(midisyn *, uint_fast8_t, uint_fast8_t);
     82  1.15.4.2      yamt static void midisyn_upd_level(midisyn *, uint_fast8_t);
     83  1.15.4.2      yamt static void midisyn_upd_pitch(midisyn *, uint_fast8_t);
     84       1.8  augustss 
     85      1.15     perry int	midisyn_open(void *, int,
     86       1.8  augustss 		     void (*iintr)(void *, int),
     87       1.8  augustss 		     void (*ointr)(void *), void *arg);
     88       1.8  augustss void	midisyn_close(void *);
     89  1.15.4.2      yamt int	midisyn_sysrt(void *, int);
     90       1.8  augustss void	midisyn_getinfo(void *, struct midi_info *);
     91  1.15.4.3      yamt int	midisyn_ioctl(void *, u_long, void *, int, struct lwp *);
     92       1.1  augustss 
     93      1.14      yamt const struct midi_hw_if midisyn_hw_if = {
     94       1.1  augustss 	midisyn_open,
     95       1.1  augustss 	midisyn_close,
     96  1.15.4.2      yamt 	midisyn_sysrt,
     97       1.1  augustss 	midisyn_getinfo,
     98       1.1  augustss 	midisyn_ioctl,
     99       1.1  augustss };
    100       1.1  augustss 
    101  1.15.4.2      yamt int	midisyn_channelmsg(void *, int, int, u_char *, int);
    102  1.15.4.2      yamt int	midisyn_commonmsg(void *, int, u_char *, int);
    103  1.15.4.2      yamt int	midisyn_sysex(void *, u_char *, int);
    104  1.15.4.2      yamt 
    105  1.15.4.2      yamt struct midi_hw_if_ext midisyn_hw_if_ext = {
    106  1.15.4.2      yamt 	.channel = midisyn_channelmsg,
    107  1.15.4.2      yamt 	.common  = midisyn_commonmsg,
    108  1.15.4.2      yamt 	.sysex   = midisyn_sysex,
    109  1.15.4.2      yamt };
    110  1.15.4.2      yamt 
    111  1.15.4.2      yamt struct channelstate { /* dyamically allocated in open() on account of size */
    112  1.15.4.2      yamt 	/* volume state components in centibels; just sum for overall level */
    113  1.15.4.2      yamt 	int16_t volume;
    114  1.15.4.2      yamt 	int16_t expression;
    115  1.15.4.2      yamt 	/* pitch state components in midipitch units; sum for overall effect */
    116  1.15.4.2      yamt 	midipitch_t bend;
    117  1.15.4.2      yamt 	midipitch_t tuning_fine;
    118  1.15.4.2      yamt 	midipitch_t tuning_coarse;
    119  1.15.4.2      yamt 	/* used by bend handlers */
    120  1.15.4.2      yamt 	int16_t bendraw;
    121  1.15.4.2      yamt 	int16_t pendingreset;
    122  1.15.4.2      yamt /* rearrange as more controls supported - 16 bits should last for a while */
    123  1.15.4.2      yamt #define PEND_VOL 1
    124  1.15.4.2      yamt #define PEND_EXP 2
    125  1.15.4.2      yamt #define PEND_LEVEL (PEND_VOL|PEND_EXP)
    126  1.15.4.2      yamt #define PEND_PBS 4
    127  1.15.4.2      yamt #define PEND_TNF 8
    128  1.15.4.2      yamt #define PEND_TNC 16
    129  1.15.4.2      yamt #define PEND_PITCH (PEND_PBS|PEND_TNF|PEND_TNC)
    130  1.15.4.2      yamt #define PEND_ALL   (PEND_LEVEL|PEND_PITCH)
    131  1.15.4.2      yamt };
    132       1.1  augustss 
    133       1.1  augustss int
    134       1.8  augustss midisyn_open(void *addr, int flags, void (*iintr)(void *, int),
    135  1.15.4.2      yamt     void (*ointr)(void *), void *arg)
    136       1.1  augustss {
    137       1.1  augustss 	midisyn *ms = addr;
    138  1.15.4.2      yamt 	int rslt;
    139  1.15.4.2      yamt 	uint_fast8_t chan;
    140       1.1  augustss 
    141       1.1  augustss 	DPRINTF(("midisyn_open: ms=%p ms->mets=%p\n", ms, ms->mets));
    142  1.15.4.2      yamt 
    143  1.15.4.2      yamt 	midictl_open(&ms->ctl);
    144  1.15.4.2      yamt 
    145  1.15.4.2      yamt 	ms->chnstate = malloc(MIDI_MAX_CHANS*sizeof *(ms->chnstate),
    146  1.15.4.2      yamt 	                      M_DEVBUF, M_WAITOK); /* init'd by RESET below */
    147  1.15.4.2      yamt 
    148  1.15.4.2      yamt 	rslt = 0;
    149       1.1  augustss 	if (ms->mets->open)
    150  1.15.4.2      yamt 		rslt = (ms->mets->open(ms, flags));
    151  1.15.4.2      yamt 
    152  1.15.4.2      yamt 	/*
    153  1.15.4.2      yamt 	 * Make the right initial things happen by faking receipt of RESET on
    154  1.15.4.2      yamt 	 * all channels. The hw driver's ctlnotice() will be called in turn.
    155  1.15.4.2      yamt 	 */
    156  1.15.4.2      yamt 	for ( chan = 0 ; chan < MIDI_MAX_CHANS ; ++ chan )
    157  1.15.4.2      yamt 		midisyn_notify(ms, MIDICTL_RESET, chan, 0);
    158  1.15.4.2      yamt 
    159  1.15.4.2      yamt 	return rslt;
    160       1.1  augustss }
    161       1.1  augustss 
    162       1.1  augustss void
    163       1.8  augustss midisyn_close(void *addr)
    164       1.1  augustss {
    165       1.1  augustss 	midisyn *ms = addr;
    166       1.4   mycroft 	struct midisyn_methods *fs;
    167  1.15.4.2      yamt 	int chan;
    168       1.1  augustss 
    169       1.1  augustss 	DPRINTF(("midisyn_close: ms=%p ms->mets=%p\n", ms, ms->mets));
    170       1.4   mycroft 	fs = ms->mets;
    171  1.15.4.2      yamt 
    172  1.15.4.2      yamt 	for (chan = 0; chan < MIDI_MAX_CHANS; chan++)
    173  1.15.4.2      yamt 		midisyn_notify(ms, MIDICTL_SOUND_OFF, chan, 0);
    174  1.15.4.2      yamt 
    175       1.4   mycroft 	if (fs->close)
    176       1.4   mycroft 		fs->close(ms);
    177  1.15.4.2      yamt 
    178  1.15.4.2      yamt 	free(ms->chnstate, M_DEVBUF);
    179  1.15.4.2      yamt 
    180  1.15.4.2      yamt 	midictl_close(&ms->ctl);
    181       1.1  augustss }
    182       1.1  augustss 
    183       1.1  augustss void
    184       1.8  augustss midisyn_getinfo(void *addr, struct midi_info *mi)
    185       1.1  augustss {
    186       1.1  augustss 	midisyn *ms = addr;
    187       1.1  augustss 
    188       1.1  augustss 	mi->name = ms->name;
    189  1.15.4.2      yamt 	/*
    190  1.15.4.2      yamt 	 * I was going to add a property here to suppress midi(4)'s warning
    191  1.15.4.2      yamt 	 * about an output device that uses no transmit interrupt, on the
    192  1.15.4.2      yamt 	 * assumption that as an onboard synth we handle "output" internally
    193  1.15.4.2      yamt 	 * with nothing like the 320 us per byte busy wait of a dumb UART.
    194  1.15.4.2      yamt 	 * Then I noticed that opl (at least as currently implemented) seems
    195  1.15.4.2      yamt 	 * to need 40 us busy wait to set each register on an OPL2, and sets
    196  1.15.4.2      yamt 	 * about 21 registers for every note-on. (Half of that is patch loading
    197  1.15.4.2      yamt 	 * and could probably be reduced by different management of voices and
    198  1.15.4.2      yamt 	 * patches.) For now I won't bother suppressing that warning....
    199  1.15.4.2      yamt 	 */
    200       1.1  augustss 	mi->props = 0;
    201  1.15.4.2      yamt 
    202  1.15.4.2      yamt 	midi_register_hw_if_ext(&midisyn_hw_if_ext);
    203       1.1  augustss }
    204       1.1  augustss 
    205       1.1  augustss int
    206  1.15.4.3      yamt midisyn_ioctl(void *maddr, u_long cmd, void *addr, int flag, struct lwp *l)
    207       1.1  augustss {
    208       1.1  augustss 	midisyn *ms = maddr;
    209       1.1  augustss 
    210       1.1  augustss 	if (ms->mets->ioctl)
    211  1.15.4.1      yamt 		return (ms->mets->ioctl(ms, cmd, addr, flag, l));
    212       1.1  augustss 	else
    213       1.1  augustss 		return (EINVAL);
    214       1.1  augustss }
    215       1.1  augustss 
    216       1.1  augustss int
    217       1.8  augustss midisyn_findvoice(midisyn *ms, int chan, int note)
    218       1.1  augustss {
    219       1.1  augustss 	u_int cn;
    220       1.1  augustss 	int v;
    221       1.1  augustss 
    222       1.3  augustss 	cn = MS_CHANNOTE(chan, note);
    223       1.1  augustss 	for (v = 0; v < ms->nvoice; v++)
    224       1.3  augustss 		if (ms->voices[v].chan_note == cn && ms->voices[v].inuse)
    225       1.1  augustss 			return (v);
    226       1.1  augustss 	return (-1);
    227       1.1  augustss }
    228       1.1  augustss 
    229       1.1  augustss void
    230       1.8  augustss midisyn_attach(struct midi_softc *sc, midisyn *ms)
    231       1.1  augustss {
    232  1.15.4.2      yamt 	/*
    233  1.15.4.2      yamt 	 * XXX there should be a way for this function to indicate failure
    234  1.15.4.2      yamt 	 * (other than panic) if some preconditions aren't met, for example
    235  1.15.4.2      yamt 	 * if some nonoptional methods are missing.
    236  1.15.4.2      yamt 	 */
    237  1.15.4.2      yamt 	if (ms->mets->allocv == 0) {
    238      1.15     perry 		ms->voices = malloc(ms->nvoice * sizeof (struct voice),
    239      1.10   tsutsui 				    M_DEVBUF, M_WAITOK|M_ZERO);
    240       1.1  augustss 		ms->seqno = 1;
    241  1.15.4.2      yamt 		ms->mets->allocv = midisyn_allocvoice;
    242       1.1  augustss 	}
    243  1.15.4.2      yamt 
    244  1.15.4.2      yamt 	if (ms->mets->attackv_vel == 0 && ms->mets->attackv != 0)
    245  1.15.4.2      yamt 		ms->mets->attackv_vel = midisyn_attackv_vel;
    246  1.15.4.2      yamt 
    247  1.15.4.2      yamt 	ms->ctl = (midictl) {
    248  1.15.4.2      yamt 		.base_channel = 16,
    249  1.15.4.2      yamt 		.cookie = ms,
    250  1.15.4.2      yamt 		.notify = midisyn_notify
    251  1.15.4.2      yamt 	};
    252  1.15.4.2      yamt 
    253       1.1  augustss 	sc->hw_if = &midisyn_hw_if;
    254       1.1  augustss 	sc->hw_hdl = ms;
    255       1.1  augustss 	DPRINTF(("midisyn_attach: ms=%p\n", sc->hw_hdl));
    256       1.1  augustss }
    257       1.1  augustss 
    258       1.1  augustss void
    259       1.8  augustss midisyn_freevoice(midisyn *ms, int voice)
    260       1.1  augustss {
    261  1.15.4.2      yamt 	if (ms->mets->allocv != midisyn_allocvoice)
    262       1.1  augustss 		return;
    263       1.3  augustss 	ms->voices[voice].inuse = 0;
    264       1.1  augustss }
    265       1.1  augustss 
    266  1.15.4.2      yamt uint_fast16_t
    267  1.15.4.2      yamt midisyn_allocvoice(midisyn *ms, uint_fast8_t chan, uint_fast8_t note)
    268       1.1  augustss {
    269       1.1  augustss 	int bestv, v;
    270       1.1  augustss 	u_int bestseq, s;
    271       1.1  augustss 
    272       1.1  augustss 	/* Find a free voice, or if no free voice is found the oldest. */
    273       1.1  augustss 	bestv = 0;
    274       1.3  augustss 	bestseq = ms->voices[0].seqno + (ms->voices[0].inuse ? 0x40000000 : 0);
    275       1.1  augustss 	for (v = 1; v < ms->nvoice; v++) {
    276       1.1  augustss 		s = ms->voices[v].seqno;
    277       1.3  augustss 		if (ms->voices[v].inuse)
    278       1.3  augustss 			s += 0x40000000;
    279       1.1  augustss 		if (s < bestseq) {
    280       1.1  augustss 			bestseq = s;
    281       1.1  augustss 			bestv = v;
    282       1.1  augustss 		}
    283       1.1  augustss 	}
    284       1.3  augustss 	DPRINTFN(10,("midisyn_allocvoice: v=%d seq=%d cn=%x inuse=%d\n",
    285      1.15     perry 		     bestv, ms->voices[bestv].seqno,
    286       1.3  augustss 		     ms->voices[bestv].chan_note,
    287       1.3  augustss 		     ms->voices[bestv].inuse));
    288       1.3  augustss #ifdef AUDIO_DEBUG
    289       1.3  augustss 	if (ms->voices[bestv].inuse)
    290      1.15     perry 		DPRINTFN(1,("midisyn_allocvoice: steal %x\n",
    291       1.3  augustss 			    ms->voices[bestv].chan_note));
    292       1.3  augustss #endif
    293       1.3  augustss 	ms->voices[bestv].chan_note = MS_CHANNOTE(chan, note);
    294       1.1  augustss 	ms->voices[bestv].seqno = ms->seqno++;
    295       1.3  augustss 	ms->voices[bestv].inuse = 1;
    296       1.1  augustss 	return (bestv);
    297       1.1  augustss }
    298       1.1  augustss 
    299  1.15.4.2      yamt /* dummy attackv_vel that just adds vel into level for simple drivers */
    300  1.15.4.2      yamt static void
    301  1.15.4.2      yamt midisyn_attackv_vel(midisyn *ms, uint_fast16_t voice, midipitch_t mp,
    302  1.15.4.2      yamt                     int16_t level_cB, uint_fast8_t vel)
    303  1.15.4.2      yamt {
    304  1.15.4.2      yamt 	ms->voices[voice].velcB = midisyn_vol2cB((uint_fast16_t)vel << 7);
    305  1.15.4.2      yamt 	ms->mets->attackv(ms, voice, mp, level_cB + ms->voices[voice].velcB);
    306  1.15.4.2      yamt }
    307  1.15.4.2      yamt 
    308       1.1  augustss int
    309  1.15.4.2      yamt midisyn_sysrt(void *addr, int b)
    310  1.15.4.2      yamt {
    311  1.15.4.2      yamt 	return 0;
    312  1.15.4.2      yamt }
    313  1.15.4.2      yamt 
    314  1.15.4.2      yamt int midisyn_channelmsg(void *addr, int status, int chan, u_char *buf,
    315  1.15.4.2      yamt     int len)
    316       1.1  augustss {
    317       1.1  augustss 	midisyn *ms = addr;
    318       1.1  augustss 	int voice = 0;		/* initialize to keep gcc quiet */
    319       1.1  augustss 	struct midisyn_methods *fs;
    320       1.1  augustss 
    321  1.15.4.2      yamt 	DPRINTF(("midisyn_channelmsg: ms=%p status=%#02x chan=%d\n",
    322  1.15.4.2      yamt 	       ms, status, chan));
    323       1.1  augustss 	fs = ms->mets;
    324  1.15.4.2      yamt 
    325  1.15.4.2      yamt 	switch (status) {
    326       1.1  augustss 	case MIDI_NOTEOFF:
    327  1.15.4.2      yamt 		/*
    328  1.15.4.2      yamt 		 * for a device that leaves voice allocation to us--and that's
    329  1.15.4.2      yamt 		 * all of 'em at the moment--the voice and release velocity
    330  1.15.4.2      yamt 		 * should be the only necessary arguments to noteoff. what use
    331  1.15.4.2      yamt 		 * are they making of note? checking... None. Cool.
    332  1.15.4.2      yamt 		 * IF there is ever a device added that does its own allocation,
    333  1.15.4.2      yamt 		 * extend the interface; this findvoice won't be what to do...
    334  1.15.4.2      yamt 		 */
    335  1.15.4.2      yamt 		voice = midisyn_findvoice(ms, chan, buf[1]);
    336       1.1  augustss 		if (voice >= 0) {
    337  1.15.4.2      yamt 			fs->releasev(ms, voice, buf[2]);
    338       1.1  augustss 			midisyn_freevoice(ms, voice);
    339       1.1  augustss 		}
    340       1.1  augustss 		break;
    341       1.1  augustss 	case MIDI_NOTEON:
    342  1.15.4.2      yamt 		/*
    343  1.15.4.2      yamt 		 * what's called for here, given current drivers, is an i/f
    344  1.15.4.2      yamt 		 * where midisyn computes a volume from vel*volume*expression*
    345  1.15.4.2      yamt 		 * mastervolume and passes that result as a single arg. It can
    346  1.15.4.2      yamt 		 * evolve later to support drivers that expose some of those
    347  1.15.4.2      yamt 		 * bits separately (e.g. a driver could expose a mixer register
    348  1.15.4.2      yamt 		 * on its sound card and use that for mastervolume).
    349  1.15.4.2      yamt 		 */
    350  1.15.4.2      yamt 		voice = fs->allocv(ms, chan, buf[1]);
    351  1.15.4.2      yamt 		ms->voices[voice].velcB = 0; /* assume driver handles vel */
    352  1.15.4.2      yamt 		fs->attackv_vel(ms, voice,
    353  1.15.4.2      yamt 		    midisyn_clamp_pitch(MIDIPITCH_FROM_KEY(buf[1]) +
    354  1.15.4.2      yamt 		                        midisyn_adj_pitch(ms, chan)),
    355  1.15.4.2      yamt 		    midisyn_adj_level(ms,chan), buf[2]);
    356       1.1  augustss 		break;
    357       1.1  augustss 	case MIDI_KEY_PRESSURE:
    358  1.15.4.2      yamt 		/*
    359  1.15.4.2      yamt 		 * unimplemented by the existing drivers. if we are doing
    360  1.15.4.2      yamt 		 * voice allocation, find the voice that corresponds to this
    361  1.15.4.2      yamt 		 * chan/note and define a method that passes the voice and
    362  1.15.4.2      yamt 		 * pressure to the driver ... not the note, /it/ doesn't matter.
    363  1.15.4.2      yamt 		 * For a driver that does its own allocation, a different
    364  1.15.4.2      yamt 		 * method may be needed passing pressure, chan, note so it can
    365  1.15.4.2      yamt 		 * find the right voice on its own. Be sure that whatever is
    366  1.15.4.2      yamt 		 * done here is undone when midisyn_notify sees MIDICTL_RESET.
    367  1.15.4.2      yamt 		 */
    368       1.1  augustss 		break;
    369       1.1  augustss 	case MIDI_CTL_CHANGE:
    370  1.15.4.2      yamt 		midictl_change(&ms->ctl, chan, buf+1);
    371       1.1  augustss 		break;
    372       1.1  augustss 	case MIDI_PGM_CHANGE:
    373       1.1  augustss 		if (fs->pgmchg)
    374  1.15.4.2      yamt 			fs->pgmchg(ms, chan, buf[1]);
    375       1.1  augustss 		break;
    376       1.1  augustss 	case MIDI_CHN_PRESSURE:
    377  1.15.4.2      yamt 		/*
    378  1.15.4.2      yamt 		 * unimplemented by the existing drivers. if driver exposes no
    379  1.15.4.2      yamt 		 * distinct method, can use KEY_PRESSURE method for each voice
    380  1.15.4.2      yamt 		 * on channel. Be sure that whatever is
    381  1.15.4.2      yamt 		 * done here is undone when midisyn_notify sees MIDICTL_RESET.
    382  1.15.4.2      yamt 		 */
    383       1.1  augustss 		break;
    384       1.1  augustss 	case MIDI_PITCH_BEND:
    385  1.15.4.2      yamt 		/*
    386  1.15.4.2      yamt 		 * Will work for most drivers that simply render the midipitch
    387  1.15.4.2      yamt 		 * as we pass it (but not cms, which chops all the bits after
    388  1.15.4.2      yamt 		 * the note number and then computes its own pitch :( ). If the
    389  1.15.4.2      yamt 		 * driver has a repitchv method for voices already sounding, so
    390  1.15.4.2      yamt 		 * much the better.
    391  1.15.4.2      yamt 		 * The bending logic lives in the handler for bend sensitivity,
    392  1.15.4.2      yamt 		 * so fake a change to that to kick it off.
    393  1.15.4.2      yamt 		 */
    394  1.15.4.2      yamt 		ms->chnstate[chan].bendraw = buf[2]<<7 | buf[1];
    395  1.15.4.2      yamt 		ms->chnstate[chan].bendraw -= MIDI_BEND_NEUTRAL;
    396  1.15.4.2      yamt 		midisyn_notify(ms, MIDICTL_RPN, chan,
    397  1.15.4.2      yamt 		               MIDI_RPN_PITCH_BEND_SENSITIVITY);
    398       1.1  augustss 		break;
    399       1.1  augustss 	}
    400  1.15.4.2      yamt 	return 0;
    401       1.1  augustss }
    402       1.1  augustss 
    403  1.15.4.2      yamt int midisyn_commonmsg(void *addr, int status,
    404  1.15.4.2      yamt     u_char *buf, int len)
    405       1.1  augustss {
    406  1.15.4.2      yamt 	return 0;
    407  1.15.4.2      yamt }
    408       1.3  augustss 
    409  1.15.4.2      yamt int midisyn_sysex(void *addr, u_char *buf, int len)
    410  1.15.4.2      yamt {
    411  1.15.4.2      yamt 	/*
    412  1.15.4.2      yamt 	 * unimplemented by existing drivers. it is surely more sensible
    413  1.15.4.2      yamt 	 * to do some parsing of well-defined sysex messages here, either
    414  1.15.4.2      yamt 	 * handling them internally or calling specific methods on the
    415  1.15.4.2      yamt 	 * driver after parsing out the details, than to ask every driver
    416  1.15.4.2      yamt 	 * to deal with sysex messages poked at it a byte at a time.
    417  1.15.4.2      yamt 	 */
    418  1.15.4.2      yamt 	return 0;
    419  1.15.4.2      yamt }
    420  1.15.4.2      yamt 
    421  1.15.4.2      yamt static void
    422  1.15.4.2      yamt midisyn_notify(void *cookie, midictl_evt evt,
    423  1.15.4.2      yamt                uint_fast8_t chan, uint_fast16_t key)
    424  1.15.4.2      yamt {
    425  1.15.4.2      yamt 	struct midisyn *ms;
    426  1.15.4.2      yamt 	int drvhandled;
    427  1.15.4.2      yamt 
    428  1.15.4.2      yamt 	ms = (struct midisyn *)cookie;
    429  1.15.4.2      yamt 	drvhandled = 0;
    430  1.15.4.2      yamt 	if ( ms->mets->ctlnotice )
    431  1.15.4.2      yamt 		drvhandled = ms->mets->ctlnotice(ms, evt, chan, key);
    432  1.15.4.2      yamt 
    433  1.15.4.2      yamt 	switch ( evt | key ) {
    434  1.15.4.2      yamt 	case MIDICTL_RESET:
    435  1.15.4.2      yamt 		/*
    436  1.15.4.2      yamt 		 * Re-read all ctls we use, revert pitchbend state.
    437  1.15.4.2      yamt 		 * Can do it by faking change notifications.
    438  1.15.4.2      yamt 		 */
    439  1.15.4.2      yamt 		ms->chnstate[chan].pendingreset |= PEND_ALL;
    440  1.15.4.2      yamt 		midisyn_notify(ms, MIDICTL_CTLR, chan,
    441  1.15.4.2      yamt 		               MIDI_CTRL_CHANNEL_VOLUME_MSB);
    442  1.15.4.2      yamt 		midisyn_notify(ms, MIDICTL_CTLR, chan,
    443  1.15.4.2      yamt 		               MIDI_CTRL_EXPRESSION_MSB);
    444  1.15.4.2      yamt 		ms->chnstate[chan].bendraw = 0; /* MIDI_BEND_NEUTRAL - itself */
    445  1.15.4.2      yamt 		midisyn_notify(ms, MIDICTL_RPN, chan,
    446  1.15.4.2      yamt 		               MIDI_RPN_PITCH_BEND_SENSITIVITY);
    447  1.15.4.2      yamt 		midisyn_notify(ms, MIDICTL_RPN, chan,
    448  1.15.4.2      yamt 		               MIDI_RPN_CHANNEL_FINE_TUNING);
    449  1.15.4.2      yamt 		midisyn_notify(ms, MIDICTL_RPN, chan,
    450  1.15.4.2      yamt 		               MIDI_RPN_CHANNEL_COARSE_TUNING);
    451  1.15.4.2      yamt 		break;
    452  1.15.4.2      yamt 	case MIDICTL_NOTES_OFF:
    453  1.15.4.2      yamt 		if ( drvhandled )
    454  1.15.4.2      yamt 			break;
    455  1.15.4.2      yamt 		/* releasev all voices sounding on chan; use normal vel 64 */
    456  1.15.4.2      yamt 		midisyn_chan_releasev(ms, chan, 64);
    457  1.15.4.2      yamt 		break;
    458  1.15.4.2      yamt 	case MIDICTL_SOUND_OFF:
    459  1.15.4.2      yamt 		if ( drvhandled )
    460  1.15.4.2      yamt 			break;
    461  1.15.4.2      yamt 		/* releasev all voices sounding on chan; use max vel 127 */
    462  1.15.4.2      yamt 		/* it is really better for driver to handle this, instantly */
    463  1.15.4.2      yamt 		midisyn_chan_releasev(ms, chan, 127);
    464  1.15.4.2      yamt 		break;
    465  1.15.4.2      yamt 	case MIDICTL_CTLR | MIDI_CTRL_CHANNEL_VOLUME_MSB:
    466  1.15.4.2      yamt 		ms->chnstate[chan].pendingreset &= ~PEND_VOL;
    467  1.15.4.2      yamt 		if ( drvhandled ) {
    468  1.15.4.2      yamt 			ms->chnstate[chan].volume = 0;
    469  1.15.4.2      yamt 			break;
    470  1.15.4.2      yamt 		}
    471  1.15.4.2      yamt 		ms->chnstate[chan].volume = midisyn_vol2cB(
    472  1.15.4.2      yamt 	    	    midictl_read(&ms->ctl, chan, key, 100<<7));
    473  1.15.4.2      yamt 		midisyn_upd_level(ms, chan);
    474  1.15.4.2      yamt 		break;
    475  1.15.4.2      yamt 	case MIDICTL_CTLR | MIDI_CTRL_EXPRESSION_MSB:
    476  1.15.4.2      yamt 		ms->chnstate[chan].pendingreset &= ~PEND_EXP;
    477  1.15.4.2      yamt 		if ( drvhandled ) {
    478  1.15.4.2      yamt 			ms->chnstate[chan].expression = 0;
    479  1.15.4.2      yamt 			break;
    480  1.15.4.2      yamt 		}
    481  1.15.4.2      yamt 		ms->chnstate[chan].expression = midisyn_vol2cB(
    482  1.15.4.2      yamt 	    	    midictl_read(&ms->ctl, chan, key, 16383));
    483  1.15.4.2      yamt 		midisyn_upd_level(ms, chan);
    484  1.15.4.2      yamt 		break;
    485  1.15.4.2      yamt 	/*
    486  1.15.4.2      yamt 	 * SOFT_PEDAL: supporting this will be trickier; must apply only
    487  1.15.4.2      yamt 	 * to notes subsequently struck, and must remember which voices
    488  1.15.4.2      yamt 	 * they are for follow-on adjustments. For another day....
    489  1.15.4.2      yamt 	 */
    490  1.15.4.2      yamt 	case MIDICTL_RPN | MIDI_RPN_PITCH_BEND_SENSITIVITY:
    491  1.15.4.2      yamt 		ms->chnstate[chan].pendingreset &= ~PEND_PBS;
    492  1.15.4.2      yamt 		if ( drvhandled )
    493  1.15.4.2      yamt 			ms->chnstate[chan].bend = 0;
    494  1.15.4.2      yamt 		else {
    495  1.15.4.2      yamt 			uint16_t w;
    496  1.15.4.2      yamt 			int8_t semis, cents;
    497  1.15.4.2      yamt 			w = midictl_rpn_read(&ms->ctl, chan, key, 2<<7);
    498  1.15.4.2      yamt 			semis = w>>7;
    499  1.15.4.2      yamt 			cents = w&0x7f;
    500  1.15.4.2      yamt 			/*
    501  1.15.4.2      yamt 			 * Mathematically, multiply semis by
    502  1.15.4.2      yamt 			 * MIDIPITCH_SEMITONE*bendraw/8192. Practically, avoid
    503  1.15.4.2      yamt 			 * shifting significant bits off by observing that
    504  1.15.4.2      yamt 			 * MIDIPITCH_SEMITONE == 1<<14 and 8192 == 1<<13, so
    505  1.15.4.2      yamt 			 * just take semis*bendraw<<1. Do the same with cents
    506  1.15.4.2      yamt 			 * except <<1 becomes /50 (but rounded).
    507  1.15.4.2      yamt 			 */
    508  1.15.4.2      yamt 			ms->chnstate[chan].bend =
    509  1.15.4.2      yamt 			    ( ms->chnstate[chan].bendraw * semis ) << 1;
    510  1.15.4.2      yamt 			ms->chnstate[chan].bend +=
    511  1.15.4.2      yamt 			    ((ms->chnstate[chan].bendraw * cents)/25 + 1) >> 1;
    512  1.15.4.2      yamt 			midisyn_upd_pitch(ms, chan);
    513  1.15.4.2      yamt 		}
    514  1.15.4.2      yamt 		break;
    515  1.15.4.2      yamt 	case MIDICTL_RPN | MIDI_RPN_CHANNEL_FINE_TUNING:
    516  1.15.4.2      yamt 		if ( drvhandled )
    517  1.15.4.2      yamt 			ms->chnstate[chan].tuning_fine = 0;
    518  1.15.4.2      yamt 		else {
    519  1.15.4.2      yamt 			midipitch_t mp;
    520  1.15.4.2      yamt 			mp = midictl_rpn_read(&ms->ctl, chan, key, 8192);
    521  1.15.4.2      yamt 			/*
    522  1.15.4.2      yamt 			 * Mathematically, subtract 8192 and scale by
    523  1.15.4.2      yamt 			 * MIDIPITCH_SEMITONE/8192. Practically, subtract 8192
    524  1.15.4.2      yamt 			 * and then << 1.
    525  1.15.4.2      yamt 			 */
    526  1.15.4.2      yamt 			ms->chnstate[chan].tuning_fine = ( mp - 8192 ) << 1;
    527  1.15.4.2      yamt 			midisyn_upd_pitch(ms, chan);
    528  1.15.4.2      yamt 		}
    529  1.15.4.2      yamt 		break;
    530  1.15.4.2      yamt 	case MIDICTL_RPN | MIDI_RPN_CHANNEL_COARSE_TUNING:
    531  1.15.4.2      yamt 		ms->chnstate[chan].pendingreset &= ~PEND_TNC;
    532  1.15.4.2      yamt 		if ( drvhandled )
    533  1.15.4.2      yamt 			ms->chnstate[chan].tuning_coarse = 0;
    534  1.15.4.2      yamt 		else {
    535  1.15.4.2      yamt 			midipitch_t mp;
    536  1.15.4.2      yamt 			/*
    537  1.15.4.2      yamt 			 * By definition only the MSB of this parameter is used.
    538  1.15.4.2      yamt 			 * Subtract 64 for a signed count of semitones; << 14
    539  1.15.4.2      yamt 			 * will convert to midipitch scale.
    540  1.15.4.2      yamt 			 */
    541  1.15.4.2      yamt 			mp = midictl_rpn_read(&ms->ctl, chan, key, 64<<7) >> 7;
    542  1.15.4.2      yamt 			ms->chnstate[chan].tuning_coarse = ( mp - 64 ) << 14;
    543  1.15.4.2      yamt 			midisyn_upd_pitch(ms, chan);
    544  1.15.4.2      yamt 		}
    545  1.15.4.2      yamt 		break;
    546  1.15.4.2      yamt 	}
    547  1.15.4.2      yamt }
    548       1.3  augustss 
    549  1.15.4.2      yamt static midipitch_t
    550  1.15.4.2      yamt midisyn_clamp_pitch(midipitch_t mp)
    551  1.15.4.2      yamt {
    552  1.15.4.2      yamt 	if ( mp <= 0 )
    553  1.15.4.2      yamt 		return 0;
    554  1.15.4.2      yamt 	if ( mp >= MIDIPITCH_MAX )
    555  1.15.4.2      yamt 		return MIDIPITCH_MAX;
    556  1.15.4.2      yamt 	return mp;
    557  1.15.4.2      yamt }
    558  1.15.4.2      yamt 
    559  1.15.4.2      yamt static int16_t
    560  1.15.4.2      yamt midisyn_adj_level(midisyn *ms, uint_fast8_t chan)
    561  1.15.4.2      yamt {
    562  1.15.4.2      yamt 	int32_t level;
    563  1.15.4.2      yamt 
    564  1.15.4.2      yamt 	level = ms->chnstate[chan].volume + ms->chnstate[chan].expression;
    565  1.15.4.2      yamt 	if ( level <= INT16_MIN )
    566  1.15.4.2      yamt 		return INT16_MIN;
    567  1.15.4.2      yamt 	return level;
    568  1.15.4.2      yamt }
    569  1.15.4.2      yamt 
    570  1.15.4.2      yamt static midipitch_t
    571  1.15.4.2      yamt midisyn_adj_pitch(midisyn *ms, uint_fast8_t chan)
    572  1.15.4.2      yamt {
    573  1.15.4.2      yamt 	struct channelstate *s = ms->chnstate + chan;
    574  1.15.4.2      yamt 	return s->bend + s->tuning_fine +s->tuning_coarse;
    575  1.15.4.2      yamt }
    576  1.15.4.2      yamt 
    577  1.15.4.2      yamt #define VOICECHAN_FOREACH_BEGIN(ms,vp,ch)			\
    578  1.15.4.2      yamt 	{							\
    579  1.15.4.2      yamt 		struct voice *vp, *_end_##vp;			\
    580  1.15.4.2      yamt 		for (vp=(ms)->voices,_end_##vp=vp+(ms)->nvoice;	\
    581  1.15.4.2      yamt 		    vp < _end_##vp; ++ vp) {			\
    582  1.15.4.2      yamt 			if ( !vp->inuse )			\
    583  1.15.4.2      yamt 				continue;			\
    584  1.15.4.2      yamt 			if ( MS_GETCHAN(vp) == (ch) )		\
    585  1.15.4.2      yamt 				;				\
    586  1.15.4.2      yamt 			else					\
    587  1.15.4.2      yamt 				continue;
    588  1.15.4.2      yamt #define VOICECHAN_FOREACH_END }}
    589  1.15.4.2      yamt 
    590  1.15.4.2      yamt static void
    591  1.15.4.2      yamt midisyn_chan_releasev(midisyn *ms, uint_fast8_t chan, uint_fast8_t vel)
    592  1.15.4.2      yamt {
    593  1.15.4.2      yamt 	VOICECHAN_FOREACH_BEGIN(ms,vp,chan)
    594  1.15.4.2      yamt 		ms->mets->releasev(ms, vp - ms->voices, vel);
    595  1.15.4.2      yamt 		midisyn_freevoice(ms, vp - ms->voices);
    596  1.15.4.2      yamt 	VOICECHAN_FOREACH_END
    597  1.15.4.2      yamt }
    598  1.15.4.2      yamt 
    599  1.15.4.2      yamt static void
    600  1.15.4.2      yamt midisyn_upd_level(midisyn *ms, uint_fast8_t chan)
    601  1.15.4.2      yamt {
    602  1.15.4.2      yamt 	int32_t level;
    603  1.15.4.2      yamt 	int16_t chan_level;
    604  1.15.4.2      yamt 	if ( NULL == ms->mets->relevelv )
    605  1.15.4.2      yamt 		return;
    606  1.15.4.2      yamt 
    607  1.15.4.2      yamt 	if ( ms->chnstate[chan].pendingreset & PEND_LEVEL )
    608  1.15.4.2      yamt 		return;
    609       1.3  augustss 
    610  1.15.4.2      yamt 	chan_level = midisyn_adj_level(ms, chan);
    611  1.15.4.2      yamt 
    612  1.15.4.2      yamt 	VOICECHAN_FOREACH_BEGIN(ms,vp,chan)
    613  1.15.4.2      yamt 		level = vp->velcB + chan_level;
    614  1.15.4.2      yamt 		ms->mets->relevelv(ms, vp - ms->voices,
    615  1.15.4.2      yamt 		    level <= INT16_MIN ? INT16_MIN : level);
    616  1.15.4.2      yamt 	VOICECHAN_FOREACH_END
    617  1.15.4.2      yamt }
    618       1.3  augustss 
    619  1.15.4.2      yamt static void
    620  1.15.4.2      yamt midisyn_upd_pitch(midisyn *ms, uint_fast8_t chan)
    621  1.15.4.2      yamt {
    622  1.15.4.2      yamt 	midipitch_t chan_adj;
    623  1.15.4.2      yamt 
    624  1.15.4.2      yamt 	if ( NULL == ms->mets->repitchv )
    625  1.15.4.2      yamt 		return;
    626  1.15.4.2      yamt 
    627  1.15.4.2      yamt 	if ( ms->chnstate[chan].pendingreset & PEND_PITCH )
    628  1.15.4.2      yamt 		return;
    629       1.3  augustss 
    630  1.15.4.2      yamt 	chan_adj = midisyn_adj_pitch(ms, chan);
    631  1.15.4.2      yamt 
    632  1.15.4.2      yamt 	VOICECHAN_FOREACH_BEGIN(ms,vp,chan)
    633  1.15.4.2      yamt 		ms->mets->repitchv(ms, vp - ms->voices,
    634  1.15.4.2      yamt 		    midisyn_clamp_pitch(chan_adj +
    635  1.15.4.2      yamt 		        MIDIPITCH_FROM_KEY(vp->chan_note&0x7f)));
    636  1.15.4.2      yamt 	VOICECHAN_FOREACH_END
    637  1.15.4.2      yamt }
    638  1.15.4.2      yamt 
    639  1.15.4.2      yamt #undef VOICECHAN_FOREACH_END
    640  1.15.4.2      yamt #undef VOICECHAN_FOREACH_BEGIN
    641  1.15.4.2      yamt 
    642  1.15.4.2      yamt int16_t
    643  1.15.4.2      yamt midisyn_vol2cB(uint_fast16_t vol)
    644  1.15.4.2      yamt {
    645  1.15.4.2      yamt 	int16_t cB = 0;
    646  1.15.4.2      yamt 	int32_t v;
    647  1.15.4.2      yamt 
    648  1.15.4.2      yamt 	if ( 0 == vol )
    649  1.15.4.2      yamt 		return INT16_MIN;
    650  1.15.4.2      yamt 	/*
    651  1.15.4.2      yamt 	 * Adjust vol to fall in the range 8192..16383. Each doubling is
    652  1.15.4.2      yamt 	 * worth 12 dB.
    653  1.15.4.2      yamt 	 */
    654  1.15.4.2      yamt 	while ( vol < 8192 ) {
    655  1.15.4.2      yamt 		vol <<= 1;
    656  1.15.4.2      yamt 		cB -= 120;
    657       1.3  augustss 	}
    658  1.15.4.2      yamt 	v = vol; /* ensure evaluation in signed 32 bit below */
    659  1.15.4.2      yamt 	/*
    660  1.15.4.2      yamt 	 * The GM vol-to-dB formula is dB = 40 log ( v / 127 ) for 7-bit v.
    661  1.15.4.2      yamt 	 * The vol and expression controllers are in 14-bit space so the
    662  1.15.4.2      yamt 	 * equivalent is 40 log ( v / 16256 ) - that is, MSB 127 LSB 0 because
    663  1.15.4.2      yamt 	 * the LSB is commonly unused. MSB 127 LSB 127 would then be a tiny
    664  1.15.4.2      yamt 	 * bit over.
    665  1.15.4.2      yamt 	 * 1 dB resolution is a little coarser than we'd like, so let's shoot
    666  1.15.4.2      yamt 	 * for centibels, i.e. 400 log ( v / 16256 ), and shift everything left
    667  1.15.4.2      yamt 	 * as far as will fit in 32 bits, which turns out to be a shift of 22.
    668  1.15.4.2      yamt 	 * This minimax polynomial approximation is good to about a centibel
    669  1.15.4.2      yamt 	 * on the range 8192..16256, a shade worse (1.4 or so) above that.
    670  1.15.4.2      yamt 	 * 26385/10166 is the 6th convergent of the coefficient for v^2.
    671  1.15.4.2      yamt 	 */
    672  1.15.4.2      yamt 	cB += ( v * ( 124828 - ( v * 26385 ) / 10166 ) - 1347349038 ) >> 22;
    673  1.15.4.2      yamt 	return cB;
    674       1.1  augustss }
    675       1.1  augustss 
    676  1.15.4.2      yamt /*
    677  1.15.4.2      yamt  * MIDI RP-012 constitutes a MIDI Tuning Specification. The units are
    678  1.15.4.2      yamt  * fractional-MIDIkeys, that is, the key number 00 - 7f left shifted
    679  1.15.4.2      yamt  * 14 bits to provide a 14-bit fraction that divides each semitone. The
    680  1.15.4.2      yamt  * whole thing is just a 21-bit number that is bent and tuned simply by
    681  1.15.4.2      yamt  * adding and subtracting--the same offset is the same pitch change anywhere
    682  1.15.4.2      yamt  * on the scale. One downside is that a cent is 163.84 of these units, so
    683  1.15.4.2      yamt  * you can't expect a lengthy integer sum of cents to come out in tune; if you
    684  1.15.4.2      yamt  * do anything in cents it is best to use them only for local adjustment of
    685  1.15.4.2      yamt  * a pitch.
    686  1.15.4.2      yamt  *
    687  1.15.4.2      yamt  * This function converts a pitch in MIDItune units to Hz left-shifted 18 bits.
    688  1.15.4.2      yamt  * That should leave you enough to shift down to whatever precision the hardware
    689  1.15.4.2      yamt  * supports.
    690  1.15.4.2      yamt  *
    691  1.15.4.2      yamt  * Its prototype is exposed in <sys/midiio.h>.
    692  1.15.4.2      yamt  */
    693  1.15.4.2      yamt midihz18_t
    694  1.15.4.2      yamt midisyn_mp2hz18(midipitch_t mp)
    695  1.15.4.2      yamt {
    696  1.15.4.2      yamt 	int64_t t64a, t64b;
    697  1.15.4.2      yamt 	uint_fast8_t shift;
    698  1.15.4.2      yamt 
    699  1.15.4.2      yamt 	/*
    700  1.15.4.2      yamt 	 * Scale from the logarithmic MIDI-Tuning units to Hz<<18. Uses the
    701  1.15.4.2      yamt 	 * continued-fraction form of a 2/2 rational function derived to
    702  1.15.4.2      yamt 	 * cover the highest octave (mt 1900544..2097151 or 74.00.00..7f.7f.7f
    703  1.15.4.2      yamt 	 * in RP-012-speak, the dotted bits are 7 wide) to produce Hz shifted
    704  1.15.4.2      yamt 	 * left just as far as the maximum Hz will fit in a uint32, which
    705  1.15.4.2      yamt 	 * turns out to be 18. Just shift off the result for lower octaves.
    706  1.15.4.2      yamt 	 * Fit is within 1/4 MIDI tuning unit throughout (disclaimer: the
    707  1.15.4.2      yamt 	 * comparison relied on the double-precision log in libm).
    708  1.15.4.2      yamt 	 */
    709  1.15.4.2      yamt 
    710  1.15.4.2      yamt 	if ( 0 == mp )
    711  1.15.4.2      yamt 		return 2143236;
    712  1.15.4.2      yamt 
    713  1.15.4.2      yamt 	for ( shift = 0; mp < 1900544; ++ shift )
    714  1.15.4.2      yamt 		mp += MIDIPITCH_OCTAVE;
    715  1.15.4.2      yamt 
    716  1.15.4.2      yamt 	if ( 1998848 == mp )
    717  1.15.4.2      yamt 		return UINT32_C(2463438621) >> shift;
    718  1.15.4.2      yamt 
    719  1.15.4.2      yamt 	t64a  = 0x5a1a0ee4; /* INT64_C(967879298788) gcc333: spurious warning */
    720  1.15.4.2      yamt 	t64a |= (int64_t)0xe1 << 32;
    721  1.15.4.2      yamt 	t64a /= mp - 1998848; /* here's why 1998848 is special-cased above ;) */
    722  1.15.4.2      yamt 	t64a += mp - 3704981;
    723  1.15.4.2      yamt 	t64b  = 0x6763759d; /* INT64_C(8405905567872413) goofy warning again */
    724  1.15.4.2      yamt 	t64b |= (int64_t)0x1ddd20 << 32;
    725  1.15.4.2      yamt 	t64b /= t64a;
    726  1.15.4.2      yamt 	t64b += UINT32_C(2463438619);
    727  1.15.4.2      yamt 	return (uint32_t)t64b >> shift;
    728  1.15.4.2      yamt }
    729