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 static void snd_opl3_note_off_unsafe(void *p
, int note
, int vel
,
29 struct snd_midi_channel
*chan
);
31 * The next table looks magical, but it certainly is not. Its values have
32 * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
33 * for i=0. This log-table converts a linear volume-scaling (0..127) to a
34 * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
35 * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
36 * volume -8 it was implemented as a table because it is only 128 bytes and
37 * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>)
40 static char opl3_volume_table
[128] =
42 -63, -48, -40, -35, -32, -29, -27, -26,
43 -24, -23, -21, -20, -19, -18, -18, -17,
44 -16, -15, -15, -14, -13, -13, -12, -12,
45 -11, -11, -10, -10, -10, -9, -9, -8,
46 -8, -8, -7, -7, -7, -6, -6, -6,
47 -5, -5, -5, -5, -4, -4, -4, -4,
48 -3, -3, -3, -3, -2, -2, -2, -2,
49 -2, -1, -1, -1, -1, 0, 0, 0,
50 0, 0, 0, 1, 1, 1, 1, 1,
51 1, 2, 2, 2, 2, 2, 2, 2,
52 3, 3, 3, 3, 3, 3, 3, 4,
53 4, 4, 4, 4, 4, 4, 4, 5,
54 5, 5, 5, 5, 5, 5, 5, 5,
55 6, 6, 6, 6, 6, 6, 6, 6,
56 6, 7, 7, 7, 7, 7, 7, 7,
57 7, 7, 7, 8, 8, 8, 8, 8
60 void snd_opl3_calc_volume(unsigned char *volbyte
, int vel
,
61 struct snd_midi_channel
*chan
)
63 int oldvol
, newvol
, n
;
66 volume
= (vel
* chan
->gm_volume
* chan
->gm_expression
) / (127*127);
70 oldvol
= OPL3_TOTAL_LEVEL_MASK
- (*volbyte
& OPL3_TOTAL_LEVEL_MASK
);
72 newvol
= opl3_volume_table
[volume
] + oldvol
;
73 if (newvol
> OPL3_TOTAL_LEVEL_MASK
)
74 newvol
= OPL3_TOTAL_LEVEL_MASK
;
78 n
= OPL3_TOTAL_LEVEL_MASK
- (newvol
& OPL3_TOTAL_LEVEL_MASK
);
80 *volbyte
= (*volbyte
& OPL3_KSL_MASK
) | (n
& OPL3_TOTAL_LEVEL_MASK
);
84 * Converts the note frequency to block and fnum values for the FM chip
86 static short opl3_note_table
[16] =
88 305, 323, /* for pitch bending, -2 semitones */
89 343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647,
90 686, 726 /* for pitch bending, +2 semitones */
93 static void snd_opl3_calc_pitch(unsigned char *fnum
, unsigned char *blocknum
,
94 int note
, struct snd_midi_channel
*chan
)
96 int block
= ((note
/ 12) & 0x07) - 1;
97 int idx
= (note
% 12) + 2;
100 if (chan
->midi_pitchbend
) {
101 int pitchbend
= chan
->midi_pitchbend
;
104 if (pitchbend
< -0x2000)
106 if (pitchbend
> 0x1FFF)
109 segment
= pitchbend
/ 0x1000;
110 freq
= opl3_note_table
[idx
+segment
];
111 freq
+= ((opl3_note_table
[idx
+segment
+1] - freq
) *
112 (pitchbend
% 0x1000)) / 0x1000;
114 freq
= opl3_note_table
[idx
];
117 *fnum
= (unsigned char) freq
;
118 *blocknum
= ((freq
>> 8) & OPL3_FNUM_HIGH_MASK
) |
119 ((block
<< 2) & OPL3_BLOCKNUM_MASK
);
124 static void debug_alloc(struct snd_opl3
*opl3
, char *s
, int voice
) {
128 printk(KERN_DEBUG
"time %.5i: %s [%.2i]: ", opl3
->use_time
, s
, voice
);
129 for (i
= 0; i
< opl3
->max_voices
; i
++)
130 printk(KERN_CONT
"%c", *(str
+ opl3
->voices
[i
].state
+ 1));
131 printk(KERN_CONT
"\n");
136 * Get a FM voice (channel) to play a note on.
138 static int opl3_get_voice(struct snd_opl3
*opl3
, int instr_4op
,
139 struct snd_midi_channel
*chan
) {
140 int chan_4op_1
; /* first voice for 4op instrument */
141 int chan_4op_2
; /* second voice for 4op instrument */
143 struct snd_opl3_voice
*vp
, *vp2
;
144 unsigned int voice_time
;
148 char *alloc_type
[3] = { "FREE ", "CHEAP ", "EXPENSIVE" };
151 /* This is our "allocation cost" table */
153 FREE
= 0, CHEAP
, EXPENSIVE
, END
156 /* Keeps track of what we are finding */
163 for (i
= 0; i
< END
; i
++) {
164 best
[i
].time
= (unsigned int)(-1); /* XXX MAX_?INT really */
168 /* Look through all the channels for the most suitable. */
169 for (i
= 0; i
< opl3
->max_voices
; i
++) {
170 vp
= &opl3
->voices
[i
];
172 if (vp
->state
== SNDRV_OPL3_ST_NOT_AVAIL
)
173 /* skip unavailable channels, allocated by
174 drum voices or by bounded 4op voices) */
177 voice_time
= vp
->time
;
180 chan_4op_1
= ((i
< 3) || (i
> 8 && i
< 12));
181 chan_4op_2
= ((i
> 2 && i
< 6) || (i
> 11 && i
< 15));
183 /* allocate 4op voice */
184 /* skip channels unavailable to 4op instrument */
189 /* kill one voice, CHEAP */
191 /* get state of bounded 2op channel
192 to be allocated for 4op instrument */
193 vp2
= &opl3
->voices
[i
+ 3];
194 if (vp2
->state
== SNDRV_OPL3_ST_ON_2OP
) {
195 /* kill two voices, EXPENSIVE */
197 voice_time
= (voice_time
> vp
->time
) ?
198 voice_time
: vp
->time
;
201 /* allocate 2op voice */
202 if ((chan_4op_1
) || (chan_4op_2
))
203 /* use bounded channels for 2op, CHEAP */
206 /* kill one voice on 2op channel, CHEAP */
208 /* raise kill cost to EXPENSIVE for all channels */
212 if (voice_time
< bp
->time
) {
213 bp
->time
= voice_time
;
218 for (i
= 0; i
< END
; i
++) {
219 if (best
[i
].voice
>= 0) {
221 printk(KERN_DEBUG
"%s %iop allocation on voice %i\n",
222 alloc_type
[i
], instr_4op
? 4 : 2,
225 return best
[i
].voice
;
232 /* ------------------------------ */
235 * System timer interrupt function
237 void snd_opl3_timer_func(struct timer_list
*t
)
240 struct snd_opl3
*opl3
= from_timer(opl3
, t
, tlist
);
245 spin_lock_irqsave(&opl3
->voice_lock
, flags
);
246 for (i
= 0; i
< opl3
->max_voices
; i
++) {
247 struct snd_opl3_voice
*vp
= &opl3
->voices
[i
];
248 if (vp
->state
> 0 && vp
->note_off_check
) {
249 if (vp
->note_off
== jiffies
)
250 snd_opl3_note_off_unsafe(opl3
, vp
->note
, 0,
256 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
258 spin_lock_irqsave(&opl3
->sys_timer_lock
, flags
);
260 mod_timer(&opl3
->tlist
, jiffies
+ 1); /* invoke again */
262 opl3
->sys_timer_status
= 0;
263 spin_unlock_irqrestore(&opl3
->sys_timer_lock
, flags
);
269 static void snd_opl3_start_timer(struct snd_opl3
*opl3
)
272 spin_lock_irqsave(&opl3
->sys_timer_lock
, flags
);
273 if (! opl3
->sys_timer_status
) {
274 mod_timer(&opl3
->tlist
, jiffies
+ 1);
275 opl3
->sys_timer_status
= 1;
277 spin_unlock_irqrestore(&opl3
->sys_timer_lock
, flags
);
280 /* ------------------------------ */
283 static int snd_opl3_oss_map
[MAX_OPL3_VOICES
] = {
284 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14
290 void snd_opl3_note_on(void *p
, int note
, int vel
, struct snd_midi_channel
*chan
)
292 struct snd_opl3
*opl3
;
296 struct snd_opl3_voice
*vp
, *vp2
;
297 unsigned short connect_mask
;
298 unsigned char connection
;
299 unsigned char vol_op
[4];
303 unsigned short reg_side
;
304 unsigned char op_offset
;
305 unsigned char voice_offset
;
306 unsigned short opl3_reg
;
307 unsigned char reg_val
;
308 unsigned char prg
, bank
;
311 unsigned char fnum
, blocknum
;
314 struct fm_patch
*patch
;
315 struct fm_instrument
*fm
;
321 snd_printk(KERN_DEBUG
"Note on, ch %i, inst %i, note %i, vel %i\n",
322 chan
->number
, chan
->midi_program
, note
, vel
);
325 /* in SYNTH mode, application takes care of voices */
326 /* in SEQ mode, drum voice numbers are notes on drum channel */
327 if (opl3
->synth_mode
== SNDRV_OPL3_MODE_SEQ
) {
328 if (chan
->drum_channel
) {
329 /* percussion instruments are located in bank 128 */
333 bank
= chan
->gm_bank_select
;
334 prg
= chan
->midi_program
;
337 /* Prepare for OSS mode */
338 if (chan
->number
>= MAX_OPL3_VOICES
)
341 /* OSS instruments are located in bank 127 */
343 prg
= chan
->midi_program
;
346 spin_lock_irqsave(&opl3
->voice_lock
, flags
);
348 if (use_internal_drums
) {
349 snd_opl3_drum_switch(opl3
, note
, vel
, 1, chan
);
350 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
355 patch
= snd_opl3_find_patch(opl3
, prg
, bank
, 0);
357 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
362 switch (patch
->type
) {
367 if (opl3
->hardware
>= OPL3_HW_OPL3
) {
373 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
377 snd_printk(KERN_DEBUG
" --> OPL%i instrument: %s\n",
378 instr_4op
? 3 : 2, patch
->name
);
380 /* in SYNTH mode, application takes care of voices */
381 /* in SEQ mode, allocate voice on free OPL3 channel */
382 if (opl3
->synth_mode
== SNDRV_OPL3_MODE_SEQ
) {
383 voice
= opl3_get_voice(opl3
, instr_4op
, chan
);
385 /* remap OSS voice */
386 voice
= snd_opl3_oss_map
[chan
->number
];
390 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
394 if (voice
< MAX_OPL2_VOICES
) {
395 /* Left register block for voices 0 .. 8 */
396 reg_side
= OPL3_LEFT
;
397 voice_offset
= voice
;
398 connect_mask
= (OPL3_LEFT_4OP_0
<< voice_offset
) & 0x07;
400 /* Right register block for voices 9 .. 17 */
401 reg_side
= OPL3_RIGHT
;
402 voice_offset
= voice
- MAX_OPL2_VOICES
;
403 connect_mask
= (OPL3_RIGHT_4OP_0
<< voice_offset
) & 0x38;
406 /* kill voice on channel */
407 vp
= &opl3
->voices
[voice
];
409 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
410 reg_val
= vp
->keyon_reg
& ~OPL3_KEYON_BIT
;
411 opl3
->command(opl3
, opl3_reg
, reg_val
);
414 vp2
= &opl3
->voices
[voice
+ 3];
416 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+
418 reg_val
= vp
->keyon_reg
& ~OPL3_KEYON_BIT
;
419 opl3
->command(opl3
, opl3_reg
, reg_val
);
423 /* set connection register */
425 if ((opl3
->connection_reg
^ connect_mask
) & connect_mask
) {
426 opl3
->connection_reg
|= connect_mask
;
427 /* set connection bit */
428 opl3_reg
= OPL3_RIGHT
| OPL3_REG_CONNECTION_SELECT
;
429 opl3
->command(opl3
, opl3_reg
, opl3
->connection_reg
);
432 if ((opl3
->connection_reg
^ ~connect_mask
) & connect_mask
) {
433 opl3
->connection_reg
&= ~connect_mask
;
434 /* clear connection bit */
435 opl3_reg
= OPL3_RIGHT
| OPL3_REG_CONNECTION_SELECT
;
436 opl3
->command(opl3
, opl3_reg
, opl3
->connection_reg
);
441 snd_printk(KERN_DEBUG
" --> setting OPL3 connection: 0x%x\n",
442 opl3
->connection_reg
);
445 * calculate volume depending on connection
446 * between FM operators (see include/opl3.h)
448 for (i
= 0; i
< (instr_4op
? 4 : 2); i
++)
449 vol_op
[i
] = fm
->op
[i
].ksl_level
;
451 connection
= fm
->feedback_connection
[0] & 0x01;
454 connection
|= fm
->feedback_connection
[1] & 0x01;
456 snd_opl3_calc_volume(&vol_op
[3], vel
, chan
);
457 switch (connection
) {
459 snd_opl3_calc_volume(&vol_op
[2], vel
, chan
);
462 snd_opl3_calc_volume(&vol_op
[0], vel
, chan
);
465 snd_opl3_calc_volume(&vol_op
[1], vel
, chan
);
468 snd_opl3_calc_volume(&vol_op
[1], vel
, chan
);
470 snd_opl3_calc_volume(&vol_op
[0], vel
, chan
);
473 /* Program the FM voice characteristics */
474 for (i
= 0; i
< (instr_4op
? 4 : 2); i
++) {
476 snd_printk(KERN_DEBUG
" --> programming operator %i\n", i
);
478 op_offset
= snd_opl3_regmap
[voice_offset
][i
];
480 /* Set OPL3 AM_VIB register of requested voice/operator */
481 reg_val
= fm
->op
[i
].am_vib
;
482 opl3_reg
= reg_side
| (OPL3_REG_AM_VIB
+ op_offset
);
483 opl3
->command(opl3
, opl3_reg
, reg_val
);
485 /* Set OPL3 KSL_LEVEL register of requested voice/operator */
487 opl3_reg
= reg_side
| (OPL3_REG_KSL_LEVEL
+ op_offset
);
488 opl3
->command(opl3
, opl3_reg
, reg_val
);
490 /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
491 reg_val
= fm
->op
[i
].attack_decay
;
492 opl3_reg
= reg_side
| (OPL3_REG_ATTACK_DECAY
+ op_offset
);
493 opl3
->command(opl3
, opl3_reg
, reg_val
);
495 /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
496 reg_val
= fm
->op
[i
].sustain_release
;
497 opl3_reg
= reg_side
| (OPL3_REG_SUSTAIN_RELEASE
+ op_offset
);
498 opl3
->command(opl3
, opl3_reg
, reg_val
);
500 /* Select waveform */
501 reg_val
= fm
->op
[i
].wave_select
;
502 opl3_reg
= reg_side
| (OPL3_REG_WAVE_SELECT
+ op_offset
);
503 opl3
->command(opl3
, opl3_reg
, reg_val
);
506 /* Set operator feedback and 2op inter-operator connection */
507 reg_val
= fm
->feedback_connection
[0];
508 /* Set output voice connection */
509 reg_val
|= OPL3_STEREO_BITS
;
510 if (chan
->gm_pan
< 43)
511 reg_val
&= ~OPL3_VOICE_TO_RIGHT
;
512 if (chan
->gm_pan
> 85)
513 reg_val
&= ~OPL3_VOICE_TO_LEFT
;
514 opl3_reg
= reg_side
| (OPL3_REG_FEEDBACK_CONNECTION
+ voice_offset
);
515 opl3
->command(opl3
, opl3_reg
, reg_val
);
518 /* Set 4op inter-operator connection */
519 reg_val
= fm
->feedback_connection
[1] & OPL3_CONNECTION_BIT
;
520 /* Set output voice connection */
521 reg_val
|= OPL3_STEREO_BITS
;
522 if (chan
->gm_pan
< 43)
523 reg_val
&= ~OPL3_VOICE_TO_RIGHT
;
524 if (chan
->gm_pan
> 85)
525 reg_val
&= ~OPL3_VOICE_TO_LEFT
;
526 opl3_reg
= reg_side
| (OPL3_REG_FEEDBACK_CONNECTION
+
528 opl3
->command(opl3
, opl3_reg
, reg_val
);
532 * Special treatment of percussion notes for fm:
533 * Requested pitch is really program, and pitch for
534 * device is whatever was specified in the patch library.
539 * use transpose if defined in patch library
542 note
+= (fm
->trnsps
- 64);
544 snd_opl3_calc_pitch(&fnum
, &blocknum
, note
, chan
);
546 /* Set OPL3 FNUM_LOW register of requested voice */
547 opl3_reg
= reg_side
| (OPL3_REG_FNUM_LOW
+ voice_offset
);
548 opl3
->command(opl3
, opl3_reg
, fnum
);
550 opl3
->voices
[voice
].keyon_reg
= blocknum
;
552 /* Set output sound flag */
553 blocknum
|= OPL3_KEYON_BIT
;
556 snd_printk(KERN_DEBUG
" --> trigger voice %i\n", voice
);
558 /* Set OPL3 KEYON_BLOCK register of requested voice */
559 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
560 opl3
->command(opl3
, opl3_reg
, blocknum
);
562 /* kill note after fixed duration (in centiseconds) */
564 opl3
->voices
[voice
].note_off
= jiffies
+
565 (fm
->fix_dur
* HZ
) / 100;
566 snd_opl3_start_timer(opl3
);
567 opl3
->voices
[voice
].note_off_check
= 1;
569 opl3
->voices
[voice
].note_off_check
= 0;
571 /* get extra pgm, but avoid possible loops */
572 extra_prg
= (extra_prg
) ? 0 : fm
->modes
;
574 /* do the bookkeeping */
575 vp
->time
= opl3
->use_time
++;
580 vp
->state
= SNDRV_OPL3_ST_ON_4OP
;
582 vp2
= &opl3
->voices
[voice
+ 3];
583 vp2
->time
= opl3
->use_time
++;
586 vp2
->state
= SNDRV_OPL3_ST_NOT_AVAIL
;
588 if (vp
->state
== SNDRV_OPL3_ST_ON_4OP
) {
589 /* 4op killed by 2op, release bounded voice */
590 vp2
= &opl3
->voices
[voice
+ 3];
591 vp2
->time
= opl3
->use_time
++;
592 vp2
->state
= SNDRV_OPL3_ST_OFF
;
594 vp
->state
= SNDRV_OPL3_ST_ON_2OP
;
598 debug_alloc(opl3
, "note on ", voice
);
601 /* allocate extra program if specified in patch library */
603 if (extra_prg
> 128) {
605 /* percussions start at 35 */
606 prg
= extra_prg
- 128 + 35 - 1;
612 snd_printk(KERN_DEBUG
" *** allocating extra program\n");
616 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
619 static void snd_opl3_kill_voice(struct snd_opl3
*opl3
, int voice
)
621 unsigned short reg_side
;
622 unsigned char voice_offset
;
623 unsigned short opl3_reg
;
625 struct snd_opl3_voice
*vp
, *vp2
;
627 if (snd_BUG_ON(voice
>= MAX_OPL3_VOICES
))
630 vp
= &opl3
->voices
[voice
];
631 if (voice
< MAX_OPL2_VOICES
) {
632 /* Left register block for voices 0 .. 8 */
633 reg_side
= OPL3_LEFT
;
634 voice_offset
= voice
;
636 /* Right register block for voices 9 .. 17 */
637 reg_side
= OPL3_RIGHT
;
638 voice_offset
= voice
- MAX_OPL2_VOICES
;
643 snd_printk(KERN_DEBUG
" --> kill voice %i\n", voice
);
645 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
646 /* clear Key ON bit */
647 opl3
->command(opl3
, opl3_reg
, vp
->keyon_reg
);
649 /* do the bookkeeping */
650 vp
->time
= opl3
->use_time
++;
652 if (vp
->state
== SNDRV_OPL3_ST_ON_4OP
) {
653 vp2
= &opl3
->voices
[voice
+ 3];
655 vp2
->time
= opl3
->use_time
++;
656 vp2
->state
= SNDRV_OPL3_ST_OFF
;
658 vp
->state
= SNDRV_OPL3_ST_OFF
;
660 debug_alloc(opl3
, "note off", voice
);
666 * Release a note in response to a midi note off.
668 static void snd_opl3_note_off_unsafe(void *p
, int note
, int vel
,
669 struct snd_midi_channel
*chan
)
671 struct snd_opl3
*opl3
;
674 struct snd_opl3_voice
*vp
;
679 snd_printk(KERN_DEBUG
"Note off, ch %i, inst %i, note %i\n",
680 chan
->number
, chan
->midi_program
, note
);
683 if (opl3
->synth_mode
== SNDRV_OPL3_MODE_SEQ
) {
684 if (chan
->drum_channel
&& use_internal_drums
) {
685 snd_opl3_drum_switch(opl3
, note
, vel
, 0, chan
);
688 /* this loop will hopefully kill all extra voices, because
689 they are grouped by the same channel and note values */
690 for (voice
= 0; voice
< opl3
->max_voices
; voice
++) {
691 vp
= &opl3
->voices
[voice
];
692 if (vp
->state
> 0 && vp
->chan
== chan
&& vp
->note
== note
) {
693 snd_opl3_kill_voice(opl3
, voice
);
697 /* remap OSS voices */
698 if (chan
->number
< MAX_OPL3_VOICES
) {
699 voice
= snd_opl3_oss_map
[chan
->number
];
700 snd_opl3_kill_voice(opl3
, voice
);
705 void snd_opl3_note_off(void *p
, int note
, int vel
,
706 struct snd_midi_channel
*chan
)
708 struct snd_opl3
*opl3
= p
;
711 spin_lock_irqsave(&opl3
->voice_lock
, flags
);
712 snd_opl3_note_off_unsafe(p
, note
, vel
, chan
);
713 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
717 * key pressure change
719 void snd_opl3_key_press(void *p
, int note
, int vel
, struct snd_midi_channel
*chan
)
722 snd_printk(KERN_DEBUG
"Key pressure, ch#: %i, inst#: %i\n",
723 chan
->number
, chan
->midi_program
);
730 void snd_opl3_terminate_note(void *p
, int note
, struct snd_midi_channel
*chan
)
733 snd_printk(KERN_DEBUG
"Terminate note, ch#: %i, inst#: %i\n",
734 chan
->number
, chan
->midi_program
);
738 static void snd_opl3_update_pitch(struct snd_opl3
*opl3
, int voice
)
740 unsigned short reg_side
;
741 unsigned char voice_offset
;
742 unsigned short opl3_reg
;
744 unsigned char fnum
, blocknum
;
746 struct snd_opl3_voice
*vp
;
748 if (snd_BUG_ON(voice
>= MAX_OPL3_VOICES
))
751 vp
= &opl3
->voices
[voice
];
752 if (vp
->chan
== NULL
)
753 return; /* not allocated? */
755 if (voice
< MAX_OPL2_VOICES
) {
756 /* Left register block for voices 0 .. 8 */
757 reg_side
= OPL3_LEFT
;
758 voice_offset
= voice
;
760 /* Right register block for voices 9 .. 17 */
761 reg_side
= OPL3_RIGHT
;
762 voice_offset
= voice
- MAX_OPL2_VOICES
;
765 snd_opl3_calc_pitch(&fnum
, &blocknum
, vp
->note
, vp
->chan
);
767 /* Set OPL3 FNUM_LOW register of requested voice */
768 opl3_reg
= reg_side
| (OPL3_REG_FNUM_LOW
+ voice_offset
);
769 opl3
->command(opl3
, opl3_reg
, fnum
);
771 vp
->keyon_reg
= blocknum
;
773 /* Set output sound flag */
774 blocknum
|= OPL3_KEYON_BIT
;
776 /* Set OPL3 KEYON_BLOCK register of requested voice */
777 opl3_reg
= reg_side
| (OPL3_REG_KEYON_BLOCK
+ voice_offset
);
778 opl3
->command(opl3
, opl3_reg
, blocknum
);
780 vp
->time
= opl3
->use_time
++;
784 * Update voice pitch controller
786 static void snd_opl3_pitch_ctrl(struct snd_opl3
*opl3
, struct snd_midi_channel
*chan
)
789 struct snd_opl3_voice
*vp
;
793 spin_lock_irqsave(&opl3
->voice_lock
, flags
);
795 if (opl3
->synth_mode
== SNDRV_OPL3_MODE_SEQ
) {
796 for (voice
= 0; voice
< opl3
->max_voices
; voice
++) {
797 vp
= &opl3
->voices
[voice
];
798 if (vp
->state
> 0 && vp
->chan
== chan
) {
799 snd_opl3_update_pitch(opl3
, voice
);
803 /* remap OSS voices */
804 if (chan
->number
< MAX_OPL3_VOICES
) {
805 voice
= snd_opl3_oss_map
[chan
->number
];
806 snd_opl3_update_pitch(opl3
, voice
);
809 spin_unlock_irqrestore(&opl3
->voice_lock
, flags
);
813 * Deal with a controller type event. This includes all types of
814 * control events, not just the midi controllers
816 void snd_opl3_control(void *p
, int type
, struct snd_midi_channel
*chan
)
818 struct snd_opl3
*opl3
;
822 snd_printk(KERN_DEBUG
"Controller, TYPE = %i, ch#: %i, inst#: %i\n",
823 type
, chan
->number
, chan
->midi_program
);
827 case MIDI_CTL_MSB_MODWHEEL
:
828 if (chan
->control
[MIDI_CTL_MSB_MODWHEEL
] > 63)
829 opl3
->drum_reg
|= OPL3_VIBRATO_DEPTH
;
831 opl3
->drum_reg
&= ~OPL3_VIBRATO_DEPTH
;
832 opl3
->command(opl3
, OPL3_LEFT
| OPL3_REG_PERCUSSION
,
835 case MIDI_CTL_E2_TREMOLO_DEPTH
:
836 if (chan
->control
[MIDI_CTL_E2_TREMOLO_DEPTH
] > 63)
837 opl3
->drum_reg
|= OPL3_TREMOLO_DEPTH
;
839 opl3
->drum_reg
&= ~OPL3_TREMOLO_DEPTH
;
840 opl3
->command(opl3
, OPL3_LEFT
| OPL3_REG_PERCUSSION
,
843 case MIDI_CTL_PITCHBEND
:
844 snd_opl3_pitch_ctrl(opl3
, chan
);
852 void snd_opl3_nrpn(void *p
, struct snd_midi_channel
*chan
,
853 struct snd_midi_channel_set
*chset
)
856 snd_printk(KERN_DEBUG
"NRPN, ch#: %i, inst#: %i\n",
857 chan
->number
, chan
->midi_program
);
864 void snd_opl3_sysex(void *p
, unsigned char *buf
, int len
,
865 int parsed
, struct snd_midi_channel_set
*chset
)
868 snd_printk(KERN_DEBUG
"SYSEX\n");