emu10k1_callback.c 14 KB

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