xref: /linux/sound/synth/emux/emux_effect.c (revision d67b569f5f620c0fb95d5212642746b7ba9d29e4)
1 /*
2  *  Midi synth routines for the Emu8k/Emu10k1
3  *
4  *  Copyright (C) 1999 Steve Ratcliffe
5  *  Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
6  *
7  *  Contains code based on awe_wave.c by Takashi Iwai
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  *
23  */
24 
25 #include "emux_voice.h"
26 #include <linux/slab.h>
27 
28 #ifdef SNDRV_EMUX_USE_RAW_EFFECT
29 /*
30  * effects table
31  */
32 
33 #define xoffsetof(type,tag)	((long)(&((type)NULL)->tag) - (long)(NULL))
34 
35 #define parm_offset(tag)	xoffsetof(soundfont_voice_parm_t*, tag)
36 
37 #define PARM_IS_BYTE		(1 << 0)
38 #define PARM_IS_WORD		(1 << 1)
39 #define PARM_IS_ALIGNED		(3 << 2)
40 #define PARM_IS_ALIGN_HI	(1 << 2)
41 #define PARM_IS_ALIGN_LO	(2 << 2)
42 #define PARM_IS_SIGNED		(1 << 4)
43 
44 #define PARM_WORD	(PARM_IS_WORD)
45 #define PARM_BYTE_LO	(PARM_IS_BYTE|PARM_IS_ALIGN_LO)
46 #define PARM_BYTE_HI	(PARM_IS_BYTE|PARM_IS_ALIGN_HI)
47 #define PARM_BYTE	(PARM_IS_BYTE)
48 #define PARM_SIGN_LO	(PARM_IS_BYTE|PARM_IS_ALIGN_LO|PARM_IS_SIGNED)
49 #define PARM_SIGN_HI	(PARM_IS_BYTE|PARM_IS_ALIGN_HI|PARM_IS_SIGNED)
50 
51 static struct emux_parm_defs {
52 	int type;	/* byte or word */
53 	int low, high;	/* value range */
54 	long offset;	/* offset in parameter record (-1 = not written) */
55 	int update;	/* flgas for real-time update */
56 } parm_defs[EMUX_NUM_EFFECTS] = {
57 	{PARM_WORD, 0, 0x8000, parm_offset(moddelay), 0},	/* env1 delay */
58 	{PARM_BYTE_LO, 1, 0x80, parm_offset(modatkhld), 0},	/* env1 attack */
59 	{PARM_BYTE_HI, 0, 0x7e, parm_offset(modatkhld), 0},	/* env1 hold */
60 	{PARM_BYTE_LO, 1, 0x7f, parm_offset(moddcysus), 0},	/* env1 decay */
61 	{PARM_BYTE_LO, 1, 0x7f, parm_offset(modrelease), 0},	/* env1 release */
62 	{PARM_BYTE_HI, 0, 0x7f, parm_offset(moddcysus), 0},	/* env1 sustain */
63 	{PARM_BYTE_HI, 0, 0xff, parm_offset(pefe), 0},	/* env1 pitch */
64 	{PARM_BYTE_LO, 0, 0xff, parm_offset(pefe), 0},	/* env1 fc */
65 
66 	{PARM_WORD, 0, 0x8000, parm_offset(voldelay), 0},	/* env2 delay */
67 	{PARM_BYTE_LO, 1, 0x80, parm_offset(volatkhld), 0},	/* env2 attack */
68 	{PARM_BYTE_HI, 0, 0x7e, parm_offset(volatkhld), 0},	/* env2 hold */
69 	{PARM_BYTE_LO, 1, 0x7f, parm_offset(voldcysus), 0},	/* env2 decay */
70 	{PARM_BYTE_LO, 1, 0x7f, parm_offset(volrelease), 0},	/* env2 release */
71 	{PARM_BYTE_HI, 0, 0x7f, parm_offset(voldcysus), 0},	/* env2 sustain */
72 
73 	{PARM_WORD, 0, 0x8000, parm_offset(lfo1delay), 0},	/* lfo1 delay */
74 	{PARM_BYTE_LO, 0, 0xff, parm_offset(tremfrq), SNDRV_EMUX_UPDATE_TREMFREQ},	/* lfo1 freq */
75 	{PARM_SIGN_HI, -128, 127, parm_offset(tremfrq), SNDRV_EMUX_UPDATE_TREMFREQ},	/* lfo1 vol */
76 	{PARM_SIGN_HI, -128, 127, parm_offset(fmmod), SNDRV_EMUX_UPDATE_FMMOD},	/* lfo1 pitch */
77 	{PARM_BYTE_LO, 0, 0xff, parm_offset(fmmod), SNDRV_EMUX_UPDATE_FMMOD},	/* lfo1 cutoff */
78 
79 	{PARM_WORD, 0, 0x8000, parm_offset(lfo2delay), 0},	/* lfo2 delay */
80 	{PARM_BYTE_LO, 0, 0xff, parm_offset(fm2frq2), SNDRV_EMUX_UPDATE_FM2FRQ2},	/* lfo2 freq */
81 	{PARM_SIGN_HI, -128, 127, parm_offset(fm2frq2), SNDRV_EMUX_UPDATE_FM2FRQ2},	/* lfo2 pitch */
82 
83 	{PARM_WORD, 0, 0xffff, -1, SNDRV_EMUX_UPDATE_PITCH},	/* initial pitch */
84 	{PARM_BYTE, 0, 0xff, parm_offset(chorus), 0},	/* chorus */
85 	{PARM_BYTE, 0, 0xff, parm_offset(reverb), 0},	/* reverb */
86 	{PARM_BYTE, 0, 0xff, parm_offset(cutoff), SNDRV_EMUX_UPDATE_VOLUME},	/* cutoff */
87 	{PARM_BYTE, 0, 15, parm_offset(filterQ), SNDRV_EMUX_UPDATE_Q},	/* resonance */
88 
89 	{PARM_WORD, 0, 0xffff, -1, 0},	/* sample start */
90 	{PARM_WORD, 0, 0xffff, -1, 0},	/* loop start */
91 	{PARM_WORD, 0, 0xffff, -1, 0},	/* loop end */
92 	{PARM_WORD, 0, 0xffff, -1, 0},	/* coarse sample start */
93 	{PARM_WORD, 0, 0xffff, -1, 0},	/* coarse loop start */
94 	{PARM_WORD, 0, 0xffff, -1, 0},	/* coarse loop end */
95 	{PARM_BYTE, 0, 0xff, -1, SNDRV_EMUX_UPDATE_VOLUME},	/* initial attenuation */
96 };
97 
98 /* set byte effect value */
99 static void
100 effect_set_byte(unsigned char *valp, snd_midi_channel_t *chan, int type)
101 {
102 	short effect;
103 	snd_emux_effect_table_t *fx = chan->private;
104 
105 	effect = fx->val[type];
106 	if (fx->flag[type] == EMUX_FX_FLAG_ADD) {
107 		if (parm_defs[type].type & PARM_IS_SIGNED)
108 			effect += *(char*)valp;
109 		else
110 			effect += *valp;
111 	}
112 	if (effect < parm_defs[type].low)
113 		effect = parm_defs[type].low;
114 	else if (effect > parm_defs[type].high)
115 		effect = parm_defs[type].high;
116 	*valp = (unsigned char)effect;
117 }
118 
119 /* set word effect value */
120 static void
121 effect_set_word(unsigned short *valp, snd_midi_channel_t *chan, int type)
122 {
123 	int effect;
124 	snd_emux_effect_table_t *fx = chan->private;
125 
126 	effect = *(unsigned short*)&fx->val[type];
127 	if (fx->flag[type] == EMUX_FX_FLAG_ADD)
128 		effect += *valp;
129 	if (effect < parm_defs[type].low)
130 		effect = parm_defs[type].low;
131 	else if (effect > parm_defs[type].high)
132 		effect = parm_defs[type].high;
133 	*valp = (unsigned short)effect;
134 }
135 
136 /* address offset */
137 static int
138 effect_get_offset(snd_midi_channel_t *chan, int lo, int hi, int mode)
139 {
140 	int addr = 0;
141 	snd_emux_effect_table_t *fx = chan->private;
142 
143 	if (fx->flag[hi])
144 		addr = (short)fx->val[hi];
145 	addr = addr << 15;
146 	if (fx->flag[lo])
147 		addr += (short)fx->val[lo];
148 	if (!(mode & SNDRV_SFNT_SAMPLE_8BITS))
149 		addr /= 2;
150 	return addr;
151 }
152 
153 #ifdef CONFIG_SND_SEQUENCER_OSS
154 /* change effects - for OSS sequencer compatibility */
155 void
156 snd_emux_send_effect_oss(snd_emux_port_t *port, snd_midi_channel_t *chan, int type, int val)
157 {
158 	int mode;
159 
160 	if (type & 0x40)
161 		mode = EMUX_FX_FLAG_OFF;
162 	else if (type & 0x80)
163 		mode = EMUX_FX_FLAG_ADD;
164 	else
165 		mode = EMUX_FX_FLAG_SET;
166 	type &= 0x3f;
167 
168 	snd_emux_send_effect(port, chan, type, val, mode);
169 }
170 #endif
171 
172 /* Modify the effect value.
173  * if update is necessary, call emu8000_control
174  */
175 void
176 snd_emux_send_effect(snd_emux_port_t *port, snd_midi_channel_t *chan, int type, int val, int mode)
177 {
178 	int i;
179 	int offset;
180 	unsigned char *srcp, *origp;
181 	snd_emux_t *emu;
182 	snd_emux_effect_table_t *fx;
183 	unsigned long flags;
184 
185 	emu = port->emu;
186 	fx = chan->private;
187 	if (emu == NULL || fx == NULL)
188 		return;
189 	if (type < 0 || type >= EMUX_NUM_EFFECTS)
190 		return;
191 
192 	fx->val[type] = val;
193 	fx->flag[type] = mode;
194 
195 	/* do we need to modify the register in realtime ? */
196 	if (! parm_defs[type].update || (offset = parm_defs[type].offset) < 0)
197 		return;
198 
199 #ifdef SNDRV_LITTLE_ENDIAN
200 	if (parm_defs[type].type & PARM_IS_ALIGN_HI)
201 		offset++;
202 #else
203 	if (parm_defs[type].type & PARM_IS_ALIGN_LO)
204 		offset++;
205 #endif
206 	/* modify the register values */
207 	spin_lock_irqsave(&emu->voice_lock, flags);
208 	for (i = 0; i < emu->max_voices; i++) {
209 		snd_emux_voice_t *vp = &emu->voices[i];
210 		if (!STATE_IS_PLAYING(vp->state) || vp->chan != chan)
211 			continue;
212 		srcp = (unsigned char*)&vp->reg.parm + offset;
213 		origp = (unsigned char*)&vp->zone->v.parm + offset;
214 		if (parm_defs[i].type & PARM_IS_BYTE) {
215 			*srcp = *origp;
216 			effect_set_byte(srcp, chan, type);
217 		} else {
218 			*(unsigned short*)srcp = *(unsigned short*)origp;
219 			effect_set_word((unsigned short*)srcp, chan, type);
220 		}
221 	}
222 	spin_unlock_irqrestore(&emu->voice_lock, flags);
223 
224 	/* activate them */
225 	snd_emux_update_channel(port, chan, parm_defs[type].update);
226 }
227 
228 
229 /* copy wavetable registers to voice table */
230 void
231 snd_emux_setup_effect(snd_emux_voice_t *vp)
232 {
233 	snd_midi_channel_t *chan = vp->chan;
234 	snd_emux_effect_table_t *fx;
235 	unsigned char *srcp;
236 	int i;
237 
238 	if (! (fx = chan->private))
239 		return;
240 
241 	/* modify the register values via effect table */
242 	for (i = 0; i < EMUX_FX_END; i++) {
243 		int offset;
244 		if (! fx->flag[i] || (offset = parm_defs[i].offset) < 0)
245 			continue;
246 #ifdef SNDRV_LITTLE_ENDIAN
247 		if (parm_defs[i].type & PARM_IS_ALIGN_HI)
248 			offset++;
249 #else
250 		if (parm_defs[i].type & PARM_IS_ALIGN_LO)
251 			offset++;
252 #endif
253 		srcp = (unsigned char*)&vp->reg.parm + offset;
254 		if (parm_defs[i].type & PARM_IS_BYTE)
255 			effect_set_byte(srcp, chan, i);
256 		else
257 			effect_set_word((unsigned short*)srcp, chan, i);
258 	}
259 
260 	/* correct sample and loop points */
261 	vp->reg.start += effect_get_offset(chan, EMUX_FX_SAMPLE_START,
262 					   EMUX_FX_COARSE_SAMPLE_START,
263 					   vp->reg.sample_mode);
264 
265 	vp->reg.loopstart += effect_get_offset(chan, EMUX_FX_LOOP_START,
266 					       EMUX_FX_COARSE_LOOP_START,
267 					       vp->reg.sample_mode);
268 
269 	vp->reg.loopend += effect_get_offset(chan, EMUX_FX_LOOP_END,
270 					     EMUX_FX_COARSE_LOOP_END,
271 					     vp->reg.sample_mode);
272 }
273 
274 /*
275  * effect table
276  */
277 void
278 snd_emux_create_effect(snd_emux_port_t *p)
279 {
280 	int i;
281 	p->effect = kcalloc(p->chset.max_channels, sizeof(snd_emux_effect_table_t), GFP_KERNEL);
282 	if (p->effect) {
283 		for (i = 0; i < p->chset.max_channels; i++)
284 			p->chset.channels[i].private = p->effect + i;
285 	} else {
286 		for (i = 0; i < p->chset.max_channels; i++)
287 			p->chset.channels[i].private = NULL;
288 	}
289 }
290 
291 void
292 snd_emux_delete_effect(snd_emux_port_t *p)
293 {
294 	kfree(p->effect);
295 	p->effect = NULL;
296 }
297 
298 void
299 snd_emux_clear_effect(snd_emux_port_t *p)
300 {
301 	if (p->effect) {
302 		memset(p->effect, 0, sizeof(snd_emux_effect_table_t) * p->chset.max_channels);
303 	}
304 }
305 
306 #endif /* SNDRV_EMUX_USE_RAW_EFFECT */
307