4 * High level midi sequencer manager for dumb MIDI interfaces.
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
14 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
15 * Andrew Veliath : fixed running status in MIDI input state machine
17 #define USE_SEQ_MACROS
18 #define USE_SIMPLE_MACROS
20 #include "sound_config.h"
22 #define _MIDI_SYNTH_C_
24 #include "midi_synth.h"
26 static int midi2synth[MAX_MIDI_DEV];
27 static int sysex_state[MAX_MIDI_DEV] =
29 static unsigned char prev_out_status[MAX_MIDI_DEV];
34 unsigned char obuf[8]; \
36 seq_input_event(obuf, len); \
41 #define _SEQ_ADVBUF(x) len=x
44 do_midi_msg(int synthno, unsigned char *msg, int mlen)
46 switch (msg[0] & 0xf0)
51 STORE(SEQ_START_NOTE(synthno, msg[0] & 0x0f, msg[1], msg[2]));
57 STORE(SEQ_STOP_NOTE(synthno, msg[0] & 0x0f, msg[1], msg[2]));
61 STORE(SEQ_KEY_PRESSURE(synthno, msg[0] & 0x0f, msg[1], msg[2]));
65 STORE(SEQ_CONTROL(synthno, msg[0] & 0x0f,
70 STORE(SEQ_SET_PATCH(synthno, msg[0] & 0x0f, msg[1]));
74 STORE(SEQ_CHN_PRESSURE(synthno, msg[0] & 0x0f, msg[1]));
78 STORE(SEQ_BENDER(synthno, msg[0] & 0x0f,
79 (msg[1] & 0x7f) | ((msg[2] & 0x7f) << 7)));
83 /* printk( "MPU: Unknown midi channel message %02x\n", msg[0]); */
89 midi_outc(int midi_dev, int data)
93 for (timeout = 0; timeout < 3200; timeout++)
94 if (midi_devs[midi_dev]->outputc(midi_dev, (unsigned char) (data & 0xff)))
99 prev_out_status[midi_dev] =
100 (unsigned char) (data & 0xff); /*
101 * Store for running status
108 * Sorry! No space on buffers.
110 printk("Midi send timed out\n");
114 prefix_cmd(int midi_dev, unsigned char status)
116 if ((char *) midi_devs[midi_dev]->prefix_cmd == NULL)
119 return midi_devs[midi_dev]->prefix_cmd(midi_dev, status);
123 midi_synth_input(int orig_dev, unsigned char data)
126 struct midi_input_info *inc;
128 static unsigned char len_tab[] = /* # of data bytes following a status
141 if (orig_dev < 0 || orig_dev > num_midis || midi_devs[orig_dev] == NULL)
144 if (data == 0xfe) /* Ignore active sensing */
147 dev = midi2synth[orig_dev];
148 inc = &midi_devs[orig_dev]->in_info;
150 switch (inc->m_state)
153 if (data & 0x80) /* MIDI status byte */
155 if ((data & 0xf0) == 0xf0) /* Common message */
159 case 0xf0: /* Sysex */
160 inc->m_state = MST_SYSEX;
163 case 0xf1: /* MTC quarter frame */
164 case 0xf3: /* Song select */
165 inc->m_state = MST_DATA;
168 inc->m_buf[0] = data;
171 case 0xf2: /* Song position pointer */
172 inc->m_state = MST_DATA;
175 inc->m_buf[0] = data;
179 inc->m_buf[0] = data;
181 do_midi_msg(dev, inc->m_buf, inc->m_ptr);
187 inc->m_state = MST_DATA;
189 inc->m_left = len_tab[(data >> 4) - 8];
190 inc->m_buf[0] = inc->m_prev_status = data;
192 } else if (inc->m_prev_status & 0x80) {
193 /* Data byte (use running status) */
195 inc->m_buf[1] = data;
196 inc->m_buf[0] = inc->m_prev_status;
197 inc->m_left = len_tab[(inc->m_buf[0] >> 4) - 8] - 1;
199 inc->m_state = MST_DATA; /* Not done yet */
201 inc->m_state = MST_INIT;
202 do_midi_msg(dev, inc->m_buf, inc->m_ptr);
206 break; /* MST_INIT */
209 inc->m_buf[inc->m_ptr++] = data;
210 if (--inc->m_left <= 0)
212 inc->m_state = MST_INIT;
213 do_midi_msg(dev, inc->m_buf, inc->m_ptr);
216 break; /* MST_DATA */
219 if (data == 0xf7) /* Sysex end */
221 inc->m_state = MST_INIT;
225 break; /* MST_SYSEX */
228 printk("MIDI%d: Unexpected state %d (%02x)\n", orig_dev, inc->m_state, (int) data);
229 inc->m_state = MST_INIT;
236 int orig_dev = synth_devs[dev]->midi_dev;
239 if (!sysex_state[dev])
242 sysex_state[dev] = 0;
244 while (!midi_devs[orig_dev]->outputc(orig_dev, 0xf7) &&
248 sysex_state[dev] = 0;
252 midi_synth_output(int dev)
259 int midi_synth_ioctl(int dev, unsigned int cmd, void __user *arg)
262 * int orig_dev = synth_devs[dev]->midi_dev;
267 case SNDCTL_SYNTH_INFO:
268 if (__copy_to_user(arg, synth_devs[dev]->info, sizeof(struct synth_info)))
272 case SNDCTL_SYNTH_MEMAVL:
281 midi_synth_kill_note(int dev, int channel, int note, int velocity)
283 int orig_dev = synth_devs[dev]->midi_dev;
286 if (note < 0 || note > 127)
288 if (channel < 0 || channel > 15)
297 msg = prev_out_status[orig_dev] & 0xf0;
298 chn = prev_out_status[orig_dev] & 0x0f;
300 if (chn == channel && ((msg == 0x90 && velocity == 64) || msg == 0x80))
304 if (!prefix_cmd(orig_dev, note))
307 midi_outc(orig_dev, note);
310 * Running status = Note on
312 midi_outc(orig_dev, 0); /*
313 * Note on with velocity 0 == note
317 midi_outc(orig_dev, velocity);
322 if (!prefix_cmd(orig_dev, 0x90 | (channel & 0x0f)))
324 midi_outc(orig_dev, 0x90 | (channel & 0x0f)); /*
327 midi_outc(orig_dev, note);
328 midi_outc(orig_dev, 0); /*
333 if (!prefix_cmd(orig_dev, 0x80 | (channel & 0x0f)))
335 midi_outc(orig_dev, 0x80 | (channel & 0x0f)); /*
338 midi_outc(orig_dev, note);
339 midi_outc(orig_dev, velocity);
347 midi_synth_set_instr(int dev, int channel, int instr_no)
349 int orig_dev = synth_devs[dev]->midi_dev;
351 if (instr_no < 0 || instr_no > 127)
353 if (channel < 0 || channel > 15)
358 if (!prefix_cmd(orig_dev, 0xc0 | (channel & 0x0f)))
360 midi_outc(orig_dev, 0xc0 | (channel & 0x0f)); /*
363 midi_outc(orig_dev, instr_no);
369 midi_synth_start_note(int dev, int channel, int note, int velocity)
371 int orig_dev = synth_devs[dev]->midi_dev;
374 if (note < 0 || note > 127)
376 if (channel < 0 || channel > 15)
385 msg = prev_out_status[orig_dev] & 0xf0;
386 chn = prev_out_status[orig_dev] & 0x0f;
388 if (chn == channel && msg == 0x90)
392 if (!prefix_cmd(orig_dev, note))
394 midi_outc(orig_dev, note);
395 midi_outc(orig_dev, velocity);
398 if (!prefix_cmd(orig_dev, 0x90 | (channel & 0x0f)))
400 midi_outc(orig_dev, 0x90 | (channel & 0x0f)); /*
403 midi_outc(orig_dev, note);
404 midi_outc(orig_dev, velocity);
410 midi_synth_reset(int dev)
417 midi_synth_open(int dev, int mode)
419 int orig_dev = synth_devs[dev]->midi_dev;
421 struct midi_input_info *inc;
423 if (orig_dev < 0 || orig_dev > num_midis || midi_devs[orig_dev] == NULL)
426 midi2synth[orig_dev] = dev;
427 sysex_state[dev] = 0;
428 prev_out_status[orig_dev] = 0;
430 if ((err = midi_devs[orig_dev]->open(orig_dev, mode,
431 midi_synth_input, midi_synth_output)) < 0)
433 inc = &midi_devs[orig_dev]->in_info;
435 /* save_flags(flags);
437 don't know against what irqhandler to protect*/
439 inc->m_state = MST_INIT;
442 inc->m_prev_status = 0x00;
443 /* restore_flags(flags); */
449 midi_synth_close(int dev)
451 int orig_dev = synth_devs[dev]->midi_dev;
456 * Shut up the synths by sending just single active sensing message.
458 midi_devs[orig_dev]->outputc(orig_dev, 0xfe);
460 midi_devs[orig_dev]->close(orig_dev);
464 midi_synth_hw_control(int dev, unsigned char *event)
469 midi_synth_load_patch(int dev, int format, const char __user *addr,
470 int offs, int count, int pmgr_flag)
472 int orig_dev = synth_devs[dev]->midi_dev;
474 struct sysex_info sysex;
476 unsigned long left, src_offs, eox_seen = 0;
478 int hdr_size = (unsigned long) &sysex.data[0] - (unsigned long) &sysex;
482 if (!prefix_cmd(orig_dev, 0xf0))
485 if (format != SYSEX_PATCH)
487 /* printk("MIDI Error: Invalid patch format (key) 0x%x\n", format);*/
490 if (count < hdr_size)
492 /* printk("MIDI Error: Patch header too short\n");*/
498 * Copy the header from user space but ignore the first bytes which have
499 * been transferred already.
502 if(copy_from_user(&((char *) &sysex)[offs], &(addr)[offs], hdr_size - offs))
505 if (count < sysex.len)
507 /* printk(KERN_WARNING "MIDI Warning: Sysex record too short (%d<%d)\n", count, (int) sysex.len);*/
513 for (i = 0; i < left && !signal_pending(current); i++)
517 get_user(*(unsigned char *) &data, (unsigned char __user *) &((addr)[hdr_size + i]));
519 eox_seen = (i > 0 && data & 0x80); /* End of sysex */
521 if (eox_seen && data != 0xf7)
528 printk(KERN_WARNING "midi_synth: Sysex start missing\n");
532 while (!midi_devs[orig_dev]->outputc(orig_dev, (unsigned char) (data & 0xff)) &&
533 !signal_pending(current))
536 if (!first_byte && data & 0x80)
542 midi_outc(orig_dev, 0xf7);
546 void midi_synth_panning(int dev, int channel, int pressure)
550 void midi_synth_aftertouch(int dev, int channel, int pressure)
552 int orig_dev = synth_devs[dev]->midi_dev;
555 if (pressure < 0 || pressure > 127)
557 if (channel < 0 || channel > 15)
562 msg = prev_out_status[orig_dev] & 0xf0;
563 chn = prev_out_status[orig_dev] & 0x0f;
565 if (msg != 0xd0 || chn != channel) /*
566 * Test for running status
569 if (!prefix_cmd(orig_dev, 0xd0 | (channel & 0x0f)))
571 midi_outc(orig_dev, 0xd0 | (channel & 0x0f)); /*
574 } else if (!prefix_cmd(orig_dev, pressure))
577 midi_outc(orig_dev, pressure);
581 midi_synth_controller(int dev, int channel, int ctrl_num, int value)
583 int orig_dev = synth_devs[dev]->midi_dev;
586 if (ctrl_num < 0 || ctrl_num > 127)
588 if (channel < 0 || channel > 15)
593 msg = prev_out_status[orig_dev] & 0xf0;
594 chn = prev_out_status[orig_dev] & 0x0f;
596 if (msg != 0xb0 || chn != channel)
598 if (!prefix_cmd(orig_dev, 0xb0 | (channel & 0x0f)))
600 midi_outc(orig_dev, 0xb0 | (channel & 0x0f));
601 } else if (!prefix_cmd(orig_dev, ctrl_num))
604 midi_outc(orig_dev, ctrl_num);
605 midi_outc(orig_dev, value & 0x7f);
609 midi_synth_bender(int dev, int channel, int value)
611 int orig_dev = synth_devs[dev]->midi_dev;
614 if (channel < 0 || channel > 15)
617 if (value < 0 || value > 16383)
622 msg = prev_out_status[orig_dev] & 0xf0;
623 prev_chn = prev_out_status[orig_dev] & 0x0f;
625 if (msg != 0xd0 || prev_chn != channel) /*
626 * Test for running status
629 if (!prefix_cmd(orig_dev, 0xe0 | (channel & 0x0f)))
631 midi_outc(orig_dev, 0xe0 | (channel & 0x0f));
632 } else if (!prefix_cmd(orig_dev, value & 0x7f))
635 midi_outc(orig_dev, value & 0x7f);
636 midi_outc(orig_dev, (value >> 7) & 0x7f);
640 midi_synth_setup_voice(int dev, int voice, int channel)
645 midi_synth_send_sysex(int dev, unsigned char *bytes, int len)
647 int orig_dev = synth_devs[dev]->midi_dev;
650 for (i = 0; i < len; i++)
654 case 0xf0: /* Start sysex */
655 if (!prefix_cmd(orig_dev, 0xf0))
657 sysex_state[dev] = 1;
660 case 0xf7: /* End sysex */
661 if (!sysex_state[dev]) /* Orphan sysex end */
663 sysex_state[dev] = 0;
667 if (!sysex_state[dev])
670 if (bytes[i] & 0x80) /* Error. Another message before sysex end */
672 bytes[i] = 0xf7; /* Sysex end */
673 sysex_state[dev] = 0;
677 if (!midi_devs[orig_dev]->outputc(orig_dev, bytes[i]))
680 * Hardware level buffer is full. Abort the sysex message.
686 sysex_state[dev] = 0;
688 while (!midi_devs[orig_dev]->outputc(orig_dev, bytes[i]) &&
692 if (!sysex_state[dev])