emu8000_callback.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * synth callback routines for the emu8000 (AWE32/64)
  3. *
  4. * Copyright (C) 1999 Steve Ratcliffe
  5. * Copyright (C) 1999-2000 Takashi Iwai <tiwai@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include "emu8000_local.h"
  22. #include <sound/asoundef.h>
  23. /*
  24. * prototypes
  25. */
  26. static snd_emux_voice_t *get_voice(snd_emux_t *emu, snd_emux_port_t *port);
  27. static int start_voice(snd_emux_voice_t *vp);
  28. static void trigger_voice(snd_emux_voice_t *vp);
  29. static void release_voice(snd_emux_voice_t *vp);
  30. static void update_voice(snd_emux_voice_t *vp, int update);
  31. static void reset_voice(snd_emux_t *emu, int ch);
  32. static void terminate_voice(snd_emux_voice_t *vp);
  33. static void sysex(snd_emux_t *emu, char *buf, int len, int parsed, snd_midi_channel_set_t *chset);
  34. #ifdef CONFIG_SND_SEQUENCER_OSS
  35. static int oss_ioctl(snd_emux_t *emu, int cmd, int p1, int p2);
  36. #endif
  37. static int load_fx(snd_emux_t *emu, int type, int mode, const void __user *buf, long len);
  38. static void set_pitch(emu8000_t *hw, snd_emux_voice_t *vp);
  39. static void set_volume(emu8000_t *hw, snd_emux_voice_t *vp);
  40. static void set_pan(emu8000_t *hw, snd_emux_voice_t *vp);
  41. static void set_fmmod(emu8000_t *hw, snd_emux_voice_t *vp);
  42. static void set_tremfreq(emu8000_t *hw, snd_emux_voice_t *vp);
  43. static void set_fm2frq2(emu8000_t *hw, snd_emux_voice_t *vp);
  44. static void set_filterQ(emu8000_t *hw, snd_emux_voice_t *vp);
  45. static void snd_emu8000_tweak_voice(emu8000_t *emu, int ch);
  46. /*
  47. * Ensure a value is between two points
  48. * macro evaluates its args more than once, so changed to upper-case.
  49. */
  50. #define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
  51. #define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
  52. /*
  53. * set up operators
  54. */
  55. static snd_emux_operators_t emu8000_ops = {
  56. .owner = THIS_MODULE,
  57. .get_voice = get_voice,
  58. .prepare = start_voice,
  59. .trigger = trigger_voice,
  60. .release = release_voice,
  61. .update = update_voice,
  62. .terminate = terminate_voice,
  63. .reset = reset_voice,
  64. .sample_new = snd_emu8000_sample_new,
  65. .sample_free = snd_emu8000_sample_free,
  66. .sample_reset = snd_emu8000_sample_reset,
  67. .load_fx = load_fx,
  68. .sysex = sysex,
  69. #ifdef CONFIG_SND_SEQUENCER_OSS
  70. .oss_ioctl = oss_ioctl,
  71. #endif
  72. };
  73. void
  74. snd_emu8000_ops_setup(emu8000_t *hw)
  75. {
  76. hw->emu->ops = emu8000_ops;
  77. }
  78. /*
  79. * Terminate a voice
  80. */
  81. static void
  82. release_voice(snd_emux_voice_t *vp)
  83. {
  84. int dcysusv;
  85. emu8000_t *hw;
  86. hw = vp->hw;
  87. dcysusv = 0x8000 | (unsigned char)vp->reg.parm.modrelease;
  88. EMU8000_DCYSUS_WRITE(hw, vp->ch, dcysusv);
  89. dcysusv = 0x8000 | (unsigned char)vp->reg.parm.volrelease;
  90. EMU8000_DCYSUSV_WRITE(hw, vp->ch, dcysusv);
  91. }
  92. /*
  93. */
  94. static void
  95. terminate_voice(snd_emux_voice_t *vp)
  96. {
  97. emu8000_t *hw;
  98. hw = vp->hw;
  99. EMU8000_DCYSUSV_WRITE(hw, vp->ch, 0x807F);
  100. }
  101. /*
  102. */
  103. static void
  104. update_voice(snd_emux_voice_t *vp, int update)
  105. {
  106. emu8000_t *hw;
  107. hw = vp->hw;
  108. if (update & SNDRV_EMUX_UPDATE_VOLUME)
  109. set_volume(hw, vp);
  110. if (update & SNDRV_EMUX_UPDATE_PITCH)
  111. set_pitch(hw, vp);
  112. if ((update & SNDRV_EMUX_UPDATE_PAN) &&
  113. vp->port->ctrls[EMUX_MD_REALTIME_PAN])
  114. set_pan(hw, vp);
  115. if (update & SNDRV_EMUX_UPDATE_FMMOD)
  116. set_fmmod(hw, vp);
  117. if (update & SNDRV_EMUX_UPDATE_TREMFREQ)
  118. set_tremfreq(hw, vp);
  119. if (update & SNDRV_EMUX_UPDATE_FM2FRQ2)
  120. set_fm2frq2(hw, vp);
  121. if (update & SNDRV_EMUX_UPDATE_Q)
  122. set_filterQ(hw, vp);
  123. }
  124. /*
  125. * Find a channel (voice) within the EMU that is not in use or at least
  126. * less in use than other channels. Always returns a valid pointer
  127. * no matter what. If there is a real shortage of voices then one
  128. * will be cut. Such is life.
  129. *
  130. * The channel index (vp->ch) must be initialized in this routine.
  131. * In Emu8k, it is identical with the array index.
  132. */
  133. static snd_emux_voice_t *
  134. get_voice(snd_emux_t *emu, snd_emux_port_t *port)
  135. {
  136. int i;
  137. snd_emux_voice_t *vp;
  138. emu8000_t *hw;
  139. /* what we are looking for, in order of preference */
  140. enum {
  141. OFF=0, RELEASED, PLAYING, END
  142. };
  143. /* Keeps track of what we are finding */
  144. struct best {
  145. unsigned int time;
  146. int voice;
  147. } best[END];
  148. struct best *bp;
  149. hw = emu->hw;
  150. for (i = 0; i < END; i++) {
  151. best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */;
  152. best[i].voice = -1;
  153. }
  154. /*
  155. * Go through them all and get a best one to use.
  156. */
  157. for (i = 0; i < emu->max_voices; i++) {
  158. int state, val;
  159. vp = &emu->voices[i];
  160. state = vp->state;
  161. if (state == SNDRV_EMUX_ST_OFF)
  162. bp = best + OFF;
  163. else if (state == SNDRV_EMUX_ST_RELEASED ||
  164. state == SNDRV_EMUX_ST_PENDING) {
  165. bp = best + RELEASED;
  166. val = (EMU8000_CVCF_READ(hw, vp->ch) >> 16) & 0xffff;
  167. if (! val)
  168. bp = best + OFF;
  169. }
  170. else if (state & SNDRV_EMUX_ST_ON)
  171. bp = best + PLAYING;
  172. else
  173. continue;
  174. /* check if sample is finished playing (non-looping only) */
  175. if (state != SNDRV_EMUX_ST_OFF &&
  176. (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_SINGLESHOT)) {
  177. val = EMU8000_CCCA_READ(hw, vp->ch) & 0xffffff;
  178. if (val >= vp->reg.loopstart)
  179. bp = best + OFF;
  180. }
  181. if (vp->time < bp->time) {
  182. bp->time = vp->time;
  183. bp->voice = i;
  184. }
  185. }
  186. for (i = 0; i < END; i++) {
  187. if (best[i].voice >= 0) {
  188. vp = &emu->voices[best[i].voice];
  189. vp->ch = best[i].voice;
  190. return vp;
  191. }
  192. }
  193. /* not found */
  194. return NULL;
  195. }
  196. /*
  197. */
  198. static int
  199. start_voice(snd_emux_voice_t *vp)
  200. {
  201. unsigned int temp;
  202. int ch;
  203. int addr;
  204. snd_midi_channel_t *chan;
  205. emu8000_t *hw;
  206. hw = vp->hw;
  207. ch = vp->ch;
  208. chan = vp->chan;
  209. /* channel to be silent and idle */
  210. EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080);
  211. EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF);
  212. EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF);
  213. EMU8000_PTRX_WRITE(hw, ch, 0);
  214. EMU8000_CPF_WRITE(hw, ch, 0);
  215. /* set pitch offset */
  216. set_pitch(hw, vp);
  217. /* set envelope parameters */
  218. EMU8000_ENVVAL_WRITE(hw, ch, vp->reg.parm.moddelay);
  219. EMU8000_ATKHLD_WRITE(hw, ch, vp->reg.parm.modatkhld);
  220. EMU8000_DCYSUS_WRITE(hw, ch, vp->reg.parm.moddcysus);
  221. EMU8000_ENVVOL_WRITE(hw, ch, vp->reg.parm.voldelay);
  222. EMU8000_ATKHLDV_WRITE(hw, ch, vp->reg.parm.volatkhld);
  223. /* decay/sustain parameter for volume envelope is used
  224. for triggerg the voice */
  225. /* cutoff and volume */
  226. set_volume(hw, vp);
  227. /* modulation envelope heights */
  228. EMU8000_PEFE_WRITE(hw, ch, vp->reg.parm.pefe);
  229. /* lfo1/2 delay */
  230. EMU8000_LFO1VAL_WRITE(hw, ch, vp->reg.parm.lfo1delay);
  231. EMU8000_LFO2VAL_WRITE(hw, ch, vp->reg.parm.lfo2delay);
  232. /* lfo1 pitch & cutoff shift */
  233. set_fmmod(hw, vp);
  234. /* lfo1 volume & freq */
  235. set_tremfreq(hw, vp);
  236. /* lfo2 pitch & freq */
  237. set_fm2frq2(hw, vp);
  238. /* pan & loop start */
  239. set_pan(hw, vp);
  240. /* chorus & loop end (chorus 8bit, MSB) */
  241. addr = vp->reg.loopend - 1;
  242. temp = vp->reg.parm.chorus;
  243. temp += (int)chan->control[MIDI_CTL_E3_CHORUS_DEPTH] * 9 / 10;
  244. LIMITMAX(temp, 255);
  245. temp = (temp <<24) | (unsigned int)addr;
  246. EMU8000_CSL_WRITE(hw, ch, temp);
  247. /* Q & current address (Q 4bit value, MSB) */
  248. addr = vp->reg.start - 1;
  249. temp = vp->reg.parm.filterQ;
  250. temp = (temp<<28) | (unsigned int)addr;
  251. EMU8000_CCCA_WRITE(hw, ch, temp);
  252. /* clear unknown registers */
  253. EMU8000_00A0_WRITE(hw, ch, 0);
  254. EMU8000_0080_WRITE(hw, ch, 0);
  255. /* reset volume */
  256. temp = vp->vtarget << 16;
  257. EMU8000_VTFT_WRITE(hw, ch, temp | vp->ftarget);
  258. EMU8000_CVCF_WRITE(hw, ch, temp | 0xff00);
  259. return 0;
  260. }
  261. /*
  262. * Start envelope
  263. */
  264. static void
  265. trigger_voice(snd_emux_voice_t *vp)
  266. {
  267. int ch = vp->ch;
  268. unsigned int temp;
  269. emu8000_t *hw;
  270. hw = vp->hw;
  271. /* set reverb and pitch target */
  272. temp = vp->reg.parm.reverb;
  273. temp += (int)vp->chan->control[MIDI_CTL_E1_REVERB_DEPTH] * 9 / 10;
  274. LIMITMAX(temp, 255);
  275. temp = (temp << 8) | (vp->ptarget << 16) | vp->aaux;
  276. EMU8000_PTRX_WRITE(hw, ch, temp);
  277. EMU8000_CPF_WRITE(hw, ch, vp->ptarget << 16);
  278. EMU8000_DCYSUSV_WRITE(hw, ch, vp->reg.parm.voldcysus);
  279. }
  280. /*
  281. * reset voice parameters
  282. */
  283. static void
  284. reset_voice(snd_emux_t *emu, int ch)
  285. {
  286. emu8000_t *hw;
  287. hw = emu->hw;
  288. EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F);
  289. snd_emu8000_tweak_voice(hw, ch);
  290. }
  291. /*
  292. * Set the pitch of a possibly playing note.
  293. */
  294. static void
  295. set_pitch(emu8000_t *hw, snd_emux_voice_t *vp)
  296. {
  297. EMU8000_IP_WRITE(hw, vp->ch, vp->apitch);
  298. }
  299. /*
  300. * Set the volume of a possibly already playing note
  301. */
  302. static void
  303. set_volume(emu8000_t *hw, snd_emux_voice_t *vp)
  304. {
  305. int ifatn;
  306. ifatn = (unsigned char)vp->acutoff;
  307. ifatn = (ifatn << 8);
  308. ifatn |= (unsigned char)vp->avol;
  309. EMU8000_IFATN_WRITE(hw, vp->ch, ifatn);
  310. }
  311. /*
  312. * Set pan and loop start address.
  313. */
  314. static void
  315. set_pan(emu8000_t *hw, snd_emux_voice_t *vp)
  316. {
  317. unsigned int temp;
  318. temp = ((unsigned int)vp->apan<<24) | ((unsigned int)vp->reg.loopstart - 1);
  319. EMU8000_PSST_WRITE(hw, vp->ch, temp);
  320. }
  321. #define MOD_SENSE 18
  322. static void
  323. set_fmmod(emu8000_t *hw, snd_emux_voice_t *vp)
  324. {
  325. unsigned short fmmod;
  326. short pitch;
  327. unsigned char cutoff;
  328. int modulation;
  329. pitch = (char)(vp->reg.parm.fmmod>>8);
  330. cutoff = (vp->reg.parm.fmmod & 0xff);
  331. modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
  332. pitch += (MOD_SENSE * modulation) / 1200;
  333. LIMITVALUE(pitch, -128, 127);
  334. fmmod = ((unsigned char)pitch<<8) | cutoff;
  335. EMU8000_FMMOD_WRITE(hw, vp->ch, fmmod);
  336. }
  337. /* set tremolo (lfo1) volume & frequency */
  338. static void
  339. set_tremfreq(emu8000_t *hw, snd_emux_voice_t *vp)
  340. {
  341. EMU8000_TREMFRQ_WRITE(hw, vp->ch, vp->reg.parm.tremfrq);
  342. }
  343. /* set lfo2 pitch & frequency */
  344. static void
  345. set_fm2frq2(emu8000_t *hw, snd_emux_voice_t *vp)
  346. {
  347. unsigned short fm2frq2;
  348. short pitch;
  349. unsigned char freq;
  350. int modulation;
  351. pitch = (char)(vp->reg.parm.fm2frq2>>8);
  352. freq = vp->reg.parm.fm2frq2 & 0xff;
  353. modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
  354. pitch += (MOD_SENSE * modulation) / 1200;
  355. LIMITVALUE(pitch, -128, 127);
  356. fm2frq2 = ((unsigned char)pitch<<8) | freq;
  357. EMU8000_FM2FRQ2_WRITE(hw, vp->ch, fm2frq2);
  358. }
  359. /* set filterQ */
  360. static void
  361. set_filterQ(emu8000_t *hw, snd_emux_voice_t *vp)
  362. {
  363. unsigned int addr;
  364. addr = EMU8000_CCCA_READ(hw, vp->ch) & 0xffffff;
  365. addr |= (vp->reg.parm.filterQ << 28);
  366. EMU8000_CCCA_WRITE(hw, vp->ch, addr);
  367. }
  368. /*
  369. * set the envelope & LFO parameters to the default values
  370. */
  371. static void
  372. snd_emu8000_tweak_voice(emu8000_t *emu, int i)
  373. {
  374. /* set all mod/vol envelope shape to minimum */
  375. EMU8000_ENVVOL_WRITE(emu, i, 0x8000);
  376. EMU8000_ENVVAL_WRITE(emu, i, 0x8000);
  377. EMU8000_DCYSUS_WRITE(emu, i, 0x7F7F);
  378. EMU8000_ATKHLDV_WRITE(emu, i, 0x7F7F);
  379. EMU8000_ATKHLD_WRITE(emu, i, 0x7F7F);
  380. EMU8000_PEFE_WRITE(emu, i, 0); /* mod envelope height to zero */
  381. EMU8000_LFO1VAL_WRITE(emu, i, 0x8000); /* no delay for LFO1 */
  382. EMU8000_LFO2VAL_WRITE(emu, i, 0x8000);
  383. EMU8000_IP_WRITE(emu, i, 0xE000); /* no pitch shift */
  384. EMU8000_IFATN_WRITE(emu, i, 0xFF00); /* volume to minimum */
  385. EMU8000_FMMOD_WRITE(emu, i, 0);
  386. EMU8000_TREMFRQ_WRITE(emu, i, 0);
  387. EMU8000_FM2FRQ2_WRITE(emu, i, 0);
  388. }
  389. /*
  390. * sysex callback
  391. */
  392. static void
  393. sysex(snd_emux_t *emu, char *buf, int len, int parsed, snd_midi_channel_set_t *chset)
  394. {
  395. emu8000_t *hw;
  396. hw = emu->hw;
  397. switch (parsed) {
  398. case SNDRV_MIDI_SYSEX_GS_CHORUS_MODE:
  399. hw->chorus_mode = chset->gs_chorus_mode;
  400. snd_emu8000_update_chorus_mode(hw);
  401. break;
  402. case SNDRV_MIDI_SYSEX_GS_REVERB_MODE:
  403. hw->reverb_mode = chset->gs_reverb_mode;
  404. snd_emu8000_update_reverb_mode(hw);
  405. break;
  406. }
  407. }
  408. #ifdef CONFIG_SND_SEQUENCER_OSS
  409. /*
  410. * OSS ioctl callback
  411. */
  412. static int
  413. oss_ioctl(snd_emux_t *emu, int cmd, int p1, int p2)
  414. {
  415. emu8000_t *hw;
  416. hw = emu->hw;
  417. switch (cmd) {
  418. case _EMUX_OSS_REVERB_MODE:
  419. hw->reverb_mode = p1;
  420. snd_emu8000_update_reverb_mode(hw);
  421. break;
  422. case _EMUX_OSS_CHORUS_MODE:
  423. hw->chorus_mode = p1;
  424. snd_emu8000_update_chorus_mode(hw);
  425. break;
  426. case _EMUX_OSS_INITIALIZE_CHIP:
  427. /* snd_emu8000_init(hw); */ /*ignored*/
  428. break;
  429. case _EMUX_OSS_EQUALIZER:
  430. hw->bass_level = p1;
  431. hw->treble_level = p2;
  432. snd_emu8000_update_equalizer(hw);
  433. break;
  434. }
  435. return 0;
  436. }
  437. #endif
  438. /*
  439. * additional patch keys
  440. */
  441. #define SNDRV_EMU8000_LOAD_CHORUS_FX 0x10 /* optarg=mode */
  442. #define SNDRV_EMU8000_LOAD_REVERB_FX 0x11 /* optarg=mode */
  443. /*
  444. * callback routine
  445. */
  446. static int
  447. load_fx(snd_emux_t *emu, int type, int mode, const void __user *buf, long len)
  448. {
  449. emu8000_t *hw;
  450. hw = emu->hw;
  451. /* skip header */
  452. buf += 16;
  453. len -= 16;
  454. switch (type) {
  455. case SNDRV_EMU8000_LOAD_CHORUS_FX:
  456. return snd_emu8000_load_chorus_fx(hw, mode, buf, len);
  457. case SNDRV_EMU8000_LOAD_REVERB_FX:
  458. return snd_emu8000_load_reverb_fx(hw, mode, buf, len);
  459. }
  460. return -EINVAL;
  461. }