[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / sound / drivers / opl3 / opl3_midi.c
blob93d674070b71eed61072f291f9adc9cebea21ac4
1 /*
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
22 #undef DEBUG_ALLOC
23 #undef DEBUG_MIDI
25 #include "opl3_voice.h"
26 #include <sound/asoundef.h>
28 extern char snd_opl3_regmap[MAX_OPL2_VOICES][4];
30 extern int use_internal_drums;
33 * The next table looks magical, but it certainly is not. Its values have
34 * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
35 * for i=0. This log-table converts a linear volume-scaling (0..127) to a
36 * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
37 * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
38 * volume -8 it was implemented as a table because it is only 128 bytes and
39 * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>)
42 static char opl3_volume_table[128] =
44 -63, -48, -40, -35, -32, -29, -27, -26,
45 -24, -23, -21, -20, -19, -18, -18, -17,
46 -16, -15, -15, -14, -13, -13, -12, -12,
47 -11, -11, -10, -10, -10, -9, -9, -8,
48 -8, -8, -7, -7, -7, -6, -6, -6,
49 -5, -5, -5, -5, -4, -4, -4, -4,
50 -3, -3, -3, -3, -2, -2, -2, -2,
51 -2, -1, -1, -1, -1, 0, 0, 0,
52 0, 0, 0, 1, 1, 1, 1, 1,
53 1, 2, 2, 2, 2, 2, 2, 2,
54 3, 3, 3, 3, 3, 3, 3, 4,
55 4, 4, 4, 4, 4, 4, 4, 5,
56 5, 5, 5, 5, 5, 5, 5, 5,
57 6, 6, 6, 6, 6, 6, 6, 6,
58 6, 7, 7, 7, 7, 7, 7, 7,
59 7, 7, 7, 8, 8, 8, 8, 8
62 void snd_opl3_calc_volume(unsigned char *volbyte, int vel,
63 snd_midi_channel_t *chan)
65 int oldvol, newvol, n;
66 int volume;
68 volume = (vel * chan->gm_volume * chan->gm_expression) / (127*127);
69 if (volume > 127)
70 volume = 127;
72 oldvol = OPL3_TOTAL_LEVEL_MASK - (*volbyte & OPL3_TOTAL_LEVEL_MASK);
74 newvol = opl3_volume_table[volume] + oldvol;
75 if (newvol > OPL3_TOTAL_LEVEL_MASK)
76 newvol = OPL3_TOTAL_LEVEL_MASK;
77 else if (newvol < 0)
78 newvol = 0;
80 n = OPL3_TOTAL_LEVEL_MASK - (newvol & OPL3_TOTAL_LEVEL_MASK);
82 *volbyte = (*volbyte & OPL3_KSL_MASK) | (n & OPL3_TOTAL_LEVEL_MASK);
86 * Converts the note frequency to block and fnum values for the FM chip
88 static short opl3_note_table[16] =
90 305, 323, /* for pitch bending, -2 semitones */
91 343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647,
92 686, 726 /* for pitch bending, +2 semitones */
95 static void snd_opl3_calc_pitch(unsigned char *fnum, unsigned char *blocknum,
96 int note, snd_midi_channel_t *chan)
98 int block = ((note / 12) & 0x07) - 1;
99 int idx = (note % 12) + 2;
100 int freq;
102 if (chan->midi_pitchbend) {
103 int pitchbend = chan->midi_pitchbend;
104 int segment;
106 if (pitchbend > 0x1FFF)
107 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;
113 } else {
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);
123 #ifdef DEBUG_ALLOC
124 static void debug_alloc(opl3_t *opl3, char *s, int voice) {
125 int i;
126 char *str = "x.24";
128 printk("time %.5i: %s [%.2i]: ", opl3->use_time, s, voice);
129 for (i = 0; i < opl3->max_voices; i++)
130 printk("%c", *(str + opl3->voices[i].state + 1));
131 printk("\n");
133 #endif
136 * Get a FM voice (channel) to play a note on.
138 static int opl3_get_voice(opl3_t *opl3, int instr_4op,
139 snd_midi_channel_t *chan) {
140 int chan_4op_1; /* first voice for 4op instrument */
141 int chan_4op_2; /* second voice for 4op instrument */
143 snd_opl3_voice_t *vp, *vp2;
144 unsigned int voice_time;
145 int i;
147 #ifdef DEBUG_ALLOC
148 char *alloc_type[3] = { "FREE ", "CHEAP ", "EXPENSIVE" };
149 #endif
151 /* This is our "allocation cost" table */
152 enum {
153 FREE = 0, CHEAP, EXPENSIVE, END
156 /* Keeps track of what we are finding */
157 struct best {
158 unsigned int time;
159 int voice;
160 } best[END];
161 struct best *bp;
163 for (i = 0; i < END; i++) {
164 best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */;
165 best[i].voice = -1;
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) */
175 continue;
177 voice_time = vp->time;
178 bp = best;
180 chan_4op_1 = ((i < 3) || (i > 8 && i < 12));
181 chan_4op_2 = ((i > 2 && i < 6) || (i > 11 && i < 15));
182 if (instr_4op) {
183 /* allocate 4op voice */
184 /* skip channels unavailable to 4op instrument */
185 if (!chan_4op_1)
186 continue;
188 if (vp->state)
189 /* kill one voice, CHEAP */
190 bp++;
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 */
196 bp++;
197 voice_time = (voice_time > vp->time) ?
198 voice_time : vp->time;
200 } else {
201 /* allocate 2op voice */
202 if ((chan_4op_1) || (chan_4op_2))
203 /* use bounded channels for 2op, CHEAP */
204 bp++;
205 else if (vp->state)
206 /* kill one voice on 2op channel, CHEAP */
207 bp++;
208 /* raise kill cost to EXPENSIVE for all channels */
209 if (vp->state)
210 bp++;
212 if (voice_time < bp->time) {
213 bp->time = voice_time;
214 bp->voice = i;
218 for (i = 0; i < END; i++) {
219 if (best[i].voice >= 0) {
220 #ifdef DEBUG_ALLOC
221 printk("%s %iop allocation on voice %i\n",
222 alloc_type[i], instr_4op ? 4 : 2,
223 best[i].voice);
224 #endif
225 return best[i].voice;
228 /* not found */
229 return -1;
232 /* ------------------------------ */
235 * System timer interrupt function
237 void snd_opl3_timer_func(unsigned long data)
240 opl3_t *opl3 = (opl3_t *)data;
241 int again = 0;
242 int i;
244 spin_lock(&opl3->sys_timer_lock);
245 for (i = 0; i < opl3->max_voices; i++) {
246 snd_opl3_voice_t *vp = &opl3->voices[i];
247 if (vp->state > 0 && vp->note_off_check) {
248 if (vp->note_off == jiffies)
249 snd_opl3_note_off(opl3, vp->note, 0, vp->chan);
250 else
251 again++;
254 if (again) {
255 opl3->tlist.expires = jiffies + 1; /* invoke again */
256 add_timer(&opl3->tlist);
257 } else {
258 opl3->sys_timer_status = 0;
260 spin_unlock(&opl3->sys_timer_lock);
264 * Start system timer
266 static void snd_opl3_start_timer(opl3_t *opl3)
268 unsigned long flags;
269 spin_lock_irqsave(&opl3->sys_timer_lock, flags);
270 if (! opl3->sys_timer_status) {
271 opl3->tlist.expires = jiffies + 1;
272 add_timer(&opl3->tlist);
273 opl3->sys_timer_status = 1;
275 spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
278 /* ------------------------------ */
281 static int snd_opl3_oss_map[MAX_OPL3_VOICES] = {
282 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14
286 * Start a note.
288 void snd_opl3_note_on(void *p, int note, int vel, snd_midi_channel_t *chan)
290 opl3_t *opl3;
291 snd_seq_instr_t wanted;
292 snd_seq_kinstr_t *kinstr;
293 int instr_4op;
295 int voice;
296 snd_opl3_voice_t *vp, *vp2;
297 unsigned short connect_mask;
298 unsigned char connection;
299 unsigned char vol_op[4];
301 int extra_prg = 0;
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;
309 int key = note;
310 unsigned char fnum, blocknum;
311 int i;
313 fm_instrument_t *fm;
314 unsigned long flags;
316 opl3 = p;
318 #ifdef DEBUG_MIDI
319 snd_printk("Note on, ch %i, inst %i, note %i, vel %i\n",
320 chan->number, chan->midi_program, note, vel);
321 #endif
322 wanted.cluster = 0;
323 wanted.std = SNDRV_SEQ_INSTR_TYPE2_OPL2_3;
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 */
330 wanted.bank = 128;
331 wanted.prg = note;
332 } else {
333 wanted.bank = chan->gm_bank_select;
334 wanted.prg = chan->midi_program;
336 } else {
337 /* Prepare for OSS mode */
338 if (chan->number >= MAX_OPL3_VOICES)
339 return;
341 /* OSS instruments are located in bank 127 */
342 wanted.bank = 127;
343 wanted.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);
351 return;
354 __extra_prg:
355 kinstr = snd_seq_instr_find(opl3->ilist, &wanted, 1, 0);
356 if (kinstr == NULL) {
357 spin_unlock_irqrestore(&opl3->voice_lock, flags);
358 return;
361 fm = KINSTR_DATA(kinstr);
363 switch (fm->type) {
364 case FM_PATCH_OPL2:
365 instr_4op = 0;
366 break;
367 case FM_PATCH_OPL3:
368 if (opl3->hardware >= OPL3_HW_OPL3) {
369 instr_4op = 1;
370 break;
372 default:
373 snd_seq_instr_free_use(opl3->ilist, kinstr);
374 spin_unlock_irqrestore(&opl3->voice_lock, flags);
375 return;
378 #ifdef DEBUG_MIDI
379 snd_printk(" --> OPL%i instrument: %s\n",
380 instr_4op ? 3 : 2, kinstr->name);
381 #endif
382 /* in SYNTH mode, application takes care of voices */
383 /* in SEQ mode, allocate voice on free OPL3 channel */
384 if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
385 voice = opl3_get_voice(opl3, instr_4op, chan);
386 } else {
387 /* remap OSS voice */
388 voice = snd_opl3_oss_map[chan->number];
391 if (voice < MAX_OPL2_VOICES) {
392 /* Left register block for voices 0 .. 8 */
393 reg_side = OPL3_LEFT;
394 voice_offset = voice;
395 connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07;
396 } else {
397 /* Right register block for voices 9 .. 17 */
398 reg_side = OPL3_RIGHT;
399 voice_offset = voice - MAX_OPL2_VOICES;
400 connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38;
403 /* kill voice on channel */
404 vp = &opl3->voices[voice];
405 if (vp->state > 0) {
406 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
407 reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
408 opl3->command(opl3, opl3_reg, reg_val);
410 if (instr_4op) {
411 vp2 = &opl3->voices[voice + 3];
412 if (vp->state > 0) {
413 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
414 voice_offset + 3);
415 reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
416 opl3->command(opl3, opl3_reg, reg_val);
420 /* set connection register */
421 if (instr_4op) {
422 if ((opl3->connection_reg ^ connect_mask) & connect_mask) {
423 opl3->connection_reg |= connect_mask;
424 /* set connection bit */
425 opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
426 opl3->command(opl3, opl3_reg, opl3->connection_reg);
428 } else {
429 if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) {
430 opl3->connection_reg &= ~connect_mask;
431 /* clear connection bit */
432 opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
433 opl3->command(opl3, opl3_reg, opl3->connection_reg);
437 #ifdef DEBUG_MIDI
438 snd_printk(" --> setting OPL3 connection: 0x%x\n",
439 opl3->connection_reg);
440 #endif
442 * calculate volume depending on connection
443 * between FM operators (see include/opl3.h)
445 for (i = 0; i < (instr_4op ? 4 : 2); i++)
446 vol_op[i] = fm->op[i].ksl_level;
448 connection = fm->feedback_connection[0] & 0x01;
449 if (instr_4op) {
450 connection <<= 1;
451 connection |= fm->feedback_connection[1] & 0x01;
453 snd_opl3_calc_volume(&vol_op[3], vel, chan);
454 switch (connection) {
455 case 0x03:
456 snd_opl3_calc_volume(&vol_op[2], vel, chan);
457 /* fallthru */
458 case 0x02:
459 snd_opl3_calc_volume(&vol_op[0], vel, chan);
460 break;
461 case 0x01:
462 snd_opl3_calc_volume(&vol_op[1], vel, chan);
464 } else {
465 snd_opl3_calc_volume(&vol_op[1], vel, chan);
466 if (connection)
467 snd_opl3_calc_volume(&vol_op[0], vel, chan);
470 /* Program the FM voice characteristics */
471 for (i = 0; i < (instr_4op ? 4 : 2); i++) {
472 #ifdef DEBUG_MIDI
473 snd_printk(" --> programming operator %i\n", i);
474 #endif
475 op_offset = snd_opl3_regmap[voice_offset][i];
477 /* Set OPL3 AM_VIB register of requested voice/operator */
478 reg_val = fm->op[i].am_vib;
479 opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
480 opl3->command(opl3, opl3_reg, reg_val);
482 /* Set OPL3 KSL_LEVEL register of requested voice/operator */
483 reg_val = vol_op[i];
484 opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
485 opl3->command(opl3, opl3_reg, reg_val);
487 /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
488 reg_val = fm->op[i].attack_decay;
489 opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
490 opl3->command(opl3, opl3_reg, reg_val);
492 /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
493 reg_val = fm->op[i].sustain_release;
494 opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
495 opl3->command(opl3, opl3_reg, reg_val);
497 /* Select waveform */
498 reg_val = fm->op[i].wave_select;
499 opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
500 opl3->command(opl3, opl3_reg, reg_val);
503 /* Set operator feedback and 2op inter-operator connection */
504 reg_val = fm->feedback_connection[0];
505 /* Set output voice connection */
506 reg_val |= OPL3_STEREO_BITS;
507 if (chan->gm_pan < 43)
508 reg_val &= ~OPL3_VOICE_TO_RIGHT;
509 if (chan->gm_pan > 85)
510 reg_val &= ~OPL3_VOICE_TO_LEFT;
511 opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
512 opl3->command(opl3, opl3_reg, reg_val);
514 if (instr_4op) {
515 /* Set 4op inter-operator connection */
516 reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT;
517 /* Set output voice connection */
518 reg_val |= OPL3_STEREO_BITS;
519 if (chan->gm_pan < 43)
520 reg_val &= ~OPL3_VOICE_TO_RIGHT;
521 if (chan->gm_pan > 85)
522 reg_val &= ~OPL3_VOICE_TO_LEFT;
523 opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION +
524 voice_offset + 3);
525 opl3->command(opl3, opl3_reg, reg_val);
529 * Special treatment of percussion notes for fm:
530 * Requested pitch is really program, and pitch for
531 * device is whatever was specified in the patch library.
533 if (fm->fix_key)
534 note = fm->fix_key;
536 * use transpose if defined in patch library
538 if (fm->trnsps)
539 note += (fm->trnsps - 64);
541 snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
543 /* Set OPL3 FNUM_LOW register of requested voice */
544 opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
545 opl3->command(opl3, opl3_reg, fnum);
547 opl3->voices[voice].keyon_reg = blocknum;
549 /* Set output sound flag */
550 blocknum |= OPL3_KEYON_BIT;
552 #ifdef DEBUG_MIDI
553 snd_printk(" --> trigger voice %i\n", voice);
554 #endif
555 /* Set OPL3 KEYON_BLOCK register of requested voice */
556 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
557 opl3->command(opl3, opl3_reg, blocknum);
559 /* kill note after fixed duration (in centiseconds) */
560 if (fm->fix_dur) {
561 opl3->voices[voice].note_off = jiffies +
562 (fm->fix_dur * HZ) / 100;
563 snd_opl3_start_timer(opl3);
564 opl3->voices[voice].note_off_check = 1;
565 } else
566 opl3->voices[voice].note_off_check = 0;
568 /* get extra pgm, but avoid possible loops */
569 extra_prg = (extra_prg) ? 0 : fm->modes;
571 snd_seq_instr_free_use(opl3->ilist, kinstr);
573 /* do the bookkeeping */
574 vp->time = opl3->use_time++;
575 vp->note = key;
576 vp->chan = chan;
578 if (instr_4op) {
579 vp->state = SNDRV_OPL3_ST_ON_4OP;
581 vp2 = &opl3->voices[voice + 3];
582 vp2->time = opl3->use_time++;
583 vp2->note = key;
584 vp2->chan = chan;
585 vp2->state = SNDRV_OPL3_ST_NOT_AVAIL;
586 } else {
587 if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
588 /* 4op killed by 2op, release bounded voice */
589 vp2 = &opl3->voices[voice + 3];
590 vp2->time = opl3->use_time++;
591 vp2->state = SNDRV_OPL3_ST_OFF;
593 vp->state = SNDRV_OPL3_ST_ON_2OP;
596 #ifdef DEBUG_ALLOC
597 debug_alloc(opl3, "note on ", voice);
598 #endif
600 /* allocate extra program if specified in patch library */
601 if (extra_prg) {
602 if (extra_prg > 128) {
603 wanted.bank = 128;
604 /* percussions start at 35 */
605 wanted.prg = extra_prg - 128 + 35 - 1;
606 } else {
607 wanted.bank = 0;
608 wanted.prg = extra_prg - 1;
610 #ifdef DEBUG_MIDI
611 snd_printk(" *** allocating extra program\n");
612 #endif
613 goto __extra_prg;
615 spin_unlock_irqrestore(&opl3->voice_lock, flags);
618 static void snd_opl3_kill_voice(opl3_t *opl3, int voice)
620 unsigned short reg_side;
621 unsigned char voice_offset;
622 unsigned short opl3_reg;
624 snd_opl3_voice_t *vp, *vp2;
626 snd_assert(voice < MAX_OPL3_VOICES, return);
628 vp = &opl3->voices[voice];
629 if (voice < MAX_OPL2_VOICES) {
630 /* Left register block for voices 0 .. 8 */
631 reg_side = OPL3_LEFT;
632 voice_offset = voice;
633 } else {
634 /* Right register block for voices 9 .. 17 */
635 reg_side = OPL3_RIGHT;
636 voice_offset = voice - MAX_OPL2_VOICES;
639 /* kill voice */
640 #ifdef DEBUG_MIDI
641 snd_printk(" --> kill voice %i\n", voice);
642 #endif
643 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
644 /* clear Key ON bit */
645 opl3->command(opl3, opl3_reg, vp->keyon_reg);
647 /* do the bookkeeping */
648 vp->time = opl3->use_time++;
650 if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
651 vp2 = &opl3->voices[voice + 3];
653 vp2->time = opl3->use_time++;
654 vp2->state = SNDRV_OPL3_ST_OFF;
656 vp->state = SNDRV_OPL3_ST_OFF;
657 #ifdef DEBUG_ALLOC
658 debug_alloc(opl3, "note off", voice);
659 #endif
664 * Release a note in response to a midi note off.
666 void snd_opl3_note_off(void *p, int note, int vel, snd_midi_channel_t *chan)
668 opl3_t *opl3;
670 int voice;
671 snd_opl3_voice_t *vp;
673 unsigned long flags;
675 opl3 = p;
677 #ifdef DEBUG_MIDI
678 snd_printk("Note off, ch %i, inst %i, note %i\n",
679 chan->number, chan->midi_program, note);
680 #endif
682 spin_lock_irqsave(&opl3->voice_lock, flags);
684 if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
685 if (chan->drum_channel && use_internal_drums) {
686 snd_opl3_drum_switch(opl3, note, vel, 0, chan);
687 spin_unlock_irqrestore(&opl3->voice_lock, flags);
688 return;
690 /* this loop will hopefully kill all extra voices, because
691 they are grouped by the same channel and note values */
692 for (voice = 0; voice < opl3->max_voices; voice++) {
693 vp = &opl3->voices[voice];
694 if (vp->state > 0 && vp->chan == chan && vp->note == note) {
695 snd_opl3_kill_voice(opl3, voice);
698 } else {
699 /* remap OSS voices */
700 if (chan->number < MAX_OPL3_VOICES) {
701 voice = snd_opl3_oss_map[chan->number];
702 snd_opl3_kill_voice(opl3, voice);
705 spin_unlock_irqrestore(&opl3->voice_lock, flags);
709 * key pressure change
711 void snd_opl3_key_press(void *p, int note, int vel, snd_midi_channel_t *chan)
713 opl3_t *opl3;
715 opl3 = p;
716 #ifdef DEBUG_MIDI
717 snd_printk("Key pressure, ch#: %i, inst#: %i\n",
718 chan->number, chan->midi_program);
719 #endif
723 * terminate note
725 void snd_opl3_terminate_note(void *p, int note, snd_midi_channel_t *chan)
727 opl3_t *opl3;
729 opl3 = p;
730 #ifdef DEBUG_MIDI
731 snd_printk("Terminate note, ch#: %i, inst#: %i\n",
732 chan->number, chan->midi_program);
733 #endif
736 static void snd_opl3_update_pitch(opl3_t *opl3, int voice)
738 unsigned short reg_side;
739 unsigned char voice_offset;
740 unsigned short opl3_reg;
742 unsigned char fnum, blocknum;
744 snd_opl3_voice_t *vp;
746 snd_assert(voice < MAX_OPL3_VOICES, return);
748 vp = &opl3->voices[voice];
749 if (vp->chan == NULL)
750 return; /* not allocated? */
752 if (voice < MAX_OPL2_VOICES) {
753 /* Left register block for voices 0 .. 8 */
754 reg_side = OPL3_LEFT;
755 voice_offset = voice;
756 } else {
757 /* Right register block for voices 9 .. 17 */
758 reg_side = OPL3_RIGHT;
759 voice_offset = voice - MAX_OPL2_VOICES;
762 snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
764 /* Set OPL3 FNUM_LOW register of requested voice */
765 opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
766 opl3->command(opl3, opl3_reg, fnum);
768 vp->keyon_reg = blocknum;
770 /* Set output sound flag */
771 blocknum |= OPL3_KEYON_BIT;
773 /* Set OPL3 KEYON_BLOCK register of requested voice */
774 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
775 opl3->command(opl3, opl3_reg, blocknum);
777 vp->time = opl3->use_time++;
781 * Update voice pitch controller
783 static void snd_opl3_pitch_ctrl(opl3_t *opl3, snd_midi_channel_t *chan)
785 int voice;
786 snd_opl3_voice_t *vp;
788 unsigned long flags;
790 spin_lock_irqsave(&opl3->voice_lock, flags);
792 if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
793 for (voice = 0; voice < opl3->max_voices; voice++) {
794 vp = &opl3->voices[voice];
795 if (vp->state > 0 && vp->chan == chan) {
796 snd_opl3_update_pitch(opl3, voice);
799 } else {
800 /* remap OSS voices */
801 if (chan->number < MAX_OPL3_VOICES) {
802 voice = snd_opl3_oss_map[chan->number];
803 snd_opl3_update_pitch(opl3, voice);
806 spin_unlock_irqrestore(&opl3->voice_lock, flags);
810 * Deal with a controler type event. This includes all types of
811 * control events, not just the midi controllers
813 void snd_opl3_control(void *p, int type, snd_midi_channel_t *chan)
815 opl3_t *opl3;
817 opl3 = p;
818 #ifdef DEBUG_MIDI
819 snd_printk("Controller, TYPE = %i, ch#: %i, inst#: %i\n",
820 type, chan->number, chan->midi_program);
821 #endif
823 switch (type) {
824 case MIDI_CTL_MSB_MODWHEEL:
825 if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
826 opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
827 else
828 opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
829 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
830 opl3->drum_reg);
831 break;
832 case MIDI_CTL_E2_TREMOLO_DEPTH:
833 if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
834 opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
835 else
836 opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
837 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
838 opl3->drum_reg);
839 break;
840 case MIDI_CTL_PITCHBEND:
841 snd_opl3_pitch_ctrl(opl3, chan);
842 break;
847 * NRPN events
849 void snd_opl3_nrpn(void *p, snd_midi_channel_t *chan,
850 snd_midi_channel_set_t *chset)
852 opl3_t *opl3;
854 opl3 = p;
855 #ifdef DEBUG_MIDI
856 snd_printk("NRPN, ch#: %i, inst#: %i\n",
857 chan->number, chan->midi_program);
858 #endif
862 * receive sysex
864 void snd_opl3_sysex(void *p, unsigned char *buf, int len,
865 int parsed, snd_midi_channel_set_t *chset)
867 opl3_t *opl3;
869 opl3 = p;
870 #ifdef DEBUG_MIDI
871 snd_printk("SYSEX\n");
872 #endif