emux_nrpn.c 9.7 KB

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