Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/shaggy...
[linux-2.6] / sound / drivers / opl3 / opl3_midi.c
1 /*
2  *  Copyright (c) by Uros Bizjak <uros@kss-loka.si>
3  *
4  *  Midi synth routines for OPL2/OPL3/OPL4 FM
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #undef DEBUG_ALLOC
23 #undef DEBUG_MIDI
24
25 #include "opl3_voice.h"
26 #include <sound/asoundef.h>
27
28 extern char snd_opl3_regmap[MAX_OPL2_VOICES][4];
29
30 extern int use_internal_drums;
31
32 /*
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>)
40  */
41
42 static char opl3_volume_table[128] =
43 {
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
60 };
61
62 void snd_opl3_calc_volume(unsigned char *volbyte, int vel,
63                           struct snd_midi_channel *chan)
64 {
65         int oldvol, newvol, n;
66         int volume;
67
68         volume = (vel * chan->gm_volume * chan->gm_expression) / (127*127);
69         if (volume > 127)
70                 volume = 127;
71
72         oldvol = OPL3_TOTAL_LEVEL_MASK - (*volbyte & OPL3_TOTAL_LEVEL_MASK);
73
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;
79
80         n = OPL3_TOTAL_LEVEL_MASK - (newvol & OPL3_TOTAL_LEVEL_MASK);
81
82         *volbyte = (*volbyte & OPL3_KSL_MASK) | (n & OPL3_TOTAL_LEVEL_MASK);
83 }
84
85 /*
86  * Converts the note frequency to block and fnum values for the FM chip
87  */
88 static short opl3_note_table[16] =
89 {
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 */
93 };
94
95 static void snd_opl3_calc_pitch(unsigned char *fnum, unsigned char *blocknum,
96                                 int note, struct snd_midi_channel *chan)
97 {
98         int block = ((note / 12) & 0x07) - 1;
99         int idx = (note % 12) + 2;
100         int freq;
101
102         if (chan->midi_pitchbend) {
103                 int pitchbend = chan->midi_pitchbend;
104                 int segment;
105
106                 if (pitchbend > 0x1FFF)
107                         pitchbend = 0x1FFF;
108
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];
115         }
116
117         *fnum = (unsigned char) freq;
118         *blocknum = ((freq >> 8) & OPL3_FNUM_HIGH_MASK) |
119                 ((block << 2) & OPL3_BLOCKNUM_MASK);
120 }
121
122
123 #ifdef DEBUG_ALLOC
124 static void debug_alloc(struct snd_opl3 *opl3, char *s, int voice) {
125         int i;
126         char *str = "x.24";
127
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");
132 }
133 #endif
134
135 /*
136  * Get a FM voice (channel) to play a note on.
137  */
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 */
142
143         struct snd_opl3_voice *vp, *vp2;
144         unsigned int voice_time;
145         int i;
146
147 #ifdef DEBUG_ALLOC
148         char *alloc_type[3] = { "FREE     ", "CHEAP    ", "EXPENSIVE" };
149 #endif
150
151         /* This is our "allocation cost" table */
152         enum {
153                 FREE = 0, CHEAP, EXPENSIVE, END
154         };
155
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;
162
163         for (i = 0; i < END; i++) {
164                 best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */;
165                 best[i].voice = -1;
166         }
167
168         /* Look through all the channels for the most suitable. */
169         for (i = 0; i < opl3->max_voices; i++) {
170                 vp = &opl3->voices[i];
171
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;
176
177                 voice_time = vp->time;
178                 bp = best;
179
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;
187
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;
199                         }
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++;
211                 }
212                 if (voice_time < bp->time) {
213                         bp->time = voice_time;
214                         bp->voice = i;
215                 }
216         }
217
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;
226                 }
227         }
228         /* not found */
229         return -1;
230 }
231
232 /* ------------------------------ */
233
234 /*
235  * System timer interrupt function
236  */
237 void snd_opl3_timer_func(unsigned long data)
238 {
239
240         struct snd_opl3 *opl3 = (struct snd_opl3 *)data;
241         unsigned long flags;
242         int again = 0;
243         int i;
244
245         spin_lock_irqsave(&opl3->sys_timer_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(opl3, vp->note, 0, vp->chan);
251                         else
252                                 again++;
253                 }
254         }
255         if (again) {
256                 opl3->tlist.expires = jiffies + 1;      /* invoke again */
257                 add_timer(&opl3->tlist);
258         } else {
259                 opl3->sys_timer_status = 0;
260         }
261         spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
262 }
263
264 /*
265  * Start system timer
266  */
267 static void snd_opl3_start_timer(struct snd_opl3 *opl3)
268 {
269         unsigned long flags;
270         spin_lock_irqsave(&opl3->sys_timer_lock, flags);
271         if (! opl3->sys_timer_status) {
272                 opl3->tlist.expires = jiffies + 1;
273                 add_timer(&opl3->tlist);
274                 opl3->sys_timer_status = 1;
275         }
276         spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
277 }
278
279 /* ------------------------------ */
280
281
282 static int snd_opl3_oss_map[MAX_OPL3_VOICES] = {
283         0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14
284 };
285
286 /*
287  * Start a note.
288  */
289 void snd_opl3_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
290 {
291         struct snd_opl3 *opl3;
292         struct snd_seq_instr wanted;
293         struct snd_seq_kinstr *kinstr;
294         int instr_4op;
295
296         int voice;
297         struct snd_opl3_voice *vp, *vp2;
298         unsigned short connect_mask;
299         unsigned char connection;
300         unsigned char vol_op[4];
301
302         int extra_prg = 0;
303
304         unsigned short reg_side;
305         unsigned char op_offset;
306         unsigned char voice_offset;
307         unsigned short opl3_reg;
308         unsigned char reg_val;
309
310         int key = note;
311         unsigned char fnum, blocknum;
312         int i;
313
314         struct fm_instrument *fm;
315         unsigned long flags;
316
317         opl3 = p;
318
319 #ifdef DEBUG_MIDI
320         snd_printk("Note on, ch %i, inst %i, note %i, vel %i\n",
321                    chan->number, chan->midi_program, note, vel);
322 #endif
323         wanted.cluster = 0;
324         wanted.std = SNDRV_SEQ_INSTR_TYPE2_OPL2_3;
325
326         /* in SYNTH mode, application takes care of voices */
327         /* in SEQ mode, drum voice numbers are notes on drum channel */
328         if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
329                 if (chan->drum_channel) {
330                         /* percussion instruments are located in bank 128 */
331                         wanted.bank = 128;
332                         wanted.prg = note;
333                 } else {
334                         wanted.bank = chan->gm_bank_select;
335                         wanted.prg = chan->midi_program;
336                 }
337         } else {
338                 /* Prepare for OSS mode */
339                 if (chan->number >= MAX_OPL3_VOICES)
340                         return;
341
342                 /* OSS instruments are located in bank 127 */
343                 wanted.bank = 127;
344                 wanted.prg = chan->midi_program;
345         }
346
347         spin_lock_irqsave(&opl3->voice_lock, flags);
348
349         if (use_internal_drums) {
350                 snd_opl3_drum_switch(opl3, note, vel, 1, chan);
351                 spin_unlock_irqrestore(&opl3->voice_lock, flags);
352                 return;
353         }
354
355  __extra_prg:
356         kinstr = snd_seq_instr_find(opl3->ilist, &wanted, 1, 0);
357         if (kinstr == NULL) {
358                 spin_unlock_irqrestore(&opl3->voice_lock, flags);
359                 return;
360         }
361
362         fm = KINSTR_DATA(kinstr);
363
364         switch (fm->type) {
365         case FM_PATCH_OPL2:
366                 instr_4op = 0;
367                 break;
368         case FM_PATCH_OPL3:
369                 if (opl3->hardware >= OPL3_HW_OPL3) {
370                         instr_4op = 1;
371                         break;
372                 }
373         default:
374                 snd_seq_instr_free_use(opl3->ilist, kinstr);
375                 spin_unlock_irqrestore(&opl3->voice_lock, flags);
376                 return;
377         }
378
379 #ifdef DEBUG_MIDI
380         snd_printk("  --> OPL%i instrument: %s\n",
381                    instr_4op ? 3 : 2, kinstr->name);
382 #endif
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);
387         } else {
388                 /* remap OSS voice */
389                 voice = snd_opl3_oss_map[chan->number];         
390         }
391
392         if (voice < MAX_OPL2_VOICES) {
393                 /* Left register block for voices 0 .. 8 */
394                 reg_side = OPL3_LEFT;
395                 voice_offset = voice;
396                 connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07;
397         } else {
398                 /* Right register block for voices 9 .. 17 */
399                 reg_side = OPL3_RIGHT;
400                 voice_offset = voice - MAX_OPL2_VOICES;
401                 connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38;
402         }
403
404         /* kill voice on channel */
405         vp = &opl3->voices[voice];
406         if (vp->state > 0) {
407                 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
408                 reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
409                 opl3->command(opl3, opl3_reg, reg_val);
410         }
411         if (instr_4op) {
412                 vp2 = &opl3->voices[voice + 3];
413                 if (vp->state > 0) {
414                         opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
415                                                voice_offset + 3);
416                         reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
417                         opl3->command(opl3, opl3_reg, reg_val);
418                 }
419         }
420
421         /* set connection register */
422         if (instr_4op) {
423                 if ((opl3->connection_reg ^ connect_mask) & connect_mask) {
424                         opl3->connection_reg |= connect_mask;
425                         /* set connection bit */
426                         opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
427                         opl3->command(opl3, opl3_reg, opl3->connection_reg);
428                 }
429         } else {
430                 if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) {
431                         opl3->connection_reg &= ~connect_mask;
432                         /* clear connection bit */
433                         opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
434                         opl3->command(opl3, opl3_reg, opl3->connection_reg);
435                 }
436         }
437
438 #ifdef DEBUG_MIDI
439         snd_printk("  --> setting OPL3 connection: 0x%x\n",
440                    opl3->connection_reg);
441 #endif
442         /*
443          * calculate volume depending on connection
444          * between FM operators (see include/opl3.h)
445          */
446         for (i = 0; i < (instr_4op ? 4 : 2); i++)
447                 vol_op[i] = fm->op[i].ksl_level;
448
449         connection = fm->feedback_connection[0] & 0x01;
450         if (instr_4op) {
451                 connection <<= 1;
452                 connection |= fm->feedback_connection[1] & 0x01;
453
454                 snd_opl3_calc_volume(&vol_op[3], vel, chan);
455                 switch (connection) {
456                 case 0x03:
457                         snd_opl3_calc_volume(&vol_op[2], vel, chan);
458                         /* fallthru */
459                 case 0x02:
460                         snd_opl3_calc_volume(&vol_op[0], vel, chan);
461                         break;
462                 case 0x01:
463                         snd_opl3_calc_volume(&vol_op[1], vel, chan);
464                 }
465         } else {
466                 snd_opl3_calc_volume(&vol_op[1], vel, chan);
467                 if (connection)
468                         snd_opl3_calc_volume(&vol_op[0], vel, chan);
469         }
470
471         /* Program the FM voice characteristics */
472         for (i = 0; i < (instr_4op ? 4 : 2); i++) {
473 #ifdef DEBUG_MIDI
474                 snd_printk("  --> programming operator %i\n", i);
475 #endif
476                 op_offset = snd_opl3_regmap[voice_offset][i];
477
478                 /* Set OPL3 AM_VIB register of requested voice/operator */ 
479                 reg_val = fm->op[i].am_vib;
480                 opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
481                 opl3->command(opl3, opl3_reg, reg_val);
482
483                 /* Set OPL3 KSL_LEVEL register of requested voice/operator */ 
484                 reg_val = vol_op[i];
485                 opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
486                 opl3->command(opl3, opl3_reg, reg_val);
487
488                 /* Set OPL3 ATTACK_DECAY register of requested voice/operator */ 
489                 reg_val = fm->op[i].attack_decay;
490                 opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
491                 opl3->command(opl3, opl3_reg, reg_val);
492
493                 /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */ 
494                 reg_val = fm->op[i].sustain_release;
495                 opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
496                 opl3->command(opl3, opl3_reg, reg_val);
497
498                 /* Select waveform */
499                 reg_val = fm->op[i].wave_select;
500                 opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
501                 opl3->command(opl3, opl3_reg, reg_val);
502         }
503
504         /* Set operator feedback and 2op inter-operator connection */
505         reg_val = fm->feedback_connection[0];
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 + voice_offset);
513         opl3->command(opl3, opl3_reg, reg_val);
514
515         if (instr_4op) {
516                 /* Set 4op inter-operator connection */
517                 reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT;
518                 /* Set output voice connection */
519                 reg_val |= OPL3_STEREO_BITS;
520                 if (chan->gm_pan < 43)
521                         reg_val &= ~OPL3_VOICE_TO_RIGHT;
522                 if (chan->gm_pan > 85)
523                         reg_val &= ~OPL3_VOICE_TO_LEFT;
524                 opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION +
525                                        voice_offset + 3);
526                 opl3->command(opl3, opl3_reg, reg_val);
527         }
528
529         /*
530          * Special treatment of percussion notes for fm:
531          * Requested pitch is really program, and pitch for
532          * device is whatever was specified in the patch library.
533          */
534         if (fm->fix_key)
535                 note = fm->fix_key;
536         /*
537          * use transpose if defined in patch library
538          */
539         if (fm->trnsps)
540                 note += (fm->trnsps - 64);
541
542         snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
543
544         /* Set OPL3 FNUM_LOW register of requested voice */
545         opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
546         opl3->command(opl3, opl3_reg, fnum);
547
548         opl3->voices[voice].keyon_reg = blocknum;
549
550         /* Set output sound flag */
551         blocknum |= OPL3_KEYON_BIT;
552
553 #ifdef DEBUG_MIDI
554         snd_printk("  --> trigger voice %i\n", voice);
555 #endif
556         /* Set OPL3 KEYON_BLOCK register of requested voice */ 
557         opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
558         opl3->command(opl3, opl3_reg, blocknum);
559
560         /* kill note after fixed duration (in centiseconds) */
561         if (fm->fix_dur) {
562                 opl3->voices[voice].note_off = jiffies +
563                         (fm->fix_dur * HZ) / 100;
564                 snd_opl3_start_timer(opl3);
565                 opl3->voices[voice].note_off_check = 1;
566         } else
567                 opl3->voices[voice].note_off_check = 0;
568
569         /* get extra pgm, but avoid possible loops */
570         extra_prg = (extra_prg) ? 0 : fm->modes;
571
572         snd_seq_instr_free_use(opl3->ilist, kinstr);
573
574         /* do the bookkeeping */
575         vp->time = opl3->use_time++;
576         vp->note = key;
577         vp->chan = chan;
578
579         if (instr_4op) {
580                 vp->state = SNDRV_OPL3_ST_ON_4OP;
581
582                 vp2 = &opl3->voices[voice + 3];
583                 vp2->time = opl3->use_time++;
584                 vp2->note = key;
585                 vp2->chan = chan;
586                 vp2->state = SNDRV_OPL3_ST_NOT_AVAIL;
587         } else {
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;
593                 }
594                 vp->state = SNDRV_OPL3_ST_ON_2OP;
595         }
596
597 #ifdef DEBUG_ALLOC
598         debug_alloc(opl3, "note on ", voice);
599 #endif
600
601         /* allocate extra program if specified in patch library */
602         if (extra_prg) {
603                 if (extra_prg > 128) {
604                         wanted.bank = 128;
605                         /* percussions start at 35 */
606                         wanted.prg = extra_prg - 128 + 35 - 1;
607                 } else {
608                         wanted.bank = 0;
609                         wanted.prg = extra_prg - 1;
610                 }
611 #ifdef DEBUG_MIDI
612                 snd_printk(" *** allocating extra program\n");
613 #endif
614                 goto __extra_prg;
615         }
616         spin_unlock_irqrestore(&opl3->voice_lock, flags);
617 }
618
619 static void snd_opl3_kill_voice(struct snd_opl3 *opl3, int voice)
620 {
621         unsigned short reg_side;
622         unsigned char voice_offset;
623         unsigned short opl3_reg;
624
625         struct snd_opl3_voice *vp, *vp2;
626
627         snd_assert(voice < MAX_OPL3_VOICES, return);
628
629         vp = &opl3->voices[voice];
630         if (voice < MAX_OPL2_VOICES) {
631                 /* Left register block for voices 0 .. 8 */
632                 reg_side = OPL3_LEFT;
633                 voice_offset = voice;
634         } else {
635                 /* Right register block for voices 9 .. 17 */
636                 reg_side = OPL3_RIGHT;
637                 voice_offset = voice - MAX_OPL2_VOICES;
638         }
639
640         /* kill voice */
641 #ifdef DEBUG_MIDI
642         snd_printk("  --> kill voice %i\n", voice);
643 #endif
644         opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
645         /* clear Key ON bit */
646         opl3->command(opl3, opl3_reg, vp->keyon_reg);
647
648         /* do the bookkeeping */
649         vp->time = opl3->use_time++;
650
651         if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
652                 vp2 = &opl3->voices[voice + 3];
653
654                 vp2->time = opl3->use_time++;
655                 vp2->state = SNDRV_OPL3_ST_OFF;
656         }
657         vp->state = SNDRV_OPL3_ST_OFF;
658 #ifdef DEBUG_ALLOC
659         debug_alloc(opl3, "note off", voice);
660 #endif
661
662 }
663
664 /*
665  * Release a note in response to a midi note off.
666  */
667 void snd_opl3_note_off(void *p, int note, int vel, struct snd_midi_channel *chan)
668 {
669         struct snd_opl3 *opl3;
670
671         int voice;
672         struct snd_opl3_voice *vp;
673
674         unsigned long flags;
675
676         opl3 = p;
677
678 #ifdef DEBUG_MIDI
679         snd_printk("Note off, ch %i, inst %i, note %i\n",
680                    chan->number, chan->midi_program, note);
681 #endif
682
683         spin_lock_irqsave(&opl3->voice_lock, flags);
684
685         if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
686                 if (chan->drum_channel && use_internal_drums) {
687                         snd_opl3_drum_switch(opl3, note, vel, 0, chan);
688                         spin_unlock_irqrestore(&opl3->voice_lock, flags);
689                         return;
690                 }
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);
697                         }
698                 }
699         } else {
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);
704                 }
705         }
706         spin_unlock_irqrestore(&opl3->voice_lock, flags);
707 }
708
709 /*
710  * key pressure change
711  */
712 void snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
713 {
714         struct snd_opl3 *opl3;
715
716         opl3 = p;
717 #ifdef DEBUG_MIDI
718         snd_printk("Key pressure, ch#: %i, inst#: %i\n",
719                    chan->number, chan->midi_program);
720 #endif
721 }
722
723 /*
724  * terminate note
725  */
726 void snd_opl3_terminate_note(void *p, int note, struct snd_midi_channel *chan)
727 {
728         struct snd_opl3 *opl3;
729
730         opl3 = p;
731 #ifdef DEBUG_MIDI
732         snd_printk("Terminate note, ch#: %i, inst#: %i\n",
733                    chan->number, chan->midi_program);
734 #endif
735 }
736
737 static void snd_opl3_update_pitch(struct snd_opl3 *opl3, int voice)
738 {
739         unsigned short reg_side;
740         unsigned char voice_offset;
741         unsigned short opl3_reg;
742
743         unsigned char fnum, blocknum;
744
745         struct snd_opl3_voice *vp;
746
747         snd_assert(voice < MAX_OPL3_VOICES, return);
748
749         vp = &opl3->voices[voice];
750         if (vp->chan == NULL)
751                 return; /* not allocated? */
752
753         if (voice < MAX_OPL2_VOICES) {
754                 /* Left register block for voices 0 .. 8 */
755                 reg_side = OPL3_LEFT;
756                 voice_offset = voice;
757         } else {
758                 /* Right register block for voices 9 .. 17 */
759                 reg_side = OPL3_RIGHT;
760                 voice_offset = voice - MAX_OPL2_VOICES;
761         }
762
763         snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
764
765         /* Set OPL3 FNUM_LOW register of requested voice */
766         opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
767         opl3->command(opl3, opl3_reg, fnum);
768
769         vp->keyon_reg = blocknum;
770
771         /* Set output sound flag */
772         blocknum |= OPL3_KEYON_BIT;
773
774         /* Set OPL3 KEYON_BLOCK register of requested voice */ 
775         opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
776         opl3->command(opl3, opl3_reg, blocknum);
777
778         vp->time = opl3->use_time++;
779 }
780
781 /*
782  * Update voice pitch controller
783  */
784 static void snd_opl3_pitch_ctrl(struct snd_opl3 *opl3, struct snd_midi_channel *chan)
785 {
786         int voice;
787         struct snd_opl3_voice *vp;
788
789         unsigned long flags;
790
791         spin_lock_irqsave(&opl3->voice_lock, flags);
792
793         if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
794                 for (voice = 0; voice < opl3->max_voices; voice++) {
795                         vp = &opl3->voices[voice];
796                         if (vp->state > 0 && vp->chan == chan) {
797                                 snd_opl3_update_pitch(opl3, voice);
798                         }
799                 }
800         } else {
801                 /* remap OSS voices */
802                 if (chan->number < MAX_OPL3_VOICES) {
803                         voice = snd_opl3_oss_map[chan->number];         
804                         snd_opl3_update_pitch(opl3, voice);
805                 }
806         }
807         spin_unlock_irqrestore(&opl3->voice_lock, flags);
808 }
809
810 /*
811  * Deal with a controler type event.  This includes all types of
812  * control events, not just the midi controllers
813  */
814 void snd_opl3_control(void *p, int type, struct snd_midi_channel *chan)
815 {
816         struct snd_opl3 *opl3;
817
818         opl3 = p;
819 #ifdef DEBUG_MIDI
820         snd_printk("Controller, TYPE = %i, ch#: %i, inst#: %i\n",
821                    type, chan->number, chan->midi_program);
822 #endif
823
824         switch (type) {
825         case MIDI_CTL_MSB_MODWHEEL:
826                 if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
827                         opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
828                 else 
829                         opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
830                 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
831                                  opl3->drum_reg);
832                 break;
833         case MIDI_CTL_E2_TREMOLO_DEPTH:
834                 if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
835                         opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
836                 else 
837                         opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
838                 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
839                                  opl3->drum_reg);
840                 break;
841         case MIDI_CTL_PITCHBEND:
842                 snd_opl3_pitch_ctrl(opl3, chan);
843                 break;
844         }
845 }
846
847 /*
848  * NRPN events
849  */
850 void snd_opl3_nrpn(void *p, struct snd_midi_channel *chan,
851                    struct snd_midi_channel_set *chset)
852 {
853         struct snd_opl3 *opl3;
854
855         opl3 = p;
856 #ifdef DEBUG_MIDI
857         snd_printk("NRPN, ch#: %i, inst#: %i\n",
858                    chan->number, chan->midi_program);
859 #endif
860 }
861
862 /*
863  * receive sysex
864  */
865 void snd_opl3_sysex(void *p, unsigned char *buf, int len,
866                     int parsed, struct snd_midi_channel_set *chset)
867 {
868         struct snd_opl3 *opl3;
869
870         opl3 = p;
871 #ifdef DEBUG_MIDI
872         snd_printk("SYSEX\n");
873 #endif
874 }