23 static int sequencer_ok;
25 static int tmr_no = -1;
26 static int pending_timer = -1;
29 static int obsolete_api_used;
36 static int max_mididev;
37 static int max_synthdev;
47 static int seq_mode =
SEQ_1;
56 static unsigned long prev_input_time;
57 static int prev_event_time;
64 static unsigned char *
queue;
65 static unsigned char *iqueue;
67 static volatile int qhead, qtail, qlen;
68 static volatile int iqhead, iqtail, iqlen;
69 static volatile int seq_playing;
70 static volatile int sequencer_busy;
72 static long pre_event_timeout;
73 static unsigned synth_open_mask;
75 static int seq_queue(
unsigned char *note,
char nonblock);
76 static void seq_startplay(
void);
77 static int seq_sync(
void);
78 static void seq_reset(
void);
80 #if MAX_SYNTH_DEV > 15
81 #error Too many synthesizer devices enabled.
92 ev_len = seq_mode ==
SEQ_1 ? 4 : 8;
98 spin_unlock_irqrestore(&
lock,flags);
108 spin_unlock_irqrestore(&
lock,flags);
112 while (iqlen && c >= ev_len)
114 char *fixit = (
char *) &iqueue[iqhead *
IEV_SZ];
115 spin_unlock_irqrestore(&
lock,flags);
125 spin_unlock_irqrestore(&
lock,flags);
129 static void sequencer_midi_output(
int dev)
144 if (len != 4 && len != 8)
146 if ((seq_mode ==
SEQ_1) != (len == 4))
157 spin_unlock_irqrestore(&
lock,flags);
161 static void sequencer_midi_input(
int dev,
unsigned char data)
164 unsigned char event_rec[4];
171 if (tstamp != prev_input_time)
187 unsigned long this_time;
189 if (seq_mode ==
SEQ_2)
194 if (this_time != prev_input_time)
196 unsigned char tmp_event[8];
202 *(
unsigned int *) &tmp_event[4] = this_time;
205 prev_input_time = this_time;
213 unsigned char event_rec[
EV_SZ], ev_code;
214 int p = 0,
c, ev_size;
215 int mode = translate_mode(file);
219 DEB(
printk(
"sequencer_write(dev=%d, count=%d)\n", dev, count));
230 ev_code = event_rec[0];
236 dev = *(
unsigned short *) &event_rec[2];
240 if (!(synth_open_mask & (1 << dev)))
243 fmt = (*(
short *) &event_rec[0]) & 0xffff;
272 if (seq_mode ==
SEQ_2)
280 obsolete_api_used = 1;
285 if (!midi_opened[event_rec[2]])
288 int dev = event_rec[2];
295 mode = translate_mode(file);
298 sequencer_midi_input, sequencer_midi_output)) < 0)
304 midi_opened[
dev] = 1;
309 int processed = count -
c;
329 static int seq_queue(
unsigned char *note,
char nonblock)
342 if (!nonblock && qlen >=
SEQ_MAX_QUEUE && !waitqueue_active(&seq_sleeper)) {
362 static int extended_event(
unsigned char *
q)
366 if (dev < 0 || dev >= max_synthdev)
369 if (!(synth_open_mask & (1 << dev)))
379 if (q[4] > 127 && q[4] != 255)
403 synth_devs[
dev]->controller(dev, q[3], q[4], (
short) (q[5] | (q[6] << 8)));
407 if (synth_devs[dev]->volume_method !=
NULL)
417 static int find_voice(
int dev,
int chn,
int note)
422 key = (chn << 8) | (note + 1);
424 if (synth_devs[dev]->alloc.map[i] == key)
429 static int alloc_voice(
int dev,
int chn,
int note)
434 key = (chn << 8) | (note + 1);
437 &synth_devs[dev]->alloc);
444 static void seq_chn_voice_event(
unsigned char *event_rec)
446 #define dev event_rec[1]
447 #define cmd event_rec[2]
448 #define chn event_rec[3]
449 #define note event_rec[4]
450 #define parm event_rec[5]
454 if ((
int) dev > max_synthdev || synth_devs[dev] ==
NULL)
456 if (!(synth_open_mask & (1 << dev)))
458 if (!synth_devs[dev])
461 if (seq_mode ==
SEQ_2)
463 if (synth_devs[dev]->alloc_voice)
464 voice = find_voice(dev, chn, note);
476 if (note > 127 && note != 255)
479 if (voice == -1 && seq_mode ==
SEQ_2 && synth_devs[dev]->alloc_voice)
482 voice = alloc_voice(dev, chn, note);
526 static void seq_chn_common_event(
unsigned char *event_rec)
528 unsigned char dev = event_rec[1];
529 unsigned char cmd = event_rec[2];
530 unsigned char chn = event_rec[3];
531 unsigned char p1 = event_rec[4];
534 unsigned short w14 = *(
short *) &event_rec[6];
538 if (!(synth_open_mask & (1 << dev)))
540 if (!synth_devs[dev])
546 if (seq_mode ==
SEQ_2)
550 synth_devs[dev]->set_instr(dev, chn, p1);
558 if (seq_mode ==
SEQ_2)
560 if (chn > 15 || p1 > 127)
570 int val = w14 & 0x7f;
576 chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
578 chn_info[chn].controllers[p1 | 32] & 0x7f);
583 key = ((
int) chn << 8);
586 if ((synth_devs[dev]->alloc.map[i] & 0xff00) ==
key)
597 if (seq_mode ==
SEQ_2)
609 if ((synth_devs[dev]->alloc.map[i] & 0xff00) ==
key)
610 synth_devs[dev]->bender(dev, i, w14);
623 static int seq_timing_event(
unsigned char *event_rec)
625 unsigned char cmd = event_rec[1];
626 unsigned int parm = *(
int *) &event_rec[4];
628 if (seq_mode ==
SEQ_2)
640 parm += prev_event_time;
653 prev_event_time =
time;
680 if (seq_mode ==
SEQ_2)
695 static void seq_local_event(
unsigned char *event_rec)
697 unsigned char cmd = event_rec[1];
698 unsigned int parm = *((
unsigned int *) &event_rec[4]);
710 static void seq_sysex_message(
unsigned char *event_rec)
712 unsigned int dev = event_rec[1];
714 unsigned char *
buf = &event_rec[2];
716 if (dev > max_synthdev)
718 if (!(synth_open_mask & (1 << dev)))
720 if (!synth_devs[dev])
724 for (i = 0; i < 6 && buf[
i] != 0xff; i++)
727 if (!synth_devs[dev]->send_sysex)
733 static int play_event(
unsigned char *q)
746 if (synth_open_mask & (1 << 0))
752 if (q[4] < 128 || q[4] == 255)
753 if (synth_open_mask & (1 << 0))
755 synth_devs[0]->start_note(0, q[1], q[2], q[3]);
759 delay = (
unsigned int *) q;
763 *delay = (*delay >> 8) & 0xffffff;
771 prev_event_time =
time;
786 if (synth_open_mask & (1 << 0))
802 if (midi_opened[q[2]])
811 if (!midi_devs[dev]->outputc(dev, q[1]))
822 midi_written[
dev] = 1;
833 if ((
int) q[1] < max_synthdev)
842 seq_chn_voice_event(q);
846 seq_chn_common_event(q);
861 seq_sysex_message(q);
870 static void seq_startplay(
void)
881 spin_unlock_irqrestore(&lock,flags);
885 if ((action = play_event(&
queue[this_one *
EV_SZ])))
902 static void reset_controllers(
int dev,
unsigned char *
controller,
int update_dev)
905 for (i = 0; i < 128; i++)
906 controller[i] = ctrl_def_values[i];
909 static void setup_mode2(
void)
917 if (midi_devs[dev] && midi_devs[dev]->converter !=
NULL)
923 for (dev = 0; dev < max_synthdev; dev++)
930 for (chn = 0; chn < 16; chn++)
933 reset_controllers(dev,
934 synth_devs[dev]->chn_info[chn].controllers,0);
954 mode = translate_mode(file);
956 DEB(
printk(
"sequencer_open(dev=%d)\n", dev));
967 request_module(
"synth0");
983 obsolete_api_used = 0;
990 if (pending_timer != -1)
992 tmr_no = pending_timer;
1021 if (!max_synthdev && !max_mididev)
1027 synth_open_mask = 0;
1029 for (i = 0; i < max_mididev; i++)
1032 midi_written[
i] = 0;
1035 for (i = 0; i < max_synthdev; i++)
1045 printk(
KERN_WARNING "Sequencer: Warning! Cannot open synth device #%d (%d)\n", i, tmp);
1051 synth_open_mask |= (1 <<
i);
1059 prev_input_time = 0;
1060 prev_event_time = 0;
1068 for (i = 0; i < max_mididev; i++)
1075 sequencer_midi_input, sequencer_midi_output)) >= 0)
1082 if (seq_mode ==
SEQ_2) {
1083 if (try_module_get(tmr->
owner))
1084 tmr->
open(tmr_no, seq_mode);
1094 static void seq_drain_midi_queues(
void)
1104 while (!signal_pending(
current) && n)
1108 for (i = 0; i < max_mididev; i++)
1109 if (midi_opened[i] && midi_written[i])
1110 if (midi_devs[i]->buffer_status !=
NULL)
1111 if (midi_devs[i]->buffer_status(i))
1127 int mode = translate_mode(file);
1131 DEB(
printk(
"sequencer_release(dev=%d)\n", dev));
1139 while (!signal_pending(
current) && qlen > 0)
1149 seq_drain_midi_queues();
1154 seq_drain_midi_queues();
1158 for (i = 0; i < max_synthdev; i++)
1160 if (synth_open_mask & (1 << i))
1174 for (i = 0; i < max_mididev; i++)
1176 if (midi_opened[i]) {
1182 if (seq_mode ==
SEQ_2) {
1184 module_put(tmr->
owner);
1187 if (obsolete_api_used)
1192 static int seq_sync(
void)
1194 if (qlen && !seq_playing && !signal_pending(
current))
1202 static void midi_outc(
int dev,
unsigned char data)
1209 unsigned long flags;
1220 while (n && !midi_devs[dev]->outputc(dev, data)) {
1224 spin_unlock_irqrestore(&lock,flags);
1227 static void seq_reset(
void)
1235 unsigned long flags;
1240 prev_input_time = 0;
1241 prev_event_time = 0;
1243 qlen = qhead = qtail = 0;
1244 iqlen = iqhead = iqtail = 0;
1246 for (i = 0; i < max_synthdev; i++)
1247 if (synth_open_mask & (1 << i))
1251 if (seq_mode ==
SEQ_2)
1253 for (chn = 0; chn < 16; chn++)
1254 for (i = 0; i < max_synthdev; i++)
1255 if (synth_open_mask & (1 << i))
1265 for (i = 0; i < max_mididev; i++)
1266 if (midi_written[i])
1277 for (chn = 0; chn < 16; chn++)
1279 midi_outc(i, (
unsigned char) (0xb0 + (chn & 0x0f)));
1286 midi_written[
i] = 0;
1295 if (waitqueue_active(&seq_sleeper)) {
1299 spin_unlock_irqrestore(&lock,flags);
1302 static void seq_panic(
void)
1325 int midi_dev, orig_dev,
val,
err;
1326 int mode = translate_mode(file);
1329 unsigned long flags;
1332 orig_dev = dev = dev >> 4;
1343 if (seq_mode !=
SEQ_2)
1345 return tmr->
ioctl(tmr_no, cmd, arg);
1348 if (seq_mode !=
SEQ_2)
1357 val = pending_timer;
1367 while (qlen > 0 && !signal_pending(
current))
1369 return qlen ? -
EINTR : 0;
1378 if (midi_dev < 0 || midi_dev >= max_mididev || !
midi_devs[midi_dev])
1381 if (!midi_opened[midi_dev] &&
1382 (err =
midi_devs[midi_dev]->
open(midi_dev, mode, sequencer_midi_input,
1383 sequencer_midi_output)) < 0)
1385 midi_opened[midi_dev] = 1;
1401 if (seq_mode ==
SEQ_2)
1402 return tmr->
ioctl(tmr_no, cmd, arg);
1410 if (seq_mode ==
SEQ_2)
1411 return tmr->
ioctl(tmr_no, cmd, arg);
1427 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1444 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1454 if (!(synth_open_mask & (1 << dev)))
1462 if (dev < 0 || dev >= max_synthdev)
1464 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1472 if (dev < 0 || dev >= max_synthdev)
1474 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1485 play_event(event_rec.
arr);
1486 spin_unlock_irqrestore(&
lock,flags);
1492 if (dev < 0 || dev >= max_mididev || !
midi_devs[dev])
1512 val = (
HZ *
val) / 10;
1513 pre_event_timeout =
val;
1521 if (!(synth_open_mask & (1 << 0)))
1533 unsigned long flags;
1534 unsigned int mask = 0;
1540 poll_wait(file, &midi_sleeper, wait);
1545 poll_wait(file, &seq_sleeper, wait);
1548 spin_unlock_irqrestore(&
lock,flags);
1566 int note, octave, note_freq;
1567 static int notes[] =
1569 261632, 277189, 293671, 311132, 329632, 349232,
1570 369998, 391998, 415306, 440000, 466162, 493880
1573 #define BASE_OCTAVE 5
1575 octave = note_num / 12;
1576 note = note_num % 12;
1578 note_freq = notes[note];
1596 unsigned long amount;
1597 int negative, semitones, cents, multiplier = 1;
1610 bend = bend * range / 8192;
1611 bend += vibrato_cents;
1616 negative = bend < 0 ? 1 : 0;
1633 semitones = bend / 100;
1636 amount = (
int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents]) / 10000;
1639 return (base_freq * 10000) / amount;
1641 return (base_freq * amount) / 10000;
1652 printk(
KERN_ERR "sequencer: Can't allocate memory for sequencer output queue\n");
1658 printk(
KERN_ERR "sequencer: Can't allocate memory for sequencer input queue\n");