This source file includes following definitions.
- enable_opl3_mode
- enter_4op_mode
- opl3_ioctl
- opl3_detect
- opl3_kill_note
- store_instr
- opl3_set_instr
- calc_vol
- set_voice_volume
- opl3_start_note
- freq_to_fnum
- opl3_command
- opl3_reset
- opl3_open
- opl3_close
- opl3_hw_control
- opl3_load_patch
- opl3_panning
- opl3_volume_method
- opl3_aftertouch
- bend_pitch
- opl3_controller
- opl3_patchmgr
- opl3_bender
- opl3_alloc_voice
- opl3_setup_voice
- opl3_init
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
31
32
33
34
35
36
37 #include "sound_config.h"
38
39 #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_YM3812)
40
41 #include "opl3.h"
42
43 #define MAX_VOICE 18
44 #define OFFS_4OP 11
45
46
47
48 static int opl3_enabled = 0;
49 static int opl4_enabled = 0;
50 static int left_address = 0x388, right_address = 0x388, both_address = 0;
51
52 static int nr_voices = 9;
53 static int logical_voices[MAX_VOICE] =
54 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
55
56 struct voice_info
57 {
58 unsigned char keyon_byte;
59 long bender;
60 long bender_range;
61 unsigned long orig_freq;
62 unsigned long current_freq;
63 int mode;
64 };
65
66 static struct voice_info voices[MAX_VOICE];
67 static struct voice_alloc_info *voice_alloc;
68 static struct channel_info *chn_info;
69
70 static struct sbi_instrument *instrmap;
71 static struct sbi_instrument *active_instrument[MAX_VOICE] =
72 {NULL};
73
74 static struct synth_info fm_info =
75 {"OPL-2", 0, SYNTH_TYPE_FM, FM_TYPE_ADLIB, 0, 9, 0, SBFM_MAXINSTR, 0};
76
77 static int already_initialized = 0;
78
79 static int opl3_ok = 0;
80 static int opl3_busy = 0;
81 static int fm_model = 0;
82
83
84
85
86
87 static int store_instr (int instr_no, struct sbi_instrument *instr);
88 static void freq_to_fnum (int freq, int *block, int *fnum);
89 static void opl3_command (int io_addr, unsigned int addr, unsigned int val);
90 static int opl3_kill_note (int dev, int voice, int note, int velocity);
91 static unsigned char connection_mask = 0x00;
92
93 void
94 enable_opl3_mode (int left, int right, int both)
95 {
96 if (opl3_enabled)
97 return;
98
99 opl3_enabled = 1;
100 left_address = left;
101 right_address = right;
102 both_address = both;
103 fm_info.capabilities = SYNTH_CAP_OPL3;
104 fm_info.synth_subtype = FM_TYPE_OPL3;
105 }
106
107 static void
108 enter_4op_mode (void)
109 {
110 int i;
111 static int voices_4op[MAX_VOICE] =
112 {0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17};
113
114 connection_mask = 0x3f;
115 opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x3f);
116
117 for (i = 0; i < 3; i++)
118 physical_voices[i].voice_mode = 4;
119 for (i = 3; i < 6; i++)
120 physical_voices[i].voice_mode = 0;
121
122 for (i = 9; i < 12; i++)
123 physical_voices[i].voice_mode = 4;
124 for (i = 12; i < 15; i++)
125 physical_voices[i].voice_mode = 0;
126
127 for (i = 0; i < 12; i++)
128 logical_voices[i] = voices_4op[i];
129 voice_alloc->max_voice = nr_voices = 12;
130 }
131
132 static int
133 opl3_ioctl (int dev,
134 unsigned int cmd, unsigned int arg)
135 {
136 switch (cmd)
137 {
138
139 case SNDCTL_FM_LOAD_INSTR:
140 {
141 struct sbi_instrument ins;
142
143 IOCTL_FROM_USER ((char *) &ins, (char *) arg, 0, sizeof (ins));
144
145 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
146 {
147 printk ("FM Error: Invalid instrument number %d\n", ins.channel);
148 return RET_ERROR (EINVAL);
149 }
150
151 pmgr_inform (dev, PM_E_PATCH_LOADED, ins.channel, 0, 0, 0);
152 return store_instr (ins.channel, &ins);
153 }
154 break;
155
156 case SNDCTL_SYNTH_INFO:
157 fm_info.nr_voices = (nr_voices == 12) ? 6 : nr_voices;
158
159 IOCTL_TO_USER ((char *) arg, 0, &fm_info, sizeof (fm_info));
160 return 0;
161 break;
162
163 case SNDCTL_SYNTH_MEMAVL:
164 return 0x7fffffff;
165 break;
166
167 case SNDCTL_FM_4OP_ENABLE:
168 if (opl3_enabled)
169 enter_4op_mode ();
170 return 0;
171 break;
172
173 default:
174 return RET_ERROR (EINVAL);
175 }
176
177 }
178
179 int
180 opl3_detect (int ioaddr)
181 {
182
183
184
185
186
187
188
189
190
191
192
193 unsigned char stat1, stat2, signature;
194 int i;
195
196 if (already_initialized)
197 {
198 return 0;
199
200
201 }
202
203 if (opl3_enabled)
204 ioaddr = left_address;
205
206
207 opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
208
209
210 opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
211
212 signature = stat1 = INB (ioaddr);
213
214 if ((stat1 & 0xE0) != 0x00)
215 {
216 return 0;
217
218
219 }
220
221 opl3_command (ioaddr, TIMER1_REGISTER, 0xff);
222
223 opl3_command (ioaddr, TIMER_CONTROL_REGISTER,
224 TIMER2_MASK | TIMER1_START);
225
226
227
228
229
230
231
232 for (i = 0; i < 50; i++)
233 tenmicrosec ();
234
235 stat2 = INB (ioaddr);
236
237
238
239
240
241
242
243
244 opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
245
246 opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
247
248 if ((stat2 & 0xE0) != 0xc0)
249 {
250 return 0;
251
252
253 }
254
255
256
257
258
259 if (signature == 0x06)
260 {
261 opl3_enabled = 0;
262 }
263 else if (signature == 0x00)
264 {
265 unsigned char tmp;
266
267 if (!opl3_enabled)
268 {
269 left_address = ioaddr;
270 right_address = ioaddr + 2;
271 opl3_enabled = 1;
272 }
273
274
275
276
277
278
279
280 opl3_command (right_address, OPL3_MODE_REGISTER, 0x00);
281 opl3_command (right_address, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
282
283 if ((tmp = INB (ioaddr)) == 0x02)
284 {
285 opl4_enabled = 1;
286 }
287 opl3_command (right_address, OPL3_MODE_REGISTER, 0);
288
289 }
290
291 for (i = 0; i < 9; i++)
292 opl3_command (ioaddr, KEYON_BLOCK + i, 0);
293
294
295
296 opl3_command (ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
297 opl3_command (ioaddr, PERCUSSION_REGISTER, 0x00);
298
299
300
301 return 1;
302 }
303
304 static int
305 opl3_kill_note (int dev, int voice, int note, int velocity)
306 {
307 struct physical_voice_info *map;
308
309 if (voice < 0 || voice >= nr_voices)
310 return 0;
311
312 voice_alloc->map[voice] = 0;
313
314 map = &physical_voices[logical_voices[voice]];
315
316 DEB (printk ("Kill note %d\n", voice));
317
318 if (map->voice_mode == 0)
319 return 0;
320
321 opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, voices[voice].keyon_byte & ~0x20);
322
323 voices[voice].keyon_byte = 0;
324 voices[voice].bender = 0;
325 voices[voice].bender_range = 200;
326
327
328 voices[voice].orig_freq = 0;
329 voices[voice].current_freq = 0;
330 voices[voice].mode = 0;
331
332 return 0;
333 }
334
335 #define HIHAT 0
336 #define CYMBAL 1
337 #define TOMTOM 2
338 #define SNARE 3
339 #define BDRUM 4
340 #define UNDEFINED TOMTOM
341 #define DEFAULT TOMTOM
342
343 static int
344 store_instr (int instr_no, struct sbi_instrument *instr)
345 {
346
347 if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || !opl3_enabled))
348 printk ("FM warning: Invalid patch format field (key) 0x%x\n", instr->key);
349 memcpy ((char *) &(instrmap[instr_no]), (char *) instr, sizeof (*instr));
350
351 return 0;
352 }
353
354 static int
355 opl3_set_instr (int dev, int voice, int instr_no)
356 {
357 if (voice < 0 || voice >= nr_voices)
358 return 0;
359
360 if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
361 return 0;
362
363 active_instrument[voice] = &instrmap[instr_no];
364 return 0;
365 }
366
367
368
369
370
371
372
373
374
375
376 char fm_volume_table[128] =
377 {-64, -48, -40, -35, -32, -29, -27, -26,
378
379
380 -24, -23, -21, -20, -19, -18, -18, -17,
381
382
383 -16, -15, -15, -14, -13, -13, -12, -12,
384
385
386 -11, -11, -10, -10, -10, -9, -9, -8,
387
388
389 -8, -8, -7, -7, -7, -6, -6, -6,
390
391
392 -5, -5, -5, -5, -4, -4, -4, -4,
393
394
395 -3, -3, -3, -3, -2, -2, -2, -2,
396
397
398 -2, -1, -1, -1, -1, 0, 0, 0,
399
400
401 0, 0, 0, 1, 1, 1, 1, 1,
402
403
404 1, 2, 2, 2, 2, 2, 2, 2,
405
406
407 3, 3, 3, 3, 3, 3, 3, 4,
408
409
410 4, 4, 4, 4, 4, 4, 4, 5,
411
412
413 5, 5, 5, 5, 5, 5, 5, 5,
414
415
416 6, 6, 6, 6, 6, 6, 6, 6,
417
418
419 6, 7, 7, 7, 7, 7, 7, 7,
420
421
422 7, 7, 7, 8, 8, 8, 8, 8};
423
424
425
426
427 static void
428 calc_vol (unsigned char *regbyte, int volume)
429 {
430 int level = (~*regbyte & 0x3f);
431
432 if (level)
433 level += fm_volume_table[volume];
434
435 if (level > 0x3f)
436 level = 0x3f;
437 if (level < 0)
438 level = 0;
439
440 *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
441 }
442
443 static void
444 set_voice_volume (int voice, int volume)
445 {
446 unsigned char vol1, vol2, vol3, vol4;
447 struct sbi_instrument *instr;
448 struct physical_voice_info *map;
449
450 if (voice < 0 || voice >= nr_voices)
451 return;
452
453 map = &physical_voices[logical_voices[voice]];
454
455 instr = active_instrument[voice];
456
457 if (!instr)
458 instr = &instrmap[0];
459
460 if (instr->channel < 0)
461 return;
462
463 if (voices[voice].mode == 0)
464 return;
465
466 if (voices[voice].mode == 2)
467 {
468
469
470
471 vol1 = instr->operators[2];
472 vol2 = instr->operators[3];
473
474 if ((instr->operators[10] & 0x01))
475 {
476
477
478 calc_vol (&vol1, volume);
479 calc_vol (&vol2, volume);
480 }
481 else
482 {
483
484
485 calc_vol (&vol2, volume);
486 }
487
488 opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1);
489
490
491
492 opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2);
493
494
495
496 }
497 else
498 {
499
500
501 int connection;
502
503 vol1 = instr->operators[2];
504 vol2 = instr->operators[3];
505 vol3 = instr->operators[OFFS_4OP + 2];
506 vol4 = instr->operators[OFFS_4OP + 3];
507
508
509
510
511
512
513 connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
514
515 switch (connection)
516 {
517 case 0:
518 calc_vol (&vol4, volume);
519
520
521 break;
522
523 case 1:
524 calc_vol (&vol2, volume);
525 calc_vol (&vol4, volume);
526 break;
527
528 case 2:
529 calc_vol (&vol1, volume);
530 calc_vol (&vol4, volume);
531 break;
532
533 case 3:
534 calc_vol (&vol1, volume);
535 calc_vol (&vol3, volume);
536 calc_vol (&vol4, volume);
537 break;
538
539 default:
540
541 ;
542 }
543
544 opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1);
545 opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2);
546 opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], vol3);
547 opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], vol4);
548 }
549 }
550
551 static int
552 opl3_start_note (int dev, int voice, int note, int volume)
553 {
554 unsigned char data, fpc;
555 int block, fnum, freq, voice_mode;
556 struct sbi_instrument *instr;
557 struct physical_voice_info *map;
558
559 if (voice < 0 || voice >= nr_voices)
560 return 0;
561
562 map = &physical_voices[logical_voices[voice]];
563
564 if (map->voice_mode == 0)
565 return 0;
566
567 if (note == 255)
568
569
570 {
571 set_voice_volume (voice, volume);
572 return 0;
573 }
574
575
576
577
578 opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], 0xff);
579
580
581
582
583 opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], 0xff);
584
585
586
587
588 if (map->voice_mode == 4)
589 {
590 opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
591 opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
592 }
593
594 opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00);
595
596
597
598
599 instr = active_instrument[voice];
600
601 if (!instr)
602 instr = &instrmap[0];
603
604 if (instr->channel < 0)
605 {
606 printk (
607 "OPL3: Initializing voice %d with undefined instrument\n",
608 voice);
609 return 0;
610 }
611
612 if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
613 return 0;
614
615
616
617 voice_mode = map->voice_mode;
618
619 if (voice_mode == 4)
620 {
621 int voice_shift;
622
623 voice_shift = (map->ioaddr == left_address) ? 0 : 3;
624 voice_shift += map->voice_num;
625
626 if (instr->key != OPL3_PATCH)
627
628
629 {
630 voice_mode = 2;
631 connection_mask &= ~(1 << voice_shift);
632 }
633 else
634 {
635 connection_mask |= (1 << voice_shift);
636 }
637
638 opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);
639 }
640
641
642
643
644 opl3_command (map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
645 opl3_command (map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
646
647
648
649
650 opl3_command (map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
651 opl3_command (map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
652
653
654
655
656 opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
657 opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
658
659
660
661
662 opl3_command (map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
663 opl3_command (map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
664
665
666
667
668 fpc = instr->operators[10];
669 if (!(fpc & 0x30))
670 fpc |= 0x30;
671
672
673 opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num,
674 fpc);
675
676
677
678
679
680 if (voice_mode == 4)
681 {
682
683
684
685
686 opl3_command (map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);
687 opl3_command (map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
688
689
690
691
692 opl3_command (map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);
693 opl3_command (map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
694
695
696
697
698 opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);
699 opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
700
701
702
703
704 opl3_command (map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);
705 opl3_command (map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
706
707
708
709
710 fpc = instr->operators[OFFS_4OP + 10];
711 if (!(fpc & 0x30))
712 fpc |= 0x30;
713
714
715 opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
716 }
717
718 voices[voice].mode = voice_mode;
719
720 set_voice_volume (voice, volume);
721
722 freq = voices[voice].orig_freq = note_to_freq (note) / 1000;
723
724
725
726
727
728
729 freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
730 voices[voice].current_freq = freq;
731
732 freq_to_fnum (freq, &block, &fnum);
733
734
735
736
737
738 data = fnum & 0xff;
739
740
741 opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
742
743 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
744 voices[voice].keyon_byte = data;
745 opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
746 if (voice_mode == 4)
747 opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
748
749 return 0;
750 }
751
752 static void
753 freq_to_fnum (int freq, int *block, int *fnum)
754 {
755 int f, octave;
756
757
758
759
760
761
762
763
764 f = freq;
765
766 octave = 5;
767
768 if (f == 0)
769 octave = 0;
770 else if (f < 261)
771 {
772 while (f < 261)
773 {
774 octave--;
775 f <<= 1;
776 }
777 }
778 else if (f > 493)
779 {
780 while (f > 493)
781 {
782 octave++;
783 f >>= 1;
784 }
785 }
786
787 if (octave > 7)
788 octave = 7;
789
790 *fnum = freq * (1 << (20 - octave)) / 49716;
791 *block = octave;
792 }
793
794 static void
795 opl3_command (int io_addr, unsigned int addr, unsigned int val)
796 {
797 int i;
798
799
800
801
802
803
804 OUTB ((unsigned char) (addr & 0xff), io_addr);
805
806
807
808
809 if (!opl3_enabled)
810 tenmicrosec ();
811 else
812 for (i = 0; i < 2; i++)
813 INB (io_addr);
814
815 OUTB ((unsigned char) (val & 0xff), io_addr + 1);
816
817
818
819
820 if (!opl3_enabled)
821 {
822 tenmicrosec ();
823 tenmicrosec ();
824 tenmicrosec ();
825 }
826 else
827 for (i = 0; i < 2; i++)
828 INB (io_addr);
829 }
830
831 static void
832 opl3_reset (int dev)
833 {
834 int i;
835
836 for (i = 0; i < nr_voices; i++)
837 {
838 opl3_command (physical_voices[logical_voices[i]].ioaddr,
839 KSL_LEVEL + physical_voices[logical_voices[i]].op[0], 0xff);
840
841 opl3_command (physical_voices[logical_voices[i]].ioaddr,
842 KSL_LEVEL + physical_voices[logical_voices[i]].op[1], 0xff);
843
844 if (physical_voices[logical_voices[i]].voice_mode == 4)
845 {
846 opl3_command (physical_voices[logical_voices[i]].ioaddr,
847 KSL_LEVEL + physical_voices[logical_voices[i]].op[2], 0xff);
848
849 opl3_command (physical_voices[logical_voices[i]].ioaddr,
850 KSL_LEVEL + physical_voices[logical_voices[i]].op[3], 0xff);
851 }
852
853 opl3_kill_note (dev, i, 0, 64);
854 }
855
856 if (opl3_enabled)
857 {
858 voice_alloc->max_voice = nr_voices = 18;
859
860 for (i = 0; i < 18; i++)
861 logical_voices[i] = i;
862
863 for (i = 0; i < 18; i++)
864 physical_voices[i].voice_mode = 2;
865
866 }
867
868 }
869
870 static int
871 opl3_open (int dev, int mode)
872 {
873 int i;
874
875 if (!opl3_ok)
876 return RET_ERROR (ENXIO);
877 if (opl3_busy)
878 return RET_ERROR (EBUSY);
879 opl3_busy = 1;
880
881 voice_alloc->max_voice = nr_voices = opl3_enabled ? 18 : 9;
882 voice_alloc->timestamp = 0;
883
884 for (i = 0; i < 18; i++)
885 {
886 voice_alloc->map[i] = 0;
887 voice_alloc->alloc_times[i] = 0;
888 }
889
890 connection_mask = 0x00;
891
892
893 if (opl3_enabled)
894 opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);
895 return 0;
896 }
897
898 static void
899 opl3_close (int dev)
900 {
901 opl3_busy = 0;
902 voice_alloc->max_voice = nr_voices = opl3_enabled ? 18 : 9;
903
904 fm_info.nr_drums = 0;
905 fm_info.perc_mode = 0;
906
907 opl3_reset (dev);
908 }
909
910 static void
911 opl3_hw_control (int dev, unsigned char *event)
912 {
913 }
914
915 static int
916 opl3_load_patch (int dev, int format, snd_rw_buf * addr,
917 int offs, int count, int pmgr_flag)
918 {
919 struct sbi_instrument ins;
920
921 if (count < sizeof (ins))
922 {
923 printk ("FM Error: Patch record too short\n");
924 return RET_ERROR (EINVAL);
925 }
926
927 COPY_FROM_USER (&((char *) &ins)[offs], (char *) addr, offs, sizeof (ins) - offs);
928
929 if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
930 {
931 printk ("FM Error: Invalid instrument number %d\n", ins.channel);
932 return RET_ERROR (EINVAL);
933 }
934 ins.key = format;
935
936 return store_instr (ins.channel, &ins);
937 }
938
939 static void
940 opl3_panning (int dev, int voice, int pressure)
941 {
942 }
943
944 static void
945 opl3_volume_method (int dev, int mode)
946 {
947 }
948
949 #define SET_VIBRATO(cell) { \
950 tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
951 if (pressure > 110) \
952 tmp |= 0x40; \
953 opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
954
955 static void
956 opl3_aftertouch (int dev, int voice, int pressure)
957 {
958 int tmp;
959 struct sbi_instrument *instr;
960 struct physical_voice_info *map;
961
962 if (voice < 0 || voice >= nr_voices)
963 return;
964
965 map = &physical_voices[logical_voices[voice]];
966
967 DEB (printk ("Aftertouch %d\n", voice));
968
969 if (map->voice_mode == 0)
970 return;
971
972
973
974
975
976 instr = active_instrument[voice];
977
978 if (!instr)
979 instr = &instrmap[0];
980
981 if (voices[voice].mode == 4)
982 {
983 int connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
984
985 switch (connection)
986 {
987 case 0:
988 SET_VIBRATO (4);
989 break;
990
991 case 1:
992 SET_VIBRATO (2);
993 SET_VIBRATO (4);
994 break;
995
996 case 2:
997 SET_VIBRATO (1);
998 SET_VIBRATO (4);
999 break;
1000
1001 case 3:
1002 SET_VIBRATO (1);
1003 SET_VIBRATO (3);
1004 SET_VIBRATO (4);
1005 break;
1006
1007 }
1008
1009
1010
1011 }
1012 else
1013 {
1014 SET_VIBRATO (1);
1015
1016 if ((instr->operators[10] & 0x01))
1017
1018
1019 SET_VIBRATO (2);
1020 }
1021 }
1022
1023 #undef SET_VIBRATO
1024
1025 static void
1026 bend_pitch (int dev, int voice, int value)
1027 {
1028 unsigned char data;
1029 int block, fnum, freq;
1030 struct physical_voice_info *map;
1031
1032 map = &physical_voices[logical_voices[voice]];
1033
1034 if (map->voice_mode == 0)
1035 return;
1036
1037 voices[voice].bender = value;
1038 if (!value)
1039 return;
1040 if (!(voices[voice].keyon_byte & 0x20))
1041 return;
1042
1043
1044
1045 freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
1046 voices[voice].current_freq = freq;
1047
1048 freq_to_fnum (freq, &block, &fnum);
1049
1050 data = fnum & 0xff;
1051
1052
1053 opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
1054
1055 data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
1056
1057
1058
1059
1060
1061
1062
1063 voices[voice].keyon_byte = data;
1064 opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
1065 }
1066
1067 static void
1068 opl3_controller (int dev, int voice, int ctrl_num, int value)
1069 {
1070 if (voice < 0 || voice >= nr_voices)
1071 return;
1072
1073 switch (ctrl_num)
1074 {
1075 case CTRL_PITCH_BENDER:
1076 bend_pitch (dev, voice, value);
1077 break;
1078
1079 case CTRL_PITCH_BENDER_RANGE:
1080 voices[voice].bender_range = value;
1081 break;
1082 }
1083 }
1084
1085 static int
1086 opl3_patchmgr (int dev, struct patmgr_info *rec)
1087 {
1088 return RET_ERROR (EINVAL);
1089 }
1090
1091 static void
1092 opl3_bender (int dev, int voice, int value)
1093 {
1094 if (voice < 0 || voice >= nr_voices)
1095 return;
1096
1097 bend_pitch (dev, voice, value - 8192);
1098 }
1099
1100 static int
1101 opl3_alloc_voice (int dev, int chn, int note, struct voice_alloc_info *alloc)
1102 {
1103 int i, p, best, first, avail_voices, best_time = 0x7fffffff;
1104 struct sbi_instrument *instr;
1105 int is4op;
1106 int instr_no;
1107
1108 if (chn < 0 || chn > 15)
1109 instr_no = 0;
1110 else
1111 instr_no = chn_info[chn].pgm_num;
1112
1113 instr = &instrmap[instr_no];
1114 if (instr->channel < 0 ||
1115 nr_voices != 12)
1116 is4op = 0;
1117 else if (nr_voices == 12)
1118 is4op = (instr->key == OPL3_PATCH);
1119 else
1120 is4op = 0;
1121
1122 if (is4op)
1123 {
1124 first = p = 0;
1125 avail_voices = 6;
1126 }
1127 else
1128 {
1129 if (nr_voices == 12)
1130 first = p = 6;
1131 else
1132 first = p = 0;
1133 avail_voices = nr_voices;
1134 }
1135
1136
1137
1138
1139 best = first;
1140
1141 for (i = 0; i < avail_voices; i++)
1142 {
1143 if (alloc->map[p] == 0)
1144 {
1145 return p;
1146 }
1147 if (alloc->alloc_times[p] < best_time)
1148 {
1149 best_time = alloc->alloc_times[p];
1150 best = p;
1151 }
1152 p = (p + 1) % avail_voices;
1153 }
1154
1155
1156
1157
1158
1159 if (best < 0)
1160 best = 0;
1161 if (best > nr_voices)
1162 best -= nr_voices;
1163
1164 return best;
1165 }
1166
1167 static void
1168 opl3_setup_voice (int dev, int voice, int chn)
1169 {
1170 struct channel_info *info =
1171 &synth_devs[dev]->chn_info[chn];
1172
1173 opl3_set_instr (dev, voice,
1174 info->pgm_num);
1175
1176 voices[voice].bender = info->bender_value;
1177 }
1178
1179 static struct synth_operations opl3_operations =
1180 {
1181 &fm_info,
1182 0,
1183 SYNTH_TYPE_FM,
1184 FM_TYPE_ADLIB,
1185 opl3_open,
1186 opl3_close,
1187 opl3_ioctl,
1188 opl3_kill_note,
1189 opl3_start_note,
1190 opl3_set_instr,
1191 opl3_reset,
1192 opl3_hw_control,
1193 opl3_load_patch,
1194 opl3_aftertouch,
1195 opl3_controller,
1196 opl3_panning,
1197 opl3_volume_method,
1198 opl3_patchmgr,
1199 opl3_bender,
1200 opl3_alloc_voice,
1201 opl3_setup_voice
1202 };
1203
1204 long
1205 opl3_init (long mem_start)
1206 {
1207 int i;
1208
1209 PERMANENT_MALLOC (struct sbi_instrument *, instrmap,
1210 SBFM_MAXINSTR * sizeof (*instrmap), mem_start);
1211
1212 if (num_synths >= MAX_SYNTH_DEV)
1213 printk ("OPL3 Error: Too many synthesizers\n");
1214 else
1215 {
1216 synth_devs[num_synths++] = &opl3_operations;
1217 voice_alloc = &opl3_operations.alloc;
1218 chn_info = &opl3_operations.chn_info[0];
1219 }
1220
1221 fm_model = 0;
1222 opl3_ok = 1;
1223 if (opl3_enabled)
1224 {
1225 if (opl4_enabled)
1226 printk (" <Yamaha OPL4/OPL3 FM>");
1227 else
1228 printk (" <Yamaha OPL-3 FM>");
1229
1230 fm_model = 2;
1231 voice_alloc->max_voice = nr_voices = 18;
1232 fm_info.nr_drums = 0;
1233 fm_info.capabilities |= SYNTH_CAP_OPL3;
1234 strcpy (fm_info.name, "Yamaha OPL-3");
1235
1236 for (i = 0; i < 18; i++)
1237 if (physical_voices[i].ioaddr == USE_LEFT)
1238 physical_voices[i].ioaddr = left_address;
1239 else
1240 physical_voices[i].ioaddr = right_address;
1241
1242
1243 opl3_command (right_address, OPL3_MODE_REGISTER, OPL3_ENABLE);
1244
1245
1246
1247
1248 opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x00);
1249
1250
1251
1252
1253
1254
1255 }
1256 else
1257 {
1258 printk (" <Yamaha 2-OP FM>");
1259 fm_model = 1;
1260 voice_alloc->max_voice = nr_voices = 9;
1261 fm_info.nr_drums = 0;
1262
1263 for (i = 0; i < 18; i++)
1264 physical_voices[i].ioaddr = left_address;
1265 };
1266
1267 already_initialized = 1;
1268 for (i = 0; i < SBFM_MAXINSTR; i++)
1269 instrmap[i].channel = -1;
1270
1271 return mem_start;
1272 }
1273
1274 #endif