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