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);
147 globals.true_block_size=samples_per_buffer/2;
149 tX_debug("tX_adudiodevice_oss::open() - blocksize: %i, samples_per_buffer: %i", blocksize, samples_per_buffer);
151 ioctl(fd, SNDCTL_DSP_SYNC, 0);
156 int tX_audiodevice_oss :: close()
169 tX_audiodevice_oss :: tX_audiodevice_oss() : tX_audiodevice(),
170 fd(0), blocksize(0) {}
172 double tX_audiodevice_oss :: get_latency()
177 void tX_audiodevice_oss :: play(int16_t *buffer)
179 #ifdef BIG_ENDIAN_MACHINE
180 swapbuffer (buffer, samples_per_buffer);
182 int res=write(fd, buffer, blocksize);
184 tX_error("failed to write to audiodevice: %s", strerror(errno));
193 int tX_audiodevice_alsa :: open()
195 snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
196 snd_pcm_hw_params_t *hw_params;
200 snd_pcm_hw_params_alloca(&hw_params);
205 sscanf(globals.alsa_device, "%i-%i: %s", &card, &device, foo);
206 sprintf(pcm_name, "hw:%i,%i", card, device);
208 if (snd_pcm_open(&pcm_handle, pcm_name, stream, 0) < 0) {
209 tX_error("ALSA: Failed to access PCM device \"%s\"", pcm_name);
210 snd_pcm_hw_params_free (hw_params);
214 if (snd_pcm_hw_params_any(pcm_handle, hw_params) < 0) {
215 tX_error("ALSA: Failed to configure PCM device \"%s\"", pcm_name);
216 snd_pcm_hw_params_free (hw_params);
220 /* Setting INTERLEAVED stereo... */
221 if (snd_pcm_hw_params_set_access(pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
222 tX_error("ALSA: Failed to set interleaved access for PCM device \"%s\"", pcm_name);
223 snd_pcm_hw_params_free (hw_params);
227 /* Make it 16 Bit LE - we handle converting from BE anyway... */
228 if (snd_pcm_hw_params_set_format(pcm_handle, hw_params, SND_PCM_FORMAT_S16_LE) < 0) {
229 tX_error("ALSA: Error setting 16 Bit sample width for PCM device \"%s\"", pcm_name);
230 snd_pcm_hw_params_free (hw_params);
234 /* Setting sampling rate */
235 unsigned int hw_rate=(unsigned int)globals.alsa_samplerate;
238 int res = snd_pcm_hw_params_set_rate_near(pcm_handle, hw_params, &hw_rate, &dir);
241 tX_warning("ALSA: The PCM device \"%s\" doesnt support 44100 Hz playback - using %i instead", pcm_name, hw_rate);
246 /* Using stereo output */
247 if (snd_pcm_hw_params_set_channels(pcm_handle, hw_params, 2) < 0) {
248 tX_error("ALSA: PCM device \"%s\" does not support stereo operation", pcm_name);
249 snd_pcm_hw_params_free (hw_params);
253 /* Setting the number of buffers... */
254 /* if (snd_pcm_hw_params_set_periods(pcm_handle, hw_params, globals.alsa_buff_no, 0) < 0) {
255 tX_error("ALSA: Failed to set %i periods for PCM device \"%s\"", globals.alsa_buff_no, pcm_name);
256 snd_pcm_hw_params_free (hw_params);
260 /* Setting the buffersize - ALSA cripples my mind, really... */
261 long unsigned int samples;
262 long unsigned int periodsize;
264 periodsize = globals.alsa_buff_size * 2;
266 samples = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hw_params, &periodsize);
268 tX_error("ALSA: Failed to set buffersize %li", globals.alsa_buff_size);
272 samples_per_buffer=periodsize;//hw_buffsize/sizeof(int16_t);
275 if (snd_pcm_hw_params_set_period_size_near(pcm_handle, hw_params, &periodsize, 0) < 0) {
276 tX_error("ALSA: Failed to set periodsize %li", periodsize);
280 globals.true_block_size=periodsize;
282 /* Apply all that setup work.. */
283 if (snd_pcm_hw_params(pcm_handle, hw_params) < 0) {
284 tX_error("ALSA: Failed to apply settings to PCM device \"%s\"", pcm_name);
285 snd_pcm_hw_params_free (hw_params);
289 tX_debug("ALSA: samples_per_buffer: %i, bs: %i, period=%i", samples_per_buffer, globals.true_block_size, periodsize);
291 snd_pcm_hw_params_free (hw_params);
295 int tX_audiodevice_alsa :: close()
297 snd_pcm_close(pcm_handle);
302 double tX_audiodevice_alsa :: get_latency()
308 tX_audiodevice_alsa :: tX_audiodevice_alsa() : tX_audiodevice(),
311 void tX_audiodevice_alsa :: play(int16_t *buffer)
313 snd_pcm_sframes_t pcmreturn;
314 #ifdef BIG_ENDIAN_MACHINE
315 swapbuffer (buffer, samples_per_buffer);
318 pcmreturn = snd_pcm_writei(pcm_handle, buffer, samples_per_buffer/2);
320 while (pcmreturn==-EPIPE) {
321 snd_pcm_prepare(pcm_handle);
322 pcmreturn=snd_pcm_writei(pcm_handle, buffer, samples_per_buffer/2);
323 //tX_warning("ALSA: ** buffer underrun **");
327 printf("snd_pcm_writei says: %s.\n", strerror(-1*pcmreturn));