emu10k1_callback.c 14 KB

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