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