emux_nrpn.c 9.7 KB

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