Blame view

sound/drivers/opl3/opl3_midi.c 22.2 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  /*
   *  Copyright (c) by Uros Bizjak <uros@kss-loka.si>
   *
   *  Midi synth routines for OPL2/OPL3/OPL4 FM
   *
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License as published by
   *   the Free Software Foundation; either version 2 of the License, or
   *   (at your option) any later version.
   *
   *   This program is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *   GNU General Public License for more details.
   *
   *   You should have received a copy of the GNU General Public License
   *   along with this program; if not, write to the Free Software
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   *
   */
  
  #undef DEBUG_ALLOC
  #undef DEBUG_MIDI
  
  #include "opl3_voice.h"
  #include <sound/asoundef.h>
  
  extern char snd_opl3_regmap[MAX_OPL2_VOICES][4];
a67ff6a54   Rusty Russell   ALSA: module_para...
29
  extern bool use_internal_drums;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30

8dce39b89   Krzysztof Helt   ALSA: opl3: circu...
31
32
  static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
  				     struct snd_midi_channel *chan);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  /*
   * The next table looks magical, but it certainly is not. Its values have
   * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
   * for i=0. This log-table converts a linear volume-scaling (0..127) to a
   * logarithmic scaling as present in the FM-synthesizer chips. so :    Volume
   * 64 =  0 db = relative volume  0 and:    Volume 32 = -6 db = relative
   * volume -8 it was implemented as a table because it is only 128 bytes and
   * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>)
   */
  
  static char opl3_volume_table[128] =
  {
  	-63, -48, -40, -35, -32, -29, -27, -26,
  	-24, -23, -21, -20, -19, -18, -18, -17,
  	-16, -15, -15, -14, -13, -13, -12, -12,
  	-11, -11, -10, -10, -10, -9, -9, -8,
  	-8, -8, -7, -7, -7, -6, -6, -6,
  	-5, -5, -5, -5, -4, -4, -4, -4,
  	-3, -3, -3, -3, -2, -2, -2, -2,
  	-2, -1, -1, -1, -1, 0, 0, 0,
  	0, 0, 0, 1, 1, 1, 1, 1,
  	1, 2, 2, 2, 2, 2, 2, 2,
  	3, 3, 3, 3, 3, 3, 3, 4,
  	4, 4, 4, 4, 4, 4, 4, 5,
  	5, 5, 5, 5, 5, 5, 5, 5,
  	6, 6, 6, 6, 6, 6, 6, 6,
  	6, 7, 7, 7, 7, 7, 7, 7,
  	7, 7, 7, 8, 8, 8, 8, 8
  };
  
  void snd_opl3_calc_volume(unsigned char *volbyte, int vel,
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
64
  			  struct snd_midi_channel *chan)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  {
  	int oldvol, newvol, n;
  	int volume;
  
  	volume = (vel * chan->gm_volume * chan->gm_expression) / (127*127);
  	if (volume > 127)
  		volume = 127;
  
  	oldvol = OPL3_TOTAL_LEVEL_MASK - (*volbyte & OPL3_TOTAL_LEVEL_MASK);
  
  	newvol = opl3_volume_table[volume] + oldvol;
  	if (newvol > OPL3_TOTAL_LEVEL_MASK)
  		newvol = OPL3_TOTAL_LEVEL_MASK;
  	else if (newvol < 0)
  		newvol = 0;
  
  	n = OPL3_TOTAL_LEVEL_MASK - (newvol & OPL3_TOTAL_LEVEL_MASK);
  
  	*volbyte = (*volbyte & OPL3_KSL_MASK) | (n & OPL3_TOTAL_LEVEL_MASK);
  }
  
  /*
   * Converts the note frequency to block and fnum values for the FM chip
   */
  static short opl3_note_table[16] =
  {
  	305, 323,	/* for pitch bending, -2 semitones */
  	343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647,
  	686, 726	/* for pitch bending, +2 semitones */
  };
  
  static void snd_opl3_calc_pitch(unsigned char *fnum, unsigned char *blocknum,
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
97
  				int note, struct snd_midi_channel *chan)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  {
  	int block = ((note / 12) & 0x07) - 1;
  	int idx = (note % 12) + 2;
  	int freq;
  
  	if (chan->midi_pitchbend) {
  		int pitchbend = chan->midi_pitchbend;
  		int segment;
  
  		if (pitchbend > 0x1FFF)
  			pitchbend = 0x1FFF;
  
  		segment = pitchbend / 0x1000;
  		freq = opl3_note_table[idx+segment];
  		freq += ((opl3_note_table[idx+segment+1] - freq) *
  			 (pitchbend % 0x1000)) / 0x1000;
  	} else {
  		freq = opl3_note_table[idx];
  	}
  
  	*fnum = (unsigned char) freq;
  	*blocknum = ((freq >> 8) & OPL3_FNUM_HIGH_MASK) |
  		((block << 2) & OPL3_BLOCKNUM_MASK);
  }
  
  
  #ifdef DEBUG_ALLOC
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
125
  static void debug_alloc(struct snd_opl3 *opl3, char *s, int voice) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
127
  	int i;
  	char *str = "x.24";
45203832d   Takashi Iwai   ALSA: Add missing...
128
  	printk(KERN_DEBUG "time %.5i: %s [%.2i]: ", opl3->use_time, s, voice);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
132
133
134
135
136
137
138
  	for (i = 0; i < opl3->max_voices; i++)
  		printk("%c", *(str + opl3->voices[i].state + 1));
  	printk("
  ");
  }
  #endif
  
  /*
   * Get a FM voice (channel) to play a note on.
   */
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
139
140
  static int opl3_get_voice(struct snd_opl3 *opl3, int instr_4op,
  			  struct snd_midi_channel *chan) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
142
  	int chan_4op_1;		/* first voice for 4op instrument */
  	int chan_4op_2;		/* second voice for 4op instrument */
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
143
  	struct snd_opl3_voice *vp, *vp2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  	unsigned int voice_time;
  	int i;
  
  #ifdef DEBUG_ALLOC
  	char *alloc_type[3] = { "FREE     ", "CHEAP    ", "EXPENSIVE" };
  #endif
  
  	/* This is our "allocation cost" table */
  	enum {
  		FREE = 0, CHEAP, EXPENSIVE, END
  	};
  
  	/* Keeps track of what we are finding */
  	struct best {
  		unsigned int time;
  		int voice;
  	} best[END];
  	struct best *bp;
  
  	for (i = 0; i < END; i++) {
  		best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */;
  		best[i].voice = -1;
  	}
  
  	/* Look through all the channels for the most suitable. */
  	for (i = 0; i < opl3->max_voices; i++) {
  		vp = &opl3->voices[i];
  
  		if (vp->state == SNDRV_OPL3_ST_NOT_AVAIL)
  		  /* skip unavailable channels, allocated by
  		     drum voices or by bounded 4op voices) */
  			continue;
  
  		voice_time = vp->time;
  		bp = best;
  
  		chan_4op_1 = ((i < 3) || (i > 8 && i < 12));
  		chan_4op_2 = ((i > 2 && i < 6) || (i > 11 && i < 15));
  		if (instr_4op) {
  			/* allocate 4op voice */
  			/* skip channels unavailable to 4op instrument */
  			if (!chan_4op_1)
  				continue;
  
  			if (vp->state)
  				/* kill one voice, CHEAP */
  				bp++;
  			/* get state of bounded 2op channel
  			   to be allocated for 4op instrument */
  			vp2 = &opl3->voices[i + 3];
  			if (vp2->state == SNDRV_OPL3_ST_ON_2OP) {
  				/* kill two voices, EXPENSIVE */
  				bp++;
  				voice_time = (voice_time > vp->time) ?
  					voice_time : vp->time;
  			}
  		} else {
  			/* allocate 2op voice */
  			if ((chan_4op_1) || (chan_4op_2))
  				/* use bounded channels for 2op, CHEAP */
  				bp++;
  			else if (vp->state)
  				/* kill one voice on 2op channel, CHEAP */
  				bp++;
  			/* raise kill cost to EXPENSIVE for all channels */
  			if (vp->state)
  				bp++;
  		}
  		if (voice_time < bp->time) {
  			bp->time = voice_time;
  			bp->voice = i;
  		}
  	}
  
  	for (i = 0; i < END; i++) {
  		if (best[i].voice >= 0) {
  #ifdef DEBUG_ALLOC
45203832d   Takashi Iwai   ALSA: Add missing...
221
222
  			printk(KERN_DEBUG "%s %iop allocation on voice %i
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  			       alloc_type[i], instr_4op ? 4 : 2,
  			       best[i].voice);
  #endif
  			return best[i].voice;
  		}
  	}
  	/* not found */
  	return -1;
  }
  
  /* ------------------------------ */
  
  /*
   * System timer interrupt function
   */
  void snd_opl3_timer_func(unsigned long data)
  {
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
240
  	struct snd_opl3 *opl3 = (struct snd_opl3 *)data;
b32425ac9   Takashi Iwai   [ALSA] Fix possib...
241
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
243
  	int again = 0;
  	int i;
8dce39b89   Krzysztof Helt   ALSA: opl3: circu...
244
  	spin_lock_irqsave(&opl3->voice_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245
  	for (i = 0; i < opl3->max_voices; i++) {
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
246
  		struct snd_opl3_voice *vp = &opl3->voices[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
248
  		if (vp->state > 0 && vp->note_off_check) {
  			if (vp->note_off == jiffies)
8dce39b89   Krzysztof Helt   ALSA: opl3: circu...
249
250
  				snd_opl3_note_off_unsafe(opl3, vp->note, 0,
  							 vp->chan);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
252
253
254
  			else
  				again++;
  		}
  	}
8dce39b89   Krzysztof Helt   ALSA: opl3: circu...
255
256
257
  	spin_unlock_irqrestore(&opl3->voice_lock, flags);
  
  	spin_lock_irqsave(&opl3->sys_timer_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258
259
260
261
262
263
  	if (again) {
  		opl3->tlist.expires = jiffies + 1;	/* invoke again */
  		add_timer(&opl3->tlist);
  	} else {
  		opl3->sys_timer_status = 0;
  	}
b32425ac9   Takashi Iwai   [ALSA] Fix possib...
264
  	spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
266
267
268
269
  }
  
  /*
   * Start system timer
   */
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
270
  static void snd_opl3_start_timer(struct snd_opl3 *opl3)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  {
  	unsigned long flags;
  	spin_lock_irqsave(&opl3->sys_timer_lock, flags);
  	if (! opl3->sys_timer_status) {
  		opl3->tlist.expires = jiffies + 1;
  		add_timer(&opl3->tlist);
  		opl3->sys_timer_status = 1;
  	}
  	spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
  }
  
  /* ------------------------------ */
  
  
  static int snd_opl3_oss_map[MAX_OPL3_VOICES] = {
  	0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14
  };
  
  /*
   * Start a note.
   */
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
292
  void snd_opl3_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293
  {
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
294
  	struct snd_opl3 *opl3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
296
297
  	int instr_4op;
  
  	int voice;
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
298
  	struct snd_opl3_voice *vp, *vp2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
300
301
302
303
304
305
306
307
308
309
  	unsigned short connect_mask;
  	unsigned char connection;
  	unsigned char vol_op[4];
  
  	int extra_prg = 0;
  
  	unsigned short reg_side;
  	unsigned char op_offset;
  	unsigned char voice_offset;
  	unsigned short opl3_reg;
  	unsigned char reg_val;
224a03325   Takashi Iwai   [ALSA] opl3 - Use...
310
  	unsigned char prg, bank;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311
312
313
314
  
  	int key = note;
  	unsigned char fnum, blocknum;
  	int i;
224a03325   Takashi Iwai   [ALSA] opl3 - Use...
315
  	struct fm_patch *patch;
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
316
  	struct fm_instrument *fm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
318
319
320
321
  	unsigned long flags;
  
  	opl3 = p;
  
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
322
323
  	snd_printk(KERN_DEBUG "Note on, ch %i, inst %i, note %i, vel %i
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
325
  		   chan->number, chan->midi_program, note, vel);
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
327
328
329
330
331
  
  	/* in SYNTH mode, application takes care of voices */
  	/* in SEQ mode, drum voice numbers are notes on drum channel */
  	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  		if (chan->drum_channel) {
  			/* percussion instruments are located in bank 128 */
224a03325   Takashi Iwai   [ALSA] opl3 - Use...
332
333
  			bank = 128;
  			prg = note;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
  		} else {
224a03325   Takashi Iwai   [ALSA] opl3 - Use...
335
336
  			bank = chan->gm_bank_select;
  			prg = chan->midi_program;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
337
338
339
340
341
342
343
  		}
  	} else {
  		/* Prepare for OSS mode */
  		if (chan->number >= MAX_OPL3_VOICES)
  			return;
  
  		/* OSS instruments are located in bank 127 */
224a03325   Takashi Iwai   [ALSA] opl3 - Use...
344
345
  		bank = 127;
  		prg = chan->midi_program;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
347
348
349
350
351
352
353
354
355
356
  	}
  
  	spin_lock_irqsave(&opl3->voice_lock, flags);
  
  	if (use_internal_drums) {
  		snd_opl3_drum_switch(opl3, note, vel, 1, chan);
  		spin_unlock_irqrestore(&opl3->voice_lock, flags);
  		return;
  	}
  
   __extra_prg:
224a03325   Takashi Iwai   [ALSA] opl3 - Use...
357
358
  	patch = snd_opl3_find_patch(opl3, prg, bank, 0);
  	if (!patch) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
360
361
  		spin_unlock_irqrestore(&opl3->voice_lock, flags);
  		return;
  	}
224a03325   Takashi Iwai   [ALSA] opl3 - Use...
362
363
  	fm = &patch->inst;
  	switch (patch->type) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
366
367
368
369
370
371
372
  	case FM_PATCH_OPL2:
  		instr_4op = 0;
  		break;
  	case FM_PATCH_OPL3:
  		if (opl3->hardware >= OPL3_HW_OPL3) {
  			instr_4op = 1;
  			break;
  		}
  	default:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
373
374
375
  		spin_unlock_irqrestore(&opl3->voice_lock, flags);
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
377
378
  	snd_printk(KERN_DEBUG "  --> OPL%i instrument: %s
  ",
224a03325   Takashi Iwai   [ALSA] opl3 - Use...
379
  		   instr_4op ? 3 : 2, patch->name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
  #endif
  	/* in SYNTH mode, application takes care of voices */
  	/* in SEQ mode, allocate voice on free OPL3 channel */
  	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  		voice = opl3_get_voice(opl3, instr_4op, chan);
  	} else {
  		/* remap OSS voice */
  		voice = snd_opl3_oss_map[chan->number];		
  	}
  
  	if (voice < MAX_OPL2_VOICES) {
  		/* Left register block for voices 0 .. 8 */
  		reg_side = OPL3_LEFT;
  		voice_offset = voice;
  		connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07;
  	} else {
  		/* Right register block for voices 9 .. 17 */
  		reg_side = OPL3_RIGHT;
  		voice_offset = voice - MAX_OPL2_VOICES;
  		connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38;
  	}
  
  	/* kill voice on channel */
  	vp = &opl3->voices[voice];
  	if (vp->state > 0) {
  		opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  		reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
  		opl3->command(opl3, opl3_reg, reg_val);
  	}
  	if (instr_4op) {
  		vp2 = &opl3->voices[voice + 3];
  		if (vp->state > 0) {
  			opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
  					       voice_offset + 3);
  			reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
  			opl3->command(opl3, opl3_reg, reg_val);
  		}
  	}
  
  	/* set connection register */
  	if (instr_4op) {
  		if ((opl3->connection_reg ^ connect_mask) & connect_mask) {
  			opl3->connection_reg |= connect_mask;
  			/* set connection bit */
  			opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
  			opl3->command(opl3, opl3_reg, opl3->connection_reg);
  		}
  	} else {
  		if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) {
  			opl3->connection_reg &= ~connect_mask;
  			/* clear connection bit */
  			opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
  			opl3->command(opl3, opl3_reg, opl3->connection_reg);
  		}
  	}
  
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
437
438
  	snd_printk(KERN_DEBUG "  --> setting OPL3 connection: 0x%x
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
  		   opl3->connection_reg);
  #endif
  	/*
  	 * calculate volume depending on connection
  	 * between FM operators (see include/opl3.h)
  	 */
  	for (i = 0; i < (instr_4op ? 4 : 2); i++)
  		vol_op[i] = fm->op[i].ksl_level;
  
  	connection = fm->feedback_connection[0] & 0x01;
  	if (instr_4op) {
  		connection <<= 1;
  		connection |= fm->feedback_connection[1] & 0x01;
  
  		snd_opl3_calc_volume(&vol_op[3], vel, chan);
  		switch (connection) {
  		case 0x03:
  			snd_opl3_calc_volume(&vol_op[2], vel, chan);
  			/* fallthru */
  		case 0x02:
  			snd_opl3_calc_volume(&vol_op[0], vel, chan);
  			break;
  		case 0x01:
  			snd_opl3_calc_volume(&vol_op[1], vel, chan);
  		}
  	} else {
  		snd_opl3_calc_volume(&vol_op[1], vel, chan);
  		if (connection)
  			snd_opl3_calc_volume(&vol_op[0], vel, chan);
  	}
  
  	/* Program the FM voice characteristics */
  	for (i = 0; i < (instr_4op ? 4 : 2); i++) {
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
473
474
  		snd_printk(KERN_DEBUG "  --> programming operator %i
  ", i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
  #endif
  		op_offset = snd_opl3_regmap[voice_offset][i];
  
  		/* Set OPL3 AM_VIB register of requested voice/operator */ 
  		reg_val = fm->op[i].am_vib;
  		opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
  		opl3->command(opl3, opl3_reg, reg_val);
  
  		/* Set OPL3 KSL_LEVEL register of requested voice/operator */ 
  		reg_val = vol_op[i];
  		opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
  		opl3->command(opl3, opl3_reg, reg_val);
  
  		/* Set OPL3 ATTACK_DECAY register of requested voice/operator */ 
  		reg_val = fm->op[i].attack_decay;
  		opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
  		opl3->command(opl3, opl3_reg, reg_val);
  
  		/* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */ 
  		reg_val = fm->op[i].sustain_release;
  		opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
  		opl3->command(opl3, opl3_reg, reg_val);
  
  		/* Select waveform */
  		reg_val = fm->op[i].wave_select;
  		opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
  		opl3->command(opl3, opl3_reg, reg_val);
  	}
  
  	/* Set operator feedback and 2op inter-operator connection */
  	reg_val = fm->feedback_connection[0];
  	/* Set output voice connection */
  	reg_val |= OPL3_STEREO_BITS;
  	if (chan->gm_pan < 43)
  		reg_val &= ~OPL3_VOICE_TO_RIGHT;
  	if (chan->gm_pan > 85)
  		reg_val &= ~OPL3_VOICE_TO_LEFT;
  	opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
  	opl3->command(opl3, opl3_reg, reg_val);
  
  	if (instr_4op) {
  		/* Set 4op inter-operator connection */
  		reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT;
  		/* Set output voice connection */
  		reg_val |= OPL3_STEREO_BITS;
  		if (chan->gm_pan < 43)
  			reg_val &= ~OPL3_VOICE_TO_RIGHT;
  		if (chan->gm_pan > 85)
  			reg_val &= ~OPL3_VOICE_TO_LEFT;
  		opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION +
  				       voice_offset + 3);
  		opl3->command(opl3, opl3_reg, reg_val);
  	}
  
  	/*
  	 * Special treatment of percussion notes for fm:
  	 * Requested pitch is really program, and pitch for
  	 * device is whatever was specified in the patch library.
  	 */
  	if (fm->fix_key)
  		note = fm->fix_key;
  	/*
  	 * use transpose if defined in patch library
  	 */
  	if (fm->trnsps)
  		note += (fm->trnsps - 64);
  
  	snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
  
  	/* Set OPL3 FNUM_LOW register of requested voice */
  	opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
  	opl3->command(opl3, opl3_reg, fnum);
  
  	opl3->voices[voice].keyon_reg = blocknum;
  
  	/* Set output sound flag */
  	blocknum |= OPL3_KEYON_BIT;
  
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
554
555
  	snd_printk(KERN_DEBUG "  --> trigger voice %i
  ", voice);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
  #endif
  	/* Set OPL3 KEYON_BLOCK register of requested voice */ 
  	opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  	opl3->command(opl3, opl3_reg, blocknum);
  
  	/* kill note after fixed duration (in centiseconds) */
  	if (fm->fix_dur) {
  		opl3->voices[voice].note_off = jiffies +
  			(fm->fix_dur * HZ) / 100;
  		snd_opl3_start_timer(opl3);
  		opl3->voices[voice].note_off_check = 1;
  	} else
  		opl3->voices[voice].note_off_check = 0;
  
  	/* get extra pgm, but avoid possible loops */
  	extra_prg = (extra_prg) ? 0 : fm->modes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
  	/* do the bookkeeping */
  	vp->time = opl3->use_time++;
  	vp->note = key;
  	vp->chan = chan;
  
  	if (instr_4op) {
  		vp->state = SNDRV_OPL3_ST_ON_4OP;
  
  		vp2 = &opl3->voices[voice + 3];
  		vp2->time = opl3->use_time++;
  		vp2->note = key;
  		vp2->chan = chan;
  		vp2->state = SNDRV_OPL3_ST_NOT_AVAIL;
  	} else {
  		if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
  			/* 4op killed by 2op, release bounded voice */
  			vp2 = &opl3->voices[voice + 3];
  			vp2->time = opl3->use_time++;
  			vp2->state = SNDRV_OPL3_ST_OFF;
  		}
  		vp->state = SNDRV_OPL3_ST_ON_2OP;
  	}
  
  #ifdef DEBUG_ALLOC
  	debug_alloc(opl3, "note on ", voice);
  #endif
  
  	/* allocate extra program if specified in patch library */
  	if (extra_prg) {
  		if (extra_prg > 128) {
224a03325   Takashi Iwai   [ALSA] opl3 - Use...
602
  			bank = 128;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
603
  			/* percussions start at 35 */
224a03325   Takashi Iwai   [ALSA] opl3 - Use...
604
  			prg = extra_prg - 128 + 35 - 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
605
  		} else {
224a03325   Takashi Iwai   [ALSA] opl3 - Use...
606
607
  			bank = 0;
  			prg = extra_prg - 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
608
609
  		}
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
610
611
  		snd_printk(KERN_DEBUG " *** allocating extra program
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
613
614
615
616
  #endif
  		goto __extra_prg;
  	}
  	spin_unlock_irqrestore(&opl3->voice_lock, flags);
  }
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
617
  static void snd_opl3_kill_voice(struct snd_opl3 *opl3, int voice)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
618
619
620
621
  {
  	unsigned short reg_side;
  	unsigned char voice_offset;
  	unsigned short opl3_reg;
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
622
  	struct snd_opl3_voice *vp, *vp2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623

5e246b850   Takashi Iwai   ALSA: Kill snd_as...
624
625
  	if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
626
627
628
629
630
631
632
633
634
635
636
637
638
639
  
  	vp = &opl3->voices[voice];
  	if (voice < MAX_OPL2_VOICES) {
  		/* Left register block for voices 0 .. 8 */
  		reg_side = OPL3_LEFT;
  		voice_offset = voice;
  	} else {
  		/* Right register block for voices 9 .. 17 */
  		reg_side = OPL3_RIGHT;
  		voice_offset = voice - MAX_OPL2_VOICES;
  	}
  
  	/* kill voice */
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
640
641
  	snd_printk(KERN_DEBUG "  --> kill voice %i
  ", voice);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
  #endif
  	opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  	/* clear Key ON bit */
  	opl3->command(opl3, opl3_reg, vp->keyon_reg);
  
  	/* do the bookkeeping */
  	vp->time = opl3->use_time++;
  
  	if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
  		vp2 = &opl3->voices[voice + 3];
  
  		vp2->time = opl3->use_time++;
  		vp2->state = SNDRV_OPL3_ST_OFF;
  	}
  	vp->state = SNDRV_OPL3_ST_OFF;
  #ifdef DEBUG_ALLOC
  	debug_alloc(opl3, "note off", voice);
  #endif
  
  }
  
  /*
   * Release a note in response to a midi note off.
   */
8dce39b89   Krzysztof Helt   ALSA: opl3: circu...
666
667
  static void snd_opl3_note_off_unsafe(void *p, int note, int vel,
  				     struct snd_midi_channel *chan)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
668
  {
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
669
    	struct snd_opl3 *opl3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
670
671
  
  	int voice;
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
672
  	struct snd_opl3_voice *vp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
674
675
676
  	opl3 = p;
  
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
677
678
  	snd_printk(KERN_DEBUG "Note off, ch %i, inst %i, note %i
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
679
680
  		   chan->number, chan->midi_program, note);
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
681
682
683
  	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  		if (chan->drum_channel && use_internal_drums) {
  			snd_opl3_drum_switch(opl3, note, vel, 0, chan);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
  			return;
  		}
  		/* this loop will hopefully kill all extra voices, because
  		   they are grouped by the same channel and note values */
  		for (voice = 0; voice < opl3->max_voices; voice++) {
  			vp = &opl3->voices[voice];
  			if (vp->state > 0 && vp->chan == chan && vp->note == note) {
  				snd_opl3_kill_voice(opl3, voice);
  			}
  		}
  	} else {
  		/* remap OSS voices */
  		if (chan->number < MAX_OPL3_VOICES) {
  			voice = snd_opl3_oss_map[chan->number];		
  			snd_opl3_kill_voice(opl3, voice);
  		}
  	}
8dce39b89   Krzysztof Helt   ALSA: opl3: circu...
701
702
703
704
705
706
707
708
709
710
  }
  
  void snd_opl3_note_off(void *p, int note, int vel,
  		       struct snd_midi_channel *chan)
  {
  	struct snd_opl3 *opl3 = p;
  	unsigned long flags;
  
  	spin_lock_irqsave(&opl3->voice_lock, flags);
  	snd_opl3_note_off_unsafe(p, note, vel, chan);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711
712
713
714
715
716
  	spin_unlock_irqrestore(&opl3->voice_lock, flags);
  }
  
  /*
   * key pressure change
   */
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
717
  void snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
718
  {
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
719
    	struct snd_opl3 *opl3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
720
721
722
  
  	opl3 = p;
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
723
724
  	snd_printk(KERN_DEBUG "Key pressure, ch#: %i, inst#: %i
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
726
727
728
729
730
731
  		   chan->number, chan->midi_program);
  #endif
  }
  
  /*
   * terminate note
   */
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
732
  void snd_opl3_terminate_note(void *p, int note, struct snd_midi_channel *chan)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
733
  {
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
734
    	struct snd_opl3 *opl3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
735
736
737
  
  	opl3 = p;
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
738
739
  	snd_printk(KERN_DEBUG "Terminate note, ch#: %i, inst#: %i
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
740
741
742
  		   chan->number, chan->midi_program);
  #endif
  }
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
743
  static void snd_opl3_update_pitch(struct snd_opl3 *opl3, int voice)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
744
745
746
747
748
749
  {
  	unsigned short reg_side;
  	unsigned char voice_offset;
  	unsigned short opl3_reg;
  
  	unsigned char fnum, blocknum;
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
750
  	struct snd_opl3_voice *vp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
751

5e246b850   Takashi Iwai   ALSA: Kill snd_as...
752
753
  	if (snd_BUG_ON(voice >= MAX_OPL3_VOICES))
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
  
  	vp = &opl3->voices[voice];
  	if (vp->chan == NULL)
  		return; /* not allocated? */
  
  	if (voice < MAX_OPL2_VOICES) {
  		/* Left register block for voices 0 .. 8 */
  		reg_side = OPL3_LEFT;
  		voice_offset = voice;
  	} else {
  		/* Right register block for voices 9 .. 17 */
  		reg_side = OPL3_RIGHT;
  		voice_offset = voice - MAX_OPL2_VOICES;
  	}
  
  	snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
  
  	/* Set OPL3 FNUM_LOW register of requested voice */
  	opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
  	opl3->command(opl3, opl3_reg, fnum);
  
  	vp->keyon_reg = blocknum;
  
  	/* Set output sound flag */
  	blocknum |= OPL3_KEYON_BIT;
  
  	/* Set OPL3 KEYON_BLOCK register of requested voice */ 
  	opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  	opl3->command(opl3, opl3_reg, blocknum);
  
  	vp->time = opl3->use_time++;
  }
  
  /*
   * Update voice pitch controller
   */
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
790
  static void snd_opl3_pitch_ctrl(struct snd_opl3 *opl3, struct snd_midi_channel *chan)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
791
792
  {
  	int voice;
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
793
  	struct snd_opl3_voice *vp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
  
  	unsigned long flags;
  
  	spin_lock_irqsave(&opl3->voice_lock, flags);
  
  	if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
  		for (voice = 0; voice < opl3->max_voices; voice++) {
  			vp = &opl3->voices[voice];
  			if (vp->state > 0 && vp->chan == chan) {
  				snd_opl3_update_pitch(opl3, voice);
  			}
  		}
  	} else {
  		/* remap OSS voices */
  		if (chan->number < MAX_OPL3_VOICES) {
  			voice = snd_opl3_oss_map[chan->number];		
  			snd_opl3_update_pitch(opl3, voice);
  		}
  	}
  	spin_unlock_irqrestore(&opl3->voice_lock, flags);
  }
  
  /*
3a4fa0a25   Robert P. J. Day   Fix misspellings ...
817
   * Deal with a controller type event.  This includes all types of
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
818
819
   * control events, not just the midi controllers
   */
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
820
  void snd_opl3_control(void *p, int type, struct snd_midi_channel *chan)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
821
  {
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
822
    	struct snd_opl3 *opl3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
824
825
  
  	opl3 = p;
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
826
827
  	snd_printk(KERN_DEBUG "Controller, TYPE = %i, ch#: %i, inst#: %i
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
  		   type, chan->number, chan->midi_program);
  #endif
  
  	switch (type) {
  	case MIDI_CTL_MSB_MODWHEEL:
  		if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
  			opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
  		else 
  			opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
  		opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
  				 opl3->drum_reg);
  		break;
  	case MIDI_CTL_E2_TREMOLO_DEPTH:
  		if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
  			opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
  		else 
  			opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
  		opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
  				 opl3->drum_reg);
  		break;
  	case MIDI_CTL_PITCHBEND:
  		snd_opl3_pitch_ctrl(opl3, chan);
  		break;
  	}
  }
  
  /*
   * NRPN events
   */
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
857
858
  void snd_opl3_nrpn(void *p, struct snd_midi_channel *chan,
  		   struct snd_midi_channel_set *chset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
859
  {
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
860
    	struct snd_opl3 *opl3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
861
862
863
  
  	opl3 = p;
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
864
865
  	snd_printk(KERN_DEBUG "NRPN, ch#: %i, inst#: %i
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
866
867
868
869
870
871
872
873
  		   chan->number, chan->midi_program);
  #endif
  }
  
  /*
   * receive sysex
   */
  void snd_opl3_sysex(void *p, unsigned char *buf, int len,
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
874
  		    int parsed, struct snd_midi_channel_set *chset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
875
  {
5b1646a8e   Takashi Iwai   [ALSA] Remove xxx...
876
    	struct snd_opl3 *opl3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
877
878
879
  
  	opl3 = p;
  #ifdef DEBUG_MIDI
45203832d   Takashi Iwai   ALSA: Add missing...
880
881
  	snd_printk(KERN_DEBUG "SYSEX
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
882
883
  #endif
  }