source: GPL/trunk/alsa-kernel/drivers/opl3/opl3_midi.c

Last change on this file was 777, checked in by David Azarewicz, 7 months ago

Merge from uniaud32-exp branch

File size: 22.2 KB
Line 
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
4 *
5 * Midi synth routines for OPL2/OPL3/OPL4 FM
6 */
7
8#undef DEBUG_ALLOC
9#undef DEBUG_MIDI
10
11#include "opl3_voice.h"
12#include <sound/asoundef.h>
13
14#ifdef DEBUG_MIDI
15#define opl3_dbg(opl3, fmt, ...) \
16 dev_dbg(((struct snd_opl3 *)(opl3))->card->dev, fmt, ##__VA_ARGS__)
17#else
18#define opl3_dbg(opl3, fmt, ...) do {} while (0)
19#endif
20
21static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
22 struct snd_midi_channel *chan);
23/*
24 * The next table looks magical, but it certainly is not. Its values have
25 * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
26 * for i=0. This log-table converts a linear volume-scaling (0..127) to a
27 * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
28 * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
29 * volume -8 it was implemented as a table because it is only 128 bytes and
30 * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>)
31 */
32
33static const char opl3_volume_table[128] =
34{
35 -63, -48, -40, -35, -32, -29, -27, -26,
36 -24, -23, -21, -20, -19, -18, -18, -17,
37 -16, -15, -15, -14, -13, -13, -12, -12,
38 -11, -11, -10, -10, -10, -9, -9, -8,
39 -8, -8, -7, -7, -7, -6, -6, -6,
40 -5, -5, -5, -5, -4, -4, -4, -4,
41 -3, -3, -3, -3, -2, -2, -2, -2,
42 -2, -1, -1, -1, -1, 0, 0, 0,
43 0, 0, 0, 1, 1, 1, 1, 1,
44 1, 2, 2, 2, 2, 2, 2, 2,
45 3, 3, 3, 3, 3, 3, 3, 4,
46 4, 4, 4, 4, 4, 4, 4, 5,
47 5, 5, 5, 5, 5, 5, 5, 5,
48 6, 6, 6, 6, 6, 6, 6, 6,
49 6, 7, 7, 7, 7, 7, 7, 7,
50 7, 7, 7, 8, 8, 8, 8, 8
51};
52
53void snd_opl3_calc_volume(unsigned char *volbyte, int vel,
54 struct snd_midi_channel *chan)
55{
56 int oldvol, newvol, n;
57 int volume;
58
59 volume = (vel * chan->gm_volume * chan->gm_expression) / (127*127);
60 if (volume > 127)
61 volume = 127;
62
63 oldvol = OPL3_TOTAL_LEVEL_MASK - (*volbyte & OPL3_TOTAL_LEVEL_MASK);
64
65 newvol = opl3_volume_table[volume] + oldvol;
66 if (newvol > OPL3_TOTAL_LEVEL_MASK)
67 newvol = OPL3_TOTAL_LEVEL_MASK;
68 else if (newvol < 0)
69 newvol = 0;
70
71 n = OPL3_TOTAL_LEVEL_MASK - (newvol & OPL3_TOTAL_LEVEL_MASK);
72
73 *volbyte = (*volbyte & OPL3_KSL_MASK) | (n & OPL3_TOTAL_LEVEL_MASK);
74}
75
76/*
77 * Converts the note frequency to block and fnum values for the FM chip
78 */
79static const short opl3_note_table[16] =
80{
81 305, 323, /* for pitch bending, -2 semitones */
82 343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647,
83 686, 726 /* for pitch bending, +2 semitones */
84};
85
86static void snd_opl3_calc_pitch(unsigned char *fnum, unsigned char *blocknum,
87 int note, struct snd_midi_channel *chan)
88{
89 int block = ((note / 12) & 0x07) - 1;
90 int idx = (note % 12) + 2;
91 int freq;
92
93 if (chan->midi_pitchbend) {
94 int pitchbend = chan->midi_pitchbend;
95 int segment;
96
97 if (pitchbend < -0x2000)
98 pitchbend = -0x2000;
99 if (pitchbend > 0x1FFF)
100 pitchbend = 0x1FFF;
101
102 segment = pitchbend / 0x1000;
103 freq = opl3_note_table[idx+segment];
104 freq += ((opl3_note_table[idx+segment+1] - freq) *
105 (pitchbend % 0x1000)) / 0x1000;
106 } else {
107 freq = opl3_note_table[idx];
108 }
109
110 *fnum = (unsigned char) freq;
111 *blocknum = ((freq >> 8) & OPL3_FNUM_HIGH_MASK) |
112 ((block << 2) & OPL3_BLOCKNUM_MASK);
113}
114
115
116#ifdef DEBUG_ALLOC
117static void debug_alloc(struct snd_opl3 *opl3, char *s, int voice)
118{
119 int i;
120 const char *str = "x.24";
121 char buf[MAX_OPL3_VOICES + 1];
122
123 for (i = 0; i < opl3->max_voices; i++)
124 buf[i] = str[opl3->voices[i].state + 1];
125 buf[i] = 0;
126 dev_dbg(opl3->card->dev, "time %.5i: %s [%.2i]: %s\n",
127 opl3->use_time, s, voice, buf);
128}
129#endif
130
131/*
132 * Get a FM voice (channel) to play a note on.
133 */
134static int opl3_get_voice(struct snd_opl3 *opl3, int instr_4op,
135 struct snd_midi_channel *chan) {
136 int chan_4op_1; /* first voice for 4op instrument */
137 int chan_4op_2; /* second voice for 4op instrument */
138
139 struct snd_opl3_voice *vp, *vp2;
140 unsigned int voice_time;
141 int i;
142
143#ifdef DEBUG_ALLOC
144 char *alloc_type[3] = { "FREE ", "CHEAP ", "EXPENSIVE" };
145#endif
146
147 /* This is our "allocation cost" table */
148 enum {
149 FREE = 0, CHEAP, EXPENSIVE, END
150 };
151
152 /* Keeps track of what we are finding */
153 struct best {
154 unsigned int time;
155 int voice;
156 } best[END];
157 struct best *bp;
158
159 for (i = 0; i < END; i++) {
160 best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */
161 best[i].voice = -1;
162 }
163
164 /* Look through all the channels for the most suitable. */
165 for (i = 0; i < opl3->max_voices; i++) {
166 vp = &opl3->voices[i];
167
168 if (vp->state == SNDRV_OPL3_ST_NOT_AVAIL)
169 /* skip unavailable channels, allocated by
170 drum voices or by bounded 4op voices) */
171 continue;
172
173 voice_time = vp->time;
174 bp = best;
175
176 chan_4op_1 = ((i < 3) || (i > 8 && i < 12));
177 chan_4op_2 = ((i > 2 && i < 6) || (i > 11 && i < 15));
178 if (instr_4op) {
179 /* allocate 4op voice */
180 /* skip channels unavailable to 4op instrument */
181 if (!chan_4op_1)
182 continue;
183
184 if (vp->state)
185 /* kill one voice, CHEAP */
186 bp++;
187 /* get state of bounded 2op channel
188 to be allocated for 4op instrument */
189 vp2 = &opl3->voices[i + 3];
190 if (vp2->state == SNDRV_OPL3_ST_ON_2OP) {
191 /* kill two voices, EXPENSIVE */
192 bp++;
193 voice_time = max(voice_time, vp2->time);
194 }
195 } else {
196 /* allocate 2op voice */
197 if ((chan_4op_1) || (chan_4op_2))
198 /* use bounded channels for 2op, CHEAP */
199 bp++;
200 else if (vp->state)
201 /* kill one voice on 2op channel, CHEAP */
202 bp++;
203 /* raise kill cost to EXPENSIVE for all channels */
204 if (vp->state)
205 bp++;
206 }
207 if (voice_time < bp->time) {
208 bp->time = voice_time;
209 bp->voice = i;
210 }
211 }
212
213 for (i = 0; i < END; i++) {
214 if (best[i].voice >= 0) {
215#ifdef DEBUG_ALLOC
216 dev_dbg(opl3->card->dev,
217 "%s %iop allocation on voice %i\n",
218 alloc_type[i], instr_4op ? 4 : 2,
219 best[i].voice);
220#endif
221 return best[i].voice;
222 }
223 }
224 /* not found */
225 return -1;
226}
227
228/* ------------------------------ */
229
230#define from_timer(var, callback_timer, timer_fieldname) \
231 container_of(callback_timer, struct snd_opl3, timer_fieldname)
232
233/*
234 * System timer interrupt function
235 */
236void snd_opl3_timer_func(struct timer_list *t)
237{
238
239 struct snd_opl3 *opl3 = from_timer(opl3, t, tlist);
240 unsigned long flags;
241 int again = 0;
242 int i;
243
244 spin_lock_irqsave(&opl3->voice_lock, flags);
245 for (i = 0; i < opl3->max_voices; i++) {
246 struct snd_opl3_voice *vp = &opl3->voices[i];
247 if (vp->state > 0 && vp->note_off_check) {
248 if (vp->note_off == jiffies)
249 snd_opl3_note_off_unsafe(opl3, vp->note, 0,
250 vp->chan);
251 else
252 again++;
253 }
254 }
255 spin_unlock_irqrestore(&opl3->voice_lock, flags);
256
257 spin_lock_irqsave(&opl3->sys_timer_lock, flags);
258 if (again)
259 mod_timer(&opl3->tlist, jiffies + 1); /* invoke again */
260 else
261 opl3->sys_timer_status = 0;
262 spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
263}
264
265/*
266 * Start system timer
267 */
268static void snd_opl3_start_timer(struct snd_opl3 *opl3)
269{
270 unsigned long flags;
271 spin_lock_irqsave(&opl3->sys_timer_lock, flags);
272 if (! opl3->sys_timer_status) {
273 mod_timer(&opl3->tlist, jiffies + 1);
274 opl3->sys_timer_status = 1;
275 }
276 spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
277}
278
279/* ------------------------------ */
280
281
282static const 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 */
289void snd_opl3_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
290{
291 struct snd_opl3 *opl3;
292 int instr_4op;
293
294 int voice;
295 struct snd_opl3_voice *vp, *vp2;
296 unsigned short connect_mask;
297 unsigned char connection;
298 unsigned char vol_op[4];
299
300 int extra_prg = 0;
301
302 unsigned short reg_side;
303 unsigned char op_offset;
304 unsigned char voice_offset;
305 unsigned short opl3_reg;
306 unsigned char reg_val;
307 unsigned char prg, bank;
308
309 int key = note;
310 unsigned char fnum, blocknum;
311 int i;
312
313 struct fm_patch *patch;
314 struct fm_instrument *fm;
315 unsigned long flags;
316
317 opl3 = p;
318
319 opl3_dbg(opl3, "Note on, ch %i, inst %i, note %i, vel %i\n",
320 chan->number, chan->midi_program, note, vel);
321
322 /* in SYNTH mode, application takes care of voices */
323 /* in SEQ mode, drum voice numbers are notes on drum channel */
324 if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
325 if (chan->drum_channel) {
326 /* percussion instruments are located in bank 128 */
327 bank = 128;
328 prg = note;
329 } else {
330 bank = chan->gm_bank_select;
331 prg = chan->midi_program;
332 }
333 } else {
334 /* Prepare for OSS mode */
335 if (chan->number >= MAX_OPL3_VOICES)
336 return;
337
338 /* OSS instruments are located in bank 127 */
339 bank = 127;
340 prg = chan->midi_program;
341 }
342
343 spin_lock_irqsave(&opl3->voice_lock, flags);
344
345 if (use_internal_drums) {
346 snd_opl3_drum_switch(opl3, note, vel, 1, chan);
347 spin_unlock_irqrestore(&opl3->voice_lock, flags);
348 return;
349 }
350
351 __extra_prg:
352 patch = snd_opl3_find_patch(opl3, prg, bank, 0);
353 if (!patch) {
354 spin_unlock_irqrestore(&opl3->voice_lock, flags);
355 return;
356 }
357
358 fm = &patch->inst;
359 switch (patch->type) {
360 case FM_PATCH_OPL2:
361 instr_4op = 0;
362 break;
363 case FM_PATCH_OPL3:
364 if (opl3->hardware >= OPL3_HW_OPL3) {
365 instr_4op = 1;
366 break;
367 }
368 fallthrough;
369 default:
370 spin_unlock_irqrestore(&opl3->voice_lock, flags);
371 return;
372 }
373 opl3_dbg(opl3, " --> OPL%i instrument: %s\n",
374 instr_4op ? 3 : 2, patch->name);
375 /* in SYNTH mode, application takes care of voices */
376 /* in SEQ mode, allocate voice on free OPL3 channel */
377 if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
378 voice = opl3_get_voice(opl3, instr_4op, chan);
379 } else {
380 /* remap OSS voice */
381 voice = snd_opl3_oss_map[chan->number];
382 }
383
384 if (voice < 0) {
385 spin_unlock_irqrestore(&opl3->voice_lock, flags);
386 return;
387 }
388
389 if (voice < MAX_OPL2_VOICES) {
390 /* Left register block for voices 0 .. 8 */
391 reg_side = OPL3_LEFT;
392 voice_offset = voice;
393 connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07;
394 } else {
395 /* Right register block for voices 9 .. 17 */
396 reg_side = OPL3_RIGHT;
397 voice_offset = voice - MAX_OPL2_VOICES;
398 connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38;
399 }
400
401 /* kill voice on channel */
402 vp = &opl3->voices[voice];
403 if (vp->state > 0) {
404 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
405 reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
406 opl3->command(opl3, opl3_reg, reg_val);
407 }
408 if (instr_4op) {
409 vp2 = &opl3->voices[voice + 3];
410 if (vp2->state > 0) {
411 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
412 voice_offset + 3);
413 reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
414 opl3->command(opl3, opl3_reg, reg_val);
415 }
416 }
417
418 /* set connection register */
419 if (instr_4op) {
420 if ((opl3->connection_reg ^ connect_mask) & connect_mask) {
421 opl3->connection_reg |= connect_mask;
422 /* set connection bit */
423 opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
424 opl3->command(opl3, opl3_reg, opl3->connection_reg);
425 }
426 } else {
427 if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) {
428 opl3->connection_reg &= ~connect_mask;
429 /* clear connection bit */
430 opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
431 opl3->command(opl3, opl3_reg, opl3->connection_reg);
432 }
433 }
434
435 opl3_dbg(opl3, " --> setting OPL3 connection: 0x%x\n",
436 opl3->connection_reg);
437 /*
438 * calculate volume depending on connection
439 * between FM operators (see include/opl3.h)
440 */
441 for (i = 0; i < (instr_4op ? 4 : 2); i++)
442 vol_op[i] = fm->op[i].ksl_level;
443
444 connection = fm->feedback_connection[0] & 0x01;
445 if (instr_4op) {
446 connection <<= 1;
447 connection |= fm->feedback_connection[1] & 0x01;
448
449 snd_opl3_calc_volume(&vol_op[3], vel, chan);
450 switch (connection) {
451 case 0x03:
452 snd_opl3_calc_volume(&vol_op[2], vel, chan);
453 fallthrough;
454 case 0x02:
455 snd_opl3_calc_volume(&vol_op[0], vel, chan);
456 break;
457 case 0x01:
458 snd_opl3_calc_volume(&vol_op[1], vel, chan);
459 }
460 } else {
461 snd_opl3_calc_volume(&vol_op[1], vel, chan);
462 if (connection)
463 snd_opl3_calc_volume(&vol_op[0], vel, chan);
464 }
465
466 /* Program the FM voice characteristics */
467 for (i = 0; i < (instr_4op ? 4 : 2); i++) {
468 opl3_dbg(opl3, " --> programming operator %i\n", i);
469 op_offset = snd_opl3_regmap[voice_offset][i];
470
471 /* Set OPL3 AM_VIB register of requested voice/operator */
472 reg_val = fm->op[i].am_vib;
473 opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
474 opl3->command(opl3, opl3_reg, reg_val);
475
476 /* Set OPL3 KSL_LEVEL register of requested voice/operator */
477 reg_val = vol_op[i];
478 opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
479 opl3->command(opl3, opl3_reg, reg_val);
480
481 /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
482 reg_val = fm->op[i].attack_decay;
483 opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
484 opl3->command(opl3, opl3_reg, reg_val);
485
486 /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
487 reg_val = fm->op[i].sustain_release;
488 opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
489 opl3->command(opl3, opl3_reg, reg_val);
490
491 /* Select waveform */
492 reg_val = fm->op[i].wave_select;
493 opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
494 opl3->command(opl3, opl3_reg, reg_val);
495 }
496
497 /* Set operator feedback and 2op inter-operator connection */
498 reg_val = fm->feedback_connection[0];
499 /* Set output voice connection */
500 reg_val |= OPL3_STEREO_BITS;
501 if (chan->gm_pan < 43)
502 reg_val &= ~OPL3_VOICE_TO_RIGHT;
503 if (chan->gm_pan > 85)
504 reg_val &= ~OPL3_VOICE_TO_LEFT;
505 opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
506 opl3->command(opl3, opl3_reg, reg_val);
507
508 if (instr_4op) {
509 /* Set 4op inter-operator connection */
510 reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT;
511 /* Set output voice connection */
512 reg_val |= OPL3_STEREO_BITS;
513 if (chan->gm_pan < 43)
514 reg_val &= ~OPL3_VOICE_TO_RIGHT;
515 if (chan->gm_pan > 85)
516 reg_val &= ~OPL3_VOICE_TO_LEFT;
517 opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION +
518 voice_offset + 3);
519 opl3->command(opl3, opl3_reg, reg_val);
520 }
521
522 /*
523 * Special treatment of percussion notes for fm:
524 * Requested pitch is really program, and pitch for
525 * device is whatever was specified in the patch library.
526 */
527 if (fm->fix_key)
528 note = fm->fix_key;
529 /*
530 * use transpose if defined in patch library
531 */
532 if (fm->trnsps)
533 note += (fm->trnsps - 64);
534
535 snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
536
537 /* Set OPL3 FNUM_LOW register of requested voice */
538 opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
539 opl3->command(opl3, opl3_reg, fnum);
540
541 opl3->voices[voice].keyon_reg = blocknum;
542
543 /* Set output sound flag */
544 blocknum |= OPL3_KEYON_BIT;
545
546 opl3_dbg(opl3, " --> trigger voice %i\n", voice);
547 /* Set OPL3 KEYON_BLOCK register of requested voice */
548 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
549 opl3->command(opl3, opl3_reg, blocknum);
550
551 /* kill note after fixed duration (in centiseconds) */
552 if (fm->fix_dur) {
553 opl3->voices[voice].note_off = jiffies +
554 (fm->fix_dur * HZ) / 100;
555 snd_opl3_start_timer(opl3);
556 opl3->voices[voice].note_off_check = 1;
557 } else
558 opl3->voices[voice].note_off_check = 0;
559
560 /* get extra pgm, but avoid possible loops */
561 extra_prg = (extra_prg) ? 0 : fm->modes;
562
563 /* do the bookkeeping */
564 vp->time = opl3->use_time++;
565 vp->note = key;
566 vp->chan = chan;
567
568 if (instr_4op) {
569 vp->state = SNDRV_OPL3_ST_ON_4OP;
570
571 vp2 = &opl3->voices[voice + 3];
572 vp2->time = opl3->use_time++;
573 vp2->note = key;
574 vp2->chan = chan;
575 vp2->state = SNDRV_OPL3_ST_NOT_AVAIL;
576 } else {
577 if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
578 /* 4op killed by 2op, release bounded voice */
579 vp2 = &opl3->voices[voice + 3];
580 vp2->time = opl3->use_time++;
581 vp2->state = SNDRV_OPL3_ST_OFF;
582 }
583 vp->state = SNDRV_OPL3_ST_ON_2OP;
584 }
585
586#ifdef DEBUG_ALLOC
587 debug_alloc(opl3, "note on ", voice);
588#endif
589
590 /* allocate extra program if specified in patch library */
591 if (extra_prg) {
592 if (extra_prg > 128) {
593 bank = 128;
594 /* percussions start at 35 */
595 prg = extra_prg - 128 + 35 - 1;
596 } else {
597 bank = 0;
598 prg = extra_prg - 1;
599 }
600 opl3_dbg(opl3, " *** allocating extra program\n");
601 goto __extra_prg;
602 }
603 spin_unlock_irqrestore(&opl3->voice_lock, flags);
604}
605
606static void snd_opl3_kill_voice(struct snd_opl3 *opl3, int voice)
607{
608 unsigned short reg_side;
609 unsigned char voice_offset;
610 unsigned short opl3_reg;
611
612 struct snd_opl3_voice *vp, *vp2;
613
614 if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
615 return;
616
617 vp = &opl3->voices[voice];
618 if (voice < MAX_OPL2_VOICES) {
619 /* Left register block for voices 0 .. 8 */
620 reg_side = OPL3_LEFT;
621 voice_offset = voice;
622 } else {
623 /* Right register block for voices 9 .. 17 */
624 reg_side = OPL3_RIGHT;
625 voice_offset = voice - MAX_OPL2_VOICES;
626 }
627
628 /* kill voice */
629 opl3_dbg(opl3, " --> kill voice %i\n", voice);
630 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
631 /* clear Key ON bit */
632 opl3->command(opl3, opl3_reg, vp->keyon_reg);
633
634 /* do the bookkeeping */
635 vp->time = opl3->use_time++;
636
637 if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
638 vp2 = &opl3->voices[voice + 3];
639
640 vp2->time = opl3->use_time++;
641 vp2->state = SNDRV_OPL3_ST_OFF;
642 }
643 vp->state = SNDRV_OPL3_ST_OFF;
644#ifdef DEBUG_ALLOC
645 debug_alloc(opl3, "note off", voice);
646#endif
647
648}
649
650/*
651 * Release a note in response to a midi note off.
652 */
653static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
654 struct snd_midi_channel *chan)
655{
656 struct snd_opl3 *opl3;
657
658 int voice;
659 struct snd_opl3_voice *vp;
660
661 opl3 = p;
662
663 opl3_dbg(opl3, "Note off, ch %i, inst %i, note %i\n",
664 chan->number, chan->midi_program, note);
665
666 if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
667 if (chan->drum_channel && use_internal_drums) {
668 snd_opl3_drum_switch(opl3, note, vel, 0, chan);
669 return;
670 }
671 /* this loop will hopefully kill all extra voices, because
672 they are grouped by the same channel and note values */
673 for (voice = 0; voice < opl3->max_voices; voice++) {
674 vp = &opl3->voices[voice];
675 if (vp->state > 0 && vp->chan == chan && vp->note == note) {
676 snd_opl3_kill_voice(opl3, voice);
677 }
678 }
679 } else {
680 /* remap OSS voices */
681 if (chan->number < MAX_OPL3_VOICES) {
682 voice = snd_opl3_oss_map[chan->number];
683 snd_opl3_kill_voice(opl3, voice);
684 }
685 }
686}
687
688void snd_opl3_note_off(void *p, int note, int vel,
689 struct snd_midi_channel *chan)
690{
691 struct snd_opl3 *opl3 = p;
692 unsigned long flags;
693
694 spin_lock_irqsave(&opl3->voice_lock, flags);
695 snd_opl3_note_off_unsafe(p, note, vel, chan);
696 spin_unlock_irqrestore(&opl3->voice_lock, flags);
697}
698
699/*
700 * key pressure change
701 */
702void snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
703{
704 opl3_dbg(p, "Key pressure, ch#: %i, inst#: %i\n",
705 chan->number, chan->midi_program);
706}
707
708/*
709 * terminate note
710 */
711void snd_opl3_terminate_note(void *p, int note, struct snd_midi_channel *chan)
712{
713 opl3_dbg(p, "Terminate note, ch#: %i, inst#: %i\n",
714 chan->number, chan->midi_program);
715}
716
717static void snd_opl3_update_pitch(struct snd_opl3 *opl3, int voice)
718{
719 unsigned short reg_side;
720 unsigned char voice_offset;
721 unsigned short opl3_reg;
722
723 unsigned char fnum, blocknum;
724
725 struct snd_opl3_voice *vp;
726
727 if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
728 return;
729
730 vp = &opl3->voices[voice];
731 if (vp->chan == NULL)
732 return; /* not allocated? */
733
734 if (voice < MAX_OPL2_VOICES) {
735 /* Left register block for voices 0 .. 8 */
736 reg_side = OPL3_LEFT;
737 voice_offset = voice;
738 } else {
739 /* Right register block for voices 9 .. 17 */
740 reg_side = OPL3_RIGHT;
741 voice_offset = voice - MAX_OPL2_VOICES;
742 }
743
744 snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
745
746 /* Set OPL3 FNUM_LOW register of requested voice */
747 opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
748 opl3->command(opl3, opl3_reg, fnum);
749
750 vp->keyon_reg = blocknum;
751
752 /* Set output sound flag */
753 blocknum |= OPL3_KEYON_BIT;
754
755 /* Set OPL3 KEYON_BLOCK register of requested voice */
756 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
757 opl3->command(opl3, opl3_reg, blocknum);
758
759 vp->time = opl3->use_time++;
760}
761
762/*
763 * Update voice pitch controller
764 */
765static void snd_opl3_pitch_ctrl(struct snd_opl3 *opl3, struct snd_midi_channel *chan)
766{
767 int voice;
768 struct snd_opl3_voice *vp;
769
770 unsigned long flags;
771
772 spin_lock_irqsave(&opl3->voice_lock, flags);
773
774 if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
775 for (voice = 0; voice < opl3->max_voices; voice++) {
776 vp = &opl3->voices[voice];
777 if (vp->state > 0 && vp->chan == chan) {
778 snd_opl3_update_pitch(opl3, voice);
779 }
780 }
781 } else {
782 /* remap OSS voices */
783 if (chan->number < MAX_OPL3_VOICES) {
784 voice = snd_opl3_oss_map[chan->number];
785 snd_opl3_update_pitch(opl3, voice);
786 }
787 }
788 spin_unlock_irqrestore(&opl3->voice_lock, flags);
789}
790
791/*
792 * Deal with a controller type event. This includes all types of
793 * control events, not just the midi controllers
794 */
795void snd_opl3_control(void *p, int type, struct snd_midi_channel *chan)
796{
797 struct snd_opl3 *opl3;
798
799 opl3 = p;
800 opl3_dbg(opl3, "Controller, TYPE = %i, ch#: %i, inst#: %i\n",
801 type, chan->number, chan->midi_program);
802
803 switch (type) {
804 case MIDI_CTL_MSB_MODWHEEL:
805 if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
806 opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
807 else
808 opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
809 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
810 opl3->drum_reg);
811 break;
812 case MIDI_CTL_E2_TREMOLO_DEPTH:
813 if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
814 opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
815 else
816 opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
817 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
818 opl3->drum_reg);
819 break;
820 case MIDI_CTL_PITCHBEND:
821 snd_opl3_pitch_ctrl(opl3, chan);
822 break;
823 }
824}
825
826/*
827 * NRPN events
828 */
829void snd_opl3_nrpn(void *p, struct snd_midi_channel *chan,
830 struct snd_midi_channel_set *chset)
831{
832 opl3_dbg(p, "NRPN, ch#: %i, inst#: %i\n",
833 chan->number, chan->midi_program);
834}
835
836/*
837 * receive sysex
838 */
839void snd_opl3_sysex(void *p, unsigned char *buf, int len,
840 int parsed, struct snd_midi_channel_set *chset)
841{
842 opl3_dbg(p, "SYSEX\n");
843}
Note: See TracBrowser for help on using the repository browser.