Linux Audio

Check our new training course

Yocto / OpenEmbedded training

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