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 #include <linux/config.h>
19 #define USE_SEQ_MACROS
20 #define USE_SIMPLE_MACROS
22 #include "sound_config.h"
24 #define _MIDI_SYNTH_C_
26 #include "midi_synth.h"
28 static int midi2synth
[MAX_MIDI_DEV
];
29 static int sysex_state
[MAX_MIDI_DEV
] =
31 static unsigned char prev_out_status
[MAX_MIDI_DEV
];
36 unsigned char obuf[8]; \
38 seq_input_event(obuf, len); \
43 #define _SEQ_ADVBUF(x) len=x
46 do_midi_msg(int synthno
, unsigned char *msg
, int mlen
)
48 switch (msg
[0] & 0xf0)
53 STORE(SEQ_START_NOTE(synthno
, msg
[0] & 0x0f, msg
[1], msg
[2]));
59 STORE(SEQ_STOP_NOTE(synthno
, msg
[0] & 0x0f, msg
[1], msg
[2]));
63 STORE(SEQ_KEY_PRESSURE(synthno
, msg
[0] & 0x0f, msg
[1], msg
[2]));
67 STORE(SEQ_CONTROL(synthno
, msg
[0] & 0x0f,
72 STORE(SEQ_SET_PATCH(synthno
, msg
[0] & 0x0f, msg
[1]));
76 STORE(SEQ_CHN_PRESSURE(synthno
, msg
[0] & 0x0f, msg
[1]));
80 STORE(SEQ_BENDER(synthno
, msg
[0] & 0x0f,
81 (msg
[1] & 0x7f) | ((msg
[2] & 0x7f) << 7)));
85 /* printk( "MPU: Unknown midi channel message %02x\n", msg[0]); */
91 midi_outc(int midi_dev
, int data
)
95 for (timeout
= 0; timeout
< 3200; timeout
++)
96 if (midi_devs
[midi_dev
]->outputc(midi_dev
, (unsigned char) (data
& 0xff)))
101 prev_out_status
[midi_dev
] =
102 (unsigned char) (data
& 0xff); /*
103 * Store for running status
110 * Sorry! No space on buffers.
112 printk("Midi send timed out\n");
116 prefix_cmd(int midi_dev
, unsigned char status
)
118 if ((char *) midi_devs
[midi_dev
]->prefix_cmd
== NULL
)
121 return midi_devs
[midi_dev
]->prefix_cmd(midi_dev
, status
);
125 midi_synth_input(int orig_dev
, unsigned char data
)
128 struct midi_input_info
*inc
;
130 static unsigned char len_tab
[] = /* # of data bytes following a status
143 if (orig_dev
< 0 || orig_dev
> num_midis
|| midi_devs
[orig_dev
] == NULL
)
146 if (data
== 0xfe) /* Ignore active sensing */
149 dev
= midi2synth
[orig_dev
];
150 inc
= &midi_devs
[orig_dev
]->in_info
;
152 switch (inc
->m_state
)
155 if (data
& 0x80) /* MIDI status byte */
157 if ((data
& 0xf0) == 0xf0) /* Common message */
161 case 0xf0: /* Sysex */
162 inc
->m_state
= MST_SYSEX
;
165 case 0xf1: /* MTC quarter frame */
166 case 0xf3: /* Song select */
167 inc
->m_state
= MST_DATA
;
170 inc
->m_buf
[0] = data
;
173 case 0xf2: /* Song position pointer */
174 inc
->m_state
= MST_DATA
;
177 inc
->m_buf
[0] = data
;
181 inc
->m_buf
[0] = data
;
183 do_midi_msg(dev
, inc
->m_buf
, inc
->m_ptr
);
189 inc
->m_state
= MST_DATA
;
191 inc
->m_left
= len_tab
[(data
>> 4) - 8];
192 inc
->m_buf
[0] = inc
->m_prev_status
= data
;
194 } else if (inc
->m_prev_status
& 0x80) {
195 /* Data byte (use running status) */
197 inc
->m_buf
[1] = data
;
198 inc
->m_buf
[0] = inc
->m_prev_status
;
199 inc
->m_left
= len_tab
[(inc
->m_buf
[0] >> 4) - 8] - 1;
201 inc
->m_state
= MST_DATA
; /* Not done yet */
203 inc
->m_state
= MST_INIT
;
204 do_midi_msg(dev
, inc
->m_buf
, inc
->m_ptr
);
208 break; /* MST_INIT */
211 inc
->m_buf
[inc
->m_ptr
++] = data
;
212 if (--inc
->m_left
<= 0)
214 inc
->m_state
= MST_INIT
;
215 do_midi_msg(dev
, inc
->m_buf
, inc
->m_ptr
);
218 break; /* MST_DATA */
221 if (data
== 0xf7) /* Sysex end */
223 inc
->m_state
= MST_INIT
;
227 break; /* MST_SYSEX */
230 printk("MIDI%d: Unexpected state %d (%02x)\n", orig_dev
, inc
->m_state
, (int) data
);
231 inc
->m_state
= MST_INIT
;
238 int orig_dev
= synth_devs
[dev
]->midi_dev
;
241 if (!sysex_state
[dev
])
244 sysex_state
[dev
] = 0;
246 while (!midi_devs
[orig_dev
]->outputc(orig_dev
, 0xf7) &&
250 sysex_state
[dev
] = 0;
254 midi_synth_output(int dev
)
261 int midi_synth_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
264 * int orig_dev = synth_devs[dev]->midi_dev;
269 case SNDCTL_SYNTH_INFO
:
270 if (__copy_to_user(arg
, synth_devs
[dev
]->info
, sizeof(struct synth_info
)))
274 case SNDCTL_SYNTH_MEMAVL
:
283 midi_synth_kill_note(int dev
, int channel
, int note
, int velocity
)
285 int orig_dev
= synth_devs
[dev
]->midi_dev
;
288 if (note
< 0 || note
> 127)
290 if (channel
< 0 || channel
> 15)
299 msg
= prev_out_status
[orig_dev
] & 0xf0;
300 chn
= prev_out_status
[orig_dev
] & 0x0f;
302 if (chn
== channel
&& ((msg
== 0x90 && velocity
== 64) || msg
== 0x80))
306 if (!prefix_cmd(orig_dev
, note
))
309 midi_outc(orig_dev
, note
);
312 * Running status = Note on
314 midi_outc(orig_dev
, 0); /*
315 * Note on with velocity 0 == note
319 midi_outc(orig_dev
, velocity
);
324 if (!prefix_cmd(orig_dev
, 0x90 | (channel
& 0x0f)))
326 midi_outc(orig_dev
, 0x90 | (channel
& 0x0f)); /*
329 midi_outc(orig_dev
, note
);
330 midi_outc(orig_dev
, 0); /*
335 if (!prefix_cmd(orig_dev
, 0x80 | (channel
& 0x0f)))
337 midi_outc(orig_dev
, 0x80 | (channel
& 0x0f)); /*
340 midi_outc(orig_dev
, note
);
341 midi_outc(orig_dev
, velocity
);
349 midi_synth_set_instr(int dev
, int channel
, int instr_no
)
351 int orig_dev
= synth_devs
[dev
]->midi_dev
;
353 if (instr_no
< 0 || instr_no
> 127)
355 if (channel
< 0 || channel
> 15)
360 if (!prefix_cmd(orig_dev
, 0xc0 | (channel
& 0x0f)))
362 midi_outc(orig_dev
, 0xc0 | (channel
& 0x0f)); /*
365 midi_outc(orig_dev
, instr_no
);
371 midi_synth_start_note(int dev
, int channel
, int note
, int velocity
)
373 int orig_dev
= synth_devs
[dev
]->midi_dev
;
376 if (note
< 0 || note
> 127)
378 if (channel
< 0 || channel
> 15)
387 msg
= prev_out_status
[orig_dev
] & 0xf0;
388 chn
= prev_out_status
[orig_dev
] & 0x0f;
390 if (chn
== channel
&& msg
== 0x90)
394 if (!prefix_cmd(orig_dev
, note
))
396 midi_outc(orig_dev
, note
);
397 midi_outc(orig_dev
, velocity
);
400 if (!prefix_cmd(orig_dev
, 0x90 | (channel
& 0x0f)))
402 midi_outc(orig_dev
, 0x90 | (channel
& 0x0f)); /*
405 midi_outc(orig_dev
, note
);
406 midi_outc(orig_dev
, velocity
);
412 midi_synth_reset(int dev
)
419 midi_synth_open(int dev
, int mode
)
421 int orig_dev
= synth_devs
[dev
]->midi_dev
;
424 struct midi_input_info
*inc
;
426 if (orig_dev
< 0 || orig_dev
> num_midis
|| midi_devs
[orig_dev
] == NULL
)
429 midi2synth
[orig_dev
] = dev
;
430 sysex_state
[dev
] = 0;
431 prev_out_status
[orig_dev
] = 0;
433 if ((err
= midi_devs
[orig_dev
]->open(orig_dev
, mode
,
434 midi_synth_input
, midi_synth_output
)) < 0)
439 inc
= &midi_devs
[orig_dev
]->in_info
;
444 inc
->m_state
= MST_INIT
;
447 inc
->m_prev_status
= 0x00;
448 restore_flags(flags
);
454 midi_synth_close(int dev
)
456 int orig_dev
= synth_devs
[dev
]->midi_dev
;
461 * Shut up the synths by sending just single active sensing message.
463 midi_devs
[orig_dev
]->outputc(orig_dev
, 0xfe);
465 midi_devs
[orig_dev
]->close(orig_dev
);
472 midi_synth_hw_control(int dev
, unsigned char *event
)
477 midi_synth_load_patch(int dev
, int format
, const char *addr
,
478 int offs
, int count
, int pmgr_flag
)
480 int orig_dev
= synth_devs
[dev
]->midi_dev
;
482 struct sysex_info sysex
;
484 unsigned long left
, src_offs
, eox_seen
= 0;
486 int hdr_size
= (unsigned long) &sysex
.data
[0] - (unsigned long) &sysex
;
490 if (!prefix_cmd(orig_dev
, 0xf0))
493 if (format
!= SYSEX_PATCH
)
495 /* printk("MIDI Error: Invalid patch format (key) 0x%x\n", format);*/
498 if (count
< hdr_size
)
500 /* printk("MIDI Error: Patch header too short\n");*/
506 * Copy the header from user space but ignore the first bytes which have
507 * been transferred already.
510 if(copy_from_user(&((char *) &sysex
)[offs
], &(addr
)[offs
], hdr_size
- offs
))
513 if (count
< sysex
.len
)
515 /* printk(KERN_WARNING "MIDI Warning: Sysex record too short (%d<%d)\n", count, (int) sysex.len);*/
521 for (i
= 0; i
< left
&& !signal_pending(current
); i
++)
525 get_user(*(unsigned char *) &data
, (unsigned char *) &((addr
)[hdr_size
+ i
]));
527 eox_seen
= (i
> 0 && data
& 0x80); /* End of sysex */
529 if (eox_seen
&& data
!= 0xf7)
536 printk(KERN_WARNING
"midi_synth: Sysex start missing\n");
540 while (!midi_devs
[orig_dev
]->outputc(orig_dev
, (unsigned char) (data
& 0xff)) &&
541 !signal_pending(current
))
544 if (!first_byte
&& data
& 0x80)
550 midi_outc(orig_dev
, 0xf7);
554 void midi_synth_panning(int dev
, int channel
, int pressure
)
558 void midi_synth_aftertouch(int dev
, int channel
, int pressure
)
560 int orig_dev
= synth_devs
[dev
]->midi_dev
;
563 if (pressure
< 0 || pressure
> 127)
565 if (channel
< 0 || channel
> 15)
570 msg
= prev_out_status
[orig_dev
] & 0xf0;
571 chn
= prev_out_status
[orig_dev
] & 0x0f;
573 if (msg
!= 0xd0 || chn
!= channel
) /*
574 * Test for running status
577 if (!prefix_cmd(orig_dev
, 0xd0 | (channel
& 0x0f)))
579 midi_outc(orig_dev
, 0xd0 | (channel
& 0x0f)); /*
582 } else if (!prefix_cmd(orig_dev
, pressure
))
585 midi_outc(orig_dev
, pressure
);
589 midi_synth_controller(int dev
, int channel
, int ctrl_num
, int value
)
591 int orig_dev
= synth_devs
[dev
]->midi_dev
;
594 if (ctrl_num
< 0 || ctrl_num
> 127)
596 if (channel
< 0 || channel
> 15)
601 msg
= prev_out_status
[orig_dev
] & 0xf0;
602 chn
= prev_out_status
[orig_dev
] & 0x0f;
604 if (msg
!= 0xb0 || chn
!= channel
)
606 if (!prefix_cmd(orig_dev
, 0xb0 | (channel
& 0x0f)))
608 midi_outc(orig_dev
, 0xb0 | (channel
& 0x0f));
609 } else if (!prefix_cmd(orig_dev
, ctrl_num
))
612 midi_outc(orig_dev
, ctrl_num
);
613 midi_outc(orig_dev
, value
& 0x7f);
617 midi_synth_bender(int dev
, int channel
, int value
)
619 int orig_dev
= synth_devs
[dev
]->midi_dev
;
622 if (channel
< 0 || channel
> 15)
625 if (value
< 0 || value
> 16383)
630 msg
= prev_out_status
[orig_dev
] & 0xf0;
631 prev_chn
= prev_out_status
[orig_dev
] & 0x0f;
633 if (msg
!= 0xd0 || prev_chn
!= channel
) /*
634 * Test for running status
637 if (!prefix_cmd(orig_dev
, 0xe0 | (channel
& 0x0f)))
639 midi_outc(orig_dev
, 0xe0 | (channel
& 0x0f));
640 } else if (!prefix_cmd(orig_dev
, value
& 0x7f))
643 midi_outc(orig_dev
, value
& 0x7f);
644 midi_outc(orig_dev
, (value
>> 7) & 0x7f);
648 midi_synth_setup_voice(int dev
, int voice
, int channel
)
653 midi_synth_send_sysex(int dev
, unsigned char *bytes
, int len
)
655 int orig_dev
= synth_devs
[dev
]->midi_dev
;
658 for (i
= 0; i
< len
; i
++)
662 case 0xf0: /* Start sysex */
663 if (!prefix_cmd(orig_dev
, 0xf0))
665 sysex_state
[dev
] = 1;
668 case 0xf7: /* End sysex */
669 if (!sysex_state
[dev
]) /* Orphan sysex end */
671 sysex_state
[dev
] = 0;
675 if (!sysex_state
[dev
])
678 if (bytes
[i
] & 0x80) /* Error. Another message before sysex end */
680 bytes
[i
] = 0xf7; /* Sysex end */
681 sysex_state
[dev
] = 0;
685 if (!midi_devs
[orig_dev
]->outputc(orig_dev
, bytes
[i
]))
688 * Hardware level buffer is full. Abort the sysex message.
694 sysex_state
[dev
] = 0;
696 while (!midi_devs
[orig_dev
]->outputc(orig_dev
, bytes
[i
]) &&
700 if (!sysex_state
[dev
])