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