This source file includes following definitions.
- sequencer_read
- sequencer_midi_output
- copy_to_input
- sequencer_midi_input
- sequencer_write
- seq_queue
- extended_event
- seq_startplay
- sequencer_open
- seq_drain_midi_queues
- sequencer_release
- seq_sync
- midi_outc
- seq_reset
- 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
39 DEFINE_WAIT_QUEUE (seq_sleeper, seq_sleep_flag);
40
41 #define midi_sleeper seq_sleeper
42 #define midi_sleep_flag seq_sleep_flag
43
44 static int midi_opened[MAX_MIDI_DEV] =
45 {0};
46 static int midi_written[MAX_MIDI_DEV] =
47 {0};
48
49 long seq_time = 0;
50
51 #include "tuning.h"
52
53 #define EV_SZ 8
54 static unsigned char queue[SEQ_MAX_QUEUE][EV_SZ];
55 static unsigned char iqueue[SEQ_MAX_QUEUE][4];
56 static volatile int qhead = 0, qtail = 0, qlen = 0;
57 static volatile int iqhead = 0, iqtail = 0, iqlen = 0;
58 static volatile int seq_playing = 0;
59 static int sequencer_busy = 0;
60 static int output_treshold;
61 static unsigned synth_open_mask;
62
63 static int seq_queue (unsigned char *note);
64 static void seq_startplay (void);
65 static int seq_sync (void);
66 static void seq_reset (void);
67 static int pmgr_present[MAX_SYNTH_DEV] =
68 {0};
69
70 #if MAX_SYNTH_DEV > 15
71 #error Too many synthesizer devices
72 #endif
73
74 int
75 sequencer_read (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
76 {
77 int c = count, p = 0;
78
79 dev = dev >> 4;
80
81 if (dev)
82 return pmgr_read (dev - 1, file, buf, count);
83
84 while (c > 3)
85 {
86 if (!iqlen)
87 {
88 DO_SLEEP (midi_sleeper, midi_sleep_flag, 0);
89
90 if (!iqlen)
91 return count - c;
92 }
93
94 COPY_TO_USER (buf, p, &iqueue[iqhead][0], 4);
95 p += 4;
96 c -= 4;
97
98 iqhead = (iqhead + 1) % SEQ_MAX_QUEUE;
99 iqlen--;
100 }
101
102 return count - c;
103 }
104
105 static void
106 sequencer_midi_output (int dev)
107 {
108
109 }
110
111 static void
112 copy_to_input (unsigned char *event)
113 {
114 unsigned long flags;
115
116 if (iqlen >= (SEQ_MAX_QUEUE - 1))
117 return;
118
119 memcpy (iqueue[iqtail], event, 4);
120 iqlen++;
121 iqtail = (iqtail + 1) % SEQ_MAX_QUEUE;
122
123 DISABLE_INTR (flags);
124 if (SOMEONE_WAITING (midi_sleep_flag))
125 {
126 WAKE_UP (midi_sleeper, midi_sleep_flag);
127 }
128 RESTORE_INTR (flags);
129 }
130
131 static void
132 sequencer_midi_input (int dev, unsigned char data)
133 {
134 int tstamp;
135 unsigned char event[4];
136
137 if (data == 0xfe)
138 return;
139
140 tstamp = GET_TIME () - seq_time;
141 tstamp = (tstamp << 8) | SEQ_WAIT;
142
143 copy_to_input ((unsigned char *) &tstamp);
144
145 event[0] = SEQ_MIDIPUTC;
146 event[1] = data;
147 event[2] = dev;
148 event[3] = 0;
149
150 copy_to_input (event);
151 }
152
153 int
154 sequencer_write (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
155 {
156 unsigned char event[EV_SZ], ev_code;
157 int p = 0, c, ev_size;
158 int err;
159 int mode = file->mode & O_ACCMODE;
160
161 dev = dev >> 4;
162
163 DEB (printk ("sequencer_write(dev=%d, count=%d)\n", dev, count));
164
165 if (mode == OPEN_READ)
166 return RET_ERROR (EIO);
167
168 if (dev)
169 return pmgr_write (dev - 1, file, buf, count);
170
171 c = count;
172
173 while (c >= 4)
174 {
175 COPY_FROM_USER (event, buf, p, 4);
176 ev_code = event[0];
177
178 if (ev_code == SEQ_FULLSIZE)
179 {
180 int err;
181
182 dev = *(unsigned short *) &event[2];
183 if (dev < 0 || dev >= num_synths)
184 return RET_ERROR (ENXIO);
185
186 if (!(synth_open_mask & (1 << dev)))
187 return RET_ERROR (ENXIO);
188
189 err = synth_devs[dev]->load_patch (dev, *(short *) &event[0], buf, p + 4, c, 0);
190 if (err < 0)
191 return err;
192
193 return err;
194 }
195
196 if (ev_code == SEQ_EXTENDED || ev_code == SEQ_PRIVATE)
197 {
198
199 ev_size = 8;
200
201 if (c < ev_size)
202 {
203 if (!seq_playing)
204 seq_startplay ();
205 return count - c;
206 }
207
208 COPY_FROM_USER (&event[4], buf, p + 4, 4);
209
210 }
211 else
212 ev_size = 4;
213
214 if (event[0] == SEQ_MIDIPUTC)
215 {
216
217 if (!midi_opened[event[2]])
218 {
219 int mode;
220 int dev = event[2];
221
222 if (dev >= num_midis)
223 {
224 printk ("Sequencer Error: Nonexistent MIDI device %d\n", dev);
225 return RET_ERROR (ENXIO);
226 }
227
228 mode = file->mode & O_ACCMODE;
229
230 if ((err = midi_devs[dev]->open (dev, mode,
231 sequencer_midi_input, sequencer_midi_output)) < 0)
232 {
233 seq_reset ();
234 printk ("Sequencer Error: Unable to open Midi #%d\n", dev);
235 return err;
236 }
237
238 midi_opened[dev] = 1;
239 }
240
241 }
242
243 if (!seq_queue (event))
244 {
245
246 if (!seq_playing)
247 seq_startplay ();
248 return count - c;
249 }
250
251 p += ev_size;
252 c -= ev_size;
253 }
254
255 if (!seq_playing)
256 seq_startplay ();
257
258 return count;
259 }
260
261 static int
262 seq_queue (unsigned char *note)
263 {
264
265
266
267 if (qlen >= SEQ_MAX_QUEUE)
268 if (!seq_playing)
269 seq_startplay ();
270
271 if (qlen >= SEQ_MAX_QUEUE && !SOMEONE_WAITING (seq_sleep_flag))
272 {
273
274 DO_SLEEP (seq_sleeper, seq_sleep_flag, 0);
275 }
276
277 if (qlen >= SEQ_MAX_QUEUE)
278 return 0;
279
280 memcpy (&queue[qtail][0], note, EV_SZ);
281
282 qtail = (qtail + 1) % SEQ_MAX_QUEUE;
283 qlen++;
284
285 return 1;
286 }
287
288 static int
289 extended_event (unsigned char *q)
290 {
291 int dev = q[2];
292
293 if (dev < 0 || dev >= num_synths)
294 return RET_ERROR (ENXIO);
295
296 if (!(synth_open_mask & (1 << dev)))
297 return RET_ERROR (ENXIO);
298
299 switch (q[1])
300 {
301 case SEQ_NOTEOFF:
302 synth_devs[dev]->kill_note (dev, q[3], q[5]);
303 break;
304
305 case SEQ_NOTEON:
306 if (q[4] > 127 && q[4] != 255)
307 return 0;
308
309 synth_devs[dev]->start_note (dev, q[3], q[4], q[5]);
310 break;
311
312 case SEQ_PGMCHANGE:
313 synth_devs[dev]->set_instr (dev, q[3], q[4]);
314 break;
315
316 case SEQ_AFTERTOUCH:
317 synth_devs[dev]->aftertouch (dev, q[3], q[4]);
318 break;
319
320 case SEQ_BALANCE:
321 synth_devs[dev]->panning (dev, q[3], (char) q[4]);
322 break;
323
324 case SEQ_CONTROLLER:
325 synth_devs[dev]->controller (dev, q[3], q[4], *(short *) &q[5]);
326 break;
327
328 default:
329 return RET_ERROR (EINVAL);
330 }
331
332 return 0;
333 }
334
335 static void
336 seq_startplay (void)
337 {
338 int this_one;
339 unsigned long *delay;
340 unsigned char *q;
341
342 while (qlen > 0)
343 {
344 qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
345 qlen--;
346
347 q = &queue[this_one][0];
348
349 switch (q[0])
350 {
351 case SEQ_NOTEOFF:
352 if (synth_open_mask & (1 << 0))
353 if (synth_devs[0])
354 synth_devs[0]->kill_note (0, q[1], q[3]);
355 break;
356
357 case SEQ_NOTEON:
358 if (q[4] < 128 || q[4] == 255)
359 if (synth_open_mask & (1 << 0))
360 if (synth_devs[0])
361 synth_devs[0]->start_note (0, q[1], q[2], q[3]);
362 break;
363
364 case SEQ_WAIT:
365 delay = (unsigned long *) q;
366
367 *delay = (*delay >> 8) & 0xffffff;
368
369 if (*delay > 0)
370 {
371 long time;
372
373 seq_playing = 1;
374 time = *delay;
375
376 request_sound_timer (time);
377
378 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
379 {
380 unsigned long flags;
381
382 DISABLE_INTR (flags);
383 if (SOMEONE_WAITING (seq_sleep_flag))
384 {
385 WAKE_UP (seq_sleeper, seq_sleep_flag);
386 }
387 RESTORE_INTR (flags);
388 }
389 return;
390
391 }
392 break;
393
394 case SEQ_PGMCHANGE:
395 if (synth_open_mask & (1 << 0))
396 if (synth_devs[0])
397 synth_devs[0]->set_instr (0, q[1], q[2]);
398 break;
399
400 case SEQ_SYNCTIMER:
401 seq_time = GET_TIME ();
402 break;
403
404 case SEQ_MIDIPUTC:
405 if (midi_opened[q[2]])
406 {
407 int dev;
408
409 dev = q[2];
410
411 if (!midi_devs[dev]->putc (dev, q[1]))
412 {
413
414
415
416
417 qlen++;
418 qhead = this_one;
419 seq_playing = 1;
420 request_sound_timer (-1);
421 return;
422 }
423 else
424 midi_written[dev] = 1;
425 }
426 break;
427
428 case SEQ_ECHO:
429 copy_to_input (q);
430 break;
431
432 case SEQ_PRIVATE:
433 if (q[1] < num_synths)
434 synth_devs[q[1]]->hw_control (q[1], q);
435 break;
436
437 case SEQ_EXTENDED:
438 extended_event (q);
439 break;
440
441 default:;
442 }
443
444 }
445
446 seq_playing = 0;
447
448 if ((SEQ_MAX_QUEUE - qlen) >= output_treshold)
449 {
450 unsigned long flags;
451
452 DISABLE_INTR (flags);
453 if (SOMEONE_WAITING (seq_sleep_flag))
454 {
455 WAKE_UP (seq_sleeper, seq_sleep_flag);
456 }
457 RESTORE_INTR (flags);
458 }
459
460 }
461
462 int
463 sequencer_open (int dev, struct fileinfo *file)
464 {
465 int retval, mode, i;
466
467 dev = dev >> 4;
468 mode = file->mode & O_ACCMODE;
469
470 DEB (printk ("sequencer_open(dev=%d)\n", dev));
471
472 if (!sequencer_ok)
473 {
474 printk ("Soundcard: Sequencer not initialized\n");
475 return RET_ERROR (ENXIO);
476 }
477
478 if (dev)
479 {
480 int err;
481
482 dev--;
483 if (pmgr_present[dev])
484 return RET_ERROR (EBUSY);
485 if ((err = pmgr_open (dev)) < 0)
486 return err;
487
488 pmgr_present[dev] = 1;
489 return err;
490 }
491
492 if (sequencer_busy)
493 {
494 printk ("Sequencer busy\n");
495 return RET_ERROR (EBUSY);
496 }
497
498 if (!(num_synths + num_midis))
499 return RET_ERROR (ENXIO);
500
501 synth_open_mask = 0;
502
503 if (mode == OPEN_WRITE || mode == OPEN_READWRITE)
504 for (i = 0; i < num_synths; i++)
505 if (synth_devs[i]->open (i, mode) < 0)
506 printk ("Sequencer: Warning! Cannot open synth device #%d\n", i);
507 else
508 synth_open_mask |= (1 << i);
509
510 seq_time = GET_TIME ();
511
512 for (i = 0; i < num_midis; i++)
513 {
514 midi_opened[i] = 0;
515 midi_written[i] = 0;
516 }
517
518 if (mode == OPEN_READ || mode == OPEN_READWRITE)
519 {
520 if (!num_midis)
521 {
522 printk ("Sequencer: No Midi devices. Input not possible\n");
523 return RET_ERROR (ENXIO);
524 }
525
526 for (i = 0; i < num_midis; i++)
527 {
528 if ((retval = midi_devs[i]->open (i, mode,
529 sequencer_midi_input, sequencer_midi_output)) >= 0)
530 midi_opened[i] = 1;
531 }
532 }
533
534 sequencer_busy = 1;
535 RESET_WAIT_QUEUE (seq_sleeper, seq_sleep_flag);
536 RESET_WAIT_QUEUE (midi_sleeper, midi_sleep_flag);
537 output_treshold = SEQ_MAX_QUEUE / 2;
538
539 for (i = 0; i < num_synths; i++)
540 if (pmgr_present[i])
541 pmgr_inform (i, PM_E_OPENED, 0, 0, 0, 0);
542
543 return 0;
544 }
545
546 void
547 seq_drain_midi_queues (void)
548 {
549 int i, n;
550
551
552
553
554
555 n = 1;
556
557 while (!PROCESS_ABORTING (midi_sleeper, midi_sleep_flag) && n)
558 {
559 n = 0;
560
561 for (i = 0; i < num_midis; i++)
562 if (midi_opened[i] && midi_written[i])
563 if (midi_devs[i]->buffer_status != NULL)
564 if (midi_devs[i]->buffer_status (i))
565 n++;
566
567
568
569
570 if (n)
571 {
572 DO_SLEEP (seq_sleeper, seq_sleep_flag, HZ / 10);
573 }
574 }
575 }
576
577 void
578 sequencer_release (int dev, struct fileinfo *file)
579 {
580 int i;
581 int mode = file->mode & O_ACCMODE;
582
583 dev = dev >> 4;
584
585 DEB (printk ("sequencer_release(dev=%d)\n", dev));
586
587 if (dev)
588 {
589 dev--;
590 pmgr_release (dev);
591 pmgr_present[dev] = 0;
592 return;
593 }
594
595
596
597
598
599 while (!PROCESS_ABORTING (seq_sleeper, seq_sleep_flag) && qlen)
600 {
601 seq_sync ();
602 }
603
604 if (mode != OPEN_READ)
605 seq_drain_midi_queues ();
606 seq_reset ();
607 if (mode != OPEN_READ)
608 seq_drain_midi_queues ();
609
610 for (i = 0; i < num_midis; i++)
611 if (midi_opened[i])
612 midi_devs[i]->close (i);
613
614 if (mode == OPEN_WRITE || mode == OPEN_READWRITE)
615 for (i = 0; i < num_synths; i++)
616 if (synth_open_mask & (1 << i))
617 if (synth_devs[i])
618 synth_devs[i]->close (i);
619
620 for (i = 0; i < num_synths; i++)
621 if (pmgr_present[i])
622 pmgr_inform (i, PM_E_CLOSED, 0, 0, 0, 0);
623
624 sequencer_busy = 0;
625 }
626
627 static int
628 seq_sync (void)
629 {
630 if (qlen && !seq_playing && !PROCESS_ABORTING (seq_sleeper, seq_sleep_flag))
631 seq_startplay ();
632
633 if (qlen && !SOMEONE_WAITING (seq_sleep_flag))
634 {
635 DO_SLEEP (seq_sleeper, seq_sleep_flag, 0);
636 }
637
638 return qlen;
639 }
640
641 static void
642 midi_outc (int dev, unsigned char data)
643 {
644
645
646
647
648 int n;
649
650
651
652
653
654 n = 300;
655
656 while (n && !midi_devs[dev]->putc (dev, data))
657 {
658 DO_SLEEP (seq_sleeper, seq_sleep_flag, 4);
659 n--;
660 }
661 }
662
663 static void
664 seq_reset (void)
665 {
666
667
668
669
670 int i, chn;
671
672 sound_stop_timer ();
673
674 qlen = qhead = qtail = 0;
675 iqlen = iqhead = iqtail = 0;
676
677 for (i = 0; i < num_synths; i++)
678 if (synth_open_mask & (1 << i))
679 if (synth_devs[i])
680 synth_devs[i]->reset (i);
681
682 for (i = 0; i < num_midis; i++)
683 if (midi_written[i])
684 {
685 for (chn = 0; chn < 16; chn++)
686 {
687 midi_outc (i, 0xb0 + chn);
688 midi_outc (i, 0x7b);
689 midi_outc (i, 0);
690 }
691
692 midi_devs[i]->close (i);
693
694 midi_written[i] = 0;
695 midi_opened[i] = 0;
696 }
697
698 seq_playing = 0;
699
700 if (SOMEONE_WAITING (seq_sleep_flag))
701 printk ("Sequencer Warning: Unexpected sleeping process\n");
702
703 }
704
705 int
706 sequencer_ioctl (int dev, struct fileinfo *file,
707 unsigned int cmd, unsigned int arg)
708 {
709 int midi_dev, orig_dev;
710 int mode = file->mode & O_ACCMODE;
711
712 orig_dev = dev = dev >> 4;
713
714 switch (cmd)
715 {
716
717 case SNDCTL_SEQ_SYNC:
718 if (dev)
719 return RET_ERROR (EIO);
720
721 if (mode == OPEN_READ)
722 return 0;
723 while (qlen && !PROCESS_ABORTING (seq_sleeper, seq_sleep_flag))
724 seq_sync ();
725 return 0;
726 break;
727
728 case SNDCTL_SEQ_RESET:
729 if (dev)
730 return RET_ERROR (EIO);
731
732 seq_reset ();
733 return 0;
734 break;
735
736 case SNDCTL_SEQ_TESTMIDI:
737 if (dev)
738 return RET_ERROR (EIO);
739
740 midi_dev = IOCTL_IN (arg);
741 if (midi_dev >= num_midis)
742 return RET_ERROR (ENXIO);
743
744 if (!midi_opened[midi_dev])
745 {
746 int err, mode;
747
748 mode = file->mode & O_ACCMODE;
749 if ((err = midi_devs[midi_dev]->open (midi_dev, mode,
750 sequencer_midi_input,
751 sequencer_midi_output)) < 0)
752 return err;
753 }
754
755 midi_opened[midi_dev] = 1;
756
757 return 0;
758 break;
759
760 case SNDCTL_SEQ_GETINCOUNT:
761 if (dev)
762 return RET_ERROR (EIO);
763
764 if (mode == OPEN_WRITE)
765 return 0;
766 return IOCTL_OUT (arg, iqlen);
767 break;
768
769 case SNDCTL_SEQ_GETOUTCOUNT:
770
771 if (mode == OPEN_READ)
772 return 0;
773 return IOCTL_OUT (arg, SEQ_MAX_QUEUE - qlen);
774 break;
775
776 case SNDCTL_SEQ_CTRLRATE:
777 if (dev)
778 return RET_ERROR (EIO);
779
780
781 return IOCTL_OUT (arg, HZ);
782 break;
783
784 case SNDCTL_SEQ_RESETSAMPLES:
785 dev = IOCTL_IN (arg);
786 if (dev < 0 || dev >= num_synths)
787 return RET_ERROR (ENXIO);
788
789 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
790 return RET_ERROR (EBUSY);
791
792 if (!orig_dev && pmgr_present[dev])
793 pmgr_inform (dev, PM_E_PATCH_RESET, 0, 0, 0, 0);
794
795 return synth_devs[dev]->ioctl (dev, cmd, arg);
796 break;
797
798 case SNDCTL_SEQ_NRSYNTHS:
799 return IOCTL_OUT (arg, num_synths);
800 break;
801
802 case SNDCTL_SEQ_NRMIDIS:
803 return IOCTL_OUT (arg, num_midis);
804 break;
805
806 case SNDCTL_SYNTH_MEMAVL:
807 {
808 int dev = IOCTL_IN (arg);
809
810 if (dev < 0 || dev >= num_synths)
811 return RET_ERROR (ENXIO);
812
813 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
814 return RET_ERROR (EBUSY);
815
816 return IOCTL_OUT (arg, synth_devs[dev]->ioctl (dev, cmd, arg));
817 }
818 break;
819
820 case SNDCTL_FM_4OP_ENABLE:
821 {
822 int dev = IOCTL_IN (arg);
823
824 if (dev < 0 || dev >= num_synths)
825 return RET_ERROR (ENXIO);
826
827 if (!(synth_open_mask & (1 << dev)))
828 return RET_ERROR (ENXIO);
829
830 synth_devs[dev]->ioctl (dev, cmd, arg);
831 return 0;
832 }
833 break;
834
835 case SNDCTL_SYNTH_INFO:
836 {
837 struct synth_info inf;
838 int dev;
839
840 IOCTL_FROM_USER ((char *) &inf, (char *) arg, 0, sizeof (inf));
841 dev = inf.device;
842
843 if (dev < 0 || dev >= num_synths)
844 return RET_ERROR (ENXIO);
845
846 if (!(synth_open_mask & (1 << dev)) && !orig_dev)
847 return RET_ERROR (EBUSY);
848
849 return synth_devs[dev]->ioctl (dev, cmd, arg);
850 }
851 break;
852
853 case SNDCTL_MIDI_INFO:
854 {
855 struct midi_info inf;
856 int dev;
857
858 IOCTL_FROM_USER ((char *) &inf, (char *) arg, 0, sizeof (inf));
859 dev = inf.device;
860
861 if (dev < 0 || dev >= num_midis)
862 return RET_ERROR (ENXIO);
863
864 IOCTL_TO_USER ((char *) arg, 0, (char *) &(midi_devs[dev]->info), sizeof (inf));
865 return 0;
866 }
867 break;
868
869 case SNDCTL_PMGR_IFACE:
870 {
871 struct patmgr_info *inf;
872 int dev, err;
873
874 inf = (struct patmgr_info *) KERNEL_MALLOC (sizeof (*inf));
875
876 IOCTL_FROM_USER ((char *) inf, (char *) arg, 0, sizeof (*inf));
877 dev = inf->device;
878
879 if (dev < 0 || dev >= num_synths)
880 {
881 KERNEL_FREE (inf);
882 return RET_ERROR (ENXIO);
883 }
884
885 if (!synth_devs[dev]->pmgr_interface)
886 {
887 KERNEL_FREE (inf);
888 return RET_ERROR (ENXIO);
889 }
890
891 if ((err = synth_devs[dev]->pmgr_interface (dev, inf)) == -1)
892 {
893 KERNEL_FREE (inf);
894 return err;
895 }
896
897 IOCTL_TO_USER ((char *) arg, 0, (char *) inf, sizeof (*inf));
898 KERNEL_FREE (inf);
899 return 0;
900 }
901 break;
902
903 case SNDCTL_PMGR_ACCESS:
904 {
905 struct patmgr_info *inf;
906 int dev, err;
907
908 inf = (struct patmgr_info *) KERNEL_MALLOC (sizeof (*inf));
909
910 IOCTL_FROM_USER ((char *) inf, (char *) arg, 0, sizeof (*inf));
911 dev = inf->device;
912
913 if (dev < 0 || dev >= num_synths)
914 {
915 KERNEL_FREE (inf);
916 return RET_ERROR (ENXIO);
917 }
918
919 if (!pmgr_present[dev])
920 {
921 KERNEL_FREE (inf);
922 return RET_ERROR (ESRCH);
923 }
924
925 if ((err = pmgr_access (dev, inf)) < 0)
926 {
927 KERNEL_FREE (inf);
928 return err;
929 }
930
931 IOCTL_TO_USER ((char *) arg, 0, (char *) inf, sizeof (*inf));
932 KERNEL_FREE (inf);
933 return 0;
934 }
935 break;
936
937 case SNDCTL_SEQ_TRESHOLD:
938 {
939 int tmp = IOCTL_IN (arg);
940
941 if (dev)
942 return RET_ERROR (EIO);
943
944 if (tmp < 1)
945 tmp = 1;
946 if (tmp >= SEQ_MAX_QUEUE)
947 tmp = SEQ_MAX_QUEUE - 1;
948 output_treshold = tmp;
949 return 0;
950 }
951 break;
952
953 default:
954 if (dev)
955 return RET_ERROR (EIO);
956
957 if (mode == OPEN_READ)
958 return RET_ERROR (EIO);
959
960 if (!synth_devs[0])
961 return RET_ERROR (ENXIO);
962 if (!(synth_open_mask & (1 << 0)))
963 return RET_ERROR (ENXIO);
964 return synth_devs[0]->ioctl (0, cmd, arg);
965 break;
966 }
967
968 return RET_ERROR (EINVAL);
969 }
970
971 #ifdef ALLOW_SELECT
972 int
973 sequencer_select (int dev, struct fileinfo *file, int sel_type, select_table * wait)
974 {
975 dev = dev >> 4;
976
977 switch (sel_type)
978 {
979 case SEL_IN:
980 if (!iqlen)
981 {
982 select_wait (&midi_sleeper, wait);
983 return 0;
984 }
985 return 1;
986
987 break;
988
989 case SEL_OUT:
990 if (qlen >= SEQ_MAX_QUEUE)
991 {
992 select_wait (&seq_sleeper, wait);
993 return 0;
994 }
995 return 1;
996 break;
997
998 case SEL_EX:
999 return 0;
1000 }
1001
1002 return 0;
1003 }
1004
1005 #endif
1006
1007 void
1008 sequencer_timer (void)
1009 {
1010 seq_startplay ();
1011 }
1012
1013 int
1014 note_to_freq (int note_num)
1015 {
1016
1017
1018
1019
1020
1021 int note, octave, note_freq;
1022 int notes[] =
1023 {
1024 261632, 277189, 293671, 311132, 329632, 349232,
1025 369998, 391998, 415306, 440000, 466162, 493880
1026 };
1027
1028 #define BASE_OCTAVE 5
1029
1030 octave = note_num / 12;
1031 note = note_num % 12;
1032
1033 note_freq = notes[note];
1034
1035 if (octave < BASE_OCTAVE)
1036 note_freq >>= (BASE_OCTAVE - octave);
1037 else if (octave > BASE_OCTAVE)
1038 note_freq <<= (octave - BASE_OCTAVE);
1039
1040
1041
1042 return note_freq;
1043 }
1044
1045 unsigned long
1046 compute_finetune (unsigned long base_freq, int bend, int range)
1047 {
1048 unsigned long amount;
1049 int negative, semitones, cents;
1050
1051 if (!bend)
1052 return base_freq;
1053 if (!range)
1054 return base_freq;
1055
1056 if (!base_freq)
1057 return base_freq;
1058
1059 if (range >= 8192)
1060 range = 8191;
1061
1062 bend = bend * range / 8192;
1063 if (!bend)
1064 return base_freq;
1065
1066 negative = bend < 0 ? 1 : 0;
1067
1068 if (bend < 0)
1069 bend *= -1;
1070 if (bend > range)
1071 bend = range;
1072
1073 if (bend > 2399)
1074 bend = 2399;
1075
1076 semitones = bend / 100;
1077 cents = bend % 100;
1078
1079 amount = semitone_tuning[semitones] * cent_tuning[cents] / 10000;
1080
1081 if (negative)
1082 return (base_freq * 10000) / amount;
1083 else
1084 return (base_freq * amount) / 10000;
1085 }
1086
1087
1088 long
1089 sequencer_init (long mem_start)
1090 {
1091
1092 sequencer_ok = 1;
1093 return mem_start;
1094 }
1095
1096 #else
1097
1098 int
1099 sequencer_read (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
1100 {
1101 return RET_ERROR (EIO);
1102 }
1103
1104 int
1105 sequencer_write (int dev, struct fileinfo *file, snd_rw_buf * buf, int count)
1106 {
1107 return RET_ERROR (EIO);
1108 }
1109
1110 int
1111 sequencer_open (int dev, struct fileinfo *file)
1112 {
1113 return RET_ERROR (ENXIO);
1114 }
1115
1116 void
1117 sequencer_release (int dev, struct fileinfo *file)
1118 {
1119 }
1120 int
1121 sequencer_ioctl (int dev, struct fileinfo *file,
1122 unsigned int cmd, unsigned int arg)
1123 {
1124 return RET_ERROR (EIO);
1125 }
1126
1127 int
1128 sequencer_lseek (int dev, struct fileinfo *file, off_t offset, int orig)
1129 {
1130 return RET_ERROR (EIO);
1131 }
1132
1133 long
1134 sequencer_init (long mem_start)
1135 {
1136 return mem_start;
1137 }
1138
1139 int
1140 sequencer_select (int dev, struct fileinfo *file, int sel_type, select_table * wait)
1141 {
1142 return RET_ERROR (EIO);
1143 }
1144
1145 #endif
1146
1147 #endif