1 /* $OpenBSD: midisyn.c,v 1.4 2003/02/24 23:57:11 tedu Exp $ */
2 /* $NetBSD: midisyn.c,v 1.5 1998/11/25 22:17:07 augustss Exp $ */
3
4 /*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (augustss@netbsd.org).
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/param.h>
41 #include <sys/ioctl.h>
42 #include <sys/fcntl.h>
43 #include <sys/vnode.h>
44 #include <sys/select.h>
45 #include <sys/proc.h>
46 #include <sys/malloc.h>
47 #include <sys/systm.h>
48 #include <sys/syslog.h>
49 #include <sys/kernel.h>
50 #include <sys/conf.h>
51 #include <sys/audioio.h>
52 #include <sys/midiio.h>
53 #include <sys/device.h>
54
55 #include <dev/audio_if.h>
56 #include <dev/midi_if.h>
57 #include <dev/midivar.h>
58 #include <dev/midisynvar.h>
59
60 #ifdef AUDIO_DEBUG
61 #define DPRINTF(x) if (midisyndebug) printf x
62 #define DPRINTFN(n,x) if (midisyndebug >= (n)) printf x
63 int midisyndebug = 0;
64 #else
65 #define DPRINTF(x)
66 #define DPRINTFN(n,x)
67 #endif
68
69 int midisyn_findvoice(midisyn *, int, int);
70 void midisyn_freevoice(midisyn *, int);
71 int midisyn_allocvoice(midisyn *, u_int32_t, u_int32_t);
72 u_int32_t midisyn_note_to_freq(int);
73 u_int32_t midisyn_finetune(u_int32_t, int, int, int);
74
75 int midisyn_open(void *, int,
76 void (*iintr)(void *, int),
77 void (*ointr)(void *), void *arg);
78 void midisyn_close(void *);
79 int midisyn_output(void *, int);
80 void midisyn_getinfo(void *, struct midi_info *);
81 int midisyn_ioctl(void *, u_long, caddr_t, int, struct proc *);
82
83 struct midi_hw_if midisyn_hw_if = {
84 midisyn_open,
85 midisyn_close,
86 midisyn_output,
87 midisyn_getinfo,
88 midisyn_ioctl,
89 };
90
91 static int midi_lengths[] = { 3,3,3,3,2,2,3,1 };
92 /* Number of bytes in a MIDI command, including status */
93 #define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7])
94
95 int
midisyn_open(addr,flags,iintr,ointr,arg)96 midisyn_open(addr, flags, iintr, ointr, arg)
97 void *addr;
98 int flags;
99 void (*iintr)(void *, int);
100 void (*ointr)(void *);
101 void *arg;
102 {
103 midisyn *ms = addr;
104
105 DPRINTF(("midisyn_open: ms=%p ms->mets=%p\n", ms, ms->mets));
106 if (ms->mets->open)
107 return (ms->mets->open(ms, flags));
108 else
109 return (0);
110 }
111
112 void
midisyn_close(addr)113 midisyn_close(addr)
114 void *addr;
115 {
116 midisyn *ms = addr;
117 struct midisyn_methods *fs;
118 int v;
119
120 DPRINTF(("midisyn_close: ms=%p ms->mets=%p\n", ms, ms->mets));
121 fs = ms->mets;
122 for (v = 0; v < ms->nvoice; v++)
123 if (ms->voices[v].inuse) {
124 fs->noteoff(ms, v, 0, 0);
125 midisyn_freevoice(ms, v);
126 }
127 if (fs->close)
128 fs->close(ms);
129 }
130
131 void
midisyn_getinfo(addr,mi)132 midisyn_getinfo(addr, mi)
133 void *addr;
134 struct midi_info *mi;
135 {
136 midisyn *ms = addr;
137
138 mi->name = ms->name;
139 mi->props = 0;
140 }
141
142 int
midisyn_ioctl(maddr,cmd,addr,flag,p)143 midisyn_ioctl(maddr, cmd, addr, flag, p)
144 void *maddr;
145 u_long cmd;
146 caddr_t addr;
147 int flag;
148 struct proc *p;
149 {
150 midisyn *ms = maddr;
151
152 if (ms->mets->ioctl)
153 return (ms->mets->ioctl(ms, cmd, addr, flag, p));
154 else
155 return (EINVAL);
156 }
157
158 int
midisyn_findvoice(ms,chan,note)159 midisyn_findvoice(ms, chan, note)
160 midisyn *ms;
161 int chan, note;
162 {
163 u_int cn;
164 int v;
165
166 if (!(ms->flags & MS_DOALLOC))
167 return (chan);
168 cn = MS_CHANNOTE(chan, note);
169 for (v = 0; v < ms->nvoice; v++)
170 if (ms->voices[v].chan_note == cn && ms->voices[v].inuse)
171 return (v);
172 return (-1);
173 }
174
175 void
midisyn_attach(sc,ms)176 midisyn_attach(sc, ms)
177 struct midi_softc *sc;
178 midisyn *ms;
179 {
180 if (ms->flags & MS_DOALLOC) {
181 ms->voices = malloc(ms->nvoice * sizeof (struct voice),
182 M_DEVBUF, M_WAITOK);
183 memset(ms->voices, 0, ms->nvoice * sizeof (struct voice));
184 ms->seqno = 1;
185 if (ms->mets->allocv == 0)
186 ms->mets->allocv = &midisyn_allocvoice;
187 }
188 sc->hw_if = &midisyn_hw_if;
189 sc->hw_hdl = ms;
190 DPRINTF(("midisyn_attach: ms=%p\n", sc->hw_hdl));
191 }
192
193 void
midisyn_freevoice(ms,voice)194 midisyn_freevoice(ms, voice)
195 midisyn *ms;
196 int voice;
197 {
198 if (!(ms->flags & MS_DOALLOC))
199 return;
200 ms->voices[voice].inuse = 0;
201 }
202
203 int
midisyn_allocvoice(ms,chan,note)204 midisyn_allocvoice(ms, chan, note)
205 midisyn *ms;
206 u_int32_t chan, note;
207 {
208 int bestv, v;
209 u_int bestseq, s;
210
211 if (!(ms->flags & MS_DOALLOC))
212 return (chan);
213 /* Find a free voice, or if no free voice is found the oldest. */
214 bestv = 0;
215 bestseq = ms->voices[0].seqno + (ms->voices[0].inuse ? 0x40000000 : 0);
216 for (v = 1; v < ms->nvoice; v++) {
217 s = ms->voices[v].seqno;
218 if (ms->voices[v].inuse)
219 s += 0x40000000;
220 if (s < bestseq) {
221 bestseq = s;
222 bestv = v;
223 }
224 }
225 DPRINTFN(10,("midisyn_allocvoice: v=%d seq=%d cn=%x inuse=%d\n",
226 bestv, ms->voices[bestv].seqno,
227 ms->voices[bestv].chan_note,
228 ms->voices[bestv].inuse));
229 #ifdef AUDIO_DEBUG
230 if (ms->voices[bestv].inuse)
231 DPRINTFN(1,("midisyn_allocvoice: steal %x\n",
232 ms->voices[bestv].chan_note));
233 #endif
234 ms->voices[bestv].chan_note = MS_CHANNOTE(chan, note);
235 ms->voices[bestv].seqno = ms->seqno++;
236 ms->voices[bestv].inuse = 1;
237 return (bestv);
238 }
239
240 int
midisyn_output(addr,b)241 midisyn_output(addr, b)
242 void *addr;
243 int b;
244 {
245 midisyn *ms = addr;
246 u_int8_t status, chan;
247 int voice = 0; /* initialize to keep gcc quiet */
248 struct midisyn_methods *fs;
249 u_int32_t note, vel;
250
251 DPRINTF(("midisyn_output: ms=%p b=0x%02x\n", ms, b));
252 fs = ms->mets;
253 if (ms->pos < 0) {
254 /* Doing SYSEX */
255 DPRINTF(("midisyn_output: sysex 0x%02x\n", b));
256 if (fs->sysex)
257 fs->sysex(ms, b);
258 if (b == MIDI_SYSEX_END)
259 ms->pos = 0;
260 return (0);
261 }
262 if (ms->pos == 0 && !MIDI_IS_STATUS(b))
263 ms->pos++; /* repeat last status byte */
264 ms->buf[ms->pos++] = b;
265 status = ms->buf[0];
266 if (ms->pos < MIDI_LENGTH(status))
267 return (0);
268 /* Decode the MIDI command */
269 chan = MIDI_GET_CHAN(status);
270 note = ms->buf[1];
271 if (ms->flags & MS_FREQXLATE)
272 note = midisyn_note_to_freq(note);
273 vel = ms->buf[2];
274 switch (MIDI_GET_STATUS(status)) {
275 case MIDI_NOTEOFF:
276 voice = midisyn_findvoice(ms, chan, ms->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, ms->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, ms->buf[1]);
289 if (voice >= 0)
290 fs->keypres(ms, voice, note, vel);
291 }
292 break;
293 case MIDI_CTL_CHANGE:
294 if (fs->ctlchg)
295 fs->ctlchg(ms, chan, ms->buf[1], vel);
296 break;
297 case MIDI_PGM_CHANGE:
298 if (fs->pgmchg)
299 fs->pgmchg(ms, chan, ms->buf[1]);
300 break;
301 case MIDI_CHN_PRESSURE:
302 if (fs->chnpres) {
303 voice = midisyn_findvoice(ms, chan, ms->buf[1]);
304 if (voice >= 0)
305 fs->chnpres(ms, voice, note);
306 }
307 break;
308 case MIDI_PITCH_BEND:
309 if (fs->pitchb) {
310 voice = midisyn_findvoice(ms, chan, ms->buf[1]);
311 if (voice >= 0)
312 fs->pitchb(ms, chan, note, vel);
313 }
314 break;
315 case MIDI_SYSTEM_PREFIX:
316 if (fs->sysex)
317 fs->sysex(ms, status);
318 ms->pos = -1;
319 return (0);
320 }
321 ms->pos = 0;
322 return (0);
323 }
324
325 /*
326 * Convert a MIDI note to the corresponding frequency.
327 * The frequency is scaled by 2^16.
328 */
329 u_int32_t
midisyn_note_to_freq(note)330 midisyn_note_to_freq(note)
331 int note;
332 {
333 int o, n, f;
334 #define BASE_OCTAVE 5
335 static u_int32_t notes[] = {
336 17145893, 18165441, 19245614, 20390018, 21602472, 22887021,
337 24247954, 25689813, 27217409, 28835840, 30550508, 32367136
338 };
339
340
341 o = note / 12;
342 n = note % 12;
343
344 f = notes[n];
345
346 if (o < BASE_OCTAVE)
347 f >>= (BASE_OCTAVE - o);
348 else if (o > BASE_OCTAVE)
349 f <<= (o - BASE_OCTAVE);
350 return (f);
351 }
352
353 u_int32_t
midisyn_finetune(base_freq,bend,range,vibrato_cents)354 midisyn_finetune(base_freq, bend, range, vibrato_cents)
355 u_int32_t base_freq;
356 int bend;
357 int range;
358 int vibrato_cents;
359 {
360 static u_int16_t semitone_tuning[24] =
361 {
362 /* 0 */ 10000, 10595, 11225, 11892, 12599, 13348, 14142, 14983,
363 /* 8 */ 15874, 16818, 17818, 18877, 20000, 21189, 22449, 23784,
364 /* 16 */ 25198, 26697, 28284, 29966, 31748, 33636, 35636, 37755
365 };
366 static u_int16_t cent_tuning[100] =
367 {
368 /* 0 */ 10000, 10006, 10012, 10017, 10023, 10029, 10035, 10041,
369 /* 8 */ 10046, 10052, 10058, 10064, 10070, 10075, 10081, 10087,
370 /* 16 */ 10093, 10099, 10105, 10110, 10116, 10122, 10128, 10134,
371 /* 24 */ 10140, 10145, 10151, 10157, 10163, 10169, 10175, 10181,
372 /* 32 */ 10187, 10192, 10198, 10204, 10210, 10216, 10222, 10228,
373 /* 40 */ 10234, 10240, 10246, 10251, 10257, 10263, 10269, 10275,
374 /* 48 */ 10281, 10287, 10293, 10299, 10305, 10311, 10317, 10323,
375 /* 56 */ 10329, 10335, 10341, 10347, 10353, 10359, 10365, 10371,
376 /* 64 */ 10377, 10383, 10389, 10395, 10401, 10407, 10413, 10419,
377 /* 72 */ 10425, 10431, 10437, 10443, 10449, 10455, 10461, 10467,
378 /* 80 */ 10473, 10479, 10485, 10491, 10497, 10503, 10509, 10515,
379 /* 88 */ 10521, 10528, 10534, 10540, 10546, 10552, 10558, 10564,
380 /* 96 */ 10570, 10576, 10582, 10589
381 };
382 u_int32_t amount;
383 int negative, semitones, cents, multiplier;
384
385 if (range == 0)
386 return base_freq;
387
388 if (base_freq == 0)
389 return base_freq;
390
391 if (range >= 8192)
392 range = 8192;
393
394 bend = bend * range / 8192;
395 bend += vibrato_cents;
396
397 if (bend == 0)
398 return base_freq;
399
400 if (bend < 0) {
401 bend = -bend;
402 negative = 1;
403 } else
404 negative = 0;
405
406 if (bend > range)
407 bend = range;
408
409 multiplier = 1;
410 while (bend > 2399) {
411 multiplier *= 4;
412 bend -= 2400;
413 }
414
415 semitones = bend / 100;
416 if (semitones > 23)
417 semitones = 23;
418 cents = bend % 100;
419
420 amount = semitone_tuning[semitones] * multiplier * cent_tuning[cents]
421 / 10000;
422
423 if (negative)
424 return (base_freq * 10000 / amount); /* Bend down */
425 else
426 return (base_freq * amount / 10000); /* Bend up */
427 }
428
429