162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  Midi synth routines for the Emu8k/Emu10k1
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Copyright (C) 1999 Steve Ratcliffe
662306a36Sopenharmony_ci *  Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci *  Contains code based on awe_wave.c by Takashi Iwai
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/export.h>
1262306a36Sopenharmony_ci#include "emux_voice.h"
1362306a36Sopenharmony_ci#include <sound/asoundef.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/*
1662306a36Sopenharmony_ci * Prototypes
1762306a36Sopenharmony_ci */
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/*
2062306a36Sopenharmony_ci * Ensure a value is between two points
2162306a36Sopenharmony_ci * macro evaluates its args more than once, so changed to upper-case.
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_ci#define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
2462306a36Sopenharmony_ci#define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistatic int get_zone(struct snd_emux *emu, struct snd_emux_port *port,
2762306a36Sopenharmony_ci		    int *notep, int vel, struct snd_midi_channel *chan,
2862306a36Sopenharmony_ci		    struct snd_sf_zone **table);
2962306a36Sopenharmony_cistatic int get_bank(struct snd_emux_port *port, struct snd_midi_channel *chan);
3062306a36Sopenharmony_cistatic void terminate_note1(struct snd_emux *emu, int note,
3162306a36Sopenharmony_ci			    struct snd_midi_channel *chan, int free);
3262306a36Sopenharmony_cistatic void exclusive_note_off(struct snd_emux *emu, struct snd_emux_port *port,
3362306a36Sopenharmony_ci			       int exclass);
3462306a36Sopenharmony_cistatic void terminate_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int free);
3562306a36Sopenharmony_cistatic void update_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int update);
3662306a36Sopenharmony_cistatic void setup_voice(struct snd_emux_voice *vp);
3762306a36Sopenharmony_cistatic int calc_pan(struct snd_emux_voice *vp);
3862306a36Sopenharmony_cistatic int calc_volume(struct snd_emux_voice *vp);
3962306a36Sopenharmony_cistatic int calc_pitch(struct snd_emux_voice *vp);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci/*
4362306a36Sopenharmony_ci * Start a note.
4462306a36Sopenharmony_ci */
4562306a36Sopenharmony_civoid
4662306a36Sopenharmony_cisnd_emux_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
4762306a36Sopenharmony_ci{
4862306a36Sopenharmony_ci	struct snd_emux *emu;
4962306a36Sopenharmony_ci	int i, key, nvoices;
5062306a36Sopenharmony_ci	struct snd_emux_voice *vp;
5162306a36Sopenharmony_ci	struct snd_sf_zone *table[SNDRV_EMUX_MAX_MULTI_VOICES];
5262306a36Sopenharmony_ci	unsigned long flags;
5362306a36Sopenharmony_ci	struct snd_emux_port *port;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	port = p;
5662306a36Sopenharmony_ci	if (snd_BUG_ON(!port || !chan))
5762306a36Sopenharmony_ci		return;
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	emu = port->emu;
6062306a36Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.get_voice || !emu->ops.trigger))
6162306a36Sopenharmony_ci		return;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	key = note; /* remember the original note */
6462306a36Sopenharmony_ci	nvoices = get_zone(emu, port, &note, vel, chan, table);
6562306a36Sopenharmony_ci	if (! nvoices)
6662306a36Sopenharmony_ci		return;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	/* exclusive note off */
6962306a36Sopenharmony_ci	for (i = 0; i < nvoices; i++) {
7062306a36Sopenharmony_ci		struct snd_sf_zone *zp = table[i];
7162306a36Sopenharmony_ci		if (zp && zp->v.exclusiveClass)
7262306a36Sopenharmony_ci			exclusive_note_off(emu, port, zp->v.exclusiveClass);
7362306a36Sopenharmony_ci	}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci#if 0 // seems not necessary
7662306a36Sopenharmony_ci	/* Turn off the same note on the same channel. */
7762306a36Sopenharmony_ci	terminate_note1(emu, key, chan, 0);
7862306a36Sopenharmony_ci#endif
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
8162306a36Sopenharmony_ci	for (i = 0; i < nvoices; i++) {
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci		/* set up each voice parameter */
8462306a36Sopenharmony_ci		/* at this stage, we don't trigger the voice yet. */
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci		if (table[i] == NULL)
8762306a36Sopenharmony_ci			continue;
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci		vp = emu->ops.get_voice(emu, port);
9062306a36Sopenharmony_ci		if (vp == NULL || vp->ch < 0)
9162306a36Sopenharmony_ci			continue;
9262306a36Sopenharmony_ci		if (STATE_IS_PLAYING(vp->state))
9362306a36Sopenharmony_ci			emu->ops.terminate(vp);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci		vp->time = emu->use_time++;
9662306a36Sopenharmony_ci		vp->chan = chan;
9762306a36Sopenharmony_ci		vp->port = port;
9862306a36Sopenharmony_ci		vp->key = key;
9962306a36Sopenharmony_ci		vp->note = note;
10062306a36Sopenharmony_ci		vp->velocity = vel;
10162306a36Sopenharmony_ci		vp->zone = table[i];
10262306a36Sopenharmony_ci		if (vp->zone->sample)
10362306a36Sopenharmony_ci			vp->block = vp->zone->sample->block;
10462306a36Sopenharmony_ci		else
10562306a36Sopenharmony_ci			vp->block = NULL;
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci		setup_voice(vp);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci		vp->state = SNDRV_EMUX_ST_STANDBY;
11062306a36Sopenharmony_ci		if (emu->ops.prepare) {
11162306a36Sopenharmony_ci			vp->state = SNDRV_EMUX_ST_OFF;
11262306a36Sopenharmony_ci			if (emu->ops.prepare(vp) >= 0)
11362306a36Sopenharmony_ci				vp->state = SNDRV_EMUX_ST_STANDBY;
11462306a36Sopenharmony_ci		}
11562306a36Sopenharmony_ci	}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	/* start envelope now */
11862306a36Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
11962306a36Sopenharmony_ci		vp = &emu->voices[i];
12062306a36Sopenharmony_ci		if (vp->state == SNDRV_EMUX_ST_STANDBY &&
12162306a36Sopenharmony_ci		    vp->chan == chan) {
12262306a36Sopenharmony_ci			emu->ops.trigger(vp);
12362306a36Sopenharmony_ci			vp->state = SNDRV_EMUX_ST_ON;
12462306a36Sopenharmony_ci			vp->ontime = jiffies; /* remember the trigger timing */
12562306a36Sopenharmony_ci		}
12662306a36Sopenharmony_ci	}
12762306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci#ifdef SNDRV_EMUX_USE_RAW_EFFECT
13062306a36Sopenharmony_ci	if (port->port_mode == SNDRV_EMUX_PORT_MODE_OSS_SYNTH) {
13162306a36Sopenharmony_ci		/* clear voice position for the next note on this channel */
13262306a36Sopenharmony_ci		struct snd_emux_effect_table *fx = chan->private;
13362306a36Sopenharmony_ci		if (fx) {
13462306a36Sopenharmony_ci			fx->flag[EMUX_FX_SAMPLE_START] = 0;
13562306a36Sopenharmony_ci			fx->flag[EMUX_FX_COARSE_SAMPLE_START] = 0;
13662306a36Sopenharmony_ci		}
13762306a36Sopenharmony_ci	}
13862306a36Sopenharmony_ci#endif
13962306a36Sopenharmony_ci}
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci/*
14262306a36Sopenharmony_ci * Release a note in response to a midi note off.
14362306a36Sopenharmony_ci */
14462306a36Sopenharmony_civoid
14562306a36Sopenharmony_cisnd_emux_note_off(void *p, int note, int vel, struct snd_midi_channel *chan)
14662306a36Sopenharmony_ci{
14762306a36Sopenharmony_ci	int ch;
14862306a36Sopenharmony_ci	struct snd_emux *emu;
14962306a36Sopenharmony_ci	struct snd_emux_voice *vp;
15062306a36Sopenharmony_ci	unsigned long flags;
15162306a36Sopenharmony_ci	struct snd_emux_port *port;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	port = p;
15462306a36Sopenharmony_ci	if (snd_BUG_ON(!port || !chan))
15562306a36Sopenharmony_ci		return;
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	emu = port->emu;
15862306a36Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.release))
15962306a36Sopenharmony_ci		return;
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
16262306a36Sopenharmony_ci	for (ch = 0; ch < emu->max_voices; ch++) {
16362306a36Sopenharmony_ci		vp = &emu->voices[ch];
16462306a36Sopenharmony_ci		if (STATE_IS_PLAYING(vp->state) &&
16562306a36Sopenharmony_ci		    vp->chan == chan && vp->key == note) {
16662306a36Sopenharmony_ci			vp->state = SNDRV_EMUX_ST_RELEASED;
16762306a36Sopenharmony_ci			if (vp->ontime == jiffies) {
16862306a36Sopenharmony_ci				/* if note-off is sent too shortly after
16962306a36Sopenharmony_ci				 * note-on, emuX engine cannot produce the sound
17062306a36Sopenharmony_ci				 * correctly.  so we'll release this note
17162306a36Sopenharmony_ci				 * a bit later via timer callback.
17262306a36Sopenharmony_ci				 */
17362306a36Sopenharmony_ci				vp->state = SNDRV_EMUX_ST_PENDING;
17462306a36Sopenharmony_ci				if (! emu->timer_active) {
17562306a36Sopenharmony_ci					mod_timer(&emu->tlist, jiffies + 1);
17662306a36Sopenharmony_ci					emu->timer_active = 1;
17762306a36Sopenharmony_ci				}
17862306a36Sopenharmony_ci			} else
17962306a36Sopenharmony_ci				/* ok now release the note */
18062306a36Sopenharmony_ci				emu->ops.release(vp);
18162306a36Sopenharmony_ci		}
18262306a36Sopenharmony_ci	}
18362306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
18462306a36Sopenharmony_ci}
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci/*
18762306a36Sopenharmony_ci * timer callback
18862306a36Sopenharmony_ci *
18962306a36Sopenharmony_ci * release the pending note-offs
19062306a36Sopenharmony_ci */
19162306a36Sopenharmony_civoid snd_emux_timer_callback(struct timer_list *t)
19262306a36Sopenharmony_ci{
19362306a36Sopenharmony_ci	struct snd_emux *emu = from_timer(emu, t, tlist);
19462306a36Sopenharmony_ci	struct snd_emux_voice *vp;
19562306a36Sopenharmony_ci	unsigned long flags;
19662306a36Sopenharmony_ci	int ch, do_again = 0;
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
19962306a36Sopenharmony_ci	for (ch = 0; ch < emu->max_voices; ch++) {
20062306a36Sopenharmony_ci		vp = &emu->voices[ch];
20162306a36Sopenharmony_ci		if (vp->state == SNDRV_EMUX_ST_PENDING) {
20262306a36Sopenharmony_ci			if (vp->ontime == jiffies)
20362306a36Sopenharmony_ci				do_again++; /* release this at the next interrupt */
20462306a36Sopenharmony_ci			else {
20562306a36Sopenharmony_ci				emu->ops.release(vp);
20662306a36Sopenharmony_ci				vp->state = SNDRV_EMUX_ST_RELEASED;
20762306a36Sopenharmony_ci			}
20862306a36Sopenharmony_ci		}
20962306a36Sopenharmony_ci	}
21062306a36Sopenharmony_ci	if (do_again) {
21162306a36Sopenharmony_ci		mod_timer(&emu->tlist, jiffies + 1);
21262306a36Sopenharmony_ci		emu->timer_active = 1;
21362306a36Sopenharmony_ci	} else
21462306a36Sopenharmony_ci		emu->timer_active = 0;
21562306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
21662306a36Sopenharmony_ci}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci/*
21962306a36Sopenharmony_ci * key pressure change
22062306a36Sopenharmony_ci */
22162306a36Sopenharmony_civoid
22262306a36Sopenharmony_cisnd_emux_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
22362306a36Sopenharmony_ci{
22462306a36Sopenharmony_ci	int ch;
22562306a36Sopenharmony_ci	struct snd_emux *emu;
22662306a36Sopenharmony_ci	struct snd_emux_voice *vp;
22762306a36Sopenharmony_ci	unsigned long flags;
22862306a36Sopenharmony_ci	struct snd_emux_port *port;
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	port = p;
23162306a36Sopenharmony_ci	if (snd_BUG_ON(!port || !chan))
23262306a36Sopenharmony_ci		return;
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	emu = port->emu;
23562306a36Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.update))
23662306a36Sopenharmony_ci		return;
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
23962306a36Sopenharmony_ci	for (ch = 0; ch < emu->max_voices; ch++) {
24062306a36Sopenharmony_ci		vp = &emu->voices[ch];
24162306a36Sopenharmony_ci		if (vp->state == SNDRV_EMUX_ST_ON &&
24262306a36Sopenharmony_ci		    vp->chan == chan && vp->key == note) {
24362306a36Sopenharmony_ci			vp->velocity = vel;
24462306a36Sopenharmony_ci			update_voice(emu, vp, SNDRV_EMUX_UPDATE_VOLUME);
24562306a36Sopenharmony_ci		}
24662306a36Sopenharmony_ci	}
24762306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
24862306a36Sopenharmony_ci}
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci/*
25262306a36Sopenharmony_ci * Modulate the voices which belong to the channel
25362306a36Sopenharmony_ci */
25462306a36Sopenharmony_civoid
25562306a36Sopenharmony_cisnd_emux_update_channel(struct snd_emux_port *port, struct snd_midi_channel *chan, int update)
25662306a36Sopenharmony_ci{
25762306a36Sopenharmony_ci	struct snd_emux *emu;
25862306a36Sopenharmony_ci	struct snd_emux_voice *vp;
25962306a36Sopenharmony_ci	int i;
26062306a36Sopenharmony_ci	unsigned long flags;
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci	if (! update)
26362306a36Sopenharmony_ci		return;
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	emu = port->emu;
26662306a36Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.update))
26762306a36Sopenharmony_ci		return;
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
27062306a36Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
27162306a36Sopenharmony_ci		vp = &emu->voices[i];
27262306a36Sopenharmony_ci		if (vp->chan == chan)
27362306a36Sopenharmony_ci			update_voice(emu, vp, update);
27462306a36Sopenharmony_ci	}
27562306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
27662306a36Sopenharmony_ci}
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci/*
27962306a36Sopenharmony_ci * Modulate all the voices which belong to the port.
28062306a36Sopenharmony_ci */
28162306a36Sopenharmony_civoid
28262306a36Sopenharmony_cisnd_emux_update_port(struct snd_emux_port *port, int update)
28362306a36Sopenharmony_ci{
28462306a36Sopenharmony_ci	struct snd_emux *emu;
28562306a36Sopenharmony_ci	struct snd_emux_voice *vp;
28662306a36Sopenharmony_ci	int i;
28762306a36Sopenharmony_ci	unsigned long flags;
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci	if (! update)
29062306a36Sopenharmony_ci		return;
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci	emu = port->emu;
29362306a36Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.update))
29462306a36Sopenharmony_ci		return;
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
29762306a36Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
29862306a36Sopenharmony_ci		vp = &emu->voices[i];
29962306a36Sopenharmony_ci		if (vp->port == port)
30062306a36Sopenharmony_ci			update_voice(emu, vp, update);
30162306a36Sopenharmony_ci	}
30262306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
30362306a36Sopenharmony_ci}
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci/*
30762306a36Sopenharmony_ci * Deal with a controller type event.  This includes all types of
30862306a36Sopenharmony_ci * control events, not just the midi controllers
30962306a36Sopenharmony_ci */
31062306a36Sopenharmony_civoid
31162306a36Sopenharmony_cisnd_emux_control(void *p, int type, struct snd_midi_channel *chan)
31262306a36Sopenharmony_ci{
31362306a36Sopenharmony_ci	struct snd_emux_port *port;
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci	port = p;
31662306a36Sopenharmony_ci	if (snd_BUG_ON(!port || !chan))
31762306a36Sopenharmony_ci		return;
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	switch (type) {
32062306a36Sopenharmony_ci	case MIDI_CTL_MSB_MAIN_VOLUME:
32162306a36Sopenharmony_ci	case MIDI_CTL_MSB_EXPRESSION:
32262306a36Sopenharmony_ci		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_VOLUME);
32362306a36Sopenharmony_ci		break;
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci	case MIDI_CTL_MSB_PAN:
32662306a36Sopenharmony_ci		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PAN);
32762306a36Sopenharmony_ci		break;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	case MIDI_CTL_SOFT_PEDAL:
33062306a36Sopenharmony_ci#ifdef SNDRV_EMUX_USE_RAW_EFFECT
33162306a36Sopenharmony_ci		/* FIXME: this is an emulation */
33262306a36Sopenharmony_ci		if (chan->control[type] >= 64)
33362306a36Sopenharmony_ci			snd_emux_send_effect(port, chan, EMUX_FX_CUTOFF, -160,
33462306a36Sopenharmony_ci				     EMUX_FX_FLAG_ADD);
33562306a36Sopenharmony_ci		else
33662306a36Sopenharmony_ci			snd_emux_send_effect(port, chan, EMUX_FX_CUTOFF, 0,
33762306a36Sopenharmony_ci				     EMUX_FX_FLAG_OFF);
33862306a36Sopenharmony_ci#endif
33962306a36Sopenharmony_ci		break;
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci	case MIDI_CTL_PITCHBEND:
34262306a36Sopenharmony_ci		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PITCH);
34362306a36Sopenharmony_ci		break;
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	case MIDI_CTL_MSB_MODWHEEL:
34662306a36Sopenharmony_ci	case MIDI_CTL_CHAN_PRESSURE:
34762306a36Sopenharmony_ci		snd_emux_update_channel(port, chan,
34862306a36Sopenharmony_ci					SNDRV_EMUX_UPDATE_FMMOD |
34962306a36Sopenharmony_ci					SNDRV_EMUX_UPDATE_FM2FRQ2);
35062306a36Sopenharmony_ci		break;
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	}
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci	if (port->chset.midi_mode == SNDRV_MIDI_MODE_XG) {
35562306a36Sopenharmony_ci		snd_emux_xg_control(port, chan, type);
35662306a36Sopenharmony_ci	}
35762306a36Sopenharmony_ci}
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci/*
36162306a36Sopenharmony_ci * terminate note - if free flag is true, free the terminated voice
36262306a36Sopenharmony_ci */
36362306a36Sopenharmony_cistatic void
36462306a36Sopenharmony_citerminate_note1(struct snd_emux *emu, int note, struct snd_midi_channel *chan, int free)
36562306a36Sopenharmony_ci{
36662306a36Sopenharmony_ci	int  i;
36762306a36Sopenharmony_ci	struct snd_emux_voice *vp;
36862306a36Sopenharmony_ci	unsigned long flags;
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
37162306a36Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
37262306a36Sopenharmony_ci		vp = &emu->voices[i];
37362306a36Sopenharmony_ci		if (STATE_IS_PLAYING(vp->state) && vp->chan == chan &&
37462306a36Sopenharmony_ci		    vp->key == note)
37562306a36Sopenharmony_ci			terminate_voice(emu, vp, free);
37662306a36Sopenharmony_ci	}
37762306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
37862306a36Sopenharmony_ci}
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci/*
38262306a36Sopenharmony_ci * terminate note - exported for midi emulation
38362306a36Sopenharmony_ci */
38462306a36Sopenharmony_civoid
38562306a36Sopenharmony_cisnd_emux_terminate_note(void *p, int note, struct snd_midi_channel *chan)
38662306a36Sopenharmony_ci{
38762306a36Sopenharmony_ci	struct snd_emux *emu;
38862306a36Sopenharmony_ci	struct snd_emux_port *port;
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	port = p;
39162306a36Sopenharmony_ci	if (snd_BUG_ON(!port || !chan))
39262306a36Sopenharmony_ci		return;
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	emu = port->emu;
39562306a36Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.terminate))
39662306a36Sopenharmony_ci		return;
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci	terminate_note1(emu, note, chan, 1);
39962306a36Sopenharmony_ci}
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ci/*
40362306a36Sopenharmony_ci * Terminate all the notes
40462306a36Sopenharmony_ci */
40562306a36Sopenharmony_civoid
40662306a36Sopenharmony_cisnd_emux_terminate_all(struct snd_emux *emu)
40762306a36Sopenharmony_ci{
40862306a36Sopenharmony_ci	int i;
40962306a36Sopenharmony_ci	struct snd_emux_voice *vp;
41062306a36Sopenharmony_ci	unsigned long flags;
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
41362306a36Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
41462306a36Sopenharmony_ci		vp = &emu->voices[i];
41562306a36Sopenharmony_ci		if (STATE_IS_PLAYING(vp->state))
41662306a36Sopenharmony_ci			terminate_voice(emu, vp, 0);
41762306a36Sopenharmony_ci		if (vp->state == SNDRV_EMUX_ST_OFF) {
41862306a36Sopenharmony_ci			if (emu->ops.free_voice)
41962306a36Sopenharmony_ci				emu->ops.free_voice(vp);
42062306a36Sopenharmony_ci			if (emu->ops.reset)
42162306a36Sopenharmony_ci				emu->ops.reset(emu, i);
42262306a36Sopenharmony_ci		}
42362306a36Sopenharmony_ci		vp->time = 0;
42462306a36Sopenharmony_ci	}
42562306a36Sopenharmony_ci	/* initialize allocation time */
42662306a36Sopenharmony_ci	emu->use_time = 0;
42762306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
42862306a36Sopenharmony_ci}
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ciEXPORT_SYMBOL(snd_emux_terminate_all);
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci/*
43362306a36Sopenharmony_ci * Terminate all voices associated with the given port
43462306a36Sopenharmony_ci */
43562306a36Sopenharmony_civoid
43662306a36Sopenharmony_cisnd_emux_sounds_off_all(struct snd_emux_port *port)
43762306a36Sopenharmony_ci{
43862306a36Sopenharmony_ci	int i;
43962306a36Sopenharmony_ci	struct snd_emux *emu;
44062306a36Sopenharmony_ci	struct snd_emux_voice *vp;
44162306a36Sopenharmony_ci	unsigned long flags;
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci	if (snd_BUG_ON(!port))
44462306a36Sopenharmony_ci		return;
44562306a36Sopenharmony_ci	emu = port->emu;
44662306a36Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.terminate))
44762306a36Sopenharmony_ci		return;
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
45062306a36Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
45162306a36Sopenharmony_ci		vp = &emu->voices[i];
45262306a36Sopenharmony_ci		if (STATE_IS_PLAYING(vp->state) &&
45362306a36Sopenharmony_ci		    vp->port == port)
45462306a36Sopenharmony_ci			terminate_voice(emu, vp, 0);
45562306a36Sopenharmony_ci		if (vp->state == SNDRV_EMUX_ST_OFF) {
45662306a36Sopenharmony_ci			if (emu->ops.free_voice)
45762306a36Sopenharmony_ci				emu->ops.free_voice(vp);
45862306a36Sopenharmony_ci			if (emu->ops.reset)
45962306a36Sopenharmony_ci				emu->ops.reset(emu, i);
46062306a36Sopenharmony_ci		}
46162306a36Sopenharmony_ci	}
46262306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
46362306a36Sopenharmony_ci}
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci/*
46762306a36Sopenharmony_ci * Terminate all voices that have the same exclusive class.  This
46862306a36Sopenharmony_ci * is mainly for drums.
46962306a36Sopenharmony_ci */
47062306a36Sopenharmony_cistatic void
47162306a36Sopenharmony_ciexclusive_note_off(struct snd_emux *emu, struct snd_emux_port *port, int exclass)
47262306a36Sopenharmony_ci{
47362306a36Sopenharmony_ci	struct snd_emux_voice *vp;
47462306a36Sopenharmony_ci	int  i;
47562306a36Sopenharmony_ci	unsigned long flags;
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
47862306a36Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
47962306a36Sopenharmony_ci		vp = &emu->voices[i];
48062306a36Sopenharmony_ci		if (STATE_IS_PLAYING(vp->state) && vp->port == port &&
48162306a36Sopenharmony_ci		    vp->reg.exclusiveClass == exclass) {
48262306a36Sopenharmony_ci			terminate_voice(emu, vp, 0);
48362306a36Sopenharmony_ci		}
48462306a36Sopenharmony_ci	}
48562306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
48662306a36Sopenharmony_ci}
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci/*
48962306a36Sopenharmony_ci * terminate a voice
49062306a36Sopenharmony_ci * if free flag is true, call free_voice after termination
49162306a36Sopenharmony_ci */
49262306a36Sopenharmony_cistatic void
49362306a36Sopenharmony_citerminate_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int free)
49462306a36Sopenharmony_ci{
49562306a36Sopenharmony_ci	emu->ops.terminate(vp);
49662306a36Sopenharmony_ci	vp->time = emu->use_time++;
49762306a36Sopenharmony_ci	vp->chan = NULL;
49862306a36Sopenharmony_ci	vp->port = NULL;
49962306a36Sopenharmony_ci	vp->zone = NULL;
50062306a36Sopenharmony_ci	vp->block = NULL;
50162306a36Sopenharmony_ci	vp->state = SNDRV_EMUX_ST_OFF;
50262306a36Sopenharmony_ci	if (free && emu->ops.free_voice)
50362306a36Sopenharmony_ci		emu->ops.free_voice(vp);
50462306a36Sopenharmony_ci}
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci/*
50862306a36Sopenharmony_ci * Modulate the voice
50962306a36Sopenharmony_ci */
51062306a36Sopenharmony_cistatic void
51162306a36Sopenharmony_ciupdate_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int update)
51262306a36Sopenharmony_ci{
51362306a36Sopenharmony_ci	if (!STATE_IS_PLAYING(vp->state))
51462306a36Sopenharmony_ci		return;
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_ci	if (vp->chan == NULL || vp->port == NULL)
51762306a36Sopenharmony_ci		return;
51862306a36Sopenharmony_ci	if (update & SNDRV_EMUX_UPDATE_VOLUME)
51962306a36Sopenharmony_ci		calc_volume(vp);
52062306a36Sopenharmony_ci	if (update & SNDRV_EMUX_UPDATE_PITCH)
52162306a36Sopenharmony_ci		calc_pitch(vp);
52262306a36Sopenharmony_ci	if (update & SNDRV_EMUX_UPDATE_PAN) {
52362306a36Sopenharmony_ci		if (! calc_pan(vp) && (update == SNDRV_EMUX_UPDATE_PAN))
52462306a36Sopenharmony_ci			return;
52562306a36Sopenharmony_ci	}
52662306a36Sopenharmony_ci	emu->ops.update(vp, update);
52762306a36Sopenharmony_ci}
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci#if 0 // not used
53162306a36Sopenharmony_ci/* table for volume target calculation */
53262306a36Sopenharmony_cistatic const unsigned short voltarget[16] = {
53362306a36Sopenharmony_ci	0xEAC0, 0xE0C8, 0xD740, 0xCE20, 0xC560, 0xBD08, 0xB500, 0xAD58,
53462306a36Sopenharmony_ci	0xA5F8, 0x9EF0, 0x9830, 0x91C0, 0x8B90, 0x85A8, 0x8000, 0x7A90
53562306a36Sopenharmony_ci};
53662306a36Sopenharmony_ci#endif
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ci#define LO_BYTE(v)	((v) & 0xff)
53962306a36Sopenharmony_ci#define HI_BYTE(v)	(((v) >> 8) & 0xff)
54062306a36Sopenharmony_ci
54162306a36Sopenharmony_ci/*
54262306a36Sopenharmony_ci * Sets up the voice structure by calculating some values that
54362306a36Sopenharmony_ci * will be needed later.
54462306a36Sopenharmony_ci */
54562306a36Sopenharmony_cistatic void
54662306a36Sopenharmony_cisetup_voice(struct snd_emux_voice *vp)
54762306a36Sopenharmony_ci{
54862306a36Sopenharmony_ci	struct soundfont_voice_parm *parm;
54962306a36Sopenharmony_ci	int pitch;
55062306a36Sopenharmony_ci
55162306a36Sopenharmony_ci	/* copy the original register values */
55262306a36Sopenharmony_ci	vp->reg = vp->zone->v;
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci#ifdef SNDRV_EMUX_USE_RAW_EFFECT
55562306a36Sopenharmony_ci	snd_emux_setup_effect(vp);
55662306a36Sopenharmony_ci#endif
55762306a36Sopenharmony_ci
55862306a36Sopenharmony_ci	/* reset status */
55962306a36Sopenharmony_ci	vp->apan = -1;
56062306a36Sopenharmony_ci	vp->avol = -1;
56162306a36Sopenharmony_ci	vp->apitch = -1;
56262306a36Sopenharmony_ci
56362306a36Sopenharmony_ci	calc_volume(vp);
56462306a36Sopenharmony_ci	calc_pitch(vp);
56562306a36Sopenharmony_ci	calc_pan(vp);
56662306a36Sopenharmony_ci
56762306a36Sopenharmony_ci	parm = &vp->reg.parm;
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci	/* compute filter target and correct modulation parameters */
57062306a36Sopenharmony_ci	if (LO_BYTE(parm->modatkhld) >= 0x80 && parm->moddelay >= 0x8000) {
57162306a36Sopenharmony_ci		parm->moddelay = 0xbfff;
57262306a36Sopenharmony_ci		pitch = (HI_BYTE(parm->pefe) << 4) + vp->apitch;
57362306a36Sopenharmony_ci		if (pitch > 0xffff)
57462306a36Sopenharmony_ci			pitch = 0xffff;
57562306a36Sopenharmony_ci		/* calculate filter target */
57662306a36Sopenharmony_ci		vp->ftarget = parm->cutoff + LO_BYTE(parm->pefe);
57762306a36Sopenharmony_ci		LIMITVALUE(vp->ftarget, 0, 255);
57862306a36Sopenharmony_ci		vp->ftarget <<= 8;
57962306a36Sopenharmony_ci	} else {
58062306a36Sopenharmony_ci		vp->ftarget = parm->cutoff;
58162306a36Sopenharmony_ci		vp->ftarget <<= 8;
58262306a36Sopenharmony_ci		pitch = vp->apitch;
58362306a36Sopenharmony_ci	}
58462306a36Sopenharmony_ci
58562306a36Sopenharmony_ci	/* compute pitch target */
58662306a36Sopenharmony_ci	if (pitch != 0xffff) {
58762306a36Sopenharmony_ci		vp->ptarget = 1 << (pitch >> 12);
58862306a36Sopenharmony_ci		if (pitch & 0x800) vp->ptarget += (vp->ptarget*0x102e)/0x2710;
58962306a36Sopenharmony_ci		if (pitch & 0x400) vp->ptarget += (vp->ptarget*0x764)/0x2710;
59062306a36Sopenharmony_ci		if (pitch & 0x200) vp->ptarget += (vp->ptarget*0x389)/0x2710;
59162306a36Sopenharmony_ci		vp->ptarget += (vp->ptarget >> 1);
59262306a36Sopenharmony_ci		if (vp->ptarget > 0xffff) vp->ptarget = 0xffff;
59362306a36Sopenharmony_ci	} else
59462306a36Sopenharmony_ci		vp->ptarget = 0xffff;
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_ci	if (LO_BYTE(parm->modatkhld) >= 0x80) {
59762306a36Sopenharmony_ci		parm->modatkhld &= ~0xff;
59862306a36Sopenharmony_ci		parm->modatkhld |= 0x7f;
59962306a36Sopenharmony_ci	}
60062306a36Sopenharmony_ci
60162306a36Sopenharmony_ci	/* compute volume target and correct volume parameters */
60262306a36Sopenharmony_ci	vp->vtarget = 0;
60362306a36Sopenharmony_ci#if 0 /* FIXME: this leads to some clicks.. */
60462306a36Sopenharmony_ci	if (LO_BYTE(parm->volatkhld) >= 0x80 && parm->voldelay >= 0x8000) {
60562306a36Sopenharmony_ci		parm->voldelay = 0xbfff;
60662306a36Sopenharmony_ci		vp->vtarget = voltarget[vp->avol % 0x10] >> (vp->avol >> 4);
60762306a36Sopenharmony_ci	}
60862306a36Sopenharmony_ci#endif
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci	if (LO_BYTE(parm->volatkhld) >= 0x80) {
61162306a36Sopenharmony_ci		parm->volatkhld &= ~0xff;
61262306a36Sopenharmony_ci		parm->volatkhld |= 0x7f;
61362306a36Sopenharmony_ci	}
61462306a36Sopenharmony_ci}
61562306a36Sopenharmony_ci
61662306a36Sopenharmony_ci/*
61762306a36Sopenharmony_ci * calculate pitch parameter
61862306a36Sopenharmony_ci */
61962306a36Sopenharmony_cistatic const unsigned char pan_volumes[256] = {
62062306a36Sopenharmony_ci0x00,0x03,0x06,0x09,0x0c,0x0f,0x12,0x14,0x17,0x1a,0x1d,0x20,0x22,0x25,0x28,0x2a,
62162306a36Sopenharmony_ci0x2d,0x30,0x32,0x35,0x37,0x3a,0x3c,0x3f,0x41,0x44,0x46,0x49,0x4b,0x4d,0x50,0x52,
62262306a36Sopenharmony_ci0x54,0x57,0x59,0x5b,0x5d,0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6f,0x71,0x73,0x75,
62362306a36Sopenharmony_ci0x77,0x79,0x7b,0x7c,0x7e,0x80,0x82,0x84,0x86,0x88,0x89,0x8b,0x8d,0x8f,0x90,0x92,
62462306a36Sopenharmony_ci0x94,0x96,0x97,0x99,0x9a,0x9c,0x9e,0x9f,0xa1,0xa2,0xa4,0xa5,0xa7,0xa8,0xaa,0xab,
62562306a36Sopenharmony_ci0xad,0xae,0xaf,0xb1,0xb2,0xb3,0xb5,0xb6,0xb7,0xb9,0xba,0xbb,0xbc,0xbe,0xbf,0xc0,
62662306a36Sopenharmony_ci0xc1,0xc2,0xc3,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,
62762306a36Sopenharmony_ci0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xdd,0xde,0xdf,
62862306a36Sopenharmony_ci0xdf,0xe0,0xe1,0xe2,0xe2,0xe3,0xe4,0xe4,0xe5,0xe6,0xe6,0xe7,0xe8,0xe8,0xe9,0xe9,
62962306a36Sopenharmony_ci0xea,0xeb,0xeb,0xec,0xec,0xed,0xed,0xee,0xee,0xef,0xef,0xf0,0xf0,0xf1,0xf1,0xf1,
63062306a36Sopenharmony_ci0xf2,0xf2,0xf3,0xf3,0xf3,0xf4,0xf4,0xf5,0xf5,0xf5,0xf6,0xf6,0xf6,0xf7,0xf7,0xf7,
63162306a36Sopenharmony_ci0xf7,0xf8,0xf8,0xf8,0xf9,0xf9,0xf9,0xf9,0xf9,0xfa,0xfa,0xfa,0xfa,0xfb,0xfb,0xfb,
63262306a36Sopenharmony_ci0xfb,0xfb,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,
63362306a36Sopenharmony_ci0xfd,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,
63462306a36Sopenharmony_ci0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
63562306a36Sopenharmony_ci0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
63662306a36Sopenharmony_ci};
63762306a36Sopenharmony_ci
63862306a36Sopenharmony_cistatic int
63962306a36Sopenharmony_cicalc_pan(struct snd_emux_voice *vp)
64062306a36Sopenharmony_ci{
64162306a36Sopenharmony_ci	struct snd_midi_channel *chan = vp->chan;
64262306a36Sopenharmony_ci	int pan;
64362306a36Sopenharmony_ci
64462306a36Sopenharmony_ci	/* pan & loop start (pan 8bit, MSB, 0:right, 0xff:left) */
64562306a36Sopenharmony_ci	if (vp->reg.fixpan > 0)	/* 0-127 */
64662306a36Sopenharmony_ci		pan = 255 - (int)vp->reg.fixpan * 2;
64762306a36Sopenharmony_ci	else {
64862306a36Sopenharmony_ci		pan = chan->control[MIDI_CTL_MSB_PAN] - 64;
64962306a36Sopenharmony_ci		if (vp->reg.pan >= 0) /* 0-127 */
65062306a36Sopenharmony_ci			pan += vp->reg.pan - 64;
65162306a36Sopenharmony_ci		pan = 127 - (int)pan * 2;
65262306a36Sopenharmony_ci	}
65362306a36Sopenharmony_ci	LIMITVALUE(pan, 0, 255);
65462306a36Sopenharmony_ci
65562306a36Sopenharmony_ci	if (vp->emu->linear_panning) {
65662306a36Sopenharmony_ci		/* assuming linear volume */
65762306a36Sopenharmony_ci		if (pan != vp->apan) {
65862306a36Sopenharmony_ci			vp->apan = pan;
65962306a36Sopenharmony_ci			if (pan == 0)
66062306a36Sopenharmony_ci				vp->aaux = 0xff;
66162306a36Sopenharmony_ci			else
66262306a36Sopenharmony_ci				vp->aaux = (-pan) & 0xff;
66362306a36Sopenharmony_ci			return 1;
66462306a36Sopenharmony_ci		} else
66562306a36Sopenharmony_ci			return 0;
66662306a36Sopenharmony_ci	} else {
66762306a36Sopenharmony_ci		/* using volume table */
66862306a36Sopenharmony_ci		if (vp->apan != (int)pan_volumes[pan]) {
66962306a36Sopenharmony_ci			vp->apan = pan_volumes[pan];
67062306a36Sopenharmony_ci			vp->aaux = pan_volumes[255 - pan];
67162306a36Sopenharmony_ci			return 1;
67262306a36Sopenharmony_ci		}
67362306a36Sopenharmony_ci		return 0;
67462306a36Sopenharmony_ci	}
67562306a36Sopenharmony_ci}
67662306a36Sopenharmony_ci
67762306a36Sopenharmony_ci
67862306a36Sopenharmony_ci/*
67962306a36Sopenharmony_ci * calculate volume attenuation
68062306a36Sopenharmony_ci *
68162306a36Sopenharmony_ci * Voice volume is controlled by volume attenuation parameter.
68262306a36Sopenharmony_ci * So volume becomes maximum when avol is 0 (no attenuation), and
68362306a36Sopenharmony_ci * minimum when 255 (-96dB or silence).
68462306a36Sopenharmony_ci */
68562306a36Sopenharmony_ci
68662306a36Sopenharmony_ci/* tables for volume->attenuation calculation */
68762306a36Sopenharmony_cistatic const unsigned char voltab1[128] = {
68862306a36Sopenharmony_ci   0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
68962306a36Sopenharmony_ci   0x63, 0x2b, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22,
69062306a36Sopenharmony_ci   0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a,
69162306a36Sopenharmony_ci   0x19, 0x19, 0x18, 0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x14,
69262306a36Sopenharmony_ci   0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10,
69362306a36Sopenharmony_ci   0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d,
69462306a36Sopenharmony_ci   0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
69562306a36Sopenharmony_ci   0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09,
69662306a36Sopenharmony_ci   0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06,
69762306a36Sopenharmony_ci   0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04,
69862306a36Sopenharmony_ci   0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02,
69962306a36Sopenharmony_ci   0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
70062306a36Sopenharmony_ci   0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
70162306a36Sopenharmony_ci};
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_cistatic const unsigned char voltab2[128] = {
70462306a36Sopenharmony_ci   0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x2a,
70562306a36Sopenharmony_ci   0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x24, 0x23, 0x22, 0x21,
70662306a36Sopenharmony_ci   0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a,
70762306a36Sopenharmony_ci   0x1a, 0x19, 0x19, 0x18, 0x18, 0x17, 0x16, 0x16, 0x15, 0x15,
70862306a36Sopenharmony_ci   0x14, 0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x10,
70962306a36Sopenharmony_ci   0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d,
71062306a36Sopenharmony_ci   0x0d, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a,
71162306a36Sopenharmony_ci   0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
71262306a36Sopenharmony_ci   0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
71362306a36Sopenharmony_ci   0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
71462306a36Sopenharmony_ci   0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
71562306a36Sopenharmony_ci   0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01,
71662306a36Sopenharmony_ci   0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
71762306a36Sopenharmony_ci};
71862306a36Sopenharmony_ci
71962306a36Sopenharmony_cistatic const unsigned char expressiontab[128] = {
72062306a36Sopenharmony_ci   0x7f, 0x6c, 0x62, 0x5a, 0x54, 0x50, 0x4b, 0x48, 0x45, 0x42,
72162306a36Sopenharmony_ci   0x40, 0x3d, 0x3b, 0x39, 0x38, 0x36, 0x34, 0x33, 0x31, 0x30,
72262306a36Sopenharmony_ci   0x2f, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25,
72362306a36Sopenharmony_ci   0x24, 0x24, 0x23, 0x22, 0x21, 0x21, 0x20, 0x1f, 0x1e, 0x1e,
72462306a36Sopenharmony_ci   0x1d, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a, 0x1a, 0x19, 0x18, 0x18,
72562306a36Sopenharmony_ci   0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x15, 0x14, 0x14, 0x13,
72662306a36Sopenharmony_ci   0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10, 0x10, 0x0f, 0x0f,
72762306a36Sopenharmony_ci   0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c,
72862306a36Sopenharmony_ci   0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09,
72962306a36Sopenharmony_ci   0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
73062306a36Sopenharmony_ci   0x06, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03,
73162306a36Sopenharmony_ci   0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
73262306a36Sopenharmony_ci   0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
73362306a36Sopenharmony_ci};
73462306a36Sopenharmony_ci
73562306a36Sopenharmony_ci/*
73662306a36Sopenharmony_ci * Magic to calculate the volume (actually attenuation) from all the
73762306a36Sopenharmony_ci * voice and channels parameters.
73862306a36Sopenharmony_ci */
73962306a36Sopenharmony_cistatic int
74062306a36Sopenharmony_cicalc_volume(struct snd_emux_voice *vp)
74162306a36Sopenharmony_ci{
74262306a36Sopenharmony_ci	int vol;
74362306a36Sopenharmony_ci	int main_vol, expression_vol, master_vol;
74462306a36Sopenharmony_ci	struct snd_midi_channel *chan = vp->chan;
74562306a36Sopenharmony_ci	struct snd_emux_port *port = vp->port;
74662306a36Sopenharmony_ci
74762306a36Sopenharmony_ci	expression_vol = chan->control[MIDI_CTL_MSB_EXPRESSION];
74862306a36Sopenharmony_ci	LIMITMAX(vp->velocity, 127);
74962306a36Sopenharmony_ci	LIMITVALUE(expression_vol, 0, 127);
75062306a36Sopenharmony_ci	if (port->port_mode == SNDRV_EMUX_PORT_MODE_OSS_SYNTH) {
75162306a36Sopenharmony_ci		/* 0 - 127 */
75262306a36Sopenharmony_ci		main_vol = chan->control[MIDI_CTL_MSB_MAIN_VOLUME];
75362306a36Sopenharmony_ci		vol = (vp->velocity * main_vol * expression_vol) / (127*127);
75462306a36Sopenharmony_ci		vol = vol * vp->reg.amplitude / 127;
75562306a36Sopenharmony_ci
75662306a36Sopenharmony_ci		LIMITVALUE(vol, 0, 127);
75762306a36Sopenharmony_ci
75862306a36Sopenharmony_ci		/* calc to attenuation */
75962306a36Sopenharmony_ci		vol = snd_sf_vol_table[vol];
76062306a36Sopenharmony_ci
76162306a36Sopenharmony_ci	} else {
76262306a36Sopenharmony_ci		main_vol = chan->control[MIDI_CTL_MSB_MAIN_VOLUME] * vp->reg.amplitude / 127;
76362306a36Sopenharmony_ci		LIMITVALUE(main_vol, 0, 127);
76462306a36Sopenharmony_ci
76562306a36Sopenharmony_ci		vol = voltab1[main_vol] + voltab2[vp->velocity];
76662306a36Sopenharmony_ci		vol = (vol * 8) / 3;
76762306a36Sopenharmony_ci		vol += vp->reg.attenuation;
76862306a36Sopenharmony_ci		vol += ((0x100 - vol) * expressiontab[expression_vol])/128;
76962306a36Sopenharmony_ci	}
77062306a36Sopenharmony_ci
77162306a36Sopenharmony_ci	master_vol = port->chset.gs_master_volume;
77262306a36Sopenharmony_ci	LIMITVALUE(master_vol, 0, 127);
77362306a36Sopenharmony_ci	vol += snd_sf_vol_table[master_vol];
77462306a36Sopenharmony_ci	vol += port->volume_atten;
77562306a36Sopenharmony_ci
77662306a36Sopenharmony_ci#ifdef SNDRV_EMUX_USE_RAW_EFFECT
77762306a36Sopenharmony_ci	if (chan->private) {
77862306a36Sopenharmony_ci		struct snd_emux_effect_table *fx = chan->private;
77962306a36Sopenharmony_ci		vol += fx->val[EMUX_FX_ATTEN];
78062306a36Sopenharmony_ci	}
78162306a36Sopenharmony_ci#endif
78262306a36Sopenharmony_ci
78362306a36Sopenharmony_ci	LIMITVALUE(vol, 0, 255);
78462306a36Sopenharmony_ci	if (vp->avol == vol)
78562306a36Sopenharmony_ci		return 0; /* value unchanged */
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci	vp->avol = vol;
78862306a36Sopenharmony_ci	if (!SF_IS_DRUM_BANK(get_bank(port, chan))
78962306a36Sopenharmony_ci	    && LO_BYTE(vp->reg.parm.volatkhld) < 0x7d) {
79062306a36Sopenharmony_ci		int atten;
79162306a36Sopenharmony_ci		if (vp->velocity < 70)
79262306a36Sopenharmony_ci			atten = 70;
79362306a36Sopenharmony_ci		else
79462306a36Sopenharmony_ci			atten = vp->velocity;
79562306a36Sopenharmony_ci		vp->acutoff = (atten * vp->reg.parm.cutoff + 0xa0) >> 7;
79662306a36Sopenharmony_ci	} else {
79762306a36Sopenharmony_ci		vp->acutoff = vp->reg.parm.cutoff;
79862306a36Sopenharmony_ci	}
79962306a36Sopenharmony_ci
80062306a36Sopenharmony_ci	return 1; /* value changed */
80162306a36Sopenharmony_ci}
80262306a36Sopenharmony_ci
80362306a36Sopenharmony_ci/*
80462306a36Sopenharmony_ci * calculate pitch offset
80562306a36Sopenharmony_ci *
80662306a36Sopenharmony_ci * 0xE000 is no pitch offset at 44100Hz sample.
80762306a36Sopenharmony_ci * Every 4096 is one octave.
80862306a36Sopenharmony_ci */
80962306a36Sopenharmony_ci
81062306a36Sopenharmony_cistatic int
81162306a36Sopenharmony_cicalc_pitch(struct snd_emux_voice *vp)
81262306a36Sopenharmony_ci{
81362306a36Sopenharmony_ci	struct snd_midi_channel *chan = vp->chan;
81462306a36Sopenharmony_ci	int offset;
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_ci	/* calculate offset */
81762306a36Sopenharmony_ci	if (vp->reg.fixkey >= 0) {
81862306a36Sopenharmony_ci		offset = (vp->reg.fixkey - vp->reg.root) * 4096 / 12;
81962306a36Sopenharmony_ci	} else {
82062306a36Sopenharmony_ci		offset = (vp->note - vp->reg.root) * 4096 / 12;
82162306a36Sopenharmony_ci	}
82262306a36Sopenharmony_ci	offset = (offset * vp->reg.scaleTuning) / 100;
82362306a36Sopenharmony_ci	offset += vp->reg.tune * 4096 / 1200;
82462306a36Sopenharmony_ci	if (chan->midi_pitchbend != 0) {
82562306a36Sopenharmony_ci		/* (128 * 8192: 1 semitone) ==> (4096: 12 semitones) */
82662306a36Sopenharmony_ci		offset += chan->midi_pitchbend * chan->gm_rpn_pitch_bend_range / 3072;
82762306a36Sopenharmony_ci	}
82862306a36Sopenharmony_ci
82962306a36Sopenharmony_ci	/* tuning via RPN:
83062306a36Sopenharmony_ci	 *   coarse = -8192 to 8192 (100 cent per 128)
83162306a36Sopenharmony_ci	 *   fine = -8192 to 8192 (max=100cent)
83262306a36Sopenharmony_ci	 */
83362306a36Sopenharmony_ci	/* 4096 = 1200 cents in emu8000 parameter */
83462306a36Sopenharmony_ci	offset += chan->gm_rpn_coarse_tuning * 4096 / (12 * 128);
83562306a36Sopenharmony_ci	offset += chan->gm_rpn_fine_tuning / 24;
83662306a36Sopenharmony_ci
83762306a36Sopenharmony_ci#ifdef SNDRV_EMUX_USE_RAW_EFFECT
83862306a36Sopenharmony_ci	/* add initial pitch correction */
83962306a36Sopenharmony_ci	if (chan->private) {
84062306a36Sopenharmony_ci		struct snd_emux_effect_table *fx = chan->private;
84162306a36Sopenharmony_ci		if (fx->flag[EMUX_FX_INIT_PITCH])
84262306a36Sopenharmony_ci			offset += fx->val[EMUX_FX_INIT_PITCH];
84362306a36Sopenharmony_ci	}
84462306a36Sopenharmony_ci#endif
84562306a36Sopenharmony_ci
84662306a36Sopenharmony_ci	/* 0xe000: root pitch */
84762306a36Sopenharmony_ci	offset += 0xe000 + vp->reg.rate_offset;
84862306a36Sopenharmony_ci	if (vp->emu->ops.get_pitch_shift)
84962306a36Sopenharmony_ci		offset += vp->emu->ops.get_pitch_shift(vp->emu);
85062306a36Sopenharmony_ci	LIMITVALUE(offset, 0, 0xffff);
85162306a36Sopenharmony_ci	if (offset == vp->apitch)
85262306a36Sopenharmony_ci		return 0; /* unchanged */
85362306a36Sopenharmony_ci	vp->apitch = offset;
85462306a36Sopenharmony_ci	return 1; /* value changed */
85562306a36Sopenharmony_ci}
85662306a36Sopenharmony_ci
85762306a36Sopenharmony_ci/*
85862306a36Sopenharmony_ci * Get the bank number assigned to the channel
85962306a36Sopenharmony_ci */
86062306a36Sopenharmony_cistatic int
86162306a36Sopenharmony_ciget_bank(struct snd_emux_port *port, struct snd_midi_channel *chan)
86262306a36Sopenharmony_ci{
86362306a36Sopenharmony_ci	int val;
86462306a36Sopenharmony_ci
86562306a36Sopenharmony_ci	switch (port->chset.midi_mode) {
86662306a36Sopenharmony_ci	case SNDRV_MIDI_MODE_XG:
86762306a36Sopenharmony_ci		val = chan->control[MIDI_CTL_MSB_BANK];
86862306a36Sopenharmony_ci		if (val == 127)
86962306a36Sopenharmony_ci			return 128; /* return drum bank */
87062306a36Sopenharmony_ci		return chan->control[MIDI_CTL_LSB_BANK];
87162306a36Sopenharmony_ci
87262306a36Sopenharmony_ci	case SNDRV_MIDI_MODE_GS:
87362306a36Sopenharmony_ci		if (chan->drum_channel)
87462306a36Sopenharmony_ci			return 128;
87562306a36Sopenharmony_ci		/* ignore LSB (bank map) */
87662306a36Sopenharmony_ci		return chan->control[MIDI_CTL_MSB_BANK];
87762306a36Sopenharmony_ci
87862306a36Sopenharmony_ci	default:
87962306a36Sopenharmony_ci		if (chan->drum_channel)
88062306a36Sopenharmony_ci			return 128;
88162306a36Sopenharmony_ci		return chan->control[MIDI_CTL_MSB_BANK];
88262306a36Sopenharmony_ci	}
88362306a36Sopenharmony_ci}
88462306a36Sopenharmony_ci
88562306a36Sopenharmony_ci
88662306a36Sopenharmony_ci/* Look for the zones matching with the given note and velocity.
88762306a36Sopenharmony_ci * The resultant zones are stored on table.
88862306a36Sopenharmony_ci */
88962306a36Sopenharmony_cistatic int
89062306a36Sopenharmony_ciget_zone(struct snd_emux *emu, struct snd_emux_port *port,
89162306a36Sopenharmony_ci	 int *notep, int vel, struct snd_midi_channel *chan,
89262306a36Sopenharmony_ci	 struct snd_sf_zone **table)
89362306a36Sopenharmony_ci{
89462306a36Sopenharmony_ci	int preset, bank, def_preset, def_bank;
89562306a36Sopenharmony_ci
89662306a36Sopenharmony_ci	bank = get_bank(port, chan);
89762306a36Sopenharmony_ci	preset = chan->midi_program;
89862306a36Sopenharmony_ci
89962306a36Sopenharmony_ci	if (SF_IS_DRUM_BANK(bank)) {
90062306a36Sopenharmony_ci		def_preset = port->ctrls[EMUX_MD_DEF_DRUM];
90162306a36Sopenharmony_ci		def_bank = bank;
90262306a36Sopenharmony_ci	} else {
90362306a36Sopenharmony_ci		def_preset = preset;
90462306a36Sopenharmony_ci		def_bank = port->ctrls[EMUX_MD_DEF_BANK];
90562306a36Sopenharmony_ci	}
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ci	return snd_soundfont_search_zone(emu->sflist, notep, vel, preset, bank,
90862306a36Sopenharmony_ci					 def_preset, def_bank,
90962306a36Sopenharmony_ci					 table, SNDRV_EMUX_MAX_MULTI_VOICES);
91062306a36Sopenharmony_ci}
91162306a36Sopenharmony_ci
91262306a36Sopenharmony_ci/*
91362306a36Sopenharmony_ci */
91462306a36Sopenharmony_civoid
91562306a36Sopenharmony_cisnd_emux_init_voices(struct snd_emux *emu)
91662306a36Sopenharmony_ci{
91762306a36Sopenharmony_ci	struct snd_emux_voice *vp;
91862306a36Sopenharmony_ci	int i;
91962306a36Sopenharmony_ci	unsigned long flags;
92062306a36Sopenharmony_ci
92162306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
92262306a36Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
92362306a36Sopenharmony_ci		vp = &emu->voices[i];
92462306a36Sopenharmony_ci		vp->ch = -1; /* not used */
92562306a36Sopenharmony_ci		vp->state = SNDRV_EMUX_ST_OFF;
92662306a36Sopenharmony_ci		vp->chan = NULL;
92762306a36Sopenharmony_ci		vp->port = NULL;
92862306a36Sopenharmony_ci		vp->time = 0;
92962306a36Sopenharmony_ci		vp->emu = emu;
93062306a36Sopenharmony_ci		vp->hw = emu->hw;
93162306a36Sopenharmony_ci	}
93262306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
93362306a36Sopenharmony_ci}
93462306a36Sopenharmony_ci
93562306a36Sopenharmony_ci/*
93662306a36Sopenharmony_ci */
93762306a36Sopenharmony_civoid snd_emux_lock_voice(struct snd_emux *emu, int voice)
93862306a36Sopenharmony_ci{
93962306a36Sopenharmony_ci	unsigned long flags;
94062306a36Sopenharmony_ci
94162306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
94262306a36Sopenharmony_ci	if (emu->voices[voice].state == SNDRV_EMUX_ST_OFF)
94362306a36Sopenharmony_ci		emu->voices[voice].state = SNDRV_EMUX_ST_LOCKED;
94462306a36Sopenharmony_ci	else
94562306a36Sopenharmony_ci		snd_printk(KERN_WARNING
94662306a36Sopenharmony_ci			   "invalid voice for lock %d (state = %x)\n",
94762306a36Sopenharmony_ci			   voice, emu->voices[voice].state);
94862306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
94962306a36Sopenharmony_ci}
95062306a36Sopenharmony_ci
95162306a36Sopenharmony_ciEXPORT_SYMBOL(snd_emux_lock_voice);
95262306a36Sopenharmony_ci
95362306a36Sopenharmony_ci/*
95462306a36Sopenharmony_ci */
95562306a36Sopenharmony_civoid snd_emux_unlock_voice(struct snd_emux *emu, int voice)
95662306a36Sopenharmony_ci{
95762306a36Sopenharmony_ci	unsigned long flags;
95862306a36Sopenharmony_ci
95962306a36Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
96062306a36Sopenharmony_ci	if (emu->voices[voice].state == SNDRV_EMUX_ST_LOCKED)
96162306a36Sopenharmony_ci		emu->voices[voice].state = SNDRV_EMUX_ST_OFF;
96262306a36Sopenharmony_ci	else
96362306a36Sopenharmony_ci		snd_printk(KERN_WARNING
96462306a36Sopenharmony_ci			   "invalid voice for unlock %d (state = %x)\n",
96562306a36Sopenharmony_ci			   voice, emu->voices[voice].state);
96662306a36Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
96762306a36Sopenharmony_ci}
96862306a36Sopenharmony_ci
96962306a36Sopenharmony_ciEXPORT_SYMBOL(snd_emux_unlock_voice);
970