xref: /openbmc/linux/sound/synth/emux/emux_nrpn.c (revision 1da177e4)
1 /*
2  *  NRPN / SYSEX callbacks for Emu8k/Emu10k1
3  *
4  *  Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21 
22 #include "emux_voice.h"
23 #include <sound/asoundef.h>
24 
25 /*
26  * conversion from NRPN/control parameters to Emu8000 raw parameters
27  */
28 
29 /* NRPN / CC -> Emu8000 parameter converter */
30 typedef struct {
31 	int control;
32 	int effect;
33 	int (*convert)(int val);
34 } nrpn_conv_table;
35 
36 /* effect sensitivity */
37 
38 #define FX_CUTOFF	0
39 #define FX_RESONANCE	1
40 #define FX_ATTACK	2
41 #define FX_RELEASE	3
42 #define FX_VIBRATE	4
43 #define FX_VIBDEPTH	5
44 #define FX_VIBDELAY	6
45 #define FX_NUMS		7
46 
47 /*
48  * convert NRPN/control values
49  */
50 
51 static int send_converted_effect(nrpn_conv_table *table, int num_tables,
52 				 snd_emux_port_t *port, snd_midi_channel_t *chan,
53 				 int type, int val, int mode)
54 {
55 	int i, cval;
56 	for (i = 0; i < num_tables; i++) {
57 		if (table[i].control == type) {
58 			cval = table[i].convert(val);
59 			snd_emux_send_effect(port, chan, table[i].effect,
60 					     cval, mode);
61 			return 1;
62 		}
63 	}
64 	return 0;
65 }
66 
67 #define DEF_FX_CUTOFF		170
68 #define DEF_FX_RESONANCE	6
69 #define DEF_FX_ATTACK		50
70 #define DEF_FX_RELEASE		50
71 #define DEF_FX_VIBRATE		30
72 #define DEF_FX_VIBDEPTH		4
73 #define DEF_FX_VIBDELAY		1500
74 
75 /* effect sensitivities for GS NRPN:
76  *  adjusted for chaos 8MB soundfonts
77  */
78 static int gs_sense[] =
79 {
80 	DEF_FX_CUTOFF, DEF_FX_RESONANCE, DEF_FX_ATTACK, DEF_FX_RELEASE,
81 	DEF_FX_VIBRATE, DEF_FX_VIBDEPTH, DEF_FX_VIBDELAY
82 };
83 
84 /* effect sensitivies for XG controls:
85  * adjusted for chaos 8MB soundfonts
86  */
87 static int xg_sense[] =
88 {
89 	DEF_FX_CUTOFF, DEF_FX_RESONANCE, DEF_FX_ATTACK, DEF_FX_RELEASE,
90 	DEF_FX_VIBRATE, DEF_FX_VIBDEPTH, DEF_FX_VIBDELAY
91 };
92 
93 
94 /*
95  * AWE32 NRPN effects
96  */
97 
98 static int fx_delay(int val);
99 static int fx_attack(int val);
100 static int fx_hold(int val);
101 static int fx_decay(int val);
102 static int fx_the_value(int val);
103 static int fx_twice_value(int val);
104 static int fx_conv_pitch(int val);
105 static int fx_conv_Q(int val);
106 
107 /* function for each NRPN */		/* [range]  units */
108 #define fx_env1_delay	fx_delay	/* [0,5900] 4msec */
109 #define fx_env1_attack	fx_attack	/* [0,5940] 1msec */
110 #define fx_env1_hold	fx_hold		/* [0,8191] 1msec */
111 #define fx_env1_decay	fx_decay	/* [0,5940] 4msec */
112 #define fx_env1_release	fx_decay	/* [0,5940] 4msec */
113 #define fx_env1_sustain	fx_the_value	/* [0,127] 0.75dB */
114 #define fx_env1_pitch	fx_the_value	/* [-127,127] 9.375cents */
115 #define fx_env1_cutoff	fx_the_value	/* [-127,127] 56.25cents */
116 
117 #define fx_env2_delay	fx_delay	/* [0,5900] 4msec */
118 #define fx_env2_attack	fx_attack	/* [0,5940] 1msec */
119 #define fx_env2_hold	fx_hold		/* [0,8191] 1msec */
120 #define fx_env2_decay	fx_decay	/* [0,5940] 4msec */
121 #define fx_env2_release	fx_decay	/* [0,5940] 4msec */
122 #define fx_env2_sustain	fx_the_value	/* [0,127] 0.75dB */
123 
124 #define fx_lfo1_delay	fx_delay	/* [0,5900] 4msec */
125 #define fx_lfo1_freq	fx_twice_value	/* [0,127] 84mHz */
126 #define fx_lfo1_volume	fx_twice_value	/* [0,127] 0.1875dB */
127 #define fx_lfo1_pitch	fx_the_value	/* [-127,127] 9.375cents */
128 #define fx_lfo1_cutoff	fx_twice_value	/* [-64,63] 56.25cents */
129 
130 #define fx_lfo2_delay	fx_delay	/* [0,5900] 4msec */
131 #define fx_lfo2_freq	fx_twice_value	/* [0,127] 84mHz */
132 #define fx_lfo2_pitch	fx_the_value	/* [-127,127] 9.375cents */
133 
134 #define fx_init_pitch	fx_conv_pitch	/* [-8192,8192] cents */
135 #define fx_chorus	fx_the_value	/* [0,255] -- */
136 #define fx_reverb	fx_the_value	/* [0,255] -- */
137 #define fx_cutoff	fx_twice_value	/* [0,127] 62Hz */
138 #define fx_filterQ	fx_conv_Q	/* [0,127] -- */
139 
140 static int fx_delay(int val)
141 {
142 	return (unsigned short)snd_sf_calc_parm_delay(val);
143 }
144 
145 static int fx_attack(int val)
146 {
147 	return (unsigned short)snd_sf_calc_parm_attack(val);
148 }
149 
150 static int fx_hold(int val)
151 {
152 	return (unsigned short)snd_sf_calc_parm_hold(val);
153 }
154 
155 static int fx_decay(int val)
156 {
157 	return (unsigned short)snd_sf_calc_parm_decay(val);
158 }
159 
160 static int fx_the_value(int val)
161 {
162 	return (unsigned short)(val & 0xff);
163 }
164 
165 static int fx_twice_value(int val)
166 {
167 	return (unsigned short)((val * 2) & 0xff);
168 }
169 
170 static int fx_conv_pitch(int val)
171 {
172 	return (short)(val * 4096 / 1200);
173 }
174 
175 static int fx_conv_Q(int val)
176 {
177 	return (unsigned short)((val / 8) & 0xff);
178 }
179 
180 
181 static nrpn_conv_table awe_effects[] =
182 {
183 	{ 0, EMUX_FX_LFO1_DELAY,	fx_lfo1_delay},
184 	{ 1, EMUX_FX_LFO1_FREQ,	fx_lfo1_freq},
185 	{ 2, EMUX_FX_LFO2_DELAY,	fx_lfo2_delay},
186 	{ 3, EMUX_FX_LFO2_FREQ,	fx_lfo2_freq},
187 
188 	{ 4, EMUX_FX_ENV1_DELAY,	fx_env1_delay},
189 	{ 5, EMUX_FX_ENV1_ATTACK,fx_env1_attack},
190 	{ 6, EMUX_FX_ENV1_HOLD,	fx_env1_hold},
191 	{ 7, EMUX_FX_ENV1_DECAY,	fx_env1_decay},
192 	{ 8, EMUX_FX_ENV1_SUSTAIN,	fx_env1_sustain},
193 	{ 9, EMUX_FX_ENV1_RELEASE,	fx_env1_release},
194 
195 	{10, EMUX_FX_ENV2_DELAY,	fx_env2_delay},
196 	{11, EMUX_FX_ENV2_ATTACK,	fx_env2_attack},
197 	{12, EMUX_FX_ENV2_HOLD,	fx_env2_hold},
198 	{13, EMUX_FX_ENV2_DECAY,	fx_env2_decay},
199 	{14, EMUX_FX_ENV2_SUSTAIN,	fx_env2_sustain},
200 	{15, EMUX_FX_ENV2_RELEASE,	fx_env2_release},
201 
202 	{16, EMUX_FX_INIT_PITCH,	fx_init_pitch},
203 	{17, EMUX_FX_LFO1_PITCH,	fx_lfo1_pitch},
204 	{18, EMUX_FX_LFO2_PITCH,	fx_lfo2_pitch},
205 	{19, EMUX_FX_ENV1_PITCH,	fx_env1_pitch},
206 	{20, EMUX_FX_LFO1_VOLUME,	fx_lfo1_volume},
207 	{21, EMUX_FX_CUTOFF,		fx_cutoff},
208 	{22, EMUX_FX_FILTERQ,	fx_filterQ},
209 	{23, EMUX_FX_LFO1_CUTOFF,	fx_lfo1_cutoff},
210 	{24, EMUX_FX_ENV1_CUTOFF,	fx_env1_cutoff},
211 	{25, EMUX_FX_CHORUS,		fx_chorus},
212 	{26, EMUX_FX_REVERB,		fx_reverb},
213 };
214 
215 
216 /*
217  * GS(SC88) NRPN effects; still experimental
218  */
219 
220 /* cutoff: quarter semitone step, max=255 */
221 static int gs_cutoff(int val)
222 {
223 	return (val - 64) * gs_sense[FX_CUTOFF] / 50;
224 }
225 
226 /* resonance: 0 to 15(max) */
227 static int gs_filterQ(int val)
228 {
229 	return (val - 64) * gs_sense[FX_RESONANCE] / 50;
230 }
231 
232 /* attack: */
233 static int gs_attack(int val)
234 {
235 	return -(val - 64) * gs_sense[FX_ATTACK] / 50;
236 }
237 
238 /* decay: */
239 static int gs_decay(int val)
240 {
241 	return -(val - 64) * gs_sense[FX_RELEASE] / 50;
242 }
243 
244 /* release: */
245 static int gs_release(int val)
246 {
247 	return -(val - 64) * gs_sense[FX_RELEASE] / 50;
248 }
249 
250 /* vibrato freq: 0.042Hz step, max=255 */
251 static int gs_vib_rate(int val)
252 {
253 	return (val - 64) * gs_sense[FX_VIBRATE] / 50;
254 }
255 
256 /* vibrato depth: max=127, 1 octave */
257 static int gs_vib_depth(int val)
258 {
259 	return (val - 64) * gs_sense[FX_VIBDEPTH] / 50;
260 }
261 
262 /* vibrato delay: -0.725msec step */
263 static int gs_vib_delay(int val)
264 {
265 	return -(val - 64) * gs_sense[FX_VIBDELAY] / 50;
266 }
267 
268 static nrpn_conv_table gs_effects[] =
269 {
270 	{32, EMUX_FX_CUTOFF,	gs_cutoff},
271 	{33, EMUX_FX_FILTERQ,	gs_filterQ},
272 	{99, EMUX_FX_ENV2_ATTACK, gs_attack},
273 	{100, EMUX_FX_ENV2_DECAY, gs_decay},
274 	{102, EMUX_FX_ENV2_RELEASE, gs_release},
275 	{8, EMUX_FX_LFO1_FREQ, gs_vib_rate},
276 	{9, EMUX_FX_LFO1_VOLUME, gs_vib_depth},
277 	{10, EMUX_FX_LFO1_DELAY, gs_vib_delay},
278 };
279 
280 
281 /*
282  * NRPN events
283  */
284 void
285 snd_emux_nrpn(void *p, snd_midi_channel_t *chan, snd_midi_channel_set_t *chset)
286 {
287 	snd_emux_port_t *port;
288 
289 	port = p;
290 	snd_assert(port != NULL, return);
291 	snd_assert(chan != NULL, return);
292 
293 	if (chan->control[MIDI_CTL_NONREG_PARM_NUM_MSB] == 127 &&
294 	    chan->control[MIDI_CTL_NONREG_PARM_NUM_LSB] <= 26) {
295 		int val;
296 		/* Win/DOS AWE32 specific NRPNs */
297 		/* both MSB/LSB necessary */
298 		val = (chan->control[MIDI_CTL_MSB_DATA_ENTRY] << 7) |
299 			chan->control[MIDI_CTL_LSB_DATA_ENTRY];
300 		val -= 8192;
301 		send_converted_effect
302 			(awe_effects, ARRAY_SIZE(awe_effects),
303 			 port, chan, chan->control[MIDI_CTL_NONREG_PARM_NUM_LSB],
304 			 val, EMUX_FX_FLAG_SET);
305 		return;
306 	}
307 
308 	if (port->chset.midi_mode == SNDRV_MIDI_MODE_GS &&
309 	    chan->control[MIDI_CTL_NONREG_PARM_NUM_MSB] == 1) {
310 		int val;
311 		/* GS specific NRPNs */
312 		/* only MSB is valid */
313 		val = chan->control[MIDI_CTL_MSB_DATA_ENTRY];
314 		send_converted_effect
315 			(gs_effects, ARRAY_SIZE(gs_effects),
316 			 port, chan, chan->control[MIDI_CTL_NONREG_PARM_NUM_LSB],
317 			 val, EMUX_FX_FLAG_ADD);
318 		return;
319 	}
320 }
321 
322 
323 /*
324  * XG control effects; still experimental
325  */
326 
327 /* cutoff: quarter semitone step, max=255 */
328 static int xg_cutoff(int val)
329 {
330 	return (val - 64) * xg_sense[FX_CUTOFF] / 64;
331 }
332 
333 /* resonance: 0(open) to 15(most nasal) */
334 static int xg_filterQ(int val)
335 {
336 	return (val - 64) * xg_sense[FX_RESONANCE] / 64;
337 }
338 
339 /* attack: */
340 static int xg_attack(int val)
341 {
342 	return -(val - 64) * xg_sense[FX_ATTACK] / 64;
343 }
344 
345 /* release: */
346 static int xg_release(int val)
347 {
348 	return -(val - 64) * xg_sense[FX_RELEASE] / 64;
349 }
350 
351 static nrpn_conv_table xg_effects[] =
352 {
353 	{71, EMUX_FX_CUTOFF,	xg_cutoff},
354 	{74, EMUX_FX_FILTERQ,	xg_filterQ},
355 	{72, EMUX_FX_ENV2_RELEASE, xg_release},
356 	{73, EMUX_FX_ENV2_ATTACK, xg_attack},
357 };
358 
359 int
360 snd_emux_xg_control(snd_emux_port_t *port, snd_midi_channel_t *chan, int param)
361 {
362 	return send_converted_effect(xg_effects, ARRAY_SIZE(xg_effects),
363 				     port, chan, param,
364 				     chan->control[param],
365 				     EMUX_FX_FLAG_ADD);
366 }
367 
368 /*
369  * receive sysex
370  */
371 void
372 snd_emux_sysex(void *p, unsigned char *buf, int len, int parsed, snd_midi_channel_set_t *chset)
373 {
374 	snd_emux_port_t *port;
375 	snd_emux_t *emu;
376 
377 	port = p;
378 	snd_assert(port != NULL, return);
379 	snd_assert(chset != NULL, return);
380 	emu = port->emu;
381 
382 	switch (parsed) {
383 	case SNDRV_MIDI_SYSEX_GS_MASTER_VOLUME:
384 		snd_emux_update_port(port, SNDRV_EMUX_UPDATE_VOLUME);
385 		break;
386 	default:
387 		if (emu->ops.sysex)
388 			emu->ops.sysex(emu, buf, len, parsed, chset);
389 		break;
390 	}
391 }
392 
393