1 /* $NetBSD: midisyn.c,v 1.21 2007/03/04 06:01:42 christos Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss@NetBSD.org).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: midisyn.c,v 1.21 2007/03/04 06:01:42 christos Exp $");
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/fcntl.h>
38 #include <sys/vnode.h>
39 #include <sys/select.h>
41 #include <sys/malloc.h>
42 #include <sys/systm.h>
43 #include <sys/syslog.h>
44 #include <sys/kernel.h>
45 #include <sys/audioio.h>
46 #include <sys/midiio.h>
47 #include <sys/device.h>
49 #include <dev/audio_if.h>
50 #include <dev/midi_if.h>
51 #include <dev/midivar.h>
52 #include <dev/midisynvar.h>
55 #define DPRINTF(x) if (midisyndebug) printf x
56 #define DPRINTFN(n,x) if (midisyndebug >= (n)) printf x
63 int midisyn_findvoice(midisyn
*, int, int);
64 void midisyn_freevoice(midisyn
*, int);
65 uint_fast16_t midisyn_allocvoice(midisyn
*, uint_fast8_t, uint_fast8_t);
66 static void midisyn_attackv_vel(midisyn
*, uint_fast16_t, midipitch_t
,
67 int16_t, uint_fast8_t);
69 static midictl_notify midisyn_notify
;
71 static midipitch_t
midisyn_clamp_pitch(midipitch_t
);
72 static int16_t midisyn_adj_level(midisyn
*, uint_fast8_t);
73 static midipitch_t
midisyn_adj_pitch(midisyn
*, uint_fast8_t);
74 static void midisyn_chan_releasev(midisyn
*, uint_fast8_t, uint_fast8_t);
75 static void midisyn_upd_level(midisyn
*, uint_fast8_t);
76 static void midisyn_upd_pitch(midisyn
*, uint_fast8_t);
78 int midisyn_open(void *, int,
79 void (*iintr
)(void *, int),
80 void (*ointr
)(void *), void *arg
);
81 void midisyn_close(void *);
82 int midisyn_sysrt(void *, int);
83 void midisyn_getinfo(void *, struct midi_info
*);
84 int midisyn_ioctl(void *, u_long
, void *, int, struct lwp
*);
86 const struct midi_hw_if midisyn_hw_if
= {
94 int midisyn_channelmsg(void *, int, int, u_char
*, int);
95 int midisyn_commonmsg(void *, int, u_char
*, int);
96 int midisyn_sysex(void *, u_char
*, int);
98 struct midi_hw_if_ext midisyn_hw_if_ext
= {
99 .channel
= midisyn_channelmsg
,
100 .common
= midisyn_commonmsg
,
101 .sysex
= midisyn_sysex
,
104 struct channelstate
{ /* dyamically allocated in open() on account of size */
105 /* volume state components in centibels; just sum for overall level */
108 /* pitch state components in midipitch units; sum for overall effect */
110 midipitch_t tuning_fine
;
111 midipitch_t tuning_coarse
;
112 /* used by bend handlers */
114 int16_t pendingreset
;
115 /* rearrange as more controls supported - 16 bits should last for a while */
118 #define PEND_LEVEL (PEND_VOL|PEND_EXP)
122 #define PEND_PITCH (PEND_PBS|PEND_TNF|PEND_TNC)
123 #define PEND_ALL (PEND_LEVEL|PEND_PITCH)
127 midisyn_open(void *addr
, int flags
, void (*iintr
)(void *, int),
128 void (*ointr
)(void *), void *arg
)
134 DPRINTF(("midisyn_open: ms=%p ms->mets=%p\n", ms
, ms
->mets
));
136 midictl_open(&ms
->ctl
);
138 ms
->chnstate
= malloc(MIDI_MAX_CHANS
*sizeof *(ms
->chnstate
),
139 M_DEVBUF
, M_WAITOK
); /* init'd by RESET below */
143 rslt
= (ms
->mets
->open(ms
, flags
));
146 * Make the right initial things happen by faking receipt of RESET on
147 * all channels. The hw driver's ctlnotice() will be called in turn.
149 for ( chan
= 0 ; chan
< MIDI_MAX_CHANS
; ++ chan
)
150 midisyn_notify(ms
, MIDICTL_RESET
, chan
, 0);
156 midisyn_close(void *addr
)
159 struct midisyn_methods
*fs
;
162 DPRINTF(("midisyn_close: ms=%p ms->mets=%p\n", ms
, ms
->mets
));
165 for (chan
= 0; chan
< MIDI_MAX_CHANS
; chan
++)
166 midisyn_notify(ms
, MIDICTL_SOUND_OFF
, chan
, 0);
171 free(ms
->chnstate
, M_DEVBUF
);
173 midictl_close(&ms
->ctl
);
177 midisyn_getinfo(void *addr
, struct midi_info
*mi
)
183 * I was going to add a property here to suppress midi(4)'s warning
184 * about an output device that uses no transmit interrupt, on the
185 * assumption that as an onboard synth we handle "output" internally
186 * with nothing like the 320 us per byte busy wait of a dumb UART.
187 * Then I noticed that opl (at least as currently implemented) seems
188 * to need 40 us busy wait to set each register on an OPL2, and sets
189 * about 21 registers for every note-on. (Half of that is patch loading
190 * and could probably be reduced by different management of voices and
191 * patches.) For now I won't bother suppressing that warning....
195 midi_register_hw_if_ext(&midisyn_hw_if_ext
);
199 midisyn_ioctl(void *maddr
, u_long cmd
, void *addr
, int flag
, struct lwp
*l
)
204 return (ms
->mets
->ioctl(ms
, cmd
, addr
, flag
, l
));
210 midisyn_findvoice(midisyn
*ms
, int chan
, int note
)
215 cn
= MS_CHANNOTE(chan
, note
);
216 for (v
= 0; v
< ms
->nvoice
; v
++)
217 if (ms
->voices
[v
].chan_note
== cn
&& ms
->voices
[v
].inuse
)
223 midisyn_attach(struct midi_softc
*sc
, midisyn
*ms
)
226 * XXX there should be a way for this function to indicate failure
227 * (other than panic) if some preconditions aren't met, for example
228 * if some nonoptional methods are missing.
230 if (ms
->mets
->allocv
== 0) {
231 ms
->voices
= malloc(ms
->nvoice
* sizeof (struct voice
),
232 M_DEVBUF
, M_WAITOK
|M_ZERO
);
234 ms
->mets
->allocv
= midisyn_allocvoice
;
237 if (ms
->mets
->attackv_vel
== 0 && ms
->mets
->attackv
!= 0)
238 ms
->mets
->attackv_vel
= midisyn_attackv_vel
;
240 ms
->ctl
= (midictl
) {
243 .notify
= midisyn_notify
246 sc
->hw_if
= &midisyn_hw_if
;
248 DPRINTF(("midisyn_attach: ms=%p\n", sc
->hw_hdl
));
252 midisyn_freevoice(midisyn
*ms
, int voice
)
254 if (ms
->mets
->allocv
!= midisyn_allocvoice
)
256 ms
->voices
[voice
].inuse
= 0;
260 midisyn_allocvoice(midisyn
*ms
, uint_fast8_t chan
, uint_fast8_t note
)
265 /* Find a free voice, or if no free voice is found the oldest. */
267 bestseq
= ms
->voices
[0].seqno
+ (ms
->voices
[0].inuse
? 0x40000000 : 0);
268 for (v
= 1; v
< ms
->nvoice
; v
++) {
269 s
= ms
->voices
[v
].seqno
;
270 if (ms
->voices
[v
].inuse
)
277 DPRINTFN(10,("midisyn_allocvoice: v=%d seq=%d cn=%x inuse=%d\n",
278 bestv
, ms
->voices
[bestv
].seqno
,
279 ms
->voices
[bestv
].chan_note
,
280 ms
->voices
[bestv
].inuse
));
282 if (ms
->voices
[bestv
].inuse
)
283 DPRINTFN(1,("midisyn_allocvoice: steal %x\n",
284 ms
->voices
[bestv
].chan_note
));
286 ms
->voices
[bestv
].chan_note
= MS_CHANNOTE(chan
, note
);
287 ms
->voices
[bestv
].seqno
= ms
->seqno
++;
288 ms
->voices
[bestv
].inuse
= 1;
292 /* dummy attackv_vel that just adds vel into level for simple drivers */
294 midisyn_attackv_vel(midisyn
*ms
, uint_fast16_t voice
, midipitch_t mp
,
295 int16_t level_cB
, uint_fast8_t vel
)
297 ms
->voices
[voice
].velcB
= midisyn_vol2cB((uint_fast16_t)vel
<< 7);
298 ms
->mets
->attackv(ms
, voice
, mp
, level_cB
+ ms
->voices
[voice
].velcB
);
302 midisyn_sysrt(void *addr
, int b
)
307 int midisyn_channelmsg(void *addr
, int status
, int chan
, u_char
*buf
,
311 int voice
= 0; /* initialize to keep gcc quiet */
312 struct midisyn_methods
*fs
;
314 DPRINTF(("midisyn_channelmsg: ms=%p status=%#02x chan=%d\n",
321 * for a device that leaves voice allocation to us--and that's
322 * all of 'em at the moment--the voice and release velocity
323 * should be the only necessary arguments to noteoff. what use
324 * are they making of note? checking... None. Cool.
325 * IF there is ever a device added that does its own allocation,
326 * extend the interface; this findvoice won't be what to do...
328 voice
= midisyn_findvoice(ms
, chan
, buf
[1]);
330 fs
->releasev(ms
, voice
, buf
[2]);
331 midisyn_freevoice(ms
, voice
);
336 * what's called for here, given current drivers, is an i/f
337 * where midisyn computes a volume from vel*volume*expression*
338 * mastervolume and passes that result as a single arg. It can
339 * evolve later to support drivers that expose some of those
340 * bits separately (e.g. a driver could expose a mixer register
341 * on its sound card and use that for mastervolume).
343 voice
= fs
->allocv(ms
, chan
, buf
[1]);
344 ms
->voices
[voice
].velcB
= 0; /* assume driver handles vel */
345 fs
->attackv_vel(ms
, voice
,
346 midisyn_clamp_pitch(MIDIPITCH_FROM_KEY(buf
[1]) +
347 midisyn_adj_pitch(ms
, chan
)),
348 midisyn_adj_level(ms
,chan
), buf
[2]);
350 case MIDI_KEY_PRESSURE
:
352 * unimplemented by the existing drivers. if we are doing
353 * voice allocation, find the voice that corresponds to this
354 * chan/note and define a method that passes the voice and
355 * pressure to the driver ... not the note, /it/ doesn't matter.
356 * For a driver that does its own allocation, a different
357 * method may be needed passing pressure, chan, note so it can
358 * find the right voice on its own. Be sure that whatever is
359 * done here is undone when midisyn_notify sees MIDICTL_RESET.
362 case MIDI_CTL_CHANGE
:
363 midictl_change(&ms
->ctl
, chan
, buf
+1);
365 case MIDI_PGM_CHANGE
:
367 fs
->pgmchg(ms
, chan
, buf
[1]);
369 case MIDI_CHN_PRESSURE
:
371 * unimplemented by the existing drivers. if driver exposes no
372 * distinct method, can use KEY_PRESSURE method for each voice
373 * on channel. Be sure that whatever is
374 * done here is undone when midisyn_notify sees MIDICTL_RESET.
377 case MIDI_PITCH_BEND
:
379 * Will work for most drivers that simply render the midipitch
380 * as we pass it (but not cms, which chops all the bits after
381 * the note number and then computes its own pitch :( ). If the
382 * driver has a repitchv method for voices already sounding, so
384 * The bending logic lives in the handler for bend sensitivity,
385 * so fake a change to that to kick it off.
387 ms
->chnstate
[chan
].bendraw
= buf
[2]<<7 | buf
[1];
388 ms
->chnstate
[chan
].bendraw
-= MIDI_BEND_NEUTRAL
;
389 midisyn_notify(ms
, MIDICTL_RPN
, chan
,
390 MIDI_RPN_PITCH_BEND_SENSITIVITY
);
396 int midisyn_commonmsg(void *addr
, int status
,
397 u_char
*buf
, int len
)
402 int midisyn_sysex(void *addr
, u_char
*buf
, int len
)
405 * unimplemented by existing drivers. it is surely more sensible
406 * to do some parsing of well-defined sysex messages here, either
407 * handling them internally or calling specific methods on the
408 * driver after parsing out the details, than to ask every driver
409 * to deal with sysex messages poked at it a byte at a time.
415 midisyn_notify(void *cookie
, midictl_evt evt
,
416 uint_fast8_t chan
, uint_fast16_t key
)
421 ms
= (struct midisyn
*)cookie
;
423 if ( ms
->mets
->ctlnotice
)
424 drvhandled
= ms
->mets
->ctlnotice(ms
, evt
, chan
, key
);
426 switch ( evt
| key
) {
429 * Re-read all ctls we use, revert pitchbend state.
430 * Can do it by faking change notifications.
432 ms
->chnstate
[chan
].pendingreset
|= PEND_ALL
;
433 midisyn_notify(ms
, MIDICTL_CTLR
, chan
,
434 MIDI_CTRL_CHANNEL_VOLUME_MSB
);
435 midisyn_notify(ms
, MIDICTL_CTLR
, chan
,
436 MIDI_CTRL_EXPRESSION_MSB
);
437 ms
->chnstate
[chan
].bendraw
= 0; /* MIDI_BEND_NEUTRAL - itself */
438 midisyn_notify(ms
, MIDICTL_RPN
, chan
,
439 MIDI_RPN_PITCH_BEND_SENSITIVITY
);
440 midisyn_notify(ms
, MIDICTL_RPN
, chan
,
441 MIDI_RPN_CHANNEL_FINE_TUNING
);
442 midisyn_notify(ms
, MIDICTL_RPN
, chan
,
443 MIDI_RPN_CHANNEL_COARSE_TUNING
);
445 case MIDICTL_NOTES_OFF
:
448 /* releasev all voices sounding on chan; use normal vel 64 */
449 midisyn_chan_releasev(ms
, chan
, 64);
451 case MIDICTL_SOUND_OFF
:
454 /* releasev all voices sounding on chan; use max vel 127 */
455 /* it is really better for driver to handle this, instantly */
456 midisyn_chan_releasev(ms
, chan
, 127);
458 case MIDICTL_CTLR
| MIDI_CTRL_CHANNEL_VOLUME_MSB
:
459 ms
->chnstate
[chan
].pendingreset
&= ~PEND_VOL
;
461 ms
->chnstate
[chan
].volume
= 0;
464 ms
->chnstate
[chan
].volume
= midisyn_vol2cB(
465 midictl_read(&ms
->ctl
, chan
, key
, 100<<7));
466 midisyn_upd_level(ms
, chan
);
468 case MIDICTL_CTLR
| MIDI_CTRL_EXPRESSION_MSB
:
469 ms
->chnstate
[chan
].pendingreset
&= ~PEND_EXP
;
471 ms
->chnstate
[chan
].expression
= 0;
474 ms
->chnstate
[chan
].expression
= midisyn_vol2cB(
475 midictl_read(&ms
->ctl
, chan
, key
, 16383));
476 midisyn_upd_level(ms
, chan
);
479 * SOFT_PEDAL: supporting this will be trickier; must apply only
480 * to notes subsequently struck, and must remember which voices
481 * they are for follow-on adjustments. For another day....
483 case MIDICTL_RPN
| MIDI_RPN_PITCH_BEND_SENSITIVITY
:
484 ms
->chnstate
[chan
].pendingreset
&= ~PEND_PBS
;
486 ms
->chnstate
[chan
].bend
= 0;
490 w
= midictl_rpn_read(&ms
->ctl
, chan
, key
, 2<<7);
494 * Mathematically, multiply semis by
495 * MIDIPITCH_SEMITONE*bendraw/8192. Practically, avoid
496 * shifting significant bits off by observing that
497 * MIDIPITCH_SEMITONE == 1<<14 and 8192 == 1<<13, so
498 * just take semis*bendraw<<1. Do the same with cents
499 * except <<1 becomes /50 (but rounded).
501 ms
->chnstate
[chan
].bend
=
502 ( ms
->chnstate
[chan
].bendraw
* semis
) << 1;
503 ms
->chnstate
[chan
].bend
+=
504 ((ms
->chnstate
[chan
].bendraw
* cents
)/25 + 1) >> 1;
505 midisyn_upd_pitch(ms
, chan
);
508 case MIDICTL_RPN
| MIDI_RPN_CHANNEL_FINE_TUNING
:
510 ms
->chnstate
[chan
].tuning_fine
= 0;
513 mp
= midictl_rpn_read(&ms
->ctl
, chan
, key
, 8192);
515 * Mathematically, subtract 8192 and scale by
516 * MIDIPITCH_SEMITONE/8192. Practically, subtract 8192
519 ms
->chnstate
[chan
].tuning_fine
= ( mp
- 8192 ) << 1;
520 midisyn_upd_pitch(ms
, chan
);
523 case MIDICTL_RPN
| MIDI_RPN_CHANNEL_COARSE_TUNING
:
524 ms
->chnstate
[chan
].pendingreset
&= ~PEND_TNC
;
526 ms
->chnstate
[chan
].tuning_coarse
= 0;
530 * By definition only the MSB of this parameter is used.
531 * Subtract 64 for a signed count of semitones; << 14
532 * will convert to midipitch scale.
534 mp
= midictl_rpn_read(&ms
->ctl
, chan
, key
, 64<<7) >> 7;
535 ms
->chnstate
[chan
].tuning_coarse
= ( mp
- 64 ) << 14;
536 midisyn_upd_pitch(ms
, chan
);
543 midisyn_clamp_pitch(midipitch_t mp
)
547 if ( mp
>= MIDIPITCH_MAX
)
548 return MIDIPITCH_MAX
;
553 midisyn_adj_level(midisyn
*ms
, uint_fast8_t chan
)
557 level
= ms
->chnstate
[chan
].volume
+ ms
->chnstate
[chan
].expression
;
558 if ( level
<= INT16_MIN
)
564 midisyn_adj_pitch(midisyn
*ms
, uint_fast8_t chan
)
566 struct channelstate
*s
= ms
->chnstate
+ chan
;
567 return s
->bend
+ s
->tuning_fine
+s
->tuning_coarse
;
570 #define VOICECHAN_FOREACH_BEGIN(ms,vp,ch) \
572 struct voice *vp, *_end_##vp; \
573 for (vp=(ms)->voices,_end_##vp=vp+(ms)->nvoice; \
574 vp < _end_##vp; ++ vp) { \
577 if ( MS_GETCHAN(vp) == (ch) ) \
581 #define VOICECHAN_FOREACH_END }}
584 midisyn_chan_releasev(midisyn
*ms
, uint_fast8_t chan
, uint_fast8_t vel
)
586 VOICECHAN_FOREACH_BEGIN(ms
,vp
,chan
)
587 ms
->mets
->releasev(ms
, vp
- ms
->voices
, vel
);
588 midisyn_freevoice(ms
, vp
- ms
->voices
);
589 VOICECHAN_FOREACH_END
593 midisyn_upd_level(midisyn
*ms
, uint_fast8_t chan
)
597 if ( NULL
== ms
->mets
->relevelv
)
600 if ( ms
->chnstate
[chan
].pendingreset
& PEND_LEVEL
)
603 chan_level
= midisyn_adj_level(ms
, chan
);
605 VOICECHAN_FOREACH_BEGIN(ms
,vp
,chan
)
606 level
= vp
->velcB
+ chan_level
;
607 ms
->mets
->relevelv(ms
, vp
- ms
->voices
,
608 level
<= INT16_MIN
? INT16_MIN
: level
);
609 VOICECHAN_FOREACH_END
613 midisyn_upd_pitch(midisyn
*ms
, uint_fast8_t chan
)
615 midipitch_t chan_adj
;
617 if ( NULL
== ms
->mets
->repitchv
)
620 if ( ms
->chnstate
[chan
].pendingreset
& PEND_PITCH
)
623 chan_adj
= midisyn_adj_pitch(ms
, chan
);
625 VOICECHAN_FOREACH_BEGIN(ms
,vp
,chan
)
626 ms
->mets
->repitchv(ms
, vp
- ms
->voices
,
627 midisyn_clamp_pitch(chan_adj
+
628 MIDIPITCH_FROM_KEY(vp
->chan_note
&0x7f)));
629 VOICECHAN_FOREACH_END
632 #undef VOICECHAN_FOREACH_END
633 #undef VOICECHAN_FOREACH_BEGIN
636 midisyn_vol2cB(uint_fast16_t vol
)
644 * Adjust vol to fall in the range 8192..16383. Each doubling is
647 while ( vol
< 8192 ) {
651 v
= vol
; /* ensure evaluation in signed 32 bit below */
653 * The GM vol-to-dB formula is dB = 40 log ( v / 127 ) for 7-bit v.
654 * The vol and expression controllers are in 14-bit space so the
655 * equivalent is 40 log ( v / 16256 ) - that is, MSB 127 LSB 0 because
656 * the LSB is commonly unused. MSB 127 LSB 127 would then be a tiny
658 * 1 dB resolution is a little coarser than we'd like, so let's shoot
659 * for centibels, i.e. 400 log ( v / 16256 ), and shift everything left
660 * as far as will fit in 32 bits, which turns out to be a shift of 22.
661 * This minimax polynomial approximation is good to about a centibel
662 * on the range 8192..16256, a shade worse (1.4 or so) above that.
663 * 26385/10166 is the 6th convergent of the coefficient for v^2.
665 cB
+= ( v
* ( 124828 - ( v
* 26385 ) / 10166 ) - 1347349038 ) >> 22;
670 * MIDI RP-012 constitutes a MIDI Tuning Specification. The units are
671 * fractional-MIDIkeys, that is, the key number 00 - 7f left shifted
672 * 14 bits to provide a 14-bit fraction that divides each semitone. The
673 * whole thing is just a 21-bit number that is bent and tuned simply by
674 * adding and subtracting--the same offset is the same pitch change anywhere
675 * on the scale. One downside is that a cent is 163.84 of these units, so
676 * you can't expect a lengthy integer sum of cents to come out in tune; if you
677 * do anything in cents it is best to use them only for local adjustment of
680 * This function converts a pitch in MIDItune units to Hz left-shifted 18 bits.
681 * That should leave you enough to shift down to whatever precision the hardware
684 * Its prototype is exposed in <sys/midiio.h>.
687 midisyn_mp2hz18(midipitch_t mp
)
693 * Scale from the logarithmic MIDI-Tuning units to Hz<<18. Uses the
694 * continued-fraction form of a 2/2 rational function derived to
695 * cover the highest octave (mt 1900544..2097151 or 74.00.00..7f.7f.7f
696 * in RP-012-speak, the dotted bits are 7 wide) to produce Hz shifted
697 * left just as far as the maximum Hz will fit in a uint32, which
698 * turns out to be 18. Just shift off the result for lower octaves.
699 * Fit is within 1/4 MIDI tuning unit throughout (disclaimer: the
700 * comparison relied on the double-precision log in libm).
706 for ( shift
= 0; mp
< 1900544; ++ shift
)
707 mp
+= MIDIPITCH_OCTAVE
;
710 return UINT32_C(2463438621) >> shift
;
712 t64a
= 0x5a1a0ee4; /* INT64_C(967879298788) gcc333: spurious warning */
713 t64a
|= (int64_t)0xe1 << 32;
714 t64a
/= mp
- 1998848; /* here's why 1998848 is special-cased above ;) */
715 t64a
+= mp
- 3704981;
716 t64b
= 0x6763759d; /* INT64_C(8405905567872413) goofy warning again */
717 t64b
|= (int64_t)0x1ddd20 << 32;
719 t64b
+= UINT32_C(2463438619);
720 return (uint32_t)t64b
>> shift
;