midisyn.c revision 1.17.2.12 1 /* $NetBSD: midisyn.c,v 1.17.2.12 2006/06/07 00:09:39 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.12 2006/06/07 00:09:39 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 voice = midisyn_findvoice(ms, chan, buf[1]);
277 if (voice >= 0) {
278 fs->noteoff(ms, voice, note, vel);
279 midisyn_freevoice(ms, voice);
280 }
281 break;
282 case MIDI_NOTEON:
283 voice = fs->allocv(ms, chan, buf[1]);
284 fs->noteon(ms, voice, note, vel);
285 break;
286 case MIDI_KEY_PRESSURE:
287 if (fs->keypres) {
288 voice = midisyn_findvoice(ms, voice, buf[1]);
289 if (voice >= 0)
290 fs->keypres(ms, voice, note, vel);
291 }
292 break;
293 case MIDI_CTL_CHANGE:
294 midictl_change(&ms->ctl, chan, buf+1);
295 break;
296 case MIDI_PGM_CHANGE:
297 if (fs->pgmchg)
298 fs->pgmchg(ms, chan, buf[1]);
299 break;
300 case MIDI_CHN_PRESSURE:
301 if (fs->chnpres) {
302 voice = midisyn_findvoice(ms, chan, buf[1]);
303 if (voice >= 0) /* XXX should do all voices on chan */
304 fs->chnpres(ms, voice, note);
305 }
306 break;
307 case MIDI_PITCH_BEND:
308 if (fs->pitchb) {
309 voice = midisyn_findvoice(ms, chan, buf[1]);
310 if (voice >= 0) /* XXX buf1-2 are a 14-bit bend value */
311 fs->pitchb(ms, chan, note, vel);
312 }
313 break;
314 }
315 return 0;
316 }
317
318 int midisyn_commonmsg(void *addr, int status, u_char *buf, int len)
319 {
320 return 0;
321 }
322
323 int midisyn_sysex(void *addr, u_char *buf, int len)
324 {
325 midisyn *ms = addr;
326 if (ms->mets->sysex) {
327 while ( len --> 0 )
328 ms->mets->sysex(ms, *buf++);
329 }
330 return 0;
331 }
332
333 /*
334 * Convert a MIDI note to the corresponding frequency.
335 * The frequency is scaled by 2^16.
336 */
337 u_int32_t
338 midisyn_note_to_freq(int note)
339 {
340 int o, n, f;
341 #define BASE_OCTAVE 5
342 static const u_int32_t notes[] = {
343 17145893, 18165441, 19245614, 20390018, 21602472, 22887021,
344 24247954, 25689813, 27217409, 28835840, 30550508, 32367136
345 };
346
347
348 o = note / 12;
349 n = note % 12;
350
351 f = notes[n];
352
353 if (o < BASE_OCTAVE)
354 f >>= (BASE_OCTAVE - o);
355 else if (o > BASE_OCTAVE)
356 f <<= (o - BASE_OCTAVE);
357 return (f);
358 }
359
360 u_int32_t
361 midisyn_finetune(u_int32_t base_freq, int bend, int range, int vibrato_cents)
362 {
363 static const u_int16_t semitone_tuning[24] =
364 {
365 /* 0 */ 10000, 10595, 11225, 11892, 12599, 13348, 14142, 14983,
366 /* 8 */ 15874, 16818, 17818, 18877, 20000, 21189, 22449, 23784,
367 /* 16 */ 25198, 26697, 28284, 29966, 31748, 33636, 35636, 37755
368 };
369 static const u_int16_t cent_tuning[100] =
370 {
371 /* 0 */ 10000, 10006, 10012, 10017, 10023, 10029, 10035, 10041,
372 /* 8 */ 10046, 10052, 10058, 10064, 10070, 10075, 10081, 10087,
373 /* 16 */ 10093, 10099, 10105, 10110, 10116, 10122, 10128, 10134,
374 /* 24 */ 10140, 10145, 10151, 10157, 10163, 10169, 10175, 10181,
375 /* 32 */ 10187, 10192, 10198, 10204, 10210, 10216, 10222, 10228,
376 /* 40 */ 10234, 10240, 10246, 10251, 10257, 10263, 10269, 10275,
377 /* 48 */ 10281, 10287, 10293, 10299, 10305, 10311, 10317, 10323,
378 /* 56 */ 10329, 10335, 10341, 10347, 10353, 10359, 10365, 10371,
379 /* 64 */ 10377, 10383, 10389, 10395, 10401, 10407, 10413, 10419,
380 /* 72 */ 10425, 10431, 10437, 10443, 10449, 10455, 10461, 10467,
381 /* 80 */ 10473, 10479, 10485, 10491, 10497, 10503, 10509, 10515,
382 /* 88 */ 10521, 10528, 10534, 10540, 10546, 10552, 10558, 10564,
383 /* 96 */ 10570, 10576, 10582, 10589
384 };
385 u_int32_t amount;
386 int negative, semitones, cents, multiplier;
387
388 if (range == 0)
389 return base_freq;
390
391 if (base_freq == 0)
392 return base_freq;
393
394 if (range >= 8192)
395 range = 8192;
396
397 bend = bend * range / 8192;
398 bend += vibrato_cents;
399
400 if (bend == 0)
401 return base_freq;
402
403 if (bend < 0) {
404 bend = -bend;
405 negative = 1;
406 } else
407 negative = 0;
408
409 if (bend > range)
410 bend = range;
411
412 multiplier = 1;
413 while (bend > 2399) {
414 multiplier *= 4;
415 bend -= 2400;
416 }
417
418 semitones = bend / 100;
419 cents = bend % 100;
420
421 amount = semitone_tuning[semitones] * multiplier * cent_tuning[cents]
422 / 10000;
423
424 if (negative)
425 return (base_freq * 10000 / amount); /* Bend down */
426 else
427 return (base_freq * amount / 10000); /* Bend up */
428 }
429
430 static void
431 midisyn_notify(void *cookie, midictl_evt evt,
432 uint_fast8_t chan, uint_fast16_t key)
433 {
434 }
435