emu10k1_callback.c 14 KB

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