2 * OSS compatible sequencer driver
4 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "seq_oss_device.h"
22 #include "seq_oss_synth.h"
23 #include "seq_oss_midi.h"
24 #include "seq_oss_event.h"
25 #include "seq_oss_timer.h"
26 #include <sound/seq_oss_legacy.h>
27 #include "seq_oss_readq.h"
28 #include "seq_oss_writeq.h"
29 #include <linux/nospec.h>
35 static int extended_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev);
36 static int chn_voice_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev);
37 static int chn_common_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev);
38 static int timing_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev);
39 static int local_event(struct seq_oss_devinfo *dp, union evrec *event_rec, struct snd_seq_event *ev);
40 static int old_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev);
41 static int note_on_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev);
42 static int note_off_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev);
43 static int set_note_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int note, int vel, struct snd_seq_event *ev);
44 static int set_control_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int param, int val, struct snd_seq_event *ev);
45 static int set_echo_event(struct seq_oss_devinfo *dp, union evrec *rec, struct snd_seq_event *ev);
49 * convert an OSS event to ALSA event
51 * non-zero : invalid - ignored
55 snd_seq_oss_process_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
59 return extended_event(dp, q, ev);
62 return chn_voice_event(dp, q, ev);
65 return chn_common_event(dp, q, ev);
68 return timing_event(dp, q, ev);
71 return local_event(dp, q, ev);
74 return snd_seq_oss_synth_sysex(dp, q->x.dev, q->x.buf, ev);
77 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
80 if (! is_write_mode(dp->file_mode))
82 if (snd_seq_oss_midi_open(dp, q->s.dev, SNDRV_SEQ_OSS_FILE_WRITE))
84 if (snd_seq_oss_midi_filemode(dp, q->s.dev) & SNDRV_SEQ_OSS_FILE_WRITE)
85 return snd_seq_oss_midi_putc(dp, q->s.dev, q->s.parm1, ev);
89 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
91 return set_echo_event(dp, q, ev);
94 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
96 return snd_seq_oss_synth_raw_event(dp, q->c[1], q->c, ev);
99 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
101 return old_event(dp, q, ev);
106 /* old type events: mode1 only */
108 old_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
112 return note_off_event(dp, 0, q->n.chn, q->n.note, q->n.vel, ev);
115 return note_on_event(dp, 0, q->n.chn, q->n.note, q->n.vel, ev);
122 return set_control_event(dp, 0, SNDRV_SEQ_EVENT_PGMCHANGE,
123 q->n.chn, 0, q->n.note, ev);
126 return snd_seq_oss_timer_reset(dp->timer);
132 /* 8bytes extended event: mode1 only */
134 extended_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
140 return note_off_event(dp, q->e.dev, q->e.chn, q->e.p1, q->e.p2, ev);
143 return note_on_event(dp, q->e.dev, q->e.chn, q->e.p1, q->e.p2, ev);
146 return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_PGMCHANGE,
147 q->e.chn, 0, q->e.p1, ev);
150 return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_CHANPRESS,
151 q->e.chn, 0, q->e.p1, ev);
154 /* convert -128:127 to 0:127 */
156 val = (val + 128) / 2;
157 return set_control_event(dp, q->e.dev, SNDRV_SEQ_EVENT_CONTROLLER,
158 q->e.chn, CTL_PAN, val, ev);
161 val = ((short)q->e.p3 << 8) | (short)q->e.p2;
163 case CTRL_PITCH_BENDER: /* SEQ1 V2 control */
165 return set_control_event(dp, q->e.dev,
166 SNDRV_SEQ_EVENT_PITCHBEND,
167 q->e.chn, 0, val, ev);
168 case CTRL_PITCH_BENDER_RANGE:
169 /* conversion: 100/semitone -> 128/semitone */
170 return set_control_event(dp, q->e.dev,
171 SNDRV_SEQ_EVENT_REGPARAM,
172 q->e.chn, 0, val*128/100, ev);
174 return set_control_event(dp, q->e.dev,
175 SNDRV_SEQ_EVENT_CONTROL14,
176 q->e.chn, q->e.p1, val, ev);
180 return snd_seq_oss_synth_raw_event(dp, q->e.dev, q->c, ev);
186 /* channel voice events: mode1 and 2 */
188 chn_voice_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
194 return note_on_event(dp, q->v.dev, q->v.chn, q->v.note, q->v.parm, ev);
197 return note_off_event(dp, q->v.dev, q->v.chn, q->v.note, q->v.parm, ev);
199 case MIDI_KEY_PRESSURE:
200 return set_note_event(dp, q->v.dev, SNDRV_SEQ_EVENT_KEYPRESS,
201 q->v.chn, q->v.note, q->v.parm, ev);
207 /* channel common events: mode1 and 2 */
209 chn_common_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
214 case MIDI_PGM_CHANGE:
215 return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_PGMCHANGE,
216 q->l.chn, 0, q->l.p1, ev);
218 case MIDI_CTL_CHANGE:
219 return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_CONTROLLER,
220 q->l.chn, q->l.p1, q->l.val, ev);
222 case MIDI_PITCH_BEND:
223 /* conversion: 0:0x3fff -> -0x2000:0x1fff */
224 return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_PITCHBEND,
225 q->l.chn, 0, q->l.val - 8192, ev);
227 case MIDI_CHN_PRESSURE:
228 return set_control_event(dp, q->l.dev, SNDRV_SEQ_EVENT_CHANPRESS,
229 q->l.chn, 0, q->l.val, ev);
234 /* timer events: mode1 and mode2 */
236 timing_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
240 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
241 return set_echo_event(dp, q, ev);
244 memset(&tmp, 0, sizeof(tmp));
245 /* XXX: only for little-endian! */
246 tmp.echo = (q->t.time << 8) | SEQ_ECHO;
247 return set_echo_event(dp, &tmp, ev);
252 return snd_seq_oss_timer_stop(dp->timer);
257 return snd_seq_oss_timer_continue(dp->timer);
262 return snd_seq_oss_timer_tempo(dp->timer, q->t.time);
269 /* local events: mode1 and 2 */
271 local_event(struct seq_oss_devinfo *dp, union evrec *q, struct snd_seq_event *ev)
277 * process note-on event for OSS synth
278 * three different modes are available:
279 * - SNDRV_SEQ_OSS_PROCESS_EVENTS (for one-voice per channel mode)
280 * Accept note 255 as volume change.
281 * - SNDRV_SEQ_OSS_PASS_EVENTS
282 * Pass all events to lowlevel driver anyway
283 * - SNDRV_SEQ_OSS_PROCESS_KEYPRESS (mostly for Emu8000)
284 * Use key-pressure if note >= 128
287 note_on_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev)
289 struct seq_oss_synthinfo *info;
291 info = snd_seq_oss_synth_info(dp, dev);
295 switch (info->arg.event_passing) {
296 case SNDRV_SEQ_OSS_PROCESS_EVENTS:
297 if (! info->ch || ch < 0 || ch >= info->nr_voices) {
299 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
302 ch = array_index_nospec(ch, info->nr_voices);
303 if (note == 255 && info->ch[ch].note >= 0) {
307 /* set volume to zero -- note off */
308 // type = SNDRV_SEQ_EVENT_NOTEOFF;
310 if (info->ch[ch].vel)
311 /* sample already started -- volume change */
312 type = SNDRV_SEQ_EVENT_KEYPRESS;
314 /* sample not started -- start now */
315 type = SNDRV_SEQ_EVENT_NOTEON;
316 info->ch[ch].vel = vel;
317 return set_note_event(dp, dev, type, ch, info->ch[ch].note, vel, ev);
318 } else if (note >= 128)
319 return -EINVAL; /* invalid */
321 if (note != info->ch[ch].note && info->ch[ch].note >= 0)
322 /* note changed - note off at beginning */
323 set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, info->ch[ch].note, 0, ev);
324 /* set current status */
325 info->ch[ch].note = note;
326 info->ch[ch].vel = vel;
327 if (vel) /* non-zero velocity - start the note now */
328 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
331 case SNDRV_SEQ_OSS_PASS_EVENTS:
332 /* pass the event anyway */
333 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
335 case SNDRV_SEQ_OSS_PROCESS_KEYPRESS:
336 if (note >= 128) /* key pressure: shifted by 128 */
337 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_KEYPRESS, ch, note - 128, vel, ev);
338 else /* normal note-on event */
339 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
345 * process note-off event for OSS synth
348 note_off_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, struct snd_seq_event *ev)
350 struct seq_oss_synthinfo *info;
352 info = snd_seq_oss_synth_info(dp, dev);
356 switch (info->arg.event_passing) {
357 case SNDRV_SEQ_OSS_PROCESS_EVENTS:
358 if (! info->ch || ch < 0 || ch >= info->nr_voices) {
360 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEON, ch, note, vel, ev);
363 ch = array_index_nospec(ch, info->nr_voices);
364 if (info->ch[ch].note >= 0) {
365 note = info->ch[ch].note;
366 info->ch[ch].vel = 0;
367 info->ch[ch].note = -1;
368 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, note, vel, ev);
370 return -EINVAL; /* invalid */
372 case SNDRV_SEQ_OSS_PASS_EVENTS:
373 case SNDRV_SEQ_OSS_PROCESS_KEYPRESS:
374 /* pass the event anyway */
375 return set_note_event(dp, dev, SNDRV_SEQ_EVENT_NOTEOFF, ch, note, vel, ev);
382 * create a note event
385 set_note_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int note, int vel, struct snd_seq_event *ev)
387 if (!snd_seq_oss_synth_info(dp, dev))
391 snd_seq_oss_synth_addr(dp, dev, ev);
392 ev->data.note.channel = ch;
393 ev->data.note.note = note;
394 ev->data.note.velocity = vel;
400 * create a control event
403 set_control_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int param, int val, struct snd_seq_event *ev)
405 if (!snd_seq_oss_synth_info(dp, dev))
409 snd_seq_oss_synth_addr(dp, dev, ev);
410 ev->data.control.channel = ch;
411 ev->data.control.param = param;
412 ev->data.control.value = val;
418 * create an echo event
421 set_echo_event(struct seq_oss_devinfo *dp, union evrec *rec, struct snd_seq_event *ev)
423 ev->type = SNDRV_SEQ_EVENT_ECHO;
424 /* echo back to itself */
425 snd_seq_oss_fill_addr(dp, ev, dp->addr.client, dp->addr.port);
426 memcpy(&ev->data, rec, LONG_EVENT_SIZE);
431 * event input callback from ALSA sequencer:
432 * the echo event is processed here.
435 snd_seq_oss_event_input(struct snd_seq_event *ev, int direct, void *private_data,
438 struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data;
441 if (ev->type != SNDRV_SEQ_EVENT_ECHO)
442 return snd_seq_oss_midi_input(ev, direct, private_data);
444 if (ev->source.client != dp->cseq)
445 return 0; /* ignored */
447 rec = (union evrec*)&ev->data;
448 if (rec->s.code == SEQ_SYNCTIMER) {
450 snd_seq_oss_writeq_wakeup(dp->writeq, rec->t.time);
453 /* echo back event */
454 if (dp->readq == NULL)
456 snd_seq_oss_readq_put_event(dp->readq, rec);