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)
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];
64 while (!engine->is_stopped()) {
65 current=current ? 0 : 1;
67 int16_t *current_buffer=sample_buffer[current];
68 int16_t *next_buffer=sample_buffer[current ? 0 : 1];
70 fill_buffer(current_buffer, next_buffer);
74 delete [] sample_buffer[0];
75 delete [] sample_buffer[1];
78 void tX_audiodevice :: fill_buffer(int16_t *target_buffer, int16_t *next_target_buffer) {
79 int vtt_buffer_size=vtt_class::get_mix_buffer_size()<<1;
82 while (buffer_pos < samples_per_buffer) {
83 int16_t *data=engine->render_cycle();
85 int rest=samples_per_buffer-(buffer_pos+vtt_buffer_size);
88 memcpy(&target_buffer[buffer_pos], data, samples_per_buffer << 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);
116 /* setting buffer size */
117 buff_cfg=(globals.oss_buff_no<<16) | globals.oss_buff_size;
120 tX_debug("tX_adudiodevice_oss::open() - buff_no: %i, buff_size: %i, buff_cfg: %08x", globals.oss_buff_no, globals.oss_buff_size, buff_cfg);
122 i = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &p);
123 ioctl(fd, SNDCTL_DSP_RESET, 0);
128 i += ioctl(fd, SOUND_PCM_WRITE_BITS, &p);
133 i += ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &p);
137 p = globals.oss_samplerate;
138 i += ioctl(fd, SOUND_PCM_WRITE_RATE, &p);
140 sample_rate=globals.oss_samplerate;
142 /* Figure actual blocksize.. */
144 i += ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &blocksize);
146 samples_per_buffer=blocksize/sizeof(int16_t);
148 tX_debug("tX_adudiodevice_oss::open() - blocksize: %i, samples_per_buffer: %i", blocksize, samples_per_buffer);
150 ioctl(fd, SNDCTL_DSP_SYNC, 0);
155 int tX_audiodevice_oss :: close()
168 tX_audiodevice_oss :: tX_audiodevice_oss() : tX_audiodevice(),
169 fd(0), blocksize(0) {}
171 double tX_audiodevice_oss :: get_latency()
176 void tX_audiodevice_oss :: play(int16_t *buffer)
178 #ifdef BIG_ENDIAN_MACHINE
179 swapbuffer (buffer, samples_per_buffer);
181 int res=write(fd, buffer, blocksize);
183 tX_error("failed to write to audiodevice: %s", strerror(errno));
192 int tX_audiodevice_alsa :: open()
194 snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
195 snd_pcm_hw_params_t *hw_params;
199 snd_pcm_hw_params_alloca(&hw_params);
204 sscanf(globals.alsa_device, "%i-%i: %s", &card, &device, foo);
205 sprintf(pcm_name, "hw:%i,%i", card, device);
207 if (snd_pcm_open(&pcm_handle, pcm_name, stream, 0) < 0) {
208 tX_error("ALSA: Failed to access PCM device \"%s\"", pcm_name);
209 snd_pcm_hw_params_free (hw_params);
213 if (snd_pcm_hw_params_any(pcm_handle, hw_params) < 0) {
214 tX_error("ALSA: Failed to configure PCM device \"%s\"", pcm_name);
215 snd_pcm_hw_params_free (hw_params);
219 /* Setting INTERLEAVED stereo... */
220 if (snd_pcm_hw_params_set_access(pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
221 tX_error("ALSA: Failed to set interleaved access for PCM device \"%s\"", pcm_name);
222 snd_pcm_hw_params_free (hw_params);
226 /* Make it 16 Bit LE - we handle converting from BE anyway... */
227 if (snd_pcm_hw_params_set_format(pcm_handle, hw_params, SND_PCM_FORMAT_S16_LE) < 0) {
228 tX_error("ALSA: Error setting 16 Bit sample width for PCM device \"%s\"", pcm_name);
229 snd_pcm_hw_params_free (hw_params);
233 /* Setting sampling rate */
234 unsigned int hw_rate=(unsigned int)globals.alsa_samplerate;
237 int res = snd_pcm_hw_params_set_rate_near(pcm_handle, hw_params, &hw_rate, &dir);
240 tX_warning("ALSA: The PCM device \"%s\" doesnt support 44100 Hz playback - using %i instead", pcm_name, hw_rate);
245 /* Using stereo output */
246 if (snd_pcm_hw_params_set_channels(pcm_handle, hw_params, 2) < 0) {
247 tX_error("ALSA: PCM device \"%s\" does not support stereo operation", pcm_name);
248 snd_pcm_hw_params_free (hw_params);
252 /* Setting the number of buffers... */
253 /* if (snd_pcm_hw_params_set_periods(pcm_handle, hw_params, globals.alsa_buff_no, 0) < 0) {
254 tX_error("ALSA: Failed to set %i periods for PCM device \"%s\"", globals.alsa_buff_no, pcm_name);
255 snd_pcm_hw_params_free (hw_params);
259 /* Setting the buffersize - ALSA cripples my mind, really... */
260 long unsigned int samples;
261 long unsigned int periodsize;
263 periodsize = globals.alsa_buff_size * 2;
265 samples = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hw_params, &periodsize);
267 tX_error("ALSA: Failed to set buffersize %li", globals.alsa_buff_size);
271 samples_per_buffer=periodsize;//hw_buffsize/sizeof(int16_t);
274 if (snd_pcm_hw_params_set_period_size_near(pcm_handle, hw_params, &periodsize, 0) < 0) {
275 tX_error("ALSA: Failed to set periodsize %li", periodsize);
279 /* Apply all that setup work.. */
280 if (snd_pcm_hw_params(pcm_handle, hw_params) < 0) {
281 tX_error("ALSA: Failed to apply settings to PCM device \"%s\"", pcm_name);
282 snd_pcm_hw_params_free (hw_params);
286 tX_debug("ALSA: samples_per_buffer: %i, period=%i", samples_per_buffer, periodsize);
288 snd_pcm_hw_params_free (hw_params);
292 int tX_audiodevice_alsa :: close()
294 snd_pcm_close(pcm_handle);
299 double tX_audiodevice_alsa :: get_latency()
305 tX_audiodevice_alsa :: tX_audiodevice_alsa() : tX_audiodevice(),
308 void tX_audiodevice_alsa :: play(int16_t *buffer)
310 snd_pcm_sframes_t pcmreturn;
311 #ifdef BIG_ENDIAN_MACHINE
312 swapbuffer (buffer, samples_per_buffer);
315 pcmreturn = snd_pcm_writei(pcm_handle, buffer, samples_per_buffer/2);
317 while (pcmreturn==-EPIPE) {
318 snd_pcm_prepare(pcm_handle);
319 pcmreturn=snd_pcm_writei(pcm_handle, buffer, samples_per_buffer/2);
320 //tX_warning("ALSA: ** buffer underrun **");
324 printf("snd_pcm_writei says: %s.\n", strerror(-1*pcmreturn));