4 * The sequencer personality manager.
6 * Copyright by Hannu Savolainen 1993
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 2.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/i386/isa/sound/sequencer.c,v 1.25.2.1 2001/06/10 02:02:05 dd Exp $
29 * $DragonFly: src/sys/dev/sound/isa/i386/Attic/sequencer.c,v 1.2 2003/06/17 04:28:38 dillon Exp $
33 #include <i386/isa/sound/sound_config.h>
37 #ifdef CONFIG_SEQUENCER
39 #include <sys/select.h>
41 #include <i386/isa/sound/midi_ctrl.h>
43 static void seq_drain_midi_queues(void);
45 sequencer_poll (int dev, struct fileinfo *file, int events, select_table * wait);
46 static int sequencer_ok = 0;
47 static struct sound_timer_operations *tmr;
48 static int tmr_no = -1; /* Currently selected timer */
49 static int pending_timer = -1; /* For timer change operation */
52 * Local counts for number of synth and MIDI devices. These are initialized
53 * by the sequencer_open.
55 static int max_mididev = 0;
56 static int max_synthdev = 0;
59 * The seq_mode gives the operating mode of the sequencer: 1 = level1 (the
60 * default) 2 = level2 (extended capabilites)
65 static int seq_mode = SEQ_1;
67 static int *seq_sleeper = NULL;
68 static volatile struct snd_wait seq_sleep_flag = {0};
69 static int *midi_sleeper = NULL;
70 static volatile struct snd_wait midi_sleep_flag = {0};
72 static int midi_opened[MAX_MIDI_DEV] = {0};
73 static int midi_written[MAX_MIDI_DEV] = {0};
75 static u_long prev_input_time = 0;
76 static int prev_event_time;
77 static u_long seq_time = 0;
79 #include <i386/isa/sound/tuning.h>
83 static u_char *queue = NULL;
84 static u_char *iqueue = NULL;
86 static volatile int qhead = 0, qtail = 0, qlen = 0;
87 static volatile int iqhead = 0, iqtail = 0, iqlen = 0;
88 static volatile int seq_playing = 0;
89 static volatile int sequencer_busy = 0;
90 static int output_treshold;
91 static int pre_event_timeout;
92 static u_int synth_open_mask;
94 static int seq_queue(u_char *note, char nonblock);
95 static void seq_startplay(void);
96 static int seq_sync(void);
97 static void seq_reset(void);
98 static int pmgr_present[MAX_SYNTH_DEV] =
100 static struct callout_handle sequencertimeout_ch
101 = CALLOUT_HANDLE_INITIALIZER(&sequencertimeout_ch);
103 #if MAX_SYNTH_DEV > 15
104 #error Too many synthesizer devices enabled.
108 * sound_timer stuff -- originally in soundcard.c
110 * A negative value means a relative timeout in |count| ticks.
111 * A positive value is used for what ?
113 * In any case, this is only used in sequencer.c
116 static int timer_running = 0;
119 request_sound_timer(int count)
121 static int current = 0;
125 sequencertimeout_ch = timeout(sequencer_timer, 0, -count);
129 current = 0; /* Timer restarted */
131 count = count - current;
135 sequencertimeout_ch = timeout(sequencer_timer, 0, count);
141 sound_stop_timer(void)
144 untimeout( sequencer_timer, 0, sequencertimeout_ch);
149 sequencer_read(int dev, struct fileinfo * file, snd_rw_buf * buf, int count)
151 int c = count, p = 0;
157 ev_len = seq_mode == SEQ_1 ? 4 : 8;
159 if (dev) /* Patch manager device */
160 return pmgr_read(dev - 1, file, buf, count);
168 DO_SLEEP(chn, midi_sleep_flag, pre_event_timeout);
175 while (iqlen && c >= ev_len) {
177 if (uiomove((char *) &iqueue[iqhead * IEV_SZ], ev_len, buf))
178 printf("sb: Bad copyout()!\n");
182 iqhead = (iqhead + 1) % SEQ_MAX_QUEUE;
191 sequencer_midi_output(int dev)
199 seq_copy_to_input(u_char *event, int len)
204 * Verify that the len is valid for the current mode.
207 if (len != 4 && len != 8)
209 if ((seq_mode == SEQ_1) != (len == 4))
212 if (iqlen >= (SEQ_MAX_QUEUE - 1))
213 return; /* Overflow */
216 bcopy(event, &iqueue[iqtail * IEV_SZ], len);
218 iqtail = (iqtail + 1) % SEQ_MAX_QUEUE;
220 if ((midi_sleep_flag.mode & WK_SLEEP)) {
221 midi_sleep_flag.mode = WK_WAKEUP;
222 wakeup(midi_sleeper);
228 sequencer_midi_input(int dev, u_char data)
233 if (data == 0xfe) /* Ignore active sensing */
236 tstamp = get_time() - seq_time;
237 if (tstamp != prev_input_time) {
238 tstamp = (tstamp << 8) | SEQ_WAIT;
240 seq_copy_to_input((u_char *) &tstamp, 4);
241 prev_input_time = tstamp;
243 event[0] = SEQ_MIDIPUTC;
248 seq_copy_to_input(event, 4);
252 seq_input_event(u_char *event, int len)
256 if (seq_mode == SEQ_2)
257 this_time = tmr->get_time(tmr_no);
259 this_time = get_time() - seq_time;
261 if (this_time != prev_input_time) {
264 tmp_event[0] = EV_TIMING;
265 tmp_event[1] = TMR_WAIT_ABS;
268 *(u_long *) &tmp_event[4] = this_time;
270 seq_copy_to_input(tmp_event, 8);
271 prev_input_time = this_time;
273 seq_copy_to_input(event, len);
277 sequencer_write(int dev, struct fileinfo * file, snd_rw_buf * buf, int count)
279 u_char event[EV_SZ], ev_code;
280 int p = 0, c, ev_size;
282 int mode = file->mode & O_ACCMODE;
286 DEB(printf("sequencer_write(dev=%d, count=%d)\n", dev, count));
288 if (mode == OPEN_READ)
292 return pmgr_write(dev - 1, file, buf, count);
298 if (uiomove((char *) event, 4, buf))
299 printf("sb: Bad copyin()!\n");
302 if (ev_code == SEQ_FULLSIZE) {
305 dev = *(u_short *) &event[2];
306 if (dev < 0 || dev >= max_synthdev)
309 if (!(synth_open_mask & (1 << dev)))
312 err = synth_devs[dev]->load_patch(dev,
313 *(short *) &event[0], buf, p + 4, c, 0);
319 if (ev_code >= 128) {
320 if (seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED) {
321 printf("Sequencer: Invalid level 2 event %x\n", ev_code);
331 if (uiomove((char *) &event[4], 4, buf))
332 printf("sb: Bad copyin()!\n");
334 if (seq_mode == SEQ_2) {
335 printf("Sequencer: 4 byte event in level 2 mode\n");
341 if (event[0] == SEQ_MIDIPUTC) {
343 if (!midi_opened[event[2]]) {
347 if (dev >= max_mididev) {
348 printf("Sequencer Error: Nonexistent MIDI device %d\n",dev);
351 mode = file->mode & O_ACCMODE;
353 if ((err = midi_devs[dev]->open(dev, mode,
354 sequencer_midi_input, sequencer_midi_output)) < 0) {
356 printf("Sequencer Error: Unable to open Midi #%d\n", dev);
359 midi_opened[dev] = 1;
362 if (!seq_queue(event, 0)) {
363 int processed = count - c;
377 return count; /* This will "eat" chunks shorter than 4 bytes
378 * (if written alone) Should we really do that ?
383 seq_queue(u_char *note, char nonblock)
387 * Test if there is space in the queue
390 if (qlen >= SEQ_MAX_QUEUE)
392 seq_startplay(); /* Give chance to drain the queue */
394 if (!nonblock && qlen >= SEQ_MAX_QUEUE &&
395 !(seq_sleep_flag.mode & WK_SLEEP)) {
397 * Sleep until there is enough space on the queue
404 DO_SLEEP(chn, seq_sleep_flag, 0);
407 if (qlen >= SEQ_MAX_QUEUE)
408 return 0; /* To be sure */
409 bcopy(note, &queue[qtail * EV_SZ], EV_SZ);
411 qtail = (qtail + 1) % SEQ_MAX_QUEUE;
418 extended_event(u_char *q)
422 if (dev < 0 || dev >= max_synthdev)
425 if (!(synth_open_mask & (1 << dev)))
430 synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
434 if (q[4] > 127 && q[4] != 255)
437 synth_devs[dev]->start_note(dev, q[3], q[4], q[5]);
441 synth_devs[dev]->set_instr(dev, q[3], q[4]);
445 synth_devs[dev]->aftertouch(dev, q[3], q[4]);
449 synth_devs[dev]->panning(dev, q[3], (char) q[4]);
453 synth_devs[dev]->controller(dev, q[3], q[4], *(short *) &q[5]);
457 if (synth_devs[dev]->volume_method != NULL)
458 synth_devs[dev]->volume_method(dev, q[3]);
469 find_voice(int dev, int chn, int note)
474 key = (chn << 8) | (note + 1);
476 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
477 if (synth_devs[dev]->alloc.map[i] == key)
484 alloc_voice(int dev, int chn, int note)
489 key = (chn << 8) | (note + 1);
491 voice = synth_devs[dev]->alloc_voice(dev, chn, note,
492 &synth_devs[dev]->alloc);
493 synth_devs[dev]->alloc.map[voice] = key;
494 synth_devs[dev]->alloc.alloc_times[voice] =
495 synth_devs[dev]->alloc.timestamp++;
500 seq_chn_voice_event(u_char *event)
502 u_char dev = event[1];
503 u_char cmd = event[2];
504 u_char chn = event[3];
505 u_char note = event[4];
506 u_char parm = event[5];
509 if ((int) dev > max_synthdev)
511 if (!(synth_open_mask & (1 << dev)))
513 if (!synth_devs[dev])
516 if (seq_mode == SEQ_2) {
517 if (synth_devs[dev]->alloc_voice)
518 voice = find_voice(dev, chn, note);
520 if (cmd == MIDI_NOTEON && parm == 0) {
527 if (note > 127 && note != 255) /* Not a seq2 feature */
530 if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice) {
531 /* Internal synthesizer (FM, GUS, etc) */
532 voice = alloc_voice(dev, chn, note);
537 if (seq_mode == SEQ_2 && (int) dev < num_synths) {
539 * The MIDI channel 10 is a percussive channel. Use
540 * the note number to select the proper patch (128 to
545 synth_devs[dev]->set_instr(dev, voice, 128 + note);
546 note = 60; /* Middle C */
550 if (seq_mode == SEQ_2)
551 synth_devs[dev]->setup_voice(dev, voice, chn);
552 synth_devs[dev]->start_note(dev, voice, note, parm);
558 synth_devs[dev]->kill_note(dev, voice, note, parm);
561 case MIDI_KEY_PRESSURE:
564 synth_devs[dev]->aftertouch(dev, voice, parm);
572 seq_chn_common_event(u_char *event)
574 u_char dev = event[1];
575 u_char cmd = event[2];
576 u_char chn = event[3];
577 u_char p1 = event[4];
579 /* u_char p2 = event[5]; */
580 u_short w14 = *(short *) &event[6];
582 if ((int) dev > max_synthdev)
584 if (!(synth_open_mask & (1 << dev)))
586 if (!synth_devs[dev])
590 case MIDI_PGM_CHANGE:
591 if (seq_mode == SEQ_2) {
592 synth_devs[dev]->chn_info[chn].pgm_num = p1;
593 if ((int) dev >= num_synths)
594 synth_devs[dev]->set_instr(dev, chn, p1);
596 synth_devs[dev]->set_instr(dev, chn, p1);
600 case MIDI_CTL_CHANGE:
601 if (seq_mode == SEQ_2) {
602 if (chn > 15 || p1 > 127)
605 synth_devs[dev]->chn_info[chn].controllers[p1] = w14 & 0x7f;
607 if (p1 < 32) /* Setting MSB should clear LSB to 0 */
608 synth_devs[dev]->chn_info[chn].controllers[p1 + 32] = 0;
610 if ((int) dev < num_synths) {
611 int val = w14 & 0x7f;
614 if (p1 < 64) { /* Combine MSB and LSB */
615 val = ((synth_devs[dev]->
616 chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
618 chn_info[chn].controllers[p1 | 32] & 0x7f);
621 /* Handle all playing notes on this channel */
623 key = ((int) chn << 8);
625 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
626 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
627 synth_devs[dev]->controller(dev, i, p1, val);
629 synth_devs[dev]->controller(dev, chn, p1, w14);
631 synth_devs[dev]->controller(dev, chn, p1, w14);
634 case MIDI_PITCH_BEND:
635 if (seq_mode == SEQ_2) {
636 synth_devs[dev]->chn_info[chn].bender_value = w14;
638 if ((int) dev < num_synths) { /* Handle all playing
639 * notes on this channel */
644 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
645 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
646 synth_devs[dev]->bender(dev, i, w14);
648 synth_devs[dev]->bender(dev, chn, w14);
650 synth_devs[dev]->bender(dev, chn, w14);
658 seq_timing_event(u_char *event)
660 u_char cmd = event[1];
661 u_int parm = *(int *) &event[4];
663 if (seq_mode == SEQ_2) {
666 if ((ret = tmr->event(tmr_no, event)) == TIMER_ARMED) {
667 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold) {
671 if ((seq_sleep_flag.mode & WK_SLEEP)) {
672 seq_sleep_flag.mode = WK_WAKEUP;
682 parm += prev_event_time;
685 * NOTE! No break here. Execution of TMR_WAIT_REL continues
686 * in the next case (TMR_WAIT_ABS)
695 prev_event_time = time;
697 request_sound_timer(time);
699 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold) {
703 if ((seq_sleep_flag.mode & WK_SLEEP)) {
704 seq_sleep_flag.mode = WK_WAKEUP;
714 seq_time = get_time();
729 if (seq_mode == SEQ_2)
730 seq_copy_to_input(event, 8);
732 parm = (parm << 8 | SEQ_ECHO);
733 seq_copy_to_input((u_char *) &parm, 4);
740 return TIMER_NOT_ARMED;
744 seq_local_event(u_char *event)
746 u_char cmd = event[1];
747 u_int parm = *((u_int *) &event[4]);
750 case LOCL_STARTAUDIO:
752 DMAbuf_start_devices(parm);
761 seq_sysex_message(u_char *event)
765 u_char *buf = &event[2];
767 if ((int) dev > max_synthdev)
769 if (!(synth_open_mask & (1 << dev)))
771 if (!synth_devs[dev])
773 if (!synth_devs[dev]->send_sysex)
777 for (i = 0; i < 6 && buf[i] != 0xff; i++)
781 synth_devs[dev]->send_sysex(dev, buf, l);
785 play_event(u_char *q)
788 * NOTE! This routine returns 0 = normal event played. 1 = Timer
789 * armed. Suspend playback until timer callback. 2 = MIDI output
790 * buffer full. Restore queue and suspend until timer
796 if (synth_open_mask & (1 << 0))
798 synth_devs[0]->kill_note(0, q[1], 255, q[3]);
802 if (q[4] < 128 || q[4] == 255)
803 if (synth_open_mask & (1 << 0))
805 synth_devs[0]->start_note(0, q[1], q[2], q[3]);
809 delay = (u_long *) q; /* Bytes 1 to 3 are
810 * containing the * delay in
812 *delay = (*delay >> 8) & 0xffffff;
819 prev_event_time = time;
821 request_sound_timer(time);
823 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold) {
827 if ((seq_sleep_flag.mode & WK_SLEEP)) {
828 seq_sleep_flag.mode = WK_WAKEUP;
834 * The timer is now active and will reinvoke this
835 * function after the timer expires. Return to the
843 if (synth_open_mask & (1 << 0))
845 synth_devs[0]->set_instr(0, q[1], q[2]);
848 case SEQ_SYNCTIMER: /* Reset timer */
849 seq_time = get_time();
854 case SEQ_MIDIPUTC: /* Put a midi character */
855 if (midi_opened[q[2]]) {
860 if (!midi_devs[dev]->putc(dev, q[1])) {
862 * Output FIFO is full. Wait one timer cycle and try again.
866 request_sound_timer(-1);
869 midi_written[dev] = 1;
874 seq_copy_to_input(q, 4); /* Echo back to the process */
878 if ((int) q[1] < max_synthdev)
879 synth_devs[q[1]]->hw_control(q[1], q);
887 seq_chn_voice_event(q);
891 seq_chn_common_event(q);
895 if (seq_timing_event(q) == TIMER_ARMED) {
905 seq_sysex_message(q);
918 int this_one, action;
923 qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
929 if ((action = play_event(&queue[this_one * EV_SZ]))) {
931 * Suspend playback. Next timer routine invokes this routine again
943 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold) {
947 if ((seq_sleep_flag.mode & WK_SLEEP)) {
948 seq_sleep_flag.mode = WK_WAKEUP;
956 reset_controllers(int dev, u_char *controller, int update_dev)
961 for (i = 0; i < 128; i++)
962 controller[i] = ctrl_def_values[i];
970 max_synthdev = num_synths;
972 for (dev = 0; dev < num_midis; dev++)
973 if (midi_devs[dev]->converter != NULL)
974 synth_devs[max_synthdev++] = midi_devs[dev]->converter;
975 for (dev = 0; dev < max_synthdev; dev++) {
978 for (chn = 0; chn < 16; chn++) {
979 synth_devs[dev]->chn_info[chn].pgm_num = 0;
980 reset_controllers(dev,
981 synth_devs[dev]->chn_info[chn].controllers, 0);
982 synth_devs[dev]->chn_info[chn].bender_value = (1<<7); /* Neutral */
991 sequencer_open(int dev, struct fileinfo * file)
997 level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1;
1000 mode = file->mode & O_ACCMODE;
1002 DEB(printf("sequencer_open(dev=%d)\n", dev));
1004 if (!sequencer_ok) {
1005 printf("Soundcard: Sequencer not initialized\n");
1008 if (dev) { /* Patch manager device */
1011 printf("Patch manager interface is currently broken. Sorry\n");
1016 if (dev >= MAX_SYNTH_DEV)
1018 if (pmgr_present[dev])
1020 if ((err = pmgr_open(dev)) < 0)
1023 pmgr_present[dev] = 1;
1027 if (sequencer_busy) {
1028 printf("Sequencer busy\n");
1035 max_mididev = num_midis;
1036 max_synthdev = num_synths;
1037 pre_event_timeout = 0;
1040 if (pending_timer != -1) {
1041 tmr_no = pending_timer;
1044 if (tmr_no == -1) { /* Not selected yet */
1048 for (i = 0; i < num_sound_timers; i++)
1049 if (sound_timer_devs[i]->priority > best) {
1051 best = sound_timer_devs[i]->priority;
1053 if (tmr_no == -1) /* Should not be */
1056 tmr = sound_timer_devs[tmr_no];
1060 printf("sequencer: No timer for level 2\n");
1066 if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1068 printf("Sequencer: No Midi devices. Input not possible\n");
1072 if (!max_synthdev && !max_mididev)
1075 synth_open_mask = 0;
1077 for (i = 0; i < max_mididev; i++) {
1079 midi_written[i] = 0;
1083 * if (mode == OPEN_WRITE || mode == OPEN_READWRITE)
1085 for (i = 0; i < max_synthdev; i++) /* Open synth devices */
1086 if ((tmp = synth_devs[i]->open(i, mode)) < 0) {
1087 printf("Sequencer: Warning! Cannot open synth device #%d (%d)\n",
1089 if (synth_devs[i]->midi_dev)
1090 printf("(Maps to MIDI dev #%d)\n", synth_devs[i]->midi_dev);
1092 synth_open_mask |= (1 << i);
1093 if (synth_devs[i]->midi_dev) /* Is a midi interface */
1094 midi_opened[synth_devs[i]->midi_dev] = 1;
1097 seq_time = get_time();
1098 prev_input_time = 0;
1099 prev_event_time = 0;
1101 if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE)) {
1102 /* Initialize midi input devices */
1103 for (i = 0; i < max_mididev; i++)
1104 if (!midi_opened[i]) {
1105 if ((retval = midi_devs[i]->open(i, mode,
1106 sequencer_midi_input, sequencer_midi_output)) >= 0)
1110 if (seq_mode == SEQ_2) {
1111 tmr->open(tmr_no, seq_mode);
1113 seq_sleep_flag.aborting = 0;
1114 seq_sleep_flag.mode = WK_NONE;
1115 midi_sleep_flag.aborting = 0;
1116 midi_sleep_flag.mode = WK_NONE;
1117 output_treshold = SEQ_MAX_QUEUE / 2;
1119 for (i = 0; i < num_synths; i++)
1120 if (pmgr_present[i])
1121 pmgr_inform(i, PM_E_OPENED, 0, 0, 0, 0);
1127 seq_drain_midi_queues(void)
1132 * Give the Midi drivers time to drain their output queues
1137 while (!(seq_sleep_flag.aborting) && n) {
1140 for (i = 0; i < max_mididev; i++)
1141 if (midi_opened[i] && midi_written[i])
1142 if (midi_devs[i]->buffer_status != NULL)
1143 if (midi_devs[i]->buffer_status(i))
1147 * Let's have a delay
1153 DO_SLEEP(chn, seq_sleep_flag, hz / 10);
1160 sequencer_release(int dev, struct fileinfo * file)
1163 int mode = file->mode & O_ACCMODE;
1167 DEB(printf("sequencer_release(dev=%d)\n", dev));
1169 if (dev) { /* Patch manager device */
1172 pmgr_present[dev] = 0;
1176 * Wait until the queue is empty (if we don't have nonblock)
1179 if (mode != OPEN_READ && !0)
1180 while (!(seq_sleep_flag.aborting) && qlen) {
1184 if (mode != OPEN_READ)
1185 seq_drain_midi_queues(); /* Ensure the output queues are empty */
1187 if (mode != OPEN_READ)
1188 seq_drain_midi_queues(); /* Flush the all notes off messages */
1190 for (i = 0; i < max_synthdev; i++)
1191 if (synth_open_mask & (1 << i)) /* Actually opened */
1192 if (synth_devs[i]) {
1193 synth_devs[i]->close(i);
1195 if (synth_devs[i]->midi_dev)
1196 midi_opened[synth_devs[i]->midi_dev] = 0;
1198 for (i = 0; i < num_synths; i++)
1199 if (pmgr_present[i])
1200 pmgr_inform(i, PM_E_CLOSED, 0, 0, 0, 0);
1202 for (i = 0; i < max_mididev; i++)
1204 midi_devs[i]->close(i);
1206 if (seq_mode == SEQ_2)
1217 if (qlen && !seq_playing && !(seq_sleep_flag.aborting))
1221 if (qlen && !(seq_sleep_flag.mode & WK_SLEEP)) {
1225 DO_SLEEP(chn, seq_sleep_flag, 0);
1234 midi_outc(int dev, u_char data)
1237 * NOTE! Calls sleep(). Don't call this from interrupt.
1244 * This routine sends one byte to the Midi channel. If the output
1245 * Fifo is full, it waits until there is space in the queue
1248 n = 3 * hz; /* Timeout */
1251 while (n && !midi_devs[dev]->putc(dev, data)) {
1254 DO_SLEEP(chn, seq_sleep_flag, 4);
1265 * NOTE! Calls sleep(). Don't call this from interrupt.
1273 seq_time = get_time();
1274 prev_input_time = 0;
1275 prev_event_time = 0;
1277 qlen = qhead = qtail = 0;
1278 iqlen = iqhead = iqtail = 0;
1280 for (i = 0; i < max_synthdev; i++)
1281 if (synth_open_mask & (1 << i))
1283 synth_devs[i]->reset(i);
1285 if (seq_mode == SEQ_2) {
1286 for (chn = 0; chn < 16; chn++)
1287 for (i = 0; i < max_synthdev; i++)
1288 if ( (synth_open_mask & (1 << i)) && (synth_devs[i]) ) {
1289 synth_devs[i]->controller(i, chn,123,0);/* All notes off */
1290 synth_devs[i]->controller(i, chn,121,0);/* Reset all ctl */
1291 synth_devs[i]->bender(i, chn, 1 << 13); /* Bender off */
1294 } else { /* seq_mode == SEQ_1 */
1295 for (i = 0; i < max_mididev; i++)
1296 if (midi_written[i]) {
1297 /* Midi used. Some notes may still be playing */
1299 * Sending just a ACTIVE SENSING message
1300 * should be enough to stop all playing
1301 * notes. Since there are devices not
1302 * recognizing the active sensing, we have to
1303 * send some all notes off messages also.
1307 for (chn = 0; chn < 16; chn++) {
1308 midi_outc(i, (u_char) (0xb0 + (chn & 0x0f))); /* control change */
1309 midi_outc(i, 0x7b); /* All notes off */
1310 midi_outc(i, 0); /* Dummy parameter */
1313 midi_devs[i]->close(i);
1315 midi_written[i] = 0;
1323 if ((seq_sleep_flag.mode & WK_SLEEP)) {
1324 seq_sleep_flag.mode = WK_WAKEUP;
1325 wakeup(seq_sleeper);
1335 * This routine is called by the application in case the user wants
1336 * to reset the system to the default state.
1342 * Since some of the devices don't recognize the active sensing and
1343 * all notes off messages, we have to shut all notes manually.
1345 * TO BE IMPLEMENTED LATER
1349 * Also return the controllers to their default states
1354 sequencer_ioctl(int dev, struct fileinfo * file,
1355 u_int cmd, ioctl_arg arg)
1357 int midi_dev, orig_dev;
1358 int mode = file->mode & O_ACCMODE;
1360 orig_dev = dev = dev >> 4;
1363 case SNDCTL_TMR_TIMEBASE:
1364 case SNDCTL_TMR_TEMPO:
1365 case SNDCTL_TMR_START:
1366 case SNDCTL_TMR_STOP:
1367 case SNDCTL_TMR_CONTINUE:
1368 case SNDCTL_TMR_METRONOME:
1369 case SNDCTL_TMR_SOURCE:
1370 if (dev) /* Patch manager */
1373 if (seq_mode != SEQ_2)
1375 return tmr->ioctl(tmr_no, cmd, arg);
1378 case SNDCTL_TMR_SELECT:
1379 if (dev) /* Patch manager */
1382 if (seq_mode != SEQ_2)
1384 pending_timer = (*(int *) arg);
1386 if (pending_timer < 0 || pending_timer >= num_sound_timers) {
1390 return *(int *) arg = pending_timer;
1393 case SNDCTL_SEQ_PANIC:
1397 case SNDCTL_SEQ_SYNC:
1398 if (dev) /* Patch manager */
1401 if (mode == OPEN_READ)
1403 while (qlen && !(seq_sleep_flag.aborting))
1411 case SNDCTL_SEQ_RESET:
1412 if (dev) /* Patch manager */
1419 case SNDCTL_SEQ_TESTMIDI:
1420 if (dev) /* Patch manager */
1423 midi_dev = (*(int *) arg);
1424 if (midi_dev >= max_mididev)
1427 if (!midi_opened[midi_dev]) {
1430 mode = file->mode & O_ACCMODE;
1431 if ((err = midi_devs[midi_dev]->open(midi_dev, mode,
1432 sequencer_midi_input, sequencer_midi_output)) < 0)
1435 midi_opened[midi_dev] = 1;
1440 case SNDCTL_SEQ_GETINCOUNT:
1441 if (dev) /* Patch manager */
1444 if (mode == OPEN_WRITE)
1446 return *(int *) arg = iqlen;
1449 case SNDCTL_SEQ_GETOUTCOUNT:
1451 if (mode == OPEN_READ)
1453 return *(int *) arg = SEQ_MAX_QUEUE - qlen;
1456 case SNDCTL_SEQ_CTRLRATE:
1457 if (dev) /* Patch manager */
1461 * If *arg == 0, just return the current rate
1463 if (seq_mode == SEQ_2)
1464 return tmr->ioctl(tmr_no, cmd, arg);
1466 if ((*(int *) arg) != 0)
1469 return *(int *) arg = hz;
1472 case SNDCTL_SEQ_RESETSAMPLES:
1476 dev = (*(int *) arg);
1477 if (dev < 0 || dev >= num_synths)
1479 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1481 if (!orig_dev && pmgr_present[dev])
1482 pmgr_inform(dev, PM_E_PATCH_RESET, 0, 0, 0, 0);
1484 err = synth_devs[dev]->ioctl(dev, cmd, arg);
1489 case SNDCTL_SEQ_NRSYNTHS:
1490 return *(int *) arg = max_synthdev;
1493 case SNDCTL_SEQ_NRMIDIS:
1494 return *(int *) arg = max_mididev;
1497 case SNDCTL_SYNTH_MEMAVL:
1499 int dev = (*(int *) arg);
1501 if (dev < 0 || dev >= num_synths)
1504 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1507 return *(int *) arg = synth_devs[dev]->ioctl(dev, cmd, arg);
1511 case SNDCTL_FM_4OP_ENABLE:
1513 int dev = (*(int *) arg);
1515 if (dev < 0 || dev >= num_synths)
1518 if (!(synth_open_mask & (1 << dev)))
1521 synth_devs[dev]->ioctl(dev, cmd, arg);
1526 case SNDCTL_SYNTH_INFO:
1528 struct synth_info inf;
1531 bcopy(&(((char *) arg)[0]), (char *) &inf, sizeof(inf));
1534 if (dev < 0 || dev >= max_synthdev)
1537 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1540 return synth_devs[dev]->ioctl(dev, cmd, arg);
1544 case SNDCTL_SEQ_OUTOFBAND:
1546 struct seq_event_rec event;
1549 bcopy(&(((char *) arg)[0]), (char *) &event, sizeof(event));
1552 play_event(event.arr);
1559 case SNDCTL_MIDI_INFO:
1561 struct midi_info inf;
1564 bcopy(&(((char *) arg)[0]), (char *) &inf, sizeof(inf));
1567 if (dev < 0 || dev >= max_mididev)
1570 bcopy((char *) &(midi_devs[dev]->info), &(((char *) arg)[0]), sizeof(inf));
1575 case SNDCTL_PMGR_IFACE:
1577 struct patmgr_info *inf;
1580 if ((inf = (struct patmgr_info *) malloc(sizeof(*inf), M_TEMP, M_WAITOK)) == NULL) {
1581 printf("patmgr: Can't allocate memory for a message\n");
1584 bcopy(&(((char *) arg)[0]), (char *) inf, sizeof(*inf));
1587 if (dev < 0 || dev >= num_synths) {
1591 if (!synth_devs[dev]->pmgr_interface) {
1595 if ((err = synth_devs[dev]->pmgr_interface(dev, inf)) == -1) {
1599 bcopy((char *) inf, &(((char *) arg)[0]), sizeof(*inf));
1605 case SNDCTL_PMGR_ACCESS:
1607 struct patmgr_info *inf;
1610 if ((inf = (struct patmgr_info *) malloc(sizeof(*inf), M_TEMP, M_WAITOK)) == NULL) {
1611 printf("patmgr: Can't allocate memory for a message\n");
1614 bcopy(&(((char *) arg)[0]), (char *) inf, sizeof(*inf));
1617 if (dev < 0 || dev >= num_synths) {
1621 if (!pmgr_present[dev]) {
1625 if ((err = pmgr_access(dev, inf)) < 0) {
1629 bcopy((char *) inf, &(((char *) arg)[0]), sizeof(*inf));
1635 case SNDCTL_SEQ_THRESHOLD:
1637 int tmp = (*(int *) arg);
1639 if (dev)/* Patch manager */
1644 if (tmp >= SEQ_MAX_QUEUE)
1645 tmp = SEQ_MAX_QUEUE - 1;
1646 output_treshold = tmp;
1651 case SNDCTL_MIDI_PRETIME:
1653 int val = (*(int *) arg);
1658 val = (hz * val) / 10;
1659 pre_event_timeout = val;
1660 return *(int *) arg = val;
1665 if (dev) /* Patch manager */
1668 if (mode == OPEN_READ)
1673 if (!(synth_open_mask & (1 << 0)))
1675 return synth_devs[0]->ioctl(0, cmd, arg);
1684 sequencer_poll (int dev, struct fileinfo *file, int events, select_table * wait)
1686 unsigned long flags;
1693 if (events & (POLLIN | POLLRDNORM)) {
1695 selrecord(wait, &selinfo[dev]);
1697 revents |= events & (POLLIN | POLLRDNORM);
1698 midi_sleep_flag.mode &= ~WK_SLEEP;
1701 if (events & (POLLOUT | POLLWRNORM)) {
1702 if (qlen >= SEQ_MAX_QUEUE)
1703 selrecord(wait, &selinfo[dev]);
1705 revents |= events & (POLLOUT | POLLWRNORM);
1706 seq_sleep_flag.mode &= ~WK_SLEEP;
1718 sequencer_timer(void *dummy)
1724 note_to_freq(int note_num)
1728 * This routine converts a midi note to a frequency (multiplied by
1732 int note, octave, note_freq;
1735 261632, 277189, 293671, 311132, 329632, 349232,
1736 369998, 391998, 415306, 440000, 466162, 493880
1739 #define BASE_OCTAVE 5
1741 octave = note_num / 12;
1742 note = note_num % 12;
1744 note_freq = notes[note];
1746 if (octave < BASE_OCTAVE)
1747 note_freq >>= (BASE_OCTAVE - octave);
1748 else if (octave > BASE_OCTAVE)
1749 note_freq <<= (octave - BASE_OCTAVE);
1759 compute_finetune(u_long base_freq, int bend, int range)
1762 int negative, semitones, cents, multiplier = 1;
1775 bend = bend * range / 8192;
1779 negative = bend < 0 ? 1 : 0;
1787 * if (bend > 2399) bend = 2399;
1789 while (bend > 2399) {
1794 semitones = bend / 100;
1797 amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents])
1801 return (base_freq * 10000) / amount; /* Bend down */
1803 return (base_freq * amount) / 10000; /* Bend up */
1813 queue = (u_char *) malloc(SEQ_MAX_QUEUE * EV_SZ, M_DEVBUF, M_NOWAIT);
1815 panic("SOUND: Cannot allocate memory\n");
1817 iqueue = (u_char *) malloc(SEQ_MAX_QUEUE * IEV_SZ, M_DEVBUF, M_NOWAIT);
1819 panic("SOUND: Cannot allocate memory\n");