This source file includes following definitions.
- sequencer_read
- sequencer_midi_output
- seq_copy_to_input
- sequencer_midi_input
- seq_input_event
- sequencer_write
- seq_queue
- extended_event
- find_voice
- alloc_voice
- seq_chn_voice_event
- seq_chn_common_event
- seq_timing_event
- seq_local_event
- seq_startplay
- reset_controllers
- setup_mode2
- sequencer_open
- seq_drain_midi_queues
- sequencer_release
- seq_sync
- midi_outc
- seq_reset
- seq_panic
- sequencer_ioctl
- sequencer_select
- sequencer_timer
- note_to_freq
- compute_finetune
- sequencer_init
- sequencer_read
- sequencer_write
- sequencer_open
- sequencer_release
- sequencer_ioctl
- sequencer_lseek
- sequencer_init
- sequencer_select
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 #define SEQUENCER_C
31 #include "sound_config.h"
32
33 #ifdef CONFIGURE_SOUNDCARD
34
35 #ifndef EXCLUDE_SEQUENCER
36
37 static int sequencer_ok = 0;
38 static struct sound_timer_operations *tmr;
39 static int tmr_no = -1;
40 static int pending_timer = -1;
41
42
43
44
45
46 static int max_mididev = 0;
47 static int max_synthdev = 0;
48
49
50
51
52
53
54
55 #define SEQ_1 1
56 #define SEQ_2 2
57 static int seq_mode = SEQ_1;
58
59 DEFINE_WAIT_QUEUE (seq_sleeper, seq_sleep_flag);
60 DEFINE_WAIT_QUEUE (midi_sleeper, midi_sleep_flag);
61
62 static int midi_opened[MAX_MIDI_DEV] =
63 {0};
64 static int midi_written[MAX_MIDI_DEV] =
65 {0};
66
67 unsigned long prev_input_time = 0;
68 int prev_event_time;
69 unsigned long seq_time = 0;
70
71 #include "tuning.h"
72
73 #define EV_SZ 8
74 #define IEV_SZ 8
75 static unsigned char *queue = NULL;
76 static unsigned char *iqueue = NULL;
77
78 static volatile int qhead = 0, qtail = 0, qlen = 0;
79 static volatile int iqhead = 0, iqtail = 0, iqlen = 0;
80 static volatile int seq_playing = 0;
81 static int sequencer_busy = 0;
82 static int output_treshold;
83 static int pre_event_timeout;
84 static unsigned synth_open_mask;
85
86 static int seq_queue (unsigned char *note);
87 static void seq_startplay (void);
88 static int seq_sync (void);
89 static void seq_reset (void);
90 static int pmgr_present[MAX_SYNTH_DEV] =
91 {0};
92
93 #if MAX_SYNTH_DEV > 15
94 #error Too many synthesizer devices enabled.
95 #endif
96
97 int
98 sequencer_read (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
99 {
100 int c = count, p = 0;
101 int ev_len;
102 unsigned long flags;
103
104 dev = dev >> 4;
105
106 ev_len = seq_mode == SEQ_1 ? 4 : 8;
107
108 if (dev)
109
110
111 return pmgr_read (dev - 1, file, buf, count);
112
113 DISABLE_INTR (flags);
114 if (!iqlen)
115 {
116 DO_SLEEP (midi_sleeper, midi_sleep_flag, pre_event_timeout);
117
118 if (!iqlen)
119 {
120 RESTORE_INTR (flags);
121 return 0;
122 }
123 }
124
125 while (iqlen && c >= ev_len)
126 {
127
128 COPY_TO_USER (buf, p, &iqueue[iqhead * IEV_SZ], ev_len);
129 p += ev_len;
130 c -= ev_len;
131
132 iqhead = (iqhead + 1) % SEQ_MAX_QUEUE;
133 iqlen--;
134 }
135 RESTORE_INTR (flags);
136
137 return count - c;
138 }
139
140 static void
141 sequencer_midi_output (int dev)
142 {
143
144
145
146 }
147
148 void
149 seq_copy_to_input (unsigned char *event, int len)
150 {
151 unsigned long flags;
152
153
154
155
156
157 if (len != 4 && len != 8)
158 return;
159 if ((seq_mode == SEQ_1) != (len == 4))
160 return;
161
162 if (iqlen >= (SEQ_MAX_QUEUE - 1))
163 return;
164
165 DISABLE_INTR (flags);
166 memcpy (&iqueue[iqtail * IEV_SZ], event, len);
167 iqlen++;
168 iqtail = (iqtail + 1) % SEQ_MAX_QUEUE;
169
170 if (SOMEONE_WAITING (midi_sleeper, midi_sleep_flag))
171 {
172 WAKE_UP (midi_sleeper, midi_sleep_flag);
173 }
174 RESTORE_INTR (flags);
175 }
176
177 static void
178 sequencer_midi_input (int dev, unsigned char data)
179 {
180 unsigned int tstamp;
181 unsigned char event[4];
182
183 if (data == 0xfe)
184 return;
185
186 tstamp = GET_TIME () - seq_time;
187 if (tstamp != prev_input_time)
188 {
189 tstamp = (tstamp << 8) | SEQ_WAIT;
190
191 seq_copy_to_input ((unsigned char *) &tstamp, 4);
192 prev_input_time = tstamp;
193 }
194
195 event[0] = SEQ_MIDIPUTC;
196 event[1] = data;
197 event[2] = dev;
198 event[3] = 0;
199
200 seq_copy_to_input (event, 4);
201 }
202
203 void
204 seq_input_event (unsigned char *event, int len)
205 {
206 unsigned long this_time;
207
208 if (seq_mode == SEQ_2)
209 this_time = tmr->get_time (tmr_no);
210 else
211 this_time = GET_TIME () - seq_time;
212
213 if (this_time != prev_input_time)
214 {
215 unsigned char tmp_event[8];
216
217 tmp_event[0] = EV_TIMING;
218 tmp_event[1] = TMR_WAIT_ABS;
219 tmp_event[2] = 0;
220 tmp_event[3] = 0;
221 *(unsigned long *) &tmp_event[4] = this_time;
222
223 seq_copy_to_input (tmp_event, 8);
224 prev_input_time = this_time;
225 }
226
227 seq_copy_to_input (event, len);
228 }
229
230 int
231 sequencer_write (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
232 {
233 unsigned char event[EV_SZ], ev_code;
234 int p = 0, c, ev_size;
235 int err;
236 int mode = file->mode & O_ACCMODE;
237
238 dev = dev >> 4;
239
240 DEB (printk ("sequencer_write(dev=%d, count=%d)\n", dev, count));
241
242 if (mode == OPEN_READ)
243 return RET_ERROR (EIO);
244
245 if (dev)
246
247
248 return pmgr_write (dev - 1, file, buf, count);
249
250 c = count;
251
252 while (c >= 4)
253 {
254 COPY_FROM_USER (event, buf, p, 4);
255 ev_code = event[0];
256
257 if (ev_code == SEQ_FULLSIZE)
258 {
259 int err;
260
261 dev = *(unsigned short *) &event[2];
262 if (dev < 0 || dev >= max_synthdev)
263 return RET_ERROR (ENXIO);
264
265 if (!(synth_open_mask & (1 << dev)))
266 return RET_ERROR (ENXIO);
267
268 err = synth_devs[dev]->load_patch (dev, *(short *) &event[0], buf, p + 4, c, 0);
269 if (err < 0)
270 return err;
271
272 return err;
273 }
274
275 if (ev_code >= 128)
276 {
277 if (seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED)
278 {
279 printk ("Sequencer: Invalid level 2 event %x\n", ev_code);
280 return RET_ERROR (EINVAL);
281 }
282
283 ev_size = 8;
284
285 if (c < ev_size)
286 {
287 if (!seq_playing)
288 seq_startplay ();
289 return count - c;
290 }
291
292 COPY_FROM_USER (&event[4], buf, p + 4, 4);
293
294 }
295 else
296 {
297 if (seq_mode == SEQ_2)
298 {
299 printk ("Sequencer: 4 byte event in level 2 mode\n");
300 return RET_ERROR (EINVAL);
301 }
302 ev_size = 4;
303 }
304
305 if (event[0] == SEQ_MIDIPUTC)
306 {
307
308 if (!midi_opened[event[2]])
309 {
310 int mode;
311 int dev = event[2];
312
313 if (dev >= max_mididev)
314 {
315 printk ("Sequencer Error: Nonexistent MIDI device %d\n", dev);
316 return RET_ERROR (ENXIO);
317 }
318
319 mode = file->mode & O_ACCMODE;
320
321 if ((err = midi_devs[dev]->open (dev, mode,
322 sequencer_midi_input, sequencer_midi_output)) < 0)
323 {
324 seq_reset ();
325 printk ("Sequencer Error: Unable to open Midi #%d\n", dev);
326 return err;
327 }
328
329 midi_opened[dev] = 1;
330 }
331
332 }
333
334 if (!seq_queue (event))
335 {
336
337 if (!seq_playing)
338 seq_startplay ();
339 return count - c;
340 }
341
342 p += ev_size;
343 c -= ev_size;
344 }
345
346 if (!seq_playing)
347 seq_startplay ();
348
349 return count;
350 }
351
352 static int
353 seq_queue (unsigned char *note)
354 {
355
356
357
358
359
360 if (qlen >= SEQ_MAX_QUEUE)
361 if (!seq_playing)
362 seq_startplay ();
363
364
365
366 if (qlen >= SEQ_MAX_QUEUE && !SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
367 {
368
369
370
371 DO_SLEEP (seq_sleeper, seq_sleep_flag, 0);
372 }
373
374 if (qlen >= SEQ_MAX_QUEUE)
375 return 0;
376
377
378
379 memcpy (&queue[qtail * EV_SZ], note, EV_SZ);
380
381 qtail = (qtail + 1) % SEQ_MAX_QUEUE;
382 qlen++;
383
384 return 1;
385 }
386
387 static int
388 extended_event (unsigned char *q)
389 {
390 int dev = q[2];
391
392 if (dev < 0 || dev >= max_synthdev)
393 return RET_ERROR (ENXIO);
394
395 if (!(synth_open_mask & (1 << dev)))
396 return RET_ERROR (ENXIO);
397
398 switch (q[1])
399 {
400 case SEQ_NOTEOFF:
401 synth_devs[dev]->kill_note (dev, q[3], q[4], q[5]);
402 break;
403
404 case SEQ_NOTEON:
405 if (q[4] > 127 && q[4] != 255)
406 return 0;
407
408 synth_devs[dev]->start_note (dev, q[3], q[4], q[5]);
409 break;
410
411 case SEQ_PGMCHANGE:
412 synth_devs[dev]->set_instr (dev, q[3], q[4]);
413 break;
414
415 case SEQ_AFTERTOUCH:
416 synth_devs[dev]->aftertouch (dev, q[3], q[4]);
417 break;
418
419 case SEQ_BALANCE:
420 synth_devs[dev]->panning (dev, q[3], (char) q[4]);
421 break;
422
423 case SEQ_CONTROLLER:
424 synth_devs[dev]->controller (dev, q[3], q[4], *(short *) &q[5]);
425 break;
426
427 case SEQ_VOLMODE:
428 if (synth_devs[dev]->volume_method != NULL)
429 synth_devs[dev]->volume_method (dev, q[3]);
430 break;
431
432 default:
433 return RET_ERROR (EINVAL);
434 }
435
436 return 0;
437 }
438
439 static int
440 find_voice (int dev, int chn, int note)
441 {
442 unsigned short key;
443 int i;
444
445 key = (chn << 8) | (note + 1);
446
447 for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
448 if (synth_devs[dev]->alloc.map[i] == key)
449 return i;
450
451 return -1;
452 }
453
454 static int
455 alloc_voice (int dev, int chn, int note)
456 {
457 unsigned short key;
458 int voice;
459
460 key = (chn << 8) | (note + 1);
461
462 voice = synth_devs[dev]->alloc_voice (dev, chn, note,
463 &synth_devs[dev]->alloc);
464 synth_devs[dev]->alloc.map[voice] = key;
465 return voice;
466 }
467
468 static void
469 seq_chn_voice_event (unsigned char *event)
470 {
471 unsigned char dev = event[1];
472 unsigned char cmd = event[2];
473 unsigned char chn = event[3];
474 unsigned char note = event[4];
475 unsigned char parm = event[5];
476 int voice = -1;
477
478 if ((int) dev > max_synthdev)
479 return;
480 if (!(synth_open_mask & (1 << dev)))
481 return;
482 if (!synth_devs[dev])
483 return;
484
485 if (seq_mode == SEQ_2)
486 if (synth_devs[dev]->alloc_voice)
487 voice = find_voice (dev, chn, note);
488
489 if (cmd == MIDI_NOTEON && parm == 0)
490 {
491 cmd = MIDI_NOTEOFF;
492 parm = 64;
493 }
494
495 switch (cmd)
496 {
497 case MIDI_NOTEON:
498 if (note > 127)
499 return;
500
501 if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice)
502 {
503 voice = alloc_voice (dev, chn, note);
504 }
505
506 if (voice == -1)
507 voice = chn;
508
509 if (seq_mode == SEQ_2)
510 {
511 synth_devs[dev]->set_instr (dev, voice,
512 synth_devs[dev]->chn_info[chn].pgm_num);
513 }
514
515 synth_devs[dev]->start_note (dev, voice, note, parm);
516 break;
517
518 case MIDI_NOTEOFF:
519 if (voice == -1)
520 voice = chn;
521 synth_devs[dev]->kill_note (dev, voice, note, parm);
522 break;
523
524 case MIDI_KEY_PRESSURE:
525
526 break;
527
528 default:;
529 }
530 }
531
532 static void
533 seq_chn_common_event (unsigned char *event)
534 {
535 unsigned char dev = event[1];
536 unsigned char cmd = event[2];
537 unsigned char chn = event[3];
538 unsigned char p1 = event[4];
539
540
541 unsigned short w14 = *(short *) &event[6];
542
543 if ((int) dev > max_synthdev)
544 return;
545 if (!(synth_open_mask & (1 << dev)))
546 return;
547 if (!synth_devs[dev])
548 return;
549
550 switch (cmd)
551 {
552 case MIDI_PGM_CHANGE:
553 if (seq_mode == SEQ_2)
554 {
555 synth_devs[dev]->chn_info[chn].pgm_num = p1;
556 }
557 else
558 synth_devs[dev]->set_instr (dev, chn, p1);
559 break;
560
561 case MIDI_CTL_CHANGE:
562 if (p1 == CTRL_MAIN_VOLUME)
563 {
564 w14 = (unsigned short) (((int) w14 * 16383) / 100);
565 p1 = CTL_MAIN_VOLUME;
566 }
567 if (p1 == CTRL_EXPRESSION)
568 {
569 w14 *= 128;
570 p1 = CTL_EXPRESSION;
571 }
572
573 if (seq_mode == SEQ_2)
574 {
575 if (chn > 15 || p1 > 127)
576 break;
577
578 synth_devs[dev]->chn_info[chn].controllers[p1] = w14 & 0xff;
579
580 if (dev < num_synths)
581 {
582 int val = w14 & 0xff;
583
584 if (p1 < 64)
585 {
586 val = ((synth_devs[dev]->
587 chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
588 | (synth_devs[dev]->
589 chn_info[chn].controllers[p1 | 32] & 0x7f);
590 p1 &= ~32;
591 }
592 else
593 val = synth_devs[dev]->chn_info[chn].controllers[p1];
594
595 synth_devs[dev]->controller (dev, chn, p1, val);
596 }
597 else
598 synth_devs[dev]->controller (dev, chn, p1, w14);
599 }
600 else
601 synth_devs[dev]->controller (dev, chn, p1, w14);
602 break;
603
604 case MIDI_PITCH_BEND:
605 synth_devs[dev]->bender (dev, chn, w14);
606 break;
607
608 default:;
609 }
610 }
611
612 static int
613 seq_timing_event (unsigned char *event)
614 {
615 unsigned char cmd = event[1];
616 unsigned int parm = *(int *) &event[4];
617
618 if (seq_mode == SEQ_2)
619 {
620 int ret;
621
622 if ((ret = tmr->event (tmr_no, event)) == TIMER_ARMED)
623 {
624 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
625 {
626 unsigned long flags;
627
628 DISABLE_INTR (flags);
629 if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
630 {
631 WAKE_UP (seq_sleeper, seq_sleep_flag);
632 }
633 RESTORE_INTR (flags);
634 }
635 }
636 return ret;
637 }
638
639 switch (cmd)
640 {
641 case TMR_WAIT_REL:
642 parm += prev_event_time;
643
644
645
646
647
648
649 case TMR_WAIT_ABS:
650 if (parm > 0)
651 {
652 long time;
653
654 seq_playing = 1;
655 time = parm;
656 prev_event_time = time;
657
658 request_sound_timer (time);
659
660 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
661 {
662 unsigned long flags;
663
664 DISABLE_INTR (flags);
665 if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
666 {
667 WAKE_UP (seq_sleeper, seq_sleep_flag);
668 }
669 RESTORE_INTR (flags);
670 }
671
672 return TIMER_ARMED;
673 }
674 break;
675
676 case TMR_START:
677 seq_time = GET_TIME ();
678 prev_input_time = 0;
679 prev_event_time = 0;
680 break;
681
682 case TMR_STOP:
683 break;
684
685 case TMR_CONTINUE:
686 break;
687
688 case TMR_TEMPO:
689 break;
690
691 case TMR_ECHO:
692 if (seq_mode == SEQ_2)
693 seq_copy_to_input (event, 8);
694 else
695 {
696 parm = (parm << 8 | SEQ_ECHO);
697 seq_copy_to_input ((unsigned char *) &parm, 4);
698 }
699 break;
700
701 default:;
702 }
703
704 return TIMER_NOT_ARMED;
705 }
706
707 static void
708 seq_local_event (unsigned char *event)
709 {
710
711
712 printk ("seq_local_event() called. WHY????????\n");
713 }
714
715 static void
716 seq_startplay (void)
717 {
718 int this_one;
719 unsigned long *delay;
720 unsigned char *q;
721
722 while (qlen > 0)
723 {
724
725 seq_playing = 1;
726
727 qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
728 qlen--;
729
730 q = &queue[this_one * EV_SZ];
731
732 switch (q[0])
733 {
734 case SEQ_NOTEOFF:
735 if (synth_open_mask & (1 << 0))
736 if (synth_devs[0])
737 synth_devs[0]->kill_note (0, q[1], 255, q[3]);
738 break;
739
740 case SEQ_NOTEON:
741 if (q[4] < 128 || q[4] == 255)
742 if (synth_open_mask & (1 << 0))
743 if (synth_devs[0])
744 synth_devs[0]->start_note (0, q[1], q[2], q[3]);
745 break;
746
747 case SEQ_WAIT:
748 delay = (unsigned long *) q;
749
750
751
752 *delay = (*delay >> 8) & 0xffffff;
753
754 if (*delay > 0)
755 {
756 long time;
757
758 seq_playing = 1;
759 time = *delay;
760 prev_event_time = time;
761
762 request_sound_timer (time);
763
764 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
765 {
766 unsigned long flags;
767
768 DISABLE_INTR (flags);
769 if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
770 {
771 WAKE_UP (seq_sleeper, seq_sleep_flag);
772 }
773 RESTORE_INTR (flags);
774 }
775
776
777
778
779 return;
780 }
781 break;
782
783 case SEQ_PGMCHANGE:
784 if (synth_open_mask & (1 << 0))
785 if (synth_devs[0])
786 synth_devs[0]->set_instr (0, q[1], q[2]);
787 break;
788
789 case SEQ_SYNCTIMER:
790
791
792 seq_time = GET_TIME ();
793 prev_input_time = 0;
794 prev_event_time = 0;
795 break;
796
797 case SEQ_MIDIPUTC:
798
799
800 if (midi_opened[q[2]])
801 {
802 int dev;
803
804 dev = q[2];
805
806 if (!midi_devs[dev]->putc (dev, q[1]))
807 {
808
809
810
811
812 qlen++;
813 qhead = this_one;
814
815
816 seq_playing = 1;
817 request_sound_timer (-1);
818 return;
819 }
820 else
821 midi_written[dev] = 1;
822 }
823 break;
824
825 case SEQ_ECHO:
826 seq_copy_to_input (q, 4);
827
828
829 break;
830
831 case SEQ_PRIVATE:
832 if ((int) q[1] < max_synthdev)
833 synth_devs[q[1]]->hw_control (q[1], q);
834 break;
835
836 case SEQ_EXTENDED:
837 extended_event (q);
838 break;
839
840 case EV_CHN_VOICE:
841 seq_chn_voice_event (q);
842 break;
843
844 case EV_CHN_COMMON:
845 seq_chn_common_event (q);
846 break;
847
848 case EV_TIMING:
849 if (seq_timing_event (q) == TIMER_ARMED)
850 {
851 return;
852 }
853 break;
854
855 case EV_SEQ_LOCAL:
856 seq_local_event (q);
857 break;
858
859 default:;
860 }
861
862 }
863
864 seq_playing = 0;
865
866 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
867 {
868 unsigned long flags;
869
870 DISABLE_INTR (flags);
871 if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
872 {
873 WAKE_UP (seq_sleeper, seq_sleep_flag);
874 }
875 RESTORE_INTR (flags);
876 }
877
878 }
879
880 static void
881 reset_controllers (int dev, unsigned char *controller, int update_dev)
882 {
883 #include "midi_ctrl.h"
884
885 int i;
886
887 for (i = 0; i < 128; i++)
888 controller[i] = ctrl_def_values[i];
889 }
890
891 static void
892 setup_mode2 (void)
893 {
894 int dev;
895
896 max_synthdev = num_synths;
897
898 for (dev = 0; dev < num_midis; dev++)
899 if (midi_devs[dev]->converter != NULL)
900 {
901 synth_devs[max_synthdev++] =
902 midi_devs[dev]->converter;
903 }
904
905 for (dev = 0; dev < max_synthdev; dev++)
906 {
907 int chn;
908
909 for (chn = 0; chn < 16; chn++)
910 {
911 synth_devs[dev]->chn_info[chn].pgm_num = 0;
912 reset_controllers (dev,
913 synth_devs[dev]->chn_info[chn].controllers,
914 0);
915 }
916 }
917
918 max_mididev = 0;
919 seq_mode = SEQ_2;
920 }
921
922 int
923 sequencer_open (int dev, struct fileinfo *file)
924 {
925 int retval, mode, i;
926 int level, tmp;
927
928 level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1;
929
930 dev = dev >> 4;
931 mode = file->mode & O_ACCMODE;
932
933 DEB (printk ("sequencer_open(dev=%d)\n", dev));
934
935 if (!sequencer_ok)
936 {
937 printk ("Soundcard: Sequencer not initialized\n");
938 return RET_ERROR (ENXIO);
939 }
940
941 if (dev)
942
943
944 {
945 int err;
946
947 dev--;
948
949 if (dev >= MAX_SYNTH_DEV)
950 return RET_ERROR (ENXIO);
951 if (pmgr_present[dev])
952 return RET_ERROR (EBUSY);
953 if ((err = pmgr_open (dev)) < 0)
954 return err;
955
956
957
958 pmgr_present[dev] = 1;
959 return err;
960 }
961
962 if (sequencer_busy)
963 {
964 printk ("Sequencer busy\n");
965 return RET_ERROR (EBUSY);
966 }
967
968 max_mididev = num_midis;
969 max_synthdev = num_synths;
970 pre_event_timeout = 0;
971 seq_mode = SEQ_1;
972
973 if (pending_timer != -1)
974 {
975 tmr_no = pending_timer;
976 pending_timer = -1;
977 }
978
979 if (tmr_no == -1)
980 {
981 int i, best;
982
983 best = -1;
984 for (i = 0; i < num_sound_timers; i++)
985 if (sound_timer_devs[i]->priority > best)
986 {
987 tmr_no = i;
988 best = sound_timer_devs[i]->priority;
989 }
990
991 if (tmr_no == -1)
992 tmr_no = 0;
993 }
994
995 tmr = sound_timer_devs[tmr_no];
996
997 if (level == 2)
998 {
999 printk ("Using timer #%d\n", tmr_no);
1000 if (tmr == NULL)
1001 {
1002 printk ("sequencer: No timer for level 2\n");
1003 return RET_ERROR (ENXIO);
1004 }
1005 setup_mode2 ();
1006 }
1007
1008 if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1009 if (!max_mididev)
1010 {
1011 printk ("Sequencer: No Midi devices. Input not possible\n");
1012 return RET_ERROR (ENXIO);
1013 }
1014
1015 if (!max_synthdev && !max_mididev)
1016 return RET_ERROR (ENXIO);
1017
1018 synth_open_mask = 0;
1019
1020 for (i = 0; i < max_mididev; i++)
1021 {
1022 midi_opened[i] = 0;
1023 midi_written[i] = 0;
1024 }
1025
1026
1027
1028
1029 for (i = 0; i < max_synthdev; i++)
1030
1031
1032 if ((tmp = synth_devs[i]->open (i, mode)) < 0)
1033 {
1034 printk ("Sequencer: Warning! Cannot open synth device #%d (%d)\n", i, tmp);
1035 if (synth_devs[i]->midi_dev)
1036 printk ("(Maps to midi dev #%d\n", synth_devs[i]->midi_dev);
1037 }
1038 else
1039 {
1040 synth_open_mask |= (1 << i);
1041 if (synth_devs[i]->midi_dev)
1042
1043
1044 midi_opened[synth_devs[i]->midi_dev] = 1;
1045 }
1046
1047 seq_time = GET_TIME ();
1048 prev_input_time = 0;
1049 prev_event_time = 0;
1050
1051 if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
1052 {
1053
1054
1055 for (i = 0; i < max_mididev; i++)
1056 if (!midi_opened[i])
1057 {
1058 if ((retval = midi_devs[i]->open (i, mode,
1059 sequencer_midi_input, sequencer_midi_output)) >= 0)
1060 midi_opened[i] = 1;
1061 }
1062 }
1063
1064 if (seq_mode == SEQ_2)
1065 {
1066 tmr->open (tmr_no, seq_mode);
1067 }
1068
1069 sequencer_busy = 1;
1070 RESET_WAIT_QUEUE (seq_sleeper, seq_sleep_flag);
1071 RESET_WAIT_QUEUE (midi_sleeper, midi_sleep_flag);
1072 output_treshold = SEQ_MAX_QUEUE / 2;
1073
1074 for (i = 0; i < num_synths; i++)
1075 if (pmgr_present[i])
1076 pmgr_inform (i, PM_E_OPENED, 0, 0, 0, 0);
1077
1078 return 0;
1079 }
1080
1081 void
1082 seq_drain_midi_queues (void)
1083 {
1084 int i, n;
1085
1086
1087
1088
1089
1090 n = 1;
1091
1092 while (!PROCESS_ABORTING (midi_sleeper, midi_sleep_flag) && n)
1093 {
1094 n = 0;
1095
1096 for (i = 0; i < max_mididev; i++)
1097 if (midi_opened[i] && midi_written[i])
1098 if (midi_devs[i]->buffer_status != NULL)
1099 if (midi_devs[i]->buffer_status (i))
1100 n++;
1101
1102
1103
1104
1105 if (n)
1106 {
1107 DO_SLEEP (seq_sleeper, seq_sleep_flag, HZ / 10);
1108 }
1109 }
1110 }
1111
1112 void
1113 sequencer_release (int dev, struct fileinfo *file)
1114 {
1115 int i;
1116 int mode = file->mode & O_ACCMODE;
1117
1118 dev = dev >> 4;
1119
1120 DEB (printk ("sequencer_release(dev=%d)\n", dev));
1121
1122 if (dev)
1123
1124
1125 {
1126 dev--;
1127 pmgr_release (dev);
1128 pmgr_present[dev] = 0;
1129 return;
1130 }
1131
1132
1133
1134
1135
1136 if (mode != OPEN_READ)
1137 while (!PROCESS_ABORTING (seq_sleeper, seq_sleep_flag) && qlen)
1138 {
1139 seq_sync ();
1140 }
1141
1142 if (mode != OPEN_READ)
1143 seq_drain_midi_queues ();
1144
1145
1146 seq_reset ();
1147 if (mode != OPEN_READ)
1148 seq_drain_midi_queues ();
1149
1150
1151
1152 for (i = 0; i < max_synthdev; i++)
1153 if (synth_open_mask & (1 << i))
1154
1155
1156 if (synth_devs[i])
1157 {
1158 synth_devs[i]->close (i);
1159
1160 if (synth_devs[i]->midi_dev)
1161 midi_opened[synth_devs[i]->midi_dev] = 0;
1162 }
1163
1164 for (i = 0; i < num_synths; i++)
1165 if (pmgr_present[i])
1166 pmgr_inform (i, PM_E_CLOSED, 0, 0, 0, 0);
1167
1168 for (i = 0; i < max_mididev; i++)
1169 if (midi_opened[i])
1170 midi_devs[i]->close (i);
1171
1172 if (seq_mode == SEQ_2)
1173 tmr->close (tmr_no);
1174
1175 sequencer_busy = 0;
1176 }
1177
1178 static int
1179 seq_sync (void)
1180 {
1181 if (qlen && !seq_playing && !PROCESS_ABORTING (seq_sleeper, seq_sleep_flag))
1182 seq_startplay ();
1183
1184 if (qlen && !SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
1185
1186
1187
1188 {
1189 DO_SLEEP (seq_sleeper, seq_sleep_flag, 0);
1190 }
1191
1192 return qlen;
1193 }
1194
1195 static void
1196 midi_outc (int dev, unsigned char data)
1197 {
1198
1199
1200
1201
1202 int n;
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214 n = 300;
1215
1216
1217
1218 while (n && !midi_devs[dev]->putc (dev, data))
1219 {
1220 DO_SLEEP (seq_sleeper, seq_sleep_flag, 4);
1221 n--;
1222 }
1223 }
1224
1225 static void
1226 seq_reset (void)
1227 {
1228
1229
1230
1231
1232 int i;
1233
1234 int chn;
1235
1236 sound_stop_timer ();
1237 seq_time = GET_TIME ();
1238 prev_input_time = 0;
1239 prev_event_time = 0;
1240
1241 qlen = qhead = qtail = 0;
1242 iqlen = iqhead = iqtail = 0;
1243
1244 for (i = 0; i < max_synthdev; i++)
1245 if (synth_open_mask & (1 << i))
1246 if (synth_devs[i])
1247 synth_devs[i]->reset (i);
1248
1249 if (seq_mode == SEQ_2)
1250 {
1251 for (i = 0; i < max_synthdev; i++)
1252 if (synth_open_mask & (1 << i))
1253 if (synth_devs[i])
1254 for (chn = 0; chn < 16; chn++)
1255 synth_devs[i]->controller (i, chn, 0xfe, 0);
1256 }
1257 else
1258 {
1259 for (i = 0; i < max_mididev; i++)
1260 if (midi_written[i])
1261
1262
1263 {
1264
1265
1266
1267
1268
1269 midi_outc (i, 0xfe);
1270
1271 for (chn = 0; chn < 16; chn++)
1272 {
1273 midi_outc (i,
1274 (unsigned char) (0xb0 + (chn & 0xff)));
1275
1276
1277
1278 midi_outc (i, 0x7b);
1279
1280
1281 midi_outc (i, 0);
1282
1283
1284 }
1285
1286 midi_devs[i]->close (i);
1287
1288 midi_written[i] = 0;
1289 midi_opened[i] = 0;
1290 }
1291 }
1292
1293 seq_playing = 0;
1294
1295 if (SOMEONE_WAITING (seq_sleeper, seq_sleep_flag))
1296 printk ("Sequencer Warning: Unexpected sleeping process\n");
1297
1298 }
1299
1300 static void
1301 seq_panic (void)
1302 {
1303
1304
1305
1306
1307
1308 seq_reset ();
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320 }
1321
1322 int
1323 sequencer_ioctl (int dev, struct fileinfo *file,
1324 unsigned int cmd, unsigned int arg)
1325 {
1326 int midi_dev, orig_dev;
1327 int mode = file->mode & O_ACCMODE;
1328
1329 orig_dev = dev = dev >> 4;
1330
1331 switch (cmd)
1332 {
1333 case SNDCTL_TMR_TIMEBASE:
1334 case SNDCTL_TMR_TEMPO:
1335 case SNDCTL_TMR_START:
1336 case SNDCTL_TMR_STOP:
1337 case SNDCTL_TMR_CONTINUE:
1338 case SNDCTL_TMR_METRONOME:
1339 case SNDCTL_TMR_SOURCE:
1340 if (dev)
1341 return RET_ERROR (EIO);
1342
1343 if (seq_mode != SEQ_2)
1344 return RET_ERROR (EINVAL);
1345 return tmr->ioctl (tmr_no, cmd, arg);
1346 break;
1347
1348 case SNDCTL_TMR_SELECT:
1349 if (dev)
1350 return RET_ERROR (EIO);
1351
1352 if (seq_mode != SEQ_2)
1353 return RET_ERROR (EINVAL);
1354 pending_timer = IOCTL_IN (arg);
1355
1356 if (pending_timer < 0 || pending_timer >= num_sound_timers)
1357 {
1358 pending_timer = -1;
1359 return RET_ERROR (EINVAL);
1360 }
1361
1362 return IOCTL_OUT (arg, pending_timer);
1363 break;
1364
1365 case SNDCTL_SEQ_PANIC:
1366 seq_panic ();
1367 break;
1368
1369 case SNDCTL_SEQ_SYNC:
1370 if (dev)
1371
1372
1373 return RET_ERROR (EIO);
1374
1375 if (mode == OPEN_READ)
1376 return 0;
1377 while (qlen && !PROCESS_ABORTING (seq_sleeper, seq_sleep_flag))
1378 seq_sync ();
1379 if (qlen)
1380 return RET_ERROR (EINTR);
1381 else
1382 return 0;
1383 break;
1384
1385 case SNDCTL_SEQ_RESET:
1386 if (dev)
1387
1388
1389 return RET_ERROR (EIO);
1390
1391 seq_reset ();
1392 return 0;
1393 break;
1394
1395 case SNDCTL_SEQ_TESTMIDI:
1396 if (dev)
1397
1398
1399 return RET_ERROR (EIO);
1400
1401 midi_dev = IOCTL_IN (arg);
1402 if (midi_dev >= max_mididev)
1403 return RET_ERROR (ENXIO);
1404
1405 if (!midi_opened[midi_dev])
1406 {
1407 int err, mode;
1408
1409 mode = file->mode & O_ACCMODE;
1410 if ((err = midi_devs[midi_dev]->open (midi_dev, mode,
1411 sequencer_midi_input,
1412 sequencer_midi_output)) < 0)
1413 return err;
1414 }
1415
1416 midi_opened[midi_dev] = 1;
1417
1418 return 0;
1419 break;
1420
1421 case SNDCTL_SEQ_GETINCOUNT:
1422 if (dev)
1423
1424
1425 return RET_ERROR (EIO);
1426
1427 if (mode == OPEN_WRITE)
1428 return 0;
1429 return IOCTL_OUT (arg, iqlen);
1430 break;
1431
1432 case SNDCTL_SEQ_GETOUTCOUNT:
1433
1434 if (mode == OPEN_READ)
1435 return 0;
1436 return IOCTL_OUT (arg, SEQ_MAX_QUEUE - qlen);
1437 break;
1438
1439 case SNDCTL_SEQ_CTRLRATE:
1440 if (dev)
1441 return RET_ERROR (EIO);
1442
1443
1444
1445
1446 if (seq_mode == SEQ_2)
1447 return tmr->ioctl (tmr_no, cmd, arg);
1448
1449 if (IOCTL_IN (arg) != 0)
1450 return RET_ERROR (EINVAL);
1451
1452 return IOCTL_OUT (arg, HZ);
1453 break;
1454
1455 case SNDCTL_SEQ_RESETSAMPLES:
1456 dev = IOCTL_IN (arg);
1457 if (dev < 0 || dev >= num_synths)
1458 return RET_ERROR (ENXIO);
1459
1460 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1461 return RET_ERROR (EBUSY);
1462
1463 if (!orig_dev && pmgr_present[dev])
1464 pmgr_inform (dev, PM_E_PATCH_RESET, 0, 0, 0, 0);
1465
1466 return synth_devs[dev]->ioctl (dev, cmd, arg);
1467 break;
1468
1469 case SNDCTL_SEQ_NRSYNTHS:
1470 return IOCTL_OUT (arg, max_synthdev);
1471 break;
1472
1473 case SNDCTL_SEQ_NRMIDIS:
1474 return IOCTL_OUT (arg, max_mididev);
1475 break;
1476
1477 case SNDCTL_SYNTH_MEMAVL:
1478 {
1479 int dev = IOCTL_IN (arg);
1480
1481 if (dev < 0 || dev >= num_synths)
1482 return RET_ERROR (ENXIO);
1483
1484 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1485 return RET_ERROR (EBUSY);
1486
1487 return IOCTL_OUT (arg, synth_devs[dev]->ioctl (dev, cmd, arg));
1488 }
1489 break;
1490
1491 case SNDCTL_FM_4OP_ENABLE:
1492 {
1493 int dev = IOCTL_IN (arg);
1494
1495 if (dev < 0 || dev >= num_synths)
1496 return RET_ERROR (ENXIO);
1497
1498 if (!(synth_open_mask & (1 << dev)))
1499 return RET_ERROR (ENXIO);
1500
1501 synth_devs[dev]->ioctl (dev, cmd, arg);
1502 return 0;
1503 }
1504 break;
1505
1506 case SNDCTL_SYNTH_INFO:
1507 {
1508 struct synth_info inf;
1509 int dev;
1510
1511 IOCTL_FROM_USER ((char *) &inf, (char *) arg, 0, sizeof (inf));
1512 dev = inf.device;
1513
1514 if (dev < 0 || dev >= max_synthdev)
1515 return RET_ERROR (ENXIO);
1516
1517 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
1518 return RET_ERROR (EBUSY);
1519
1520 return synth_devs[dev]->ioctl (dev, cmd, arg);
1521 }
1522 break;
1523
1524 case SNDCTL_MIDI_INFO:
1525 {
1526 struct midi_info inf;
1527 int dev;
1528
1529 IOCTL_FROM_USER ((char *) &inf, (char *) arg, 0, sizeof (inf));
1530 dev = inf.device;
1531
1532 if (dev < 0 || dev >= max_mididev)
1533 return RET_ERROR (ENXIO);
1534
1535 IOCTL_TO_USER ((char *) arg, 0, (char *) &(midi_devs[dev]->info), sizeof (inf));
1536 return 0;
1537 }
1538 break;
1539
1540 case SNDCTL_PMGR_IFACE:
1541 {
1542 struct patmgr_info *inf;
1543 int dev, err;
1544
1545 inf = (struct patmgr_info *) KERNEL_MALLOC (sizeof (*inf));
1546
1547 IOCTL_FROM_USER ((char *) inf, (char *) arg, 0, sizeof (*inf));
1548 dev = inf->device;
1549
1550 if (dev < 0 || dev >= num_synths)
1551 {
1552 KERNEL_FREE (inf);
1553 return RET_ERROR (ENXIO);
1554 }
1555
1556 if (!synth_devs[dev]->pmgr_interface)
1557 {
1558 KERNEL_FREE (inf);
1559 return RET_ERROR (ENXIO);
1560 }
1561
1562 if ((err = synth_devs[dev]->pmgr_interface (dev, inf)) == -1)
1563 {
1564 KERNEL_FREE (inf);
1565 return err;
1566 }
1567
1568 IOCTL_TO_USER ((char *) arg, 0, (char *) inf, sizeof (*inf));
1569 KERNEL_FREE (inf);
1570 return 0;
1571 }
1572 break;
1573
1574 case SNDCTL_PMGR_ACCESS:
1575 {
1576 struct patmgr_info *inf;
1577 int dev, err;
1578
1579 inf = (struct patmgr_info *) KERNEL_MALLOC (sizeof (*inf));
1580
1581 IOCTL_FROM_USER ((char *) inf, (char *) arg, 0, sizeof (*inf));
1582 dev = inf->device;
1583
1584 if (dev < 0 || dev >= num_synths)
1585 {
1586 KERNEL_FREE (inf);
1587 return RET_ERROR (ENXIO);
1588 }
1589
1590 if (!pmgr_present[dev])
1591 {
1592 KERNEL_FREE (inf);
1593 return RET_ERROR (ESRCH);
1594 }
1595
1596 if ((err = pmgr_access (dev, inf)) < 0)
1597 {
1598 KERNEL_FREE (inf);
1599 return err;
1600 }
1601
1602 IOCTL_TO_USER ((char *) arg, 0, (char *) inf, sizeof (*inf));
1603 KERNEL_FREE (inf);
1604 return 0;
1605 }
1606 break;
1607
1608 case SNDCTL_SEQ_TRESHOLD:
1609 {
1610 int tmp = IOCTL_IN (arg);
1611
1612 if (dev)
1613
1614
1615 return RET_ERROR (EIO);
1616
1617 if (tmp < 1)
1618 tmp = 1;
1619 if (tmp >= SEQ_MAX_QUEUE)
1620 tmp = SEQ_MAX_QUEUE - 1;
1621 output_treshold = tmp;
1622 return 0;
1623 }
1624 break;
1625
1626 case SNDCTL_MIDI_PRETIME:
1627 {
1628 int val = IOCTL_IN (arg);
1629
1630 if (val < 0)
1631 val = 0;
1632
1633 val = (HZ * val) / 10;
1634 pre_event_timeout = val;
1635 return IOCTL_OUT (arg, val);
1636 }
1637 break;
1638
1639 default:
1640 if (dev)
1641
1642
1643 return RET_ERROR (EIO);
1644
1645 if (mode == OPEN_READ)
1646 return RET_ERROR (EIO);
1647
1648 if (!synth_devs[0])
1649 return RET_ERROR (ENXIO);
1650 if (!(synth_open_mask & (1 << 0)))
1651 return RET_ERROR (ENXIO);
1652 return synth_devs[0]->ioctl (0, cmd, arg);
1653 break;
1654 }
1655
1656 return RET_ERROR (EINVAL);
1657 }
1658
1659 #ifdef ALLOW_SELECT
1660 int
1661 sequencer_select (int dev, struct fileinfo *file, int sel_type, select_table * wait)
1662 {
1663 unsigned long flags;
1664
1665 dev = dev >> 4;
1666
1667 switch (sel_type)
1668 {
1669 case SEL_IN:
1670 if (!iqlen)
1671 {
1672 DISABLE_INTR (flags);
1673 midi_sleep_flag.mode = WK_SLEEP;
1674 select_wait (&midi_sleeper, wait);
1675 RESTORE_INTR (flags);
1676 return 0;
1677 }
1678 return 1;
1679 break;
1680
1681 case SEL_OUT:
1682 if (qlen >= SEQ_MAX_QUEUE)
1683 {
1684 DISABLE_INTR (flags);
1685 seq_sleep_flag.mode = WK_SLEEP;
1686 select_wait (&seq_sleeper, wait);
1687 RESTORE_INTR (flags);
1688 return 0;
1689 }
1690 return 1;
1691 break;
1692
1693 case SEL_EX:
1694 return 0;
1695 }
1696
1697 return 0;
1698 }
1699
1700 #endif
1701
1702 void
1703 sequencer_timer (void)
1704 {
1705 seq_startplay ();
1706 }
1707
1708 int
1709 note_to_freq (int note_num)
1710 {
1711
1712
1713
1714
1715
1716 int note, octave, note_freq;
1717 int notes[] =
1718 {
1719 261632, 277189, 293671, 311132, 329632, 349232,
1720 369998, 391998, 415306, 440000, 466162, 493880
1721 };
1722
1723 #define BASE_OCTAVE 5
1724
1725 octave = note_num / 12;
1726 note = note_num % 12;
1727
1728 note_freq = notes[note];
1729
1730 if (octave < BASE_OCTAVE)
1731 note_freq >>= (BASE_OCTAVE - octave);
1732 else if (octave > BASE_OCTAVE)
1733 note_freq <<= (octave - BASE_OCTAVE);
1734
1735
1736
1737
1738
1739 return note_freq;
1740 }
1741
1742 unsigned long
1743 compute_finetune (unsigned long base_freq, int bend, int range)
1744 {
1745 unsigned long amount;
1746 int negative, semitones, cents, multiplier = 1;
1747
1748 if (!bend)
1749 return base_freq;
1750 if (!range)
1751 return base_freq;
1752
1753 if (!base_freq)
1754 return base_freq;
1755
1756 if (range >= 8192)
1757 range = 8191;
1758
1759 bend = bend * range / 8192;
1760 if (!bend)
1761 return base_freq;
1762
1763 negative = bend < 0 ? 1 : 0;
1764
1765 if (bend < 0)
1766 bend *= -1;
1767 if (bend > range)
1768 bend = range;
1769
1770
1771
1772
1773
1774 while (bend > 2399)
1775 {
1776 multiplier *= 4;
1777 bend -= 2400;
1778 }
1779
1780 semitones = bend / 100;
1781 cents = bend % 100;
1782
1783 amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents])
1784 / 10000;
1785
1786 if (negative)
1787 return (base_freq * 10000) / amount;
1788
1789
1790 else
1791 return (base_freq * amount) / 10000;
1792
1793
1794 }
1795
1796
1797 long
1798 sequencer_init (long mem_start)
1799 {
1800
1801 sequencer_ok = 1;
1802 PERMANENT_MALLOC (unsigned char *, queue, SEQ_MAX_QUEUE * EV_SZ, mem_start);
1803 PERMANENT_MALLOC (unsigned char *, iqueue, SEQ_MAX_QUEUE * IEV_SZ, mem_start);
1804
1805 return mem_start;
1806 }
1807
1808 #else
1809
1810
1811
1812 int
1813 sequencer_read (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
1814 {
1815 return RET_ERROR (EIO);
1816 }
1817
1818 int
1819 sequencer_write (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
1820 {
1821 return RET_ERROR (EIO);
1822 }
1823
1824 int
1825 sequencer_open (int dev, struct fileinfo *file)
1826 {
1827 return RET_ERROR (ENXIO);
1828 }
1829
1830 void
1831 sequencer_release (int dev, struct fileinfo *file)
1832 {
1833 }
1834 int
1835 sequencer_ioctl (int dev, struct fileinfo *file,
1836 unsigned int cmd, unsigned int arg)
1837 {
1838 return RET_ERROR (EIO);
1839 }
1840
1841 int
1842 sequencer_lseek (int dev, struct fileinfo *file, off_t offset, int orig)
1843 {
1844 return RET_ERROR (EIO);
1845 }
1846
1847 long
1848 sequencer_init (long mem_start)
1849 {
1850 return mem_start;
1851 }
1852
1853 int
1854 sequencer_select (int dev, struct fileinfo *file, int sel_type, select_table * wait)
1855 {
1856 return RET_ERROR (EIO);
1857 }
1858
1859 #endif
1860
1861 #endif