1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
5 * Midi synth routines for OPL2/OPL3/OPL4 FM
11 #include "opl3_voice.h"
12 #include <sound/asoundef.h>
14 static void snd_opl3_note_off_unsafe(void *p
, int note
, int vel
,
15 struct snd_midi_channel
*chan
);
17 * The next table looks magical, but it certainly is not. Its values have
18 * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
19 * for i=0. This log-table converts a linear volume-scaling (0..127) to a
20 * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
21 * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
22 * volume -8 it was implemented as a table because it is only 128 bytes and
23 * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>)
26 static const char opl3_volume_table
[128] =
28 -63, -48, -40, -35, -32, -29, -27, -26,
29 -24, -23, -21, -20, -19, -18, -18, -17,
30 -16, -15, -15, -14, -13, -13, -12, -12,
31 -11, -11, -10, -10, -10, -9, -9, -8,
32 -8, -8, -7, -7, -7, -6, -6, -6,
33 -5, -5, -5, -5, -4, -4, -4, -4,
34 -3, -3, -3, -3, -2, -2, -2, -2,
35 -2, -1, -1, -1, -1, 0, 0, 0,
36 0, 0, 0, 1, 1, 1, 1, 1,
37 1, 2, 2, 2, 2, 2, 2, 2,
38 3, 3, 3, 3, 3, 3, 3, 4,
39 4, 4, 4, 4, 4, 4, 4, 5,
40 5, 5, 5, 5, 5, 5, 5, 5,
41 6, 6, 6, 6, 6, 6, 6, 6,
42 6, 7, 7, 7, 7, 7, 7, 7,
43 7, 7, 7, 8, 8, 8, 8, 8
46 void snd_opl3_calc_volume(unsigned char *volbyte
, int vel
,
47 struct snd_midi_channel
*chan
)
49 int oldvol
, newvol
, n
;
52 volume
= (vel
* chan
->gm_volume
* chan
->gm_expression
) / (127*127);
56 oldvol
= OPL3_TOTAL_LEVEL_MASK
- (*volbyte
& OPL3_TOTAL_LEVEL_MASK
);
58 newvol
= opl3_volume_table
[volume
] + oldvol
;
59 if (newvol
> OPL3_TOTAL_LEVEL_MASK
)
60 newvol
= OPL3_TOTAL_LEVEL_MASK
;
64 n
= OPL3_TOTAL_LEVEL_MASK
- (newvol
& OPL3_TOTAL_LEVEL_MASK
);
66 *volbyte
= (*volbyte
& OPL3_KSL_MASK
) | (n
& OPL3_TOTAL_LEVEL_MASK
);
70 * Converts the note frequency to block and fnum values for the FM chip
72 static const short opl3_note_table
[16] =
74 305, 323, /* for pitch bending, -2 semitones */
75 343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647,
76 686, 726 /* for pitch bending, +2 semitones */
79 static void snd_opl3_calc_pitch(unsigned char *fnum
, unsigned char *blocknum
,
80 int note
, struct snd_midi_channel
*chan
)
82 int block
= ((note
/ 12) & 0x07) - 1;
83 int idx
= (note
% 12) + 2;
86 if (chan
->midi_pitchbend
) {
87 int pitchbend
= chan
->midi_pitchbend
;
90 if (pitchbend
< -0x2000)
92 if (pitchbend
> 0x1FFF)
95 segment
= pitchbend
/ 0x1000;
96 freq
= opl3_note_table
[idx
+segment
];
97 freq
+= ((opl3_note_table
[idx
+segment
+1] - freq
) *
98 (pitchbend
% 0x1000)) / 0x1000;
100 freq
= opl3_note_table
[idx
];
103 *fnum
= (unsigned char) freq
;
104 *blocknum
= ((freq
>> 8) & OPL3_FNUM_HIGH_MASK
) |
105 ((block
<< 2) & OPL3_BLOCKNUM_MASK
);
110 static void debug_alloc(struct snd_opl3
*opl3
, char *s
, int voice
) {
114 printk(KERN_DEBUG
"time %.5i: %s [%.2i]: ", opl3
->use_time
, s
, voice
);
115 for (i
= 0; i
< opl3
->max_voices
; i
++)
116 printk(KERN_CONT
"%c", *(str
+ opl3
->voices
[i
].state
+ 1));
117 printk(KERN_CONT
"\n");
122 * Get a FM voice (channel) to play a note on.
124 static int opl3_get_voice(struct snd_opl3
*opl3
, int instr_4op
,
125 struct snd_midi_channel
*chan
) {
126 int chan_4op_1
; /* first voice for 4op instrument */
127 int chan_4op_2
; /* second voice for 4op instrument */
129 struct snd_opl3_voice
*vp
, *vp2
;
130 unsigned int voice_time
;
134 char *alloc_type
[3] = { "FREE ", "CHEAP ", "EXPENSIVE" };
137 /* This is our "allocation cost" table */
139 FREE
= 0, CHEAP
, EXPENSIVE
, END
142 /* Keeps track of what we are finding */
149 for (i
= 0; i
< END
; i
++) {
150 best
[i
].time
= (unsigned int)(-1); /* XXX MAX_?INT really */
154 /* Look through all the channels for the most suitable. */
155 for (i
= 0; i
< opl3
->max_voices
; i
++) {
156 vp
= &opl3
->voices
[i
];
158 if (vp
->state
== SNDRV_OPL3_ST_NOT_AVAIL
)
159 /* skip unavailable channels, allocated by
160 drum voices or by bounded 4op voices) */
163 voice_time
= vp
->time
;
166 chan_4op_1
= ((i
< 3) || (i
> 8 && i
< 12));
167 chan_4op_2
= ((i
> 2 && i
< 6) || (i
> 11 && i
< 15));
169 /* allocate 4op voice */
170 /* skip channels unavailable to 4op instrument */
175 /* kill one voice, CHEAP */
177 /* get state of bounded 2op channel
178 to be allocated for 4op instrument */
179 vp2
= &opl3
->voices
[i
+ 3];
180 if (vp2
->state
== SNDRV_OPL3_ST_ON_2OP
) {
181 /* kill two voices, EXPENSIVE */
183 voice_time
= (voice_time
> vp
->time
) ?
184 voice_time
: vp
->time
;
187 /* allocate 2op voice */
188 if ((chan_4op_1
) || (chan_4op_2
))
189 /* use bounded channels for 2op, CHEAP */
192 /* kill one voice on 2op channel, CHEAP */
194 /* raise kill cost to EXPENSIVE for all channels */
198 if (voice_time
< bp
->time
) {
199 bp
->time
= voice_time
;
204 for (i
= 0; i
< END
; i
++) {
205 if (best
[i
].voice
>= 0) {
207 printk(KERN_DEBUG
"%s %iop allocation on voice %i\n",
208 alloc_type
[i
], instr_4op
? 4 : 2,
211 return best
[i
].voice
;
218 /* ------------------------------ */
221 * System timer interrupt function
223 void snd_opl3_timer_func(struct timer_list
*t
)
226 struct snd_opl3
*opl3
= from_timer(opl3
, t
, tlist
);
231 spin_lock_irqsave(&opl3
->voice_lock
, flags
);
232 for (i
= 0; i
< opl3
->max_voices
; i
++) {
233 struct snd_opl3_voice
*vp
= &opl3
->voices
[i
];
234 if (vp
->state
> 0 && vp
->note_off_check
) {
235 if (vp
->note_off
== jiffies
)
236 snd_opl3_note_off_unsafe(opl3
, vp
->note
, 0,
242 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
244 spin_lock_irqsave(&opl3
->sys_timer_lock
, flags
);
246 mod_timer(&opl3
->tlist
, jiffies
+ 1); /* invoke again */
248 opl3
->sys_timer_status
= 0;
249 spin_unlock_irqrestore(&opl3
->sys_timer_lock
, flags
);
255 static void snd_opl3_start_timer(struct snd_opl3
*opl3
)
258 spin_lock_irqsave(&opl3
->sys_timer_lock
, flags
);
259 if (! opl3
->sys_timer_status
) {
260 mod_timer(&opl3
->tlist
, jiffies
+ 1);
261 opl3
->sys_timer_status
= 1;
263 spin_unlock_irqrestore(&opl3
->sys_timer_lock
, flags
);
266 /* ------------------------------ */
269 static const int snd_opl3_oss_map
[MAX_OPL3_VOICES
] = {
270 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14
276 void snd_opl3_note_on(void *p
, int note
, int vel
, struct snd_midi_channel
*chan
)
278 struct snd_opl3
*opl3
;
282 struct snd_opl3_voice
*vp
, *vp2
;
283 unsigned short connect_mask
;
284 unsigned char connection
;
285 unsigned char vol_op
[4];
289 unsigned short reg_side
;
290 unsigned char op_offset
;
291 unsigned char voice_offset
;
292 unsigned short opl3_reg
;
293 unsigned char reg_val
;
294 unsigned char prg
, bank
;
297 unsigned char fnum
, blocknum
;
300 struct fm_patch
*patch
;
301 struct fm_instrument
*fm
;
307 snd_printk(KERN_DEBUG
"Note on, ch %i, inst %i, note %i, vel %i\n",
308 chan
->number
, chan
->midi_program
, note
, vel
);
311 /* in SYNTH mode, application takes care of voices */
312 /* in SEQ mode, drum voice numbers are notes on drum channel */
313 if (opl3
->synth_mode
== SNDRV_OPL3_MODE_SEQ
) {
314 if (chan
->drum_channel
) {
315 /* percussion instruments are located in bank 128 */
319 bank
= chan
->gm_bank_select
;
320 prg
= chan
->midi_program
;
323 /* Prepare for OSS mode */
324 if (chan
->number
>= MAX_OPL3_VOICES
)
327 /* OSS instruments are located in bank 127 */
329 prg
= chan
->midi_program
;
332 spin_lock_irqsave(&opl3
->voice_lock
, flags
);
334 if (use_internal_drums
) {
335 snd_opl3_drum_switch(opl3
, note
, vel
, 1, chan
);
336 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
341 patch
= snd_opl3_find_patch(opl3
, prg
, bank
, 0);
343 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
348 switch (patch
->type
) {
353 if (opl3
->hardware
>= OPL3_HW_OPL3
) {
359 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
363 snd_printk(KERN_DEBUG
" --> OPL%i instrument: %s\n",
364 instr_4op
? 3 : 2, patch
->name
);
366 /* in SYNTH mode, application takes care of voices */
367 /* in SEQ mode, allocate voice on free OPL3 channel */
368 if (opl3
->synth_mode
== SNDRV_OPL3_MODE_SEQ
) {
369 voice
= opl3_get_voice(opl3
, instr_4op
, chan
);
371 /* remap OSS voice */
372 voice
= snd_opl3_oss_map
[chan
->number
];
376 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
380 if (voice
< MAX_OPL2_VOICES
) {
381 /* Left register block for voices 0 .. 8 */
382 reg_side
= OPL3_LEFT
;
383 voice_offset
= voice
;
384 connect_mask
= (OPL3_LEFT_4OP_0
<< voice_offset
) & 0x07;
386 /* Right register block for voices 9 .. 17 */
387 reg_side
= OPL3_RIGHT
;
388 voice_offset
= voice
- MAX_OPL2_VOICES
;
389 connect_mask
= (OPL3_RIGHT_4OP_0
<< voice_offset
) & 0x38;
392 /* kill voice on channel */
393 vp
= &opl3
->voices
[voice
];
395 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
396 reg_val
= vp
->keyon_reg
& ~OPL3_KEYON_BIT
;
397 opl3
->command(opl3
, opl3_reg
, reg_val
);
400 vp2
= &opl3
->voices
[voice
+ 3];
402 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+
404 reg_val
= vp
->keyon_reg
& ~OPL3_KEYON_BIT
;
405 opl3
->command(opl3
, opl3_reg
, reg_val
);
409 /* set connection register */
411 if ((opl3
->connection_reg
^ connect_mask
) & connect_mask
) {
412 opl3
->connection_reg
|= connect_mask
;
413 /* set connection bit */
414 opl3_reg
= OPL3_RIGHT
| OPL3_REG_CONNECTION_SELECT
;
415 opl3
->command(opl3
, opl3_reg
, opl3
->connection_reg
);
418 if ((opl3
->connection_reg
^ ~connect_mask
) & connect_mask
) {
419 opl3
->connection_reg
&= ~connect_mask
;
420 /* clear connection bit */
421 opl3_reg
= OPL3_RIGHT
| OPL3_REG_CONNECTION_SELECT
;
422 opl3
->command(opl3
, opl3_reg
, opl3
->connection_reg
);
427 snd_printk(KERN_DEBUG
" --> setting OPL3 connection: 0x%x\n",
428 opl3
->connection_reg
);
431 * calculate volume depending on connection
432 * between FM operators (see include/opl3.h)
434 for (i
= 0; i
< (instr_4op
? 4 : 2); i
++)
435 vol_op
[i
] = fm
->op
[i
].ksl_level
;
437 connection
= fm
->feedback_connection
[0] & 0x01;
440 connection
|= fm
->feedback_connection
[1] & 0x01;
442 snd_opl3_calc_volume(&vol_op
[3], vel
, chan
);
443 switch (connection
) {
445 snd_opl3_calc_volume(&vol_op
[2], vel
, chan
);
448 snd_opl3_calc_volume(&vol_op
[0], vel
, chan
);
451 snd_opl3_calc_volume(&vol_op
[1], vel
, chan
);
454 snd_opl3_calc_volume(&vol_op
[1], vel
, chan
);
456 snd_opl3_calc_volume(&vol_op
[0], vel
, chan
);
459 /* Program the FM voice characteristics */
460 for (i
= 0; i
< (instr_4op
? 4 : 2); i
++) {
462 snd_printk(KERN_DEBUG
" --> programming operator %i\n", i
);
464 op_offset
= snd_opl3_regmap
[voice_offset
][i
];
466 /* Set OPL3 AM_VIB register of requested voice/operator */
467 reg_val
= fm
->op
[i
].am_vib
;
468 opl3_reg
= reg_side
| (OPL3_REG_AM_VIB
+ op_offset
);
469 opl3
->command(opl3
, opl3_reg
, reg_val
);
471 /* Set OPL3 KSL_LEVEL register of requested voice/operator */
473 opl3_reg
= reg_side
| (OPL3_REG_KSL_LEVEL
+ op_offset
);
474 opl3
->command(opl3
, opl3_reg
, reg_val
);
476 /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
477 reg_val
= fm
->op
[i
].attack_decay
;
478 opl3_reg
= reg_side
| (OPL3_REG_ATTACK_DECAY
+ op_offset
);
479 opl3
->command(opl3
, opl3_reg
, reg_val
);
481 /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
482 reg_val
= fm
->op
[i
].sustain_release
;
483 opl3_reg
= reg_side
| (OPL3_REG_SUSTAIN_RELEASE
+ op_offset
);
484 opl3
->command(opl3
, opl3_reg
, reg_val
);
486 /* Select waveform */
487 reg_val
= fm
->op
[i
].wave_select
;
488 opl3_reg
= reg_side
| (OPL3_REG_WAVE_SELECT
+ op_offset
);
489 opl3
->command(opl3
, opl3_reg
, reg_val
);
492 /* Set operator feedback and 2op inter-operator connection */
493 reg_val
= fm
->feedback_connection
[0];
494 /* Set output voice connection */
495 reg_val
|= OPL3_STEREO_BITS
;
496 if (chan
->gm_pan
< 43)
497 reg_val
&= ~OPL3_VOICE_TO_RIGHT
;
498 if (chan
->gm_pan
> 85)
499 reg_val
&= ~OPL3_VOICE_TO_LEFT
;
500 opl3_reg
= reg_side
| (OPL3_REG_FEEDBACK_CONNECTION
+ voice_offset
);
501 opl3
->command(opl3
, opl3_reg
, reg_val
);
504 /* Set 4op inter-operator connection */
505 reg_val
= fm
->feedback_connection
[1] & OPL3_CONNECTION_BIT
;
506 /* Set output voice connection */
507 reg_val
|= OPL3_STEREO_BITS
;
508 if (chan
->gm_pan
< 43)
509 reg_val
&= ~OPL3_VOICE_TO_RIGHT
;
510 if (chan
->gm_pan
> 85)
511 reg_val
&= ~OPL3_VOICE_TO_LEFT
;
512 opl3_reg
= reg_side
| (OPL3_REG_FEEDBACK_CONNECTION
+
514 opl3
->command(opl3
, opl3_reg
, reg_val
);
518 * Special treatment of percussion notes for fm:
519 * Requested pitch is really program, and pitch for
520 * device is whatever was specified in the patch library.
525 * use transpose if defined in patch library
528 note
+= (fm
->trnsps
- 64);
530 snd_opl3_calc_pitch(&fnum
, &blocknum
, note
, chan
);
532 /* Set OPL3 FNUM_LOW register of requested voice */
533 opl3_reg
= reg_side
| (OPL3_REG_FNUM_LOW
+ voice_offset
);
534 opl3
->command(opl3
, opl3_reg
, fnum
);
536 opl3
->voices
[voice
].keyon_reg
= blocknum
;
538 /* Set output sound flag */
539 blocknum
|= OPL3_KEYON_BIT
;
542 snd_printk(KERN_DEBUG
" --> trigger voice %i\n", voice
);
544 /* Set OPL3 KEYON_BLOCK register of requested voice */
545 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
546 opl3
->command(opl3
, opl3_reg
, blocknum
);
548 /* kill note after fixed duration (in centiseconds) */
550 opl3
->voices
[voice
].note_off
= jiffies
+
551 (fm
->fix_dur
* HZ
) / 100;
552 snd_opl3_start_timer(opl3
);
553 opl3
->voices
[voice
].note_off_check
= 1;
555 opl3
->voices
[voice
].note_off_check
= 0;
557 /* get extra pgm, but avoid possible loops */
558 extra_prg
= (extra_prg
) ? 0 : fm
->modes
;
560 /* do the bookkeeping */
561 vp
->time
= opl3
->use_time
++;
566 vp
->state
= SNDRV_OPL3_ST_ON_4OP
;
568 vp2
= &opl3
->voices
[voice
+ 3];
569 vp2
->time
= opl3
->use_time
++;
572 vp2
->state
= SNDRV_OPL3_ST_NOT_AVAIL
;
574 if (vp
->state
== SNDRV_OPL3_ST_ON_4OP
) {
575 /* 4op killed by 2op, release bounded voice */
576 vp2
= &opl3
->voices
[voice
+ 3];
577 vp2
->time
= opl3
->use_time
++;
578 vp2
->state
= SNDRV_OPL3_ST_OFF
;
580 vp
->state
= SNDRV_OPL3_ST_ON_2OP
;
584 debug_alloc(opl3
, "note on ", voice
);
587 /* allocate extra program if specified in patch library */
589 if (extra_prg
> 128) {
591 /* percussions start at 35 */
592 prg
= extra_prg
- 128 + 35 - 1;
598 snd_printk(KERN_DEBUG
" *** allocating extra program\n");
602 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
605 static void snd_opl3_kill_voice(struct snd_opl3
*opl3
, int voice
)
607 unsigned short reg_side
;
608 unsigned char voice_offset
;
609 unsigned short opl3_reg
;
611 struct snd_opl3_voice
*vp
, *vp2
;
613 if (snd_BUG_ON(voice
>= MAX_OPL3_VOICES
))
616 vp
= &opl3
->voices
[voice
];
617 if (voice
< MAX_OPL2_VOICES
) {
618 /* Left register block for voices 0 .. 8 */
619 reg_side
= OPL3_LEFT
;
620 voice_offset
= voice
;
622 /* Right register block for voices 9 .. 17 */
623 reg_side
= OPL3_RIGHT
;
624 voice_offset
= voice
- MAX_OPL2_VOICES
;
629 snd_printk(KERN_DEBUG
" --> kill voice %i\n", voice
);
631 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
632 /* clear Key ON bit */
633 opl3
->command(opl3
, opl3_reg
, vp
->keyon_reg
);
635 /* do the bookkeeping */
636 vp
->time
= opl3
->use_time
++;
638 if (vp
->state
== SNDRV_OPL3_ST_ON_4OP
) {
639 vp2
= &opl3
->voices
[voice
+ 3];
641 vp2
->time
= opl3
->use_time
++;
642 vp2
->state
= SNDRV_OPL3_ST_OFF
;
644 vp
->state
= SNDRV_OPL3_ST_OFF
;
646 debug_alloc(opl3
, "note off", voice
);
652 * Release a note in response to a midi note off.
654 static void snd_opl3_note_off_unsafe(void *p
, int note
, int vel
,
655 struct snd_midi_channel
*chan
)
657 struct snd_opl3
*opl3
;
660 struct snd_opl3_voice
*vp
;
665 snd_printk(KERN_DEBUG
"Note off, ch %i, inst %i, note %i\n",
666 chan
->number
, chan
->midi_program
, note
);
669 if (opl3
->synth_mode
== SNDRV_OPL3_MODE_SEQ
) {
670 if (chan
->drum_channel
&& use_internal_drums
) {
671 snd_opl3_drum_switch(opl3
, note
, vel
, 0, chan
);
674 /* this loop will hopefully kill all extra voices, because
675 they are grouped by the same channel and note values */
676 for (voice
= 0; voice
< opl3
->max_voices
; voice
++) {
677 vp
= &opl3
->voices
[voice
];
678 if (vp
->state
> 0 && vp
->chan
== chan
&& vp
->note
== note
) {
679 snd_opl3_kill_voice(opl3
, voice
);
683 /* remap OSS voices */
684 if (chan
->number
< MAX_OPL3_VOICES
) {
685 voice
= snd_opl3_oss_map
[chan
->number
];
686 snd_opl3_kill_voice(opl3
, voice
);
691 void snd_opl3_note_off(void *p
, int note
, int vel
,
692 struct snd_midi_channel
*chan
)
694 struct snd_opl3
*opl3
= p
;
697 spin_lock_irqsave(&opl3
->voice_lock
, flags
);
698 snd_opl3_note_off_unsafe(p
, note
, vel
, chan
);
699 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
703 * key pressure change
705 void snd_opl3_key_press(void *p
, int note
, int vel
, struct snd_midi_channel
*chan
)
708 snd_printk(KERN_DEBUG
"Key pressure, ch#: %i, inst#: %i\n",
709 chan
->number
, chan
->midi_program
);
716 void snd_opl3_terminate_note(void *p
, int note
, struct snd_midi_channel
*chan
)
719 snd_printk(KERN_DEBUG
"Terminate note, ch#: %i, inst#: %i\n",
720 chan
->number
, chan
->midi_program
);
724 static void snd_opl3_update_pitch(struct snd_opl3
*opl3
, int voice
)
726 unsigned short reg_side
;
727 unsigned char voice_offset
;
728 unsigned short opl3_reg
;
730 unsigned char fnum
, blocknum
;
732 struct snd_opl3_voice
*vp
;
734 if (snd_BUG_ON(voice
>= MAX_OPL3_VOICES
))
737 vp
= &opl3
->voices
[voice
];
738 if (vp
->chan
== NULL
)
739 return; /* not allocated? */
741 if (voice
< MAX_OPL2_VOICES
) {
742 /* Left register block for voices 0 .. 8 */
743 reg_side
= OPL3_LEFT
;
744 voice_offset
= voice
;
746 /* Right register block for voices 9 .. 17 */
747 reg_side
= OPL3_RIGHT
;
748 voice_offset
= voice
- MAX_OPL2_VOICES
;
751 snd_opl3_calc_pitch(&fnum
, &blocknum
, vp
->note
, vp
->chan
);
753 /* Set OPL3 FNUM_LOW register of requested voice */
754 opl3_reg
= reg_side
| (OPL3_REG_FNUM_LOW
+ voice_offset
);
755 opl3
->command(opl3
, opl3_reg
, fnum
);
757 vp
->keyon_reg
= blocknum
;
759 /* Set output sound flag */
760 blocknum
|= OPL3_KEYON_BIT
;
762 /* Set OPL3 KEYON_BLOCK register of requested voice */
763 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
764 opl3
->command(opl3
, opl3_reg
, blocknum
);
766 vp
->time
= opl3
->use_time
++;
770 * Update voice pitch controller
772 static void snd_opl3_pitch_ctrl(struct snd_opl3
*opl3
, struct snd_midi_channel
*chan
)
775 struct snd_opl3_voice
*vp
;
779 spin_lock_irqsave(&opl3
->voice_lock
, flags
);
781 if (opl3
->synth_mode
== SNDRV_OPL3_MODE_SEQ
) {
782 for (voice
= 0; voice
< opl3
->max_voices
; voice
++) {
783 vp
= &opl3
->voices
[voice
];
784 if (vp
->state
> 0 && vp
->chan
== chan
) {
785 snd_opl3_update_pitch(opl3
, voice
);
789 /* remap OSS voices */
790 if (chan
->number
< MAX_OPL3_VOICES
) {
791 voice
= snd_opl3_oss_map
[chan
->number
];
792 snd_opl3_update_pitch(opl3
, voice
);
795 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
799 * Deal with a controller type event. This includes all types of
800 * control events, not just the midi controllers
802 void snd_opl3_control(void *p
, int type
, struct snd_midi_channel
*chan
)
804 struct snd_opl3
*opl3
;
808 snd_printk(KERN_DEBUG
"Controller, TYPE = %i, ch#: %i, inst#: %i\n",
809 type
, chan
->number
, chan
->midi_program
);
813 case MIDI_CTL_MSB_MODWHEEL
:
814 if (chan
->control
[MIDI_CTL_MSB_MODWHEEL
] > 63)
815 opl3
->drum_reg
|= OPL3_VIBRATO_DEPTH
;
817 opl3
->drum_reg
&= ~OPL3_VIBRATO_DEPTH
;
818 opl3
->command(opl3
, OPL3_LEFT
| OPL3_REG_PERCUSSION
,
821 case MIDI_CTL_E2_TREMOLO_DEPTH
:
822 if (chan
->control
[MIDI_CTL_E2_TREMOLO_DEPTH
] > 63)
823 opl3
->drum_reg
|= OPL3_TREMOLO_DEPTH
;
825 opl3
->drum_reg
&= ~OPL3_TREMOLO_DEPTH
;
826 opl3
->command(opl3
, OPL3_LEFT
| OPL3_REG_PERCUSSION
,
829 case MIDI_CTL_PITCHBEND
:
830 snd_opl3_pitch_ctrl(opl3
, chan
);
838 void snd_opl3_nrpn(void *p
, struct snd_midi_channel
*chan
,
839 struct snd_midi_channel_set
*chset
)
842 snd_printk(KERN_DEBUG
"NRPN, ch#: %i, inst#: %i\n",
843 chan
->number
, chan
->midi_program
);
850 void snd_opl3_sysex(void *p
, unsigned char *buf
, int len
,
851 int parsed
, struct snd_midi_channel_set
*chset
)
854 snd_printk(KERN_DEBUG
"SYSEX\n");