1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * NRPN / SYSEX callbacks for Emu8k/Emu10k1
5 * Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
8 #include "emux_voice.h"
9 #include <sound/asoundef.h>
12 * conversion from NRPN/control parameters to Emu8000 raw parameters
15 /* NRPN / CC -> Emu8000 parameter converter */
16 struct nrpn_conv_table
{
19 int (*convert
)(int val
);
22 /* effect sensitivity */
25 #define FX_RESONANCE 1
34 * convert NRPN/control values
37 static int send_converted_effect(const struct nrpn_conv_table
*table
,
39 struct snd_emux_port
*port
,
40 struct snd_midi_channel
*chan
,
41 int type
, int val
, int mode
)
44 for (i
= 0; i
< num_tables
; i
++) {
45 if (table
[i
].control
== type
) {
46 cval
= table
[i
].convert(val
);
47 snd_emux_send_effect(port
, chan
, table
[i
].effect
,
55 #define DEF_FX_CUTOFF 170
56 #define DEF_FX_RESONANCE 6
57 #define DEF_FX_ATTACK 50
58 #define DEF_FX_RELEASE 50
59 #define DEF_FX_VIBRATE 30
60 #define DEF_FX_VIBDEPTH 4
61 #define DEF_FX_VIBDELAY 1500
63 /* effect sensitivities for GS NRPN:
64 * adjusted for chaos 8MB soundfonts
66 static const int gs_sense
[] =
68 DEF_FX_CUTOFF
, DEF_FX_RESONANCE
, DEF_FX_ATTACK
, DEF_FX_RELEASE
,
69 DEF_FX_VIBRATE
, DEF_FX_VIBDEPTH
, DEF_FX_VIBDELAY
72 /* effect sensitivies for XG controls:
73 * adjusted for chaos 8MB soundfonts
75 static const int xg_sense
[] =
77 DEF_FX_CUTOFF
, DEF_FX_RESONANCE
, DEF_FX_ATTACK
, DEF_FX_RELEASE
,
78 DEF_FX_VIBRATE
, DEF_FX_VIBDEPTH
, DEF_FX_VIBDELAY
86 static int fx_delay(int val
);
87 static int fx_attack(int val
);
88 static int fx_hold(int val
);
89 static int fx_decay(int val
);
90 static int fx_the_value(int val
);
91 static int fx_twice_value(int val
);
92 static int fx_conv_pitch(int val
);
93 static int fx_conv_Q(int val
);
95 /* function for each NRPN */ /* [range] units */
96 #define fx_env1_delay fx_delay /* [0,5900] 4msec */
97 #define fx_env1_attack fx_attack /* [0,5940] 1msec */
98 #define fx_env1_hold fx_hold /* [0,8191] 1msec */
99 #define fx_env1_decay fx_decay /* [0,5940] 4msec */
100 #define fx_env1_release fx_decay /* [0,5940] 4msec */
101 #define fx_env1_sustain fx_the_value /* [0,127] 0.75dB */
102 #define fx_env1_pitch fx_the_value /* [-127,127] 9.375cents */
103 #define fx_env1_cutoff fx_the_value /* [-127,127] 56.25cents */
105 #define fx_env2_delay fx_delay /* [0,5900] 4msec */
106 #define fx_env2_attack fx_attack /* [0,5940] 1msec */
107 #define fx_env2_hold fx_hold /* [0,8191] 1msec */
108 #define fx_env2_decay fx_decay /* [0,5940] 4msec */
109 #define fx_env2_release fx_decay /* [0,5940] 4msec */
110 #define fx_env2_sustain fx_the_value /* [0,127] 0.75dB */
112 #define fx_lfo1_delay fx_delay /* [0,5900] 4msec */
113 #define fx_lfo1_freq fx_twice_value /* [0,127] 84mHz */
114 #define fx_lfo1_volume fx_twice_value /* [0,127] 0.1875dB */
115 #define fx_lfo1_pitch fx_the_value /* [-127,127] 9.375cents */
116 #define fx_lfo1_cutoff fx_twice_value /* [-64,63] 56.25cents */
118 #define fx_lfo2_delay fx_delay /* [0,5900] 4msec */
119 #define fx_lfo2_freq fx_twice_value /* [0,127] 84mHz */
120 #define fx_lfo2_pitch fx_the_value /* [-127,127] 9.375cents */
122 #define fx_init_pitch fx_conv_pitch /* [-8192,8192] cents */
123 #define fx_chorus fx_the_value /* [0,255] -- */
124 #define fx_reverb fx_the_value /* [0,255] -- */
125 #define fx_cutoff fx_twice_value /* [0,127] 62Hz */
126 #define fx_filterQ fx_conv_Q /* [0,127] -- */
128 static int fx_delay(int val
)
130 return (unsigned short)snd_sf_calc_parm_delay(val
);
133 static int fx_attack(int val
)
135 return (unsigned short)snd_sf_calc_parm_attack(val
);
138 static int fx_hold(int val
)
140 return (unsigned short)snd_sf_calc_parm_hold(val
);
143 static int fx_decay(int val
)
145 return (unsigned short)snd_sf_calc_parm_decay(val
);
148 static int fx_the_value(int val
)
150 return (unsigned short)(val
& 0xff);
153 static int fx_twice_value(int val
)
155 return (unsigned short)((val
* 2) & 0xff);
158 static int fx_conv_pitch(int val
)
160 return (short)(val
* 4096 / 1200);
163 static int fx_conv_Q(int val
)
165 return (unsigned short)((val
/ 8) & 0xff);
169 static const struct nrpn_conv_table awe_effects
[] =
171 { 0, EMUX_FX_LFO1_DELAY
, fx_lfo1_delay
},
172 { 1, EMUX_FX_LFO1_FREQ
, fx_lfo1_freq
},
173 { 2, EMUX_FX_LFO2_DELAY
, fx_lfo2_delay
},
174 { 3, EMUX_FX_LFO2_FREQ
, fx_lfo2_freq
},
176 { 4, EMUX_FX_ENV1_DELAY
, fx_env1_delay
},
177 { 5, EMUX_FX_ENV1_ATTACK
,fx_env1_attack
},
178 { 6, EMUX_FX_ENV1_HOLD
, fx_env1_hold
},
179 { 7, EMUX_FX_ENV1_DECAY
, fx_env1_decay
},
180 { 8, EMUX_FX_ENV1_SUSTAIN
, fx_env1_sustain
},
181 { 9, EMUX_FX_ENV1_RELEASE
, fx_env1_release
},
183 {10, EMUX_FX_ENV2_DELAY
, fx_env2_delay
},
184 {11, EMUX_FX_ENV2_ATTACK
, fx_env2_attack
},
185 {12, EMUX_FX_ENV2_HOLD
, fx_env2_hold
},
186 {13, EMUX_FX_ENV2_DECAY
, fx_env2_decay
},
187 {14, EMUX_FX_ENV2_SUSTAIN
, fx_env2_sustain
},
188 {15, EMUX_FX_ENV2_RELEASE
, fx_env2_release
},
190 {16, EMUX_FX_INIT_PITCH
, fx_init_pitch
},
191 {17, EMUX_FX_LFO1_PITCH
, fx_lfo1_pitch
},
192 {18, EMUX_FX_LFO2_PITCH
, fx_lfo2_pitch
},
193 {19, EMUX_FX_ENV1_PITCH
, fx_env1_pitch
},
194 {20, EMUX_FX_LFO1_VOLUME
, fx_lfo1_volume
},
195 {21, EMUX_FX_CUTOFF
, fx_cutoff
},
196 {22, EMUX_FX_FILTERQ
, fx_filterQ
},
197 {23, EMUX_FX_LFO1_CUTOFF
, fx_lfo1_cutoff
},
198 {24, EMUX_FX_ENV1_CUTOFF
, fx_env1_cutoff
},
199 {25, EMUX_FX_CHORUS
, fx_chorus
},
200 {26, EMUX_FX_REVERB
, fx_reverb
},
205 * GS(SC88) NRPN effects; still experimental
208 /* cutoff: quarter semitone step, max=255 */
209 static int gs_cutoff(int val
)
211 return (val
- 64) * gs_sense
[FX_CUTOFF
] / 50;
214 /* resonance: 0 to 15(max) */
215 static int gs_filterQ(int val
)
217 return (val
- 64) * gs_sense
[FX_RESONANCE
] / 50;
221 static int gs_attack(int val
)
223 return -(val
- 64) * gs_sense
[FX_ATTACK
] / 50;
227 static int gs_decay(int val
)
229 return -(val
- 64) * gs_sense
[FX_RELEASE
] / 50;
233 static int gs_release(int val
)
235 return -(val
- 64) * gs_sense
[FX_RELEASE
] / 50;
238 /* vibrato freq: 0.042Hz step, max=255 */
239 static int gs_vib_rate(int val
)
241 return (val
- 64) * gs_sense
[FX_VIBRATE
] / 50;
244 /* vibrato depth: max=127, 1 octave */
245 static int gs_vib_depth(int val
)
247 return (val
- 64) * gs_sense
[FX_VIBDEPTH
] / 50;
250 /* vibrato delay: -0.725msec step */
251 static int gs_vib_delay(int val
)
253 return -(val
- 64) * gs_sense
[FX_VIBDELAY
] / 50;
256 static const struct nrpn_conv_table gs_effects
[] =
258 {32, EMUX_FX_CUTOFF
, gs_cutoff
},
259 {33, EMUX_FX_FILTERQ
, gs_filterQ
},
260 {99, EMUX_FX_ENV2_ATTACK
, gs_attack
},
261 {100, EMUX_FX_ENV2_DECAY
, gs_decay
},
262 {102, EMUX_FX_ENV2_RELEASE
, gs_release
},
263 {8, EMUX_FX_LFO1_FREQ
, gs_vib_rate
},
264 {9, EMUX_FX_LFO1_VOLUME
, gs_vib_depth
},
265 {10, EMUX_FX_LFO1_DELAY
, gs_vib_delay
},
273 snd_emux_nrpn(void *p
, struct snd_midi_channel
*chan
,
274 struct snd_midi_channel_set
*chset
)
276 struct snd_emux_port
*port
;
279 if (snd_BUG_ON(!port
|| !chan
))
282 if (chan
->control
[MIDI_CTL_NONREG_PARM_NUM_MSB
] == 127 &&
283 chan
->control
[MIDI_CTL_NONREG_PARM_NUM_LSB
] <= 26) {
285 /* Win/DOS AWE32 specific NRPNs */
286 /* both MSB/LSB necessary */
287 val
= (chan
->control
[MIDI_CTL_MSB_DATA_ENTRY
] << 7) |
288 chan
->control
[MIDI_CTL_LSB_DATA_ENTRY
];
290 send_converted_effect
291 (awe_effects
, ARRAY_SIZE(awe_effects
),
292 port
, chan
, chan
->control
[MIDI_CTL_NONREG_PARM_NUM_LSB
],
293 val
, EMUX_FX_FLAG_SET
);
297 if (port
->chset
.midi_mode
== SNDRV_MIDI_MODE_GS
&&
298 chan
->control
[MIDI_CTL_NONREG_PARM_NUM_MSB
] == 1) {
300 /* GS specific NRPNs */
301 /* only MSB is valid */
302 val
= chan
->control
[MIDI_CTL_MSB_DATA_ENTRY
];
303 send_converted_effect
304 (gs_effects
, ARRAY_SIZE(gs_effects
),
305 port
, chan
, chan
->control
[MIDI_CTL_NONREG_PARM_NUM_LSB
],
306 val
, EMUX_FX_FLAG_ADD
);
313 * XG control effects; still experimental
316 /* cutoff: quarter semitone step, max=255 */
317 static int xg_cutoff(int val
)
319 return (val
- 64) * xg_sense
[FX_CUTOFF
] / 64;
322 /* resonance: 0(open) to 15(most nasal) */
323 static int xg_filterQ(int val
)
325 return (val
- 64) * xg_sense
[FX_RESONANCE
] / 64;
329 static int xg_attack(int val
)
331 return -(val
- 64) * xg_sense
[FX_ATTACK
] / 64;
335 static int xg_release(int val
)
337 return -(val
- 64) * xg_sense
[FX_RELEASE
] / 64;
340 static const struct nrpn_conv_table xg_effects
[] =
342 {71, EMUX_FX_CUTOFF
, xg_cutoff
},
343 {74, EMUX_FX_FILTERQ
, xg_filterQ
},
344 {72, EMUX_FX_ENV2_RELEASE
, xg_release
},
345 {73, EMUX_FX_ENV2_ATTACK
, xg_attack
},
349 snd_emux_xg_control(struct snd_emux_port
*port
, struct snd_midi_channel
*chan
,
352 return send_converted_effect(xg_effects
, ARRAY_SIZE(xg_effects
),
354 chan
->control
[param
],
362 snd_emux_sysex(void *p
, unsigned char *buf
, int len
, int parsed
,
363 struct snd_midi_channel_set
*chset
)
365 struct snd_emux_port
*port
;
366 struct snd_emux
*emu
;
369 if (snd_BUG_ON(!port
|| !chset
))
374 case SNDRV_MIDI_SYSEX_GS_MASTER_VOLUME
:
375 snd_emux_update_port(port
, SNDRV_EMUX_UPDATE_VOLUME
);
379 emu
->ops
.sysex(emu
, buf
, len
, parsed
, chset
);