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