emu8000_callback.c 13 KB

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