Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/*	$NetBSD: midisyn.c,v 1.25 2019/05/08 13:40:17 isaki Exp $	*/

/*
 * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Lennart Augustsson (augustss@NetBSD.org), and by Andrew Doran.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: midisyn.c,v 1.25 2019/05/08 13:40:17 isaki Exp $");

#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/vnode.h>
#include <sys/select.h>
#include <sys/proc.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/syslog.h>
#include <sys/kernel.h>
#include <sys/audioio.h>
#include <sys/midiio.h>
#include <sys/device.h>

#include <dev/audio/audio_if.h>
#include <dev/midi_if.h>
#include <dev/midivar.h>
#include <dev/midisynvar.h>

#ifdef AUDIO_DEBUG
#define DPRINTF(x)	if (midisyndebug) printf x
#define DPRINTFN(n,x)	if (midisyndebug >= (n)) printf x
int	midisyndebug = 0;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif

static int	midisyn_findvoice(midisyn *, int, int);
static void	midisyn_freevoice(midisyn *, int);
static uint_fast16_t	midisyn_allocvoice(midisyn *, uint_fast8_t, uint_fast8_t);
static void	midisyn_attackv_vel(midisyn *, uint_fast16_t, midipitch_t,
                                    int16_t, uint_fast8_t);

static midictl_notify midisyn_notify;

static midipitch_t midisyn_clamp_pitch(midipitch_t);
static int16_t midisyn_adj_level(midisyn *, uint_fast8_t);
static midipitch_t midisyn_adj_pitch(midisyn *, uint_fast8_t);
static void midisyn_chan_releasev(midisyn *, uint_fast8_t, uint_fast8_t);
static void midisyn_upd_level(midisyn *, uint_fast8_t);
static void midisyn_upd_pitch(midisyn *, uint_fast8_t);

static int	midisyn_open(void *, int,
			     void (*iintr)(void *, int),
			     void (*ointr)(void *), void *arg);
static void	midisyn_close(void *);
static int	midisyn_sysrt(void *, int);
static void	midisyn_getinfo(void *, struct midi_info *);
static int	midisyn_ioctl(void *, u_long, void *, int, struct lwp *);
static void	midisyn_get_locks(void *, kmutex_t **, kmutex_t **);

const struct midi_hw_if midisyn_hw_if = {
	midisyn_open,
	midisyn_close,
	midisyn_sysrt,
	midisyn_getinfo,
	midisyn_ioctl,
	midisyn_get_locks,
};

static int	midisyn_channelmsg(void *, int, int, u_char *, int);
static int	midisyn_commonmsg(void *, int, u_char *, int);
static int	midisyn_sysex(void *, u_char *, int);

struct midi_hw_if_ext midisyn_hw_if_ext = {
	.channel = midisyn_channelmsg,
	.common  = midisyn_commonmsg,
	.sysex   = midisyn_sysex,
};

struct channelstate { /* dyamically allocated in open() on account of size */
	/* volume state components in centibels; just sum for overall level */
	int16_t volume;
	int16_t expression;
	/* pitch state components in midipitch units; sum for overall effect */
	midipitch_t bend;
	midipitch_t tuning_fine;
	midipitch_t tuning_coarse;
	/* used by bend handlers */
	int16_t bendraw;
	int16_t pendingreset;
/* rearrange as more controls supported - 16 bits should last for a while */
#define PEND_VOL 1
#define PEND_EXP 2
#define PEND_LEVEL (PEND_VOL|PEND_EXP)
#define PEND_PBS 4
#define PEND_TNF 8
#define PEND_TNC 16
#define PEND_PITCH (PEND_PBS|PEND_TNF|PEND_TNC)
#define PEND_ALL   (PEND_LEVEL|PEND_PITCH)
};

static int
midisyn_open(void *addr, int flags, void (*iintr)(void *, int),
    void (*ointr)(void *), void *arg)
{
	midisyn *ms = addr;
	int rslt, error;
	uint_fast8_t chan;

	KASSERT(ms->lock != NULL);
	KASSERT(mutex_owned(ms->lock));
	DPRINTF(("midisyn_open: ms=%p ms->mets=%p\n", ms, ms->mets));

	mutex_exit(ms->lock);
	ms->ctl.lock = ms->lock;
	error = midictl_open(&ms->ctl);
	if (error != 0) {
		mutex_enter(ms->lock);	
		return error;
	}
	ms->chnstate = kmem_alloc(MIDI_MAX_CHANS * sizeof(*ms->chnstate),
	    KM_SLEEP); /* init'd by RESET below */
	mutex_enter(ms->lock);	
	
	rslt = 0;
	if (ms->mets->open)
		rslt = (ms->mets->open(ms, flags));
	
	/*
	 * Make the right initial things happen by faking receipt of RESET on
	 * all channels. The hw driver's ctlnotice() will be called in turn.
	 */
	for ( chan = 0 ; chan < MIDI_MAX_CHANS ; ++ chan )
		midisyn_notify(ms, MIDICTL_RESET, chan, 0);
	
	return rslt;
}

static void
midisyn_close(void *addr)
{
	midisyn *ms = addr;
	struct midisyn_methods *fs;
	int chan;

	KASSERT(mutex_owned(ms->lock));
	DPRINTF(("midisyn_close: ms=%p ms->mets=%p\n", ms, ms->mets));
	fs = ms->mets;

	for (chan = 0; chan < MIDI_MAX_CHANS; chan++)
		midisyn_notify(ms, MIDICTL_SOUND_OFF, chan, 0);

	if (fs->close)
		fs->close(ms);

	mutex_exit(ms->lock);
	midictl_close(&ms->ctl);
	kmem_free(ms->chnstate, MIDI_MAX_CHANS * sizeof(*ms->chnstate));
	mutex_enter(ms->lock);
}

static void
midisyn_getinfo(void *addr, struct midi_info *mi)
{
	midisyn *ms = addr;

	KASSERT(mutex_owned(ms->lock));

	mi->name = ms->name;
	/*
	 * I was going to add a property here to suppress midi(4)'s warning
	 * about an output device that uses no transmit interrupt, on the
	 * assumption that as an onboard synth we handle "output" internally
	 * with nothing like the 320 us per byte busy wait of a dumb UART.
	 * Then I noticed that opl (at least as currently implemented) seems
	 * to need 40 us busy wait to set each register on an OPL2, and sets
	 * about 21 registers for every note-on. (Half of that is patch loading
	 * and could probably be reduced by different management of voices and
	 * patches.) For now I won't bother suppressing that warning....
	 */
	mi->props = 0;
	
	midi_register_hw_if_ext(&midisyn_hw_if_ext);
}

static void
midisyn_get_locks(void *addr, kmutex_t **intr, kmutex_t **proc)
{
	midisyn *ms = addr;

	*intr = ms->lock;
	*proc = NULL;
}

static int
midisyn_ioctl(void *maddr, u_long cmd, void *addr, int flag, struct lwp *l)
{
	midisyn *ms = maddr;

	KASSERT(mutex_owned(ms->lock));

	if (ms->mets->ioctl)
		return (ms->mets->ioctl(ms, cmd, addr, flag, l));
	else
		return (EINVAL);
}

static int
midisyn_findvoice(midisyn *ms, int chan, int note)
{
	u_int cn;
	int v;

	KASSERT(mutex_owned(ms->lock));

	cn = MS_CHANNOTE(chan, note);
	for (v = 0; v < ms->nvoice; v++)
		if (ms->voices[v].chan_note == cn && ms->voices[v].inuse)
			return (v);
	return (-1);
}

void
midisyn_init(midisyn *ms)
{

	KASSERT(ms->lock != NULL);

	/*
	 * XXX there should be a way for this function to indicate failure
	 * (other than panic) if some preconditions aren't met, for example
	 * if some nonoptional methods are missing.
	 */
	if (ms->mets->allocv == 0) {
		ms->voices = kmem_zalloc(ms->nvoice * sizeof(struct voice),
		    KM_SLEEP);
		ms->seqno = 1;
		ms->mets->allocv = midisyn_allocvoice;
	}
	
	if (ms->mets->attackv_vel == 0 && ms->mets->attackv != 0)
		ms->mets->attackv_vel = midisyn_attackv_vel;
	
	ms->ctl = (midictl) {
		.base_channel = 16,
		.cookie = ms,
		.notify = midisyn_notify
	};
	
	DPRINTF(("midisyn_init: ms=%p\n", ms));
}

static void
midisyn_freevoice(midisyn *ms, int voice)
{

	KASSERT(mutex_owned(ms->lock));

	if (ms->mets->allocv != midisyn_allocvoice)
		return;
	ms->voices[voice].inuse = 0;
}

static uint_fast16_t
midisyn_allocvoice(midisyn *ms, uint_fast8_t chan, uint_fast8_t note)
{
	int bestv, v;
	u_int bestseq, s;

	KASSERT(mutex_owned(ms->lock));

	/* Find a free voice, or if no free voice is found the oldest. */
	bestv = 0;
	bestseq = ms->voices[0].seqno + (ms->voices[0].inuse ? 0x40000000 : 0);
	for (v = 1; v < ms->nvoice; v++) {
		s = ms->voices[v].seqno;
		if (ms->voices[v].inuse)
			s += 0x40000000;
		if (s < bestseq) {
			bestseq = s;
			bestv = v;
		}
	}
	DPRINTFN(10,("midisyn_allocvoice: v=%d seq=%d cn=%x inuse=%d\n",
		     bestv, ms->voices[bestv].seqno,
		     ms->voices[bestv].chan_note,
		     ms->voices[bestv].inuse));
#ifdef AUDIO_DEBUG
	if (ms->voices[bestv].inuse)
		DPRINTFN(1,("midisyn_allocvoice: steal %x\n",
			    ms->voices[bestv].chan_note));
#endif
	ms->voices[bestv].chan_note = MS_CHANNOTE(chan, note);
	ms->voices[bestv].seqno = ms->seqno++;
	ms->voices[bestv].inuse = 1;
	return (bestv);
}

/* dummy attackv_vel that just adds vel into level for simple drivers */
static void
midisyn_attackv_vel(midisyn *ms, uint_fast16_t voice, midipitch_t mp,
                    int16_t level_cB, uint_fast8_t vel)
{

	KASSERT(mutex_owned(ms->lock));

	ms->voices[voice].velcB = midisyn_vol2cB((uint_fast16_t)vel << 7);
	ms->mets->attackv(ms, voice, mp, level_cB + ms->voices[voice].velcB);
}

static int
midisyn_sysrt(void *addr, int b)
{

	return 0;
}

static int
midisyn_channelmsg(void *addr, int status, int chan, u_char *buf, int len)
{
	midisyn *ms = addr;
	int voice = 0;		/* initialize to keep gcc quiet */
	struct midisyn_methods *fs;

	KASSERT(mutex_owned(ms->lock));

	DPRINTF(("midisyn_channelmsg: ms=%p status=%#02x chan=%d\n",
	       ms, status, chan));
	fs = ms->mets;

	switch (status) {
	case MIDI_NOTEOFF:
		/*
		 * for a device that leaves voice allocation to us--and that's
		 * all of 'em at the moment--the voice and release velocity
		 * should be the only necessary arguments to noteoff. what use
		 * are they making of note? checking... None. Cool.
		 * IF there is ever a device added that does its own allocation,
		 * extend the interface; this findvoice won't be what to do...
		 */
		voice = midisyn_findvoice(ms, chan, buf[1]);
		if (voice >= 0) {
			fs->releasev(ms, voice, buf[2]);
			midisyn_freevoice(ms, voice);
		}
		break;
	case MIDI_NOTEON:
		/*
		 * what's called for here, given current drivers, is an i/f
		 * where midisyn computes a volume from vel*volume*expression*
		 * mastervolume and passes that result as a single arg. It can
		 * evolve later to support drivers that expose some of those
		 * bits separately (e.g. a driver could expose a mixer register
		 * on its sound card and use that for mastervolume).
		 */
		voice = fs->allocv(ms, chan, buf[1]);
		ms->voices[voice].velcB = 0; /* assume driver handles vel */
		fs->attackv_vel(ms, voice,
		    midisyn_clamp_pitch(MIDIPITCH_FROM_KEY(buf[1]) +
		                        midisyn_adj_pitch(ms, chan)),
		    midisyn_adj_level(ms,chan), buf[2]);
		break;
	case MIDI_KEY_PRESSURE:
		/*
		 * unimplemented by the existing drivers. if we are doing
		 * voice allocation, find the voice that corresponds to this
		 * chan/note and define a method that passes the voice and
		 * pressure to the driver ... not the note, /it/ doesn't matter.
		 * For a driver that does its own allocation, a different
		 * method may be needed passing pressure, chan, note so it can
		 * find the right voice on its own. Be sure that whatever is
		 * done here is undone when midisyn_notify sees MIDICTL_RESET.
		 */
		break;
	case MIDI_CTL_CHANGE:
		midictl_change(&ms->ctl, chan, buf+1);
		break;
	case MIDI_PGM_CHANGE:
		if (fs->pgmchg)
			fs->pgmchg(ms, chan, buf[1]);
		break;
	case MIDI_CHN_PRESSURE:
		/*
		 * unimplemented by the existing drivers. if driver exposes no
		 * distinct method, can use KEY_PRESSURE method for each voice
		 * on channel. Be sure that whatever is
		 * done here is undone when midisyn_notify sees MIDICTL_RESET.
		 */
		break;
	case MIDI_PITCH_BEND:
		/*
		 * Will work for most drivers that simply render the midipitch
		 * as we pass it (but not cms, which chops all the bits after
		 * the note number and then computes its own pitch :( ). If the
		 * driver has a repitchv method for voices already sounding, so
		 * much the better.
		 * The bending logic lives in the handler for bend sensitivity,
		 * so fake a change to that to kick it off.
		 */
		ms->chnstate[chan].bendraw = buf[2]<<7 | buf[1];
		ms->chnstate[chan].bendraw -= MIDI_BEND_NEUTRAL;
		midisyn_notify(ms, MIDICTL_RPN, chan,
		               MIDI_RPN_PITCH_BEND_SENSITIVITY);
		break;
	}
	return 0;
}

static int
midisyn_commonmsg(void *addr, int status, u_char *buf, int len)
{

	return 0;
}

static int
midisyn_sysex(void *addr, u_char *buf, int len)
{

	/*
	 * unimplemented by existing drivers. it is surely more sensible
	 * to do some parsing of well-defined sysex messages here, either
	 * handling them internally or calling specific methods on the
	 * driver after parsing out the details, than to ask every driver
	 * to deal with sysex messages poked at it a byte at a time.
	 */
	return 0;
}

static void
midisyn_notify(void *cookie, midictl_evt evt,
               uint_fast8_t chan, uint_fast16_t key)
{
	struct midisyn *ms;
	int drvhandled;
	
	ms = (struct midisyn *)cookie;

	KASSERT(mutex_owned(ms->lock));

	drvhandled = 0;
	if ( ms->mets->ctlnotice )
		drvhandled = ms->mets->ctlnotice(ms, evt, chan, key);

	switch ( evt | key ) {
	case MIDICTL_RESET:
		/*
		 * Re-read all ctls we use, revert pitchbend state.
		 * Can do it by faking change notifications.
		 */
		ms->chnstate[chan].pendingreset |= PEND_ALL;
		midisyn_notify(ms, MIDICTL_CTLR, chan,
		               MIDI_CTRL_CHANNEL_VOLUME_MSB);
		midisyn_notify(ms, MIDICTL_CTLR, chan,
		               MIDI_CTRL_EXPRESSION_MSB);
		ms->chnstate[chan].bendraw = 0; /* MIDI_BEND_NEUTRAL - itself */
		midisyn_notify(ms, MIDICTL_RPN, chan,
		               MIDI_RPN_PITCH_BEND_SENSITIVITY);
		midisyn_notify(ms, MIDICTL_RPN, chan,
		               MIDI_RPN_CHANNEL_FINE_TUNING);
		midisyn_notify(ms, MIDICTL_RPN, chan,
		               MIDI_RPN_CHANNEL_COARSE_TUNING);
		break;
	case MIDICTL_NOTES_OFF:
		if ( drvhandled )
			break;
		/* releasev all voices sounding on chan; use normal vel 64 */
		midisyn_chan_releasev(ms, chan, 64);
		break;
	case MIDICTL_SOUND_OFF:
		if ( drvhandled )
			break;
		/* releasev all voices sounding on chan; use max vel 127 */
		/* it is really better for driver to handle this, instantly */
		midisyn_chan_releasev(ms, chan, 127);
		break;
	case MIDICTL_CTLR | MIDI_CTRL_CHANNEL_VOLUME_MSB:
		ms->chnstate[chan].pendingreset &= ~PEND_VOL;
		if ( drvhandled ) {
			ms->chnstate[chan].volume = 0;
			break;
		}
		ms->chnstate[chan].volume = midisyn_vol2cB(
	    	    midictl_read(&ms->ctl, chan, key, 100<<7));
		midisyn_upd_level(ms, chan);
		break;
	case MIDICTL_CTLR | MIDI_CTRL_EXPRESSION_MSB:
		ms->chnstate[chan].pendingreset &= ~PEND_EXP;
		if ( drvhandled ) {
			ms->chnstate[chan].expression = 0;
			break;
		}
		ms->chnstate[chan].expression = midisyn_vol2cB(
	    	    midictl_read(&ms->ctl, chan, key, 16383));
		midisyn_upd_level(ms, chan);
		break;
	/*
	 * SOFT_PEDAL: supporting this will be trickier; must apply only
	 * to notes subsequently struck, and must remember which voices
	 * they are for follow-on adjustments. For another day....
	 */
	case MIDICTL_RPN | MIDI_RPN_PITCH_BEND_SENSITIVITY:
		ms->chnstate[chan].pendingreset &= ~PEND_PBS;
		if ( drvhandled )
			ms->chnstate[chan].bend = 0;
		else {
			uint16_t w;
			int8_t semis, cents;
			w = midictl_rpn_read(&ms->ctl, chan, key, 2<<7);
			semis = w>>7;
			cents = w&0x7f;
			/*
			 * Mathematically, multiply semis by
			 * MIDIPITCH_SEMITONE*bendraw/8192. Practically, avoid
			 * shifting significant bits off by observing that
			 * MIDIPITCH_SEMITONE == 1<<14 and 8192 == 1<<13, so
			 * just take semis*bendraw<<1. Do the same with cents
			 * except <<1 becomes /50 (but rounded).
			 */
			ms->chnstate[chan].bend =
			    ( ms->chnstate[chan].bendraw * semis ) << 1;
			ms->chnstate[chan].bend +=
			    ((ms->chnstate[chan].bendraw * cents)/25 + 1) >> 1;
			midisyn_upd_pitch(ms, chan);
		}
		break;
	case MIDICTL_RPN | MIDI_RPN_CHANNEL_FINE_TUNING:
		if ( drvhandled )
			ms->chnstate[chan].tuning_fine = 0;
		else {
			midipitch_t mp;
			mp = midictl_rpn_read(&ms->ctl, chan, key, 8192);
			/*
			 * Mathematically, subtract 8192 and scale by
			 * MIDIPITCH_SEMITONE/8192. Practically, subtract 8192
			 * and then << 1.
			 */
			ms->chnstate[chan].tuning_fine = ( mp - 8192 ) << 1;
			midisyn_upd_pitch(ms, chan);
		}
		break;
	case MIDICTL_RPN | MIDI_RPN_CHANNEL_COARSE_TUNING:
		ms->chnstate[chan].pendingreset &= ~PEND_TNC;
		if ( drvhandled )
			ms->chnstate[chan].tuning_coarse = 0;
		else {
			midipitch_t mp;
			/*
			 * By definition only the MSB of this parameter is used.
			 * Subtract 64 for a signed count of semitones; << 14
			 * will convert to midipitch scale.
			 */
			mp = midictl_rpn_read(&ms->ctl, chan, key, 64<<7) >> 7;
			ms->chnstate[chan].tuning_coarse = ( mp - 64 ) << 14;
			midisyn_upd_pitch(ms, chan);
		}
		break;
	}
}

static midipitch_t
midisyn_clamp_pitch(midipitch_t mp)
{

	if ( mp <= 0 )
		return 0;
	if ( mp >= MIDIPITCH_MAX )
		return MIDIPITCH_MAX;
	return mp;
}

static int16_t
midisyn_adj_level(midisyn *ms, uint_fast8_t chan)
{
	int32_t level;

	KASSERT(mutex_owned(ms->lock));
	
	level = ms->chnstate[chan].volume + ms->chnstate[chan].expression;
	if ( level <= INT16_MIN )
		return INT16_MIN;
	return level;
}

static midipitch_t
midisyn_adj_pitch(midisyn *ms, uint_fast8_t chan)
{
	struct channelstate *s = ms->chnstate + chan;

	KASSERT(mutex_owned(ms->lock));

	return s->bend + s->tuning_fine +s->tuning_coarse;
}

#define VOICECHAN_FOREACH_BEGIN(ms,vp,ch)			\
	{							\
		struct voice *vp, *_end_##vp;			\
		for (vp=(ms)->voices,_end_##vp=vp+(ms)->nvoice;	\
		    vp < _end_##vp; ++ vp) {			\
			if ( !vp->inuse )			\
				continue;			\
			if ( MS_GETCHAN(vp) == (ch) )		\
				;				\
			else					\
				continue;
#define VOICECHAN_FOREACH_END }}

static void
midisyn_chan_releasev(midisyn *ms, uint_fast8_t chan, uint_fast8_t vel)
{

	KASSERT(mutex_owned(ms->lock));

	VOICECHAN_FOREACH_BEGIN(ms,vp,chan)
		ms->mets->releasev(ms, vp - ms->voices, vel);
		midisyn_freevoice(ms, vp - ms->voices);
	VOICECHAN_FOREACH_END
}

static void
midisyn_upd_level(midisyn *ms, uint_fast8_t chan)
{
	int32_t level;
	int16_t chan_level;

	KASSERT(mutex_owned(ms->lock));

	if ( NULL == ms->mets->relevelv )
		return;
	
	if ( ms->chnstate[chan].pendingreset & PEND_LEVEL )
		return;

	chan_level = midisyn_adj_level(ms, chan);
	
	VOICECHAN_FOREACH_BEGIN(ms,vp,chan)
		level = vp->velcB + chan_level;
		ms->mets->relevelv(ms, vp - ms->voices,
		    level <= INT16_MIN ? INT16_MIN : level);
	VOICECHAN_FOREACH_END
}

static void
midisyn_upd_pitch(midisyn *ms, uint_fast8_t chan)
{
	midipitch_t chan_adj;

	KASSERT(mutex_owned(ms->lock));
	
	if ( NULL == ms->mets->repitchv )
		return;
	
	if ( ms->chnstate[chan].pendingreset & PEND_PITCH )
		return;

	chan_adj = midisyn_adj_pitch(ms, chan);
	
	VOICECHAN_FOREACH_BEGIN(ms,vp,chan)
		ms->mets->repitchv(ms, vp - ms->voices,
		    midisyn_clamp_pitch(chan_adj +
		        MIDIPITCH_FROM_KEY(vp->chan_note&0x7f)));
	VOICECHAN_FOREACH_END
}

#undef VOICECHAN_FOREACH_END
#undef VOICECHAN_FOREACH_BEGIN

int16_t
midisyn_vol2cB(uint_fast16_t vol)
{
	int16_t cB = 0;
	int32_t v;
	
	if ( 0 == vol )
		return INT16_MIN;
	/*
	 * Adjust vol to fall in the range 8192..16383. Each doubling is
	 * worth 12 dB.
	 */
	while ( vol < 8192 ) {
		vol <<= 1;
		cB -= 120;
	}
	v = vol; /* ensure evaluation in signed 32 bit below */
	/*
	 * The GM vol-to-dB formula is dB = 40 log ( v / 127 ) for 7-bit v.
	 * The vol and expression controllers are in 14-bit space so the
	 * equivalent is 40 log ( v / 16256 ) - that is, MSB 127 LSB 0 because
	 * the LSB is commonly unused. MSB 127 LSB 127 would then be a tiny
	 * bit over.
	 * 1 dB resolution is a little coarser than we'd like, so let's shoot
	 * for centibels, i.e. 400 log ( v / 16256 ), and shift everything left
	 * as far as will fit in 32 bits, which turns out to be a shift of 22.
	 * This minimax polynomial approximation is good to about a centibel
	 * on the range 8192..16256, a shade worse (1.4 or so) above that.
	 * 26385/10166 is the 6th convergent of the coefficient for v^2.
	 */
	cB += ( v * ( 124828 - ( v * 26385 ) / 10166 ) - 1347349038 ) >> 22;
	return cB;
}

/*
 * MIDI RP-012 constitutes a MIDI Tuning Specification. The units are
 * fractional-MIDIkeys, that is, the key number 00 - 7f left shifted
 * 14 bits to provide a 14-bit fraction that divides each semitone. The
 * whole thing is just a 21-bit number that is bent and tuned simply by
 * adding and subtracting--the same offset is the same pitch change anywhere
 * on the scale. One downside is that a cent is 163.84 of these units, so
 * you can't expect a lengthy integer sum of cents to come out in tune; if you
 * do anything in cents it is best to use them only for local adjustment of
 * a pitch.
 * 
 * This function converts a pitch in MIDItune units to Hz left-shifted 18 bits.
 * That should leave you enough to shift down to whatever precision the hardware
 * supports.
 *
 * Its prototype is exposed in <sys/midiio.h>.
 */
midihz18_t
midisyn_mp2hz18(midipitch_t mp)
{
	int64_t t64a, t64b;
	uint_fast8_t shift;
	
	/*
	 * Scale from the logarithmic MIDI-Tuning units to Hz<<18. Uses the
	 * continued-fraction form of a 2/2 rational function derived to
	 * cover the highest octave (mt 1900544..2097151 or 74.00.00..7f.7f.7f
	 * in RP-012-speak, the dotted bits are 7 wide) to produce Hz shifted
	 * left just as far as the maximum Hz will fit in a uint32, which
	 * turns out to be 18. Just shift off the result for lower octaves.
	 * Fit is within 1/4 MIDI tuning unit throughout (disclaimer: the
	 * comparison relied on the double-precision log in libm).
	 */

	if ( 0 == mp )
		return 2143236;
	
	for ( shift = 0; mp < 1900544; ++ shift )
		mp += MIDIPITCH_OCTAVE;

	if ( 1998848 == mp )
		return UINT32_C(2463438621) >> shift;
	
	t64a  = 0x5a1a0ee4; /* INT64_C(967879298788) gcc333: spurious warning */
	t64a |= (int64_t)0xe1 << 32;
	t64a /= mp - 1998848; /* here's why 1998848 is special-cased above ;) */
	t64a += mp - 3704981;
	t64b  = 0x6763759d; /* INT64_C(8405905567872413) goofy warning again */
	t64b |= (int64_t)0x1ddd20 << 32;
	t64b /= t64a;
	t64b += UINT32_C(2463438619);
	return (uint32_t)t64b >> shift;
}