xref: /openbmc/linux/sound/drivers/opl3/opl3_midi.c (revision 3b23dc52)
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 	default:
372 		spin_unlock_irqrestore(&opl3->voice_lock, flags);
373 		return;
374 	}
375 #ifdef DEBUG_MIDI
376 	snd_printk(KERN_DEBUG "  --> OPL%i instrument: %s\n",
377 		   instr_4op ? 3 : 2, patch->name);
378 #endif
379 	/* in SYNTH mode, application takes care of voices */
380 	/* in SEQ mode, allocate voice on free OPL3 channel */
381 	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
382 		voice = opl3_get_voice(opl3, instr_4op, chan);
383 	} else {
384 		/* remap OSS voice */
385 		voice = snd_opl3_oss_map[chan->number];
386 	}
387 
388 	if (voice < 0) {
389 		spin_unlock_irqrestore(&opl3->voice_lock, flags);
390 		return;
391 	}
392 
393 	if (voice < MAX_OPL2_VOICES) {
394 		/* Left register block for voices 0 .. 8 */
395 		reg_side = OPL3_LEFT;
396 		voice_offset = voice;
397 		connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07;
398 	} else {
399 		/* Right register block for voices 9 .. 17 */
400 		reg_side = OPL3_RIGHT;
401 		voice_offset = voice - MAX_OPL2_VOICES;
402 		connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38;
403 	}
404 
405 	/* kill voice on channel */
406 	vp = &opl3->voices[voice];
407 	if (vp->state > 0) {
408 		opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
409 		reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
410 		opl3->command(opl3, opl3_reg, reg_val);
411 	}
412 	if (instr_4op) {
413 		vp2 = &opl3->voices[voice + 3];
414 		if (vp->state > 0) {
415 			opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
416 					       voice_offset + 3);
417 			reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
418 			opl3->command(opl3, opl3_reg, reg_val);
419 		}
420 	}
421 
422 	/* set connection register */
423 	if (instr_4op) {
424 		if ((opl3->connection_reg ^ connect_mask) & connect_mask) {
425 			opl3->connection_reg |= connect_mask;
426 			/* set connection bit */
427 			opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
428 			opl3->command(opl3, opl3_reg, opl3->connection_reg);
429 		}
430 	} else {
431 		if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) {
432 			opl3->connection_reg &= ~connect_mask;
433 			/* clear connection bit */
434 			opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
435 			opl3->command(opl3, opl3_reg, opl3->connection_reg);
436 		}
437 	}
438 
439 #ifdef DEBUG_MIDI
440 	snd_printk(KERN_DEBUG "  --> setting OPL3 connection: 0x%x\n",
441 		   opl3->connection_reg);
442 #endif
443 	/*
444 	 * calculate volume depending on connection
445 	 * between FM operators (see include/opl3.h)
446 	 */
447 	for (i = 0; i < (instr_4op ? 4 : 2); i++)
448 		vol_op[i] = fm->op[i].ksl_level;
449 
450 	connection = fm->feedback_connection[0] & 0x01;
451 	if (instr_4op) {
452 		connection <<= 1;
453 		connection |= fm->feedback_connection[1] & 0x01;
454 
455 		snd_opl3_calc_volume(&vol_op[3], vel, chan);
456 		switch (connection) {
457 		case 0x03:
458 			snd_opl3_calc_volume(&vol_op[2], vel, chan);
459 			/* fallthru */
460 		case 0x02:
461 			snd_opl3_calc_volume(&vol_op[0], vel, chan);
462 			break;
463 		case 0x01:
464 			snd_opl3_calc_volume(&vol_op[1], vel, chan);
465 		}
466 	} else {
467 		snd_opl3_calc_volume(&vol_op[1], vel, chan);
468 		if (connection)
469 			snd_opl3_calc_volume(&vol_op[0], vel, chan);
470 	}
471 
472 	/* Program the FM voice characteristics */
473 	for (i = 0; i < (instr_4op ? 4 : 2); i++) {
474 #ifdef DEBUG_MIDI
475 		snd_printk(KERN_DEBUG "  --> programming operator %i\n", i);
476 #endif
477 		op_offset = snd_opl3_regmap[voice_offset][i];
478 
479 		/* Set OPL3 AM_VIB register of requested voice/operator */
480 		reg_val = fm->op[i].am_vib;
481 		opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
482 		opl3->command(opl3, opl3_reg, reg_val);
483 
484 		/* Set OPL3 KSL_LEVEL register of requested voice/operator */
485 		reg_val = vol_op[i];
486 		opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
487 		opl3->command(opl3, opl3_reg, reg_val);
488 
489 		/* Set OPL3 ATTACK_DECAY register of requested voice/operator */
490 		reg_val = fm->op[i].attack_decay;
491 		opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
492 		opl3->command(opl3, opl3_reg, reg_val);
493 
494 		/* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
495 		reg_val = fm->op[i].sustain_release;
496 		opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
497 		opl3->command(opl3, opl3_reg, reg_val);
498 
499 		/* Select waveform */
500 		reg_val = fm->op[i].wave_select;
501 		opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
502 		opl3->command(opl3, opl3_reg, reg_val);
503 	}
504 
505 	/* Set operator feedback and 2op inter-operator connection */
506 	reg_val = fm->feedback_connection[0];
507 	/* Set output voice connection */
508 	reg_val |= OPL3_STEREO_BITS;
509 	if (chan->gm_pan < 43)
510 		reg_val &= ~OPL3_VOICE_TO_RIGHT;
511 	if (chan->gm_pan > 85)
512 		reg_val &= ~OPL3_VOICE_TO_LEFT;
513 	opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
514 	opl3->command(opl3, opl3_reg, reg_val);
515 
516 	if (instr_4op) {
517 		/* Set 4op inter-operator connection */
518 		reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT;
519 		/* Set output voice connection */
520 		reg_val |= OPL3_STEREO_BITS;
521 		if (chan->gm_pan < 43)
522 			reg_val &= ~OPL3_VOICE_TO_RIGHT;
523 		if (chan->gm_pan > 85)
524 			reg_val &= ~OPL3_VOICE_TO_LEFT;
525 		opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION +
526 				       voice_offset + 3);
527 		opl3->command(opl3, opl3_reg, reg_val);
528 	}
529 
530 	/*
531 	 * Special treatment of percussion notes for fm:
532 	 * Requested pitch is really program, and pitch for
533 	 * device is whatever was specified in the patch library.
534 	 */
535 	if (fm->fix_key)
536 		note = fm->fix_key;
537 	/*
538 	 * use transpose if defined in patch library
539 	 */
540 	if (fm->trnsps)
541 		note += (fm->trnsps - 64);
542 
543 	snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
544 
545 	/* Set OPL3 FNUM_LOW register of requested voice */
546 	opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
547 	opl3->command(opl3, opl3_reg, fnum);
548 
549 	opl3->voices[voice].keyon_reg = blocknum;
550 
551 	/* Set output sound flag */
552 	blocknum |= OPL3_KEYON_BIT;
553 
554 #ifdef DEBUG_MIDI
555 	snd_printk(KERN_DEBUG "  --> trigger voice %i\n", voice);
556 #endif
557 	/* Set OPL3 KEYON_BLOCK register of requested voice */
558 	opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
559 	opl3->command(opl3, opl3_reg, blocknum);
560 
561 	/* kill note after fixed duration (in centiseconds) */
562 	if (fm->fix_dur) {
563 		opl3->voices[voice].note_off = jiffies +
564 			(fm->fix_dur * HZ) / 100;
565 		snd_opl3_start_timer(opl3);
566 		opl3->voices[voice].note_off_check = 1;
567 	} else
568 		opl3->voices[voice].note_off_check = 0;
569 
570 	/* get extra pgm, but avoid possible loops */
571 	extra_prg = (extra_prg) ? 0 : fm->modes;
572 
573 	/* do the bookkeeping */
574 	vp->time = opl3->use_time++;
575 	vp->note = key;
576 	vp->chan = chan;
577 
578 	if (instr_4op) {
579 		vp->state = SNDRV_OPL3_ST_ON_4OP;
580 
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;
592 		}
593 		vp->state = SNDRV_OPL3_ST_ON_2OP;
594 	}
595 
596 #ifdef DEBUG_ALLOC
597 	debug_alloc(opl3, "note on ", voice);
598 #endif
599 
600 	/* allocate extra program if specified in patch library */
601 	if (extra_prg) {
602 		if (extra_prg > 128) {
603 			bank = 128;
604 			/* percussions start at 35 */
605 			prg = extra_prg - 128 + 35 - 1;
606 		} else {
607 			bank = 0;
608 			prg = extra_prg - 1;
609 		}
610 #ifdef DEBUG_MIDI
611 		snd_printk(KERN_DEBUG " *** allocating extra program\n");
612 #endif
613 		goto __extra_prg;
614 	}
615 	spin_unlock_irqrestore(&opl3->voice_lock, flags);
616 }
617 
618 static void snd_opl3_kill_voice(struct snd_opl3 *opl3, int voice)
619 {
620 	unsigned short reg_side;
621 	unsigned char voice_offset;
622 	unsigned short opl3_reg;
623 
624 	struct snd_opl3_voice *vp, *vp2;
625 
626 	if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
627 		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(KERN_DEBUG "  --> 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 static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
668 				     struct snd_midi_channel *chan)
669 {
670   	struct snd_opl3 *opl3;
671 
672 	int voice;
673 	struct snd_opl3_voice *vp;
674 
675 	opl3 = p;
676 
677 #ifdef DEBUG_MIDI
678 	snd_printk(KERN_DEBUG "Note off, ch %i, inst %i, note %i\n",
679 		   chan->number, chan->midi_program, note);
680 #endif
681 
682 	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
683 		if (chan->drum_channel && use_internal_drums) {
684 			snd_opl3_drum_switch(opl3, note, vel, 0, chan);
685 			return;
686 		}
687 		/* this loop will hopefully kill all extra voices, because
688 		   they are grouped by the same channel and note values */
689 		for (voice = 0; voice < opl3->max_voices; voice++) {
690 			vp = &opl3->voices[voice];
691 			if (vp->state > 0 && vp->chan == chan && vp->note == note) {
692 				snd_opl3_kill_voice(opl3, voice);
693 			}
694 		}
695 	} else {
696 		/* remap OSS voices */
697 		if (chan->number < MAX_OPL3_VOICES) {
698 			voice = snd_opl3_oss_map[chan->number];
699 			snd_opl3_kill_voice(opl3, voice);
700 		}
701 	}
702 }
703 
704 void snd_opl3_note_off(void *p, int note, int vel,
705 		       struct snd_midi_channel *chan)
706 {
707 	struct snd_opl3 *opl3 = p;
708 	unsigned long flags;
709 
710 	spin_lock_irqsave(&opl3->voice_lock, flags);
711 	snd_opl3_note_off_unsafe(p, note, vel, chan);
712 	spin_unlock_irqrestore(&opl3->voice_lock, flags);
713 }
714 
715 /*
716  * key pressure change
717  */
718 void snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
719 {
720 #ifdef DEBUG_MIDI
721 	snd_printk(KERN_DEBUG "Key pressure, ch#: %i, inst#: %i\n",
722 		   chan->number, chan->midi_program);
723 #endif
724 }
725 
726 /*
727  * terminate note
728  */
729 void snd_opl3_terminate_note(void *p, int note, struct snd_midi_channel *chan)
730 {
731 #ifdef DEBUG_MIDI
732 	snd_printk(KERN_DEBUG "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 	if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
748 		return;
749 
750 	vp = &opl3->voices[voice];
751 	if (vp->chan == NULL)
752 		return; /* not allocated? */
753 
754 	if (voice < MAX_OPL2_VOICES) {
755 		/* Left register block for voices 0 .. 8 */
756 		reg_side = OPL3_LEFT;
757 		voice_offset = voice;
758 	} else {
759 		/* Right register block for voices 9 .. 17 */
760 		reg_side = OPL3_RIGHT;
761 		voice_offset = voice - MAX_OPL2_VOICES;
762 	}
763 
764 	snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
765 
766 	/* Set OPL3 FNUM_LOW register of requested voice */
767 	opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
768 	opl3->command(opl3, opl3_reg, fnum);
769 
770 	vp->keyon_reg = blocknum;
771 
772 	/* Set output sound flag */
773 	blocknum |= OPL3_KEYON_BIT;
774 
775 	/* Set OPL3 KEYON_BLOCK register of requested voice */
776 	opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
777 	opl3->command(opl3, opl3_reg, blocknum);
778 
779 	vp->time = opl3->use_time++;
780 }
781 
782 /*
783  * Update voice pitch controller
784  */
785 static void snd_opl3_pitch_ctrl(struct snd_opl3 *opl3, struct snd_midi_channel *chan)
786 {
787 	int voice;
788 	struct snd_opl3_voice *vp;
789 
790 	unsigned long flags;
791 
792 	spin_lock_irqsave(&opl3->voice_lock, flags);
793 
794 	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
795 		for (voice = 0; voice < opl3->max_voices; voice++) {
796 			vp = &opl3->voices[voice];
797 			if (vp->state > 0 && vp->chan == chan) {
798 				snd_opl3_update_pitch(opl3, voice);
799 			}
800 		}
801 	} else {
802 		/* remap OSS voices */
803 		if (chan->number < MAX_OPL3_VOICES) {
804 			voice = snd_opl3_oss_map[chan->number];
805 			snd_opl3_update_pitch(opl3, voice);
806 		}
807 	}
808 	spin_unlock_irqrestore(&opl3->voice_lock, flags);
809 }
810 
811 /*
812  * Deal with a controller type event.  This includes all types of
813  * control events, not just the midi controllers
814  */
815 void snd_opl3_control(void *p, int type, struct snd_midi_channel *chan)
816 {
817   	struct snd_opl3 *opl3;
818 
819 	opl3 = p;
820 #ifdef DEBUG_MIDI
821 	snd_printk(KERN_DEBUG "Controller, TYPE = %i, ch#: %i, inst#: %i\n",
822 		   type, chan->number, chan->midi_program);
823 #endif
824 
825 	switch (type) {
826 	case MIDI_CTL_MSB_MODWHEEL:
827 		if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
828 			opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
829 		else
830 			opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
831 		opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
832 				 opl3->drum_reg);
833 		break;
834 	case MIDI_CTL_E2_TREMOLO_DEPTH:
835 		if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
836 			opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
837 		else
838 			opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
839 		opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
840 				 opl3->drum_reg);
841 		break;
842 	case MIDI_CTL_PITCHBEND:
843 		snd_opl3_pitch_ctrl(opl3, chan);
844 		break;
845 	}
846 }
847 
848 /*
849  * NRPN events
850  */
851 void snd_opl3_nrpn(void *p, struct snd_midi_channel *chan,
852 		   struct snd_midi_channel_set *chset)
853 {
854 #ifdef DEBUG_MIDI
855 	snd_printk(KERN_DEBUG "NRPN, ch#: %i, inst#: %i\n",
856 		   chan->number, chan->midi_program);
857 #endif
858 }
859 
860 /*
861  * receive sysex
862  */
863 void snd_opl3_sysex(void *p, unsigned char *buf, int len,
864 		    int parsed, struct snd_midi_channel_set *chset)
865 {
866 #ifdef DEBUG_MIDI
867 	snd_printk(KERN_DEBUG "SYSEX\n");
868 #endif
869 }
870