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>
27 * define the following if you want to use this pcm with non-interleaved mode
29 /* #define USE_NONINTERLEAVE */
31 /* NOTE: for using the non-interleaved mode with alsa-lib, you have to set
32 * mmap_emulation flag to 1 in your .asoundrc, such like
44 * besides, for the time being, the non-interleaved mode doesn't work well on
49 typedef struct snd_emu8k_pcm emu8k_pcm_t
;
51 struct snd_emu8k_pcm
{
53 snd_pcm_substream_t
*substream
;
55 unsigned int allocated_bytes
;
56 snd_util_memblk_t
*block
;
58 unsigned int buf_size
;
59 unsigned int period_size
;
60 unsigned int loop_start
[2];
66 unsigned int dram_opened
: 1;
67 unsigned int running
: 1;
68 unsigned int timer_running
: 1;
69 struct timer_list timer
;
70 spinlock_t timer_lock
;
73 #define LOOP_BLANK_SIZE 8
77 * open up channels for the simultaneous data transfer and playback
80 emu8k_open_dram_for_pcm(emu8000_t
*emu
, int channels
)
84 /* reserve up to 2 voices for playback */
85 snd_emux_lock_voice(emu
->emu
, 0);
87 snd_emux_lock_voice(emu
->emu
, 1);
89 /* reserve 28 voices for loading */
90 for (i
= channels
+ 1; i
< EMU8000_DRAM_VOICES
; i
++) {
91 unsigned int mode
= EMU8000_RAM_WRITE
;
92 snd_emux_lock_voice(emu
->emu
, i
);
93 #ifndef USE_NONINTERLEAVE
94 if (channels
> 1 && (i
& 1) != 0)
95 mode
|= EMU8000_RAM_RIGHT
;
97 snd_emu8000_dma_chan(emu
, i
, mode
);
100 /* assign voice 31 and 32 to ROM */
101 EMU8000_VTFT_WRITE(emu
, 30, 0);
102 EMU8000_PSST_WRITE(emu
, 30, 0x1d8);
103 EMU8000_CSL_WRITE(emu
, 30, 0x1e0);
104 EMU8000_CCCA_WRITE(emu
, 30, 0x1d8);
105 EMU8000_VTFT_WRITE(emu
, 31, 0);
106 EMU8000_PSST_WRITE(emu
, 31, 0x1d8);
107 EMU8000_CSL_WRITE(emu
, 31, 0x1e0);
108 EMU8000_CCCA_WRITE(emu
, 31, 0x1d8);
116 snd_emu8000_write_wait(emu8000_t
*emu
, int can_schedule
)
118 while ((EMU8000_SMALW_READ(emu
) & 0x80000000) != 0) {
120 set_current_state(TASK_INTERRUPTIBLE
);
122 if (signal_pending(current
))
132 emu8k_close_dram(emu8000_t
*emu
)
136 for (i
= 0; i
< 2; i
++)
137 snd_emux_unlock_voice(emu
->emu
, i
);
138 for (; i
< EMU8000_DRAM_VOICES
; i
++) {
139 snd_emu8000_dma_chan(emu
, i
, EMU8000_RAM_CLOSE
);
140 snd_emux_unlock_voice(emu
->emu
, i
);
145 * convert Hz to AWE32 rate offset (see emux/soundfont.c)
148 #define OFFSET_SAMPLERATE 1011119 /* base = 44100 */
149 #define SAMPLERATE_RATIO 4096
151 static int calc_rate_offset(int hz
)
153 return snd_sf_linear_to_log(hz
, OFFSET_SAMPLERATE
, SAMPLERATE_RATIO
);
160 static snd_pcm_hardware_t emu8k_pcm_hw
= {
161 #ifdef USE_NONINTERLEAVE
162 .info
= SNDRV_PCM_INFO_NONINTERLEAVED
,
164 .info
= SNDRV_PCM_INFO_INTERLEAVED
,
166 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
167 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
172 .buffer_bytes_max
= (128*1024),
173 .period_bytes_min
= 1024,
174 .period_bytes_max
= (128*1024),
182 * get the current position at the given channel from CCCA register
184 static inline int emu8k_get_curpos(emu8k_pcm_t
*rec
, int ch
)
186 int val
= EMU8000_CCCA_READ(rec
->emu
, ch
) & 0xfffffff;
187 val
-= rec
->loop_start
[ch
] - 1;
193 * timer interrupt handler
194 * check the current position and update the period if necessary.
196 static void emu8k_pcm_timer_func(unsigned long data
)
198 emu8k_pcm_t
*rec
= (emu8k_pcm_t
*)data
;
201 spin_lock(&rec
->timer_lock
);
202 /* update the current pointer */
203 ptr
= emu8k_get_curpos(rec
, 0);
204 if (ptr
< rec
->last_ptr
)
205 delta
= ptr
+ rec
->buf_size
- rec
->last_ptr
;
207 delta
= ptr
- rec
->last_ptr
;
208 rec
->period_pos
+= delta
;
211 /* reprogram timer */
212 rec
->timer
.expires
= jiffies
+ 1;
213 add_timer(&rec
->timer
);
216 if (rec
->period_pos
>= (int)rec
->period_size
) {
217 rec
->period_pos
%= rec
->period_size
;
218 spin_unlock(&rec
->timer_lock
);
219 snd_pcm_period_elapsed(rec
->substream
);
222 spin_unlock(&rec
->timer_lock
);
228 * creating an instance here
230 static int emu8k_pcm_open(snd_pcm_substream_t
*subs
)
232 emu8000_t
*emu
= snd_pcm_substream_chip(subs
);
234 snd_pcm_runtime_t
*runtime
= subs
->runtime
;
236 rec
= kcalloc(1, sizeof(*rec
), GFP_KERNEL
);
241 rec
->substream
= subs
;
242 runtime
->private_data
= rec
;
244 spin_lock_init(&rec
->timer_lock
);
245 init_timer(&rec
->timer
);
246 rec
->timer
.function
= emu8k_pcm_timer_func
;
247 rec
->timer
.data
= (unsigned long)rec
;
249 runtime
->hw
= emu8k_pcm_hw
;
250 runtime
->hw
.buffer_bytes_max
= emu
->mem_size
- LOOP_BLANK_SIZE
* 3;
251 runtime
->hw
.period_bytes_max
= runtime
->hw
.buffer_bytes_max
/ 2;
253 /* use timer to update periods.. (specified in msec) */
254 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
255 (1000000 + HZ
- 1) / HZ
, UINT_MAX
);
260 static int emu8k_pcm_close(snd_pcm_substream_t
*subs
)
262 emu8k_pcm_t
*rec
= subs
->runtime
->private_data
;
265 subs
->runtime
->private_data
= NULL
;
270 * calculate pitch target
272 static int calc_pitch_target(int pitch
)
274 int ptarget
= 1 << (pitch
>> 12);
275 if (pitch
& 0x800) ptarget
+= (ptarget
* 0x102e) / 0x2710;
276 if (pitch
& 0x400) ptarget
+= (ptarget
* 0x764) / 0x2710;
277 if (pitch
& 0x200) ptarget
+= (ptarget
* 0x389) / 0x2710;
278 ptarget
+= (ptarget
>> 1);
279 if (ptarget
> 0xffff) ptarget
= 0xffff;
286 static void setup_voice(emu8k_pcm_t
*rec
, int ch
)
288 emu8000_t
*hw
= rec
->emu
;
291 /* channel to be silent and idle */
292 EMU8000_DCYSUSV_WRITE(hw
, ch
, 0x0080);
293 EMU8000_VTFT_WRITE(hw
, ch
, 0x0000FFFF);
294 EMU8000_CVCF_WRITE(hw
, ch
, 0x0000FFFF);
295 EMU8000_PTRX_WRITE(hw
, ch
, 0);
296 EMU8000_CPF_WRITE(hw
, ch
, 0);
299 EMU8000_IP_WRITE(hw
, ch
, rec
->pitch
);
300 /* set envelope parameters */
301 EMU8000_ENVVAL_WRITE(hw
, ch
, 0x8000);
302 EMU8000_ATKHLD_WRITE(hw
, ch
, 0x7f7f);
303 EMU8000_DCYSUS_WRITE(hw
, ch
, 0x7f7f);
304 EMU8000_ENVVOL_WRITE(hw
, ch
, 0x8000);
305 EMU8000_ATKHLDV_WRITE(hw
, ch
, 0x7f7f);
306 /* decay/sustain parameter for volume envelope is used
307 for triggerg the voice */
308 /* modulation envelope heights */
309 EMU8000_PEFE_WRITE(hw
, ch
, 0x0);
311 EMU8000_LFO1VAL_WRITE(hw
, ch
, 0x8000);
312 EMU8000_LFO2VAL_WRITE(hw
, ch
, 0x8000);
313 /* lfo1 pitch & cutoff shift */
314 EMU8000_FMMOD_WRITE(hw
, ch
, 0);
315 /* lfo1 volume & freq */
316 EMU8000_TREMFRQ_WRITE(hw
, ch
, 0);
317 /* lfo2 pitch & freq */
318 EMU8000_FM2FRQ2_WRITE(hw
, ch
, 0);
319 /* pan & loop start */
320 temp
= rec
->panning
[ch
];
321 temp
= (temp
<<24) | ((unsigned int)rec
->loop_start
[ch
] - 1);
322 EMU8000_PSST_WRITE(hw
, ch
, temp
);
323 /* chorus & loop end (chorus 8bit, MSB) */
325 temp
= (temp
<< 24) | ((unsigned int)rec
->loop_start
[ch
] + rec
->buf_size
- 1);
326 EMU8000_CSL_WRITE(hw
, ch
, temp
);
327 /* Q & current address (Q 4bit value, MSB) */
329 temp
= (temp
<< 28) | ((unsigned int)rec
->loop_start
[ch
] - 1);
330 EMU8000_CCCA_WRITE(hw
, ch
, temp
);
331 /* clear unknown registers */
332 EMU8000_00A0_WRITE(hw
, ch
, 0);
333 EMU8000_0080_WRITE(hw
, ch
, 0);
339 static void start_voice(emu8k_pcm_t
*rec
, int ch
)
342 emu8000_t
*hw
= rec
->emu
;
343 unsigned int temp
, aux
;
344 int pt
= calc_pitch_target(rec
->pitch
);
346 /* cutoff and volume */
347 EMU8000_IFATN_WRITE(hw
, ch
, 0xff00);
348 EMU8000_VTFT_WRITE(hw
, ch
, 0xffff);
349 EMU8000_CVCF_WRITE(hw
, ch
, 0xffff);
350 /* trigger envelope */
351 EMU8000_DCYSUSV_WRITE(hw
, ch
, 0x7f7f);
352 /* set reverb and pitch target */
354 if (rec
->panning
[ch
] == 0)
357 aux
= (-rec
->panning
[ch
]) & 0xff;
358 temp
= (temp
<< 8) | (pt
<< 16) | aux
;
359 EMU8000_PTRX_WRITE(hw
, ch
, temp
);
360 EMU8000_CPF_WRITE(hw
, ch
, pt
<< 16);
363 spin_lock_irqsave(&rec
->timer_lock
, flags
);
364 if (! rec
->timer_running
) {
365 rec
->timer
.expires
= jiffies
+ 1;
366 add_timer(&rec
->timer
);
367 rec
->timer_running
= 1;
369 spin_unlock_irqrestore(&rec
->timer_lock
, flags
);
373 * stop the voice immediately
375 static void stop_voice(emu8k_pcm_t
*rec
, int ch
)
378 emu8000_t
*hw
= rec
->emu
;
380 EMU8000_DCYSUSV_WRITE(hw
, ch
, 0x807F);
383 spin_lock_irqsave(&rec
->timer_lock
, flags
);
384 if (rec
->timer_running
) {
385 del_timer(&rec
->timer
);
386 rec
->timer_running
= 0;
388 spin_unlock_irqrestore(&rec
->timer_lock
, flags
);
391 static int emu8k_pcm_trigger(snd_pcm_substream_t
*subs
, int cmd
)
393 emu8k_pcm_t
*rec
= subs
->runtime
->private_data
;
397 case SNDRV_PCM_TRIGGER_START
:
398 for (ch
= 0; ch
< rec
->voices
; ch
++)
399 start_voice(rec
, ch
);
402 case SNDRV_PCM_TRIGGER_STOP
:
404 for (ch
= 0; ch
< rec
->voices
; ch
++)
419 * this macro should be inserted in the copy/silence loops
420 * to reduce the latency. without this, the system will hang up
421 * during the whole loop.
423 #define CHECK_SCHEDULER() \
426 if (signal_pending(current))\
431 #ifdef USE_NONINTERLEAVE
432 /* copy one channel block */
433 static int emu8k_transfer_block(emu8000_t
*emu
, int offset
, unsigned short *buf
, int count
)
435 EMU8000_SMALW_WRITE(emu
, offset
);
440 EMU8000_SMLD_WRITE(emu
, sval
);
447 static int emu8k_pcm_copy(snd_pcm_substream_t
*subs
,
449 snd_pcm_uframes_t pos
,
451 snd_pcm_uframes_t count
)
453 emu8k_pcm_t
*rec
= subs
->runtime
->private_data
;
454 emu8000_t
*emu
= rec
->emu
;
456 snd_emu8000_write_wait(emu
, 1);
458 unsigned short *buf
= src
;
460 count
/= rec
->voices
;
461 for (i
= 0; i
< rec
->voices
; i
++) {
462 err
= emu8k_transfer_block(emu
, pos
+ rec
->loop_start
[i
], buf
, count
);
469 return emu8k_transfer_block(emu
, pos
+ rec
->loop_start
[voice
], src
, count
);
473 /* make a channel block silence */
474 static int emu8k_silence_block(emu8000_t
*emu
, int offset
, int count
)
476 EMU8000_SMALW_WRITE(emu
, offset
);
479 EMU8000_SMLD_WRITE(emu
, 0);
485 static int emu8k_pcm_silence(snd_pcm_substream_t
*subs
,
487 snd_pcm_uframes_t pos
,
488 snd_pcm_uframes_t count
)
490 emu8k_pcm_t
*rec
= subs
->runtime
->private_data
;
491 emu8000_t
*emu
= rec
->emu
;
493 snd_emu8000_write_wait(emu
, 1);
494 if (voice
== -1 && rec
->voices
== 1)
498 err
= emu8k_silence_block(emu
, pos
+ rec
->loop_start
[0], count
/ 2);
501 return emu8k_silence_block(emu
, pos
+ rec
->loop_start
[1], count
/ 2);
503 return emu8k_silence_block(emu
, pos
+ rec
->loop_start
[voice
], count
);
507 #else /* interleave */
510 * copy the interleaved data can be done easily by using
511 * DMA "left" and "right" channels on emu8k engine.
513 static int emu8k_pcm_copy(snd_pcm_substream_t
*subs
,
515 snd_pcm_uframes_t pos
,
517 snd_pcm_uframes_t count
)
519 emu8k_pcm_t
*rec
= subs
->runtime
->private_data
;
520 emu8000_t
*emu
= rec
->emu
;
521 unsigned short __user
*buf
= src
;
523 snd_emu8000_write_wait(emu
, 1);
524 EMU8000_SMALW_WRITE(emu
, pos
+ rec
->loop_start
[0]);
526 EMU8000_SMARW_WRITE(emu
, pos
+ rec
->loop_start
[1]);
528 while (count
-- > 0) {
532 EMU8000_SMLD_WRITE(emu
, sval
);
534 if (rec
->voices
> 1) {
537 EMU8000_SMRD_WRITE(emu
, sval
);
544 static int emu8k_pcm_silence(snd_pcm_substream_t
*subs
,
546 snd_pcm_uframes_t pos
,
547 snd_pcm_uframes_t count
)
549 emu8k_pcm_t
*rec
= subs
->runtime
->private_data
;
550 emu8000_t
*emu
= rec
->emu
;
552 snd_emu8000_write_wait(emu
, 1);
553 EMU8000_SMALW_WRITE(emu
, rec
->loop_start
[0] + pos
);
555 EMU8000_SMARW_WRITE(emu
, rec
->loop_start
[1] + pos
);
556 while (count
-- > 0) {
558 EMU8000_SMLD_WRITE(emu
, 0);
559 if (rec
->voices
> 1) {
561 EMU8000_SMRD_WRITE(emu
, 0);
570 * allocate a memory block
572 static int emu8k_pcm_hw_params(snd_pcm_substream_t
*subs
,
573 snd_pcm_hw_params_t
*hw_params
)
575 emu8k_pcm_t
*rec
= subs
->runtime
->private_data
;
578 /* reallocation - release the old block */
579 snd_util_mem_free(rec
->emu
->memhdr
, rec
->block
);
583 rec
->allocated_bytes
= params_buffer_bytes(hw_params
) + LOOP_BLANK_SIZE
* 4;
584 rec
->block
= snd_util_mem_alloc(rec
->emu
->memhdr
, rec
->allocated_bytes
);
587 rec
->offset
= EMU8000_DRAM_OFFSET
+ (rec
->block
->offset
>> 1); /* in word */
588 /* at least dma_bytes must be set for non-interleaved mode */
589 subs
->dma_buffer
.bytes
= params_buffer_bytes(hw_params
);
595 * free the memory block
597 static int emu8k_pcm_hw_free(snd_pcm_substream_t
*subs
)
599 emu8k_pcm_t
*rec
= subs
->runtime
->private_data
;
603 for (ch
= 0; ch
< rec
->voices
; ch
++)
604 stop_voice(rec
, ch
); // to be sure
605 if (rec
->dram_opened
)
606 emu8k_close_dram(rec
->emu
);
607 snd_util_mem_free(rec
->emu
->memhdr
, rec
->block
);
615 static int emu8k_pcm_prepare(snd_pcm_substream_t
*subs
)
617 emu8k_pcm_t
*rec
= subs
->runtime
->private_data
;
619 rec
->pitch
= 0xe000 + calc_rate_offset(subs
->runtime
->rate
);
623 rec
->buf_size
= subs
->runtime
->buffer_size
;
624 rec
->period_size
= subs
->runtime
->period_size
;
625 rec
->voices
= subs
->runtime
->channels
;
626 rec
->loop_start
[0] = rec
->offset
+ LOOP_BLANK_SIZE
;
628 rec
->loop_start
[1] = rec
->loop_start
[0] + rec
->buf_size
+ LOOP_BLANK_SIZE
;
629 if (rec
->voices
> 1) {
630 rec
->panning
[0] = 0xff;
631 rec
->panning
[1] = 0x00;
633 rec
->panning
[0] = 0x80;
635 if (! rec
->dram_opened
) {
638 snd_emux_terminate_all(rec
->emu
->emu
);
639 if ((err
= emu8k_open_dram_for_pcm(rec
->emu
, rec
->voices
)) != 0)
641 rec
->dram_opened
= 1;
643 /* clear loop blanks */
644 snd_emu8000_write_wait(rec
->emu
, 0);
645 EMU8000_SMALW_WRITE(rec
->emu
, rec
->offset
);
646 for (i
= 0; i
< LOOP_BLANK_SIZE
; i
++)
647 EMU8000_SMLD_WRITE(rec
->emu
, 0);
648 for (ch
= 0; ch
< rec
->voices
; ch
++) {
649 EMU8000_SMALW_WRITE(rec
->emu
, rec
->loop_start
[ch
] + rec
->buf_size
);
650 for (i
= 0; i
< LOOP_BLANK_SIZE
; i
++)
651 EMU8000_SMLD_WRITE(rec
->emu
, 0);
661 static snd_pcm_uframes_t
emu8k_pcm_pointer(snd_pcm_substream_t
*subs
)
663 emu8k_pcm_t
*rec
= subs
->runtime
->private_data
;
665 return emu8k_get_curpos(rec
, 0);
670 static snd_pcm_ops_t emu8k_pcm_ops
= {
671 .open
= emu8k_pcm_open
,
672 .close
= emu8k_pcm_close
,
673 .ioctl
= snd_pcm_lib_ioctl
,
674 .hw_params
= emu8k_pcm_hw_params
,
675 .hw_free
= emu8k_pcm_hw_free
,
676 .prepare
= emu8k_pcm_prepare
,
677 .trigger
= emu8k_pcm_trigger
,
678 .pointer
= emu8k_pcm_pointer
,
679 .copy
= emu8k_pcm_copy
,
680 .silence
= emu8k_pcm_silence
,
684 static void snd_emu8000_pcm_free(snd_pcm_t
*pcm
)
686 emu8000_t
*emu
= pcm
->private_data
;
690 int snd_emu8000_pcm_new(snd_card_t
*card
, emu8000_t
*emu
, int index
)
695 if ((err
= snd_pcm_new(card
, "Emu8000 PCM", index
, 1, 0, &pcm
)) < 0)
697 pcm
->private_data
= emu
;
698 pcm
->private_free
= snd_emu8000_pcm_free
;
699 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &emu8k_pcm_ops
);
702 snd_device_register(card
, pcm
);