emu10k1_callback.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. int
  80. snd_emu10k1_synth_get_voice(struct snd_emu10k1 *hw)
  81. {
  82. struct snd_emux *emu;
  83. struct snd_emux_voice *vp;
  84. struct best_voice best[V_END];
  85. unsigned long flags;
  86. int i;
  87. emu = hw->synth;
  88. spin_lock_irqsave(&emu->voice_lock, flags);
  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. printk(KERN_WARNING
  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. spin_unlock_irqrestore(&emu->voice_lock, flags);
  105. return ch;
  106. }
  107. }
  108. spin_unlock_irqrestore(&emu->voice_lock, flags);
  109. /* not found */
  110. return -ENOMEM;
  111. }
  112. /*
  113. * turn off the voice (not terminated)
  114. */
  115. static void
  116. release_voice(struct snd_emux_voice *vp)
  117. {
  118. int dcysusv;
  119. struct snd_emu10k1 *hw;
  120. hw = vp->hw;
  121. dcysusv = 0x8000 | (unsigned char)vp->reg.parm.modrelease;
  122. snd_emu10k1_ptr_write(hw, DCYSUSM, vp->ch, dcysusv);
  123. dcysusv = 0x8000 | (unsigned char)vp->reg.parm.volrelease | DCYSUSV_CHANNELENABLE_MASK;
  124. snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, dcysusv);
  125. }
  126. /*
  127. * terminate the voice
  128. */
  129. static void
  130. terminate_voice(struct snd_emux_voice *vp)
  131. {
  132. struct snd_emu10k1 *hw;
  133. if (snd_BUG_ON(!vp))
  134. return;
  135. hw = vp->hw;
  136. snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0x807f | DCYSUSV_CHANNELENABLE_MASK);
  137. if (vp->block) {
  138. struct snd_emu10k1_memblk *emem;
  139. emem = (struct snd_emu10k1_memblk *)vp->block;
  140. if (emem->map_locked > 0)
  141. emem->map_locked--;
  142. }
  143. }
  144. /*
  145. * release the voice to system
  146. */
  147. static void
  148. free_voice(struct snd_emux_voice *vp)
  149. {
  150. struct snd_emu10k1 *hw;
  151. hw = vp->hw;
  152. /* FIXME: emu10k1_synth is broken. */
  153. /* This can get called with hw == 0 */
  154. /* Problem apparent on plug, unplug then plug */
  155. /* on the Audigy 2 ZS Notebook. */
  156. if (hw && (vp->ch >= 0)) {
  157. snd_emu10k1_ptr_write(hw, IFATN, vp->ch, 0xff00);
  158. snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0x807f | DCYSUSV_CHANNELENABLE_MASK);
  159. // snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, 0);
  160. snd_emu10k1_ptr_write(hw, VTFT, vp->ch, 0xffff);
  161. snd_emu10k1_ptr_write(hw, CVCF, vp->ch, 0xffff);
  162. snd_emu10k1_voice_free(hw, &hw->voices[vp->ch]);
  163. vp->emu->num_voices--;
  164. vp->ch = -1;
  165. }
  166. }
  167. /*
  168. * update registers
  169. */
  170. static void
  171. update_voice(struct snd_emux_voice *vp, int update)
  172. {
  173. struct snd_emu10k1 *hw;
  174. hw = vp->hw;
  175. if (update & SNDRV_EMUX_UPDATE_VOLUME)
  176. snd_emu10k1_ptr_write(hw, IFATN_ATTENUATION, vp->ch, vp->avol);
  177. if (update & SNDRV_EMUX_UPDATE_PITCH)
  178. snd_emu10k1_ptr_write(hw, IP, vp->ch, vp->apitch);
  179. if (update & SNDRV_EMUX_UPDATE_PAN) {
  180. snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_A, vp->ch, vp->apan);
  181. snd_emu10k1_ptr_write(hw, PTRX_FXSENDAMOUNT_B, vp->ch, vp->aaux);
  182. }
  183. if (update & SNDRV_EMUX_UPDATE_FMMOD)
  184. set_fmmod(hw, vp);
  185. if (update & SNDRV_EMUX_UPDATE_TREMFREQ)
  186. snd_emu10k1_ptr_write(hw, TREMFRQ, vp->ch, vp->reg.parm.tremfrq);
  187. if (update & SNDRV_EMUX_UPDATE_FM2FRQ2)
  188. set_fm2frq2(hw, vp);
  189. if (update & SNDRV_EMUX_UPDATE_Q)
  190. set_filterQ(hw, vp);
  191. }
  192. /*
  193. * look up voice table - get the best voice in order of preference
  194. */
  195. /* spinlock held! */
  196. static void
  197. lookup_voices(struct snd_emux *emu, struct snd_emu10k1 *hw,
  198. struct best_voice *best, int active_only)
  199. {
  200. struct snd_emux_voice *vp;
  201. struct best_voice *bp;
  202. int i;
  203. for (i = 0; i < V_END; i++) {
  204. best[i].time = (unsigned int)-1; /* XXX MAX_?INT really */;
  205. best[i].voice = -1;
  206. }
  207. /*
  208. * Go through them all and get a best one to use.
  209. * NOTE: could also look at volume and pick the quietest one.
  210. */
  211. for (i = 0; i < emu->max_voices; i++) {
  212. int state, val;
  213. vp = &emu->voices[i];
  214. state = vp->state;
  215. if (state == SNDRV_EMUX_ST_OFF) {
  216. if (vp->ch < 0) {
  217. if (active_only)
  218. continue;
  219. bp = best + V_FREE;
  220. } else
  221. bp = best + V_OFF;
  222. }
  223. else if (state == SNDRV_EMUX_ST_RELEASED ||
  224. state == SNDRV_EMUX_ST_PENDING) {
  225. bp = best + V_RELEASED;
  226. #if 1
  227. val = snd_emu10k1_ptr_read(hw, CVCF_CURRENTVOL, vp->ch);
  228. if (! val)
  229. bp = best + V_OFF;
  230. #endif
  231. }
  232. else if (state == SNDRV_EMUX_ST_STANDBY)
  233. continue;
  234. else if (state & SNDRV_EMUX_ST_ON)
  235. bp = best + V_PLAYING;
  236. else
  237. continue;
  238. /* check if sample is finished playing (non-looping only) */
  239. if (bp != best + V_OFF && bp != best + V_FREE &&
  240. (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_SINGLESHOT)) {
  241. val = snd_emu10k1_ptr_read(hw, CCCA_CURRADDR, vp->ch);
  242. if (val >= vp->reg.loopstart)
  243. bp = best + V_OFF;
  244. }
  245. if (vp->time < bp->time) {
  246. bp->time = vp->time;
  247. bp->voice = i;
  248. }
  249. }
  250. }
  251. /*
  252. * get an empty voice
  253. *
  254. * emu->voice_lock is already held.
  255. */
  256. static struct snd_emux_voice *
  257. get_voice(struct snd_emux *emu, struct snd_emux_port *port)
  258. {
  259. struct snd_emu10k1 *hw;
  260. struct snd_emux_voice *vp;
  261. struct best_voice best[V_END];
  262. int i;
  263. hw = emu->hw;
  264. lookup_voices(emu, hw, best, 0);
  265. for (i = 0; i < V_END; i++) {
  266. if (best[i].voice >= 0) {
  267. vp = &emu->voices[best[i].voice];
  268. if (vp->ch < 0) {
  269. /* allocate a voice */
  270. struct snd_emu10k1_voice *hwvoice;
  271. if (snd_emu10k1_voice_alloc(hw, EMU10K1_SYNTH, 1, &hwvoice) < 0 || hwvoice == NULL)
  272. continue;
  273. vp->ch = hwvoice->number;
  274. emu->num_voices++;
  275. }
  276. return vp;
  277. }
  278. }
  279. /* not found */
  280. return NULL;
  281. }
  282. /*
  283. * prepare envelopes and LFOs
  284. */
  285. static int
  286. start_voice(struct snd_emux_voice *vp)
  287. {
  288. unsigned int temp;
  289. int ch;
  290. unsigned int addr, mapped_offset;
  291. struct snd_midi_channel *chan;
  292. struct snd_emu10k1 *hw;
  293. struct snd_emu10k1_memblk *emem;
  294. hw = vp->hw;
  295. ch = vp->ch;
  296. if (snd_BUG_ON(ch < 0))
  297. return -EINVAL;
  298. chan = vp->chan;
  299. emem = (struct snd_emu10k1_memblk *)vp->block;
  300. if (emem == NULL)
  301. return -EINVAL;
  302. emem->map_locked++;
  303. if (snd_emu10k1_memblk_map(hw, emem) < 0) {
  304. /* printk(KERN_ERR "emu: cannot map!\n"); */
  305. return -ENOMEM;
  306. }
  307. mapped_offset = snd_emu10k1_memblk_offset(emem) >> 1;
  308. vp->reg.start += mapped_offset;
  309. vp->reg.end += mapped_offset;
  310. vp->reg.loopstart += mapped_offset;
  311. vp->reg.loopend += mapped_offset;
  312. /* set channel routing */
  313. /* A = left(0), B = right(1), C = reverb(c), D = chorus(d) */
  314. if (hw->audigy) {
  315. temp = FXBUS_MIDI_LEFT | (FXBUS_MIDI_RIGHT << 8) |
  316. (FXBUS_MIDI_REVERB << 16) | (FXBUS_MIDI_CHORUS << 24);
  317. snd_emu10k1_ptr_write(hw, A_FXRT1, ch, temp);
  318. } else {
  319. temp = (FXBUS_MIDI_LEFT << 16) | (FXBUS_MIDI_RIGHT << 20) |
  320. (FXBUS_MIDI_REVERB << 24) | (FXBUS_MIDI_CHORUS << 28);
  321. snd_emu10k1_ptr_write(hw, FXRT, ch, temp);
  322. }
  323. /* channel to be silent and idle */
  324. snd_emu10k1_ptr_write(hw, DCYSUSV, ch, 0x0000);
  325. snd_emu10k1_ptr_write(hw, VTFT, ch, 0x0000FFFF);
  326. snd_emu10k1_ptr_write(hw, CVCF, ch, 0x0000FFFF);
  327. snd_emu10k1_ptr_write(hw, PTRX, ch, 0);
  328. snd_emu10k1_ptr_write(hw, CPF, ch, 0);
  329. /* set pitch offset */
  330. snd_emu10k1_ptr_write(hw, IP, vp->ch, vp->apitch);
  331. /* set envelope parameters */
  332. snd_emu10k1_ptr_write(hw, ENVVAL, ch, vp->reg.parm.moddelay);
  333. snd_emu10k1_ptr_write(hw, ATKHLDM, ch, vp->reg.parm.modatkhld);
  334. snd_emu10k1_ptr_write(hw, DCYSUSM, ch, vp->reg.parm.moddcysus);
  335. snd_emu10k1_ptr_write(hw, ENVVOL, ch, vp->reg.parm.voldelay);
  336. snd_emu10k1_ptr_write(hw, ATKHLDV, ch, vp->reg.parm.volatkhld);
  337. /* decay/sustain parameter for volume envelope is used
  338. for triggerg the voice */
  339. /* cutoff and volume */
  340. temp = (unsigned int)vp->acutoff << 8 | (unsigned char)vp->avol;
  341. snd_emu10k1_ptr_write(hw, IFATN, vp->ch, temp);
  342. /* modulation envelope heights */
  343. snd_emu10k1_ptr_write(hw, PEFE, ch, vp->reg.parm.pefe);
  344. /* lfo1/2 delay */
  345. snd_emu10k1_ptr_write(hw, LFOVAL1, ch, vp->reg.parm.lfo1delay);
  346. snd_emu10k1_ptr_write(hw, LFOVAL2, ch, vp->reg.parm.lfo2delay);
  347. /* lfo1 pitch & cutoff shift */
  348. set_fmmod(hw, vp);
  349. /* lfo1 volume & freq */
  350. snd_emu10k1_ptr_write(hw, TREMFRQ, vp->ch, vp->reg.parm.tremfrq);
  351. /* lfo2 pitch & freq */
  352. set_fm2frq2(hw, vp);
  353. /* reverb and loop start (reverb 8bit, MSB) */
  354. temp = vp->reg.parm.reverb;
  355. temp += (int)vp->chan->control[MIDI_CTL_E1_REVERB_DEPTH] * 9 / 10;
  356. LIMITMAX(temp, 255);
  357. addr = vp->reg.loopstart;
  358. snd_emu10k1_ptr_write(hw, PSST, vp->ch, (temp << 24) | addr);
  359. /* chorus & loop end (chorus 8bit, MSB) */
  360. addr = vp->reg.loopend;
  361. temp = vp->reg.parm.chorus;
  362. temp += (int)chan->control[MIDI_CTL_E3_CHORUS_DEPTH] * 9 / 10;
  363. LIMITMAX(temp, 255);
  364. temp = (temp <<24) | addr;
  365. snd_emu10k1_ptr_write(hw, DSL, ch, temp);
  366. /* clear filter delay memory */
  367. snd_emu10k1_ptr_write(hw, Z1, ch, 0);
  368. snd_emu10k1_ptr_write(hw, Z2, ch, 0);
  369. /* invalidate maps */
  370. temp = (hw->silent_page.addr << 1) | MAP_PTI_MASK;
  371. snd_emu10k1_ptr_write(hw, MAPA, ch, temp);
  372. snd_emu10k1_ptr_write(hw, MAPB, ch, temp);
  373. #if 0
  374. /* cache */
  375. {
  376. unsigned int val, sample;
  377. val = 32;
  378. if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
  379. sample = 0x80808080;
  380. else {
  381. sample = 0;
  382. val *= 2;
  383. }
  384. /* cache */
  385. snd_emu10k1_ptr_write(hw, CCR, ch, 0x1c << 16);
  386. snd_emu10k1_ptr_write(hw, CDE, ch, sample);
  387. snd_emu10k1_ptr_write(hw, CDF, ch, sample);
  388. /* invalidate maps */
  389. temp = ((unsigned int)hw->silent_page.addr << 1) | MAP_PTI_MASK;
  390. snd_emu10k1_ptr_write(hw, MAPA, ch, temp);
  391. snd_emu10k1_ptr_write(hw, MAPB, ch, temp);
  392. /* fill cache */
  393. val -= 4;
  394. val <<= 25;
  395. val |= 0x1c << 16;
  396. snd_emu10k1_ptr_write(hw, CCR, ch, val);
  397. }
  398. #endif
  399. /* Q & current address (Q 4bit value, MSB) */
  400. addr = vp->reg.start;
  401. temp = vp->reg.parm.filterQ;
  402. temp = (temp<<28) | addr;
  403. if (vp->apitch < 0xe400)
  404. temp |= CCCA_INTERPROM_0;
  405. else {
  406. unsigned int shift = (vp->apitch - 0xe000) >> 10;
  407. temp |= shift << 25;
  408. }
  409. if (vp->reg.sample_mode & SNDRV_SFNT_SAMPLE_8BITS)
  410. temp |= CCCA_8BITSELECT;
  411. snd_emu10k1_ptr_write(hw, CCCA, ch, temp);
  412. /* reset volume */
  413. temp = (unsigned int)vp->vtarget << 16;
  414. snd_emu10k1_ptr_write(hw, VTFT, ch, temp | vp->ftarget);
  415. snd_emu10k1_ptr_write(hw, CVCF, ch, temp | 0xff00);
  416. return 0;
  417. }
  418. /*
  419. * Start envelope
  420. */
  421. static void
  422. trigger_voice(struct snd_emux_voice *vp)
  423. {
  424. unsigned int temp, ptarget;
  425. struct snd_emu10k1 *hw;
  426. struct snd_emu10k1_memblk *emem;
  427. hw = vp->hw;
  428. emem = (struct snd_emu10k1_memblk *)vp->block;
  429. if (! emem || emem->mapped_page < 0)
  430. return; /* not mapped */
  431. #if 0
  432. ptarget = (unsigned int)vp->ptarget << 16;
  433. #else
  434. ptarget = IP_TO_CP(vp->apitch);
  435. #endif
  436. /* set pitch target and pan (volume) */
  437. temp = ptarget | (vp->apan << 8) | vp->aaux;
  438. snd_emu10k1_ptr_write(hw, PTRX, vp->ch, temp);
  439. /* pitch target */
  440. snd_emu10k1_ptr_write(hw, CPF, vp->ch, ptarget);
  441. /* trigger voice */
  442. snd_emu10k1_ptr_write(hw, DCYSUSV, vp->ch, vp->reg.parm.voldcysus|DCYSUSV_CHANNELENABLE_MASK);
  443. }
  444. #define MOD_SENSE 18
  445. /* set lfo1 modulation height and cutoff */
  446. static void
  447. set_fmmod(struct snd_emu10k1 *hw, struct snd_emux_voice *vp)
  448. {
  449. unsigned short fmmod;
  450. short pitch;
  451. unsigned char cutoff;
  452. int modulation;
  453. pitch = (char)(vp->reg.parm.fmmod>>8);
  454. cutoff = (vp->reg.parm.fmmod & 0xff);
  455. modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
  456. pitch += (MOD_SENSE * modulation) / 1200;
  457. LIMITVALUE(pitch, -128, 127);
  458. fmmod = ((unsigned char)pitch<<8) | cutoff;
  459. snd_emu10k1_ptr_write(hw, FMMOD, vp->ch, fmmod);
  460. }
  461. /* set lfo2 pitch & frequency */
  462. static void
  463. set_fm2frq2(struct snd_emu10k1 *hw, struct snd_emux_voice *vp)
  464. {
  465. unsigned short fm2frq2;
  466. short pitch;
  467. unsigned char freq;
  468. int modulation;
  469. pitch = (char)(vp->reg.parm.fm2frq2>>8);
  470. freq = vp->reg.parm.fm2frq2 & 0xff;
  471. modulation = vp->chan->gm_modulation + vp->chan->midi_pressure;
  472. pitch += (MOD_SENSE * modulation) / 1200;
  473. LIMITVALUE(pitch, -128, 127);
  474. fm2frq2 = ((unsigned char)pitch<<8) | freq;
  475. snd_emu10k1_ptr_write(hw, FM2FRQ2, vp->ch, fm2frq2);
  476. }
  477. /* set filterQ */
  478. static void
  479. set_filterQ(struct snd_emu10k1 *hw, struct snd_emux_voice *vp)
  480. {
  481. unsigned int val;
  482. val = snd_emu10k1_ptr_read(hw, CCCA, vp->ch) & ~CCCA_RESONANCE;
  483. val |= (vp->reg.parm.filterQ << 28);
  484. snd_emu10k1_ptr_write(hw, CCCA, vp->ch, val);
  485. }