Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *  Midi synth routines for the Emu8k/Emu10k1
  4 *
  5 *  Copyright (C) 1999 Steve Ratcliffe
  6 *  Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
  7 *
  8 *  Contains code based on awe_wave.c by Takashi Iwai
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  9 */
 10
 11#include <linux/export.h>
 12#include "emux_voice.h"
 13#include <sound/asoundef.h>
 14
 15/*
 16 * Prototypes
 17 */
 18
 19/*
 20 * Ensure a value is between two points
 21 * macro evaluates its args more than once, so changed to upper-case.
 22 */
 23#define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
 24#define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
 25
 26static int get_zone(struct snd_emux *emu, struct snd_emux_port *port,
 27		    int *notep, int vel, struct snd_midi_channel *chan,
 28		    struct snd_sf_zone **table);
 29static int get_bank(struct snd_emux_port *port, struct snd_midi_channel *chan);
 30static void terminate_note1(struct snd_emux *emu, int note,
 31			    struct snd_midi_channel *chan, int free);
 32static void exclusive_note_off(struct snd_emux *emu, struct snd_emux_port *port,
 33			       int exclass);
 34static void terminate_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int free);
 35static void update_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int update);
 36static void setup_voice(struct snd_emux_voice *vp);
 37static int calc_pan(struct snd_emux_voice *vp);
 38static int calc_volume(struct snd_emux_voice *vp);
 39static int calc_pitch(struct snd_emux_voice *vp);
 40
 41
 42/*
 43 * Start a note.
 44 */
 45void
 46snd_emux_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
 47{
 48	struct snd_emux *emu;
 49	int i, key, nvoices;
 50	struct snd_emux_voice *vp;
 51	struct snd_sf_zone *table[SNDRV_EMUX_MAX_MULTI_VOICES];
 52	unsigned long flags;
 53	struct snd_emux_port *port;
 54
 55	port = p;
 56	if (snd_BUG_ON(!port || !chan))
 57		return;
 58
 59	emu = port->emu;
 60	if (snd_BUG_ON(!emu || !emu->ops.get_voice || !emu->ops.trigger))
 61		return;
 62
 63	key = note; /* remember the original note */
 64	nvoices = get_zone(emu, port, &note, vel, chan, table);
 65	if (! nvoices)
 66		return;
 67
 68	/* exclusive note off */
 69	for (i = 0; i < nvoices; i++) {
 70		struct snd_sf_zone *zp = table[i];
 71		if (zp && zp->v.exclusiveClass)
 72			exclusive_note_off(emu, port, zp->v.exclusiveClass);
 73	}
 74
 75#if 0 // seems not necessary
 76	/* Turn off the same note on the same channel. */
 77	terminate_note1(emu, key, chan, 0);
 78#endif
 79
 80	spin_lock_irqsave(&emu->voice_lock, flags);
 81	for (i = 0; i < nvoices; i++) {
 82
 83		/* set up each voice parameter */
 84		/* at this stage, we don't trigger the voice yet. */
 85
 86		if (table[i] == NULL)
 87			continue;
 88
 89		vp = emu->ops.get_voice(emu, port);
 90		if (vp == NULL || vp->ch < 0)
 91			continue;
 92		if (STATE_IS_PLAYING(vp->state))
 93			emu->ops.terminate(vp);
 94
 95		vp->time = emu->use_time++;
 96		vp->chan = chan;
 97		vp->port = port;
 98		vp->key = key;
 99		vp->note = note;
100		vp->velocity = vel;
101		vp->zone = table[i];
102		if (vp->zone->sample)
103			vp->block = vp->zone->sample->block;
104		else
105			vp->block = NULL;
106
107		setup_voice(vp);
108
109		vp->state = SNDRV_EMUX_ST_STANDBY;
110		if (emu->ops.prepare) {
111			vp->state = SNDRV_EMUX_ST_OFF;
112			if (emu->ops.prepare(vp) >= 0)
113				vp->state = SNDRV_EMUX_ST_STANDBY;
114		}
115	}
116
117	/* start envelope now */
118	for (i = 0; i < emu->max_voices; i++) {
119		vp = &emu->voices[i];
120		if (vp->state == SNDRV_EMUX_ST_STANDBY &&
121		    vp->chan == chan) {
122			emu->ops.trigger(vp);
123			vp->state = SNDRV_EMUX_ST_ON;
124			vp->ontime = jiffies; /* remember the trigger timing */
125		}
126	}
127	spin_unlock_irqrestore(&emu->voice_lock, flags);
128
129#ifdef SNDRV_EMUX_USE_RAW_EFFECT
130	if (port->port_mode == SNDRV_EMUX_PORT_MODE_OSS_SYNTH) {
131		/* clear voice position for the next note on this channel */
132		struct snd_emux_effect_table *fx = chan->private;
133		if (fx) {
134			fx->flag[EMUX_FX_SAMPLE_START] = 0;
135			fx->flag[EMUX_FX_COARSE_SAMPLE_START] = 0;
136		}
137	}
138#endif
139}
140
141/*
142 * Release a note in response to a midi note off.
143 */
144void
145snd_emux_note_off(void *p, int note, int vel, struct snd_midi_channel *chan)
146{
147	int ch;
148	struct snd_emux *emu;
149	struct snd_emux_voice *vp;
150	unsigned long flags;
151	struct snd_emux_port *port;
152
153	port = p;
154	if (snd_BUG_ON(!port || !chan))
155		return;
156
157	emu = port->emu;
158	if (snd_BUG_ON(!emu || !emu->ops.release))
159		return;
160
161	spin_lock_irqsave(&emu->voice_lock, flags);
162	for (ch = 0; ch < emu->max_voices; ch++) {
163		vp = &emu->voices[ch];
164		if (STATE_IS_PLAYING(vp->state) &&
165		    vp->chan == chan && vp->key == note) {
166			vp->state = SNDRV_EMUX_ST_RELEASED;
167			if (vp->ontime == jiffies) {
168				/* if note-off is sent too shortly after
169				 * note-on, emuX engine cannot produce the sound
170				 * correctly.  so we'll release this note
171				 * a bit later via timer callback.
172				 */
173				vp->state = SNDRV_EMUX_ST_PENDING;
174				if (! emu->timer_active) {
175					mod_timer(&emu->tlist, jiffies + 1);
 
176					emu->timer_active = 1;
177				}
178			} else
179				/* ok now release the note */
180				emu->ops.release(vp);
181		}
182	}
183	spin_unlock_irqrestore(&emu->voice_lock, flags);
184}
185
186/*
187 * timer callback
188 *
189 * release the pending note-offs
190 */
191void snd_emux_timer_callback(struct timer_list *t)
192{
193	struct snd_emux *emu = from_timer(emu, t, tlist);
194	struct snd_emux_voice *vp;
195	unsigned long flags;
196	int ch, do_again = 0;
197
198	spin_lock_irqsave(&emu->voice_lock, flags);
199	for (ch = 0; ch < emu->max_voices; ch++) {
200		vp = &emu->voices[ch];
201		if (vp->state == SNDRV_EMUX_ST_PENDING) {
202			if (vp->ontime == jiffies)
203				do_again++; /* release this at the next interrupt */
204			else {
205				emu->ops.release(vp);
206				vp->state = SNDRV_EMUX_ST_RELEASED;
207			}
208		}
209	}
210	if (do_again) {
211		mod_timer(&emu->tlist, jiffies + 1);
 
212		emu->timer_active = 1;
213	} else
214		emu->timer_active = 0;
215	spin_unlock_irqrestore(&emu->voice_lock, flags);
216}
217
218/*
219 * key pressure change
220 */
221void
222snd_emux_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
223{
224	int ch;
225	struct snd_emux *emu;
226	struct snd_emux_voice *vp;
227	unsigned long flags;
228	struct snd_emux_port *port;
229
230	port = p;
231	if (snd_BUG_ON(!port || !chan))
232		return;
233
234	emu = port->emu;
235	if (snd_BUG_ON(!emu || !emu->ops.update))
236		return;
237
238	spin_lock_irqsave(&emu->voice_lock, flags);
239	for (ch = 0; ch < emu->max_voices; ch++) {
240		vp = &emu->voices[ch];
241		if (vp->state == SNDRV_EMUX_ST_ON &&
242		    vp->chan == chan && vp->key == note) {
243			vp->velocity = vel;
244			update_voice(emu, vp, SNDRV_EMUX_UPDATE_VOLUME);
245		}
246	}
247	spin_unlock_irqrestore(&emu->voice_lock, flags);
248}
249
250
251/*
252 * Modulate the voices which belong to the channel
253 */
254void
255snd_emux_update_channel(struct snd_emux_port *port, struct snd_midi_channel *chan, int update)
256{
257	struct snd_emux *emu;
258	struct snd_emux_voice *vp;
259	int i;
260	unsigned long flags;
261
262	if (! update)
263		return;
264
265	emu = port->emu;
266	if (snd_BUG_ON(!emu || !emu->ops.update))
267		return;
268
269	spin_lock_irqsave(&emu->voice_lock, flags);
270	for (i = 0; i < emu->max_voices; i++) {
271		vp = &emu->voices[i];
272		if (vp->chan == chan)
273			update_voice(emu, vp, update);
274	}
275	spin_unlock_irqrestore(&emu->voice_lock, flags);
276}
277
278/*
279 * Modulate all the voices which belong to the port.
280 */
281void
282snd_emux_update_port(struct snd_emux_port *port, int update)
283{
284	struct snd_emux *emu; 
285	struct snd_emux_voice *vp;
286	int i;
287	unsigned long flags;
288
289	if (! update)
290		return;
291
292	emu = port->emu;
293	if (snd_BUG_ON(!emu || !emu->ops.update))
294		return;
295
296	spin_lock_irqsave(&emu->voice_lock, flags);
297	for (i = 0; i < emu->max_voices; i++) {
298		vp = &emu->voices[i];
299		if (vp->port == port)
300			update_voice(emu, vp, update);
301	}
302	spin_unlock_irqrestore(&emu->voice_lock, flags);
303}
304
305
306/*
307 * Deal with a controller type event.  This includes all types of
308 * control events, not just the midi controllers
309 */
310void
311snd_emux_control(void *p, int type, struct snd_midi_channel *chan)
312{
313	struct snd_emux_port *port;
314
315	port = p;
316	if (snd_BUG_ON(!port || !chan))
317		return;
318
319	switch (type) {
320	case MIDI_CTL_MSB_MAIN_VOLUME:
321	case MIDI_CTL_MSB_EXPRESSION:
322		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_VOLUME);
323		break;
324		
325	case MIDI_CTL_MSB_PAN:
326		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PAN);
327		break;
328
329	case MIDI_CTL_SOFT_PEDAL:
330#ifdef SNDRV_EMUX_USE_RAW_EFFECT
331		/* FIXME: this is an emulation */
332		if (chan->control[type] >= 64)
333			snd_emux_send_effect(port, chan, EMUX_FX_CUTOFF, -160,
334				     EMUX_FX_FLAG_ADD);
335		else
336			snd_emux_send_effect(port, chan, EMUX_FX_CUTOFF, 0,
337				     EMUX_FX_FLAG_OFF);
338#endif
339		break;
340
341	case MIDI_CTL_PITCHBEND:
342		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PITCH);
343		break;
344
345	case MIDI_CTL_MSB_MODWHEEL:
346	case MIDI_CTL_CHAN_PRESSURE:
347		snd_emux_update_channel(port, chan,
348					SNDRV_EMUX_UPDATE_FMMOD |
349					SNDRV_EMUX_UPDATE_FM2FRQ2);
350		break;
351
352	}
353
354	if (port->chset.midi_mode == SNDRV_MIDI_MODE_XG) {
355		snd_emux_xg_control(port, chan, type);
356	}
357}
358
359
360/*
361 * terminate note - if free flag is true, free the terminated voice
362 */
363static void
364terminate_note1(struct snd_emux *emu, int note, struct snd_midi_channel *chan, int free)
365{
366	int  i;
367	struct snd_emux_voice *vp;
368	unsigned long flags;
369
370	spin_lock_irqsave(&emu->voice_lock, flags);
371	for (i = 0; i < emu->max_voices; i++) {
372		vp = &emu->voices[i];
373		if (STATE_IS_PLAYING(vp->state) && vp->chan == chan &&
374		    vp->key == note)
375			terminate_voice(emu, vp, free);
376	}
377	spin_unlock_irqrestore(&emu->voice_lock, flags);
378}
379
380
381/*
382 * terminate note - exported for midi emulation
383 */
384void
385snd_emux_terminate_note(void *p, int note, struct snd_midi_channel *chan)
386{
387	struct snd_emux *emu;
388	struct snd_emux_port *port;
389
390	port = p;
391	if (snd_BUG_ON(!port || !chan))
392		return;
393
394	emu = port->emu;
395	if (snd_BUG_ON(!emu || !emu->ops.terminate))
396		return;
397
398	terminate_note1(emu, note, chan, 1);
399}
400
401
402/*
403 * Terminate all the notes
404 */
405void
406snd_emux_terminate_all(struct snd_emux *emu)
407{
408	int i;
409	struct snd_emux_voice *vp;
410	unsigned long flags;
411
412	spin_lock_irqsave(&emu->voice_lock, flags);
413	for (i = 0; i < emu->max_voices; i++) {
414		vp = &emu->voices[i];
415		if (STATE_IS_PLAYING(vp->state))
416			terminate_voice(emu, vp, 0);
417		if (vp->state == SNDRV_EMUX_ST_OFF) {
418			if (emu->ops.free_voice)
419				emu->ops.free_voice(vp);
420			if (emu->ops.reset)
421				emu->ops.reset(emu, i);
422		}
423		vp->time = 0;
424	}
425	/* initialize allocation time */
426	emu->use_time = 0;
427	spin_unlock_irqrestore(&emu->voice_lock, flags);
428}
429
430EXPORT_SYMBOL(snd_emux_terminate_all);
431
432/*
433 * Terminate all voices associated with the given port
434 */
435void
436snd_emux_sounds_off_all(struct snd_emux_port *port)
437{
438	int i;
439	struct snd_emux *emu;
440	struct snd_emux_voice *vp;
441	unsigned long flags;
442
443	if (snd_BUG_ON(!port))
444		return;
445	emu = port->emu;
446	if (snd_BUG_ON(!emu || !emu->ops.terminate))
447		return;
448
449	spin_lock_irqsave(&emu->voice_lock, flags);
450	for (i = 0; i < emu->max_voices; i++) {
451		vp = &emu->voices[i];
452		if (STATE_IS_PLAYING(vp->state) &&
453		    vp->port == port)
454			terminate_voice(emu, vp, 0);
455		if (vp->state == SNDRV_EMUX_ST_OFF) {
456			if (emu->ops.free_voice)
457				emu->ops.free_voice(vp);
458			if (emu->ops.reset)
459				emu->ops.reset(emu, i);
460		}
461	}
462	spin_unlock_irqrestore(&emu->voice_lock, flags);
463}
464
465
466/*
467 * Terminate all voices that have the same exclusive class.  This
468 * is mainly for drums.
469 */
470static void
471exclusive_note_off(struct snd_emux *emu, struct snd_emux_port *port, int exclass)
472{
473	struct snd_emux_voice *vp;
474	int  i;
475	unsigned long flags;
476
477	spin_lock_irqsave(&emu->voice_lock, flags);
478	for (i = 0; i < emu->max_voices; i++) {
479		vp = &emu->voices[i];
480		if (STATE_IS_PLAYING(vp->state) && vp->port == port &&
481		    vp->reg.exclusiveClass == exclass) {
482			terminate_voice(emu, vp, 0);
483		}
484	}
485	spin_unlock_irqrestore(&emu->voice_lock, flags);
486}
487
488/*
489 * terminate a voice
490 * if free flag is true, call free_voice after termination
491 */
492static void
493terminate_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int free)
494{
495	emu->ops.terminate(vp);
496	vp->time = emu->use_time++;
497	vp->chan = NULL;
498	vp->port = NULL;
499	vp->zone = NULL;
500	vp->block = NULL;
501	vp->state = SNDRV_EMUX_ST_OFF;
502	if (free && emu->ops.free_voice)
503		emu->ops.free_voice(vp);
504}
505
506
507/*
508 * Modulate the voice
509 */
510static void
511update_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int update)
512{
513	if (!STATE_IS_PLAYING(vp->state))
514		return;
515
516	if (vp->chan == NULL || vp->port == NULL)
517		return;
518	if (update & SNDRV_EMUX_UPDATE_VOLUME)
519		calc_volume(vp);
520	if (update & SNDRV_EMUX_UPDATE_PITCH)
521		calc_pitch(vp);
522	if (update & SNDRV_EMUX_UPDATE_PAN) {
523		if (! calc_pan(vp) && (update == SNDRV_EMUX_UPDATE_PAN))
524			return;
525	}
526	emu->ops.update(vp, update);
527}
528
529
530#if 0 // not used
531/* table for volume target calculation */
532static const unsigned short voltarget[16] = {
533	0xEAC0, 0xE0C8, 0xD740, 0xCE20, 0xC560, 0xBD08, 0xB500, 0xAD58,
534	0xA5F8, 0x9EF0, 0x9830, 0x91C0, 0x8B90, 0x85A8, 0x8000, 0x7A90
535};
536#endif
537
538#define LO_BYTE(v)	((v) & 0xff)
539#define HI_BYTE(v)	(((v) >> 8) & 0xff)
540
541/*
542 * Sets up the voice structure by calculating some values that
543 * will be needed later.
544 */
545static void
546setup_voice(struct snd_emux_voice *vp)
547{
548	struct soundfont_voice_parm *parm;
549	int pitch;
550
551	/* copy the original register values */
552	vp->reg = vp->zone->v;
553
554#ifdef SNDRV_EMUX_USE_RAW_EFFECT
555	snd_emux_setup_effect(vp);
556#endif
557
558	/* reset status */
559	vp->apan = -1;
560	vp->avol = -1;
561	vp->apitch = -1;
562
563	calc_volume(vp);
564	calc_pitch(vp);
565	calc_pan(vp);
566
567	parm = &vp->reg.parm;
568
569	/* compute filter target and correct modulation parameters */
570	if (LO_BYTE(parm->modatkhld) >= 0x80 && parm->moddelay >= 0x8000) {
571		parm->moddelay = 0xbfff;
572		pitch = (HI_BYTE(parm->pefe) << 4) + vp->apitch;
573		if (pitch > 0xffff)
574			pitch = 0xffff;
575		/* calculate filter target */
576		vp->ftarget = parm->cutoff + LO_BYTE(parm->pefe);
577		LIMITVALUE(vp->ftarget, 0, 255);
578		vp->ftarget <<= 8;
579	} else {
580		vp->ftarget = parm->cutoff;
581		vp->ftarget <<= 8;
582		pitch = vp->apitch;
583	}
584
585	/* compute pitch target */
586	if (pitch != 0xffff) {
587		vp->ptarget = 1 << (pitch >> 12);
588		if (pitch & 0x800) vp->ptarget += (vp->ptarget*0x102e)/0x2710;
589		if (pitch & 0x400) vp->ptarget += (vp->ptarget*0x764)/0x2710;
590		if (pitch & 0x200) vp->ptarget += (vp->ptarget*0x389)/0x2710;
591		vp->ptarget += (vp->ptarget >> 1);
592		if (vp->ptarget > 0xffff) vp->ptarget = 0xffff;
593	} else
594		vp->ptarget = 0xffff;
595
596	if (LO_BYTE(parm->modatkhld) >= 0x80) {
597		parm->modatkhld &= ~0xff;
598		parm->modatkhld |= 0x7f;
599	}
600
601	/* compute volume target and correct volume parameters */
602	vp->vtarget = 0;
603#if 0 /* FIXME: this leads to some clicks.. */
604	if (LO_BYTE(parm->volatkhld) >= 0x80 && parm->voldelay >= 0x8000) {
605		parm->voldelay = 0xbfff;
606		vp->vtarget = voltarget[vp->avol % 0x10] >> (vp->avol >> 4);
607	}
608#endif
609
610	if (LO_BYTE(parm->volatkhld) >= 0x80) {
611		parm->volatkhld &= ~0xff;
612		parm->volatkhld |= 0x7f;
613	}
614}
615
616/*
617 * calculate pitch parameter
618 */
619static const unsigned char pan_volumes[256] = {
6200x00,0x03,0x06,0x09,0x0c,0x0f,0x12,0x14,0x17,0x1a,0x1d,0x20,0x22,0x25,0x28,0x2a,
6210x2d,0x30,0x32,0x35,0x37,0x3a,0x3c,0x3f,0x41,0x44,0x46,0x49,0x4b,0x4d,0x50,0x52,
6220x54,0x57,0x59,0x5b,0x5d,0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6f,0x71,0x73,0x75,
6230x77,0x79,0x7b,0x7c,0x7e,0x80,0x82,0x84,0x86,0x88,0x89,0x8b,0x8d,0x8f,0x90,0x92,
6240x94,0x96,0x97,0x99,0x9a,0x9c,0x9e,0x9f,0xa1,0xa2,0xa4,0xa5,0xa7,0xa8,0xaa,0xab,
6250xad,0xae,0xaf,0xb1,0xb2,0xb3,0xb5,0xb6,0xb7,0xb9,0xba,0xbb,0xbc,0xbe,0xbf,0xc0,
6260xc1,0xc2,0xc3,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,
6270xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xdd,0xde,0xdf,
6280xdf,0xe0,0xe1,0xe2,0xe2,0xe3,0xe4,0xe4,0xe5,0xe6,0xe6,0xe7,0xe8,0xe8,0xe9,0xe9,
6290xea,0xeb,0xeb,0xec,0xec,0xed,0xed,0xee,0xee,0xef,0xef,0xf0,0xf0,0xf1,0xf1,0xf1,
6300xf2,0xf2,0xf3,0xf3,0xf3,0xf4,0xf4,0xf5,0xf5,0xf5,0xf6,0xf6,0xf6,0xf7,0xf7,0xf7,
6310xf7,0xf8,0xf8,0xf8,0xf9,0xf9,0xf9,0xf9,0xf9,0xfa,0xfa,0xfa,0xfa,0xfb,0xfb,0xfb,
6320xfb,0xfb,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,
6330xfd,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,
6340xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
6350xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
636};
637
638static int
639calc_pan(struct snd_emux_voice *vp)
640{
641	struct snd_midi_channel *chan = vp->chan;
642	int pan;
643
644	/* pan & loop start (pan 8bit, MSB, 0:right, 0xff:left) */
645	if (vp->reg.fixpan > 0)	/* 0-127 */
646		pan = 255 - (int)vp->reg.fixpan * 2;
647	else {
648		pan = chan->control[MIDI_CTL_MSB_PAN] - 64;
649		if (vp->reg.pan >= 0) /* 0-127 */
650			pan += vp->reg.pan - 64;
651		pan = 127 - (int)pan * 2;
652	}
653	LIMITVALUE(pan, 0, 255);
654
655	if (vp->emu->linear_panning) {
656		/* assuming linear volume */
657		if (pan != vp->apan) {
658			vp->apan = pan;
659			if (pan == 0)
660				vp->aaux = 0xff;
661			else
662				vp->aaux = (-pan) & 0xff;
663			return 1;
664		} else
665			return 0;
666	} else {
667		/* using volume table */
668		if (vp->apan != (int)pan_volumes[pan]) {
669			vp->apan = pan_volumes[pan];
670			vp->aaux = pan_volumes[255 - pan];
671			return 1;
672		}
673		return 0;
674	}
675}
676
677
678/*
679 * calculate volume attenuation
680 *
681 * Voice volume is controlled by volume attenuation parameter.
682 * So volume becomes maximum when avol is 0 (no attenuation), and
683 * minimum when 255 (-96dB or silence).
684 */
685
686/* tables for volume->attenuation calculation */
687static const unsigned char voltab1[128] = {
688   0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
689   0x63, 0x2b, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22,
690   0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a,
691   0x19, 0x19, 0x18, 0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x14,
692   0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10,
693   0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d,
694   0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
695   0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09,
696   0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06,
697   0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04,
698   0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02,
699   0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
700   0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
701};
702
703static const unsigned char voltab2[128] = {
704   0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x2a,
705   0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x24, 0x23, 0x22, 0x21,
706   0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a,
707   0x1a, 0x19, 0x19, 0x18, 0x18, 0x17, 0x16, 0x16, 0x15, 0x15,
708   0x14, 0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x10,
709   0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d,
710   0x0d, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a,
711   0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
712   0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
713   0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
714   0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
715   0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01,
716   0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
717};
718
719static const unsigned char expressiontab[128] = {
720   0x7f, 0x6c, 0x62, 0x5a, 0x54, 0x50, 0x4b, 0x48, 0x45, 0x42,
721   0x40, 0x3d, 0x3b, 0x39, 0x38, 0x36, 0x34, 0x33, 0x31, 0x30,
722   0x2f, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25,
723   0x24, 0x24, 0x23, 0x22, 0x21, 0x21, 0x20, 0x1f, 0x1e, 0x1e,
724   0x1d, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a, 0x1a, 0x19, 0x18, 0x18,
725   0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x15, 0x14, 0x14, 0x13,
726   0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10, 0x10, 0x0f, 0x0f,
727   0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c,
728   0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09,
729   0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
730   0x06, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03,
731   0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
732   0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
733};
734
735/*
736 * Magic to calculate the volume (actually attenuation) from all the
737 * voice and channels parameters.
738 */
739static int
740calc_volume(struct snd_emux_voice *vp)
741{
742	int vol;
743	int main_vol, expression_vol, master_vol;
744	struct snd_midi_channel *chan = vp->chan;
745	struct snd_emux_port *port = vp->port;
746
747	expression_vol = chan->control[MIDI_CTL_MSB_EXPRESSION];
748	LIMITMAX(vp->velocity, 127);
749	LIMITVALUE(expression_vol, 0, 127);
750	if (port->port_mode == SNDRV_EMUX_PORT_MODE_OSS_SYNTH) {
751		/* 0 - 127 */
752		main_vol = chan->control[MIDI_CTL_MSB_MAIN_VOLUME];
753		vol = (vp->velocity * main_vol * expression_vol) / (127*127);
754		vol = vol * vp->reg.amplitude / 127;
755
756		LIMITVALUE(vol, 0, 127);
757
758		/* calc to attenuation */
759		vol = snd_sf_vol_table[vol];
760
761	} else {
762		main_vol = chan->control[MIDI_CTL_MSB_MAIN_VOLUME] * vp->reg.amplitude / 127;
763		LIMITVALUE(main_vol, 0, 127);
764
765		vol = voltab1[main_vol] + voltab2[vp->velocity];
766		vol = (vol * 8) / 3;
767		vol += vp->reg.attenuation;
768		vol += ((0x100 - vol) * expressiontab[expression_vol])/128;
769	}
770
771	master_vol = port->chset.gs_master_volume;
772	LIMITVALUE(master_vol, 0, 127);
773	vol += snd_sf_vol_table[master_vol];
774	vol += port->volume_atten;
775
776#ifdef SNDRV_EMUX_USE_RAW_EFFECT
777	if (chan->private) {
778		struct snd_emux_effect_table *fx = chan->private;
779		vol += fx->val[EMUX_FX_ATTEN];
780	}
781#endif
782
783	LIMITVALUE(vol, 0, 255);
784	if (vp->avol == vol)
785		return 0; /* value unchanged */
786
787	vp->avol = vol;
788	if (!SF_IS_DRUM_BANK(get_bank(port, chan))
789	    && LO_BYTE(vp->reg.parm.volatkhld) < 0x7d) {
790		int atten;
791		if (vp->velocity < 70)
792			atten = 70;
793		else
794			atten = vp->velocity;
795		vp->acutoff = (atten * vp->reg.parm.cutoff + 0xa0) >> 7;
796	} else {
797		vp->acutoff = vp->reg.parm.cutoff;
798	}
799
800	return 1; /* value changed */
801}
802
803/*
804 * calculate pitch offset
805 *
806 * 0xE000 is no pitch offset at 44100Hz sample.
807 * Every 4096 is one octave.
808 */
809
810static int
811calc_pitch(struct snd_emux_voice *vp)
812{
813	struct snd_midi_channel *chan = vp->chan;
814	int offset;
815
816	/* calculate offset */
817	if (vp->reg.fixkey >= 0) {
818		offset = (vp->reg.fixkey - vp->reg.root) * 4096 / 12;
819	} else {
820		offset = (vp->note - vp->reg.root) * 4096 / 12;
821	}
822	offset = (offset * vp->reg.scaleTuning) / 100;
823	offset += vp->reg.tune * 4096 / 1200;
824	if (chan->midi_pitchbend != 0) {
825		/* (128 * 8192: 1 semitone) ==> (4096: 12 semitones) */
826		offset += chan->midi_pitchbend * chan->gm_rpn_pitch_bend_range / 3072;
827	}
828
829	/* tuning via RPN:
830	 *   coarse = -8192 to 8192 (100 cent per 128)
831	 *   fine = -8192 to 8192 (max=100cent)
832	 */
833	/* 4096 = 1200 cents in emu8000 parameter */
834	offset += chan->gm_rpn_coarse_tuning * 4096 / (12 * 128);
835	offset += chan->gm_rpn_fine_tuning / 24;
836
837#ifdef SNDRV_EMUX_USE_RAW_EFFECT
838	/* add initial pitch correction */
839	if (chan->private) {
840		struct snd_emux_effect_table *fx = chan->private;
841		if (fx->flag[EMUX_FX_INIT_PITCH])
842			offset += fx->val[EMUX_FX_INIT_PITCH];
843	}
844#endif
845
846	/* 0xe000: root pitch */
847	offset += 0xe000 + vp->reg.rate_offset;
848	if (vp->emu->ops.get_pitch_shift)
849		offset += vp->emu->ops.get_pitch_shift(vp->emu);
850	LIMITVALUE(offset, 0, 0xffff);
851	if (offset == vp->apitch)
852		return 0; /* unchanged */
853	vp->apitch = offset;
854	return 1; /* value changed */
855}
856
857/*
858 * Get the bank number assigned to the channel
859 */
860static int
861get_bank(struct snd_emux_port *port, struct snd_midi_channel *chan)
862{
863	int val;
864
865	switch (port->chset.midi_mode) {
866	case SNDRV_MIDI_MODE_XG:
867		val = chan->control[MIDI_CTL_MSB_BANK];
868		if (val == 127)
869			return 128; /* return drum bank */
870		return chan->control[MIDI_CTL_LSB_BANK];
871
872	case SNDRV_MIDI_MODE_GS:
873		if (chan->drum_channel)
874			return 128;
875		/* ignore LSB (bank map) */
876		return chan->control[MIDI_CTL_MSB_BANK];
877		
878	default:
879		if (chan->drum_channel)
880			return 128;
881		return chan->control[MIDI_CTL_MSB_BANK];
882	}
883}
884
885
886/* Look for the zones matching with the given note and velocity.
887 * The resultant zones are stored on table.
888 */
889static int
890get_zone(struct snd_emux *emu, struct snd_emux_port *port,
891	 int *notep, int vel, struct snd_midi_channel *chan,
892	 struct snd_sf_zone **table)
893{
894	int preset, bank, def_preset, def_bank;
895
896	bank = get_bank(port, chan);
897	preset = chan->midi_program;
898
899	if (SF_IS_DRUM_BANK(bank)) {
900		def_preset = port->ctrls[EMUX_MD_DEF_DRUM];
901		def_bank = bank;
902	} else {
903		def_preset = preset;
904		def_bank = port->ctrls[EMUX_MD_DEF_BANK];
905	}
906
907	return snd_soundfont_search_zone(emu->sflist, notep, vel, preset, bank,
908					 def_preset, def_bank,
909					 table, SNDRV_EMUX_MAX_MULTI_VOICES);
910}
911
912/*
913 */
914void
915snd_emux_init_voices(struct snd_emux *emu)
916{
917	struct snd_emux_voice *vp;
918	int i;
919	unsigned long flags;
920
921	spin_lock_irqsave(&emu->voice_lock, flags);
922	for (i = 0; i < emu->max_voices; i++) {
923		vp = &emu->voices[i];
924		vp->ch = -1; /* not used */
925		vp->state = SNDRV_EMUX_ST_OFF;
926		vp->chan = NULL;
927		vp->port = NULL;
928		vp->time = 0;
929		vp->emu = emu;
930		vp->hw = emu->hw;
931	}
932	spin_unlock_irqrestore(&emu->voice_lock, flags);
933}
934
935/*
936 */
937void snd_emux_lock_voice(struct snd_emux *emu, int voice)
938{
939	unsigned long flags;
940
941	spin_lock_irqsave(&emu->voice_lock, flags);
942	if (emu->voices[voice].state == SNDRV_EMUX_ST_OFF)
943		emu->voices[voice].state = SNDRV_EMUX_ST_LOCKED;
944	else
945		dev_warn(emu->card->dev,
946			 "invalid voice for lock %d (state = %x)\n",
947			 voice, emu->voices[voice].state);
948	spin_unlock_irqrestore(&emu->voice_lock, flags);
949}
950
951EXPORT_SYMBOL(snd_emux_lock_voice);
952
953/*
954 */
955void snd_emux_unlock_voice(struct snd_emux *emu, int voice)
956{
957	unsigned long flags;
958
959	spin_lock_irqsave(&emu->voice_lock, flags);
960	if (emu->voices[voice].state == SNDRV_EMUX_ST_LOCKED)
961		emu->voices[voice].state = SNDRV_EMUX_ST_OFF;
962	else
963		dev_warn(emu->card->dev,
964			 "invalid voice for unlock %d (state = %x)\n",
965			 voice, emu->voices[voice].state);
966	spin_unlock_irqrestore(&emu->voice_lock, flags);
967}
968
969EXPORT_SYMBOL(snd_emux_unlock_voice);
v3.15
 
  1/*
  2 *  Midi synth routines for the Emu8k/Emu10k1
  3 *
  4 *  Copyright (C) 1999 Steve Ratcliffe
  5 *  Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
  6 *
  7 *  Contains code based on awe_wave.c by Takashi Iwai
  8 *
  9 *   This program is free software; you can redistribute it and/or modify
 10 *   it under the terms of the GNU General Public License as published by
 11 *   the Free Software Foundation; either version 2 of the License, or
 12 *   (at your option) any later version.
 13 *
 14 *   This program is distributed in the hope that it will be useful,
 15 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 *   GNU General Public License for more details.
 18 *
 19 *   You should have received a copy of the GNU General Public License
 20 *   along with this program; if not, write to the Free Software
 21 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 22 *
 23 */
 24
 25#include <linux/export.h>
 26#include "emux_voice.h"
 27#include <sound/asoundef.h>
 28
 29/*
 30 * Prototypes
 31 */
 32
 33/*
 34 * Ensure a value is between two points
 35 * macro evaluates its args more than once, so changed to upper-case.
 36 */
 37#define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
 38#define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
 39
 40static int get_zone(struct snd_emux *emu, struct snd_emux_port *port,
 41		    int *notep, int vel, struct snd_midi_channel *chan,
 42		    struct snd_sf_zone **table);
 43static int get_bank(struct snd_emux_port *port, struct snd_midi_channel *chan);
 44static void terminate_note1(struct snd_emux *emu, int note,
 45			    struct snd_midi_channel *chan, int free);
 46static void exclusive_note_off(struct snd_emux *emu, struct snd_emux_port *port,
 47			       int exclass);
 48static void terminate_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int free);
 49static void update_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int update);
 50static void setup_voice(struct snd_emux_voice *vp);
 51static int calc_pan(struct snd_emux_voice *vp);
 52static int calc_volume(struct snd_emux_voice *vp);
 53static int calc_pitch(struct snd_emux_voice *vp);
 54
 55
 56/*
 57 * Start a note.
 58 */
 59void
 60snd_emux_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
 61{
 62	struct snd_emux *emu;
 63	int i, key, nvoices;
 64	struct snd_emux_voice *vp;
 65	struct snd_sf_zone *table[SNDRV_EMUX_MAX_MULTI_VOICES];
 66	unsigned long flags;
 67	struct snd_emux_port *port;
 68
 69	port = p;
 70	if (snd_BUG_ON(!port || !chan))
 71		return;
 72
 73	emu = port->emu;
 74	if (snd_BUG_ON(!emu || !emu->ops.get_voice || !emu->ops.trigger))
 75		return;
 76
 77	key = note; /* remember the original note */
 78	nvoices = get_zone(emu, port, &note, vel, chan, table);
 79	if (! nvoices)
 80		return;
 81
 82	/* exclusive note off */
 83	for (i = 0; i < nvoices; i++) {
 84		struct snd_sf_zone *zp = table[i];
 85		if (zp && zp->v.exclusiveClass)
 86			exclusive_note_off(emu, port, zp->v.exclusiveClass);
 87	}
 88
 89#if 0 // seems not necessary
 90	/* Turn off the same note on the same channel. */
 91	terminate_note1(emu, key, chan, 0);
 92#endif
 93
 94	spin_lock_irqsave(&emu->voice_lock, flags);
 95	for (i = 0; i < nvoices; i++) {
 96
 97		/* set up each voice parameter */
 98		/* at this stage, we don't trigger the voice yet. */
 99
100		if (table[i] == NULL)
101			continue;
102
103		vp = emu->ops.get_voice(emu, port);
104		if (vp == NULL || vp->ch < 0)
105			continue;
106		if (STATE_IS_PLAYING(vp->state))
107			emu->ops.terminate(vp);
108
109		vp->time = emu->use_time++;
110		vp->chan = chan;
111		vp->port = port;
112		vp->key = key;
113		vp->note = note;
114		vp->velocity = vel;
115		vp->zone = table[i];
116		if (vp->zone->sample)
117			vp->block = vp->zone->sample->block;
118		else
119			vp->block = NULL;
120
121		setup_voice(vp);
122
123		vp->state = SNDRV_EMUX_ST_STANDBY;
124		if (emu->ops.prepare) {
125			vp->state = SNDRV_EMUX_ST_OFF;
126			if (emu->ops.prepare(vp) >= 0)
127				vp->state = SNDRV_EMUX_ST_STANDBY;
128		}
129	}
130
131	/* start envelope now */
132	for (i = 0; i < emu->max_voices; i++) {
133		vp = &emu->voices[i];
134		if (vp->state == SNDRV_EMUX_ST_STANDBY &&
135		    vp->chan == chan) {
136			emu->ops.trigger(vp);
137			vp->state = SNDRV_EMUX_ST_ON;
138			vp->ontime = jiffies; /* remember the trigger timing */
139		}
140	}
141	spin_unlock_irqrestore(&emu->voice_lock, flags);
142
143#ifdef SNDRV_EMUX_USE_RAW_EFFECT
144	if (port->port_mode == SNDRV_EMUX_PORT_MODE_OSS_SYNTH) {
145		/* clear voice position for the next note on this channel */
146		struct snd_emux_effect_table *fx = chan->private;
147		if (fx) {
148			fx->flag[EMUX_FX_SAMPLE_START] = 0;
149			fx->flag[EMUX_FX_COARSE_SAMPLE_START] = 0;
150		}
151	}
152#endif
153}
154
155/*
156 * Release a note in response to a midi note off.
157 */
158void
159snd_emux_note_off(void *p, int note, int vel, struct snd_midi_channel *chan)
160{
161	int ch;
162	struct snd_emux *emu;
163	struct snd_emux_voice *vp;
164	unsigned long flags;
165	struct snd_emux_port *port;
166
167	port = p;
168	if (snd_BUG_ON(!port || !chan))
169		return;
170
171	emu = port->emu;
172	if (snd_BUG_ON(!emu || !emu->ops.release))
173		return;
174
175	spin_lock_irqsave(&emu->voice_lock, flags);
176	for (ch = 0; ch < emu->max_voices; ch++) {
177		vp = &emu->voices[ch];
178		if (STATE_IS_PLAYING(vp->state) &&
179		    vp->chan == chan && vp->key == note) {
180			vp->state = SNDRV_EMUX_ST_RELEASED;
181			if (vp->ontime == jiffies) {
182				/* if note-off is sent too shortly after
183				 * note-on, emuX engine cannot produce the sound
184				 * correctly.  so we'll release this note
185				 * a bit later via timer callback.
186				 */
187				vp->state = SNDRV_EMUX_ST_PENDING;
188				if (! emu->timer_active) {
189					emu->tlist.expires = jiffies + 1;
190					add_timer(&emu->tlist);
191					emu->timer_active = 1;
192				}
193			} else
194				/* ok now release the note */
195				emu->ops.release(vp);
196		}
197	}
198	spin_unlock_irqrestore(&emu->voice_lock, flags);
199}
200
201/*
202 * timer callback
203 *
204 * release the pending note-offs
205 */
206void snd_emux_timer_callback(unsigned long data)
207{
208	struct snd_emux *emu = (struct snd_emux *) data;
209	struct snd_emux_voice *vp;
210	unsigned long flags;
211	int ch, do_again = 0;
212
213	spin_lock_irqsave(&emu->voice_lock, flags);
214	for (ch = 0; ch < emu->max_voices; ch++) {
215		vp = &emu->voices[ch];
216		if (vp->state == SNDRV_EMUX_ST_PENDING) {
217			if (vp->ontime == jiffies)
218				do_again++; /* release this at the next interrupt */
219			else {
220				emu->ops.release(vp);
221				vp->state = SNDRV_EMUX_ST_RELEASED;
222			}
223		}
224	}
225	if (do_again) {
226		emu->tlist.expires = jiffies + 1;
227		add_timer(&emu->tlist);
228		emu->timer_active = 1;
229	} else
230		emu->timer_active = 0;
231	spin_unlock_irqrestore(&emu->voice_lock, flags);
232}
233
234/*
235 * key pressure change
236 */
237void
238snd_emux_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
239{
240	int ch;
241	struct snd_emux *emu;
242	struct snd_emux_voice *vp;
243	unsigned long flags;
244	struct snd_emux_port *port;
245
246	port = p;
247	if (snd_BUG_ON(!port || !chan))
248		return;
249
250	emu = port->emu;
251	if (snd_BUG_ON(!emu || !emu->ops.update))
252		return;
253
254	spin_lock_irqsave(&emu->voice_lock, flags);
255	for (ch = 0; ch < emu->max_voices; ch++) {
256		vp = &emu->voices[ch];
257		if (vp->state == SNDRV_EMUX_ST_ON &&
258		    vp->chan == chan && vp->key == note) {
259			vp->velocity = vel;
260			update_voice(emu, vp, SNDRV_EMUX_UPDATE_VOLUME);
261		}
262	}
263	spin_unlock_irqrestore(&emu->voice_lock, flags);
264}
265
266
267/*
268 * Modulate the voices which belong to the channel
269 */
270void
271snd_emux_update_channel(struct snd_emux_port *port, struct snd_midi_channel *chan, int update)
272{
273	struct snd_emux *emu;
274	struct snd_emux_voice *vp;
275	int i;
276	unsigned long flags;
277
278	if (! update)
279		return;
280
281	emu = port->emu;
282	if (snd_BUG_ON(!emu || !emu->ops.update))
283		return;
284
285	spin_lock_irqsave(&emu->voice_lock, flags);
286	for (i = 0; i < emu->max_voices; i++) {
287		vp = &emu->voices[i];
288		if (vp->chan == chan)
289			update_voice(emu, vp, update);
290	}
291	spin_unlock_irqrestore(&emu->voice_lock, flags);
292}
293
294/*
295 * Modulate all the voices which belong to the port.
296 */
297void
298snd_emux_update_port(struct snd_emux_port *port, int update)
299{
300	struct snd_emux *emu; 
301	struct snd_emux_voice *vp;
302	int i;
303	unsigned long flags;
304
305	if (! update)
306		return;
307
308	emu = port->emu;
309	if (snd_BUG_ON(!emu || !emu->ops.update))
310		return;
311
312	spin_lock_irqsave(&emu->voice_lock, flags);
313	for (i = 0; i < emu->max_voices; i++) {
314		vp = &emu->voices[i];
315		if (vp->port == port)
316			update_voice(emu, vp, update);
317	}
318	spin_unlock_irqrestore(&emu->voice_lock, flags);
319}
320
321
322/*
323 * Deal with a controller type event.  This includes all types of
324 * control events, not just the midi controllers
325 */
326void
327snd_emux_control(void *p, int type, struct snd_midi_channel *chan)
328{
329	struct snd_emux_port *port;
330
331	port = p;
332	if (snd_BUG_ON(!port || !chan))
333		return;
334
335	switch (type) {
336	case MIDI_CTL_MSB_MAIN_VOLUME:
337	case MIDI_CTL_MSB_EXPRESSION:
338		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_VOLUME);
339		break;
340		
341	case MIDI_CTL_MSB_PAN:
342		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PAN);
343		break;
344
345	case MIDI_CTL_SOFT_PEDAL:
346#ifdef SNDRV_EMUX_USE_RAW_EFFECT
347		/* FIXME: this is an emulation */
348		if (chan->control[type] >= 64)
349			snd_emux_send_effect(port, chan, EMUX_FX_CUTOFF, -160,
350				     EMUX_FX_FLAG_ADD);
351		else
352			snd_emux_send_effect(port, chan, EMUX_FX_CUTOFF, 0,
353				     EMUX_FX_FLAG_OFF);
354#endif
355		break;
356
357	case MIDI_CTL_PITCHBEND:
358		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PITCH);
359		break;
360
361	case MIDI_CTL_MSB_MODWHEEL:
362	case MIDI_CTL_CHAN_PRESSURE:
363		snd_emux_update_channel(port, chan,
364					SNDRV_EMUX_UPDATE_FMMOD |
365					SNDRV_EMUX_UPDATE_FM2FRQ2);
366		break;
367
368	}
369
370	if (port->chset.midi_mode == SNDRV_MIDI_MODE_XG) {
371		snd_emux_xg_control(port, chan, type);
372	}
373}
374
375
376/*
377 * terminate note - if free flag is true, free the terminated voice
378 */
379static void
380terminate_note1(struct snd_emux *emu, int note, struct snd_midi_channel *chan, int free)
381{
382	int  i;
383	struct snd_emux_voice *vp;
384	unsigned long flags;
385
386	spin_lock_irqsave(&emu->voice_lock, flags);
387	for (i = 0; i < emu->max_voices; i++) {
388		vp = &emu->voices[i];
389		if (STATE_IS_PLAYING(vp->state) && vp->chan == chan &&
390		    vp->key == note)
391			terminate_voice(emu, vp, free);
392	}
393	spin_unlock_irqrestore(&emu->voice_lock, flags);
394}
395
396
397/*
398 * terminate note - exported for midi emulation
399 */
400void
401snd_emux_terminate_note(void *p, int note, struct snd_midi_channel *chan)
402{
403	struct snd_emux *emu;
404	struct snd_emux_port *port;
405
406	port = p;
407	if (snd_BUG_ON(!port || !chan))
408		return;
409
410	emu = port->emu;
411	if (snd_BUG_ON(!emu || !emu->ops.terminate))
412		return;
413
414	terminate_note1(emu, note, chan, 1);
415}
416
417
418/*
419 * Terminate all the notes
420 */
421void
422snd_emux_terminate_all(struct snd_emux *emu)
423{
424	int i;
425	struct snd_emux_voice *vp;
426	unsigned long flags;
427
428	spin_lock_irqsave(&emu->voice_lock, flags);
429	for (i = 0; i < emu->max_voices; i++) {
430		vp = &emu->voices[i];
431		if (STATE_IS_PLAYING(vp->state))
432			terminate_voice(emu, vp, 0);
433		if (vp->state == SNDRV_EMUX_ST_OFF) {
434			if (emu->ops.free_voice)
435				emu->ops.free_voice(vp);
436			if (emu->ops.reset)
437				emu->ops.reset(emu, i);
438		}
439		vp->time = 0;
440	}
441	/* initialize allocation time */
442	emu->use_time = 0;
443	spin_unlock_irqrestore(&emu->voice_lock, flags);
444}
445
446EXPORT_SYMBOL(snd_emux_terminate_all);
447
448/*
449 * Terminate all voices associated with the given port
450 */
451void
452snd_emux_sounds_off_all(struct snd_emux_port *port)
453{
454	int i;
455	struct snd_emux *emu;
456	struct snd_emux_voice *vp;
457	unsigned long flags;
458
459	if (snd_BUG_ON(!port))
460		return;
461	emu = port->emu;
462	if (snd_BUG_ON(!emu || !emu->ops.terminate))
463		return;
464
465	spin_lock_irqsave(&emu->voice_lock, flags);
466	for (i = 0; i < emu->max_voices; i++) {
467		vp = &emu->voices[i];
468		if (STATE_IS_PLAYING(vp->state) &&
469		    vp->port == port)
470			terminate_voice(emu, vp, 0);
471		if (vp->state == SNDRV_EMUX_ST_OFF) {
472			if (emu->ops.free_voice)
473				emu->ops.free_voice(vp);
474			if (emu->ops.reset)
475				emu->ops.reset(emu, i);
476		}
477	}
478	spin_unlock_irqrestore(&emu->voice_lock, flags);
479}
480
481
482/*
483 * Terminate all voices that have the same exclusive class.  This
484 * is mainly for drums.
485 */
486static void
487exclusive_note_off(struct snd_emux *emu, struct snd_emux_port *port, int exclass)
488{
489	struct snd_emux_voice *vp;
490	int  i;
491	unsigned long flags;
492
493	spin_lock_irqsave(&emu->voice_lock, flags);
494	for (i = 0; i < emu->max_voices; i++) {
495		vp = &emu->voices[i];
496		if (STATE_IS_PLAYING(vp->state) && vp->port == port &&
497		    vp->reg.exclusiveClass == exclass) {
498			terminate_voice(emu, vp, 0);
499		}
500	}
501	spin_unlock_irqrestore(&emu->voice_lock, flags);
502}
503
504/*
505 * terminate a voice
506 * if free flag is true, call free_voice after termination
507 */
508static void
509terminate_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int free)
510{
511	emu->ops.terminate(vp);
512	vp->time = emu->use_time++;
513	vp->chan = NULL;
514	vp->port = NULL;
515	vp->zone = NULL;
516	vp->block = NULL;
517	vp->state = SNDRV_EMUX_ST_OFF;
518	if (free && emu->ops.free_voice)
519		emu->ops.free_voice(vp);
520}
521
522
523/*
524 * Modulate the voice
525 */
526static void
527update_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int update)
528{
529	if (!STATE_IS_PLAYING(vp->state))
530		return;
531
532	if (vp->chan == NULL || vp->port == NULL)
533		return;
534	if (update & SNDRV_EMUX_UPDATE_VOLUME)
535		calc_volume(vp);
536	if (update & SNDRV_EMUX_UPDATE_PITCH)
537		calc_pitch(vp);
538	if (update & SNDRV_EMUX_UPDATE_PAN) {
539		if (! calc_pan(vp) && (update == SNDRV_EMUX_UPDATE_PAN))
540			return;
541	}
542	emu->ops.update(vp, update);
543}
544
545
546#if 0 // not used
547/* table for volume target calculation */
548static unsigned short voltarget[16] = { 
549	0xEAC0, 0xE0C8, 0xD740, 0xCE20, 0xC560, 0xBD08, 0xB500, 0xAD58,
550	0xA5F8, 0x9EF0, 0x9830, 0x91C0, 0x8B90, 0x85A8, 0x8000, 0x7A90
551};
552#endif
553
554#define LO_BYTE(v)	((v) & 0xff)
555#define HI_BYTE(v)	(((v) >> 8) & 0xff)
556
557/*
558 * Sets up the voice structure by calculating some values that
559 * will be needed later.
560 */
561static void
562setup_voice(struct snd_emux_voice *vp)
563{
564	struct soundfont_voice_parm *parm;
565	int pitch;
566
567	/* copy the original register values */
568	vp->reg = vp->zone->v;
569
570#ifdef SNDRV_EMUX_USE_RAW_EFFECT
571	snd_emux_setup_effect(vp);
572#endif
573
574	/* reset status */
575	vp->apan = -1;
576	vp->avol = -1;
577	vp->apitch = -1;
578
579	calc_volume(vp);
580	calc_pitch(vp);
581	calc_pan(vp);
582
583	parm = &vp->reg.parm;
584
585	/* compute filter target and correct modulation parameters */
586	if (LO_BYTE(parm->modatkhld) >= 0x80 && parm->moddelay >= 0x8000) {
587		parm->moddelay = 0xbfff;
588		pitch = (HI_BYTE(parm->pefe) << 4) + vp->apitch;
589		if (pitch > 0xffff)
590			pitch = 0xffff;
591		/* calculate filter target */
592		vp->ftarget = parm->cutoff + LO_BYTE(parm->pefe);
593		LIMITVALUE(vp->ftarget, 0, 255);
594		vp->ftarget <<= 8;
595	} else {
596		vp->ftarget = parm->cutoff;
597		vp->ftarget <<= 8;
598		pitch = vp->apitch;
599	}
600
601	/* compute pitch target */
602	if (pitch != 0xffff) {
603		vp->ptarget = 1 << (pitch >> 12);
604		if (pitch & 0x800) vp->ptarget += (vp->ptarget*0x102e)/0x2710;
605		if (pitch & 0x400) vp->ptarget += (vp->ptarget*0x764)/0x2710;
606		if (pitch & 0x200) vp->ptarget += (vp->ptarget*0x389)/0x2710;
607		vp->ptarget += (vp->ptarget >> 1);
608		if (vp->ptarget > 0xffff) vp->ptarget = 0xffff;
609	} else
610		vp->ptarget = 0xffff;
611
612	if (LO_BYTE(parm->modatkhld) >= 0x80) {
613		parm->modatkhld &= ~0xff;
614		parm->modatkhld |= 0x7f;
615	}
616
617	/* compute volume target and correct volume parameters */
618	vp->vtarget = 0;
619#if 0 /* FIXME: this leads to some clicks.. */
620	if (LO_BYTE(parm->volatkhld) >= 0x80 && parm->voldelay >= 0x8000) {
621		parm->voldelay = 0xbfff;
622		vp->vtarget = voltarget[vp->avol % 0x10] >> (vp->avol >> 4);
623	}
624#endif
625
626	if (LO_BYTE(parm->volatkhld) >= 0x80) {
627		parm->volatkhld &= ~0xff;
628		parm->volatkhld |= 0x7f;
629	}
630}
631
632/*
633 * calculate pitch parameter
634 */
635static unsigned char pan_volumes[256] = {
6360x00,0x03,0x06,0x09,0x0c,0x0f,0x12,0x14,0x17,0x1a,0x1d,0x20,0x22,0x25,0x28,0x2a,
6370x2d,0x30,0x32,0x35,0x37,0x3a,0x3c,0x3f,0x41,0x44,0x46,0x49,0x4b,0x4d,0x50,0x52,
6380x54,0x57,0x59,0x5b,0x5d,0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6f,0x71,0x73,0x75,
6390x77,0x79,0x7b,0x7c,0x7e,0x80,0x82,0x84,0x86,0x88,0x89,0x8b,0x8d,0x8f,0x90,0x92,
6400x94,0x96,0x97,0x99,0x9a,0x9c,0x9e,0x9f,0xa1,0xa2,0xa4,0xa5,0xa7,0xa8,0xaa,0xab,
6410xad,0xae,0xaf,0xb1,0xb2,0xb3,0xb5,0xb6,0xb7,0xb9,0xba,0xbb,0xbc,0xbe,0xbf,0xc0,
6420xc1,0xc2,0xc3,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,
6430xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xdd,0xde,0xdf,
6440xdf,0xe0,0xe1,0xe2,0xe2,0xe3,0xe4,0xe4,0xe5,0xe6,0xe6,0xe7,0xe8,0xe8,0xe9,0xe9,
6450xea,0xeb,0xeb,0xec,0xec,0xed,0xed,0xee,0xee,0xef,0xef,0xf0,0xf0,0xf1,0xf1,0xf1,
6460xf2,0xf2,0xf3,0xf3,0xf3,0xf4,0xf4,0xf5,0xf5,0xf5,0xf6,0xf6,0xf6,0xf7,0xf7,0xf7,
6470xf7,0xf8,0xf8,0xf8,0xf9,0xf9,0xf9,0xf9,0xf9,0xfa,0xfa,0xfa,0xfa,0xfb,0xfb,0xfb,
6480xfb,0xfb,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,
6490xfd,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,
6500xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
6510xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
652};
653
654static int
655calc_pan(struct snd_emux_voice *vp)
656{
657	struct snd_midi_channel *chan = vp->chan;
658	int pan;
659
660	/* pan & loop start (pan 8bit, MSB, 0:right, 0xff:left) */
661	if (vp->reg.fixpan > 0)	/* 0-127 */
662		pan = 255 - (int)vp->reg.fixpan * 2;
663	else {
664		pan = chan->control[MIDI_CTL_MSB_PAN] - 64;
665		if (vp->reg.pan >= 0) /* 0-127 */
666			pan += vp->reg.pan - 64;
667		pan = 127 - (int)pan * 2;
668	}
669	LIMITVALUE(pan, 0, 255);
670
671	if (vp->emu->linear_panning) {
672		/* assuming linear volume */
673		if (pan != vp->apan) {
674			vp->apan = pan;
675			if (pan == 0)
676				vp->aaux = 0xff;
677			else
678				vp->aaux = (-pan) & 0xff;
679			return 1;
680		} else
681			return 0;
682	} else {
683		/* using volume table */
684		if (vp->apan != (int)pan_volumes[pan]) {
685			vp->apan = pan_volumes[pan];
686			vp->aaux = pan_volumes[255 - pan];
687			return 1;
688		}
689		return 0;
690	}
691}
692
693
694/*
695 * calculate volume attenuation
696 *
697 * Voice volume is controlled by volume attenuation parameter.
698 * So volume becomes maximum when avol is 0 (no attenuation), and
699 * minimum when 255 (-96dB or silence).
700 */
701
702/* tables for volume->attenuation calculation */
703static unsigned char voltab1[128] = {
704   0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
705   0x63, 0x2b, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22,
706   0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a,
707   0x19, 0x19, 0x18, 0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x14,
708   0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10,
709   0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d,
710   0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
711   0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09,
712   0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06,
713   0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04,
714   0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02,
715   0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
716   0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
717};
718
719static unsigned char voltab2[128] = {
720   0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x2a,
721   0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x24, 0x23, 0x22, 0x21,
722   0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a,
723   0x1a, 0x19, 0x19, 0x18, 0x18, 0x17, 0x16, 0x16, 0x15, 0x15,
724   0x14, 0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x10,
725   0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d,
726   0x0d, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a,
727   0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
728   0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
729   0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
730   0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
731   0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01,
732   0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
733};
734
735static unsigned char expressiontab[128] = {
736   0x7f, 0x6c, 0x62, 0x5a, 0x54, 0x50, 0x4b, 0x48, 0x45, 0x42,
737   0x40, 0x3d, 0x3b, 0x39, 0x38, 0x36, 0x34, 0x33, 0x31, 0x30,
738   0x2f, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25,
739   0x24, 0x24, 0x23, 0x22, 0x21, 0x21, 0x20, 0x1f, 0x1e, 0x1e,
740   0x1d, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a, 0x1a, 0x19, 0x18, 0x18,
741   0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x15, 0x14, 0x14, 0x13,
742   0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10, 0x10, 0x0f, 0x0f,
743   0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c,
744   0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09,
745   0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
746   0x06, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03,
747   0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
748   0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
749};
750
751/*
752 * Magic to calculate the volume (actually attenuation) from all the
753 * voice and channels parameters.
754 */
755static int
756calc_volume(struct snd_emux_voice *vp)
757{
758	int vol;
759	int main_vol, expression_vol, master_vol;
760	struct snd_midi_channel *chan = vp->chan;
761	struct snd_emux_port *port = vp->port;
762
763	expression_vol = chan->control[MIDI_CTL_MSB_EXPRESSION];
764	LIMITMAX(vp->velocity, 127);
765	LIMITVALUE(expression_vol, 0, 127);
766	if (port->port_mode == SNDRV_EMUX_PORT_MODE_OSS_SYNTH) {
767		/* 0 - 127 */
768		main_vol = chan->control[MIDI_CTL_MSB_MAIN_VOLUME];
769		vol = (vp->velocity * main_vol * expression_vol) / (127*127);
770		vol = vol * vp->reg.amplitude / 127;
771
772		LIMITVALUE(vol, 0, 127);
773
774		/* calc to attenuation */
775		vol = snd_sf_vol_table[vol];
776
777	} else {
778		main_vol = chan->control[MIDI_CTL_MSB_MAIN_VOLUME] * vp->reg.amplitude / 127;
779		LIMITVALUE(main_vol, 0, 127);
780
781		vol = voltab1[main_vol] + voltab2[vp->velocity];
782		vol = (vol * 8) / 3;
783		vol += vp->reg.attenuation;
784		vol += ((0x100 - vol) * expressiontab[expression_vol])/128;
785	}
786
787	master_vol = port->chset.gs_master_volume;
788	LIMITVALUE(master_vol, 0, 127);
789	vol += snd_sf_vol_table[master_vol];
790	vol += port->volume_atten;
791
792#ifdef SNDRV_EMUX_USE_RAW_EFFECT
793	if (chan->private) {
794		struct snd_emux_effect_table *fx = chan->private;
795		vol += fx->val[EMUX_FX_ATTEN];
796	}
797#endif
798
799	LIMITVALUE(vol, 0, 255);
800	if (vp->avol == vol)
801		return 0; /* value unchanged */
802
803	vp->avol = vol;
804	if (!SF_IS_DRUM_BANK(get_bank(port, chan))
805	    && LO_BYTE(vp->reg.parm.volatkhld) < 0x7d) {
806		int atten;
807		if (vp->velocity < 70)
808			atten = 70;
809		else
810			atten = vp->velocity;
811		vp->acutoff = (atten * vp->reg.parm.cutoff + 0xa0) >> 7;
812	} else {
813		vp->acutoff = vp->reg.parm.cutoff;
814	}
815
816	return 1; /* value changed */
817}
818
819/*
820 * calculate pitch offset
821 *
822 * 0xE000 is no pitch offset at 44100Hz sample.
823 * Every 4096 is one octave.
824 */
825
826static int
827calc_pitch(struct snd_emux_voice *vp)
828{
829	struct snd_midi_channel *chan = vp->chan;
830	int offset;
831
832	/* calculate offset */
833	if (vp->reg.fixkey >= 0) {
834		offset = (vp->reg.fixkey - vp->reg.root) * 4096 / 12;
835	} else {
836		offset = (vp->note - vp->reg.root) * 4096 / 12;
837	}
838	offset = (offset * vp->reg.scaleTuning) / 100;
839	offset += vp->reg.tune * 4096 / 1200;
840	if (chan->midi_pitchbend != 0) {
841		/* (128 * 8192: 1 semitone) ==> (4096: 12 semitones) */
842		offset += chan->midi_pitchbend * chan->gm_rpn_pitch_bend_range / 3072;
843	}
844
845	/* tuning via RPN:
846	 *   coarse = -8192 to 8192 (100 cent per 128)
847	 *   fine = -8192 to 8192 (max=100cent)
848	 */
849	/* 4096 = 1200 cents in emu8000 parameter */
850	offset += chan->gm_rpn_coarse_tuning * 4096 / (12 * 128);
851	offset += chan->gm_rpn_fine_tuning / 24;
852
853#ifdef SNDRV_EMUX_USE_RAW_EFFECT
854	/* add initial pitch correction */
855	if (chan->private) {
856		struct snd_emux_effect_table *fx = chan->private;
857		if (fx->flag[EMUX_FX_INIT_PITCH])
858			offset += fx->val[EMUX_FX_INIT_PITCH];
859	}
860#endif
861
862	/* 0xe000: root pitch */
863	offset += 0xe000 + vp->reg.rate_offset;
864	offset += vp->emu->pitch_shift;
 
865	LIMITVALUE(offset, 0, 0xffff);
866	if (offset == vp->apitch)
867		return 0; /* unchanged */
868	vp->apitch = offset;
869	return 1; /* value changed */
870}
871
872/*
873 * Get the bank number assigned to the channel
874 */
875static int
876get_bank(struct snd_emux_port *port, struct snd_midi_channel *chan)
877{
878	int val;
879
880	switch (port->chset.midi_mode) {
881	case SNDRV_MIDI_MODE_XG:
882		val = chan->control[MIDI_CTL_MSB_BANK];
883		if (val == 127)
884			return 128; /* return drum bank */
885		return chan->control[MIDI_CTL_LSB_BANK];
886
887	case SNDRV_MIDI_MODE_GS:
888		if (chan->drum_channel)
889			return 128;
890		/* ignore LSB (bank map) */
891		return chan->control[MIDI_CTL_MSB_BANK];
892		
893	default:
894		if (chan->drum_channel)
895			return 128;
896		return chan->control[MIDI_CTL_MSB_BANK];
897	}
898}
899
900
901/* Look for the zones matching with the given note and velocity.
902 * The resultant zones are stored on table.
903 */
904static int
905get_zone(struct snd_emux *emu, struct snd_emux_port *port,
906	 int *notep, int vel, struct snd_midi_channel *chan,
907	 struct snd_sf_zone **table)
908{
909	int preset, bank, def_preset, def_bank;
910
911	bank = get_bank(port, chan);
912	preset = chan->midi_program;
913
914	if (SF_IS_DRUM_BANK(bank)) {
915		def_preset = port->ctrls[EMUX_MD_DEF_DRUM];
916		def_bank = bank;
917	} else {
918		def_preset = preset;
919		def_bank = port->ctrls[EMUX_MD_DEF_BANK];
920	}
921
922	return snd_soundfont_search_zone(emu->sflist, notep, vel, preset, bank,
923					 def_preset, def_bank,
924					 table, SNDRV_EMUX_MAX_MULTI_VOICES);
925}
926
927/*
928 */
929void
930snd_emux_init_voices(struct snd_emux *emu)
931{
932	struct snd_emux_voice *vp;
933	int i;
934	unsigned long flags;
935
936	spin_lock_irqsave(&emu->voice_lock, flags);
937	for (i = 0; i < emu->max_voices; i++) {
938		vp = &emu->voices[i];
939		vp->ch = -1; /* not used */
940		vp->state = SNDRV_EMUX_ST_OFF;
941		vp->chan = NULL;
942		vp->port = NULL;
943		vp->time = 0;
944		vp->emu = emu;
945		vp->hw = emu->hw;
946	}
947	spin_unlock_irqrestore(&emu->voice_lock, flags);
948}
949
950/*
951 */
952void snd_emux_lock_voice(struct snd_emux *emu, int voice)
953{
954	unsigned long flags;
955
956	spin_lock_irqsave(&emu->voice_lock, flags);
957	if (emu->voices[voice].state == SNDRV_EMUX_ST_OFF)
958		emu->voices[voice].state = SNDRV_EMUX_ST_LOCKED;
959	else
960		snd_printk(KERN_WARNING
961			   "invalid voice for lock %d (state = %x)\n",
962			   voice, emu->voices[voice].state);
963	spin_unlock_irqrestore(&emu->voice_lock, flags);
964}
965
966EXPORT_SYMBOL(snd_emux_lock_voice);
967
968/*
969 */
970void snd_emux_unlock_voice(struct snd_emux *emu, int voice)
971{
972	unsigned long flags;
973
974	spin_lock_irqsave(&emu->voice_lock, flags);
975	if (emu->voices[voice].state == SNDRV_EMUX_ST_LOCKED)
976		emu->voices[voice].state = SNDRV_EMUX_ST_OFF;
977	else
978		snd_printk(KERN_WARNING
979			   "invalid voice for unlock %d (state = %x)\n",
980			   voice, emu->voices[voice].state);
981	spin_unlock_irqrestore(&emu->voice_lock, flags);
982}
983
984EXPORT_SYMBOL(snd_emux_unlock_voice);