midisyn.c revision 1.2 1 1.2 augustss /* $NetBSD: midisyn.c,v 1.2 1998/08/13 00:13:56 augustss 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.1 augustss * Author: Lennart Augustsson
8 1.1 augustss *
9 1.1 augustss * Redistribution and use in source and binary forms, with or without
10 1.1 augustss * modification, are permitted provided that the following conditions
11 1.1 augustss * are met:
12 1.1 augustss * 1. Redistributions of source code must retain the above copyright
13 1.1 augustss * notice, this list of conditions and the following disclaimer.
14 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer in the
16 1.1 augustss * documentation and/or other materials provided with the distribution.
17 1.1 augustss * 3. All advertising materials mentioning features or use of this software
18 1.1 augustss * must display the following acknowledgement:
19 1.1 augustss * This product includes software developed by the NetBSD
20 1.1 augustss * Foundation, Inc. and its contributors.
21 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.1 augustss * contributors may be used to endorse or promote products derived
23 1.1 augustss * from this software without specific prior written permission.
24 1.1 augustss *
25 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
36 1.1 augustss */
37 1.1 augustss
38 1.1 augustss #include <sys/param.h>
39 1.1 augustss #include <sys/ioctl.h>
40 1.1 augustss #include <sys/fcntl.h>
41 1.1 augustss #include <sys/vnode.h>
42 1.1 augustss #include <sys/select.h>
43 1.1 augustss #include <sys/proc.h>
44 1.1 augustss #include <sys/malloc.h>
45 1.1 augustss #include <sys/systm.h>
46 1.1 augustss #include <sys/syslog.h>
47 1.1 augustss #include <sys/kernel.h>
48 1.1 augustss #include <sys/conf.h>
49 1.1 augustss #include <sys/audioio.h>
50 1.1 augustss #include <sys/midiio.h>
51 1.1 augustss #include <sys/device.h>
52 1.1 augustss
53 1.1 augustss #include <dev/audio_if.h>
54 1.1 augustss #include <dev/midivar.h>
55 1.1 augustss #include <dev/midisynvar.h>
56 1.1 augustss
57 1.1 augustss #ifdef AUDIO_DEBUG
58 1.1 augustss #define DPRINTF(x) if (midisyndebug) printf x
59 1.1 augustss #define DPRINTFN(n,x) if (midisyndebug >= (n)) printf x
60 1.1 augustss int midisyndebug = 0;
61 1.1 augustss #else
62 1.1 augustss #define DPRINTF(x)
63 1.1 augustss #define DPRINTFN(n,x)
64 1.1 augustss #endif
65 1.1 augustss
66 1.1 augustss int midisyn_findvoice __P((midisyn *, int, int));
67 1.1 augustss void midisyn_freevoice __P((midisyn *, int));
68 1.1 augustss int midisyn_allocvoice __P((midisyn *, u_int32_t, u_int32_t));
69 1.1 augustss u_int32_t midisyn_note_to_freq __P((int));
70 1.1 augustss
71 1.1 augustss int midisyn_open __P((void *, int,
72 1.1 augustss void (*iintr)__P((void *, int)),
73 1.1 augustss void (*ointr)__P((void *)), void *arg));
74 1.1 augustss void midisyn_close __P((void *));
75 1.1 augustss int midisyn_output __P((void *, int));
76 1.1 augustss void midisyn_getinfo __P((void *, struct midi_info *));
77 1.1 augustss int midisyn_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
78 1.1 augustss
79 1.1 augustss struct midi_hw_if midisyn_hw_if = {
80 1.1 augustss midisyn_open,
81 1.1 augustss midisyn_close,
82 1.1 augustss midisyn_output,
83 1.1 augustss midisyn_getinfo,
84 1.1 augustss midisyn_ioctl,
85 1.1 augustss };
86 1.1 augustss
87 1.1 augustss static int midi_lengths[] = { 3,3,3,3,2,2,3,1 };
88 1.1 augustss /* Number of bytes in a MIDI command, including status */
89 1.1 augustss #define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7])
90 1.1 augustss
91 1.1 augustss int
92 1.1 augustss midisyn_open(addr, flags, iintr, ointr, arg)
93 1.1 augustss void *addr;
94 1.1 augustss int flags;
95 1.1 augustss void (*iintr)__P((void *, int));
96 1.1 augustss void (*ointr)__P((void *));
97 1.1 augustss void *arg;
98 1.1 augustss {
99 1.1 augustss midisyn *ms = addr;
100 1.1 augustss
101 1.1 augustss DPRINTF(("midisyn_open: ms=%p ms->mets=%p\n", ms, ms->mets));
102 1.1 augustss if (ms->mets->open)
103 1.1 augustss return (ms->mets->open(ms, flags));
104 1.1 augustss else
105 1.1 augustss return (0);
106 1.1 augustss }
107 1.1 augustss
108 1.1 augustss void
109 1.1 augustss midisyn_close(addr)
110 1.1 augustss void *addr;
111 1.1 augustss {
112 1.1 augustss midisyn *ms = addr;
113 1.1 augustss
114 1.1 augustss DPRINTF(("midisyn_close: ms=%p ms->mets=%p\n", ms, ms->mets));
115 1.1 augustss if (ms->mets->close)
116 1.1 augustss ms->mets->close(ms);
117 1.1 augustss }
118 1.1 augustss
119 1.1 augustss void
120 1.1 augustss midisyn_getinfo(addr, mi)
121 1.1 augustss void *addr;
122 1.1 augustss struct midi_info *mi;
123 1.1 augustss {
124 1.1 augustss midisyn *ms = addr;
125 1.1 augustss
126 1.1 augustss mi->name = ms->name;
127 1.1 augustss mi->props = 0;
128 1.1 augustss }
129 1.1 augustss
130 1.1 augustss int
131 1.1 augustss midisyn_ioctl(maddr, cmd, addr, flag, p)
132 1.1 augustss void *maddr;
133 1.1 augustss u_long cmd;
134 1.1 augustss caddr_t addr;
135 1.1 augustss int flag;
136 1.1 augustss struct proc *p;
137 1.1 augustss {
138 1.1 augustss midisyn *ms = maddr;
139 1.1 augustss
140 1.1 augustss if (ms->mets->ioctl)
141 1.1 augustss return (ms->mets->ioctl(ms, cmd, addr, flag, p));
142 1.1 augustss else
143 1.1 augustss return (EINVAL);
144 1.1 augustss }
145 1.1 augustss
146 1.1 augustss int
147 1.1 augustss midisyn_findvoice(ms, chan, note)
148 1.1 augustss midisyn *ms;
149 1.1 augustss int chan, note;
150 1.1 augustss {
151 1.1 augustss u_int cn;
152 1.1 augustss int v;
153 1.1 augustss
154 1.1 augustss if (!(ms->flags & MS_DOALLOC))
155 1.1 augustss return (chan);
156 1.1 augustss cn = CHANNOTE(chan, note);
157 1.1 augustss for (v = 0; v < ms->nvoice; v++)
158 1.1 augustss if (ms->voices[v].chan_note == cn)
159 1.1 augustss return (v);
160 1.1 augustss return (-1);
161 1.1 augustss }
162 1.1 augustss
163 1.1 augustss void
164 1.1 augustss midisyn_attach(sc, ms, parent)
165 1.1 augustss struct midi_softc *sc;
166 1.1 augustss midisyn *ms;
167 1.1 augustss struct device *parent;
168 1.1 augustss {
169 1.1 augustss if (ms->flags & MS_DOALLOC) {
170 1.1 augustss ms->voices = malloc(ms->nvoice * sizeof (struct voice),
171 1.1 augustss M_DEVBUF, M_WAITOK);
172 1.1 augustss memset(ms->voices, 0, ms->nvoice * sizeof (struct voice));
173 1.1 augustss ms->seqno = 1;
174 1.1 augustss if (ms->mets->allocv == 0)
175 1.1 augustss ms->mets->allocv = &midisyn_allocvoice;
176 1.1 augustss }
177 1.1 augustss sc->hw_if = &midisyn_hw_if;
178 1.1 augustss sc->hw_hdl = ms;
179 1.1 augustss DPRINTF(("midisyn_attach: ms=%p\n", sc->hw_hdl));
180 1.1 augustss midi_attach(sc, parent);
181 1.1 augustss }
182 1.1 augustss
183 1.1 augustss void
184 1.1 augustss midisyn_freevoice(ms, voice)
185 1.1 augustss midisyn *ms;
186 1.1 augustss int voice;
187 1.1 augustss {
188 1.1 augustss if (!(ms->flags & MS_DOALLOC))
189 1.1 augustss return;
190 1.1 augustss ms->voices[voice].chan_note = ~0;
191 1.1 augustss }
192 1.1 augustss
193 1.1 augustss int
194 1.1 augustss midisyn_allocvoice(ms, chan, note)
195 1.1 augustss midisyn *ms;
196 1.1 augustss u_int32_t chan, note;
197 1.1 augustss {
198 1.1 augustss int bestv, v;
199 1.1 augustss u_int bestseq, s;
200 1.1 augustss
201 1.1 augustss if (!(ms->flags & MS_DOALLOC))
202 1.1 augustss return (chan);
203 1.1 augustss /* Find a free voice, or if no free voice is found the oldest. */
204 1.1 augustss bestv = 0;
205 1.1 augustss bestseq = ms->voices[0].seqno;
206 1.1 augustss for (v = 1; v < ms->nvoice; v++) {
207 1.1 augustss s = ms->voices[v].seqno;
208 1.1 augustss if (s < bestseq) {
209 1.1 augustss bestseq = s;
210 1.1 augustss bestv = v;
211 1.1 augustss }
212 1.1 augustss }
213 1.1 augustss ms->voices[bestv].chan_note = CHANNOTE(chan, note);
214 1.1 augustss ms->voices[bestv].seqno = ms->seqno++;
215 1.1 augustss return (bestv);
216 1.1 augustss }
217 1.1 augustss
218 1.1 augustss int
219 1.1 augustss midisyn_output(addr, b)
220 1.1 augustss void *addr;
221 1.1 augustss int b;
222 1.1 augustss {
223 1.1 augustss midisyn *ms = addr;
224 1.1 augustss u_int8_t status, chan;
225 1.1 augustss int voice = 0; /* initialize to keep gcc quiet */
226 1.1 augustss struct midisyn_methods *fs;
227 1.1 augustss u_int32_t note, vel;
228 1.1 augustss
229 1.2 augustss DPRINTF(("midisyn_output: ms=%p b=0x%02x\n", ms, b));
230 1.1 augustss fs = ms->mets;
231 1.1 augustss if (ms->pos < 0) {
232 1.1 augustss /* Doing SYSEX */
233 1.2 augustss DPRINTF(("midisyn_output: sysex 0x%02x\n", b));
234 1.1 augustss if (fs->sysex)
235 1.1 augustss fs->sysex(ms, b);
236 1.1 augustss if (b == MIDI_SYSEX_END)
237 1.1 augustss ms->pos = 0;
238 1.1 augustss return (0);
239 1.1 augustss }
240 1.1 augustss if (ms->pos == 0 && !MIDI_IS_STATUS(b))
241 1.1 augustss ms->pos++; /* repeat last status byte */
242 1.1 augustss ms->buf[ms->pos++] = b;
243 1.1 augustss status = ms->buf[0];
244 1.1 augustss if (ms->pos < MIDI_LENGTH(status))
245 1.1 augustss return (0);
246 1.1 augustss /* Decode the MIDI command */
247 1.1 augustss chan = MIDI_GET_CHAN(status);
248 1.1 augustss note = ms->buf[1];
249 1.1 augustss if (ms->flags & MS_FREQXLATE)
250 1.1 augustss note = midisyn_note_to_freq(note);
251 1.1 augustss vel = ms->buf[2];
252 1.1 augustss switch (MIDI_GET_STATUS(status)) {
253 1.1 augustss case MIDI_NOTEOFF:
254 1.1 augustss voice = midisyn_findvoice(ms, chan, ms->buf[1]);
255 1.1 augustss if (voice >= 0) {
256 1.1 augustss fs->noteoff(ms, voice, note, vel);
257 1.1 augustss midisyn_freevoice(ms, voice);
258 1.1 augustss }
259 1.1 augustss break;
260 1.1 augustss case MIDI_NOTEON:
261 1.1 augustss voice = fs->allocv(ms, chan, ms->buf[1]);
262 1.1 augustss fs->noteon(ms, voice, note, vel);
263 1.1 augustss break;
264 1.1 augustss case MIDI_KEY_PRESSURE:
265 1.1 augustss if (fs->keypres) {
266 1.1 augustss voice = midisyn_findvoice(ms, voice, ms->buf[1]);
267 1.1 augustss if (voice >= 0)
268 1.1 augustss fs->keypres(ms, voice, note, vel);
269 1.1 augustss }
270 1.1 augustss break;
271 1.1 augustss case MIDI_CTL_CHANGE:
272 1.1 augustss if (fs->ctlchg)
273 1.1 augustss fs->ctlchg(ms, chan, ms->buf[1], vel);
274 1.1 augustss break;
275 1.1 augustss case MIDI_PGM_CHANGE:
276 1.1 augustss if (fs->pgmchg)
277 1.1 augustss fs->pgmchg(ms, chan, ms->buf[1]);
278 1.1 augustss break;
279 1.1 augustss case MIDI_CHN_PRESSURE:
280 1.1 augustss if (fs->chnpres) {
281 1.1 augustss voice = midisyn_findvoice(ms, chan, ms->buf[1]);
282 1.1 augustss if (voice >= 0)
283 1.1 augustss fs->chnpres(ms, voice, note);
284 1.1 augustss }
285 1.1 augustss break;
286 1.1 augustss case MIDI_PITCH_BEND:
287 1.1 augustss if (fs->pitchb) {
288 1.1 augustss voice = midisyn_findvoice(ms, chan, ms->buf[1]);
289 1.1 augustss if (voice >= 0)
290 1.1 augustss fs->pitchb(ms, chan, note, vel);
291 1.1 augustss }
292 1.1 augustss break;
293 1.1 augustss case MIDI_SYSTEM_PREFIX:
294 1.1 augustss if (fs->sysex)
295 1.1 augustss fs->sysex(ms, status);
296 1.1 augustss ms->pos = -1;
297 1.1 augustss return (0);
298 1.1 augustss }
299 1.1 augustss ms->pos = 0;
300 1.1 augustss return (0);
301 1.1 augustss }
302 1.1 augustss
303 1.1 augustss /*
304 1.1 augustss * Convert a MIDI note to the corresponding frequency.
305 1.1 augustss * The frequency is scaled by 2^16.
306 1.1 augustss */
307 1.1 augustss u_int32_t
308 1.1 augustss midisyn_note_to_freq(note)
309 1.1 augustss int note;
310 1.1 augustss {
311 1.1 augustss int o, n, f;
312 1.1 augustss #define BASE_OCTAVE 5
313 1.1 augustss static u_int32_t notes[] = {
314 1.1 augustss 17145893, 18165441, 19245614, 20390018, 21602472, 22887021,
315 1.1 augustss 24247954, 25689813, 27217409, 28835840, 30550508, 32367136
316 1.1 augustss };
317 1.1 augustss
318 1.1 augustss
319 1.1 augustss o = note / 12;
320 1.1 augustss n = note % 12;
321 1.1 augustss
322 1.1 augustss f = notes[n];
323 1.1 augustss
324 1.1 augustss if (o < BASE_OCTAVE)
325 1.1 augustss f >>= (BASE_OCTAVE - o);
326 1.1 augustss else if (o > BASE_OCTAVE)
327 1.1 augustss f <<= (o - BASE_OCTAVE);
328 1.1 augustss return (f);
329 1.1 augustss }
330 1.1 augustss
331