emu10k1_callback.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * synth callback routines for Emu10k1
  4. *
  5. * Copyright (C) 2000 Takashi Iwai <[email protected]>
  6. */
  7. #include <linux/export.h>
  8. #include "emu10k1_synth_local.h"
  9. #include <sound/asoundef.h>
  10. /* voice status */
  11. enum {
  12. V_FREE=0, V_OFF, V_RELEASED, V_PLAYING, V_END
  13. };
  14. /* Keeps track of what we are finding */
  15. struct best_voice {
  16. unsigned int time;
  17. int voice;
  18. };
  19. /*
  20. * prototypes
  21. */
  22. static void lookup_voices(struct snd_emux *emux, struct snd_emu10k1 *hw,
  23. struct best_voice *best, int active_only);
  24. static struct snd_emux_voice *get_voice(struct snd_emux *emux,
  25. struct snd_emux_port *port);
  26. static int start_voice(struct snd_emux_voice *vp);
  27. static void trigger_voice(struct snd_emux_voice *vp);
  28. static void release_voice(struct snd_emux_voice *vp);
  29. static void update_voice(struct snd_emux_voice *vp, int update);
  30. static void terminate_voice(struct snd_emux_voice *vp);
  31. static void free_voice(struct snd_emux_voice *vp);
  32. static void set_fmmod(struct snd_emu10k1 *hw, struct snd_emux_voice *vp);
  33. static void set_fm2frq2(struct snd_emu10k1 *hw, struct snd_emux_voice *vp);
  34. static void set_filterQ(struct snd_emu10k1 *hw, struct snd_emux_voice *vp);
  35. /*
  36. * Ensure a value is between two points
  37. * macro evaluates its args more than once, so changed to upper-case.
  38. */
  39. #define LIMITVALUE(x, a, b) do { if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b); } while (0)
  40. #define LIMITMAX(x, a) do {if ((x) > (a)) (x) = (a); } while (0)
  41. /*
  42. * set up operators
  43. */
  44. static const struct snd_emux_operators emu10k1_ops = {
  45. .owner = THIS_MODULE,
  46. .get_voice = get_voice,
  47. .prepare = start_voice,
  48. .trigger = trigger_voice,
  49. .release = release_voice,
  50. .update = update_voice,
  51. .terminate = terminate_voice,
  52. .free_voice = free_voice,
  53. .sample_new = snd_emu10k1_sample_new,
  54. .sample_free = snd_emu10k1_sample_free,
  55. };
  56. void
  57. snd_emu10k1_ops_setup(struct snd_emux *emux)
  58. {
  59. emux->ops = emu10k1_ops;
  60. }
  61. /*
  62. * get more voice for pcm
  63. *
  64. * terminate most inactive voice and give it as a pcm voice.
  65. *
  66. * voice_lock is already held.
  67. */
  68. int
  69. snd_emu10k1_synth_get_voice(struct snd_emu10k1 *hw)
  70. {
  71. struct snd_emux *emu;
  72. struct snd_emux_voice *vp;
  73. struct best_voice best[V_END];
  74. int i;
  75. emu = hw->synth;
  76. lookup_voices(emu, hw, best, 1); /* no OFF voices */
  77. for (i = 0; i < V_END; i++) {
  78. if (best[i].voice >= 0) {
  79. int ch;
  80. vp = &emu->voices[best[i].voice];
  81. ch = vp->ch;
  82. if (ch < 0) {
  83. /*
  84. dev_warn(emu->card->dev,
  85. "synth_get_voice: ch < 0 (%d) ??", i);
  86. */
  87. continue;
  88. }
  89. vp->emu->num_voices--;
  90. vp->ch = -1;
  91. vp->state = SNDRV_EMUX_ST_OFF;
  92. return ch;
  93. }
  94. }
  95. /* not found */
  96. return -ENOMEM;
  97. }
  98. /*
  99. * turn off the voice (not terminated)
  100. */
  101. static void
  102. release_voice(struct snd_emux_voice *vp)
  103. {
  104. int dcysusv;
  105. struct snd_emu10k1 *hw;
  106. hw = vp->hw;
  107. dcysusv = 0x8000 | (unsigned char)vp->reg.parm.modrelease;
  108. snd_emu10k1_ptr_write(hw, DCYSUSM, vp->ch, dcysusv);
  109. dcysusv = 0x8000 | (unsigned char)vp->reg.parm.volrelease | DCYSUSV_CHANNELENABLE_MASK;
  110. snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, dcysusv);
  111. }
  112. /*
  113. * terminate the voice
  114. */
  115. static void
  116. terminate_voice(struct snd_emux_voice *vp)
  117. {
  118. struct snd_emu10k1 *hw;
  119. if (snd_BUG_ON(!vp))
  120. return;
  121. hw = vp->hw;
  122. snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0x807f | DCYSUSV_CHANNELENABLE_MASK);
  123. if (vp->block) {
  124. struct snd_emu10k1_memblk *emem;
  125. emem = (struct snd_emu10k1_memblk *)vp->block;
  126. if (emem->map_locked > 0)
  127. emem->map_locked--;
  128. }
  129. }
  130. /*
  131. * release the voice to system
  132. */
  133. static void
  134. free_voice(struct snd_emux_voice *vp)
  135. {
  136. struct snd_emu10k1 *hw;
  137. hw = vp->hw;
  138. /* FIXME: emu10k1_synth is broken. */
  139. /* This can get called with hw == 0 */
  140. /* Problem apparent on plug, unplug then plug */
  141. /* on the Audigy 2 ZS Notebook. */
  142. if (hw && (vp->ch >= 0)) {
  143. snd_emu10k1_ptr_write(hw, IFATN, vp->ch, 0xff00);
  144. snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0x807f | DCYSUSV_CHANNELENABLE_MASK);
  145. // snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0);
  146. snd_emu10k1_ptr_write(hw, VTFT, vp->ch, 0xffff);
  147. snd_emu10k1_ptr_write(hw, CVCF, vp->ch, 0xffff);
  148. snd_emu10k1_voice_free(hw, &hw->voices[vp->ch]);
  149. vp->emu->num_voices--;
  150. vp->ch = -1;
  151. }
  152. }
  153. /*
  154. * update registers
  155. */
  156. static void
  157. update_voice(struct snd_emux_voice *vp, int update)
  158. {
  159. struct snd_emu10k1 *hw;
  160. hw = vp->hw;
  161. if (update & SNDRV_EMUX_UPDATE_VOLUME)
  162. snd_emu10k1_ptr_write(hw, IFATN_ATTENUATION, vp->ch, vp->avol);
  163. if (update & SNDRV_EMUX_UPDATE_PITCH)
  164. snd_emu10k1_ptr_write(hw, IP, vp->ch, vp->apitch);
  165. if (update & SNDRV_EMUX_UPDATE_PAN) {
  166. snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_A, vp->ch, vp->apan);
  167. snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_B, vp->ch, vp->aaux);
  168. }
  169. if (update & SNDRV_EMUX_UPDATE_FMMOD)
  170. set_fmmod(hw, vp);
  171. if (update & SNDRV_EMUX_UPDATE_TREMFREQ)
  172. snd_emu10k1_ptr_write(hw, TREMFRQ, vp->ch, vp->reg.parm.tremfrq);
  173. if (update & SNDRV_EMUX_UPDATE_FM2FRQ2)
  174. set_fm2frq2(hw, vp);
  175. if (update & SNDRV_EMUX_UPDATE_Q)
  176. set_filterQ(hw, vp);
  177. }
  178. /*
  179. * look up voice table - get the best voice in order of preference
  180. */
  181. /* spinlock held! */
  182. static void
  183. lookup_voices(struct snd_emux *emu, struct snd_emu10k1 *hw,
  184. struct best_voice *best, int active_only)
  185. {
  186. struct snd_emux_voice *vp;
  187. struct best_voice *bp;
  188. int i;
  189. for (i = 0; i < V_END; i++) {
  190. best[i].time = (unsigned int)-1; /* XXX MAX_?INT really */
  191. best[i].voice = -1;
  192. }
  193. /*
  194. * Go through them all and get a best one to use.
  195. * NOTE: could also look at volume and pick the quietest one.
  196. */
  197. for (i = 0; i < emu->max_voices; i++) {
  198. int state, val;
  199. vp = &emu->voices[i];
  200. state = vp->state;
  201. if (state == SNDRV_EMUX_ST_OFF) {
  202. if (vp->ch < 0) {
  203. if (active_only)
  204. continue;
  205. bp = best + V_FREE;
  206. } else
  207. bp = best + V_OFF;
  208. }
  209. else if (state == SNDRV_EMUX_ST_RELEASED ||
  210. state == SNDRV_EMUX_ST_PENDING) {
  211. bp = best + V_RELEASED;
  212. #if 1
  213. val = snd_emu10k1_ptr_read(hw, CVCF_CURRENTVOL, vp->ch);
  214. if (! val)
  215. bp = best + V_OFF;
  216. #endif
  217. }
  218. else if (state == SNDRV_EMUX_ST_STANDBY)
  219. continue;
  220. else if (state & SNDRV_EMUX_ST_ON)
  221. bp = best + V_PLAYING;
  222. else
  223. continue;
  224. /* check if sample is finished playing (non-looping only) */
  225. if (bp != best + V_OFF && bp != best + V_FREE &&
  226. (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_SINGLESHOT)) {
  227. val = snd_emu10k1_ptr_read(hw, CCCA_CURRADDR, vp->ch);
  228. if (val >= vp->reg.loopstart)
  229. bp = best + V_OFF;
  230. }
  231. if (vp->time < bp->time) {
  232. bp->time = vp->time;
  233. bp->voice = i;
  234. }
  235. }
  236. }
  237. /*
  238. * get an empty voice
  239. *
  240. * emu->voice_lock is already held.
  241. */
  242. static struct snd_emux_voice *
  243. get_voice(struct snd_emux *emu, struct snd_emux_port *port)
  244. {
  245. struct snd_emu10k1 *hw;
  246. struct snd_emux_voice *vp;
  247. struct best_voice best[V_END];
  248. int i;
  249. hw = emu->hw;
  250. lookup_voices(emu, hw, best, 0);
  251. for (i = 0; i < V_END; i++) {
  252. if (best[i].voice >= 0) {
  253. vp = &emu->voices[best[i].voice];
  254. if (vp->ch < 0) {
  255. /* allocate a voice */
  256. struct snd_emu10k1_voice *hwvoice;
  257. if (snd_emu10k1_voice_alloc(hw, EMU10K1_SYNTH, 1, &hwvoice) < 0 || hwvoice == NULL)
  258. continue;
  259. vp->ch = hwvoice->number;
  260. emu->num_voices++;
  261. }
  262. return vp;
  263. }
  264. }
  265. /* not found */
  266. return NULL;
  267. }
  268. /*
  269. * prepare envelopes and LFOs
  270. */
  271. static int
  272. start_voice(struct snd_emux_voice *vp)
  273. {
  274. unsigned int temp;
  275. int ch;
  276. unsigned int addr, mapped_offset;
  277. struct snd_midi_channel *chan;
  278. struct snd_emu10k1 *hw;
  279. struct snd_emu10k1_memblk *emem;
  280. hw = vp->hw;
  281. ch = vp->ch;
  282. if (snd_BUG_ON(ch < 0))
  283. return -EINVAL;
  284. chan = vp->chan;
  285. emem = (struct snd_emu10k1_memblk *)vp->block;
  286. if (emem == NULL)
  287. return -EINVAL;
  288. emem->map_locked++;
  289. if (snd_emu10k1_memblk_map(hw, emem) < 0) {
  290. /* dev_err(hw->card->devK, "emu: cannot map!\n"); */
  291. return -ENOMEM;
  292. }
  293. mapped_offset = snd_emu10k1_memblk_offset(emem) >> 1;
  294. vp->reg.start += mapped_offset;
  295. vp->reg.end += mapped_offset;
  296. vp->reg.loopstart += mapped_offset;
  297. vp->reg.loopend += mapped_offset;
  298. /* set channel routing */
  299. /* A = left(0), B = right(1), C = reverb(c), D = chorus(d) */
  300. if (hw->audigy) {
  301. temp = FXBUS_MIDI_LEFT | (FXBUS_MIDI_RIGHT << 8) |
  302. (FXBUS_MIDI_REVERB << 16) | (FXBUS_MIDI_CHORUS << 24);
  303. snd_emu10k1_ptr_write(hw, A_FXRT1, ch, temp);
  304. } else {
  305. temp = (FXBUS_MIDI_LEFT << 16) | (FXBUS_MIDI_RIGHT << 20) |
  306. (FXBUS_MIDI_REVERB << 24) | (FXBUS_MIDI_CHORUS << 28);
  307. snd_emu10k1_ptr_write(hw, FXRT, ch, temp);
  308. }
  309. /* channel to be silent and idle */
  310. snd_emu10k1_ptr_write(hw, DCYSUSV, ch, 0x0000);
  311. snd_emu10k1_ptr_write(hw, VTFT, ch, 0x0000FFFF);
  312. snd_emu10k1_ptr_write(hw, CVCF, ch, 0x0000FFFF);
  313. snd_emu10k1_ptr_write(hw, PTRX, ch, 0);
  314. snd_emu10k1_ptr_write(hw, CPF, ch, 0);
  315. /* set pitch offset */
  316. snd_emu10k1_ptr_write(hw, IP, vp->ch, vp->apitch);
  317. /* set envelope parameters */
  318. snd_emu10k1_ptr_write(hw, ENVVAL, ch, vp->reg.parm.moddelay);
  319. snd_emu10k1_ptr_write(hw, ATKHLDM, ch, vp->reg.parm.modatkhld);
  320. snd_emu10k1_ptr_write(hw, DCYSUSM, ch, vp->reg.parm.moddcysus);
  321. snd_emu10k1_ptr_write(hw, ENVVOL, ch, vp->reg.parm.voldelay);
  322. snd_emu10k1_ptr_write(hw, ATKHLDV, ch, vp->reg.parm.volatkhld);
  323. /* decay/sustain parameter for volume envelope is used
  324. for triggerg the voice */
  325. /* cutoff and volume */
  326. temp = (unsigned int)vp->acutoff << 8 | (unsigned char)vp->avol;
  327. snd_emu10k1_ptr_write(hw, IFATN, vp->ch, temp);
  328. /* modulation envelope heights */
  329. snd_emu10k1_ptr_write(hw, PEFE, ch, vp->reg.parm.pefe);
  330. /* lfo1/2 delay */
  331. snd_emu10k1_ptr_write(hw, LFOVAL1, ch, vp->reg.parm.lfo1delay);
  332. snd_emu10k1_ptr_write(hw, LFOVAL2, ch, vp->reg.parm.lfo2delay);
  333. /* lfo1 pitch & cutoff shift */
  334. set_fmmod(hw, vp);
  335. /* lfo1 volume & freq */
  336. snd_emu10k1_ptr_write(hw, TREMFRQ, vp->ch, vp->reg.parm.tremfrq);
  337. /* lfo2 pitch & freq */
  338. set_fm2frq2(hw, vp);
  339. /* reverb and loop start (reverb 8bit, MSB) */
  340. temp = vp->reg.parm.reverb;
  341. temp += (int)vp->chan->control[MIDI_CTL_E1_REVERB_DEPTH] * 9 / 10;
  342. LIMITMAX(temp, 255);
  343. addr = vp->reg.loopstart;
  344. snd_emu10k1_ptr_write(hw, PSST, vp->ch, (temp << 24) | addr);
  345. /* chorus & loop end (chorus 8bit, MSB) */
  346. addr = vp->reg.loopend;
  347. temp = vp->reg.parm.chorus;
  348. temp += (int)chan->control[MIDI_CTL_E3_CHORUS_DEPTH] * 9 / 10;
  349. LIMITMAX(temp, 255);
  350. temp = (temp <<24) | addr;
  351. snd_emu10k1_ptr_write(hw, DSL, ch, temp);
  352. /* clear filter delay memory */
  353. snd_emu10k1_ptr_write(hw, Z1, ch, 0);
  354. snd_emu10k1_ptr_write(hw, Z2, ch, 0);
  355. /* invalidate maps */
  356. temp = (hw->silent_page.addr << hw->address_mode) | (hw->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
  357. snd_emu10k1_ptr_write(hw, MAPA, ch, temp);
  358. snd_emu10k1_ptr_write(hw, MAPB, ch, temp);
  359. #if 0
  360. /* cache */
  361. {
  362. unsigned int val, sample;
  363. val = 32;
  364. if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
  365. sample = 0x80808080;
  366. else {
  367. sample = 0;
  368. val *= 2;
  369. }
  370. /* cache */
  371. snd_emu10k1_ptr_write(hw, CCR, ch, 0x1c << 16);
  372. snd_emu10k1_ptr_write(hw, CDE, ch, sample);
  373. snd_emu10k1_ptr_write(hw, CDF, ch, sample);
  374. /* invalidate maps */
  375. temp = ((unsigned int)hw->silent_page.addr << hw_address_mode) | (hw->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
  376. snd_emu10k1_ptr_write(hw, MAPA, ch, temp);
  377. snd_emu10k1_ptr_write(hw, MAPB, ch, temp);
  378. /* fill cache */
  379. val -= 4;
  380. val <<= 25;
  381. val |= 0x1c << 16;
  382. snd_emu10k1_ptr_write(hw, CCR, ch, val);
  383. }
  384. #endif
  385. /* Q & current address (Q 4bit value, MSB) */
  386. addr = vp->reg.start;
  387. temp = vp->reg.parm.filterQ;
  388. temp = (temp<<28) | addr;
  389. if (vp->apitch < 0xe400)
  390. temp |= CCCA_INTERPROM_0;
  391. else {
  392. unsigned int shift = (vp->apitch - 0xe000) >> 10;
  393. temp |= shift << 25;
  394. }
  395. if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
  396. temp |= CCCA_8BITSELECT;
  397. snd_emu10k1_ptr_write(hw, CCCA, ch, temp);
  398. /* reset volume */
  399. temp = (unsigned int)vp->vtarget << 16;
  400. snd_emu10k1_ptr_write(hw, VTFT, ch, temp | vp->ftarget);
  401. snd_emu10k1_ptr_write(hw, CVCF, ch, temp | 0xff00);
  402. return 0;
  403. }
  404. /*
  405. * Start envelope
  406. */
  407. static void
  408. trigger_voice(struct snd_emux_voice *vp)
  409. {
  410. unsigned int temp, ptarget;
  411. struct snd_emu10k1 *hw;
  412. struct snd_emu10k1_memblk *emem;
  413. hw = vp->hw;
  414. emem = (struct snd_emu10k1_memblk *)vp->block;
  415. if (! emem || emem->mapped_page < 0)
  416. return; /* not mapped */
  417. #if 0
  418. ptarget = (unsigned int)vp->ptarget << 16;
  419. #else
  420. ptarget = IP_TO_CP(vp->apitch);
  421. #endif
  422. /* set pitch target and pan (volume) */
  423. temp = ptarget | (vp->apan << 8) | vp->aaux;
  424. snd_emu10k1_ptr_write(hw, PTRX, vp->ch, temp);
  425. /* pitch target */
  426. snd_emu10k1_ptr_write(hw, CPF, vp->ch, ptarget);
  427. /* trigger voice */
  428. snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, vp->reg.parm.voldcysus|DCYSUSV_CHANNELENABLE_MASK);
  429. }
  430. #define MOD_SENSE 18
  431. /* set lfo1 modulation height and cutoff */
  432. static void
  433. set_fmmod(struct snd_emu10k1 *hw, struct snd_emux_voice *vp)
  434. {
  435. unsigned short fmmod;
  436. short pitch;
  437. unsigned char cutoff;
  438. int modulation;
  439. pitch = (char)(vp->reg.parm.fmmod>>8);
  440. cutoff = (vp->reg.parm.fmmod & 0xff);
  441. modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
  442. pitch += (MOD_SENSE * modulation) / 1200;
  443. LIMITVALUE(pitch, -128, 127);
  444. fmmod = ((unsigned char)pitch<<8) | cutoff;
  445. snd_emu10k1_ptr_write(hw, FMMOD, vp->ch, fmmod);
  446. }
  447. /* set lfo2 pitch & frequency */
  448. static void
  449. set_fm2frq2(struct snd_emu10k1 *hw, struct snd_emux_voice *vp)
  450. {
  451. unsigned short fm2frq2;
  452. short pitch;
  453. unsigned char freq;
  454. int modulation;
  455. pitch = (char)(vp->reg.parm.fm2frq2>>8);
  456. freq = vp->reg.parm.fm2frq2 & 0xff;
  457. modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
  458. pitch += (MOD_SENSE * modulation) / 1200;
  459. LIMITVALUE(pitch, -128, 127);
  460. fm2frq2 = ((unsigned char)pitch<<8) | freq;
  461. snd_emu10k1_ptr_write(hw, FM2FRQ2, vp->ch, fm2frq2);
  462. }
  463. /* set filterQ */
  464. static void
  465. set_filterQ(struct snd_emu10k1 *hw, struct snd_emux_voice *vp)
  466. {
  467. unsigned int val;
  468. val = snd_emu10k1_ptr_read(hw, CCCA, vp->ch) & ~CCCA_RESONANCE;
  469. val |= (vp->reg.parm.filterQ << 28);
  470. snd_emu10k1_ptr_write(hw, CCCA, vp->ch, val);
  471. }