2 terminatorX - realtime audio scratching software
3 Copyright (C) 1999-2004 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()
175 tX_audiodevice_oss :: tX_audiodevice_oss() : tX_audiodevice(),
176 fd(0), blocksize(0) {}
178 void tX_audiodevice_oss :: play(int16_t *buffer)
180 #ifdef BIG_ENDIAN_MACHINE
181 swapbuffer (buffer, samples_per_buffer);
183 int res=write(fd, buffer, blocksize);
185 tX_error("failed to write to audiodevice: %s", strerror(errno));
194 int tX_audiodevice_alsa :: open()
196 snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
197 snd_pcm_hw_params_t *hw_params;
200 /* Removing the device ID comment... */
201 for (unsigned int i=0; i<strlen(globals.alsa_device_id); i++) {
202 if (globals.alsa_device_id[i]!='#') {
203 pcm_name[i]=globals.alsa_device_id[i];
210 if (snd_pcm_open(&pcm_handle, pcm_name, stream, 0) < 0) {
211 tX_error("ALSA: Failed to access PCM device \"%s\"", pcm_name);
217 snd_pcm_hw_params_alloca(&hw_params);
219 if (snd_pcm_hw_params_any(pcm_handle, hw_params) < 0) {
220 tX_error("ALSA: Failed to configure PCM device \"%s\"", pcm_name);
221 snd_pcm_hw_params_free (hw_params);
225 /* Setting INTERLEAVED stereo... */
226 #ifdef USE_ALSA_MEMCPY
227 if (snd_pcm_hw_params_set_access(pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
229 if (snd_pcm_hw_params_set_access(pcm_handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0) {
231 tX_error("ALSA: Failed to set interleaved access for PCM device \"%s\"", pcm_name);
232 snd_pcm_hw_params_free (hw_params);
236 /* Make it 16 Bit LE - we handle converting from BE anyway... */
237 if (snd_pcm_hw_params_set_format(pcm_handle, hw_params, SND_PCM_FORMAT_S16_LE) < 0) {
238 tX_error("ALSA: Error setting 16 Bit sample width for PCM device \"%s\"", pcm_name);
239 snd_pcm_hw_params_free (hw_params);
243 /* Setting sampling rate */
244 unsigned int hw_rate=(unsigned int)globals.alsa_samplerate;
247 if (snd_pcm_hw_params_set_rate_near(pcm_handle, hw_params, &hw_rate, &dir) < 0) {
248 tX_error("ALSA: Failed setting sample rate: %i", globals.alsa_samplerate);
249 snd_pcm_hw_params_free (hw_params);
254 tX_warning("ALSA: The PCM device \"%s\" doesnt support 44100 Hz playback - using %i instead", pcm_name, hw_rate);
259 /* Using stereo output */
260 if (snd_pcm_hw_params_set_channels(pcm_handle, hw_params, 2) < 0) {
261 tX_error("ALSA: PCM device \"%s\" does not support stereo operation", pcm_name);
262 snd_pcm_hw_params_free (hw_params);
266 unsigned int buffer_time=globals.alsa_buffer_time;
267 unsigned int period_time=globals.alsa_period_time;
269 if (snd_pcm_hw_params_set_buffer_time_near(pcm_handle, hw_params, &buffer_time, &dir) < 0) {
270 tX_error("ALSA: failed to set the buffer time opf %i usecs", globals.alsa_buffer_time);
274 long unsigned int buffer_size;
276 if (snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_size) < 0) {
277 tX_error("ALSA: failed to retreive buffer size");
281 tX_debug("ALSA: buffer size is %lu", buffer_size);
283 if (snd_pcm_hw_params_set_period_time_near(pcm_handle, hw_params, &period_time, &dir) < 0) {
284 tX_error("ALSA: failed to set period time %i", globals.alsa_period_time);
288 if (snd_pcm_hw_params_get_period_size(hw_params, &period_size, &dir)<0) {
289 tX_error("ALSA: failed to retreive period size");
293 samples_per_buffer=period_size;
295 /* Apply all that setup work.. */
296 if (snd_pcm_hw_params(pcm_handle, hw_params) < 0) {
297 tX_error("ALSA: Failed to apply settings to PCM device \"%s\"", pcm_name);
298 snd_pcm_hw_params_free (hw_params);
302 if (globals.alsa_free_hwstats) {
303 snd_pcm_hw_params_free (hw_params);
306 return 0; //snd_pcm_prepare(pcm_handle);
309 int tX_audiodevice_alsa :: close()
312 snd_pcm_close(pcm_handle);
319 tX_audiodevice_alsa :: tX_audiodevice_alsa() : tX_audiodevice(),
322 void tX_audiodevice_alsa :: play(int16_t *buffer)
325 snd_pcm_sframes_t pcmreturn;
326 #ifdef BIG_ENDIAN_MACHINE
327 swapbuffer (buffer, samples_per_buffer);
330 #ifdef USE_ALSA_MEMCPY
331 pcmreturn = snd_pcm_writei(pcm_handle, buffer, samples_per_buffer >> 1);
333 pcmreturn = snd_pcm_mmap_writei(pcm_handle, buffer, samples_per_buffer >> 1);
336 while (pcmreturn==-EPIPE) {
337 snd_pcm_prepare(pcm_handle);
339 #ifdef USE_ALSA_MEMCPY
340 pcmreturn = snd_pcm_writei(pcm_handle, buffer, samples_per_buffer >> 1);
342 pcmreturn = snd_pcm_mmap_writei(pcm_handle, buffer, samples_per_buffer >> 1);
345 if (underrun_ctr>100) {
346 tX_error("tX_audiodevice_alsa::play() more than 10 EPIPE cycles. Giving up.");
349 //tX_warning("ALSA: ** buffer underrun **");
353 printf("snd_pcm_writei says: %s.\n", strerror(-1*pcmreturn));
361 tX_jack_client* tX_jack_client::instance=NULL;
363 void tX_jack_client::init()
365 tX_jack_client *test=new tX_jack_client();
372 tX_jack_client::tX_jack_client():device(NULL),jack_shutdown(false)
374 jack_set_error_function(tX_jack_client::error);
376 if ((client=jack_client_new("terminatorX"))==0) {
377 tX_error("tX_jack_client() -> failed to connect to jackd.");
383 /* Setting up jack callbacks... */
384 jack_set_process_callback(client, tX_jack_client::process, NULL);
385 jack_set_sample_rate_callback(client, tX_jack_client::srate, NULL);
386 jack_on_shutdown (client, tX_jack_client::shutdown, NULL);
388 /* Creating the port... */
389 left_port = jack_port_register (client, "output_1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
390 right_port = jack_port_register (client, "output_2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
393 jack_activate(client);
395 /* Connect some ports... */
396 if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
397 tX_error("tX_jack_client() no ports to connect to found. Connect manually.");
398 } else if (ports[0] && ports[1]) {
399 if (jack_connect (client, jack_port_name(left_port), ports[0])) {
400 tX_error("tX_jack_client() failed to connect left port.");
402 if (jack_connect (client, jack_port_name(right_port), ports[1])) {
403 tX_error("tX_jack_client() failed to connect right port.");
410 tX_jack_client::~tX_jack_client()
413 if (client) jack_client_close(client);
416 void tX_jack_client::error(const char *desc)
418 tX_error("jack error: %s.", desc);
421 int tX_jack_client::srate(jack_nframes_t nframes, void *arg)
423 tX_error("tX_jack_client::srate() jack changed samplerate - ignored.");
427 void tX_jack_client::shutdown(void *arg)
429 tX_error("tX_jack_client::shutdown() jack daemon has shut down. Bad!");
430 if (instance) instance->jack_shutdown=true;
433 int tX_jack_client::process(jack_nframes_t nframes, void *arg)
436 return instance->play(nframes);
439 /* Hmm, what to do in such a case? */
443 int tX_jack_client::play(jack_nframes_t nframes)
445 jack_default_audio_sample_t *left = (jack_default_audio_sample_t *) jack_port_get_buffer (left_port, nframes);
446 jack_default_audio_sample_t *right = (jack_default_audio_sample_t *) jack_port_get_buffer (right_port, nframes);
449 device->fill_frames(left, right, nframes);
451 memset(left, 0, sizeof (jack_default_audio_sample_t) * nframes);
452 memset(right, 0, sizeof (jack_default_audio_sample_t) * nframes);
458 int tX_jack_client::get_sample_rate()
460 return jack_get_sample_rate(client);
463 /* tX_audiodevice_jack */
465 tX_audiodevice_jack::tX_audiodevice_jack():tX_audiodevice()
470 int tX_audiodevice_jack::open()
472 tX_jack_client *jack_client=tX_jack_client::get_instance();
475 sample_rate=jack_client->get_sample_rate();
485 int tX_audiodevice_jack::close()
488 client->set_device(NULL);
496 void tX_audiodevice_jack::play(int16_t *buffer)
498 tX_error("tX_audiodevice_jack::play()");
501 void tX_audiodevice_jack::start()
503 overrun_buffer=new f_prec[vtt_class::samples_in_mix_buffer];
505 client->set_device(this);
506 while ((!engine->is_stopped()) && !(client->get_jack_shutdown())) {
509 client->set_device(NULL);
511 delete [] overrun_buffer;
514 void tX_audiodevice_jack::fill_frames(jack_default_audio_sample_t *left, jack_default_audio_sample_t *right, jack_nframes_t nframes)
516 unsigned int outbuffer_pos=0;
519 if (samples_in_overrun_buffer) {
520 for (sample=0; ((sample<samples_in_overrun_buffer) && (outbuffer_pos<nframes));) {
521 left[outbuffer_pos]=overrun_buffer[sample++]/32767.0;
522 right[outbuffer_pos++]=overrun_buffer[sample++]/32767.0;
526 while (outbuffer_pos<nframes) {
527 engine->render_cycle();
529 for (sample=0; ((sample<(unsigned int) vtt_class::samples_in_mix_buffer) && (outbuffer_pos<nframes));) {
530 left[outbuffer_pos]=vtt_class::mix_buffer[sample++]/32767.0;
531 right[outbuffer_pos++]=vtt_class::mix_buffer[sample++]/32767.0;
534 if (sample<(unsigned int) vtt_class::samples_in_mix_buffer) {
535 samples_in_overrun_buffer=vtt_class::samples_in_mix_buffer-sample;
536 /* There's more data in the mixbuffer... */
537 memcpy(overrun_buffer, &vtt_class::mix_buffer[sample], sizeof(f_prec) * samples_in_overrun_buffer);
539 samples_in_overrun_buffer=0;