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