GNU Linux-libre 4.19.264-gnu1
[releases.git] / 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 static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
29                                      struct snd_midi_channel *chan);
30 /*
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>)
38  */
39
40 static char opl3_volume_table[128] =
41 {
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
58 };
59
60 void snd_opl3_calc_volume(unsigned char *volbyte, int vel,
61                           struct snd_midi_channel *chan)
62 {
63         int oldvol, newvol, n;
64         int volume;
65
66         volume = (vel * chan->gm_volume * chan->gm_expression) / (127*127);
67         if (volume > 127)
68                 volume = 127;
69
70         oldvol = OPL3_TOTAL_LEVEL_MASK - (*volbyte & OPL3_TOTAL_LEVEL_MASK);
71
72         newvol = opl3_volume_table[volume] + oldvol;
73         if (newvol > OPL3_TOTAL_LEVEL_MASK)
74                 newvol = OPL3_TOTAL_LEVEL_MASK;
75         else if (newvol < 0)
76                 newvol = 0;
77
78         n = OPL3_TOTAL_LEVEL_MASK - (newvol & OPL3_TOTAL_LEVEL_MASK);
79
80         *volbyte = (*volbyte & OPL3_KSL_MASK) | (n & OPL3_TOTAL_LEVEL_MASK);
81 }
82
83 /*
84  * Converts the note frequency to block and fnum values for the FM chip
85  */
86 static short opl3_note_table[16] =
87 {
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 */
91 };
92
93 static void snd_opl3_calc_pitch(unsigned char *fnum, unsigned char *blocknum,
94                                 int note, struct snd_midi_channel *chan)
95 {
96         int block = ((note / 12) & 0x07) - 1;
97         int idx = (note % 12) + 2;
98         int freq;
99
100         if (chan->midi_pitchbend) {
101                 int pitchbend = chan->midi_pitchbend;
102                 int segment;
103
104                 if (pitchbend < -0x2000)
105                         pitchbend = -0x2000;
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(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");
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(KERN_DEBUG "%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(struct timer_list *t)
238 {
239
240         struct snd_opl3 *opl3 = from_timer(opl3, t, tlist);
241         unsigned long flags;
242         int again = 0;
243         int i;
244
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,
251                                                          vp->chan);
252                         else
253                                 again++;
254                 }
255         }
256         spin_unlock_irqrestore(&opl3->voice_lock, flags);
257
258         spin_lock_irqsave(&opl3->sys_timer_lock, flags);
259         if (again)
260                 mod_timer(&opl3->tlist, jiffies + 1);   /* invoke again */
261         else
262                 opl3->sys_timer_status = 0;
263         spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
264 }
265
266 /*
267  * Start system timer
268  */
269 static void snd_opl3_start_timer(struct snd_opl3 *opl3)
270 {
271         unsigned long flags;
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;
276         }
277         spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
278 }
279
280 /* ------------------------------ */
281
282
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
285 };
286
287 /*
288  * Start a note.
289  */
290 void snd_opl3_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
291 {
292         struct snd_opl3 *opl3;
293         int instr_4op;
294
295         int voice;
296         struct snd_opl3_voice *vp, *vp2;
297         unsigned short connect_mask;
298         unsigned char connection;
299         unsigned char vol_op[4];
300
301         int extra_prg = 0;
302
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;
309
310         int key = note;
311         unsigned char fnum, blocknum;
312         int i;
313
314         struct fm_patch *patch;
315         struct fm_instrument *fm;
316         unsigned long flags;
317
318         opl3 = p;
319
320 #ifdef DEBUG_MIDI
321         snd_printk(KERN_DEBUG "Note on, ch %i, inst %i, note %i, vel %i\n",
322                    chan->number, chan->midi_program, note, vel);
323 #endif
324
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                         bank = 128;
331                         prg = note;
332                 } else {
333                         bank = chan->gm_bank_select;
334                         prg = chan->midi_program;
335                 }
336         } else {
337                 /* Prepare for OSS mode */
338                 if (chan->number >= MAX_OPL3_VOICES)
339                         return;
340
341                 /* OSS instruments are located in bank 127 */
342                 bank = 127;
343                 prg = chan->midi_program;
344         }
345
346         spin_lock_irqsave(&opl3->voice_lock, flags);
347
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;
352         }
353
354  __extra_prg:
355         patch = snd_opl3_find_patch(opl3, prg, bank, 0);
356         if (!patch) {
357                 spin_unlock_irqrestore(&opl3->voice_lock, flags);
358                 return;
359         }
360
361         fm = &patch->inst;
362         switch (patch->type) {
363         case FM_PATCH_OPL2:
364                 instr_4op = 0;
365                 break;
366         case FM_PATCH_OPL3:
367                 if (opl3->hardware >= OPL3_HW_OPL3) {
368                         instr_4op = 1;
369                         break;
370                 }
371                 /* fall through */
372         default:
373                 spin_unlock_irqrestore(&opl3->voice_lock, flags);
374                 return;
375         }
376 #ifdef DEBUG_MIDI
377         snd_printk(KERN_DEBUG "  --> OPL%i instrument: %s\n",
378                    instr_4op ? 3 : 2, patch->name);
379 #endif
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);
384         } else {
385                 /* remap OSS voice */
386                 voice = snd_opl3_oss_map[chan->number];         
387         }
388
389         if (voice < 0) {
390                 spin_unlock_irqrestore(&opl3->voice_lock, flags);
391                 return;
392         }
393
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;
399         } else {
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;
404         }
405
406         /* kill voice on channel */
407         vp = &opl3->voices[voice];
408         if (vp->state > 0) {
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);
412         }
413         if (instr_4op) {
414                 vp2 = &opl3->voices[voice + 3];
415                 if (vp2->state > 0) {
416                         opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
417                                                voice_offset + 3);
418                         reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
419                         opl3->command(opl3, opl3_reg, reg_val);
420                 }
421         }
422
423         /* set connection register */
424         if (instr_4op) {
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);
430                 }
431         } else {
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);
437                 }
438         }
439
440 #ifdef DEBUG_MIDI
441         snd_printk(KERN_DEBUG "  --> setting OPL3 connection: 0x%x\n",
442                    opl3->connection_reg);
443 #endif
444         /*
445          * calculate volume depending on connection
446          * between FM operators (see include/opl3.h)
447          */
448         for (i = 0; i < (instr_4op ? 4 : 2); i++)
449                 vol_op[i] = fm->op[i].ksl_level;
450
451         connection = fm->feedback_connection[0] & 0x01;
452         if (instr_4op) {
453                 connection <<= 1;
454                 connection |= fm->feedback_connection[1] & 0x01;
455
456                 snd_opl3_calc_volume(&vol_op[3], vel, chan);
457                 switch (connection) {
458                 case 0x03:
459                         snd_opl3_calc_volume(&vol_op[2], vel, chan);
460                         /* fallthru */
461                 case 0x02:
462                         snd_opl3_calc_volume(&vol_op[0], vel, chan);
463                         break;
464                 case 0x01:
465                         snd_opl3_calc_volume(&vol_op[1], vel, chan);
466                 }
467         } else {
468                 snd_opl3_calc_volume(&vol_op[1], vel, chan);
469                 if (connection)
470                         snd_opl3_calc_volume(&vol_op[0], vel, chan);
471         }
472
473         /* Program the FM voice characteristics */
474         for (i = 0; i < (instr_4op ? 4 : 2); i++) {
475 #ifdef DEBUG_MIDI
476                 snd_printk(KERN_DEBUG "  --> programming operator %i\n", i);
477 #endif
478                 op_offset = snd_opl3_regmap[voice_offset][i];
479
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);
484
485                 /* Set OPL3 KSL_LEVEL register of requested voice/operator */ 
486                 reg_val = vol_op[i];
487                 opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
488                 opl3->command(opl3, opl3_reg, reg_val);
489
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);
494
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);
499
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);
504         }
505
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);
516
517         if (instr_4op) {
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 +
527                                        voice_offset + 3);
528                 opl3->command(opl3, opl3_reg, reg_val);
529         }
530
531         /*
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.
535          */
536         if (fm->fix_key)
537                 note = fm->fix_key;
538         /*
539          * use transpose if defined in patch library
540          */
541         if (fm->trnsps)
542                 note += (fm->trnsps - 64);
543
544         snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
545
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);
549
550         opl3->voices[voice].keyon_reg = blocknum;
551
552         /* Set output sound flag */
553         blocknum |= OPL3_KEYON_BIT;
554
555 #ifdef DEBUG_MIDI
556         snd_printk(KERN_DEBUG "  --> trigger voice %i\n", voice);
557 #endif
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);
561
562         /* kill note after fixed duration (in centiseconds) */
563         if (fm->fix_dur) {
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;
568         } else
569                 opl3->voices[voice].note_off_check = 0;
570
571         /* get extra pgm, but avoid possible loops */
572         extra_prg = (extra_prg) ? 0 : fm->modes;
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                         bank = 128;
605                         /* percussions start at 35 */
606                         prg = extra_prg - 128 + 35 - 1;
607                 } else {
608                         bank = 0;
609                         prg = extra_prg - 1;
610                 }
611 #ifdef DEBUG_MIDI
612                 snd_printk(KERN_DEBUG " *** 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         if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
628                 return;
629
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;
635         } else {
636                 /* Right register block for voices 9 .. 17 */
637                 reg_side = OPL3_RIGHT;
638                 voice_offset = voice - MAX_OPL2_VOICES;
639         }
640
641         /* kill voice */
642 #ifdef DEBUG_MIDI
643         snd_printk(KERN_DEBUG "  --> kill voice %i\n", voice);
644 #endif
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);
648
649         /* do the bookkeeping */
650         vp->time = opl3->use_time++;
651
652         if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
653                 vp2 = &opl3->voices[voice + 3];
654
655                 vp2->time = opl3->use_time++;
656                 vp2->state = SNDRV_OPL3_ST_OFF;
657         }
658         vp->state = SNDRV_OPL3_ST_OFF;
659 #ifdef DEBUG_ALLOC
660         debug_alloc(opl3, "note off", voice);
661 #endif
662
663 }
664
665 /*
666  * Release a note in response to a midi note off.
667  */
668 static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
669                                      struct snd_midi_channel *chan)
670 {
671         struct snd_opl3 *opl3;
672
673         int voice;
674         struct snd_opl3_voice *vp;
675
676         opl3 = p;
677
678 #ifdef DEBUG_MIDI
679         snd_printk(KERN_DEBUG "Note off, ch %i, inst %i, note %i\n",
680                    chan->number, chan->midi_program, note);
681 #endif
682
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);
686                         return;
687                 }
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);
694                         }
695                 }
696         } else {
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);
701                 }
702         }
703 }
704
705 void snd_opl3_note_off(void *p, int note, int vel,
706                        struct snd_midi_channel *chan)
707 {
708         struct snd_opl3 *opl3 = p;
709         unsigned long flags;
710
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);
714 }
715
716 /*
717  * key pressure change
718  */
719 void snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
720 {
721 #ifdef DEBUG_MIDI
722         snd_printk(KERN_DEBUG "Key pressure, ch#: %i, inst#: %i\n",
723                    chan->number, chan->midi_program);
724 #endif
725 }
726
727 /*
728  * terminate note
729  */
730 void snd_opl3_terminate_note(void *p, int note, struct snd_midi_channel *chan)
731 {
732 #ifdef DEBUG_MIDI
733         snd_printk(KERN_DEBUG "Terminate note, ch#: %i, inst#: %i\n",
734                    chan->number, chan->midi_program);
735 #endif
736 }
737
738 static void snd_opl3_update_pitch(struct snd_opl3 *opl3, int voice)
739 {
740         unsigned short reg_side;
741         unsigned char voice_offset;
742         unsigned short opl3_reg;
743
744         unsigned char fnum, blocknum;
745
746         struct snd_opl3_voice *vp;
747
748         if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
749                 return;
750
751         vp = &opl3->voices[voice];
752         if (vp->chan == NULL)
753                 return; /* not allocated? */
754
755         if (voice < MAX_OPL2_VOICES) {
756                 /* Left register block for voices 0 .. 8 */
757                 reg_side = OPL3_LEFT;
758                 voice_offset = voice;
759         } else {
760                 /* Right register block for voices 9 .. 17 */
761                 reg_side = OPL3_RIGHT;
762                 voice_offset = voice - MAX_OPL2_VOICES;
763         }
764
765         snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
766
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);
770
771         vp->keyon_reg = blocknum;
772
773         /* Set output sound flag */
774         blocknum |= OPL3_KEYON_BIT;
775
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);
779
780         vp->time = opl3->use_time++;
781 }
782
783 /*
784  * Update voice pitch controller
785  */
786 static void snd_opl3_pitch_ctrl(struct snd_opl3 *opl3, struct snd_midi_channel *chan)
787 {
788         int voice;
789         struct snd_opl3_voice *vp;
790
791         unsigned long flags;
792
793         spin_lock_irqsave(&opl3->voice_lock, flags);
794
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);
800                         }
801                 }
802         } else {
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);
807                 }
808         }
809         spin_unlock_irqrestore(&opl3->voice_lock, flags);
810 }
811
812 /*
813  * Deal with a controller type event.  This includes all types of
814  * control events, not just the midi controllers
815  */
816 void snd_opl3_control(void *p, int type, struct snd_midi_channel *chan)
817 {
818         struct snd_opl3 *opl3;
819
820         opl3 = p;
821 #ifdef DEBUG_MIDI
822         snd_printk(KERN_DEBUG "Controller, TYPE = %i, ch#: %i, inst#: %i\n",
823                    type, chan->number, chan->midi_program);
824 #endif
825
826         switch (type) {
827         case MIDI_CTL_MSB_MODWHEEL:
828                 if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
829                         opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
830                 else 
831                         opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
832                 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
833                                  opl3->drum_reg);
834                 break;
835         case MIDI_CTL_E2_TREMOLO_DEPTH:
836                 if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
837                         opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
838                 else 
839                         opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
840                 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
841                                  opl3->drum_reg);
842                 break;
843         case MIDI_CTL_PITCHBEND:
844                 snd_opl3_pitch_ctrl(opl3, chan);
845                 break;
846         }
847 }
848
849 /*
850  * NRPN events
851  */
852 void snd_opl3_nrpn(void *p, struct snd_midi_channel *chan,
853                    struct snd_midi_channel_set *chset)
854 {
855 #ifdef DEBUG_MIDI
856         snd_printk(KERN_DEBUG "NRPN, ch#: %i, inst#: %i\n",
857                    chan->number, chan->midi_program);
858 #endif
859 }
860
861 /*
862  * receive sysex
863  */
864 void snd_opl3_sysex(void *p, unsigned char *buf, int len,
865                     int parsed, struct snd_midi_channel_set *chset)
866 {
867 #ifdef DEBUG_MIDI
868         snd_printk(KERN_DEBUG "SYSEX\n");
869 #endif
870 }