2 * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
4 * Midi synth routines for OPL2/OPL3/OPL4 FM
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "opl3_voice.h"
26 #include <sound/asoundef.h>
28 extern char snd_opl3_regmap
[MAX_OPL2_VOICES
][4];
30 extern bool use_internal_drums
;
32 static void snd_opl3_note_off_unsafe(void *p
, int note
, int vel
,
33 struct snd_midi_channel
*chan
);
35 * The next table looks magical, but it certainly is not. Its values have
36 * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
37 * for i=0. This log-table converts a linear volume-scaling (0..127) to a
38 * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
39 * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
40 * volume -8 it was implemented as a table because it is only 128 bytes and
41 * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>)
44 static char opl3_volume_table
[128] =
46 -63, -48, -40, -35, -32, -29, -27, -26,
47 -24, -23, -21, -20, -19, -18, -18, -17,
48 -16, -15, -15, -14, -13, -13, -12, -12,
49 -11, -11, -10, -10, -10, -9, -9, -8,
50 -8, -8, -7, -7, -7, -6, -6, -6,
51 -5, -5, -5, -5, -4, -4, -4, -4,
52 -3, -3, -3, -3, -2, -2, -2, -2,
53 -2, -1, -1, -1, -1, 0, 0, 0,
54 0, 0, 0, 1, 1, 1, 1, 1,
55 1, 2, 2, 2, 2, 2, 2, 2,
56 3, 3, 3, 3, 3, 3, 3, 4,
57 4, 4, 4, 4, 4, 4, 4, 5,
58 5, 5, 5, 5, 5, 5, 5, 5,
59 6, 6, 6, 6, 6, 6, 6, 6,
60 6, 7, 7, 7, 7, 7, 7, 7,
61 7, 7, 7, 8, 8, 8, 8, 8
64 void snd_opl3_calc_volume(unsigned char *volbyte
, int vel
,
65 struct snd_midi_channel
*chan
)
67 int oldvol
, newvol
, n
;
70 volume
= (vel
* chan
->gm_volume
* chan
->gm_expression
) / (127*127);
74 oldvol
= OPL3_TOTAL_LEVEL_MASK
- (*volbyte
& OPL3_TOTAL_LEVEL_MASK
);
76 newvol
= opl3_volume_table
[volume
] + oldvol
;
77 if (newvol
> OPL3_TOTAL_LEVEL_MASK
)
78 newvol
= OPL3_TOTAL_LEVEL_MASK
;
82 n
= OPL3_TOTAL_LEVEL_MASK
- (newvol
& OPL3_TOTAL_LEVEL_MASK
);
84 *volbyte
= (*volbyte
& OPL3_KSL_MASK
) | (n
& OPL3_TOTAL_LEVEL_MASK
);
88 * Converts the note frequency to block and fnum values for the FM chip
90 static short opl3_note_table
[16] =
92 305, 323, /* for pitch bending, -2 semitones */
93 343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647,
94 686, 726 /* for pitch bending, +2 semitones */
97 static void snd_opl3_calc_pitch(unsigned char *fnum
, unsigned char *blocknum
,
98 int note
, struct snd_midi_channel
*chan
)
100 int block
= ((note
/ 12) & 0x07) - 1;
101 int idx
= (note
% 12) + 2;
104 if (chan
->midi_pitchbend
) {
105 int pitchbend
= chan
->midi_pitchbend
;
108 if (pitchbend
< -0x2000)
110 if (pitchbend
> 0x1FFF)
113 segment
= pitchbend
/ 0x1000;
114 freq
= opl3_note_table
[idx
+segment
];
115 freq
+= ((opl3_note_table
[idx
+segment
+1] - freq
) *
116 (pitchbend
% 0x1000)) / 0x1000;
118 freq
= opl3_note_table
[idx
];
121 *fnum
= (unsigned char) freq
;
122 *blocknum
= ((freq
>> 8) & OPL3_FNUM_HIGH_MASK
) |
123 ((block
<< 2) & OPL3_BLOCKNUM_MASK
);
128 static void debug_alloc(struct snd_opl3
*opl3
, char *s
, int voice
) {
132 printk(KERN_DEBUG
"time %.5i: %s [%.2i]: ", opl3
->use_time
, s
, voice
);
133 for (i
= 0; i
< opl3
->max_voices
; i
++)
134 printk("%c", *(str
+ opl3
->voices
[i
].state
+ 1));
140 * Get a FM voice (channel) to play a note on.
142 static int opl3_get_voice(struct snd_opl3
*opl3
, int instr_4op
,
143 struct snd_midi_channel
*chan
) {
144 int chan_4op_1
; /* first voice for 4op instrument */
145 int chan_4op_2
; /* second voice for 4op instrument */
147 struct snd_opl3_voice
*vp
, *vp2
;
148 unsigned int voice_time
;
152 char *alloc_type
[3] = { "FREE ", "CHEAP ", "EXPENSIVE" };
155 /* This is our "allocation cost" table */
157 FREE
= 0, CHEAP
, EXPENSIVE
, END
160 /* Keeps track of what we are finding */
167 for (i
= 0; i
< END
; i
++) {
168 best
[i
].time
= (unsigned int)(-1); /* XXX MAX_?INT really */
172 /* Look through all the channels for the most suitable. */
173 for (i
= 0; i
< opl3
->max_voices
; i
++) {
174 vp
= &opl3
->voices
[i
];
176 if (vp
->state
== SNDRV_OPL3_ST_NOT_AVAIL
)
177 /* skip unavailable channels, allocated by
178 drum voices or by bounded 4op voices) */
181 voice_time
= vp
->time
;
184 chan_4op_1
= ((i
< 3) || (i
> 8 && i
< 12));
185 chan_4op_2
= ((i
> 2 && i
< 6) || (i
> 11 && i
< 15));
187 /* allocate 4op voice */
188 /* skip channels unavailable to 4op instrument */
193 /* kill one voice, CHEAP */
195 /* get state of bounded 2op channel
196 to be allocated for 4op instrument */
197 vp2
= &opl3
->voices
[i
+ 3];
198 if (vp2
->state
== SNDRV_OPL3_ST_ON_2OP
) {
199 /* kill two voices, EXPENSIVE */
201 voice_time
= (voice_time
> vp
->time
) ?
202 voice_time
: vp
->time
;
205 /* allocate 2op voice */
206 if ((chan_4op_1
) || (chan_4op_2
))
207 /* use bounded channels for 2op, CHEAP */
210 /* kill one voice on 2op channel, CHEAP */
212 /* raise kill cost to EXPENSIVE for all channels */
216 if (voice_time
< bp
->time
) {
217 bp
->time
= voice_time
;
222 for (i
= 0; i
< END
; i
++) {
223 if (best
[i
].voice
>= 0) {
225 printk(KERN_DEBUG
"%s %iop allocation on voice %i\n",
226 alloc_type
[i
], instr_4op
? 4 : 2,
229 return best
[i
].voice
;
236 /* ------------------------------ */
239 * System timer interrupt function
241 void snd_opl3_timer_func(unsigned long data
)
244 struct snd_opl3
*opl3
= (struct snd_opl3
*)data
;
249 spin_lock_irqsave(&opl3
->voice_lock
, flags
);
250 for (i
= 0; i
< opl3
->max_voices
; i
++) {
251 struct snd_opl3_voice
*vp
= &opl3
->voices
[i
];
252 if (vp
->state
> 0 && vp
->note_off_check
) {
253 if (vp
->note_off
== jiffies
)
254 snd_opl3_note_off_unsafe(opl3
, vp
->note
, 0,
260 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
262 spin_lock_irqsave(&opl3
->sys_timer_lock
, flags
);
264 mod_timer(&opl3
->tlist
, jiffies
+ 1); /* invoke again */
266 opl3
->sys_timer_status
= 0;
267 spin_unlock_irqrestore(&opl3
->sys_timer_lock
, flags
);
273 static void snd_opl3_start_timer(struct snd_opl3
*opl3
)
276 spin_lock_irqsave(&opl3
->sys_timer_lock
, flags
);
277 if (! opl3
->sys_timer_status
) {
278 mod_timer(&opl3
->tlist
, jiffies
+ 1);
279 opl3
->sys_timer_status
= 1;
281 spin_unlock_irqrestore(&opl3
->sys_timer_lock
, flags
);
284 /* ------------------------------ */
287 static int snd_opl3_oss_map
[MAX_OPL3_VOICES
] = {
288 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14
294 void snd_opl3_note_on(void *p
, int note
, int vel
, struct snd_midi_channel
*chan
)
296 struct snd_opl3
*opl3
;
300 struct snd_opl3_voice
*vp
, *vp2
;
301 unsigned short connect_mask
;
302 unsigned char connection
;
303 unsigned char vol_op
[4];
307 unsigned short reg_side
;
308 unsigned char op_offset
;
309 unsigned char voice_offset
;
310 unsigned short opl3_reg
;
311 unsigned char reg_val
;
312 unsigned char prg
, bank
;
315 unsigned char fnum
, blocknum
;
318 struct fm_patch
*patch
;
319 struct fm_instrument
*fm
;
325 snd_printk(KERN_DEBUG
"Note on, ch %i, inst %i, note %i, vel %i\n",
326 chan
->number
, chan
->midi_program
, note
, vel
);
329 /* in SYNTH mode, application takes care of voices */
330 /* in SEQ mode, drum voice numbers are notes on drum channel */
331 if (opl3
->synth_mode
== SNDRV_OPL3_MODE_SEQ
) {
332 if (chan
->drum_channel
) {
333 /* percussion instruments are located in bank 128 */
337 bank
= chan
->gm_bank_select
;
338 prg
= chan
->midi_program
;
341 /* Prepare for OSS mode */
342 if (chan
->number
>= MAX_OPL3_VOICES
)
345 /* OSS instruments are located in bank 127 */
347 prg
= chan
->midi_program
;
350 spin_lock_irqsave(&opl3
->voice_lock
, flags
);
352 if (use_internal_drums
) {
353 snd_opl3_drum_switch(opl3
, note
, vel
, 1, chan
);
354 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
359 patch
= snd_opl3_find_patch(opl3
, prg
, bank
, 0);
361 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
366 switch (patch
->type
) {
371 if (opl3
->hardware
>= OPL3_HW_OPL3
) {
376 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
380 snd_printk(KERN_DEBUG
" --> OPL%i instrument: %s\n",
381 instr_4op
? 3 : 2, patch
->name
);
383 /* in SYNTH mode, application takes care of voices */
384 /* in SEQ mode, allocate voice on free OPL3 channel */
385 if (opl3
->synth_mode
== SNDRV_OPL3_MODE_SEQ
) {
386 voice
= opl3_get_voice(opl3
, instr_4op
, chan
);
388 /* remap OSS voice */
389 voice
= snd_opl3_oss_map
[chan
->number
];
393 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
397 if (voice
< MAX_OPL2_VOICES
) {
398 /* Left register block for voices 0 .. 8 */
399 reg_side
= OPL3_LEFT
;
400 voice_offset
= voice
;
401 connect_mask
= (OPL3_LEFT_4OP_0
<< voice_offset
) & 0x07;
403 /* Right register block for voices 9 .. 17 */
404 reg_side
= OPL3_RIGHT
;
405 voice_offset
= voice
- MAX_OPL2_VOICES
;
406 connect_mask
= (OPL3_RIGHT_4OP_0
<< voice_offset
) & 0x38;
409 /* kill voice on channel */
410 vp
= &opl3
->voices
[voice
];
412 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
413 reg_val
= vp
->keyon_reg
& ~OPL3_KEYON_BIT
;
414 opl3
->command(opl3
, opl3_reg
, reg_val
);
417 vp2
= &opl3
->voices
[voice
+ 3];
419 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+
421 reg_val
= vp
->keyon_reg
& ~OPL3_KEYON_BIT
;
422 opl3
->command(opl3
, opl3_reg
, reg_val
);
426 /* set connection register */
428 if ((opl3
->connection_reg
^ connect_mask
) & connect_mask
) {
429 opl3
->connection_reg
|= connect_mask
;
430 /* set connection bit */
431 opl3_reg
= OPL3_RIGHT
| OPL3_REG_CONNECTION_SELECT
;
432 opl3
->command(opl3
, opl3_reg
, opl3
->connection_reg
);
435 if ((opl3
->connection_reg
^ ~connect_mask
) & connect_mask
) {
436 opl3
->connection_reg
&= ~connect_mask
;
437 /* clear connection bit */
438 opl3_reg
= OPL3_RIGHT
| OPL3_REG_CONNECTION_SELECT
;
439 opl3
->command(opl3
, opl3_reg
, opl3
->connection_reg
);
444 snd_printk(KERN_DEBUG
" --> setting OPL3 connection: 0x%x\n",
445 opl3
->connection_reg
);
448 * calculate volume depending on connection
449 * between FM operators (see include/opl3.h)
451 for (i
= 0; i
< (instr_4op
? 4 : 2); i
++)
452 vol_op
[i
] = fm
->op
[i
].ksl_level
;
454 connection
= fm
->feedback_connection
[0] & 0x01;
457 connection
|= fm
->feedback_connection
[1] & 0x01;
459 snd_opl3_calc_volume(&vol_op
[3], vel
, chan
);
460 switch (connection
) {
462 snd_opl3_calc_volume(&vol_op
[2], vel
, chan
);
465 snd_opl3_calc_volume(&vol_op
[0], vel
, chan
);
468 snd_opl3_calc_volume(&vol_op
[1], vel
, chan
);
471 snd_opl3_calc_volume(&vol_op
[1], vel
, chan
);
473 snd_opl3_calc_volume(&vol_op
[0], vel
, chan
);
476 /* Program the FM voice characteristics */
477 for (i
= 0; i
< (instr_4op
? 4 : 2); i
++) {
479 snd_printk(KERN_DEBUG
" --> programming operator %i\n", i
);
481 op_offset
= snd_opl3_regmap
[voice_offset
][i
];
483 /* Set OPL3 AM_VIB register of requested voice/operator */
484 reg_val
= fm
->op
[i
].am_vib
;
485 opl3_reg
= reg_side
| (OPL3_REG_AM_VIB
+ op_offset
);
486 opl3
->command(opl3
, opl3_reg
, reg_val
);
488 /* Set OPL3 KSL_LEVEL register of requested voice/operator */
490 opl3_reg
= reg_side
| (OPL3_REG_KSL_LEVEL
+ op_offset
);
491 opl3
->command(opl3
, opl3_reg
, reg_val
);
493 /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
494 reg_val
= fm
->op
[i
].attack_decay
;
495 opl3_reg
= reg_side
| (OPL3_REG_ATTACK_DECAY
+ op_offset
);
496 opl3
->command(opl3
, opl3_reg
, reg_val
);
498 /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
499 reg_val
= fm
->op
[i
].sustain_release
;
500 opl3_reg
= reg_side
| (OPL3_REG_SUSTAIN_RELEASE
+ op_offset
);
501 opl3
->command(opl3
, opl3_reg
, reg_val
);
503 /* Select waveform */
504 reg_val
= fm
->op
[i
].wave_select
;
505 opl3_reg
= reg_side
| (OPL3_REG_WAVE_SELECT
+ op_offset
);
506 opl3
->command(opl3
, opl3_reg
, reg_val
);
509 /* Set operator feedback and 2op inter-operator connection */
510 reg_val
= fm
->feedback_connection
[0];
511 /* Set output voice connection */
512 reg_val
|= OPL3_STEREO_BITS
;
513 if (chan
->gm_pan
< 43)
514 reg_val
&= ~OPL3_VOICE_TO_RIGHT
;
515 if (chan
->gm_pan
> 85)
516 reg_val
&= ~OPL3_VOICE_TO_LEFT
;
517 opl3_reg
= reg_side
| (OPL3_REG_FEEDBACK_CONNECTION
+ voice_offset
);
518 opl3
->command(opl3
, opl3_reg
, reg_val
);
521 /* Set 4op inter-operator connection */
522 reg_val
= fm
->feedback_connection
[1] & OPL3_CONNECTION_BIT
;
523 /* Set output voice connection */
524 reg_val
|= OPL3_STEREO_BITS
;
525 if (chan
->gm_pan
< 43)
526 reg_val
&= ~OPL3_VOICE_TO_RIGHT
;
527 if (chan
->gm_pan
> 85)
528 reg_val
&= ~OPL3_VOICE_TO_LEFT
;
529 opl3_reg
= reg_side
| (OPL3_REG_FEEDBACK_CONNECTION
+
531 opl3
->command(opl3
, opl3_reg
, reg_val
);
535 * Special treatment of percussion notes for fm:
536 * Requested pitch is really program, and pitch for
537 * device is whatever was specified in the patch library.
542 * use transpose if defined in patch library
545 note
+= (fm
->trnsps
- 64);
547 snd_opl3_calc_pitch(&fnum
, &blocknum
, note
, chan
);
549 /* Set OPL3 FNUM_LOW register of requested voice */
550 opl3_reg
= reg_side
| (OPL3_REG_FNUM_LOW
+ voice_offset
);
551 opl3
->command(opl3
, opl3_reg
, fnum
);
553 opl3
->voices
[voice
].keyon_reg
= blocknum
;
555 /* Set output sound flag */
556 blocknum
|= OPL3_KEYON_BIT
;
559 snd_printk(KERN_DEBUG
" --> trigger voice %i\n", voice
);
561 /* Set OPL3 KEYON_BLOCK register of requested voice */
562 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
563 opl3
->command(opl3
, opl3_reg
, blocknum
);
565 /* kill note after fixed duration (in centiseconds) */
567 opl3
->voices
[voice
].note_off
= jiffies
+
568 (fm
->fix_dur
* HZ
) / 100;
569 snd_opl3_start_timer(opl3
);
570 opl3
->voices
[voice
].note_off_check
= 1;
572 opl3
->voices
[voice
].note_off_check
= 0;
574 /* get extra pgm, but avoid possible loops */
575 extra_prg
= (extra_prg
) ? 0 : fm
->modes
;
577 /* do the bookkeeping */
578 vp
->time
= opl3
->use_time
++;
583 vp
->state
= SNDRV_OPL3_ST_ON_4OP
;
585 vp2
= &opl3
->voices
[voice
+ 3];
586 vp2
->time
= opl3
->use_time
++;
589 vp2
->state
= SNDRV_OPL3_ST_NOT_AVAIL
;
591 if (vp
->state
== SNDRV_OPL3_ST_ON_4OP
) {
592 /* 4op killed by 2op, release bounded voice */
593 vp2
= &opl3
->voices
[voice
+ 3];
594 vp2
->time
= opl3
->use_time
++;
595 vp2
->state
= SNDRV_OPL3_ST_OFF
;
597 vp
->state
= SNDRV_OPL3_ST_ON_2OP
;
601 debug_alloc(opl3
, "note on ", voice
);
604 /* allocate extra program if specified in patch library */
606 if (extra_prg
> 128) {
608 /* percussions start at 35 */
609 prg
= extra_prg
- 128 + 35 - 1;
615 snd_printk(KERN_DEBUG
" *** allocating extra program\n");
619 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
622 static void snd_opl3_kill_voice(struct snd_opl3
*opl3
, int voice
)
624 unsigned short reg_side
;
625 unsigned char voice_offset
;
626 unsigned short opl3_reg
;
628 struct snd_opl3_voice
*vp
, *vp2
;
630 if (snd_BUG_ON(voice
>= MAX_OPL3_VOICES
))
633 vp
= &opl3
->voices
[voice
];
634 if (voice
< MAX_OPL2_VOICES
) {
635 /* Left register block for voices 0 .. 8 */
636 reg_side
= OPL3_LEFT
;
637 voice_offset
= voice
;
639 /* Right register block for voices 9 .. 17 */
640 reg_side
= OPL3_RIGHT
;
641 voice_offset
= voice
- MAX_OPL2_VOICES
;
646 snd_printk(KERN_DEBUG
" --> kill voice %i\n", voice
);
648 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
649 /* clear Key ON bit */
650 opl3
->command(opl3
, opl3_reg
, vp
->keyon_reg
);
652 /* do the bookkeeping */
653 vp
->time
= opl3
->use_time
++;
655 if (vp
->state
== SNDRV_OPL3_ST_ON_4OP
) {
656 vp2
= &opl3
->voices
[voice
+ 3];
658 vp2
->time
= opl3
->use_time
++;
659 vp2
->state
= SNDRV_OPL3_ST_OFF
;
661 vp
->state
= SNDRV_OPL3_ST_OFF
;
663 debug_alloc(opl3
, "note off", voice
);
669 * Release a note in response to a midi note off.
671 static void snd_opl3_note_off_unsafe(void *p
, int note
, int vel
,
672 struct snd_midi_channel
*chan
)
674 struct snd_opl3
*opl3
;
677 struct snd_opl3_voice
*vp
;
682 snd_printk(KERN_DEBUG
"Note off, ch %i, inst %i, note %i\n",
683 chan
->number
, chan
->midi_program
, note
);
686 if (opl3
->synth_mode
== SNDRV_OPL3_MODE_SEQ
) {
687 if (chan
->drum_channel
&& use_internal_drums
) {
688 snd_opl3_drum_switch(opl3
, note
, vel
, 0, chan
);
691 /* this loop will hopefully kill all extra voices, because
692 they are grouped by the same channel and note values */
693 for (voice
= 0; voice
< opl3
->max_voices
; voice
++) {
694 vp
= &opl3
->voices
[voice
];
695 if (vp
->state
> 0 && vp
->chan
== chan
&& vp
->note
== note
) {
696 snd_opl3_kill_voice(opl3
, voice
);
700 /* remap OSS voices */
701 if (chan
->number
< MAX_OPL3_VOICES
) {
702 voice
= snd_opl3_oss_map
[chan
->number
];
703 snd_opl3_kill_voice(opl3
, voice
);
708 void snd_opl3_note_off(void *p
, int note
, int vel
,
709 struct snd_midi_channel
*chan
)
711 struct snd_opl3
*opl3
= p
;
714 spin_lock_irqsave(&opl3
->voice_lock
, flags
);
715 snd_opl3_note_off_unsafe(p
, note
, vel
, chan
);
716 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
720 * key pressure change
722 void snd_opl3_key_press(void *p
, int note
, int vel
, struct snd_midi_channel
*chan
)
724 struct snd_opl3
*opl3
;
728 snd_printk(KERN_DEBUG
"Key pressure, ch#: %i, inst#: %i\n",
729 chan
->number
, chan
->midi_program
);
736 void snd_opl3_terminate_note(void *p
, int note
, struct snd_midi_channel
*chan
)
738 struct snd_opl3
*opl3
;
742 snd_printk(KERN_DEBUG
"Terminate note, ch#: %i, inst#: %i\n",
743 chan
->number
, chan
->midi_program
);
747 static void snd_opl3_update_pitch(struct snd_opl3
*opl3
, int voice
)
749 unsigned short reg_side
;
750 unsigned char voice_offset
;
751 unsigned short opl3_reg
;
753 unsigned char fnum
, blocknum
;
755 struct snd_opl3_voice
*vp
;
757 if (snd_BUG_ON(voice
>= MAX_OPL3_VOICES
))
760 vp
= &opl3
->voices
[voice
];
761 if (vp
->chan
== NULL
)
762 return; /* not allocated? */
764 if (voice
< MAX_OPL2_VOICES
) {
765 /* Left register block for voices 0 .. 8 */
766 reg_side
= OPL3_LEFT
;
767 voice_offset
= voice
;
769 /* Right register block for voices 9 .. 17 */
770 reg_side
= OPL3_RIGHT
;
771 voice_offset
= voice
- MAX_OPL2_VOICES
;
774 snd_opl3_calc_pitch(&fnum
, &blocknum
, vp
->note
, vp
->chan
);
776 /* Set OPL3 FNUM_LOW register of requested voice */
777 opl3_reg
= reg_side
| (OPL3_REG_FNUM_LOW
+ voice_offset
);
778 opl3
->command(opl3
, opl3_reg
, fnum
);
780 vp
->keyon_reg
= blocknum
;
782 /* Set output sound flag */
783 blocknum
|= OPL3_KEYON_BIT
;
785 /* Set OPL3 KEYON_BLOCK register of requested voice */
786 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
787 opl3
->command(opl3
, opl3_reg
, blocknum
);
789 vp
->time
= opl3
->use_time
++;
793 * Update voice pitch controller
795 static void snd_opl3_pitch_ctrl(struct snd_opl3
*opl3
, struct snd_midi_channel
*chan
)
798 struct snd_opl3_voice
*vp
;
802 spin_lock_irqsave(&opl3
->voice_lock
, flags
);
804 if (opl3
->synth_mode
== SNDRV_OPL3_MODE_SEQ
) {
805 for (voice
= 0; voice
< opl3
->max_voices
; voice
++) {
806 vp
= &opl3
->voices
[voice
];
807 if (vp
->state
> 0 && vp
->chan
== chan
) {
808 snd_opl3_update_pitch(opl3
, voice
);
812 /* remap OSS voices */
813 if (chan
->number
< MAX_OPL3_VOICES
) {
814 voice
= snd_opl3_oss_map
[chan
->number
];
815 snd_opl3_update_pitch(opl3
, voice
);
818 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
822 * Deal with a controller type event. This includes all types of
823 * control events, not just the midi controllers
825 void snd_opl3_control(void *p
, int type
, struct snd_midi_channel
*chan
)
827 struct snd_opl3
*opl3
;
831 snd_printk(KERN_DEBUG
"Controller, TYPE = %i, ch#: %i, inst#: %i\n",
832 type
, chan
->number
, chan
->midi_program
);
836 case MIDI_CTL_MSB_MODWHEEL
:
837 if (chan
->control
[MIDI_CTL_MSB_MODWHEEL
] > 63)
838 opl3
->drum_reg
|= OPL3_VIBRATO_DEPTH
;
840 opl3
->drum_reg
&= ~OPL3_VIBRATO_DEPTH
;
841 opl3
->command(opl3
, OPL3_LEFT
| OPL3_REG_PERCUSSION
,
844 case MIDI_CTL_E2_TREMOLO_DEPTH
:
845 if (chan
->control
[MIDI_CTL_E2_TREMOLO_DEPTH
] > 63)
846 opl3
->drum_reg
|= OPL3_TREMOLO_DEPTH
;
848 opl3
->drum_reg
&= ~OPL3_TREMOLO_DEPTH
;
849 opl3
->command(opl3
, OPL3_LEFT
| OPL3_REG_PERCUSSION
,
852 case MIDI_CTL_PITCHBEND
:
853 snd_opl3_pitch_ctrl(opl3
, chan
);
861 void snd_opl3_nrpn(void *p
, struct snd_midi_channel
*chan
,
862 struct snd_midi_channel_set
*chset
)
864 struct snd_opl3
*opl3
;
868 snd_printk(KERN_DEBUG
"NRPN, ch#: %i, inst#: %i\n",
869 chan
->number
, chan
->midi_program
);
876 void snd_opl3_sysex(void *p
, unsigned char *buf
, int len
,
877 int parsed
, struct snd_midi_channel_set
*chset
)
879 struct snd_opl3
*opl3
;
883 snd_printk(KERN_DEBUG
"SYSEX\n");