Home | History | Annotate | Line # | Download | only in dev
midisyn.c revision 1.17.2.17
      1  1.17.2.17      chap /*	$NetBSD: midisyn.c,v 1.17.2.17 2006/06/09 17:05:28 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.17      chap __KERNEL_RCSID(0, "$NetBSD: midisyn.c,v 1.17.2.17 2006/06/09 17:05:28 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.15      chap uint_fast16_t	midisyn_allocvoice(midisyn *, uint_fast8_t, uint_fast8_t);
     73  1.17.2.17      chap static void	midisyn_attackv_vel(midisyn *, uint_fast16_t, midipitch_t,
     74  1.17.2.15      chap                                     int16_t, uint_fast8_t);
     75  1.17.2.15      chap static int16_t midisyn_adj_level(midisyn *, uint_fast8_t);
     76  1.17.2.17      chap static midipitch_t midisyn_adj_pitch(midisyn *, uint_fast8_t);
     77        1.8  augustss 
     78  1.17.2.12      chap static midictl_notify midisyn_notify;
     79  1.17.2.12      chap 
     80  1.17.2.10      chap int	midisyn_open(void *, int,
     81        1.8  augustss 		     void (*iintr)(void *, int),
     82        1.8  augustss 		     void (*ointr)(void *), void *arg);
     83        1.8  augustss void	midisyn_close(void *);
     84   1.17.2.2      chap int	midisyn_sysrt(void *, int);
     85        1.8  augustss void	midisyn_getinfo(void *, struct midi_info *);
     86  1.17.2.10      chap int	midisyn_ioctl(void *, u_long, caddr_t, int, struct lwp *);
     87        1.1  augustss 
     88  1.17.2.10      chap const struct midi_hw_if midisyn_hw_if = {
     89        1.1  augustss 	midisyn_open,
     90        1.1  augustss 	midisyn_close,
     91   1.17.2.2      chap 	midisyn_sysrt,
     92        1.1  augustss 	midisyn_getinfo,
     93        1.1  augustss 	midisyn_ioctl,
     94        1.1  augustss };
     95        1.1  augustss 
     96   1.17.2.2      chap int	midisyn_channelmsg(void *, int, int, u_char *, int);
     97   1.17.2.2      chap int	midisyn_commonmsg(void *, int, u_char *, int);
     98   1.17.2.2      chap int	midisyn_sysex(void *, u_char *, int);
     99   1.17.2.2      chap 
    100   1.17.2.2      chap struct midi_hw_if_ext midisyn_hw_if_ext = {
    101   1.17.2.2      chap 	.channel = midisyn_channelmsg,
    102   1.17.2.2      chap 	.common  = midisyn_commonmsg,
    103   1.17.2.2      chap 	.sysex   = midisyn_sysex,
    104   1.17.2.2      chap };
    105        1.1  augustss 
    106        1.1  augustss int
    107        1.8  augustss midisyn_open(void *addr, int flags, void (*iintr)(void *, int),
    108        1.8  augustss 	     void (*ointr)(void *), void *arg)
    109        1.1  augustss {
    110        1.1  augustss 	midisyn *ms = addr;
    111  1.17.2.15      chap 	int rslt;
    112  1.17.2.15      chap 	uint_fast8_t chan;
    113        1.1  augustss 
    114        1.1  augustss 	DPRINTF(("midisyn_open: ms=%p ms->mets=%p\n", ms, ms->mets));
    115  1.17.2.12      chap 
    116  1.17.2.12      chap 	midictl_open(&ms->ctl);
    117  1.17.2.12      chap 
    118  1.17.2.15      chap 	rslt = 0;
    119        1.1  augustss 	if (ms->mets->open)
    120  1.17.2.15      chap 		rslt = (ms->mets->open(ms, flags));
    121  1.17.2.15      chap 
    122  1.17.2.15      chap 	/*
    123  1.17.2.15      chap 	 * Make the right initial things happen by faking receipt of RESET on
    124  1.17.2.15      chap 	 * all channels. The hw driver's ctlnotice() will be called in turn.
    125  1.17.2.15      chap 	 */
    126  1.17.2.15      chap 	for ( chan = 0 ; chan < MIDI_MAX_CHANS ; ++ chan )
    127  1.17.2.15      chap 		midisyn_notify(ms, MIDICTL_RESET, chan, 0);
    128  1.17.2.15      chap 
    129  1.17.2.15      chap 	return rslt;
    130        1.1  augustss }
    131        1.1  augustss 
    132        1.1  augustss void
    133        1.8  augustss midisyn_close(void *addr)
    134        1.1  augustss {
    135        1.1  augustss 	midisyn *ms = addr;
    136        1.4   mycroft 	struct midisyn_methods *fs;
    137        1.4   mycroft 	int v;
    138        1.1  augustss 
    139        1.1  augustss 	DPRINTF(("midisyn_close: ms=%p ms->mets=%p\n", ms, ms->mets));
    140        1.4   mycroft 	fs = ms->mets;
    141  1.17.2.15      chap 	/* TODO
    142  1.17.2.15      chap 	 * This loop is much like what midisyn_notify should do for NOTES_OFF
    143  1.17.2.15      chap 	 * or SOUND_OFF. It should be moved there, and here we should call
    144  1.17.2.15      chap 	 * notify faking one of those events.
    145  1.17.2.15      chap 	 */
    146        1.4   mycroft 	for (v = 0; v < ms->nvoice; v++)
    147        1.4   mycroft 		if (ms->voices[v].inuse) {
    148  1.17.2.15      chap 			fs->releasev(ms, v, 127);
    149        1.4   mycroft 			midisyn_freevoice(ms, v);
    150        1.4   mycroft 		}
    151        1.4   mycroft 	if (fs->close)
    152        1.4   mycroft 		fs->close(ms);
    153  1.17.2.12      chap 
    154  1.17.2.12      chap 	midictl_close(&ms->ctl);
    155        1.1  augustss }
    156        1.1  augustss 
    157        1.1  augustss void
    158        1.8  augustss midisyn_getinfo(void *addr, struct midi_info *mi)
    159        1.1  augustss {
    160        1.1  augustss 	midisyn *ms = addr;
    161        1.1  augustss 
    162        1.1  augustss 	mi->name = ms->name;
    163  1.17.2.11      chap 	/*
    164  1.17.2.11      chap 	 * I was going to add a property here to suppress midi(4)'s warning
    165  1.17.2.11      chap 	 * about an output device that uses no transmit interrupt, on the
    166  1.17.2.11      chap 	 * assumption that as an onboard synth we handle "output" internally
    167  1.17.2.11      chap 	 * with nothing like the 320 us per byte busy wait of a dumb UART.
    168  1.17.2.11      chap 	 * Then I noticed that opl (at least as currently implemented) seems
    169  1.17.2.11      chap 	 * to need 40 us busy wait to set each register on an OPL2, and sets
    170  1.17.2.11      chap 	 * about 21 registers for every note-on. (Half of that is patch loading
    171  1.17.2.11      chap 	 * and could probably be reduced by different management of voices and
    172  1.17.2.11      chap 	 * patches.) For now I won't bother suppressing that warning....
    173  1.17.2.11      chap 	 */
    174        1.1  augustss 	mi->props = 0;
    175   1.17.2.2      chap 
    176   1.17.2.2      chap 	midi_register_hw_if_ext(&midisyn_hw_if_ext);
    177        1.1  augustss }
    178        1.1  augustss 
    179        1.1  augustss int
    180  1.17.2.10      chap midisyn_ioctl(void *maddr, u_long cmd, caddr_t addr, int flag, struct lwp *l)
    181        1.1  augustss {
    182        1.1  augustss 	midisyn *ms = maddr;
    183        1.1  augustss 
    184        1.1  augustss 	if (ms->mets->ioctl)
    185  1.17.2.10      chap 		return (ms->mets->ioctl(ms, cmd, addr, flag, l));
    186        1.1  augustss 	else
    187        1.1  augustss 		return (EINVAL);
    188        1.1  augustss }
    189        1.1  augustss 
    190        1.1  augustss int
    191        1.8  augustss midisyn_findvoice(midisyn *ms, int chan, int note)
    192        1.1  augustss {
    193        1.1  augustss 	u_int cn;
    194        1.1  augustss 	int v;
    195        1.1  augustss 
    196        1.3  augustss 	cn = MS_CHANNOTE(chan, note);
    197        1.1  augustss 	for (v = 0; v < ms->nvoice; v++)
    198        1.3  augustss 		if (ms->voices[v].chan_note == cn && ms->voices[v].inuse)
    199        1.1  augustss 			return (v);
    200        1.1  augustss 	return (-1);
    201        1.1  augustss }
    202        1.1  augustss 
    203        1.1  augustss void
    204        1.8  augustss midisyn_attach(struct midi_softc *sc, midisyn *ms)
    205        1.1  augustss {
    206  1.17.2.15      chap 	if (ms->mets->allocv == 0) {
    207  1.17.2.10      chap 		ms->voices = malloc(ms->nvoice * sizeof (struct voice),
    208       1.10   tsutsui 				    M_DEVBUF, M_WAITOK|M_ZERO);
    209        1.1  augustss 		ms->seqno = 1;
    210  1.17.2.15      chap 		ms->mets->allocv = midisyn_allocvoice;
    211        1.1  augustss 	}
    212  1.17.2.12      chap 
    213  1.17.2.15      chap 	if (ms->mets->attackv_vel == 0 && ms->mets->attackv != 0)
    214  1.17.2.15      chap 		ms->mets->attackv_vel = midisyn_attackv_vel;
    215  1.17.2.15      chap 
    216  1.17.2.12      chap 	ms->ctl = (midictl) {
    217  1.17.2.12      chap 		.base_channel = 16,
    218  1.17.2.12      chap 		.cookie = ms,
    219  1.17.2.12      chap 		.notify = midisyn_notify
    220  1.17.2.12      chap 	};
    221  1.17.2.12      chap 
    222        1.1  augustss 	sc->hw_if = &midisyn_hw_if;
    223        1.1  augustss 	sc->hw_hdl = ms;
    224        1.1  augustss 	DPRINTF(("midisyn_attach: ms=%p\n", sc->hw_hdl));
    225        1.1  augustss }
    226        1.1  augustss 
    227        1.1  augustss void
    228        1.8  augustss midisyn_freevoice(midisyn *ms, int voice)
    229        1.1  augustss {
    230  1.17.2.15      chap 	if (ms->mets->allocv != midisyn_allocvoice)
    231        1.1  augustss 		return;
    232        1.3  augustss 	ms->voices[voice].inuse = 0;
    233        1.1  augustss }
    234        1.1  augustss 
    235  1.17.2.15      chap uint_fast16_t
    236  1.17.2.14      chap midisyn_allocvoice(midisyn *ms, uint_fast8_t chan, uint_fast8_t note)
    237        1.1  augustss {
    238        1.1  augustss 	int bestv, v;
    239        1.1  augustss 	u_int bestseq, s;
    240        1.1  augustss 
    241        1.1  augustss 	/* Find a free voice, or if no free voice is found the oldest. */
    242        1.1  augustss 	bestv = 0;
    243        1.3  augustss 	bestseq = ms->voices[0].seqno + (ms->voices[0].inuse ? 0x40000000 : 0);
    244        1.1  augustss 	for (v = 1; v < ms->nvoice; v++) {
    245        1.1  augustss 		s = ms->voices[v].seqno;
    246        1.3  augustss 		if (ms->voices[v].inuse)
    247        1.3  augustss 			s += 0x40000000;
    248        1.1  augustss 		if (s < bestseq) {
    249        1.1  augustss 			bestseq = s;
    250        1.1  augustss 			bestv = v;
    251        1.1  augustss 		}
    252        1.1  augustss 	}
    253        1.3  augustss 	DPRINTFN(10,("midisyn_allocvoice: v=%d seq=%d cn=%x inuse=%d\n",
    254  1.17.2.10      chap 		     bestv, ms->voices[bestv].seqno,
    255        1.3  augustss 		     ms->voices[bestv].chan_note,
    256        1.3  augustss 		     ms->voices[bestv].inuse));
    257        1.3  augustss #ifdef AUDIO_DEBUG
    258        1.3  augustss 	if (ms->voices[bestv].inuse)
    259  1.17.2.10      chap 		DPRINTFN(1,("midisyn_allocvoice: steal %x\n",
    260        1.3  augustss 			    ms->voices[bestv].chan_note));
    261        1.3  augustss #endif
    262        1.3  augustss 	ms->voices[bestv].chan_note = MS_CHANNOTE(chan, note);
    263        1.1  augustss 	ms->voices[bestv].seqno = ms->seqno++;
    264        1.3  augustss 	ms->voices[bestv].inuse = 1;
    265        1.1  augustss 	return (bestv);
    266        1.1  augustss }
    267        1.1  augustss 
    268  1.17.2.15      chap /* dummy attackv_vel that just adds vel into level for simple drivers */
    269  1.17.2.15      chap static void
    270  1.17.2.17      chap midisyn_attackv_vel(midisyn *ms, uint_fast16_t voice, midipitch_t mp,
    271  1.17.2.15      chap                     int16_t level_cB, uint_fast8_t vel)
    272  1.17.2.15      chap {
    273  1.17.2.17      chap 	ms->mets->attackv(ms, voice, mp,
    274  1.17.2.15      chap 	                  level_cB + midisyn_vol2cB((uint_fast16_t)vel << 7));
    275  1.17.2.15      chap }
    276  1.17.2.15      chap 
    277        1.1  augustss int
    278   1.17.2.2      chap midisyn_sysrt(void *addr, int b)
    279   1.17.2.2      chap {
    280   1.17.2.2      chap 	return 0;
    281   1.17.2.2      chap }
    282   1.17.2.2      chap 
    283   1.17.2.2      chap int midisyn_channelmsg(void *addr, int status, int chan, u_char *buf, int len)
    284        1.1  augustss {
    285        1.1  augustss 	midisyn *ms = addr;
    286        1.1  augustss 	int voice = 0;		/* initialize to keep gcc quiet */
    287        1.1  augustss 	struct midisyn_methods *fs;
    288        1.1  augustss 
    289   1.17.2.2      chap 	DPRINTF(("midisyn_channelmsg: ms=%p status=%#02x chan=%d\n",
    290   1.17.2.2      chap 	       ms, status, chan));
    291        1.1  augustss 	fs = ms->mets;
    292   1.17.2.2      chap 
    293   1.17.2.2      chap 	switch (status) {
    294        1.1  augustss 	case MIDI_NOTEOFF:
    295  1.17.2.13      chap 		/*
    296  1.17.2.13      chap 		 * for a device that leaves voice allocation to us--and that's
    297  1.17.2.13      chap 		 * all of 'em at the moment--the voice and release velocity
    298  1.17.2.13      chap 		 * should be the only necessary arguments to noteoff. what use
    299  1.17.2.13      chap 		 * are they making of note? checking... None. Cool.
    300  1.17.2.16      chap 		 * IF there is ever a device added that does its own allocation,
    301  1.17.2.16      chap 		 * extend the interface; this findvoice won't be what to do...
    302  1.17.2.13      chap 		 */
    303   1.17.2.2      chap 		voice = midisyn_findvoice(ms, chan, buf[1]);
    304        1.1  augustss 		if (voice >= 0) {
    305  1.17.2.15      chap 			fs->releasev(ms, voice, buf[2]);
    306        1.1  augustss 			midisyn_freevoice(ms, voice);
    307        1.1  augustss 		}
    308        1.1  augustss 		break;
    309        1.1  augustss 	case MIDI_NOTEON:
    310  1.17.2.13      chap 		/*
    311  1.17.2.13      chap 		 * opl combines vel with a 'mainvol' that has no setter
    312  1.17.2.13      chap 		 * anywhere. pcppi punts volume entirely. cms uses vel alone.
    313  1.17.2.13      chap 		 * what's called for here, given current drivers, is an i/f
    314  1.17.2.13      chap 		 * where midisyn computes a volume from vel*volume*expression*
    315  1.17.2.13      chap 		 * mastervolume and passes that result as a single arg. It can
    316  1.17.2.13      chap 		 * evolve later to support drivers that expose some of those
    317  1.17.2.13      chap 		 * bits separately (e.g. a driver could expose a mixer register
    318  1.17.2.13      chap 		 * on its sound card and use that for mastervolume).
    319  1.17.2.13      chap 		 */
    320   1.17.2.2      chap 		voice = fs->allocv(ms, chan, buf[1]);
    321  1.17.2.15      chap 		fs->attackv_vel(ms, voice,
    322  1.17.2.17      chap 		                MIDIPITCH_FROM_KEY(buf[1])
    323  1.17.2.15      chap 				+ midisyn_adj_pitch(ms, chan),
    324  1.17.2.15      chap 				midisyn_adj_level(ms,chan), buf[2]);
    325        1.1  augustss 		break;
    326        1.1  augustss 	case MIDI_KEY_PRESSURE:
    327  1.17.2.13      chap 		/*
    328  1.17.2.13      chap 		 * unimplemented by the existing drivers. if we are doing
    329  1.17.2.13      chap 		 * voice allocation, find the voice that corresponds to this
    330  1.17.2.13      chap 		 * chan/note and define a method that passes the voice and
    331  1.17.2.13      chap 		 * pressure to the driver ... not the note, /it/ doesn't matter.
    332  1.17.2.13      chap 		 * For a driver that does its own allocation, a different
    333  1.17.2.13      chap 		 * method may be needed passing pressure, chan, note so it can
    334  1.17.2.13      chap 		 * find the right voice on its own. Be sure that whatever is
    335  1.17.2.13      chap 		 * done here is undone when midisyn_notify sees MIDICTL_RESET.
    336  1.17.2.13      chap 		 */
    337        1.1  augustss 		break;
    338        1.1  augustss 	case MIDI_CTL_CHANGE:
    339  1.17.2.12      chap 		midictl_change(&ms->ctl, chan, buf+1);
    340        1.1  augustss 		break;
    341        1.1  augustss 	case MIDI_PGM_CHANGE:
    342        1.1  augustss 		if (fs->pgmchg)
    343   1.17.2.2      chap 			fs->pgmchg(ms, chan, buf[1]);
    344        1.1  augustss 		break;
    345        1.1  augustss 	case MIDI_CHN_PRESSURE:
    346  1.17.2.13      chap 		/*
    347  1.17.2.13      chap 		 * unimplemented by the existing drivers. if driver exposes no
    348  1.17.2.13      chap 		 * distinct method, can use KEY_PRESSURE method for each voice
    349  1.17.2.13      chap 		 * on channel. Be sure that whatever is
    350  1.17.2.13      chap 		 * done here is undone when midisyn_notify sees MIDICTL_RESET.
    351  1.17.2.13      chap 		 */
    352        1.1  augustss 		break;
    353        1.1  augustss 	case MIDI_PITCH_BEND:
    354  1.17.2.13      chap 		/*
    355  1.17.2.13      chap 		 * unimplemented by existing drivers. it is good that most
    356  1.17.2.13      chap 		 * existing drivers take a /frequency/ rather than a /note no/
    357  1.17.2.13      chap 		 * for control; midisyn should use this event to update some
    358  1.17.2.13      chap 		 * internal state and use it (along with tuning) to derive the
    359  1.17.2.13      chap 		 * frequency passed to the driver for any note on. A driver that
    360  1.17.2.13      chap 		 * can change freq of a sounding voice should expose a method
    361  1.17.2.13      chap 		 * for that, i.e. m(voice,newfreq) and we should call that too,
    362  1.17.2.13      chap 		 * for each voice on the affected channel, when a pitch bend
    363  1.17.2.13      chap 		 * change is received. Note RPN 0 must be used in scaling the
    364  1.17.2.13      chap 		 * pitch bend. Be sure that whatever is
    365  1.17.2.13      chap 		 * done here is undone when midisyn_notify sees MIDICTL_RESET.
    366  1.17.2.13      chap 		 */
    367        1.1  augustss 		break;
    368        1.1  augustss 	}
    369   1.17.2.2      chap 	return 0;
    370   1.17.2.2      chap }
    371   1.17.2.2      chap 
    372   1.17.2.2      chap int midisyn_commonmsg(void *addr, int status, u_char *buf, int len)
    373   1.17.2.2      chap {
    374   1.17.2.2      chap 	return 0;
    375   1.17.2.2      chap }
    376   1.17.2.2      chap 
    377   1.17.2.2      chap int midisyn_sysex(void *addr, u_char *buf, int len)
    378   1.17.2.2      chap {
    379  1.17.2.13      chap 	/*
    380  1.17.2.13      chap 	 * unimplemented by existing drivers. it is surely more sensible
    381  1.17.2.13      chap 	 * to do some parsing of well-defined sysex messages here, either
    382  1.17.2.13      chap 	 * handling them internally or calling specific methods on the
    383  1.17.2.13      chap 	 * driver after parsing out the details, than to ask every driver
    384  1.17.2.13      chap 	 * to deal with sysex messages poked at it a byte at a time.
    385  1.17.2.13      chap 	 */
    386   1.17.2.2      chap 	return 0;
    387        1.1  augustss }
    388        1.1  augustss 
    389  1.17.2.15      chap static void
    390  1.17.2.15      chap midisyn_notify(void *cookie, midictl_evt evt,
    391  1.17.2.15      chap                uint_fast8_t chan, uint_fast16_t key)
    392        1.1  augustss {
    393  1.17.2.15      chap 	struct midisyn *ms;
    394  1.17.2.15      chap 	int drvhandled;
    395  1.17.2.15      chap 
    396  1.17.2.15      chap 	ms = (struct midisyn *)cookie;
    397  1.17.2.15      chap 	drvhandled = 0;
    398  1.17.2.15      chap 	if ( ms->mets->ctlnotice )
    399  1.17.2.15      chap 		drvhandled = ms->mets->ctlnotice(ms, evt, chan, key);
    400  1.17.2.15      chap 
    401  1.17.2.15      chap 	switch ( evt ) {
    402  1.17.2.15      chap 	case MIDICTL_RESET:
    403  1.17.2.15      chap 		/* re-read all ctls we use, revert pitchbend state */
    404  1.17.2.15      chap 		break;
    405  1.17.2.15      chap 	case MIDICTL_NOTES_OFF:
    406  1.17.2.15      chap 		if ( drvhandled )
    407  1.17.2.15      chap 			break;
    408  1.17.2.15      chap 		/* releasev all voices sounding on chan; use normal vel 64 */
    409  1.17.2.15      chap 		break;
    410  1.17.2.15      chap 	case MIDICTL_SOUND_OFF:
    411  1.17.2.15      chap 		if ( drvhandled )
    412  1.17.2.15      chap 			break;
    413  1.17.2.15      chap 		/* releasev all voices sounding on chan; use max vel 127 */
    414  1.17.2.15      chap 		/* it is really better for driver to handle this, instantly */
    415  1.17.2.15      chap 		break;
    416  1.17.2.15      chap 	default:
    417  1.17.2.15      chap 		break;
    418        1.3  augustss 	}
    419  1.17.2.15      chap }
    420        1.3  augustss 
    421  1.17.2.15      chap static int16_t
    422  1.17.2.15      chap midisyn_adj_level(midisyn *ms, uint_fast8_t chan)
    423  1.17.2.15      chap {
    424  1.17.2.15      chap 	return 0; /* XXX for now. Use Volume and Expression ctlrs. */
    425        1.1  augustss }
    426        1.1  augustss 
    427  1.17.2.17      chap static midipitch_t
    428  1.17.2.15      chap midisyn_adj_pitch(midisyn *ms, uint_fast8_t chan)
    429  1.17.2.12      chap {
    430  1.17.2.15      chap 	return 0; /* XXX for now. Use Pitchbend, PBrange, fine/coarse tuning. */
    431  1.17.2.12      chap }
    432  1.17.2.14      chap 
    433  1.17.2.14      chap int16_t
    434  1.17.2.14      chap midisyn_vol2cB(uint_fast16_t vol)
    435  1.17.2.14      chap {
    436  1.17.2.14      chap 	int16_t cB = 0;
    437  1.17.2.14      chap 	int32_t v;
    438  1.17.2.14      chap 
    439  1.17.2.14      chap 	if ( 0 == vol )
    440  1.17.2.14      chap 		return INT16_MIN;
    441  1.17.2.14      chap 	/*
    442  1.17.2.14      chap 	 * Adjust vol to fall in the range 8192..16383. Each doubling is
    443  1.17.2.14      chap 	 * worth 12 dB.
    444  1.17.2.14      chap 	 */
    445  1.17.2.14      chap 	while ( vol < 8192 ) {
    446  1.17.2.14      chap 		vol <<= 1;
    447  1.17.2.14      chap 		cB -= 120;
    448  1.17.2.14      chap 	}
    449  1.17.2.14      chap 	v = vol; /* ensure evaluation in signed 32 bit below */
    450  1.17.2.14      chap 	/*
    451  1.17.2.14      chap 	 * The GM vol-to-dB formula is dB = 40 log ( v / 127 ) for 7-bit v.
    452  1.17.2.14      chap 	 * The vol and expression controllers are in 14-bit space so the
    453  1.17.2.14      chap 	 * equivalent is 40 log ( v / 16256 ) - that is, MSB 127 LSB 0 because
    454  1.17.2.14      chap 	 * the LSB is commonly unused. MSB 127 LSB 127 would then be a tiny
    455  1.17.2.14      chap 	 * bit over.
    456  1.17.2.14      chap 	 * 1 dB resolution is a little coarser than we'd like, so let's shoot
    457  1.17.2.14      chap 	 * for centibels, i.e. 400 log ( v / 16256 ), and shift everything left
    458  1.17.2.14      chap 	 * as far as will fit in 32 bits, which turns out to be a shift of 22.
    459  1.17.2.14      chap 	 * This minimax polynomial approximation is good to about a centibel
    460  1.17.2.14      chap 	 * on the range 8192..16256, a shade worse (1.4 or so) above that.
    461  1.17.2.14      chap 	 * 26385/10166 is the 6th convergent of the coefficient for v^2.
    462  1.17.2.14      chap 	 */
    463  1.17.2.14      chap 	cB += ( v * ( 124828 - ( v * 26385 ) / 10166 ) - 1347349038 ) >> 22;
    464  1.17.2.14      chap 	return cB;
    465  1.17.2.14      chap }
    466  1.17.2.14      chap 
    467  1.17.2.17      chap /*
    468  1.17.2.17      chap  * MIDI RP-012 constitutes a MIDI Tuning Specification. The units are
    469  1.17.2.17      chap  * fractional-MIDIkeys, that is, the key number 00 - 7f left shifted
    470  1.17.2.17      chap  * 14 bits to provide a 14-bit fraction that divides each semitone. The
    471  1.17.2.17      chap  * whole thing is just a 21-bit number that is bent and tuned simply by
    472  1.17.2.17      chap  * adding and subtracting--the same offset is the same pitch change anywhere
    473  1.17.2.17      chap  * on the scale. One downside is that a cent is 163.84 of these units, so
    474  1.17.2.17      chap  * you can't expect a lengthy integer sum of cents to come out in tune; if you
    475  1.17.2.17      chap  * do anything in cents it is best to use them only for local adjustment of
    476  1.17.2.17      chap  * a pitch.
    477  1.17.2.17      chap  *
    478  1.17.2.17      chap  * This function converts a pitch in MIDItune units to Hz left-shifted 18 bits.
    479  1.17.2.17      chap  * That should leave you enough to shift down to whatever precision the hardware
    480  1.17.2.17      chap  * supports.
    481  1.17.2.17      chap  *
    482  1.17.2.17      chap  * Its prototype is exposed in <sys/midiio.h>.
    483  1.17.2.17      chap  */
    484  1.17.2.17      chap midihz18_t
    485  1.17.2.17      chap midisyn_mp2hz18(midipitch_t mp)
    486  1.17.2.14      chap {
    487  1.17.2.14      chap 	int64_t t64a, t64b;
    488  1.17.2.14      chap 	uint_fast8_t shift;
    489  1.17.2.14      chap 
    490  1.17.2.14      chap 	/*
    491  1.17.2.14      chap 	 * Scale from the logarithmic MIDI-Tuning units to Hz<<18. Uses the
    492  1.17.2.14      chap 	 * continued-fraction form of a 2/2 rational function derived to
    493  1.17.2.14      chap 	 * cover the highest octave (mt 1900544..2097151 or 74.00.00..7f.7f.7f
    494  1.17.2.14      chap 	 * in RP-012-speak, the dotted bits are 7 wide) to produce Hz shifted
    495  1.17.2.14      chap 	 * left just as far as the maximum Hz will fit in a uint32, which
    496  1.17.2.14      chap 	 * turns out to be 18. Just shift off the result for lower octaves.
    497  1.17.2.14      chap 	 * Fit is within 1/4 MIDI tuning unit throughout (disclaimer: the
    498  1.17.2.14      chap 	 * comparison relied on the double-precision log in libm).
    499  1.17.2.14      chap 	 */
    500  1.17.2.14      chap 
    501  1.17.2.17      chap 	if ( 0 == mp )
    502  1.17.2.14      chap 		return 2143236;
    503  1.17.2.14      chap 
    504  1.17.2.17      chap 	for ( shift = 0; mp < 1900544; ++ shift )
    505  1.17.2.17      chap 		mp += MIDIPITCH_OCTAVE;
    506  1.17.2.14      chap 
    507  1.17.2.17      chap 	if ( 1998848 == mp )
    508  1.17.2.14      chap 		return UINT32_C(2463438621) >> shift;
    509  1.17.2.14      chap 
    510  1.17.2.14      chap 	t64a  = 0x5a1a0ee4; /* INT64_C(967879298788) gcc333: spurious warning */
    511  1.17.2.14      chap 	t64a |= (int64_t)0xe1 << 32;
    512  1.17.2.17      chap 	t64a /= mp - 1998848; /* here's why 1998848 is special-cased above ;) */
    513  1.17.2.17      chap 	t64a += mp - 3704981;
    514  1.17.2.14      chap 	t64b  = 0x6763759d; /* INT64_C(8405905567872413) and here too */
    515  1.17.2.14      chap 	t64b |= (int64_t)0x1ddd20 << 32;
    516  1.17.2.14      chap 	t64b /= t64a;
    517  1.17.2.14      chap 	t64b += UINT32_C(2463438619);
    518  1.17.2.14      chap 	return (uint32_t)t64b >> shift;
    519  1.17.2.14      chap }
    520