2 * pcm emulation on emu8000 wavetable
4 * Copyright (C) 2002 Takashi Iwai <tiwai@suse.de>
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.
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.
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
21 #include "emu8000_local.h"
22 #include <linux/init.h>
23 #include <sound/initval.h>
24 #include <sound/pcm.h>
26 #define chip_t emu8000_t
29 * define the following if you want to use this pcm with non-interleaved mode
31 /* #define USE_NONINTERLEAVE */
33 /* NOTE: for using the non-interleaved mode with alsa-lib, you have to set
34 * mmap_emulation flag to 1 in your .asoundrc, such like
46 * besides, for the time being, the non-interleaved mode doesn't work well on
51 typedef struct snd_emu8k_pcm emu8k_pcm_t;
53 struct snd_emu8k_pcm {
55 snd_pcm_substream_t *substream;
57 unsigned int allocated_bytes;
58 snd_util_memblk_t *block;
60 unsigned int buf_size;
61 unsigned int period_size;
62 unsigned int loop_start[2];
68 unsigned int dram_opened: 1;
69 unsigned int running: 1;
70 unsigned int timer_running: 1;
71 struct timer_list timer;
72 spinlock_t timer_lock;
75 #define LOOP_BLANK_SIZE 8
79 * open up channels for the simultaneous data transfer and playback
82 emu8k_open_dram_for_pcm(emu8000_t *emu, int channels)
86 /* reserve up to 2 voices for playback */
87 snd_emux_lock_voice(emu->emu, 0);
89 snd_emux_lock_voice(emu->emu, 1);
91 /* reserve 28 voices for loading */
92 for (i = channels + 1; i < EMU8000_DRAM_VOICES; i++) {
93 unsigned int mode = EMU8000_RAM_WRITE;
94 snd_emux_lock_voice(emu->emu, i);
95 #ifndef USE_NONINTERLEAVE
96 if (channels > 1 && (i & 1) != 0)
97 mode |= EMU8000_RAM_RIGHT;
99 snd_emu8000_dma_chan(emu, i, mode);
102 /* assign voice 31 and 32 to ROM */
103 EMU8000_VTFT_WRITE(emu, 30, 0);
104 EMU8000_PSST_WRITE(emu, 30, 0x1d8);
105 EMU8000_CSL_WRITE(emu, 30, 0x1e0);
106 EMU8000_CCCA_WRITE(emu, 30, 0x1d8);
107 EMU8000_VTFT_WRITE(emu, 31, 0);
108 EMU8000_PSST_WRITE(emu, 31, 0x1d8);
109 EMU8000_CSL_WRITE(emu, 31, 0x1e0);
110 EMU8000_CCCA_WRITE(emu, 31, 0x1d8);
118 snd_emu8000_write_wait(emu8000_t *emu, int can_schedule)
120 while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) {
122 set_current_state(TASK_INTERRUPTIBLE);
124 if (signal_pending(current))
134 emu8k_close_dram(emu8000_t *emu)
138 for (i = 0; i < 2; i++)
139 snd_emux_unlock_voice(emu->emu, i);
140 for (; i < EMU8000_DRAM_VOICES; i++) {
141 snd_emu8000_dma_chan(emu, i, EMU8000_RAM_CLOSE);
142 snd_emux_unlock_voice(emu->emu, i);
147 * convert Hz to AWE32 rate offset (see emux/soundfont.c)
150 #define OFFSET_SAMPLERATE 1011119 /* base = 44100 */
151 #define SAMPLERATE_RATIO 4096
153 static int calc_rate_offset(int hz)
155 return snd_sf_linear_to_log(hz, OFFSET_SAMPLERATE, SAMPLERATE_RATIO);
162 static snd_pcm_hardware_t emu8k_pcm_hw = {
163 #ifdef USE_NONINTERLEAVE
164 .info = SNDRV_PCM_INFO_NONINTERLEAVED,
166 .info = SNDRV_PCM_INFO_INTERLEAVED,
168 .formats = SNDRV_PCM_FMTBIT_S16_LE,
169 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
174 .buffer_bytes_max = (128*1024),
175 .period_bytes_min = 1024,
176 .period_bytes_max = (128*1024),
184 * get the current position at the given channel from CCCA register
186 static inline int emu8k_get_curpos(emu8k_pcm_t *rec, int ch)
188 int val = EMU8000_CCCA_READ(rec->emu, ch) & 0xfffffff;
189 val -= rec->loop_start[ch] - 1;
195 * timer interrupt handler
196 * check the current position and update the period if necessary.
198 static void emu8k_pcm_timer_func(unsigned long data)
200 emu8k_pcm_t *rec = (emu8k_pcm_t *)data;
203 spin_lock(&rec->timer_lock);
204 /* update the current pointer */
205 ptr = emu8k_get_curpos(rec, 0);
206 if (ptr < rec->last_ptr)
207 delta = ptr + rec->buf_size - rec->last_ptr;
209 delta = ptr - rec->last_ptr;
210 rec->period_pos += delta;
213 /* reprogram timer */
214 rec->timer.expires = jiffies + 1;
215 add_timer(&rec->timer);
218 if (rec->period_pos >= (int)rec->period_size) {
219 rec->period_pos %= rec->period_size;
220 spin_unlock(&rec->timer_lock);
221 snd_pcm_period_elapsed(rec->substream);
224 spin_unlock(&rec->timer_lock);
230 * creating an instance here
232 static int emu8k_pcm_open(snd_pcm_substream_t *subs)
234 emu8000_t *emu = snd_pcm_substream_chip(subs);
236 snd_pcm_runtime_t *runtime = subs->runtime;
238 rec = snd_kcalloc(sizeof(*rec), GFP_KERNEL);
243 rec->substream = subs;
244 runtime->private_data = rec;
246 spin_lock_init(&rec->timer_lock);
247 init_timer(&rec->timer);
248 rec->timer.function = emu8k_pcm_timer_func;
249 rec->timer.data = (unsigned long)rec;
251 runtime->hw = emu8k_pcm_hw;
252 runtime->hw.buffer_bytes_max = emu->mem_size - LOOP_BLANK_SIZE * 3;
253 runtime->hw.period_bytes_max = runtime->hw.buffer_bytes_max / 2;
255 /* use timer to update periods.. (specified in msec) */
256 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
257 (1000000 + HZ - 1) / HZ, UINT_MAX);
262 static int emu8k_pcm_close(snd_pcm_substream_t *subs)
264 emu8k_pcm_t *rec = subs->runtime->private_data;
267 subs->runtime->private_data = NULL;
272 * calculate pitch target
274 static int calc_pitch_target(int pitch)
276 int ptarget = 1 << (pitch >> 12);
277 if (pitch & 0x800) ptarget += (ptarget * 0x102e) / 0x2710;
278 if (pitch & 0x400) ptarget += (ptarget * 0x764) / 0x2710;
279 if (pitch & 0x200) ptarget += (ptarget * 0x389) / 0x2710;
280 ptarget += (ptarget >> 1);
281 if (ptarget > 0xffff) ptarget = 0xffff;
288 static void setup_voice(emu8k_pcm_t *rec, int ch)
290 emu8000_t *hw = rec->emu;
293 /* channel to be silent and idle */
294 EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080);
295 EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF);
296 EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF);
297 EMU8000_PTRX_WRITE(hw, ch, 0);
298 EMU8000_CPF_WRITE(hw, ch, 0);
301 EMU8000_IP_WRITE(hw, ch, rec->pitch);
302 /* set envelope parameters */
303 EMU8000_ENVVAL_WRITE(hw, ch, 0x8000);
304 EMU8000_ATKHLD_WRITE(hw, ch, 0x7f7f);
305 EMU8000_DCYSUS_WRITE(hw, ch, 0x7f7f);
306 EMU8000_ENVVOL_WRITE(hw, ch, 0x8000);
307 EMU8000_ATKHLDV_WRITE(hw, ch, 0x7f7f);
308 /* decay/sustain parameter for volume envelope is used
309 for triggerg the voice */
310 /* modulation envelope heights */
311 EMU8000_PEFE_WRITE(hw, ch, 0x0);
313 EMU8000_LFO1VAL_WRITE(hw, ch, 0x8000);
314 EMU8000_LFO2VAL_WRITE(hw, ch, 0x8000);
315 /* lfo1 pitch & cutoff shift */
316 EMU8000_FMMOD_WRITE(hw, ch, 0);
317 /* lfo1 volume & freq */
318 EMU8000_TREMFRQ_WRITE(hw, ch, 0);
319 /* lfo2 pitch & freq */
320 EMU8000_FM2FRQ2_WRITE(hw, ch, 0);
321 /* pan & loop start */
322 temp = rec->panning[ch];
323 temp = (temp <<24) | ((unsigned int)rec->loop_start[ch] - 1);
324 EMU8000_PSST_WRITE(hw, ch, temp);
325 /* chorus & loop end (chorus 8bit, MSB) */
327 temp = (temp << 24) | ((unsigned int)rec->loop_start[ch] + rec->buf_size - 1);
328 EMU8000_CSL_WRITE(hw, ch, temp);
329 /* Q & current address (Q 4bit value, MSB) */
331 temp = (temp << 28) | ((unsigned int)rec->loop_start[ch] - 1);
332 EMU8000_CCCA_WRITE(hw, ch, temp);
333 /* clear unknown registers */
334 EMU8000_00A0_WRITE(hw, ch, 0);
335 EMU8000_0080_WRITE(hw, ch, 0);
341 static void start_voice(emu8k_pcm_t *rec, int ch)
344 emu8000_t *hw = rec->emu;
345 unsigned int temp, aux;
346 int pt = calc_pitch_target(rec->pitch);
348 /* cutoff and volume */
349 EMU8000_IFATN_WRITE(hw, ch, 0xff00);
350 EMU8000_VTFT_WRITE(hw, ch, 0xffff);
351 EMU8000_CVCF_WRITE(hw, ch, 0xffff);
352 /* trigger envelope */
353 EMU8000_DCYSUSV_WRITE(hw, ch, 0x7f7f);
354 /* set reverb and pitch target */
356 if (rec->panning[ch] == 0)
359 aux = (-rec->panning[ch]) & 0xff;
360 temp = (temp << 8) | (pt << 16) | aux;
361 EMU8000_PTRX_WRITE(hw, ch, temp);
362 EMU8000_CPF_WRITE(hw, ch, pt << 16);
365 spin_lock_irqsave(&rec->timer_lock, flags);
366 if (! rec->timer_running) {
367 rec->timer.expires = jiffies + 1;
368 add_timer(&rec->timer);
369 rec->timer_running = 1;
371 spin_unlock_irqrestore(&rec->timer_lock, flags);
375 * stop the voice immediately
377 static void stop_voice(emu8k_pcm_t *rec, int ch)
380 emu8000_t *hw = rec->emu;
382 EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F);
385 spin_lock_irqsave(&rec->timer_lock, flags);
386 if (rec->timer_running) {
387 del_timer(&rec->timer);
388 rec->timer_running = 0;
390 spin_unlock_irqrestore(&rec->timer_lock, flags);
393 static int emu8k_pcm_trigger(snd_pcm_substream_t *subs, int cmd)
395 emu8k_pcm_t *rec = subs->runtime->private_data;
399 case SNDRV_PCM_TRIGGER_START:
400 for (ch = 0; ch < rec->voices; ch++)
401 start_voice(rec, ch);
404 case SNDRV_PCM_TRIGGER_STOP:
406 for (ch = 0; ch < rec->voices; ch++)
421 * this macro should be inserted in the copy/silence loops
422 * to reduce the latency. without this, the system will hang up
423 * during the whole loop.
425 #define CHECK_SCHEDULER() \
428 if (signal_pending(current))\
433 #ifdef USE_NONINTERLEAVE
434 /* copy one channel block */
435 static int emu8k_transfer_block(emu8000_t *emu, int offset, unsigned short *buf, int count)
437 EMU8000_SMALW_WRITE(emu, offset);
442 EMU8000_SMLD_WRITE(emu, sval);
449 static int emu8k_pcm_copy(snd_pcm_substream_t *subs,
451 snd_pcm_uframes_t pos,
453 snd_pcm_uframes_t count)
455 emu8k_pcm_t *rec = subs->runtime->private_data;
456 emu8000_t *emu = rec->emu;
458 snd_emu8000_write_wait(emu, 1);
460 unsigned short *buf = src;
462 count /= rec->voices;
463 for (i = 0; i < rec->voices; i++) {
464 err = emu8k_transfer_block(emu, pos + rec->loop_start[i], buf, count);
471 return emu8k_transfer_block(emu, pos + rec->loop_start[voice], src, count);
475 /* make a channel block silence */
476 static int emu8k_silence_block(emu8000_t *emu, int offset, int count)
478 EMU8000_SMALW_WRITE(emu, offset);
481 EMU8000_SMLD_WRITE(emu, 0);
487 static int emu8k_pcm_silence(snd_pcm_substream_t *subs,
489 snd_pcm_uframes_t pos,
490 snd_pcm_uframes_t count)
492 emu8k_pcm_t *rec = subs->runtime->private_data;
493 emu8000_t *emu = rec->emu;
495 snd_emu8000_write_wait(emu, 1);
496 if (voice == -1 && rec->voices == 1)
500 err = emu8k_silence_block(emu, pos + rec->loop_start[0], count / 2);
503 return emu8k_silence_block(emu, pos + rec->loop_start[1], count / 2);
505 return emu8k_silence_block(emu, pos + rec->loop_start[voice], count);
509 #else /* interleave */
512 * copy the interleaved data can be done easily by using
513 * DMA "left" and "right" channels on emu8k engine.
515 static int emu8k_pcm_copy(snd_pcm_substream_t *subs,
517 snd_pcm_uframes_t pos,
519 snd_pcm_uframes_t count)
521 emu8k_pcm_t *rec = subs->runtime->private_data;
522 emu8000_t *emu = rec->emu;
523 unsigned short __user *buf = src;
525 snd_emu8000_write_wait(emu, 1);
526 EMU8000_SMALW_WRITE(emu, pos + rec->loop_start[0]);
528 EMU8000_SMARW_WRITE(emu, pos + rec->loop_start[1]);
530 while (count-- > 0) {
534 EMU8000_SMLD_WRITE(emu, sval);
536 if (rec->voices > 1) {
539 EMU8000_SMRD_WRITE(emu, sval);
546 static int emu8k_pcm_silence(snd_pcm_substream_t *subs,
548 snd_pcm_uframes_t pos,
549 snd_pcm_uframes_t count)
551 emu8k_pcm_t *rec = subs->runtime->private_data;
552 emu8000_t *emu = rec->emu;
554 snd_emu8000_write_wait(emu, 1);
555 EMU8000_SMALW_WRITE(emu, rec->loop_start[0] + pos);
557 EMU8000_SMARW_WRITE(emu, rec->loop_start[1] + pos);
558 while (count-- > 0) {
560 EMU8000_SMLD_WRITE(emu, 0);
561 if (rec->voices > 1) {
563 EMU8000_SMRD_WRITE(emu, 0);
572 * allocate a memory block
574 static int emu8k_pcm_hw_params(snd_pcm_substream_t *subs,
575 snd_pcm_hw_params_t *hw_params)
577 emu8k_pcm_t *rec = subs->runtime->private_data;
580 /* reallocation - release the old block */
581 snd_util_mem_free(rec->emu->memhdr, rec->block);
585 rec->allocated_bytes = params_buffer_bytes(hw_params) + LOOP_BLANK_SIZE * 4;
586 rec->block = snd_util_mem_alloc(rec->emu->memhdr, rec->allocated_bytes);
589 rec->offset = EMU8000_DRAM_OFFSET + (rec->block->offset >> 1); /* in word */
590 /* at least dma_bytes must be set for non-interleaved mode */
591 subs->dma_buffer.bytes = params_buffer_bytes(hw_params);
597 * free the memory block
599 static int emu8k_pcm_hw_free(snd_pcm_substream_t *subs)
601 emu8k_pcm_t *rec = subs->runtime->private_data;
605 for (ch = 0; ch < rec->voices; ch++)
606 stop_voice(rec, ch); // to be sure
607 if (rec->dram_opened)
608 emu8k_close_dram(rec->emu);
609 snd_util_mem_free(rec->emu->memhdr, rec->block);
617 static int emu8k_pcm_prepare(snd_pcm_substream_t *subs)
619 emu8k_pcm_t *rec = subs->runtime->private_data;
621 rec->pitch = 0xe000 + calc_rate_offset(subs->runtime->rate);
625 rec->buf_size = subs->runtime->buffer_size;
626 rec->period_size = subs->runtime->period_size;
627 rec->voices = subs->runtime->channels;
628 rec->loop_start[0] = rec->offset + LOOP_BLANK_SIZE;
630 rec->loop_start[1] = rec->loop_start[0] + rec->buf_size + LOOP_BLANK_SIZE;
631 if (rec->voices > 1) {
632 rec->panning[0] = 0xff;
633 rec->panning[1] = 0x00;
635 rec->panning[0] = 0x80;
637 if (! rec->dram_opened) {
640 snd_emux_terminate_all(rec->emu->emu);
641 if ((err = emu8k_open_dram_for_pcm(rec->emu, rec->voices)) != 0)
643 rec->dram_opened = 1;
645 /* clear loop blanks */
646 snd_emu8000_write_wait(rec->emu, 0);
647 EMU8000_SMALW_WRITE(rec->emu, rec->offset);
648 for (i = 0; i < LOOP_BLANK_SIZE; i++)
649 EMU8000_SMLD_WRITE(rec->emu, 0);
650 for (ch = 0; ch < rec->voices; ch++) {
651 EMU8000_SMALW_WRITE(rec->emu, rec->loop_start[ch] + rec->buf_size);
652 for (i = 0; i < LOOP_BLANK_SIZE; i++)
653 EMU8000_SMLD_WRITE(rec->emu, 0);
663 static snd_pcm_uframes_t emu8k_pcm_pointer(snd_pcm_substream_t *subs)
665 emu8k_pcm_t *rec = subs->runtime->private_data;
667 return emu8k_get_curpos(rec, 0);
672 static snd_pcm_ops_t emu8k_pcm_ops = {
673 .open = emu8k_pcm_open,
674 .close = emu8k_pcm_close,
675 .ioctl = snd_pcm_lib_ioctl,
676 .hw_params = emu8k_pcm_hw_params,
677 .hw_free = emu8k_pcm_hw_free,
678 .prepare = emu8k_pcm_prepare,
679 .trigger = emu8k_pcm_trigger,
680 .pointer = emu8k_pcm_pointer,
681 .copy = emu8k_pcm_copy,
682 .silence = emu8k_pcm_silence,
686 static void snd_emu8000_pcm_free(snd_pcm_t *pcm)
688 emu8000_t *emu = pcm->private_data;
692 int snd_emu8000_pcm_new(snd_card_t *card, emu8000_t *emu, int index)
697 if ((err = snd_pcm_new(card, "Emu8000 PCM", index, 1, 0, &pcm)) < 0)
699 pcm->private_data = emu;
700 pcm->private_free = snd_emu8000_pcm_free;
701 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &emu8k_pcm_ops);
704 snd_device_register(card, pcm);