2 terminatorX - realtime audio scratching software
3 Copyright (C) 1999-2003 Alexander König
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 File: tX_aduiodevice.cc
21 Description: Implements Audiodevice handling...
24 #define ALSA_PCM_NEW_HW_PARAMS_API
26 #include "tX_audiodevice.h"
29 #include <sys/types.h>
31 #include <sys/ioctl.h>
33 #include <sys/soundcard.h>
36 #include "tX_endian.h"
39 # define __USE_XOPEN // we need this for swab()
49 #include "tX_engine.h"
51 tX_audiodevice :: tX_audiodevice() : samples_per_buffer(0),
52 current_buffer(0), buffer_pos(0), is_open(false)
54 sample_buffer[0]=NULL;
55 sample_buffer[1]=NULL;
56 engine=tX_engine::get_instance();
59 void tX_audiodevice :: start() {
60 sample_buffer[0]=new int16_t[samples_per_buffer*2];
61 sample_buffer[1]=new int16_t[samples_per_buffer*2];
63 vtt_buffer_size=vtt_class::get_mix_buffer_size()<<1;
67 while (!engine->is_stopped()) {
68 current=current ? 0 : 1;
70 int16_t *current_buffer=sample_buffer[current];
71 int16_t *next_buffer=sample_buffer[current ? 0 : 1];
73 fill_buffer(current_buffer, next_buffer);
77 delete [] sample_buffer[0];
78 delete [] sample_buffer[1];
81 void tX_audiodevice :: fill_buffer(int16_t *target_buffer, int16_t *next_target_buffer) {
84 while (buffer_pos <= samples_per_buffer) {
85 int16_t *data=engine->render_cycle();
87 int rest=(buffer_pos+vtt_buffer_size)-samples_per_buffer;
90 memcpy(&target_buffer[buffer_pos], data, vtt_buffer_size << 1);
92 memcpy(&target_buffer[buffer_pos], data, (vtt_buffer_size-rest) << 1);
93 memcpy(next_target_buffer, &data[vtt_buffer_size-rest], rest << 1);
97 buffer_pos+=vtt_buffer_size;
103 /* Driver Specific Code follows.. */
107 int tX_audiodevice_oss :: open()
114 fd=::open(globals.oss_device, O_WRONLY, 0);
117 tX_error("tX_audiodevice_oss::open() can't open device: %s", strerror(errno));
123 /* setting buffer size */
124 buff_cfg=(globals.oss_buff_no<<16) | globals.oss_buff_size;
127 tX_debug("tX_audiodevice_oss::open() - buff_no: %i, buff_size: %i, buff_cfg: %08x", globals.oss_buff_no, globals.oss_buff_size, buff_cfg);
129 i = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &p);
130 ioctl(fd, SNDCTL_DSP_RESET, 0);
135 i += ioctl(fd, SOUND_PCM_WRITE_BITS, &p);
140 i += ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &p);
144 p = globals.oss_samplerate;
145 i += ioctl(fd, SOUND_PCM_WRITE_RATE, &p);
147 sample_rate=globals.oss_samplerate;
149 /* Figure actual blocksize.. */
151 i += ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &blocksize);
153 samples_per_buffer=blocksize/sizeof(int16_t);
155 tX_debug("tX_adudiodevice_oss::open() - blocksize: %i, samples_per_buffer: %i", blocksize, samples_per_buffer);
157 ioctl(fd, SNDCTL_DSP_SYNC, 0);
162 int tX_audiodevice_oss :: close()
176 tX_audiodevice_oss :: tX_audiodevice_oss() : tX_audiodevice(),
177 fd(0), blocksize(0) {}
179 double tX_audiodevice_oss :: get_latency()
184 void tX_audiodevice_oss :: play(int16_t *buffer)
186 #ifdef BIG_ENDIAN_MACHINE
187 swapbuffer (buffer, samples_per_buffer);
189 int res=write(fd, buffer, blocksize);
191 tX_error("failed to write to audiodevice: %s", strerror(errno));
200 int tX_audiodevice_alsa :: open()
202 snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
203 snd_pcm_hw_params_t *hw_params;
211 sscanf(globals.alsa_device, "%i-%i: %s", &card, &device, foo);
212 sprintf(pcm_name, "hw:%i,%i", card, device);
214 if (snd_pcm_open(&pcm_handle, pcm_name, stream, SND_PCM_NONBLOCK) < 0) {
215 tX_error("ALSA: Failed to access PCM device \"%s\"", pcm_name);
221 snd_pcm_hw_params_alloca(&hw_params);
223 if (snd_pcm_hw_params_any(pcm_handle, hw_params) < 0) {
224 tX_error("ALSA: Failed to configure PCM device \"%s\"", pcm_name);
225 snd_pcm_hw_params_free (hw_params);
229 /* Setting INTERLEAVED stereo... */
230 #ifdef USE_ALSA_MEMCPY
231 if (snd_pcm_hw_params_set_access(pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
233 if (snd_pcm_hw_params_set_access(pcm_handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0) {
235 tX_error("ALSA: Failed to set interleaved access for PCM device \"%s\"", pcm_name);
236 snd_pcm_hw_params_free (hw_params);
240 /* Make it 16 Bit LE - we handle converting from BE anyway... */
241 if (snd_pcm_hw_params_set_format(pcm_handle, hw_params, SND_PCM_FORMAT_S16_LE) < 0) {
242 tX_error("ALSA: Error setting 16 Bit sample width for PCM device \"%s\"", pcm_name);
243 snd_pcm_hw_params_free (hw_params);
247 /* Setting sampling rate */
248 unsigned int hw_rate=(unsigned int)globals.alsa_samplerate;
251 if (snd_pcm_hw_params_set_rate_near(pcm_handle, hw_params, &hw_rate, &dir) < 0) {
252 tX_error("ALSA: Failed setting sample rate: %i", globals.alsa_samplerate);
253 snd_pcm_hw_params_free (hw_params);
258 tX_warning("ALSA: The PCM device \"%s\" doesnt support 44100 Hz playback - using %i instead", pcm_name, hw_rate);
263 /* Using stereo output */
264 if (snd_pcm_hw_params_set_channels(pcm_handle, hw_params, 2) < 0) {
265 tX_error("ALSA: PCM device \"%s\" does not support stereo operation", pcm_name);
266 snd_pcm_hw_params_free (hw_params);
270 unsigned int buffer_time=globals.alsa_buffer_time;
271 unsigned int period_time=globals.alsa_period_time;
273 if (snd_pcm_hw_params_set_buffer_time_near(pcm_handle, hw_params, &buffer_time, &dir) < 0) {
274 tX_error("ALSA: failed to set the buffer time opf %i usecs", globals.alsa_buffer_time);
278 long unsigned int buffer_size;
280 if (snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_size) < 0) {
281 tX_error("ALSA: failed to retreive buffer size");
285 tX_debug("ALSA: buffer size is %lu", buffer_size);
287 if (snd_pcm_hw_params_set_period_time_near(pcm_handle, hw_params, &period_time, &dir) < 0) {
288 tX_error("ALSA: failed to set period time %i", globals.alsa_period_time);
292 if (snd_pcm_hw_params_get_period_size(hw_params, &period_size, &dir)<0) {
293 tX_error("ALSA: failed to retreive period size");
297 samples_per_buffer=period_size;
299 /* Apply all that setup work.. */
300 if (snd_pcm_hw_params(pcm_handle, hw_params) < 0) {
301 tX_error("ALSA: Failed to apply settings to PCM device \"%s\"", pcm_name);
302 snd_pcm_hw_params_free (hw_params);
306 //snd_pcm_hw_params_free (hw_params);
307 return 0; //snd_pcm_prepare(pcm_handle);
310 int tX_audiodevice_alsa :: close()
313 snd_pcm_close(pcm_handle);
320 double tX_audiodevice_alsa :: get_latency()
325 tX_audiodevice_alsa :: tX_audiodevice_alsa() : tX_audiodevice(),
328 void tX_audiodevice_alsa :: play(int16_t *buffer)
330 snd_pcm_sframes_t pcmreturn;
331 #ifdef BIG_ENDIAN_MACHINE
332 swapbuffer (buffer, samples_per_buffer);
335 #ifdef USE_ALSA_MEMCPY
336 pcmreturn = snd_pcm_writei(pcm_handle, buffer, samples_per_buffer >> 1);
338 pcmreturn = snd_pcm_mmap_writei(pcm_handle, buffer, samples_per_buffer >> 1);
341 while (pcmreturn==-EPIPE) {
342 snd_pcm_prepare(pcm_handle);
344 #ifdef USE_ALSA_MEMCPY
345 pcmreturn = snd_pcm_writei(pcm_handle, buffer, samples_per_buffer >> 1);
347 pcmreturn = snd_pcm_mmap_writei(pcm_handle, buffer, samples_per_buffer >> 1);
349 //tX_warning("ALSA: ** buffer underrun **");
353 printf("snd_pcm_writei says: %s.\n", strerror(-1*pcmreturn));