emu8000_pcm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * pcm emulation on emu8000 wavetable
  3. *
  4. * Copyright (C) 2002 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 "emu8000_local.h"
  21. #include <linux/init.h>
  22. #include <sound/initval.h>
  23. #include <sound/pcm.h>
  24. /*
  25. * define the following if you want to use this pcm with non-interleaved mode
  26. */
  27. /* #define USE_NONINTERLEAVE */
  28. /* NOTE: for using the non-interleaved mode with alsa-lib, you have to set
  29. * mmap_emulation flag to 1 in your .asoundrc, such like
  30. *
  31. * pcm.emu8k {
  32. * type plug
  33. * slave.pcm {
  34. * type hw
  35. * card 0
  36. * device 1
  37. * mmap_emulation 1
  38. * }
  39. * }
  40. *
  41. * besides, for the time being, the non-interleaved mode doesn't work well on
  42. * alsa-lib...
  43. */
  44. typedef struct snd_emu8k_pcm emu8k_pcm_t;
  45. struct snd_emu8k_pcm {
  46. emu8000_t *emu;
  47. snd_pcm_substream_t *substream;
  48. unsigned int allocated_bytes;
  49. snd_util_memblk_t *block;
  50. unsigned int offset;
  51. unsigned int buf_size;
  52. unsigned int period_size;
  53. unsigned int loop_start[2];
  54. unsigned int pitch;
  55. int panning[2];
  56. int last_ptr;
  57. int period_pos;
  58. int voices;
  59. unsigned int dram_opened: 1;
  60. unsigned int running: 1;
  61. unsigned int timer_running: 1;
  62. struct timer_list timer;
  63. spinlock_t timer_lock;
  64. };
  65. #define LOOP_BLANK_SIZE 8
  66. /*
  67. * open up channels for the simultaneous data transfer and playback
  68. */
  69. static int
  70. emu8k_open_dram_for_pcm(emu8000_t *emu, int channels)
  71. {
  72. int i;
  73. /* reserve up to 2 voices for playback */
  74. snd_emux_lock_voice(emu->emu, 0);
  75. if (channels > 1)
  76. snd_emux_lock_voice(emu->emu, 1);
  77. /* reserve 28 voices for loading */
  78. for (i = channels + 1; i < EMU8000_DRAM_VOICES; i++) {
  79. unsigned int mode = EMU8000_RAM_WRITE;
  80. snd_emux_lock_voice(emu->emu, i);
  81. #ifndef USE_NONINTERLEAVE
  82. if (channels > 1 && (i & 1) != 0)
  83. mode |= EMU8000_RAM_RIGHT;
  84. #endif
  85. snd_emu8000_dma_chan(emu, i, mode);
  86. }
  87. /* assign voice 31 and 32 to ROM */
  88. EMU8000_VTFT_WRITE(emu, 30, 0);
  89. EMU8000_PSST_WRITE(emu, 30, 0x1d8);
  90. EMU8000_CSL_WRITE(emu, 30, 0x1e0);
  91. EMU8000_CCCA_WRITE(emu, 30, 0x1d8);
  92. EMU8000_VTFT_WRITE(emu, 31, 0);
  93. EMU8000_PSST_WRITE(emu, 31, 0x1d8);
  94. EMU8000_CSL_WRITE(emu, 31, 0x1e0);
  95. EMU8000_CCCA_WRITE(emu, 31, 0x1d8);
  96. return 0;
  97. }
  98. /*
  99. */
  100. static void
  101. snd_emu8000_write_wait(emu8000_t *emu, int can_schedule)
  102. {
  103. while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) {
  104. if (can_schedule) {
  105. set_current_state(TASK_INTERRUPTIBLE);
  106. schedule_timeout(1);
  107. if (signal_pending(current))
  108. break;
  109. }
  110. }
  111. }
  112. /*
  113. * close all channels
  114. */
  115. static void
  116. emu8k_close_dram(emu8000_t *emu)
  117. {
  118. int i;
  119. for (i = 0; i < 2; i++)
  120. snd_emux_unlock_voice(emu->emu, i);
  121. for (; i < EMU8000_DRAM_VOICES; i++) {
  122. snd_emu8000_dma_chan(emu, i, EMU8000_RAM_CLOSE);
  123. snd_emux_unlock_voice(emu->emu, i);
  124. }
  125. }
  126. /*
  127. * convert Hz to AWE32 rate offset (see emux/soundfont.c)
  128. */
  129. #define OFFSET_SAMPLERATE 1011119 /* base = 44100 */
  130. #define SAMPLERATE_RATIO 4096
  131. static int calc_rate_offset(int hz)
  132. {
  133. return snd_sf_linear_to_log(hz, OFFSET_SAMPLERATE, SAMPLERATE_RATIO);
  134. }
  135. /*
  136. */
  137. static snd_pcm_hardware_t emu8k_pcm_hw = {
  138. #ifdef USE_NONINTERLEAVE
  139. .info = SNDRV_PCM_INFO_NONINTERLEAVED,
  140. #else
  141. .info = SNDRV_PCM_INFO_INTERLEAVED,
  142. #endif
  143. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  144. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  145. .rate_min = 4000,
  146. .rate_max = 48000,
  147. .channels_min = 1,
  148. .channels_max = 2,
  149. .buffer_bytes_max = (128*1024),
  150. .period_bytes_min = 1024,
  151. .period_bytes_max = (128*1024),
  152. .periods_min = 2,
  153. .periods_max = 1024,
  154. .fifo_size = 0,
  155. };
  156. /*
  157. * get the current position at the given channel from CCCA register
  158. */
  159. static inline int emu8k_get_curpos(emu8k_pcm_t *rec, int ch)
  160. {
  161. int val = EMU8000_CCCA_READ(rec->emu, ch) & 0xfffffff;
  162. val -= rec->loop_start[ch] - 1;
  163. return val;
  164. }
  165. /*
  166. * timer interrupt handler
  167. * check the current position and update the period if necessary.
  168. */
  169. static void emu8k_pcm_timer_func(unsigned long data)
  170. {
  171. emu8k_pcm_t *rec = (emu8k_pcm_t *)data;
  172. int ptr, delta;
  173. spin_lock(&rec->timer_lock);
  174. /* update the current pointer */
  175. ptr = emu8k_get_curpos(rec, 0);
  176. if (ptr < rec->last_ptr)
  177. delta = ptr + rec->buf_size - rec->last_ptr;
  178. else
  179. delta = ptr - rec->last_ptr;
  180. rec->period_pos += delta;
  181. rec->last_ptr = ptr;
  182. /* reprogram timer */
  183. rec->timer.expires = jiffies + 1;
  184. add_timer(&rec->timer);
  185. /* update period */
  186. if (rec->period_pos >= (int)rec->period_size) {
  187. rec->period_pos %= rec->period_size;
  188. spin_unlock(&rec->timer_lock);
  189. snd_pcm_period_elapsed(rec->substream);
  190. return;
  191. }
  192. spin_unlock(&rec->timer_lock);
  193. }
  194. /*
  195. * open pcm
  196. * creating an instance here
  197. */
  198. static int emu8k_pcm_open(snd_pcm_substream_t *subs)
  199. {
  200. emu8000_t *emu = snd_pcm_substream_chip(subs);
  201. emu8k_pcm_t *rec;
  202. snd_pcm_runtime_t *runtime = subs->runtime;
  203. rec = kcalloc(1, sizeof(*rec), GFP_KERNEL);
  204. if (! rec)
  205. return -ENOMEM;
  206. rec->emu = emu;
  207. rec->substream = subs;
  208. runtime->private_data = rec;
  209. spin_lock_init(&rec->timer_lock);
  210. init_timer(&rec->timer);
  211. rec->timer.function = emu8k_pcm_timer_func;
  212. rec->timer.data = (unsigned long)rec;
  213. runtime->hw = emu8k_pcm_hw;
  214. runtime->hw.buffer_bytes_max = emu->mem_size - LOOP_BLANK_SIZE * 3;
  215. runtime->hw.period_bytes_max = runtime->hw.buffer_bytes_max / 2;
  216. /* use timer to update periods.. (specified in msec) */
  217. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  218. (1000000 + HZ - 1) / HZ, UINT_MAX);
  219. return 0;
  220. }
  221. static int emu8k_pcm_close(snd_pcm_substream_t *subs)
  222. {
  223. emu8k_pcm_t *rec = subs->runtime->private_data;
  224. kfree(rec);
  225. subs->runtime->private_data = NULL;
  226. return 0;
  227. }
  228. /*
  229. * calculate pitch target
  230. */
  231. static int calc_pitch_target(int pitch)
  232. {
  233. int ptarget = 1 << (pitch >> 12);
  234. if (pitch & 0x800) ptarget += (ptarget * 0x102e) / 0x2710;
  235. if (pitch & 0x400) ptarget += (ptarget * 0x764) / 0x2710;
  236. if (pitch & 0x200) ptarget += (ptarget * 0x389) / 0x2710;
  237. ptarget += (ptarget >> 1);
  238. if (ptarget > 0xffff) ptarget = 0xffff;
  239. return ptarget;
  240. }
  241. /*
  242. * set up the voice
  243. */
  244. static void setup_voice(emu8k_pcm_t *rec, int ch)
  245. {
  246. emu8000_t *hw = rec->emu;
  247. unsigned int temp;
  248. /* channel to be silent and idle */
  249. EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080);
  250. EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF);
  251. EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF);
  252. EMU8000_PTRX_WRITE(hw, ch, 0);
  253. EMU8000_CPF_WRITE(hw, ch, 0);
  254. /* pitch offset */
  255. EMU8000_IP_WRITE(hw, ch, rec->pitch);
  256. /* set envelope parameters */
  257. EMU8000_ENVVAL_WRITE(hw, ch, 0x8000);
  258. EMU8000_ATKHLD_WRITE(hw, ch, 0x7f7f);
  259. EMU8000_DCYSUS_WRITE(hw, ch, 0x7f7f);
  260. EMU8000_ENVVOL_WRITE(hw, ch, 0x8000);
  261. EMU8000_ATKHLDV_WRITE(hw, ch, 0x7f7f);
  262. /* decay/sustain parameter for volume envelope is used
  263. for triggerg the voice */
  264. /* modulation envelope heights */
  265. EMU8000_PEFE_WRITE(hw, ch, 0x0);
  266. /* lfo1/2 delay */
  267. EMU8000_LFO1VAL_WRITE(hw, ch, 0x8000);
  268. EMU8000_LFO2VAL_WRITE(hw, ch, 0x8000);
  269. /* lfo1 pitch & cutoff shift */
  270. EMU8000_FMMOD_WRITE(hw, ch, 0);
  271. /* lfo1 volume & freq */
  272. EMU8000_TREMFRQ_WRITE(hw, ch, 0);
  273. /* lfo2 pitch & freq */
  274. EMU8000_FM2FRQ2_WRITE(hw, ch, 0);
  275. /* pan & loop start */
  276. temp = rec->panning[ch];
  277. temp = (temp <<24) | ((unsigned int)rec->loop_start[ch] - 1);
  278. EMU8000_PSST_WRITE(hw, ch, temp);
  279. /* chorus & loop end (chorus 8bit, MSB) */
  280. temp = 0; // chorus
  281. temp = (temp << 24) | ((unsigned int)rec->loop_start[ch] + rec->buf_size - 1);
  282. EMU8000_CSL_WRITE(hw, ch, temp);
  283. /* Q & current address (Q 4bit value, MSB) */
  284. temp = 0; // filterQ
  285. temp = (temp << 28) | ((unsigned int)rec->loop_start[ch] - 1);
  286. EMU8000_CCCA_WRITE(hw, ch, temp);
  287. /* clear unknown registers */
  288. EMU8000_00A0_WRITE(hw, ch, 0);
  289. EMU8000_0080_WRITE(hw, ch, 0);
  290. }
  291. /*
  292. * trigger the voice
  293. */
  294. static void start_voice(emu8k_pcm_t *rec, int ch)
  295. {
  296. unsigned long flags;
  297. emu8000_t *hw = rec->emu;
  298. unsigned int temp, aux;
  299. int pt = calc_pitch_target(rec->pitch);
  300. /* cutoff and volume */
  301. EMU8000_IFATN_WRITE(hw, ch, 0xff00);
  302. EMU8000_VTFT_WRITE(hw, ch, 0xffff);
  303. EMU8000_CVCF_WRITE(hw, ch, 0xffff);
  304. /* trigger envelope */
  305. EMU8000_DCYSUSV_WRITE(hw, ch, 0x7f7f);
  306. /* set reverb and pitch target */
  307. temp = 0; // reverb
  308. if (rec->panning[ch] == 0)
  309. aux = 0xff;
  310. else
  311. aux = (-rec->panning[ch]) & 0xff;
  312. temp = (temp << 8) | (pt << 16) | aux;
  313. EMU8000_PTRX_WRITE(hw, ch, temp);
  314. EMU8000_CPF_WRITE(hw, ch, pt << 16);
  315. /* start timer */
  316. spin_lock_irqsave(&rec->timer_lock, flags);
  317. if (! rec->timer_running) {
  318. rec->timer.expires = jiffies + 1;
  319. add_timer(&rec->timer);
  320. rec->timer_running = 1;
  321. }
  322. spin_unlock_irqrestore(&rec->timer_lock, flags);
  323. }
  324. /*
  325. * stop the voice immediately
  326. */
  327. static void stop_voice(emu8k_pcm_t *rec, int ch)
  328. {
  329. unsigned long flags;
  330. emu8000_t *hw = rec->emu;
  331. EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F);
  332. /* stop timer */
  333. spin_lock_irqsave(&rec->timer_lock, flags);
  334. if (rec->timer_running) {
  335. del_timer(&rec->timer);
  336. rec->timer_running = 0;
  337. }
  338. spin_unlock_irqrestore(&rec->timer_lock, flags);
  339. }
  340. static int emu8k_pcm_trigger(snd_pcm_substream_t *subs, int cmd)
  341. {
  342. emu8k_pcm_t *rec = subs->runtime->private_data;
  343. int ch;
  344. switch (cmd) {
  345. case SNDRV_PCM_TRIGGER_START:
  346. for (ch = 0; ch < rec->voices; ch++)
  347. start_voice(rec, ch);
  348. rec->running = 1;
  349. break;
  350. case SNDRV_PCM_TRIGGER_STOP:
  351. rec->running = 0;
  352. for (ch = 0; ch < rec->voices; ch++)
  353. stop_voice(rec, ch);
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. return 0;
  359. }
  360. /*
  361. * copy / silence ops
  362. */
  363. /*
  364. * this macro should be inserted in the copy/silence loops
  365. * to reduce the latency. without this, the system will hang up
  366. * during the whole loop.
  367. */
  368. #define CHECK_SCHEDULER() \
  369. do { \
  370. cond_resched();\
  371. if (signal_pending(current))\
  372. return -EAGAIN;\
  373. } while (0)
  374. #ifdef USE_NONINTERLEAVE
  375. /* copy one channel block */
  376. static int emu8k_transfer_block(emu8000_t *emu, int offset, unsigned short *buf, int count)
  377. {
  378. EMU8000_SMALW_WRITE(emu, offset);
  379. while (count > 0) {
  380. unsigned short sval;
  381. CHECK_SCHEDULER();
  382. get_user(sval, buf);
  383. EMU8000_SMLD_WRITE(emu, sval);
  384. buf++;
  385. count--;
  386. }
  387. return 0;
  388. }
  389. static int emu8k_pcm_copy(snd_pcm_substream_t *subs,
  390. int voice,
  391. snd_pcm_uframes_t pos,
  392. void *src,
  393. snd_pcm_uframes_t count)
  394. {
  395. emu8k_pcm_t *rec = subs->runtime->private_data;
  396. emu8000_t *emu = rec->emu;
  397. snd_emu8000_write_wait(emu, 1);
  398. if (voice == -1) {
  399. unsigned short *buf = src;
  400. int i, err;
  401. count /= rec->voices;
  402. for (i = 0; i < rec->voices; i++) {
  403. err = emu8k_transfer_block(emu, pos + rec->loop_start[i], buf, count);
  404. if (err < 0)
  405. return err;
  406. buf += count;
  407. }
  408. return 0;
  409. } else {
  410. return emu8k_transfer_block(emu, pos + rec->loop_start[voice], src, count);
  411. }
  412. }
  413. /* make a channel block silence */
  414. static int emu8k_silence_block(emu8000_t *emu, int offset, int count)
  415. {
  416. EMU8000_SMALW_WRITE(emu, offset);
  417. while (count > 0) {
  418. CHECK_SCHEDULER();
  419. EMU8000_SMLD_WRITE(emu, 0);
  420. count--;
  421. }
  422. return 0;
  423. }
  424. static int emu8k_pcm_silence(snd_pcm_substream_t *subs,
  425. int voice,
  426. snd_pcm_uframes_t pos,
  427. snd_pcm_uframes_t count)
  428. {
  429. emu8k_pcm_t *rec = subs->runtime->private_data;
  430. emu8000_t *emu = rec->emu;
  431. snd_emu8000_write_wait(emu, 1);
  432. if (voice == -1 && rec->voices == 1)
  433. voice = 0;
  434. if (voice == -1) {
  435. int err;
  436. err = emu8k_silence_block(emu, pos + rec->loop_start[0], count / 2);
  437. if (err < 0)
  438. return err;
  439. return emu8k_silence_block(emu, pos + rec->loop_start[1], count / 2);
  440. } else {
  441. return emu8k_silence_block(emu, pos + rec->loop_start[voice], count);
  442. }
  443. }
  444. #else /* interleave */
  445. /*
  446. * copy the interleaved data can be done easily by using
  447. * DMA "left" and "right" channels on emu8k engine.
  448. */
  449. static int emu8k_pcm_copy(snd_pcm_substream_t *subs,
  450. int voice,
  451. snd_pcm_uframes_t pos,
  452. void __user *src,
  453. snd_pcm_uframes_t count)
  454. {
  455. emu8k_pcm_t *rec = subs->runtime->private_data;
  456. emu8000_t *emu = rec->emu;
  457. unsigned short __user *buf = src;
  458. snd_emu8000_write_wait(emu, 1);
  459. EMU8000_SMALW_WRITE(emu, pos + rec->loop_start[0]);
  460. if (rec->voices > 1)
  461. EMU8000_SMARW_WRITE(emu, pos + rec->loop_start[1]);
  462. while (count-- > 0) {
  463. unsigned short sval;
  464. CHECK_SCHEDULER();
  465. get_user(sval, buf);
  466. EMU8000_SMLD_WRITE(emu, sval);
  467. buf++;
  468. if (rec->voices > 1) {
  469. CHECK_SCHEDULER();
  470. get_user(sval, buf);
  471. EMU8000_SMRD_WRITE(emu, sval);
  472. buf++;
  473. }
  474. }
  475. return 0;
  476. }
  477. static int emu8k_pcm_silence(snd_pcm_substream_t *subs,
  478. int voice,
  479. snd_pcm_uframes_t pos,
  480. snd_pcm_uframes_t count)
  481. {
  482. emu8k_pcm_t *rec = subs->runtime->private_data;
  483. emu8000_t *emu = rec->emu;
  484. snd_emu8000_write_wait(emu, 1);
  485. EMU8000_SMALW_WRITE(emu, rec->loop_start[0] + pos);
  486. if (rec->voices > 1)
  487. EMU8000_SMARW_WRITE(emu, rec->loop_start[1] + pos);
  488. while (count-- > 0) {
  489. CHECK_SCHEDULER();
  490. EMU8000_SMLD_WRITE(emu, 0);
  491. if (rec->voices > 1) {
  492. CHECK_SCHEDULER();
  493. EMU8000_SMRD_WRITE(emu, 0);
  494. }
  495. }
  496. return 0;
  497. }
  498. #endif
  499. /*
  500. * allocate a memory block
  501. */
  502. static int emu8k_pcm_hw_params(snd_pcm_substream_t *subs,
  503. snd_pcm_hw_params_t *hw_params)
  504. {
  505. emu8k_pcm_t *rec = subs->runtime->private_data;
  506. if (rec->block) {
  507. /* reallocation - release the old block */
  508. snd_util_mem_free(rec->emu->memhdr, rec->block);
  509. rec->block = NULL;
  510. }
  511. rec->allocated_bytes = params_buffer_bytes(hw_params) + LOOP_BLANK_SIZE * 4;
  512. rec->block = snd_util_mem_alloc(rec->emu->memhdr, rec->allocated_bytes);
  513. if (! rec->block)
  514. return -ENOMEM;
  515. rec->offset = EMU8000_DRAM_OFFSET + (rec->block->offset >> 1); /* in word */
  516. /* at least dma_bytes must be set for non-interleaved mode */
  517. subs->dma_buffer.bytes = params_buffer_bytes(hw_params);
  518. return 0;
  519. }
  520. /*
  521. * free the memory block
  522. */
  523. static int emu8k_pcm_hw_free(snd_pcm_substream_t *subs)
  524. {
  525. emu8k_pcm_t *rec = subs->runtime->private_data;
  526. if (rec->block) {
  527. int ch;
  528. for (ch = 0; ch < rec->voices; ch++)
  529. stop_voice(rec, ch); // to be sure
  530. if (rec->dram_opened)
  531. emu8k_close_dram(rec->emu);
  532. snd_util_mem_free(rec->emu->memhdr, rec->block);
  533. rec->block = NULL;
  534. }
  535. return 0;
  536. }
  537. /*
  538. */
  539. static int emu8k_pcm_prepare(snd_pcm_substream_t *subs)
  540. {
  541. emu8k_pcm_t *rec = subs->runtime->private_data;
  542. rec->pitch = 0xe000 + calc_rate_offset(subs->runtime->rate);
  543. rec->last_ptr = 0;
  544. rec->period_pos = 0;
  545. rec->buf_size = subs->runtime->buffer_size;
  546. rec->period_size = subs->runtime->period_size;
  547. rec->voices = subs->runtime->channels;
  548. rec->loop_start[0] = rec->offset + LOOP_BLANK_SIZE;
  549. if (rec->voices > 1)
  550. rec->loop_start[1] = rec->loop_start[0] + rec->buf_size + LOOP_BLANK_SIZE;
  551. if (rec->voices > 1) {
  552. rec->panning[0] = 0xff;
  553. rec->panning[1] = 0x00;
  554. } else
  555. rec->panning[0] = 0x80;
  556. if (! rec->dram_opened) {
  557. int err, i, ch;
  558. snd_emux_terminate_all(rec->emu->emu);
  559. if ((err = emu8k_open_dram_for_pcm(rec->emu, rec->voices)) != 0)
  560. return err;
  561. rec->dram_opened = 1;
  562. /* clear loop blanks */
  563. snd_emu8000_write_wait(rec->emu, 0);
  564. EMU8000_SMALW_WRITE(rec->emu, rec->offset);
  565. for (i = 0; i < LOOP_BLANK_SIZE; i++)
  566. EMU8000_SMLD_WRITE(rec->emu, 0);
  567. for (ch = 0; ch < rec->voices; ch++) {
  568. EMU8000_SMALW_WRITE(rec->emu, rec->loop_start[ch] + rec->buf_size);
  569. for (i = 0; i < LOOP_BLANK_SIZE; i++)
  570. EMU8000_SMLD_WRITE(rec->emu, 0);
  571. }
  572. }
  573. setup_voice(rec, 0);
  574. if (rec->voices > 1)
  575. setup_voice(rec, 1);
  576. return 0;
  577. }
  578. static snd_pcm_uframes_t emu8k_pcm_pointer(snd_pcm_substream_t *subs)
  579. {
  580. emu8k_pcm_t *rec = subs->runtime->private_data;
  581. if (rec->running)
  582. return emu8k_get_curpos(rec, 0);
  583. return 0;
  584. }
  585. static snd_pcm_ops_t emu8k_pcm_ops = {
  586. .open = emu8k_pcm_open,
  587. .close = emu8k_pcm_close,
  588. .ioctl = snd_pcm_lib_ioctl,
  589. .hw_params = emu8k_pcm_hw_params,
  590. .hw_free = emu8k_pcm_hw_free,
  591. .prepare = emu8k_pcm_prepare,
  592. .trigger = emu8k_pcm_trigger,
  593. .pointer = emu8k_pcm_pointer,
  594. .copy = emu8k_pcm_copy,
  595. .silence = emu8k_pcm_silence,
  596. };
  597. static void snd_emu8000_pcm_free(snd_pcm_t *pcm)
  598. {
  599. emu8000_t *emu = pcm->private_data;
  600. emu->pcm = NULL;
  601. }
  602. int snd_emu8000_pcm_new(snd_card_t *card, emu8000_t *emu, int index)
  603. {
  604. snd_pcm_t *pcm;
  605. int err;
  606. if ((err = snd_pcm_new(card, "Emu8000 PCM", index, 1, 0, &pcm)) < 0)
  607. return err;
  608. pcm->private_data = emu;
  609. pcm->private_free = snd_emu8000_pcm_free;
  610. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &emu8k_pcm_ops);
  611. emu->pcm = pcm;
  612. snd_device_register(card, pcm);
  613. return 0;
  614. }