emu10k1_callback.c 14 KB

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