emu8000_callback.c 13 KB

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