emux_nrpn.c 9.7 KB

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