midisyn.c revision 1.17.2.13 1 /* $NetBSD: midisyn.c,v 1.17.2.13 2006/06/07 05:31:43 chap Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) NetBSD.org).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: midisyn.c,v 1.17.2.13 2006/06/07 05:31:43 chap Exp $");
41
42 #include <sys/param.h>
43 #include <sys/ioctl.h>
44 #include <sys/fcntl.h>
45 #include <sys/vnode.h>
46 #include <sys/select.h>
47 #include <sys/proc.h>
48 #include <sys/malloc.h>
49 #include <sys/systm.h>
50 #include <sys/syslog.h>
51 #include <sys/kernel.h>
52 #include <sys/audioio.h>
53 #include <sys/midiio.h>
54 #include <sys/device.h>
55
56 #include <dev/audio_if.h>
57 #include <dev/midi_if.h>
58 #include <dev/midivar.h>
59 #include <dev/midisynvar.h>
60
61 #ifdef AUDIO_DEBUG
62 #define DPRINTF(x) if (midisyndebug) printf x
63 #define DPRINTFN(n,x) if (midisyndebug >= (n)) printf x
64 int midisyndebug = 0;
65 #else
66 #define DPRINTF(x)
67 #define DPRINTFN(n,x)
68 #endif
69
70 int midisyn_findvoice(midisyn *, int, int);
71 void midisyn_freevoice(midisyn *, int);
72 int midisyn_allocvoice(midisyn *, u_int32_t, u_int32_t);
73 u_int32_t midisyn_note_to_freq(int);
74 u_int32_t midisyn_finetune(u_int32_t, int, int, int);
75
76 static midictl_notify midisyn_notify;
77
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, caddr_t, int, struct lwp *);
85
86 const struct midi_hw_if midisyn_hw_if = {
87 midisyn_open,
88 midisyn_close,
89 midisyn_sysrt,
90 midisyn_getinfo,
91 midisyn_ioctl,
92 };
93
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);
97
98 struct midi_hw_if_ext midisyn_hw_if_ext = {
99 .channel = midisyn_channelmsg,
100 .common = midisyn_commonmsg,
101 .sysex = midisyn_sysex,
102 };
103
104 int
105 midisyn_open(void *addr, int flags, void (*iintr)(void *, int),
106 void (*ointr)(void *), void *arg)
107 {
108 midisyn *ms = addr;
109
110 DPRINTF(("midisyn_open: ms=%p ms->mets=%p\n", ms, ms->mets));
111
112 midictl_open(&ms->ctl);
113
114 if (ms->mets->open)
115 return (ms->mets->open(ms, flags));
116 else
117 return (0);
118 }
119
120 void
121 midisyn_close(void *addr)
122 {
123 midisyn *ms = addr;
124 struct midisyn_methods *fs;
125 int v;
126
127 DPRINTF(("midisyn_close: ms=%p ms->mets=%p\n", ms, ms->mets));
128 fs = ms->mets;
129 for (v = 0; v < ms->nvoice; v++)
130 if (ms->voices[v].inuse) {
131 fs->noteoff(ms, v, 0, 0);
132 midisyn_freevoice(ms, v);
133 }
134 if (fs->close)
135 fs->close(ms);
136
137 midictl_close(&ms->ctl);
138 }
139
140 void
141 midisyn_getinfo(void *addr, struct midi_info *mi)
142 {
143 midisyn *ms = addr;
144
145 mi->name = ms->name;
146 /*
147 * I was going to add a property here to suppress midi(4)'s warning
148 * about an output device that uses no transmit interrupt, on the
149 * assumption that as an onboard synth we handle "output" internally
150 * with nothing like the 320 us per byte busy wait of a dumb UART.
151 * Then I noticed that opl (at least as currently implemented) seems
152 * to need 40 us busy wait to set each register on an OPL2, and sets
153 * about 21 registers for every note-on. (Half of that is patch loading
154 * and could probably be reduced by different management of voices and
155 * patches.) For now I won't bother suppressing that warning....
156 */
157 mi->props = 0;
158
159 midi_register_hw_if_ext(&midisyn_hw_if_ext);
160 }
161
162 int
163 midisyn_ioctl(void *maddr, u_long cmd, caddr_t addr, int flag, struct lwp *l)
164 {
165 midisyn *ms = maddr;
166
167 if (ms->mets->ioctl)
168 return (ms->mets->ioctl(ms, cmd, addr, flag, l));
169 else
170 return (EINVAL);
171 }
172
173 int
174 midisyn_findvoice(midisyn *ms, int chan, int note)
175 {
176 u_int cn;
177 int v;
178
179 if (!(ms->flags & MS_DOALLOC))
180 return (chan);
181 cn = MS_CHANNOTE(chan, note);
182 for (v = 0; v < ms->nvoice; v++)
183 if (ms->voices[v].chan_note == cn && ms->voices[v].inuse)
184 return (v);
185 return (-1);
186 }
187
188 void
189 midisyn_attach(struct midi_softc *sc, midisyn *ms)
190 {
191 if (ms->flags & MS_DOALLOC) {
192 ms->voices = malloc(ms->nvoice * sizeof (struct voice),
193 M_DEVBUF, M_WAITOK|M_ZERO);
194 ms->seqno = 1;
195 if (ms->mets->allocv == 0)
196 ms->mets->allocv = &midisyn_allocvoice;
197 }
198
199 ms->ctl = (midictl) {
200 .base_channel = 16,
201 .cookie = ms,
202 .notify = midisyn_notify
203 };
204
205 sc->hw_if = &midisyn_hw_if;
206 sc->hw_hdl = ms;
207 DPRINTF(("midisyn_attach: ms=%p\n", sc->hw_hdl));
208 }
209
210 void
211 midisyn_freevoice(midisyn *ms, int voice)
212 {
213 if (!(ms->flags & MS_DOALLOC))
214 return;
215 ms->voices[voice].inuse = 0;
216 }
217
218 int
219 midisyn_allocvoice(midisyn *ms, u_int32_t chan, u_int32_t note)
220 {
221 int bestv, v;
222 u_int bestseq, s;
223
224 if (!(ms->flags & MS_DOALLOC))
225 return (chan);
226 /* Find a free voice, or if no free voice is found the oldest. */
227 bestv = 0;
228 bestseq = ms->voices[0].seqno + (ms->voices[0].inuse ? 0x40000000 : 0);
229 for (v = 1; v < ms->nvoice; v++) {
230 s = ms->voices[v].seqno;
231 if (ms->voices[v].inuse)
232 s += 0x40000000;
233 if (s < bestseq) {
234 bestseq = s;
235 bestv = v;
236 }
237 }
238 DPRINTFN(10,("midisyn_allocvoice: v=%d seq=%d cn=%x inuse=%d\n",
239 bestv, ms->voices[bestv].seqno,
240 ms->voices[bestv].chan_note,
241 ms->voices[bestv].inuse));
242 #ifdef AUDIO_DEBUG
243 if (ms->voices[bestv].inuse)
244 DPRINTFN(1,("midisyn_allocvoice: steal %x\n",
245 ms->voices[bestv].chan_note));
246 #endif
247 ms->voices[bestv].chan_note = MS_CHANNOTE(chan, note);
248 ms->voices[bestv].seqno = ms->seqno++;
249 ms->voices[bestv].inuse = 1;
250 return (bestv);
251 }
252
253 int
254 midisyn_sysrt(void *addr, int b)
255 {
256 return 0;
257 }
258
259 int midisyn_channelmsg(void *addr, int status, int chan, u_char *buf, int len)
260 {
261 midisyn *ms = addr;
262 int voice = 0; /* initialize to keep gcc quiet */
263 struct midisyn_methods *fs;
264 u_int32_t note, vel;
265
266 DPRINTF(("midisyn_channelmsg: ms=%p status=%#02x chan=%d\n",
267 ms, status, chan));
268 fs = ms->mets;
269 note = buf[1];
270 if (ms->flags & MS_FREQXLATE)
271 note = midisyn_note_to_freq(note);
272 vel = buf[2];
273
274 switch (status) {
275 case MIDI_NOTEOFF:
276 /*
277 * for a device that leaves voice allocation to us--and that's
278 * all of 'em at the moment--the voice and release velocity
279 * should be the only necessary arguments to noteoff. what use
280 * are they making of note? checking... None. Cool.
281 */
282 voice = midisyn_findvoice(ms, chan, buf[1]);
283 if (voice >= 0) {
284 fs->noteoff(ms, voice, note, vel);
285 midisyn_freevoice(ms, voice);
286 }
287 break;
288 case MIDI_NOTEON:
289 /*
290 * opl combines vel with a 'mainvol' that has no setter
291 * anywhere. pcppi punts volume entirely. cms uses vel alone.
292 * what's called for here, given current drivers, is an i/f
293 * where midisyn computes a volume from vel*volume*expression*
294 * mastervolume and passes that result as a single arg. It can
295 * evolve later to support drivers that expose some of those
296 * bits separately (e.g. a driver could expose a mixer register
297 * on its sound card and use that for mastervolume).
298 */
299 voice = fs->allocv(ms, chan, buf[1]);
300 fs->noteon(ms, voice, note, vel);
301 break;
302 case MIDI_KEY_PRESSURE:
303 /*
304 * unimplemented by the existing drivers. if we are doing
305 * voice allocation, find the voice that corresponds to this
306 * chan/note and define a method that passes the voice and
307 * pressure to the driver ... not the note, /it/ doesn't matter.
308 * For a driver that does its own allocation, a different
309 * method may be needed passing pressure, chan, note so it can
310 * find the right voice on its own. Be sure that whatever is
311 * done here is undone when midisyn_notify sees MIDICTL_RESET.
312 */
313 break;
314 case MIDI_CTL_CHANGE:
315 midictl_change(&ms->ctl, chan, buf+1);
316 break;
317 case MIDI_PGM_CHANGE:
318 if (fs->pgmchg)
319 fs->pgmchg(ms, chan, buf[1]);
320 break;
321 case MIDI_CHN_PRESSURE:
322 /*
323 * unimplemented by the existing drivers. if driver exposes no
324 * distinct method, can use KEY_PRESSURE method for each voice
325 * on channel. Be sure that whatever is
326 * done here is undone when midisyn_notify sees MIDICTL_RESET.
327 */
328 break;
329 case MIDI_PITCH_BEND:
330 /*
331 * unimplemented by existing drivers. it is good that most
332 * existing drivers take a /frequency/ rather than a /note no/
333 * for control; midisyn should use this event to update some
334 * internal state and use it (along with tuning) to derive the
335 * frequency passed to the driver for any note on. A driver that
336 * can change freq of a sounding voice should expose a method
337 * for that, i.e. m(voice,newfreq) and we should call that too,
338 * for each voice on the affected channel, when a pitch bend
339 * change is received. Note RPN 0 must be used in scaling the
340 * pitch bend. Be sure that whatever is
341 * done here is undone when midisyn_notify sees MIDICTL_RESET.
342 */
343 break;
344 }
345 return 0;
346 }
347
348 int midisyn_commonmsg(void *addr, int status, u_char *buf, int len)
349 {
350 return 0;
351 }
352
353 int midisyn_sysex(void *addr, u_char *buf, int len)
354 {
355 /*
356 * unimplemented by existing drivers. it is surely more sensible
357 * to do some parsing of well-defined sysex messages here, either
358 * handling them internally or calling specific methods on the
359 * driver after parsing out the details, than to ask every driver
360 * to deal with sysex messages poked at it a byte at a time.
361 */
362 return 0;
363 }
364
365 /*
366 * Convert a MIDI note to the corresponding frequency.
367 * The frequency is scaled by 2^16.
368 */
369 u_int32_t
370 midisyn_note_to_freq(int note)
371 {
372 int o, n, f;
373 #define BASE_OCTAVE 5
374 static const u_int32_t notes[] = {
375 17145893, 18165441, 19245614, 20390018, 21602472, 22887021,
376 24247954, 25689813, 27217409, 28835840, 30550508, 32367136
377 };
378
379
380 o = note / 12;
381 n = note % 12;
382
383 f = notes[n];
384
385 if (o < BASE_OCTAVE)
386 f >>= (BASE_OCTAVE - o);
387 else if (o > BASE_OCTAVE)
388 f <<= (o - BASE_OCTAVE);
389 return (f);
390 }
391
392 u_int32_t
393 midisyn_finetune(u_int32_t base_freq, int bend, int range, int vibrato_cents)
394 {
395 static const u_int16_t semitone_tuning[24] =
396 {
397 /* 0 */ 10000, 10595, 11225, 11892, 12599, 13348, 14142, 14983,
398 /* 8 */ 15874, 16818, 17818, 18877, 20000, 21189, 22449, 23784,
399 /* 16 */ 25198, 26697, 28284, 29966, 31748, 33636, 35636, 37755
400 };
401 static const u_int16_t cent_tuning[100] =
402 {
403 /* 0 */ 10000, 10006, 10012, 10017, 10023, 10029, 10035, 10041,
404 /* 8 */ 10046, 10052, 10058, 10064, 10070, 10075, 10081, 10087,
405 /* 16 */ 10093, 10099, 10105, 10110, 10116, 10122, 10128, 10134,
406 /* 24 */ 10140, 10145, 10151, 10157, 10163, 10169, 10175, 10181,
407 /* 32 */ 10187, 10192, 10198, 10204, 10210, 10216, 10222, 10228,
408 /* 40 */ 10234, 10240, 10246, 10251, 10257, 10263, 10269, 10275,
409 /* 48 */ 10281, 10287, 10293, 10299, 10305, 10311, 10317, 10323,
410 /* 56 */ 10329, 10335, 10341, 10347, 10353, 10359, 10365, 10371,
411 /* 64 */ 10377, 10383, 10389, 10395, 10401, 10407, 10413, 10419,
412 /* 72 */ 10425, 10431, 10437, 10443, 10449, 10455, 10461, 10467,
413 /* 80 */ 10473, 10479, 10485, 10491, 10497, 10503, 10509, 10515,
414 /* 88 */ 10521, 10528, 10534, 10540, 10546, 10552, 10558, 10564,
415 /* 96 */ 10570, 10576, 10582, 10589
416 };
417 u_int32_t amount;
418 int negative, semitones, cents, multiplier;
419
420 if (range == 0)
421 return base_freq;
422
423 if (base_freq == 0)
424 return base_freq;
425
426 if (range >= 8192)
427 range = 8192;
428
429 bend = bend * range / 8192;
430 bend += vibrato_cents;
431
432 if (bend == 0)
433 return base_freq;
434
435 if (bend < 0) {
436 bend = -bend;
437 negative = 1;
438 } else
439 negative = 0;
440
441 if (bend > range)
442 bend = range;
443
444 multiplier = 1;
445 while (bend > 2399) {
446 multiplier *= 4;
447 bend -= 2400;
448 }
449
450 semitones = bend / 100;
451 cents = bend % 100;
452
453 amount = semitone_tuning[semitones] * multiplier * cent_tuning[cents]
454 / 10000;
455
456 if (negative)
457 return (base_freq * 10000 / amount); /* Bend down */
458 else
459 return (base_freq * amount / 10000); /* Bend up */
460 }
461
462 static void
463 midisyn_notify(void *cookie, midictl_evt evt,
464 uint_fast8_t chan, uint_fast16_t key)
465 {
466 }
467