18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Midi synth routines for the Emu8k/Emu10k1
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 1999 Steve Ratcliffe
68c2ecf20Sopenharmony_ci *  Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *  Contains code based on awe_wave.c by Takashi Iwai
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/export.h>
128c2ecf20Sopenharmony_ci#include "emux_voice.h"
138c2ecf20Sopenharmony_ci#include <sound/asoundef.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * Prototypes
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * Ensure a value is between two points
218c2ecf20Sopenharmony_ci * macro evaluates its args more than once, so changed to upper-case.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
248c2ecf20Sopenharmony_ci#define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic int get_zone(struct snd_emux *emu, struct snd_emux_port *port,
278c2ecf20Sopenharmony_ci		    int *notep, int vel, struct snd_midi_channel *chan,
288c2ecf20Sopenharmony_ci		    struct snd_sf_zone **table);
298c2ecf20Sopenharmony_cistatic int get_bank(struct snd_emux_port *port, struct snd_midi_channel *chan);
308c2ecf20Sopenharmony_cistatic void terminate_note1(struct snd_emux *emu, int note,
318c2ecf20Sopenharmony_ci			    struct snd_midi_channel *chan, int free);
328c2ecf20Sopenharmony_cistatic void exclusive_note_off(struct snd_emux *emu, struct snd_emux_port *port,
338c2ecf20Sopenharmony_ci			       int exclass);
348c2ecf20Sopenharmony_cistatic void terminate_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int free);
358c2ecf20Sopenharmony_cistatic void update_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int update);
368c2ecf20Sopenharmony_cistatic void setup_voice(struct snd_emux_voice *vp);
378c2ecf20Sopenharmony_cistatic int calc_pan(struct snd_emux_voice *vp);
388c2ecf20Sopenharmony_cistatic int calc_volume(struct snd_emux_voice *vp);
398c2ecf20Sopenharmony_cistatic int calc_pitch(struct snd_emux_voice *vp);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/*
438c2ecf20Sopenharmony_ci * Start a note.
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_civoid
468c2ecf20Sopenharmony_cisnd_emux_note_on(void *p, int note, int vel, struct snd_midi_channel *chan)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct snd_emux *emu;
498c2ecf20Sopenharmony_ci	int i, key, nvoices;
508c2ecf20Sopenharmony_ci	struct snd_emux_voice *vp;
518c2ecf20Sopenharmony_ci	struct snd_sf_zone *table[SNDRV_EMUX_MAX_MULTI_VOICES];
528c2ecf20Sopenharmony_ci	unsigned long flags;
538c2ecf20Sopenharmony_ci	struct snd_emux_port *port;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	port = p;
568c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!port || !chan))
578c2ecf20Sopenharmony_ci		return;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	emu = port->emu;
608c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.get_voice || !emu->ops.trigger))
618c2ecf20Sopenharmony_ci		return;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	key = note; /* remember the original note */
648c2ecf20Sopenharmony_ci	nvoices = get_zone(emu, port, &note, vel, chan, table);
658c2ecf20Sopenharmony_ci	if (! nvoices)
668c2ecf20Sopenharmony_ci		return;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	/* exclusive note off */
698c2ecf20Sopenharmony_ci	for (i = 0; i < nvoices; i++) {
708c2ecf20Sopenharmony_ci		struct snd_sf_zone *zp = table[i];
718c2ecf20Sopenharmony_ci		if (zp && zp->v.exclusiveClass)
728c2ecf20Sopenharmony_ci			exclusive_note_off(emu, port, zp->v.exclusiveClass);
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#if 0 // seems not necessary
768c2ecf20Sopenharmony_ci	/* Turn off the same note on the same channel. */
778c2ecf20Sopenharmony_ci	terminate_note1(emu, key, chan, 0);
788c2ecf20Sopenharmony_ci#endif
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
818c2ecf20Sopenharmony_ci	for (i = 0; i < nvoices; i++) {
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci		/* set up each voice parameter */
848c2ecf20Sopenharmony_ci		/* at this stage, we don't trigger the voice yet. */
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci		if (table[i] == NULL)
878c2ecf20Sopenharmony_ci			continue;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci		vp = emu->ops.get_voice(emu, port);
908c2ecf20Sopenharmony_ci		if (vp == NULL || vp->ch < 0)
918c2ecf20Sopenharmony_ci			continue;
928c2ecf20Sopenharmony_ci		if (STATE_IS_PLAYING(vp->state))
938c2ecf20Sopenharmony_ci			emu->ops.terminate(vp);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci		vp->time = emu->use_time++;
968c2ecf20Sopenharmony_ci		vp->chan = chan;
978c2ecf20Sopenharmony_ci		vp->port = port;
988c2ecf20Sopenharmony_ci		vp->key = key;
998c2ecf20Sopenharmony_ci		vp->note = note;
1008c2ecf20Sopenharmony_ci		vp->velocity = vel;
1018c2ecf20Sopenharmony_ci		vp->zone = table[i];
1028c2ecf20Sopenharmony_ci		if (vp->zone->sample)
1038c2ecf20Sopenharmony_ci			vp->block = vp->zone->sample->block;
1048c2ecf20Sopenharmony_ci		else
1058c2ecf20Sopenharmony_ci			vp->block = NULL;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		setup_voice(vp);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		vp->state = SNDRV_EMUX_ST_STANDBY;
1108c2ecf20Sopenharmony_ci		if (emu->ops.prepare) {
1118c2ecf20Sopenharmony_ci			vp->state = SNDRV_EMUX_ST_OFF;
1128c2ecf20Sopenharmony_ci			if (emu->ops.prepare(vp) >= 0)
1138c2ecf20Sopenharmony_ci				vp->state = SNDRV_EMUX_ST_STANDBY;
1148c2ecf20Sopenharmony_ci		}
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	/* start envelope now */
1188c2ecf20Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
1198c2ecf20Sopenharmony_ci		vp = &emu->voices[i];
1208c2ecf20Sopenharmony_ci		if (vp->state == SNDRV_EMUX_ST_STANDBY &&
1218c2ecf20Sopenharmony_ci		    vp->chan == chan) {
1228c2ecf20Sopenharmony_ci			emu->ops.trigger(vp);
1238c2ecf20Sopenharmony_ci			vp->state = SNDRV_EMUX_ST_ON;
1248c2ecf20Sopenharmony_ci			vp->ontime = jiffies; /* remember the trigger timing */
1258c2ecf20Sopenharmony_ci		}
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci#ifdef SNDRV_EMUX_USE_RAW_EFFECT
1308c2ecf20Sopenharmony_ci	if (port->port_mode == SNDRV_EMUX_PORT_MODE_OSS_SYNTH) {
1318c2ecf20Sopenharmony_ci		/* clear voice position for the next note on this channel */
1328c2ecf20Sopenharmony_ci		struct snd_emux_effect_table *fx = chan->private;
1338c2ecf20Sopenharmony_ci		if (fx) {
1348c2ecf20Sopenharmony_ci			fx->flag[EMUX_FX_SAMPLE_START] = 0;
1358c2ecf20Sopenharmony_ci			fx->flag[EMUX_FX_COARSE_SAMPLE_START] = 0;
1368c2ecf20Sopenharmony_ci		}
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci#endif
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci/*
1428c2ecf20Sopenharmony_ci * Release a note in response to a midi note off.
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_civoid
1458c2ecf20Sopenharmony_cisnd_emux_note_off(void *p, int note, int vel, struct snd_midi_channel *chan)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	int ch;
1488c2ecf20Sopenharmony_ci	struct snd_emux *emu;
1498c2ecf20Sopenharmony_ci	struct snd_emux_voice *vp;
1508c2ecf20Sopenharmony_ci	unsigned long flags;
1518c2ecf20Sopenharmony_ci	struct snd_emux_port *port;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	port = p;
1548c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!port || !chan))
1558c2ecf20Sopenharmony_ci		return;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	emu = port->emu;
1588c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.release))
1598c2ecf20Sopenharmony_ci		return;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
1628c2ecf20Sopenharmony_ci	for (ch = 0; ch < emu->max_voices; ch++) {
1638c2ecf20Sopenharmony_ci		vp = &emu->voices[ch];
1648c2ecf20Sopenharmony_ci		if (STATE_IS_PLAYING(vp->state) &&
1658c2ecf20Sopenharmony_ci		    vp->chan == chan && vp->key == note) {
1668c2ecf20Sopenharmony_ci			vp->state = SNDRV_EMUX_ST_RELEASED;
1678c2ecf20Sopenharmony_ci			if (vp->ontime == jiffies) {
1688c2ecf20Sopenharmony_ci				/* if note-off is sent too shortly after
1698c2ecf20Sopenharmony_ci				 * note-on, emuX engine cannot produce the sound
1708c2ecf20Sopenharmony_ci				 * correctly.  so we'll release this note
1718c2ecf20Sopenharmony_ci				 * a bit later via timer callback.
1728c2ecf20Sopenharmony_ci				 */
1738c2ecf20Sopenharmony_ci				vp->state = SNDRV_EMUX_ST_PENDING;
1748c2ecf20Sopenharmony_ci				if (! emu->timer_active) {
1758c2ecf20Sopenharmony_ci					mod_timer(&emu->tlist, jiffies + 1);
1768c2ecf20Sopenharmony_ci					emu->timer_active = 1;
1778c2ecf20Sopenharmony_ci				}
1788c2ecf20Sopenharmony_ci			} else
1798c2ecf20Sopenharmony_ci				/* ok now release the note */
1808c2ecf20Sopenharmony_ci				emu->ops.release(vp);
1818c2ecf20Sopenharmony_ci		}
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci/*
1878c2ecf20Sopenharmony_ci * timer callback
1888c2ecf20Sopenharmony_ci *
1898c2ecf20Sopenharmony_ci * release the pending note-offs
1908c2ecf20Sopenharmony_ci */
1918c2ecf20Sopenharmony_civoid snd_emux_timer_callback(struct timer_list *t)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	struct snd_emux *emu = from_timer(emu, t, tlist);
1948c2ecf20Sopenharmony_ci	struct snd_emux_voice *vp;
1958c2ecf20Sopenharmony_ci	unsigned long flags;
1968c2ecf20Sopenharmony_ci	int ch, do_again = 0;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
1998c2ecf20Sopenharmony_ci	for (ch = 0; ch < emu->max_voices; ch++) {
2008c2ecf20Sopenharmony_ci		vp = &emu->voices[ch];
2018c2ecf20Sopenharmony_ci		if (vp->state == SNDRV_EMUX_ST_PENDING) {
2028c2ecf20Sopenharmony_ci			if (vp->ontime == jiffies)
2038c2ecf20Sopenharmony_ci				do_again++; /* release this at the next interrupt */
2048c2ecf20Sopenharmony_ci			else {
2058c2ecf20Sopenharmony_ci				emu->ops.release(vp);
2068c2ecf20Sopenharmony_ci				vp->state = SNDRV_EMUX_ST_RELEASED;
2078c2ecf20Sopenharmony_ci			}
2088c2ecf20Sopenharmony_ci		}
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci	if (do_again) {
2118c2ecf20Sopenharmony_ci		mod_timer(&emu->tlist, jiffies + 1);
2128c2ecf20Sopenharmony_ci		emu->timer_active = 1;
2138c2ecf20Sopenharmony_ci	} else
2148c2ecf20Sopenharmony_ci		emu->timer_active = 0;
2158c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci/*
2198c2ecf20Sopenharmony_ci * key pressure change
2208c2ecf20Sopenharmony_ci */
2218c2ecf20Sopenharmony_civoid
2228c2ecf20Sopenharmony_cisnd_emux_key_press(void *p, int note, int vel, struct snd_midi_channel *chan)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	int ch;
2258c2ecf20Sopenharmony_ci	struct snd_emux *emu;
2268c2ecf20Sopenharmony_ci	struct snd_emux_voice *vp;
2278c2ecf20Sopenharmony_ci	unsigned long flags;
2288c2ecf20Sopenharmony_ci	struct snd_emux_port *port;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	port = p;
2318c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!port || !chan))
2328c2ecf20Sopenharmony_ci		return;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	emu = port->emu;
2358c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.update))
2368c2ecf20Sopenharmony_ci		return;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
2398c2ecf20Sopenharmony_ci	for (ch = 0; ch < emu->max_voices; ch++) {
2408c2ecf20Sopenharmony_ci		vp = &emu->voices[ch];
2418c2ecf20Sopenharmony_ci		if (vp->state == SNDRV_EMUX_ST_ON &&
2428c2ecf20Sopenharmony_ci		    vp->chan == chan && vp->key == note) {
2438c2ecf20Sopenharmony_ci			vp->velocity = vel;
2448c2ecf20Sopenharmony_ci			update_voice(emu, vp, SNDRV_EMUX_UPDATE_VOLUME);
2458c2ecf20Sopenharmony_ci		}
2468c2ecf20Sopenharmony_ci	}
2478c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci/*
2528c2ecf20Sopenharmony_ci * Modulate the voices which belong to the channel
2538c2ecf20Sopenharmony_ci */
2548c2ecf20Sopenharmony_civoid
2558c2ecf20Sopenharmony_cisnd_emux_update_channel(struct snd_emux_port *port, struct snd_midi_channel *chan, int update)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	struct snd_emux *emu;
2588c2ecf20Sopenharmony_ci	struct snd_emux_voice *vp;
2598c2ecf20Sopenharmony_ci	int i;
2608c2ecf20Sopenharmony_ci	unsigned long flags;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	if (! update)
2638c2ecf20Sopenharmony_ci		return;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	emu = port->emu;
2668c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.update))
2678c2ecf20Sopenharmony_ci		return;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
2708c2ecf20Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
2718c2ecf20Sopenharmony_ci		vp = &emu->voices[i];
2728c2ecf20Sopenharmony_ci		if (vp->chan == chan)
2738c2ecf20Sopenharmony_ci			update_voice(emu, vp, update);
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci/*
2798c2ecf20Sopenharmony_ci * Modulate all the voices which belong to the port.
2808c2ecf20Sopenharmony_ci */
2818c2ecf20Sopenharmony_civoid
2828c2ecf20Sopenharmony_cisnd_emux_update_port(struct snd_emux_port *port, int update)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	struct snd_emux *emu;
2858c2ecf20Sopenharmony_ci	struct snd_emux_voice *vp;
2868c2ecf20Sopenharmony_ci	int i;
2878c2ecf20Sopenharmony_ci	unsigned long flags;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	if (! update)
2908c2ecf20Sopenharmony_ci		return;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	emu = port->emu;
2938c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.update))
2948c2ecf20Sopenharmony_ci		return;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
2978c2ecf20Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
2988c2ecf20Sopenharmony_ci		vp = &emu->voices[i];
2998c2ecf20Sopenharmony_ci		if (vp->port == port)
3008c2ecf20Sopenharmony_ci			update_voice(emu, vp, update);
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci/*
3078c2ecf20Sopenharmony_ci * Deal with a controller type event.  This includes all types of
3088c2ecf20Sopenharmony_ci * control events, not just the midi controllers
3098c2ecf20Sopenharmony_ci */
3108c2ecf20Sopenharmony_civoid
3118c2ecf20Sopenharmony_cisnd_emux_control(void *p, int type, struct snd_midi_channel *chan)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	struct snd_emux_port *port;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	port = p;
3168c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!port || !chan))
3178c2ecf20Sopenharmony_ci		return;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	switch (type) {
3208c2ecf20Sopenharmony_ci	case MIDI_CTL_MSB_MAIN_VOLUME:
3218c2ecf20Sopenharmony_ci	case MIDI_CTL_MSB_EXPRESSION:
3228c2ecf20Sopenharmony_ci		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_VOLUME);
3238c2ecf20Sopenharmony_ci		break;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	case MIDI_CTL_MSB_PAN:
3268c2ecf20Sopenharmony_ci		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PAN);
3278c2ecf20Sopenharmony_ci		break;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	case MIDI_CTL_SOFT_PEDAL:
3308c2ecf20Sopenharmony_ci#ifdef SNDRV_EMUX_USE_RAW_EFFECT
3318c2ecf20Sopenharmony_ci		/* FIXME: this is an emulation */
3328c2ecf20Sopenharmony_ci		if (chan->control[type] >= 64)
3338c2ecf20Sopenharmony_ci			snd_emux_send_effect(port, chan, EMUX_FX_CUTOFF, -160,
3348c2ecf20Sopenharmony_ci				     EMUX_FX_FLAG_ADD);
3358c2ecf20Sopenharmony_ci		else
3368c2ecf20Sopenharmony_ci			snd_emux_send_effect(port, chan, EMUX_FX_CUTOFF, 0,
3378c2ecf20Sopenharmony_ci				     EMUX_FX_FLAG_OFF);
3388c2ecf20Sopenharmony_ci#endif
3398c2ecf20Sopenharmony_ci		break;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	case MIDI_CTL_PITCHBEND:
3428c2ecf20Sopenharmony_ci		snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PITCH);
3438c2ecf20Sopenharmony_ci		break;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	case MIDI_CTL_MSB_MODWHEEL:
3468c2ecf20Sopenharmony_ci	case MIDI_CTL_CHAN_PRESSURE:
3478c2ecf20Sopenharmony_ci		snd_emux_update_channel(port, chan,
3488c2ecf20Sopenharmony_ci					SNDRV_EMUX_UPDATE_FMMOD |
3498c2ecf20Sopenharmony_ci					SNDRV_EMUX_UPDATE_FM2FRQ2);
3508c2ecf20Sopenharmony_ci		break;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	if (port->chset.midi_mode == SNDRV_MIDI_MODE_XG) {
3558c2ecf20Sopenharmony_ci		snd_emux_xg_control(port, chan, type);
3568c2ecf20Sopenharmony_ci	}
3578c2ecf20Sopenharmony_ci}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci/*
3618c2ecf20Sopenharmony_ci * terminate note - if free flag is true, free the terminated voice
3628c2ecf20Sopenharmony_ci */
3638c2ecf20Sopenharmony_cistatic void
3648c2ecf20Sopenharmony_citerminate_note1(struct snd_emux *emu, int note, struct snd_midi_channel *chan, int free)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	int  i;
3678c2ecf20Sopenharmony_ci	struct snd_emux_voice *vp;
3688c2ecf20Sopenharmony_ci	unsigned long flags;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
3718c2ecf20Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
3728c2ecf20Sopenharmony_ci		vp = &emu->voices[i];
3738c2ecf20Sopenharmony_ci		if (STATE_IS_PLAYING(vp->state) && vp->chan == chan &&
3748c2ecf20Sopenharmony_ci		    vp->key == note)
3758c2ecf20Sopenharmony_ci			terminate_voice(emu, vp, free);
3768c2ecf20Sopenharmony_ci	}
3778c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci/*
3828c2ecf20Sopenharmony_ci * terminate note - exported for midi emulation
3838c2ecf20Sopenharmony_ci */
3848c2ecf20Sopenharmony_civoid
3858c2ecf20Sopenharmony_cisnd_emux_terminate_note(void *p, int note, struct snd_midi_channel *chan)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	struct snd_emux *emu;
3888c2ecf20Sopenharmony_ci	struct snd_emux_port *port;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	port = p;
3918c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!port || !chan))
3928c2ecf20Sopenharmony_ci		return;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	emu = port->emu;
3958c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.terminate))
3968c2ecf20Sopenharmony_ci		return;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	terminate_note1(emu, note, chan, 1);
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci/*
4038c2ecf20Sopenharmony_ci * Terminate all the notes
4048c2ecf20Sopenharmony_ci */
4058c2ecf20Sopenharmony_civoid
4068c2ecf20Sopenharmony_cisnd_emux_terminate_all(struct snd_emux *emu)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	int i;
4098c2ecf20Sopenharmony_ci	struct snd_emux_voice *vp;
4108c2ecf20Sopenharmony_ci	unsigned long flags;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
4138c2ecf20Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
4148c2ecf20Sopenharmony_ci		vp = &emu->voices[i];
4158c2ecf20Sopenharmony_ci		if (STATE_IS_PLAYING(vp->state))
4168c2ecf20Sopenharmony_ci			terminate_voice(emu, vp, 0);
4178c2ecf20Sopenharmony_ci		if (vp->state == SNDRV_EMUX_ST_OFF) {
4188c2ecf20Sopenharmony_ci			if (emu->ops.free_voice)
4198c2ecf20Sopenharmony_ci				emu->ops.free_voice(vp);
4208c2ecf20Sopenharmony_ci			if (emu->ops.reset)
4218c2ecf20Sopenharmony_ci				emu->ops.reset(emu, i);
4228c2ecf20Sopenharmony_ci		}
4238c2ecf20Sopenharmony_ci		vp->time = 0;
4248c2ecf20Sopenharmony_ci	}
4258c2ecf20Sopenharmony_ci	/* initialize allocation time */
4268c2ecf20Sopenharmony_ci	emu->use_time = 0;
4278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_emux_terminate_all);
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci/*
4338c2ecf20Sopenharmony_ci * Terminate all voices associated with the given port
4348c2ecf20Sopenharmony_ci */
4358c2ecf20Sopenharmony_civoid
4368c2ecf20Sopenharmony_cisnd_emux_sounds_off_all(struct snd_emux_port *port)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	int i;
4398c2ecf20Sopenharmony_ci	struct snd_emux *emu;
4408c2ecf20Sopenharmony_ci	struct snd_emux_voice *vp;
4418c2ecf20Sopenharmony_ci	unsigned long flags;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!port))
4448c2ecf20Sopenharmony_ci		return;
4458c2ecf20Sopenharmony_ci	emu = port->emu;
4468c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!emu || !emu->ops.terminate))
4478c2ecf20Sopenharmony_ci		return;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
4508c2ecf20Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
4518c2ecf20Sopenharmony_ci		vp = &emu->voices[i];
4528c2ecf20Sopenharmony_ci		if (STATE_IS_PLAYING(vp->state) &&
4538c2ecf20Sopenharmony_ci		    vp->port == port)
4548c2ecf20Sopenharmony_ci			terminate_voice(emu, vp, 0);
4558c2ecf20Sopenharmony_ci		if (vp->state == SNDRV_EMUX_ST_OFF) {
4568c2ecf20Sopenharmony_ci			if (emu->ops.free_voice)
4578c2ecf20Sopenharmony_ci				emu->ops.free_voice(vp);
4588c2ecf20Sopenharmony_ci			if (emu->ops.reset)
4598c2ecf20Sopenharmony_ci				emu->ops.reset(emu, i);
4608c2ecf20Sopenharmony_ci		}
4618c2ecf20Sopenharmony_ci	}
4628c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
4638c2ecf20Sopenharmony_ci}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci/*
4678c2ecf20Sopenharmony_ci * Terminate all voices that have the same exclusive class.  This
4688c2ecf20Sopenharmony_ci * is mainly for drums.
4698c2ecf20Sopenharmony_ci */
4708c2ecf20Sopenharmony_cistatic void
4718c2ecf20Sopenharmony_ciexclusive_note_off(struct snd_emux *emu, struct snd_emux_port *port, int exclass)
4728c2ecf20Sopenharmony_ci{
4738c2ecf20Sopenharmony_ci	struct snd_emux_voice *vp;
4748c2ecf20Sopenharmony_ci	int  i;
4758c2ecf20Sopenharmony_ci	unsigned long flags;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
4788c2ecf20Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
4798c2ecf20Sopenharmony_ci		vp = &emu->voices[i];
4808c2ecf20Sopenharmony_ci		if (STATE_IS_PLAYING(vp->state) && vp->port == port &&
4818c2ecf20Sopenharmony_ci		    vp->reg.exclusiveClass == exclass) {
4828c2ecf20Sopenharmony_ci			terminate_voice(emu, vp, 0);
4838c2ecf20Sopenharmony_ci		}
4848c2ecf20Sopenharmony_ci	}
4858c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
4868c2ecf20Sopenharmony_ci}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci/*
4898c2ecf20Sopenharmony_ci * terminate a voice
4908c2ecf20Sopenharmony_ci * if free flag is true, call free_voice after termination
4918c2ecf20Sopenharmony_ci */
4928c2ecf20Sopenharmony_cistatic void
4938c2ecf20Sopenharmony_citerminate_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int free)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	emu->ops.terminate(vp);
4968c2ecf20Sopenharmony_ci	vp->time = emu->use_time++;
4978c2ecf20Sopenharmony_ci	vp->chan = NULL;
4988c2ecf20Sopenharmony_ci	vp->port = NULL;
4998c2ecf20Sopenharmony_ci	vp->zone = NULL;
5008c2ecf20Sopenharmony_ci	vp->block = NULL;
5018c2ecf20Sopenharmony_ci	vp->state = SNDRV_EMUX_ST_OFF;
5028c2ecf20Sopenharmony_ci	if (free && emu->ops.free_voice)
5038c2ecf20Sopenharmony_ci		emu->ops.free_voice(vp);
5048c2ecf20Sopenharmony_ci}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci/*
5088c2ecf20Sopenharmony_ci * Modulate the voice
5098c2ecf20Sopenharmony_ci */
5108c2ecf20Sopenharmony_cistatic void
5118c2ecf20Sopenharmony_ciupdate_voice(struct snd_emux *emu, struct snd_emux_voice *vp, int update)
5128c2ecf20Sopenharmony_ci{
5138c2ecf20Sopenharmony_ci	if (!STATE_IS_PLAYING(vp->state))
5148c2ecf20Sopenharmony_ci		return;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	if (vp->chan == NULL || vp->port == NULL)
5178c2ecf20Sopenharmony_ci		return;
5188c2ecf20Sopenharmony_ci	if (update & SNDRV_EMUX_UPDATE_VOLUME)
5198c2ecf20Sopenharmony_ci		calc_volume(vp);
5208c2ecf20Sopenharmony_ci	if (update & SNDRV_EMUX_UPDATE_PITCH)
5218c2ecf20Sopenharmony_ci		calc_pitch(vp);
5228c2ecf20Sopenharmony_ci	if (update & SNDRV_EMUX_UPDATE_PAN) {
5238c2ecf20Sopenharmony_ci		if (! calc_pan(vp) && (update == SNDRV_EMUX_UPDATE_PAN))
5248c2ecf20Sopenharmony_ci			return;
5258c2ecf20Sopenharmony_ci	}
5268c2ecf20Sopenharmony_ci	emu->ops.update(vp, update);
5278c2ecf20Sopenharmony_ci}
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci#if 0 // not used
5318c2ecf20Sopenharmony_ci/* table for volume target calculation */
5328c2ecf20Sopenharmony_cistatic const unsigned short voltarget[16] = {
5338c2ecf20Sopenharmony_ci	0xEAC0, 0xE0C8, 0xD740, 0xCE20, 0xC560, 0xBD08, 0xB500, 0xAD58,
5348c2ecf20Sopenharmony_ci	0xA5F8, 0x9EF0, 0x9830, 0x91C0, 0x8B90, 0x85A8, 0x8000, 0x7A90
5358c2ecf20Sopenharmony_ci};
5368c2ecf20Sopenharmony_ci#endif
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci#define LO_BYTE(v)	((v) & 0xff)
5398c2ecf20Sopenharmony_ci#define HI_BYTE(v)	(((v) >> 8) & 0xff)
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci/*
5428c2ecf20Sopenharmony_ci * Sets up the voice structure by calculating some values that
5438c2ecf20Sopenharmony_ci * will be needed later.
5448c2ecf20Sopenharmony_ci */
5458c2ecf20Sopenharmony_cistatic void
5468c2ecf20Sopenharmony_cisetup_voice(struct snd_emux_voice *vp)
5478c2ecf20Sopenharmony_ci{
5488c2ecf20Sopenharmony_ci	struct soundfont_voice_parm *parm;
5498c2ecf20Sopenharmony_ci	int pitch;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	/* copy the original register values */
5528c2ecf20Sopenharmony_ci	vp->reg = vp->zone->v;
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci#ifdef SNDRV_EMUX_USE_RAW_EFFECT
5558c2ecf20Sopenharmony_ci	snd_emux_setup_effect(vp);
5568c2ecf20Sopenharmony_ci#endif
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	/* reset status */
5598c2ecf20Sopenharmony_ci	vp->apan = -1;
5608c2ecf20Sopenharmony_ci	vp->avol = -1;
5618c2ecf20Sopenharmony_ci	vp->apitch = -1;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	calc_volume(vp);
5648c2ecf20Sopenharmony_ci	calc_pitch(vp);
5658c2ecf20Sopenharmony_ci	calc_pan(vp);
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	parm = &vp->reg.parm;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	/* compute filter target and correct modulation parameters */
5708c2ecf20Sopenharmony_ci	if (LO_BYTE(parm->modatkhld) >= 0x80 && parm->moddelay >= 0x8000) {
5718c2ecf20Sopenharmony_ci		parm->moddelay = 0xbfff;
5728c2ecf20Sopenharmony_ci		pitch = (HI_BYTE(parm->pefe) << 4) + vp->apitch;
5738c2ecf20Sopenharmony_ci		if (pitch > 0xffff)
5748c2ecf20Sopenharmony_ci			pitch = 0xffff;
5758c2ecf20Sopenharmony_ci		/* calculate filter target */
5768c2ecf20Sopenharmony_ci		vp->ftarget = parm->cutoff + LO_BYTE(parm->pefe);
5778c2ecf20Sopenharmony_ci		LIMITVALUE(vp->ftarget, 0, 255);
5788c2ecf20Sopenharmony_ci		vp->ftarget <<= 8;
5798c2ecf20Sopenharmony_ci	} else {
5808c2ecf20Sopenharmony_ci		vp->ftarget = parm->cutoff;
5818c2ecf20Sopenharmony_ci		vp->ftarget <<= 8;
5828c2ecf20Sopenharmony_ci		pitch = vp->apitch;
5838c2ecf20Sopenharmony_ci	}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	/* compute pitch target */
5868c2ecf20Sopenharmony_ci	if (pitch != 0xffff) {
5878c2ecf20Sopenharmony_ci		vp->ptarget = 1 << (pitch >> 12);
5888c2ecf20Sopenharmony_ci		if (pitch & 0x800) vp->ptarget += (vp->ptarget*0x102e)/0x2710;
5898c2ecf20Sopenharmony_ci		if (pitch & 0x400) vp->ptarget += (vp->ptarget*0x764)/0x2710;
5908c2ecf20Sopenharmony_ci		if (pitch & 0x200) vp->ptarget += (vp->ptarget*0x389)/0x2710;
5918c2ecf20Sopenharmony_ci		vp->ptarget += (vp->ptarget >> 1);
5928c2ecf20Sopenharmony_ci		if (vp->ptarget > 0xffff) vp->ptarget = 0xffff;
5938c2ecf20Sopenharmony_ci	} else
5948c2ecf20Sopenharmony_ci		vp->ptarget = 0xffff;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	if (LO_BYTE(parm->modatkhld) >= 0x80) {
5978c2ecf20Sopenharmony_ci		parm->modatkhld &= ~0xff;
5988c2ecf20Sopenharmony_ci		parm->modatkhld |= 0x7f;
5998c2ecf20Sopenharmony_ci	}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	/* compute volume target and correct volume parameters */
6028c2ecf20Sopenharmony_ci	vp->vtarget = 0;
6038c2ecf20Sopenharmony_ci#if 0 /* FIXME: this leads to some clicks.. */
6048c2ecf20Sopenharmony_ci	if (LO_BYTE(parm->volatkhld) >= 0x80 && parm->voldelay >= 0x8000) {
6058c2ecf20Sopenharmony_ci		parm->voldelay = 0xbfff;
6068c2ecf20Sopenharmony_ci		vp->vtarget = voltarget[vp->avol % 0x10] >> (vp->avol >> 4);
6078c2ecf20Sopenharmony_ci	}
6088c2ecf20Sopenharmony_ci#endif
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	if (LO_BYTE(parm->volatkhld) >= 0x80) {
6118c2ecf20Sopenharmony_ci		parm->volatkhld &= ~0xff;
6128c2ecf20Sopenharmony_ci		parm->volatkhld |= 0x7f;
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci}
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci/*
6178c2ecf20Sopenharmony_ci * calculate pitch parameter
6188c2ecf20Sopenharmony_ci */
6198c2ecf20Sopenharmony_cistatic const unsigned char pan_volumes[256] = {
6208c2ecf20Sopenharmony_ci0x00,0x03,0x06,0x09,0x0c,0x0f,0x12,0x14,0x17,0x1a,0x1d,0x20,0x22,0x25,0x28,0x2a,
6218c2ecf20Sopenharmony_ci0x2d,0x30,0x32,0x35,0x37,0x3a,0x3c,0x3f,0x41,0x44,0x46,0x49,0x4b,0x4d,0x50,0x52,
6228c2ecf20Sopenharmony_ci0x54,0x57,0x59,0x5b,0x5d,0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6f,0x71,0x73,0x75,
6238c2ecf20Sopenharmony_ci0x77,0x79,0x7b,0x7c,0x7e,0x80,0x82,0x84,0x86,0x88,0x89,0x8b,0x8d,0x8f,0x90,0x92,
6248c2ecf20Sopenharmony_ci0x94,0x96,0x97,0x99,0x9a,0x9c,0x9e,0x9f,0xa1,0xa2,0xa4,0xa5,0xa7,0xa8,0xaa,0xab,
6258c2ecf20Sopenharmony_ci0xad,0xae,0xaf,0xb1,0xb2,0xb3,0xb5,0xb6,0xb7,0xb9,0xba,0xbb,0xbc,0xbe,0xbf,0xc0,
6268c2ecf20Sopenharmony_ci0xc1,0xc2,0xc3,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,
6278c2ecf20Sopenharmony_ci0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xdd,0xde,0xdf,
6288c2ecf20Sopenharmony_ci0xdf,0xe0,0xe1,0xe2,0xe2,0xe3,0xe4,0xe4,0xe5,0xe6,0xe6,0xe7,0xe8,0xe8,0xe9,0xe9,
6298c2ecf20Sopenharmony_ci0xea,0xeb,0xeb,0xec,0xec,0xed,0xed,0xee,0xee,0xef,0xef,0xf0,0xf0,0xf1,0xf1,0xf1,
6308c2ecf20Sopenharmony_ci0xf2,0xf2,0xf3,0xf3,0xf3,0xf4,0xf4,0xf5,0xf5,0xf5,0xf6,0xf6,0xf6,0xf7,0xf7,0xf7,
6318c2ecf20Sopenharmony_ci0xf7,0xf8,0xf8,0xf8,0xf9,0xf9,0xf9,0xf9,0xf9,0xfa,0xfa,0xfa,0xfa,0xfb,0xfb,0xfb,
6328c2ecf20Sopenharmony_ci0xfb,0xfb,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,0xfd,
6338c2ecf20Sopenharmony_ci0xfd,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,
6348c2ecf20Sopenharmony_ci0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
6358c2ecf20Sopenharmony_ci0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
6368c2ecf20Sopenharmony_ci};
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_cistatic int
6398c2ecf20Sopenharmony_cicalc_pan(struct snd_emux_voice *vp)
6408c2ecf20Sopenharmony_ci{
6418c2ecf20Sopenharmony_ci	struct snd_midi_channel *chan = vp->chan;
6428c2ecf20Sopenharmony_ci	int pan;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	/* pan & loop start (pan 8bit, MSB, 0:right, 0xff:left) */
6458c2ecf20Sopenharmony_ci	if (vp->reg.fixpan > 0)	/* 0-127 */
6468c2ecf20Sopenharmony_ci		pan = 255 - (int)vp->reg.fixpan * 2;
6478c2ecf20Sopenharmony_ci	else {
6488c2ecf20Sopenharmony_ci		pan = chan->control[MIDI_CTL_MSB_PAN] - 64;
6498c2ecf20Sopenharmony_ci		if (vp->reg.pan >= 0) /* 0-127 */
6508c2ecf20Sopenharmony_ci			pan += vp->reg.pan - 64;
6518c2ecf20Sopenharmony_ci		pan = 127 - (int)pan * 2;
6528c2ecf20Sopenharmony_ci	}
6538c2ecf20Sopenharmony_ci	LIMITVALUE(pan, 0, 255);
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	if (vp->emu->linear_panning) {
6568c2ecf20Sopenharmony_ci		/* assuming linear volume */
6578c2ecf20Sopenharmony_ci		if (pan != vp->apan) {
6588c2ecf20Sopenharmony_ci			vp->apan = pan;
6598c2ecf20Sopenharmony_ci			if (pan == 0)
6608c2ecf20Sopenharmony_ci				vp->aaux = 0xff;
6618c2ecf20Sopenharmony_ci			else
6628c2ecf20Sopenharmony_ci				vp->aaux = (-pan) & 0xff;
6638c2ecf20Sopenharmony_ci			return 1;
6648c2ecf20Sopenharmony_ci		} else
6658c2ecf20Sopenharmony_ci			return 0;
6668c2ecf20Sopenharmony_ci	} else {
6678c2ecf20Sopenharmony_ci		/* using volume table */
6688c2ecf20Sopenharmony_ci		if (vp->apan != (int)pan_volumes[pan]) {
6698c2ecf20Sopenharmony_ci			vp->apan = pan_volumes[pan];
6708c2ecf20Sopenharmony_ci			vp->aaux = pan_volumes[255 - pan];
6718c2ecf20Sopenharmony_ci			return 1;
6728c2ecf20Sopenharmony_ci		}
6738c2ecf20Sopenharmony_ci		return 0;
6748c2ecf20Sopenharmony_ci	}
6758c2ecf20Sopenharmony_ci}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci/*
6798c2ecf20Sopenharmony_ci * calculate volume attenuation
6808c2ecf20Sopenharmony_ci *
6818c2ecf20Sopenharmony_ci * Voice volume is controlled by volume attenuation parameter.
6828c2ecf20Sopenharmony_ci * So volume becomes maximum when avol is 0 (no attenuation), and
6838c2ecf20Sopenharmony_ci * minimum when 255 (-96dB or silence).
6848c2ecf20Sopenharmony_ci */
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci/* tables for volume->attenuation calculation */
6878c2ecf20Sopenharmony_cistatic const unsigned char voltab1[128] = {
6888c2ecf20Sopenharmony_ci   0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
6898c2ecf20Sopenharmony_ci   0x63, 0x2b, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22,
6908c2ecf20Sopenharmony_ci   0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a,
6918c2ecf20Sopenharmony_ci   0x19, 0x19, 0x18, 0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x14,
6928c2ecf20Sopenharmony_ci   0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10,
6938c2ecf20Sopenharmony_ci   0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d,
6948c2ecf20Sopenharmony_ci   0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
6958c2ecf20Sopenharmony_ci   0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09,
6968c2ecf20Sopenharmony_ci   0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06,
6978c2ecf20Sopenharmony_ci   0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04,
6988c2ecf20Sopenharmony_ci   0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02,
6998c2ecf20Sopenharmony_ci   0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
7008c2ecf20Sopenharmony_ci   0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
7018c2ecf20Sopenharmony_ci};
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_cistatic const unsigned char voltab2[128] = {
7048c2ecf20Sopenharmony_ci   0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x2a,
7058c2ecf20Sopenharmony_ci   0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x24, 0x23, 0x22, 0x21,
7068c2ecf20Sopenharmony_ci   0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a,
7078c2ecf20Sopenharmony_ci   0x1a, 0x19, 0x19, 0x18, 0x18, 0x17, 0x16, 0x16, 0x15, 0x15,
7088c2ecf20Sopenharmony_ci   0x14, 0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x10,
7098c2ecf20Sopenharmony_ci   0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d,
7108c2ecf20Sopenharmony_ci   0x0d, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a,
7118c2ecf20Sopenharmony_ci   0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
7128c2ecf20Sopenharmony_ci   0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
7138c2ecf20Sopenharmony_ci   0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
7148c2ecf20Sopenharmony_ci   0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
7158c2ecf20Sopenharmony_ci   0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01,
7168c2ecf20Sopenharmony_ci   0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
7178c2ecf20Sopenharmony_ci};
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_cistatic const unsigned char expressiontab[128] = {
7208c2ecf20Sopenharmony_ci   0x7f, 0x6c, 0x62, 0x5a, 0x54, 0x50, 0x4b, 0x48, 0x45, 0x42,
7218c2ecf20Sopenharmony_ci   0x40, 0x3d, 0x3b, 0x39, 0x38, 0x36, 0x34, 0x33, 0x31, 0x30,
7228c2ecf20Sopenharmony_ci   0x2f, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25,
7238c2ecf20Sopenharmony_ci   0x24, 0x24, 0x23, 0x22, 0x21, 0x21, 0x20, 0x1f, 0x1e, 0x1e,
7248c2ecf20Sopenharmony_ci   0x1d, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a, 0x1a, 0x19, 0x18, 0x18,
7258c2ecf20Sopenharmony_ci   0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x15, 0x14, 0x14, 0x13,
7268c2ecf20Sopenharmony_ci   0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10, 0x10, 0x0f, 0x0f,
7278c2ecf20Sopenharmony_ci   0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c,
7288c2ecf20Sopenharmony_ci   0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09,
7298c2ecf20Sopenharmony_ci   0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
7308c2ecf20Sopenharmony_ci   0x06, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03,
7318c2ecf20Sopenharmony_ci   0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
7328c2ecf20Sopenharmony_ci   0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
7338c2ecf20Sopenharmony_ci};
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci/*
7368c2ecf20Sopenharmony_ci * Magic to calculate the volume (actually attenuation) from all the
7378c2ecf20Sopenharmony_ci * voice and channels parameters.
7388c2ecf20Sopenharmony_ci */
7398c2ecf20Sopenharmony_cistatic int
7408c2ecf20Sopenharmony_cicalc_volume(struct snd_emux_voice *vp)
7418c2ecf20Sopenharmony_ci{
7428c2ecf20Sopenharmony_ci	int vol;
7438c2ecf20Sopenharmony_ci	int main_vol, expression_vol, master_vol;
7448c2ecf20Sopenharmony_ci	struct snd_midi_channel *chan = vp->chan;
7458c2ecf20Sopenharmony_ci	struct snd_emux_port *port = vp->port;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	expression_vol = chan->control[MIDI_CTL_MSB_EXPRESSION];
7488c2ecf20Sopenharmony_ci	LIMITMAX(vp->velocity, 127);
7498c2ecf20Sopenharmony_ci	LIMITVALUE(expression_vol, 0, 127);
7508c2ecf20Sopenharmony_ci	if (port->port_mode == SNDRV_EMUX_PORT_MODE_OSS_SYNTH) {
7518c2ecf20Sopenharmony_ci		/* 0 - 127 */
7528c2ecf20Sopenharmony_ci		main_vol = chan->control[MIDI_CTL_MSB_MAIN_VOLUME];
7538c2ecf20Sopenharmony_ci		vol = (vp->velocity * main_vol * expression_vol) / (127*127);
7548c2ecf20Sopenharmony_ci		vol = vol * vp->reg.amplitude / 127;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci		LIMITVALUE(vol, 0, 127);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci		/* calc to attenuation */
7598c2ecf20Sopenharmony_ci		vol = snd_sf_vol_table[vol];
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	} else {
7628c2ecf20Sopenharmony_ci		main_vol = chan->control[MIDI_CTL_MSB_MAIN_VOLUME] * vp->reg.amplitude / 127;
7638c2ecf20Sopenharmony_ci		LIMITVALUE(main_vol, 0, 127);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci		vol = voltab1[main_vol] + voltab2[vp->velocity];
7668c2ecf20Sopenharmony_ci		vol = (vol * 8) / 3;
7678c2ecf20Sopenharmony_ci		vol += vp->reg.attenuation;
7688c2ecf20Sopenharmony_ci		vol += ((0x100 - vol) * expressiontab[expression_vol])/128;
7698c2ecf20Sopenharmony_ci	}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	master_vol = port->chset.gs_master_volume;
7728c2ecf20Sopenharmony_ci	LIMITVALUE(master_vol, 0, 127);
7738c2ecf20Sopenharmony_ci	vol += snd_sf_vol_table[master_vol];
7748c2ecf20Sopenharmony_ci	vol += port->volume_atten;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci#ifdef SNDRV_EMUX_USE_RAW_EFFECT
7778c2ecf20Sopenharmony_ci	if (chan->private) {
7788c2ecf20Sopenharmony_ci		struct snd_emux_effect_table *fx = chan->private;
7798c2ecf20Sopenharmony_ci		vol += fx->val[EMUX_FX_ATTEN];
7808c2ecf20Sopenharmony_ci	}
7818c2ecf20Sopenharmony_ci#endif
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	LIMITVALUE(vol, 0, 255);
7848c2ecf20Sopenharmony_ci	if (vp->avol == vol)
7858c2ecf20Sopenharmony_ci		return 0; /* value unchanged */
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	vp->avol = vol;
7888c2ecf20Sopenharmony_ci	if (!SF_IS_DRUM_BANK(get_bank(port, chan))
7898c2ecf20Sopenharmony_ci	    && LO_BYTE(vp->reg.parm.volatkhld) < 0x7d) {
7908c2ecf20Sopenharmony_ci		int atten;
7918c2ecf20Sopenharmony_ci		if (vp->velocity < 70)
7928c2ecf20Sopenharmony_ci			atten = 70;
7938c2ecf20Sopenharmony_ci		else
7948c2ecf20Sopenharmony_ci			atten = vp->velocity;
7958c2ecf20Sopenharmony_ci		vp->acutoff = (atten * vp->reg.parm.cutoff + 0xa0) >> 7;
7968c2ecf20Sopenharmony_ci	} else {
7978c2ecf20Sopenharmony_ci		vp->acutoff = vp->reg.parm.cutoff;
7988c2ecf20Sopenharmony_ci	}
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	return 1; /* value changed */
8018c2ecf20Sopenharmony_ci}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci/*
8048c2ecf20Sopenharmony_ci * calculate pitch offset
8058c2ecf20Sopenharmony_ci *
8068c2ecf20Sopenharmony_ci * 0xE000 is no pitch offset at 44100Hz sample.
8078c2ecf20Sopenharmony_ci * Every 4096 is one octave.
8088c2ecf20Sopenharmony_ci */
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_cistatic int
8118c2ecf20Sopenharmony_cicalc_pitch(struct snd_emux_voice *vp)
8128c2ecf20Sopenharmony_ci{
8138c2ecf20Sopenharmony_ci	struct snd_midi_channel *chan = vp->chan;
8148c2ecf20Sopenharmony_ci	int offset;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	/* calculate offset */
8178c2ecf20Sopenharmony_ci	if (vp->reg.fixkey >= 0) {
8188c2ecf20Sopenharmony_ci		offset = (vp->reg.fixkey - vp->reg.root) * 4096 / 12;
8198c2ecf20Sopenharmony_ci	} else {
8208c2ecf20Sopenharmony_ci		offset = (vp->note - vp->reg.root) * 4096 / 12;
8218c2ecf20Sopenharmony_ci	}
8228c2ecf20Sopenharmony_ci	offset = (offset * vp->reg.scaleTuning) / 100;
8238c2ecf20Sopenharmony_ci	offset += vp->reg.tune * 4096 / 1200;
8248c2ecf20Sopenharmony_ci	if (chan->midi_pitchbend != 0) {
8258c2ecf20Sopenharmony_ci		/* (128 * 8192: 1 semitone) ==> (4096: 12 semitones) */
8268c2ecf20Sopenharmony_ci		offset += chan->midi_pitchbend * chan->gm_rpn_pitch_bend_range / 3072;
8278c2ecf20Sopenharmony_ci	}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	/* tuning via RPN:
8308c2ecf20Sopenharmony_ci	 *   coarse = -8192 to 8192 (100 cent per 128)
8318c2ecf20Sopenharmony_ci	 *   fine = -8192 to 8192 (max=100cent)
8328c2ecf20Sopenharmony_ci	 */
8338c2ecf20Sopenharmony_ci	/* 4096 = 1200 cents in emu8000 parameter */
8348c2ecf20Sopenharmony_ci	offset += chan->gm_rpn_coarse_tuning * 4096 / (12 * 128);
8358c2ecf20Sopenharmony_ci	offset += chan->gm_rpn_fine_tuning / 24;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci#ifdef SNDRV_EMUX_USE_RAW_EFFECT
8388c2ecf20Sopenharmony_ci	/* add initial pitch correction */
8398c2ecf20Sopenharmony_ci	if (chan->private) {
8408c2ecf20Sopenharmony_ci		struct snd_emux_effect_table *fx = chan->private;
8418c2ecf20Sopenharmony_ci		if (fx->flag[EMUX_FX_INIT_PITCH])
8428c2ecf20Sopenharmony_ci			offset += fx->val[EMUX_FX_INIT_PITCH];
8438c2ecf20Sopenharmony_ci	}
8448c2ecf20Sopenharmony_ci#endif
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	/* 0xe000: root pitch */
8478c2ecf20Sopenharmony_ci	offset += 0xe000 + vp->reg.rate_offset;
8488c2ecf20Sopenharmony_ci	offset += vp->emu->pitch_shift;
8498c2ecf20Sopenharmony_ci	LIMITVALUE(offset, 0, 0xffff);
8508c2ecf20Sopenharmony_ci	if (offset == vp->apitch)
8518c2ecf20Sopenharmony_ci		return 0; /* unchanged */
8528c2ecf20Sopenharmony_ci	vp->apitch = offset;
8538c2ecf20Sopenharmony_ci	return 1; /* value changed */
8548c2ecf20Sopenharmony_ci}
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci/*
8578c2ecf20Sopenharmony_ci * Get the bank number assigned to the channel
8588c2ecf20Sopenharmony_ci */
8598c2ecf20Sopenharmony_cistatic int
8608c2ecf20Sopenharmony_ciget_bank(struct snd_emux_port *port, struct snd_midi_channel *chan)
8618c2ecf20Sopenharmony_ci{
8628c2ecf20Sopenharmony_ci	int val;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	switch (port->chset.midi_mode) {
8658c2ecf20Sopenharmony_ci	case SNDRV_MIDI_MODE_XG:
8668c2ecf20Sopenharmony_ci		val = chan->control[MIDI_CTL_MSB_BANK];
8678c2ecf20Sopenharmony_ci		if (val == 127)
8688c2ecf20Sopenharmony_ci			return 128; /* return drum bank */
8698c2ecf20Sopenharmony_ci		return chan->control[MIDI_CTL_LSB_BANK];
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	case SNDRV_MIDI_MODE_GS:
8728c2ecf20Sopenharmony_ci		if (chan->drum_channel)
8738c2ecf20Sopenharmony_ci			return 128;
8748c2ecf20Sopenharmony_ci		/* ignore LSB (bank map) */
8758c2ecf20Sopenharmony_ci		return chan->control[MIDI_CTL_MSB_BANK];
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	default:
8788c2ecf20Sopenharmony_ci		if (chan->drum_channel)
8798c2ecf20Sopenharmony_ci			return 128;
8808c2ecf20Sopenharmony_ci		return chan->control[MIDI_CTL_MSB_BANK];
8818c2ecf20Sopenharmony_ci	}
8828c2ecf20Sopenharmony_ci}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci/* Look for the zones matching with the given note and velocity.
8868c2ecf20Sopenharmony_ci * The resultant zones are stored on table.
8878c2ecf20Sopenharmony_ci */
8888c2ecf20Sopenharmony_cistatic int
8898c2ecf20Sopenharmony_ciget_zone(struct snd_emux *emu, struct snd_emux_port *port,
8908c2ecf20Sopenharmony_ci	 int *notep, int vel, struct snd_midi_channel *chan,
8918c2ecf20Sopenharmony_ci	 struct snd_sf_zone **table)
8928c2ecf20Sopenharmony_ci{
8938c2ecf20Sopenharmony_ci	int preset, bank, def_preset, def_bank;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	bank = get_bank(port, chan);
8968c2ecf20Sopenharmony_ci	preset = chan->midi_program;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	if (SF_IS_DRUM_BANK(bank)) {
8998c2ecf20Sopenharmony_ci		def_preset = port->ctrls[EMUX_MD_DEF_DRUM];
9008c2ecf20Sopenharmony_ci		def_bank = bank;
9018c2ecf20Sopenharmony_ci	} else {
9028c2ecf20Sopenharmony_ci		def_preset = preset;
9038c2ecf20Sopenharmony_ci		def_bank = port->ctrls[EMUX_MD_DEF_BANK];
9048c2ecf20Sopenharmony_ci	}
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	return snd_soundfont_search_zone(emu->sflist, notep, vel, preset, bank,
9078c2ecf20Sopenharmony_ci					 def_preset, def_bank,
9088c2ecf20Sopenharmony_ci					 table, SNDRV_EMUX_MAX_MULTI_VOICES);
9098c2ecf20Sopenharmony_ci}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci/*
9128c2ecf20Sopenharmony_ci */
9138c2ecf20Sopenharmony_civoid
9148c2ecf20Sopenharmony_cisnd_emux_init_voices(struct snd_emux *emu)
9158c2ecf20Sopenharmony_ci{
9168c2ecf20Sopenharmony_ci	struct snd_emux_voice *vp;
9178c2ecf20Sopenharmony_ci	int i;
9188c2ecf20Sopenharmony_ci	unsigned long flags;
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
9218c2ecf20Sopenharmony_ci	for (i = 0; i < emu->max_voices; i++) {
9228c2ecf20Sopenharmony_ci		vp = &emu->voices[i];
9238c2ecf20Sopenharmony_ci		vp->ch = -1; /* not used */
9248c2ecf20Sopenharmony_ci		vp->state = SNDRV_EMUX_ST_OFF;
9258c2ecf20Sopenharmony_ci		vp->chan = NULL;
9268c2ecf20Sopenharmony_ci		vp->port = NULL;
9278c2ecf20Sopenharmony_ci		vp->time = 0;
9288c2ecf20Sopenharmony_ci		vp->emu = emu;
9298c2ecf20Sopenharmony_ci		vp->hw = emu->hw;
9308c2ecf20Sopenharmony_ci	}
9318c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
9328c2ecf20Sopenharmony_ci}
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci/*
9358c2ecf20Sopenharmony_ci */
9368c2ecf20Sopenharmony_civoid snd_emux_lock_voice(struct snd_emux *emu, int voice)
9378c2ecf20Sopenharmony_ci{
9388c2ecf20Sopenharmony_ci	unsigned long flags;
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
9418c2ecf20Sopenharmony_ci	if (emu->voices[voice].state == SNDRV_EMUX_ST_OFF)
9428c2ecf20Sopenharmony_ci		emu->voices[voice].state = SNDRV_EMUX_ST_LOCKED;
9438c2ecf20Sopenharmony_ci	else
9448c2ecf20Sopenharmony_ci		snd_printk(KERN_WARNING
9458c2ecf20Sopenharmony_ci			   "invalid voice for lock %d (state = %x)\n",
9468c2ecf20Sopenharmony_ci			   voice, emu->voices[voice].state);
9478c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
9488c2ecf20Sopenharmony_ci}
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_emux_lock_voice);
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci/*
9538c2ecf20Sopenharmony_ci */
9548c2ecf20Sopenharmony_civoid snd_emux_unlock_voice(struct snd_emux *emu, int voice)
9558c2ecf20Sopenharmony_ci{
9568c2ecf20Sopenharmony_ci	unsigned long flags;
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	spin_lock_irqsave(&emu->voice_lock, flags);
9598c2ecf20Sopenharmony_ci	if (emu->voices[voice].state == SNDRV_EMUX_ST_LOCKED)
9608c2ecf20Sopenharmony_ci		emu->voices[voice].state = SNDRV_EMUX_ST_OFF;
9618c2ecf20Sopenharmony_ci	else
9628c2ecf20Sopenharmony_ci		snd_printk(KERN_WARNING
9638c2ecf20Sopenharmony_ci			   "invalid voice for unlock %d (state = %x)\n",
9648c2ecf20Sopenharmony_ci			   voice, emu->voices[voice].state);
9658c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&emu->voice_lock, flags);
9668c2ecf20Sopenharmony_ci}
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_emux_unlock_voice);
969