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 $
32 #include <i386/isa/sound/sound_config.h>
36 #ifdef CONFIG_SEQUENCER
38 #include <sys/select.h>
40 #include <i386/isa/sound/midi_ctrl.h>
42 static void seq_drain_midi_queues(void);
44 sequencer_poll (int dev, struct fileinfo *file, int events, select_table * wait);
45 static int sequencer_ok = 0;
46 static struct sound_timer_operations *tmr;
47 static int tmr_no = -1; /* Currently selected timer */
48 static int pending_timer = -1; /* For timer change operation */
51 * Local counts for number of synth and MIDI devices. These are initialized
52 * by the sequencer_open.
54 static int max_mididev = 0;
55 static int max_synthdev = 0;
58 * The seq_mode gives the operating mode of the sequencer: 1 = level1 (the
59 * default) 2 = level2 (extended capabilites)
64 static int seq_mode = SEQ_1;
66 static int *seq_sleeper = NULL;
67 static volatile struct snd_wait seq_sleep_flag = {0};
68 static int *midi_sleeper = NULL;
69 static volatile struct snd_wait midi_sleep_flag = {0};
71 static int midi_opened[MAX_MIDI_DEV] = {0};
72 static int midi_written[MAX_MIDI_DEV] = {0};
74 static u_long prev_input_time = 0;
75 static int prev_event_time;
76 static u_long seq_time = 0;
78 #include <i386/isa/sound/tuning.h>
82 static u_char *queue = NULL;
83 static u_char *iqueue = NULL;
85 static volatile int qhead = 0, qtail = 0, qlen = 0;
86 static volatile int iqhead = 0, iqtail = 0, iqlen = 0;
87 static volatile int seq_playing = 0;
88 static volatile int sequencer_busy = 0;
89 static int output_treshold;
90 static int pre_event_timeout;
91 static u_int synth_open_mask;
93 static int seq_queue(u_char *note, char nonblock);
94 static void seq_startplay(void);
95 static int seq_sync(void);
96 static void seq_reset(void);
97 static int pmgr_present[MAX_SYNTH_DEV] =
99 static struct callout_handle sequencertimeout_ch
100 = CALLOUT_HANDLE_INITIALIZER(&sequencertimeout_ch);
102 #if MAX_SYNTH_DEV > 15
103 #error Too many synthesizer devices enabled.
107 * sound_timer stuff -- originally in soundcard.c
109 * A negative value means a relative timeout in |count| ticks.
110 * A positive value is used for what ?
112 * In any case, this is only used in sequencer.c
115 static int timer_running = 0;
118 request_sound_timer(int count)
120 static int current = 0;
124 sequencertimeout_ch = timeout(sequencer_timer, 0, -count);
128 current = 0; /* Timer restarted */
130 count = count - current;
134 sequencertimeout_ch = timeout(sequencer_timer, 0, count);
140 sound_stop_timer(void)
143 untimeout( sequencer_timer, 0, sequencertimeout_ch);
148 sequencer_read(int dev, struct fileinfo * file, snd_rw_buf * buf, int count)
150 int c = count, p = 0;
156 ev_len = seq_mode == SEQ_1 ? 4 : 8;
158 if (dev) /* Patch manager device */
159 return pmgr_read(dev - 1, file, buf, count);
167 DO_SLEEP(chn, midi_sleep_flag, pre_event_timeout);
174 while (iqlen && c >= ev_len) {
176 if (uiomove((char *) &iqueue[iqhead * IEV_SZ], ev_len, buf))
177 printf("sb: Bad copyout()!\n");
181 iqhead = (iqhead + 1) % SEQ_MAX_QUEUE;
190 sequencer_midi_output(int dev)
198 seq_copy_to_input(u_char *event, int len)
203 * Verify that the len is valid for the current mode.
206 if (len != 4 && len != 8)
208 if ((seq_mode == SEQ_1) != (len == 4))
211 if (iqlen >= (SEQ_MAX_QUEUE - 1))
212 return; /* Overflow */
215 bcopy(event, &iqueue[iqtail * IEV_SZ], len);
217 iqtail = (iqtail + 1) % SEQ_MAX_QUEUE;
219 if ((midi_sleep_flag.mode & WK_SLEEP)) {
220 midi_sleep_flag.mode = WK_WAKEUP;
221 wakeup(midi_sleeper);
227 sequencer_midi_input(int dev, u_char data)
232 if (data == 0xfe) /* Ignore active sensing */
235 tstamp = get_time() - seq_time;
236 if (tstamp != prev_input_time) {
237 tstamp = (tstamp << 8) | SEQ_WAIT;
239 seq_copy_to_input((u_char *) &tstamp, 4);
240 prev_input_time = tstamp;
242 event[0] = SEQ_MIDIPUTC;
247 seq_copy_to_input(event, 4);
251 seq_input_event(u_char *event, int len)
255 if (seq_mode == SEQ_2)
256 this_time = tmr->get_time(tmr_no);
258 this_time = get_time() - seq_time;
260 if (this_time != prev_input_time) {
263 tmp_event[0] = EV_TIMING;
264 tmp_event[1] = TMR_WAIT_ABS;
267 *(u_long *) &tmp_event[4] = this_time;
269 seq_copy_to_input(tmp_event, 8);
270 prev_input_time = this_time;
272 seq_copy_to_input(event, len);
276 sequencer_write(int dev, struct fileinfo * file, snd_rw_buf * buf, int count)
278 u_char event[EV_SZ], ev_code;
279 int p = 0, c, ev_size;
281 int mode = file->mode & O_ACCMODE;
285 DEB(printf("sequencer_write(dev=%d, count=%d)\n", dev, count));
287 if (mode == OPEN_READ)
291 return pmgr_write(dev - 1, file, buf, count);
297 if (uiomove((char *) event, 4, buf))
298 printf("sb: Bad copyin()!\n");
301 if (ev_code == SEQ_FULLSIZE) {
304 dev = *(u_short *) &event[2];
305 if (dev < 0 || dev >= max_synthdev)
308 if (!(synth_open_mask & (1 << dev)))
311 err = synth_devs[dev]->load_patch(dev,
312 *(short *) &event[0], buf, p + 4, c, 0);
318 if (ev_code >= 128) {
319 if (seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED) {
320 printf("Sequencer: Invalid level 2 event %x\n", ev_code);
330 if (uiomove((char *) &event[4], 4, buf))
331 printf("sb: Bad copyin()!\n");
333 if (seq_mode == SEQ_2) {
334 printf("Sequencer: 4 byte event in level 2 mode\n");
340 if (event[0] == SEQ_MIDIPUTC) {
342 if (!midi_opened[event[2]]) {
346 if (dev >= max_mididev) {
347 printf("Sequencer Error: Nonexistent MIDI device %d\n",dev);
350 mode = file->mode & O_ACCMODE;
352 if ((err = midi_devs[dev]->open(dev, mode,
353 sequencer_midi_input, sequencer_midi_output)) < 0) {
355 printf("Sequencer Error: Unable to open Midi #%d\n", dev);
358 midi_opened[dev] = 1;
361 if (!seq_queue(event, 0)) {
362 int processed = count - c;
376 return count; /* This will "eat" chunks shorter than 4 bytes
377 * (if written alone) Should we really do that ?
382 seq_queue(u_char *note, char nonblock)
386 * Test if there is space in the queue
389 if (qlen >= SEQ_MAX_QUEUE)
391 seq_startplay(); /* Give chance to drain the queue */
393 if (!nonblock && qlen >= SEQ_MAX_QUEUE &&
394 !(seq_sleep_flag.mode & WK_SLEEP)) {
396 * Sleep until there is enough space on the queue
403 DO_SLEEP(chn, seq_sleep_flag, 0);
406 if (qlen >= SEQ_MAX_QUEUE)
407 return 0; /* To be sure */
408 bcopy(note, &queue[qtail * EV_SZ], EV_SZ);
410 qtail = (qtail + 1) % SEQ_MAX_QUEUE;
417 extended_event(u_char *q)
421 if (dev < 0 || dev >= max_synthdev)
424 if (!(synth_open_mask & (1 << dev)))
429 synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
433 if (q[4] > 127 && q[4] != 255)
436 synth_devs[dev]->start_note(dev, q[3], q[4], q[5]);
440 synth_devs[dev]->set_instr(dev, q[3], q[4]);
444 synth_devs[dev]->aftertouch(dev, q[3], q[4]);
448 synth_devs[dev]->panning(dev, q[3], (char) q[4]);
452 synth_devs[dev]->controller(dev, q[3], q[4], *(short *) &q[5]);
456 if (synth_devs[dev]->volume_method != NULL)
457 synth_devs[dev]->volume_method(dev, q[3]);
468 find_voice(int dev, int chn, int note)
473 key = (chn << 8) | (note + 1);
475 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
476 if (synth_devs[dev]->alloc.map[i] == key)
483 alloc_voice(int dev, int chn, int note)
488 key = (chn << 8) | (note + 1);
490 voice = synth_devs[dev]->alloc_voice(dev, chn, note,
491 &synth_devs[dev]->alloc);
492 synth_devs[dev]->alloc.map[voice] = key;
493 synth_devs[dev]->alloc.alloc_times[voice] =
494 synth_devs[dev]->alloc.timestamp++;
499 seq_chn_voice_event(u_char *event)
501 u_char dev = event[1];
502 u_char cmd = event[2];
503 u_char chn = event[3];
504 u_char note = event[4];
505 u_char parm = event[5];
508 if ((int) dev > max_synthdev)
510 if (!(synth_open_mask & (1 << dev)))
512 if (!synth_devs[dev])
515 if (seq_mode == SEQ_2) {
516 if (synth_devs[dev]->alloc_voice)
517 voice = find_voice(dev, chn, note);
519 if (cmd == MIDI_NOTEON && parm == 0) {
526 if (note > 127 && note != 255) /* Not a seq2 feature */
529 if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice) {
530 /* Internal synthesizer (FM, GUS, etc) */
531 voice = alloc_voice(dev, chn, note);
536 if (seq_mode == SEQ_2 && (int) dev < num_synths) {
538 * The MIDI channel 10 is a percussive channel. Use
539 * the note number to select the proper patch (128 to
544 synth_devs[dev]->set_instr(dev, voice, 128 + note);
545 note = 60; /* Middle C */
549 if (seq_mode == SEQ_2)
550 synth_devs[dev]->setup_voice(dev, voice, chn);
551 synth_devs[dev]->start_note(dev, voice, note, parm);
557 synth_devs[dev]->kill_note(dev, voice, note, parm);
560 case MIDI_KEY_PRESSURE:
563 synth_devs[dev]->aftertouch(dev, voice, parm);
571 seq_chn_common_event(u_char *event)
573 u_char dev = event[1];
574 u_char cmd = event[2];
575 u_char chn = event[3];
576 u_char p1 = event[4];
578 /* u_char p2 = event[5]; */
579 u_short w14 = *(short *) &event[6];
581 if ((int) dev > max_synthdev)
583 if (!(synth_open_mask & (1 << dev)))
585 if (!synth_devs[dev])
589 case MIDI_PGM_CHANGE:
590 if (seq_mode == SEQ_2) {
591 synth_devs[dev]->chn_info[chn].pgm_num = p1;
592 if ((int) dev >= num_synths)
593 synth_devs[dev]->set_instr(dev, chn, p1);
595 synth_devs[dev]->set_instr(dev, chn, p1);
599 case MIDI_CTL_CHANGE:
600 if (seq_mode == SEQ_2) {
601 if (chn > 15 || p1 > 127)
604 synth_devs[dev]->chn_info[chn].controllers[p1] = w14 & 0x7f;
606 if (p1 < 32) /* Setting MSB should clear LSB to 0 */
607 synth_devs[dev]->chn_info[chn].controllers[p1 + 32] = 0;
609 if ((int) dev < num_synths) {
610 int val = w14 & 0x7f;
613 if (p1 < 64) { /* Combine MSB and LSB */
614 val = ((synth_devs[dev]->
615 chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
617 chn_info[chn].controllers[p1 | 32] & 0x7f);
620 /* Handle all playing notes on this channel */
622 key = ((int) chn << 8);
624 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
625 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
626 synth_devs[dev]->controller(dev, i, p1, val);
628 synth_devs[dev]->controller(dev, chn, p1, w14);
630 synth_devs[dev]->controller(dev, chn, p1, w14);
633 case MIDI_PITCH_BEND:
634 if (seq_mode == SEQ_2) {
635 synth_devs[dev]->chn_info[chn].bender_value = w14;
637 if ((int) dev < num_synths) { /* Handle all playing
638 * notes on this channel */
643 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
644 if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
645 synth_devs[dev]->bender(dev, i, w14);
647 synth_devs[dev]->bender(dev, chn, w14);
649 synth_devs[dev]->bender(dev, chn, w14);
657 seq_timing_event(u_char *event)
659 u_char cmd = event[1];
660 u_int parm = *(int *) &event[4];
662 if (seq_mode == SEQ_2) {
665 if ((ret = tmr->event(tmr_no, event)) == TIMER_ARMED) {
666 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold) {
670 if ((seq_sleep_flag.mode & WK_SLEEP)) {
671 seq_sleep_flag.mode = WK_WAKEUP;
681 parm += prev_event_time;
684 * NOTE! No break here. Execution of TMR_WAIT_REL continues
685 * in the next case (TMR_WAIT_ABS)
694 prev_event_time = time;
696 request_sound_timer(time);
698 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold) {
702 if ((seq_sleep_flag.mode & WK_SLEEP)) {
703 seq_sleep_flag.mode = WK_WAKEUP;
713 seq_time = get_time();
728 if (seq_mode == SEQ_2)
729 seq_copy_to_input(event, 8);
731 parm = (parm << 8 | SEQ_ECHO);
732 seq_copy_to_input((u_char *) &parm, 4);
739 return TIMER_NOT_ARMED;
743 seq_local_event(u_char *event)
745 u_char cmd = event[1];
746 u_int parm = *((u_int *) &event[4]);
749 case LOCL_STARTAUDIO:
751 DMAbuf_start_devices(parm);
760 seq_sysex_message(u_char *event)
764 u_char *buf = &event[2];
766 if ((int) dev > max_synthdev)
768 if (!(synth_open_mask & (1 << dev)))
770 if (!synth_devs[dev])
772 if (!synth_devs[dev]->send_sysex)
776 for (i = 0; i < 6 && buf[i] != 0xff; i++)
780 synth_devs[dev]->send_sysex(dev, buf, l);
784 play_event(u_char *q)
787 * NOTE! This routine returns 0 = normal event played. 1 = Timer
788 * armed. Suspend playback until timer callback. 2 = MIDI output
789 * buffer full. Restore queue and suspend until timer
795 if (synth_open_mask & (1 << 0))
797 synth_devs[0]->kill_note(0, q[1], 255, q[3]);
801 if (q[4] < 128 || q[4] == 255)
802 if (synth_open_mask & (1 << 0))
804 synth_devs[0]->start_note(0, q[1], q[2], q[3]);
808 delay = (u_long *) q; /* Bytes 1 to 3 are
809 * containing the * delay in
811 *delay = (*delay >> 8) & 0xffffff;
818 prev_event_time = time;
820 request_sound_timer(time);
822 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold) {
826 if ((seq_sleep_flag.mode & WK_SLEEP)) {
827 seq_sleep_flag.mode = WK_WAKEUP;
833 * The timer is now active and will reinvoke this
834 * function after the timer expires. Return to the
842 if (synth_open_mask & (1 << 0))
844 synth_devs[0]->set_instr(0, q[1], q[2]);
847 case SEQ_SYNCTIMER: /* Reset timer */
848 seq_time = get_time();
853 case SEQ_MIDIPUTC: /* Put a midi character */
854 if (midi_opened[q[2]]) {
859 if (!midi_devs[dev]->putc(dev, q[1])) {
861 * Output FIFO is full. Wait one timer cycle and try again.
865 request_sound_timer(-1);
868 midi_written[dev] = 1;
873 seq_copy_to_input(q, 4); /* Echo back to the process */
877 if ((int) q[1] < max_synthdev)
878 synth_devs[q[1]]->hw_control(q[1], q);
886 seq_chn_voice_event(q);
890 seq_chn_common_event(q);
894 if (seq_timing_event(q) == TIMER_ARMED) {
904 seq_sysex_message(q);
917 int this_one, action;
922 qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
928 if ((action = play_event(&queue[this_one * EV_SZ]))) {
930 * Suspend playback. Next timer routine invokes this routine again
942 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold) {
946 if ((seq_sleep_flag.mode & WK_SLEEP)) {
947 seq_sleep_flag.mode = WK_WAKEUP;
955 reset_controllers(int dev, u_char *controller, int update_dev)
960 for (i = 0; i < 128; i++)
961 controller[i] = ctrl_def_values[i];
969 max_synthdev = num_synths;
971 for (dev = 0; dev < num_midis; dev++)
972 if (midi_devs[dev]->converter != NULL)
973 synth_devs[max_synthdev++] = midi_devs[dev]->converter;
974 for (dev = 0; dev < max_synthdev; dev++) {
977 for (chn = 0; chn < 16; chn++) {
978 synth_devs[dev]->chn_info[chn].pgm_num = 0;
979 reset_controllers(dev,
980 synth_devs[dev]->chn_info[chn].controllers, 0);
981 synth_devs[dev]->chn_info[chn].bender_value = (1<<7); /* Neutral */
990 sequencer_open(int dev, struct fileinfo * file)
996 level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1;
999 mode = file->mode & O_ACCMODE;
1001 DEB(printf("sequencer_open(dev=%d)\n", dev));
1003 if (!sequencer_ok) {
1004 printf("Soundcard: Sequencer not initialized\n");
1007 if (dev) { /* Patch manager device */
1010 printf("Patch manager interface is currently broken. Sorry\n");
1015 if (dev >= MAX_SYNTH_DEV)
1017 if (pmgr_present[dev])
1019 if ((err = pmgr_open(dev)) < 0)
1022 pmgr_present[dev] = 1;
1026 if (sequencer_busy) {
1027 printf("Sequencer busy\n");
1034 max_mididev = num_midis;
1035 max_synthdev = num_synths;
1036 pre_event_timeout = 0;
1039 if (pending_timer != -1) {
1040 tmr_no = pending_timer;
1043 if (tmr_no == -1) { /* Not selected yet */
1047 for (i = 0; i < num_sound_timers; i++)
1048 if (sound_timer_devs[i]->priority > best) {
1050 best = sound_timer_devs[i]->priority;
1052 if (tmr_no == -1) /* Should not be */
1055 tmr = sound_timer_devs[tmr_no];
1059 printf("sequencer: No timer for level 2\n");
1065 if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1067 printf("Sequencer: No Midi devices. Input not possible\n");
1071 if (!max_synthdev && !max_mididev)
1074 synth_open_mask = 0;
1076 for (i = 0; i < max_mididev; i++) {
1078 midi_written[i] = 0;
1082 * if (mode == OPEN_WRITE || mode == OPEN_READWRITE)
1084 for (i = 0; i < max_synthdev; i++) /* Open synth devices */
1085 if ((tmp = synth_devs[i]->open(i, mode)) < 0) {
1086 printf("Sequencer: Warning! Cannot open synth device #%d (%d)\n",
1088 if (synth_devs[i]->midi_dev)
1089 printf("(Maps to MIDI dev #%d)\n", synth_devs[i]->midi_dev);
1091 synth_open_mask |= (1 << i);
1092 if (synth_devs[i]->midi_dev) /* Is a midi interface */
1093 midi_opened[synth_devs[i]->midi_dev] = 1;
1096 seq_time = get_time();
1097 prev_input_time = 0;
1098 prev_event_time = 0;
1100 if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE)) {
1101 /* Initialize midi input devices */
1102 for (i = 0; i < max_mididev; i++)
1103 if (!midi_opened[i]) {
1104 if ((retval = midi_devs[i]->open(i, mode,
1105 sequencer_midi_input, sequencer_midi_output)) >= 0)
1109 if (seq_mode == SEQ_2) {
1110 tmr->open(tmr_no, seq_mode);
1112 seq_sleep_flag.aborting = 0;
1113 seq_sleep_flag.mode = WK_NONE;
1114 midi_sleep_flag.aborting = 0;
1115 midi_sleep_flag.mode = WK_NONE;
1116 output_treshold = SEQ_MAX_QUEUE / 2;
1118 for (i = 0; i < num_synths; i++)
1119 if (pmgr_present[i])
1120 pmgr_inform(i, PM_E_OPENED, 0, 0, 0, 0);
1126 seq_drain_midi_queues(void)
1131 * Give the Midi drivers time to drain their output queues
1136 while (!(seq_sleep_flag.aborting) && n) {
1139 for (i = 0; i < max_mididev; i++)
1140 if (midi_opened[i] && midi_written[i])
1141 if (midi_devs[i]->buffer_status != NULL)
1142 if (midi_devs[i]->buffer_status(i))
1146 * Let's have a delay
1152 DO_SLEEP(chn, seq_sleep_flag, hz / 10);
1159 sequencer_release(int dev, struct fileinfo * file)
1162 int mode = file->mode & O_ACCMODE;
1166 DEB(printf("sequencer_release(dev=%d)\n", dev));
1168 if (dev) { /* Patch manager device */
1171 pmgr_present[dev] = 0;
1175 * Wait until the queue is empty (if we don't have nonblock)
1178 if (mode != OPEN_READ && !0)
1179 while (!(seq_sleep_flag.aborting) && qlen) {
1183 if (mode != OPEN_READ)
1184 seq_drain_midi_queues(); /* Ensure the output queues are empty */
1186 if (mode != OPEN_READ)
1187 seq_drain_midi_queues(); /* Flush the all notes off messages */
1189 for (i = 0; i < max_synthdev; i++)
1190 if (synth_open_mask & (1 << i)) /* Actually opened */
1191 if (synth_devs[i]) {
1192 synth_devs[i]->close(i);
1194 if (synth_devs[i]->midi_dev)
1195 midi_opened[synth_devs[i]->midi_dev] = 0;
1197 for (i = 0; i < num_synths; i++)
1198 if (pmgr_present[i])
1199 pmgr_inform(i, PM_E_CLOSED, 0, 0, 0, 0);
1201 for (i = 0; i < max_mididev; i++)
1203 midi_devs[i]->close(i);
1205 if (seq_mode == SEQ_2)
1216 if (qlen && !seq_playing && !(seq_sleep_flag.aborting))
1220 if (qlen && !(seq_sleep_flag.mode & WK_SLEEP)) {
1224 DO_SLEEP(chn, seq_sleep_flag, 0);
1233 midi_outc(int dev, u_char data)
1236 * NOTE! Calls sleep(). Don't call this from interrupt.
1243 * This routine sends one byte to the Midi channel. If the output
1244 * Fifo is full, it waits until there is space in the queue
1247 n = 3 * hz; /* Timeout */
1250 while (n && !midi_devs[dev]->putc(dev, data)) {
1253 DO_SLEEP(chn, seq_sleep_flag, 4);
1264 * NOTE! Calls sleep(). Don't call this from interrupt.
1272 seq_time = get_time();
1273 prev_input_time = 0;
1274 prev_event_time = 0;
1276 qlen = qhead = qtail = 0;
1277 iqlen = iqhead = iqtail = 0;
1279 for (i = 0; i < max_synthdev; i++)
1280 if (synth_open_mask & (1 << i))
1282 synth_devs[i]->reset(i);
1284 if (seq_mode == SEQ_2) {
1285 for (chn = 0; chn < 16; chn++)
1286 for (i = 0; i < max_synthdev; i++)
1287 if ( (synth_open_mask & (1 << i)) && (synth_devs[i]) ) {
1288 synth_devs[i]->controller(i, chn,123,0);/* All notes off */
1289 synth_devs[i]->controller(i, chn,121,0);/* Reset all ctl */
1290 synth_devs[i]->bender(i, chn, 1 << 13); /* Bender off */
1293 } else { /* seq_mode == SEQ_1 */
1294 for (i = 0; i < max_mididev; i++)
1295 if (midi_written[i]) {
1296 /* Midi used. Some notes may still be playing */
1298 * Sending just a ACTIVE SENSING message
1299 * should be enough to stop all playing
1300 * notes. Since there are devices not
1301 * recognizing the active sensing, we have to
1302 * send some all notes off messages also.
1306 for (chn = 0; chn < 16; chn++) {
1307 midi_outc(i, (u_char) (0xb0 + (chn & 0x0f))); /* control change */
1308 midi_outc(i, 0x7b); /* All notes off */
1309 midi_outc(i, 0); /* Dummy parameter */
1312 midi_devs[i]->close(i);
1314 midi_written[i] = 0;
1322 if ((seq_sleep_flag.mode & WK_SLEEP)) {
1323 seq_sleep_flag.mode = WK_WAKEUP;
1324 wakeup(seq_sleeper);
1334 * This routine is called by the application in case the user wants
1335 * to reset the system to the default state.
1341 * Since some of the devices don't recognize the active sensing and
1342 * all notes off messages, we have to shut all notes manually.
1344 * TO BE IMPLEMENTED LATER
1348 * Also return the controllers to their default states
1353 sequencer_ioctl(int dev, struct fileinfo * file,
1354 u_int cmd, ioctl_arg arg)
1356 int midi_dev, orig_dev;
1357 int mode = file->mode & O_ACCMODE;
1359 orig_dev = dev = dev >> 4;
1362 case SNDCTL_TMR_TIMEBASE:
1363 case SNDCTL_TMR_TEMPO:
1364 case SNDCTL_TMR_START:
1365 case SNDCTL_TMR_STOP:
1366 case SNDCTL_TMR_CONTINUE:
1367 case SNDCTL_TMR_METRONOME:
1368 case SNDCTL_TMR_SOURCE:
1369 if (dev) /* Patch manager */
1372 if (seq_mode != SEQ_2)
1374 return tmr->ioctl(tmr_no, cmd, arg);
1377 case SNDCTL_TMR_SELECT:
1378 if (dev) /* Patch manager */
1381 if (seq_mode != SEQ_2)
1383 pending_timer = (*(int *) arg);
1385 if (pending_timer < 0 || pending_timer >= num_sound_timers) {
1389 return *(int *) arg = pending_timer;
1392 case SNDCTL_SEQ_PANIC:
1396 case SNDCTL_SEQ_SYNC:
1397 if (dev) /* Patch manager */
1400 if (mode == OPEN_READ)
1402 while (qlen && !(seq_sleep_flag.aborting))
1410 case SNDCTL_SEQ_RESET:
1411 if (dev) /* Patch manager */
1418 case SNDCTL_SEQ_TESTMIDI:
1419 if (dev) /* Patch manager */
1422 midi_dev = (*(int *) arg);
1423 if (midi_dev >= max_mididev)
1426 if (!midi_opened[midi_dev]) {
1429 mode = file->mode & O_ACCMODE;
1430 if ((err = midi_devs[midi_dev]->open(midi_dev, mode,
1431 sequencer_midi_input, sequencer_midi_output)) < 0)
1434 midi_opened[midi_dev] = 1;
1439 case SNDCTL_SEQ_GETINCOUNT:
1440 if (dev) /* Patch manager */
1443 if (mode == OPEN_WRITE)
1445 return *(int *) arg = iqlen;
1448 case SNDCTL_SEQ_GETOUTCOUNT:
1450 if (mode == OPEN_READ)
1452 return *(int *) arg = SEQ_MAX_QUEUE - qlen;
1455 case SNDCTL_SEQ_CTRLRATE:
1456 if (dev) /* Patch manager */
1460 * If *arg == 0, just return the current rate
1462 if (seq_mode == SEQ_2)
1463 return tmr->ioctl(tmr_no, cmd, arg);
1465 if ((*(int *) arg) != 0)
1468 return *(int *) arg = hz;
1471 case SNDCTL_SEQ_RESETSAMPLES:
1475 dev = (*(int *) arg);
1476 if (dev < 0 || dev >= num_synths)
1478 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1480 if (!orig_dev && pmgr_present[dev])
1481 pmgr_inform(dev, PM_E_PATCH_RESET, 0, 0, 0, 0);
1483 err = synth_devs[dev]->ioctl(dev, cmd, arg);
1488 case SNDCTL_SEQ_NRSYNTHS:
1489 return *(int *) arg = max_synthdev;
1492 case SNDCTL_SEQ_NRMIDIS:
1493 return *(int *) arg = max_mididev;
1496 case SNDCTL_SYNTH_MEMAVL:
1498 int dev = (*(int *) arg);
1500 if (dev < 0 || dev >= num_synths)
1503 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1506 return *(int *) arg = synth_devs[dev]->ioctl(dev, cmd, arg);
1510 case SNDCTL_FM_4OP_ENABLE:
1512 int dev = (*(int *) arg);
1514 if (dev < 0 || dev >= num_synths)
1517 if (!(synth_open_mask & (1 << dev)))
1520 synth_devs[dev]->ioctl(dev, cmd, arg);
1525 case SNDCTL_SYNTH_INFO:
1527 struct synth_info inf;
1530 bcopy(&(((char *) arg)[0]), (char *) &inf, sizeof(inf));
1533 if (dev < 0 || dev >= max_synthdev)
1536 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1539 return synth_devs[dev]->ioctl(dev, cmd, arg);
1543 case SNDCTL_SEQ_OUTOFBAND:
1545 struct seq_event_rec event;
1548 bcopy(&(((char *) arg)[0]), (char *) &event, sizeof(event));
1551 play_event(event.arr);
1558 case SNDCTL_MIDI_INFO:
1560 struct midi_info inf;
1563 bcopy(&(((char *) arg)[0]), (char *) &inf, sizeof(inf));
1566 if (dev < 0 || dev >= max_mididev)
1569 bcopy((char *) &(midi_devs[dev]->info), &(((char *) arg)[0]), sizeof(inf));
1574 case SNDCTL_PMGR_IFACE:
1576 struct patmgr_info *inf;
1579 if ((inf = (struct patmgr_info *) malloc(sizeof(*inf), M_TEMP, M_WAITOK)) == NULL) {
1580 printf("patmgr: Can't allocate memory for a message\n");
1583 bcopy(&(((char *) arg)[0]), (char *) inf, sizeof(*inf));
1586 if (dev < 0 || dev >= num_synths) {
1590 if (!synth_devs[dev]->pmgr_interface) {
1594 if ((err = synth_devs[dev]->pmgr_interface(dev, inf)) == -1) {
1598 bcopy((char *) inf, &(((char *) arg)[0]), sizeof(*inf));
1604 case SNDCTL_PMGR_ACCESS:
1606 struct patmgr_info *inf;
1609 if ((inf = (struct patmgr_info *) malloc(sizeof(*inf), M_TEMP, M_WAITOK)) == NULL) {
1610 printf("patmgr: Can't allocate memory for a message\n");
1613 bcopy(&(((char *) arg)[0]), (char *) inf, sizeof(*inf));
1616 if (dev < 0 || dev >= num_synths) {
1620 if (!pmgr_present[dev]) {
1624 if ((err = pmgr_access(dev, inf)) < 0) {
1628 bcopy((char *) inf, &(((char *) arg)[0]), sizeof(*inf));
1634 case SNDCTL_SEQ_THRESHOLD:
1636 int tmp = (*(int *) arg);
1638 if (dev)/* Patch manager */
1643 if (tmp >= SEQ_MAX_QUEUE)
1644 tmp = SEQ_MAX_QUEUE - 1;
1645 output_treshold = tmp;
1650 case SNDCTL_MIDI_PRETIME:
1652 int val = (*(int *) arg);
1657 val = (hz * val) / 10;
1658 pre_event_timeout = val;
1659 return *(int *) arg = val;
1664 if (dev) /* Patch manager */
1667 if (mode == OPEN_READ)
1672 if (!(synth_open_mask & (1 << 0)))
1674 return synth_devs[0]->ioctl(0, cmd, arg);
1683 sequencer_poll (int dev, struct fileinfo *file, int events, select_table * wait)
1685 unsigned long flags;
1692 if (events & (POLLIN | POLLRDNORM)) {
1694 selrecord(wait, &selinfo[dev]);
1696 revents |= events & (POLLIN | POLLRDNORM);
1697 midi_sleep_flag.mode &= ~WK_SLEEP;
1700 if (events & (POLLOUT | POLLWRNORM)) {
1701 if (qlen >= SEQ_MAX_QUEUE)
1702 selrecord(wait, &selinfo[dev]);
1704 revents |= events & (POLLOUT | POLLWRNORM);
1705 seq_sleep_flag.mode &= ~WK_SLEEP;
1717 sequencer_timer(void *dummy)
1723 note_to_freq(int note_num)
1727 * This routine converts a midi note to a frequency (multiplied by
1731 int note, octave, note_freq;
1734 261632, 277189, 293671, 311132, 329632, 349232,
1735 369998, 391998, 415306, 440000, 466162, 493880
1738 #define BASE_OCTAVE 5
1740 octave = note_num / 12;
1741 note = note_num % 12;
1743 note_freq = notes[note];
1745 if (octave < BASE_OCTAVE)
1746 note_freq >>= (BASE_OCTAVE - octave);
1747 else if (octave > BASE_OCTAVE)
1748 note_freq <<= (octave - BASE_OCTAVE);
1758 compute_finetune(u_long base_freq, int bend, int range)
1761 int negative, semitones, cents, multiplier = 1;
1774 bend = bend * range / 8192;
1778 negative = bend < 0 ? 1 : 0;
1786 * if (bend > 2399) bend = 2399;
1788 while (bend > 2399) {
1793 semitones = bend / 100;
1796 amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents])
1800 return (base_freq * 10000) / amount; /* Bend down */
1802 return (base_freq * amount) / 10000; /* Bend up */
1812 queue = (u_char *) malloc(SEQ_MAX_QUEUE * EV_SZ, M_DEVBUF, M_NOWAIT);
1814 panic("SOUND: Cannot allocate memory\n");
1816 iqueue = (u_char *) malloc(SEQ_MAX_QUEUE * IEV_SZ, M_DEVBUF, M_NOWAIT);
1818 panic("SOUND: Cannot allocate memory\n");