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