emu8000_callback.c 13 KB

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