diff options
Diffstat (limited to 'gr-audio')
-rw-r--r-- | gr-audio/lib/Makefile.am | 95 | ||||
-rw-r--r-- | gr-audio/lib/jack/audio_jack_sink.cc | 236 | ||||
-rw-r--r-- | gr-audio/lib/jack/audio_jack_sink.h | 79 | ||||
-rw-r--r-- | gr-audio/lib/jack/audio_jack_source.cc | 237 | ||||
-rw-r--r-- | gr-audio/lib/jack/audio_jack_source.h | 79 | ||||
-rw-r--r-- | gr-audio/lib/jack/gr-audio-jack.conf | 8 | ||||
-rw-r--r-- | gr-audio/lib/jack/gri_jack.cc | 30 | ||||
-rw-r--r-- | gr-audio/lib/jack/gri_jack.h | 28 | ||||
-rw-r--r-- | gr-audio/lib/portaudio/audio_portaudio_sink.cc | 362 | ||||
-rw-r--r-- | gr-audio/lib/portaudio/audio_portaudio_sink.h | 85 | ||||
-rw-r--r-- | gr-audio/lib/portaudio/audio_portaudio_source.cc | 374 | ||||
-rw-r--r-- | gr-audio/lib/portaudio/audio_portaudio_source.h | 83 | ||||
-rw-r--r-- | gr-audio/lib/portaudio/gr-audio-portaudio.conf | 10 | ||||
-rw-r--r-- | gr-audio/lib/portaudio/gri_portaudio.cc | 111 | ||||
-rw-r--r-- | gr-audio/lib/portaudio/gri_portaudio.h | 32 |
15 files changed, 1847 insertions, 2 deletions
diff --git a/gr-audio/lib/Makefile.am b/gr-audio/lib/Makefile.am index 6cbaf2679..9972efadc 100644 --- a/gr-audio/lib/Makefile.am +++ b/gr-audio/lib/Makefile.am @@ -73,8 +73,7 @@ endif if GR_AUDIO_OSS_SUPPORT AM_CPPFLAGS += \ - -I$(srcdir)/oss \ - $(OSS_CPPFLAGS) + -I$(srcdir)/oss libgnuradio_audio_la_LIBADD += $(OSS_LIBS) @@ -89,3 +88,95 @@ noinst_HEADERS += \ dist_etc_DATA += oss/gr-audio-oss.conf endif + +######################################################################## +## Jack Support +######################################################################## +if GR_AUDIO_JACK_SUPPORT + +AM_CPPFLAGS += \ + -I$(srcdir)/jack \ + $(JACK_CPPFLAGS) + +libgnuradio_audio_la_LIBADD += $(JACK_LIBS) + +libgnuradio_audio_la_SOURCES += \ + jack/gri_jack.cc \ + jack/audio_jack_source.cc \ + jack/audio_jack_sink.cc + +noinst_HEADERS += \ + jack/gri_jack.h \ + jack/audio_jack_source.h \ + jack/audio_jack_sink.h + +dist_etc_DATA += jack/gr-audio-jack.conf + +endif + +######################################################################## +## OSX Support +######################################################################## +if GR_AUDIO_OSX_SUPPORT + +AM_CPPFLAGS += \ + -I$(srcdir)/osx + +libgnuradio_audio_la_SOURCES += \ + osx/audio_osx_source.cc \ + osx/audio_osx_sink.cc + +noinst_HEADERS += \ + osx/audio_osx_source.h \ + osx/audio_osx_sink.h + +dist_etc_DATA += osx/gr-audio-osx.conf + +endif + +######################################################################## +## PortAudio Support +######################################################################## +if GR_AUDIO_PORTAUDIO_SUPPORT + +AM_CPPFLAGS += \ + -I$(srcdir)/portaudio \ + $(PORTAUDIO_CPPFLAGS) + +libgnuradio_audio_la_LIBADD += $(PORTAUDIO_LIBS) + +libgnuradio_audio_la_SOURCES += \ + portaudio/gri_portaudio.cc \ + portaudio/audio_portaudio_source.cc \ + portaudio/audio_portaudio_sink.cc + +noinst_HEADERS += \ + portaudio/gri_portaudio.h \ + portaudio/audio_portaudio_source.h \ + portaudio/audio_portaudio_sink.h + +dist_etc_DATA += portaudio/gr-audio-portaudio.conf + +endif + +######################################################################## +## Windows Support +######################################################################## +if GR_AUDIO_WINDOWS_SUPPORT + +AM_CPPFLAGS += \ + -I$(srcdir)/windows + +libgnuradio_audio_la_LIBADD += $(WINAUDIO_LIBS) + +libgnuradio_audio_la_SOURCES += \ + windows/audio_windows_source.cc \ + windows/audio_windows_sink.cc + +noinst_HEADERS += \ + windows/audio_windows_source.h \ + windows/audio_windows_sink.h + +dist_etc_DATA += windows/gr-audio-windows.conf + +endif diff --git a/gr-audio/lib/jack/audio_jack_sink.cc b/gr-audio/lib/jack/audio_jack_sink.cc new file mode 100644 index 000000000..db365a1f8 --- /dev/null +++ b/gr-audio/lib/jack/audio_jack_sink.cc @@ -0,0 +1,236 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005-2011 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gr_audio_registry.h" +#include <audio_jack_sink.h> +#include <gr_io_signature.h> +#include <gr_prefs.h> +#include <stdio.h> +#include <iostream> +#include <stdexcept> +#include <gri_jack.h> + +#ifndef NO_PTHREAD +#include <pthread.h> +#endif + +AUDIO_REGISTER_SINK(REG_PRIO_MED, jack)( + int sampling_rate, const std::string &device_name, bool ok_to_block +){ + return audio_sink::sptr(new audio_jack_sink(sampling_rate, device_name, ok_to_block)); +} + +typedef jack_default_audio_sample_t sample_t; + + +// Number of jack buffers in the ringbuffer +// TODO: make it to match at least the quantity of items passed by work() +static const unsigned int N_BUFFERS = 16; + +static std::string +default_device_name () +{ + return gr_prefs::singleton()->get_string("audio_jack", "default_output_device", "gr_sink"); +} + +int +jack_sink_process (jack_nframes_t nframes, void *arg) +{ + audio_jack_sink *self = (audio_jack_sink *)arg; + unsigned int read_size = nframes*sizeof(sample_t); + + if (jack_ringbuffer_read_space (self->d_ringbuffer) < read_size) { + self->d_nunderuns++; + // FIXME: move this fputs out, we shouldn't use blocking calls in process() + fputs ("jU", stderr); + return 0; + } + + char *buffer = (char *) jack_port_get_buffer (self->d_jack_output_port, nframes); + + jack_ringbuffer_read (self->d_ringbuffer, buffer, read_size); + +#ifndef NO_PTHREAD + // Tell the sink thread there is room in the ringbuffer. + // If it is already running, the lock will not be available. + // We can't wait here in the process() thread, but we don't + // need to signal in that case, because the sink thread will + // check for room availability. + + if (pthread_mutex_trylock (&self->d_jack_process_lock) == 0) { + pthread_cond_signal (&self->d_ringbuffer_ready); + pthread_mutex_unlock (&self->d_jack_process_lock); + } +#endif + + return 0; +} + +// ---------------------------------------------------------------- + +audio_jack_sink::audio_jack_sink (int sampling_rate, + const std::string device_name, + bool ok_to_block) + : audio_sink ("audio_jack_sink", + gr_make_io_signature (0, 0, 0), + gr_make_io_signature (0, 0, 0)), + d_sampling_rate (sampling_rate), + d_device_name (device_name.empty() ? default_device_name() : device_name), + d_ok_to_block (ok_to_block), + d_jack_client (0), + d_ringbuffer (0), + d_nunderuns (0) +{ +#ifndef NO_PTHREAD + pthread_cond_init(&d_ringbuffer_ready, NULL);; + pthread_mutex_init(&d_jack_process_lock, NULL); +#endif + + // try to become a client of the JACK server + jack_options_t options = JackNullOption; + jack_status_t status; + const char *server_name = NULL; + if ((d_jack_client = jack_client_open (d_device_name.c_str (), + options, &status, + server_name)) == NULL) { + fprintf (stderr, "audio_jack_sink[%s]: jack server not running?\n", + d_device_name.c_str()); + throw std::runtime_error ("audio_jack_sink"); + } + + // tell the JACK server to call `jack_sink_process()' whenever + // there is work to be done. + jack_set_process_callback (d_jack_client, &jack_sink_process, (void*)this); + + // tell the JACK server to call `jack_shutdown()' if + // it ever shuts down, either entirely, or if it + // just decides to stop calling us. + + //jack_on_shutdown (d_jack_client, &jack_shutdown, (void*)this); + + d_jack_output_port = + jack_port_register (d_jack_client, "out", + JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); + + + d_jack_buffer_size = jack_get_buffer_size (d_jack_client); + + set_output_multiple (d_jack_buffer_size); + + d_ringbuffer = + jack_ringbuffer_create (N_BUFFERS*d_jack_buffer_size*sizeof(sample_t)); + if (d_ringbuffer == NULL) + bail ("jack_ringbuffer_create failed", 0); + + assert(sizeof(float)==sizeof(sample_t)); + set_input_signature (gr_make_io_signature (1, 1, sizeof (sample_t))); + + + jack_nframes_t sample_rate = jack_get_sample_rate (d_jack_client); + + if ((jack_nframes_t)sampling_rate != sample_rate){ + fprintf (stderr, "audio_jack_sink[%s]: unable to support sampling rate %d\n", + d_device_name.c_str (), sampling_rate); + fprintf (stderr, " card requested %d instead.\n", sample_rate); + } +} + + +bool +audio_jack_sink::check_topology (int ninputs, int noutputs) +{ + if (ninputs != 1) + return false; + + // tell the JACK server that we are ready to roll + if (jack_activate (d_jack_client)) + throw std::runtime_error ("audio_jack_sink"); + + return true; +} + +audio_jack_sink::~audio_jack_sink () +{ + jack_client_close (d_jack_client); + jack_ringbuffer_free (d_ringbuffer); +} + +int +audio_jack_sink::work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + // write_size and work_size are in bytes + int work_size = noutput_items*sizeof(sample_t); + unsigned int write_size; + + while (work_size > 0) { + unsigned int write_space; // bytes + +#ifdef NO_PTHREAD + while ((write_space=jack_ringbuffer_write_space (d_ringbuffer)) < + d_jack_buffer_size*sizeof(sample_t)) { + usleep(1000000*((d_jack_buffer_size-write_space/sizeof(sample_t))/d_sampling_rate)); + } +#else + // JACK actually requires POSIX + + pthread_mutex_lock (&d_jack_process_lock); + while ((write_space=jack_ringbuffer_write_space (d_ringbuffer)) < + d_jack_buffer_size*sizeof(sample_t)) { + + // wait until jack_sink_process() signals more room + pthread_cond_wait (&d_ringbuffer_ready, &d_jack_process_lock); + } + pthread_mutex_unlock (&d_jack_process_lock); +#endif + + write_space -= write_space%(d_jack_buffer_size*sizeof(sample_t)); + write_size = std::min(write_space, (unsigned int)work_size); + + if (jack_ringbuffer_write (d_ringbuffer, (char *) input_items[0], + write_size) < write_size) { + bail ("jack_ringbuffer_write failed", 0); + } + work_size -= write_size; + } + + return noutput_items; +} + +void +audio_jack_sink::output_error_msg (const char *msg, int err) +{ + fprintf (stderr, "audio_jack_sink[%s]: %s: %d\n", + d_device_name.c_str (), msg, err); +} + +void +audio_jack_sink::bail (const char *msg, int err) throw (std::runtime_error) +{ + output_error_msg (msg, err); + throw std::runtime_error ("audio_jack_sink"); +} diff --git a/gr-audio/lib/jack/audio_jack_sink.h b/gr-audio/lib/jack/audio_jack_sink.h new file mode 100644 index 000000000..a11863ee0 --- /dev/null +++ b/gr-audio/lib/jack/audio_jack_sink.h @@ -0,0 +1,79 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005-2011 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ +#ifndef INCLUDED_AUDIO_JACK_SINK_H +#define INCLUDED_AUDIO_JACK_SINK_H + +#include <gr_audio_sink.h> +#include <string> +#include <jack/jack.h> +#include <jack/ringbuffer.h> +#include <stdexcept> + +int jack_sink_process (jack_nframes_t nframes, void *arg); + +/*! + * \brief audio sink using JACK + * + * The sink has one input stream of floats. + * + * Input samples must be in the range [-1,1]. + */ +class audio_jack_sink : public audio_sink { + + friend int jack_sink_process (jack_nframes_t nframes, void *arg); + + // typedef for pointer to class work method + typedef int (audio_jack_sink::*work_t)(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + + unsigned int d_sampling_rate; + std::string d_device_name; + bool d_ok_to_block; + + jack_client_t *d_jack_client; + jack_port_t *d_jack_output_port; + jack_ringbuffer_t *d_ringbuffer; + jack_nframes_t d_jack_buffer_size; + pthread_cond_t d_ringbuffer_ready; + pthread_mutex_t d_jack_process_lock; + + // random stats + int d_nunderuns; // count of underruns + + void output_error_msg (const char *msg, int err); + void bail (const char *msg, int err) throw (std::runtime_error); + + +public: + audio_jack_sink (int sampling_rate, const std::string device_name, bool ok_to_block); + + ~audio_jack_sink (); + + bool check_topology (int ninputs, int noutputs); + + int work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; + +#endif /* INCLUDED_AUDIO_JACK_SINK_H */ diff --git a/gr-audio/lib/jack/audio_jack_source.cc b/gr-audio/lib/jack/audio_jack_source.cc new file mode 100644 index 000000000..415c7f22b --- /dev/null +++ b/gr-audio/lib/jack/audio_jack_source.cc @@ -0,0 +1,237 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005,2006,2010 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gr_audio_registry.h" +#include <audio_jack_source.h> +#include <gr_io_signature.h> +#include <gr_prefs.h> +#include <stdio.h> +#include <iostream> +#include <stdexcept> +#include <gri_jack.h> + +#ifndef NO_PTHREAD +#include <pthread.h> +#endif + +AUDIO_REGISTER_SOURCE(REG_PRIO_MED, jack)( + int sampling_rate, const std::string &device_name, bool ok_to_block +){ + return audio_source::sptr(new audio_jack_source(sampling_rate, device_name, ok_to_block)); +} + +typedef jack_default_audio_sample_t sample_t; + + +// Number of jack buffers in the ringbuffer +// TODO: make it to match at least the quantity of items passed to work() +static const unsigned int N_BUFFERS = 16; + +static std::string +default_device_name () +{ + return gr_prefs::singleton()->get_string("audio_jack", "default_input_device", "gr_source"); +} + + +int +jack_source_process (jack_nframes_t nframes, void *arg) +{ + audio_jack_source *self = (audio_jack_source *)arg; + unsigned int write_size = nframes*sizeof(sample_t); + + if (jack_ringbuffer_write_space (self->d_ringbuffer) < write_size) { + self->d_noverruns++; + // FIXME: move this fputs out, we shouldn't use blocking calls in process() + fputs ("jO", stderr); + return 0; + } + + char *buffer = (char *) jack_port_get_buffer (self->d_jack_input_port, nframes); + + jack_ringbuffer_write (self->d_ringbuffer, buffer, write_size); + +#ifndef NO_PTHREAD + // Tell the source thread there is data in the ringbuffer. + // If it is already running, the lock will not be available. + // We can't wait here in the process() thread, but we don't + // need to signal in that case, because the source thread will + // check for data availability. + + if (pthread_mutex_trylock (&self->d_jack_process_lock) == 0) { + pthread_cond_signal (&self->d_ringbuffer_ready); + pthread_mutex_unlock (&self->d_jack_process_lock); + } +#endif + + return 0; +} + +// ---------------------------------------------------------------- + +audio_jack_source::audio_jack_source (int sampling_rate, + const std::string device_name, + bool ok_to_block) + : audio_source ("audio_jack_source", + gr_make_io_signature (0, 0, 0), + gr_make_io_signature (0, 0, 0)), + d_sampling_rate (sampling_rate), + d_device_name (device_name.empty() ? default_device_name() : device_name), + d_ok_to_block(ok_to_block), + d_jack_client (0), + d_ringbuffer (0), + d_noverruns (0) +{ +#ifndef NO_PTHREAD + pthread_cond_init(&d_ringbuffer_ready, NULL);; + pthread_mutex_init(&d_jack_process_lock, NULL); +#endif + + // try to become a client of the JACK server + jack_options_t options = JackNullOption; + jack_status_t status; + const char *server_name = NULL; + if ((d_jack_client = jack_client_open (d_device_name.c_str (), + options, &status, + server_name)) == NULL) { + fprintf (stderr, "audio_jack_source[%s]: jack server not running?\n", + d_device_name.c_str()); + throw std::runtime_error ("audio_jack_source"); + } + + // tell the JACK server to call `jack_source_process()' whenever + // there is work to be done. + jack_set_process_callback (d_jack_client, &jack_source_process, (void*)this); + + // tell the JACK server to call `jack_shutdown()' if + // it ever shuts down, either entirely, or if it + // just decides to stop calling us. + + //jack_on_shutdown (d_jack_client, &jack_shutdown, (void*)this); + + d_jack_input_port = jack_port_register (d_jack_client, "in", + JACK_DEFAULT_AUDIO_TYPE, + JackPortIsInput, 0); + + + d_jack_buffer_size = jack_get_buffer_size (d_jack_client); + + set_output_multiple (d_jack_buffer_size); + + d_ringbuffer = jack_ringbuffer_create (N_BUFFERS*d_jack_buffer_size*sizeof(sample_t)); + if (d_ringbuffer == NULL) + bail ("jack_ringbuffer_create failed", 0); + + assert(sizeof(float)==sizeof(sample_t)); + set_output_signature (gr_make_io_signature (1, 1, sizeof (sample_t))); + + + jack_nframes_t sample_rate = jack_get_sample_rate (d_jack_client); + + if ((jack_nframes_t)sampling_rate != sample_rate){ + fprintf (stderr, "audio_jack_source[%s]: unable to support sampling rate %d\n", + d_device_name.c_str (), sampling_rate); + fprintf (stderr, " card requested %d instead.\n", sample_rate); + } +} + + +bool +audio_jack_source::check_topology (int ninputs, int noutputs) +{ + // tell the JACK server that we are ready to roll + if (jack_activate (d_jack_client)) + throw std::runtime_error ("audio_jack_source"); + + return true; +} + +audio_jack_source::~audio_jack_source () +{ + jack_client_close (d_jack_client); + jack_ringbuffer_free (d_ringbuffer); +} + +int +audio_jack_source::work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + // read_size and work_size are in bytes + unsigned int read_size; + + // Minimize latency + noutput_items = std::min (noutput_items, (int)d_jack_buffer_size); + + int work_size = noutput_items*sizeof(sample_t); + + while (work_size > 0) { + unsigned int read_space; // bytes + +#ifdef NO_PTHREAD + while ((read_space=jack_ringbuffer_read_space (d_ringbuffer)) < + d_jack_buffer_size*sizeof(sample_t)) { + usleep(1000000*((d_jack_buffer_size-read_space/sizeof(sample_t))/d_sampling_rate)); + } +#else + // JACK actually requires POSIX + + pthread_mutex_lock (&d_jack_process_lock); + while ((read_space=jack_ringbuffer_read_space (d_ringbuffer)) < + d_jack_buffer_size*sizeof(sample_t)) { + + // wait until jack_source_process() signals more data + pthread_cond_wait (&d_ringbuffer_ready, &d_jack_process_lock); + } + pthread_mutex_unlock (&d_jack_process_lock); +#endif + + read_space -= read_space%(d_jack_buffer_size*sizeof(sample_t)); + read_size = std::min(read_space, (unsigned int)work_size); + + if (jack_ringbuffer_read (d_ringbuffer, (char *) output_items[0], + read_size) < read_size) { + bail ("jack_ringbuffer_read failed", 0); + } + work_size -= read_size; + } + + return noutput_items; +} + +void +audio_jack_source::output_error_msg (const char *msg, int err) +{ + fprintf (stderr, "audio_jack_source[%s]: %s: %d\n", + d_device_name.c_str (), msg, err); +} + +void +audio_jack_source::bail (const char *msg, int err) throw (std::runtime_error) +{ + output_error_msg (msg, err); + throw std::runtime_error ("audio_jack_source"); +} diff --git a/gr-audio/lib/jack/audio_jack_source.h b/gr-audio/lib/jack/audio_jack_source.h new file mode 100644 index 000000000..858f34528 --- /dev/null +++ b/gr-audio/lib/jack/audio_jack_source.h @@ -0,0 +1,79 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005-2011 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ +#ifndef INCLUDED_AUDIO_JACK_SOURCE_H +#define INCLUDED_AUDIO_JACK_SOURCE_H + +#include <gr_audio_source.h> +#include <string> +#include <jack/jack.h> +#include <jack/ringbuffer.h> +#include <stdexcept> + +int jack_source_process (jack_nframes_t nframes, void *arg); + +/*! + * \brief audio source using JACK + * + * The source has one input stream of floats. + * + * Output samples will be in the range [-1,1]. + */ +class audio_jack_source : public audio_source { + + friend int jack_source_process (jack_nframes_t nframes, void *arg); + + // typedef for pointer to class work method + typedef int (audio_jack_source::*work_t)(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + + unsigned int d_sampling_rate; + std::string d_device_name; + bool d_ok_to_block; + + jack_client_t *d_jack_client; + jack_port_t *d_jack_input_port; + jack_ringbuffer_t *d_ringbuffer; + jack_nframes_t d_jack_buffer_size; + pthread_cond_t d_ringbuffer_ready; + pthread_mutex_t d_jack_process_lock; + + // random stats + int d_noverruns; // count of overruns + + void output_error_msg (const char *msg, int err); + void bail (const char *msg, int err) throw (std::runtime_error); + + +public: + audio_jack_source (int sampling_rate, const std::string device_name, bool ok_to_block); + + ~audio_jack_source (); + + bool check_topology (int ninputs, int noutputs); + + int work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; + +#endif /* INCLUDED_AUDIO_JACK_SOURCE_H */ diff --git a/gr-audio/lib/jack/gr-audio-jack.conf b/gr-audio/lib/jack/gr-audio-jack.conf new file mode 100644 index 000000000..bdbc1fd1d --- /dev/null +++ b/gr-audio/lib/jack/gr-audio-jack.conf @@ -0,0 +1,8 @@ +# This file contains system wide configuration data for GNU Radio. +# You may override any setting on a per-user basis by editing +# ~/.gnuradio/config.conf + +[audio_jack] + +default_input_device = gr_source +default_output_device = gr_sink diff --git a/gr-audio/lib/jack/gri_jack.cc b/gr-audio/lib/jack/gri_jack.cc new file mode 100644 index 000000000..fef1c58a6 --- /dev/null +++ b/gr-audio/lib/jack/gri_jack.cc @@ -0,0 +1,30 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gri_jack.h> +#include <algorithm> + + diff --git a/gr-audio/lib/jack/gri_jack.h b/gr-audio/lib/jack/gri_jack.h new file mode 100644 index 000000000..ddc0b744d --- /dev/null +++ b/gr-audio/lib/jack/gri_jack.h @@ -0,0 +1,28 @@ +/* -*- c++ -*- */ +/* + * Copyright 2005 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GRI_JACK_H +#define INCLUDED_GRI_JACK_H + +#include <stdio.h> + +#endif /* INCLUDED_GRI_JACK_H */ diff --git a/gr-audio/lib/portaudio/audio_portaudio_sink.cc b/gr-audio/lib/portaudio/audio_portaudio_sink.cc new file mode 100644 index 000000000..515cd04d9 --- /dev/null +++ b/gr-audio/lib/portaudio/audio_portaudio_sink.cc @@ -0,0 +1,362 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006-2011 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in he hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gr_audio_registry.h" +#include <audio_portaudio_sink.h> +#include <gr_io_signature.h> +#include <gr_prefs.h> +#include <stdio.h> +#include <iostream> +#include <unistd.h> +#include <stdexcept> +#include <gri_portaudio.h> +#include <string.h> + +AUDIO_REGISTER_SINK(REG_PRIO_MED, portaudio)( + int sampling_rate, const std::string &device_name, bool ok_to_block +){ + return audio_sink::sptr(new audio_portaudio_sink(sampling_rate, device_name, ok_to_block)); +} + +//#define LOGGING 0 // define to 0 or 1 + +#define SAMPLE_FORMAT paFloat32 +typedef float sample_t; + +// Number of portaudio buffers in the ringbuffer +static const unsigned int N_BUFFERS = 4; + +static std::string +default_device_name () +{ + return gr_prefs::singleton()->get_string("audio_portaudio", "default_output_device", ""); +} + +void +audio_portaudio_sink::create_ringbuffer(void) +{ + int bufsize_samples = d_portaudio_buffer_size_frames * d_output_parameters.channelCount; + + if (d_verbose) + fprintf(stderr,"ring buffer size = %d frames\n", + N_BUFFERS*bufsize_samples/d_output_parameters.channelCount); + + // FYI, the buffer indicies are in units of samples. + d_writer = gr_make_buffer(N_BUFFERS * bufsize_samples, sizeof(sample_t)); + d_reader = gr_buffer_add_reader(d_writer, 0); +} + +/* + * This routine will be called by the PortAudio engine when audio is needed. + * It may called at interrupt level on some machines so don't do anything + * that could mess up the system like calling malloc() or free(). + * + * Our job is to write framesPerBuffer frames into outputBuffer. + */ +int +portaudio_sink_callback (const void *inputBuffer, + void *outputBuffer, + unsigned long framesPerBuffer, + const PaStreamCallbackTimeInfo* timeInfo, + PaStreamCallbackFlags statusFlags, + void *arg) +{ + audio_portaudio_sink *self = (audio_portaudio_sink *)arg; + int nreqd_samples = + framesPerBuffer * self->d_output_parameters.channelCount; + + int navail_samples = self->d_reader->items_available(); + + if (nreqd_samples <= navail_samples) { // We've got enough data... + { + gruel::scoped_lock guard(self->d_ringbuffer_mutex); + + memcpy(outputBuffer, + self->d_reader->read_pointer(), + nreqd_samples * sizeof(sample_t)); + self->d_reader->update_read_pointer(nreqd_samples); + + self->d_ringbuffer_ready = true; + } + + // Tell the sink thread there is new room in the ringbuffer. + self->d_ringbuffer_cond.notify_one(); + return paContinue; + } + + else { // underrun + self->d_nunderuns++; + ssize_t r = ::write(2, "aU", 2); // FIXME change to non-blocking call + if(r == -1) { + perror("audio_portaudio_source::portaudio_source_callback write error to stderr."); + } + + // FIXME we should transfer what we've got and pad the rest + memset(outputBuffer, 0, nreqd_samples * sizeof(sample_t)); + + self->d_ringbuffer_ready = true; + self->d_ringbuffer_cond.notify_one(); // Tell the sink to get going! + + return paContinue; + } +} + + +// ---------------------------------------------------------------- + +audio_portaudio_sink::audio_portaudio_sink(int sampling_rate, + const std::string device_name, + bool ok_to_block) + : audio_sink ("audio_portaudio_sink", + gr_make_io_signature(0, 0, 0), + gr_make_io_signature(0, 0, 0)), + d_sampling_rate(sampling_rate), + d_device_name(device_name.empty() ? default_device_name() : device_name), + d_ok_to_block(ok_to_block), + d_verbose(gr_prefs::singleton()->get_bool("audio_portaudio", "verbose", false)), + d_portaudio_buffer_size_frames(0), + d_stream(0), + d_ringbuffer_mutex(), + d_ringbuffer_cond(), + d_ringbuffer_ready(false), + d_nunderuns(0) +{ + memset(&d_output_parameters, 0, sizeof(d_output_parameters)); + //if (LOGGING) + // d_log = gri_logger::singleton(); + + PaError err; + int i, numDevices; + PaDeviceIndex device = 0; + const PaDeviceInfo *deviceInfo = NULL; + + err = Pa_Initialize(); + if (err != paNoError) { + bail ("Initialize failed", err); + } + + if (d_verbose) + gri_print_devices(); + + numDevices = Pa_GetDeviceCount(); + if (numDevices < 0) + bail("Pa Device count failed", 0); + if (numDevices == 0) + bail("no devices available", 0); + + if (d_device_name.empty()) + { + // FIXME Get smarter about picking something + fprintf(stderr,"\nUsing Default Device\n"); + device = Pa_GetDefaultOutputDevice(); + deviceInfo = Pa_GetDeviceInfo(device); + fprintf(stderr,"%s is the chosen device using %s as the host\n", + deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name); + } + else + { + bool found = false; + fprintf(stderr,"\nTest Devices\n"); + for (i=0;i<numDevices;i++) { + deviceInfo = Pa_GetDeviceInfo( i ); + fprintf(stderr,"Testing device name: %s",deviceInfo->name); + if (deviceInfo->maxOutputChannels <= 0) { + fprintf(stderr,"\n"); + continue; + } + if (strstr(deviceInfo->name, d_device_name.c_str())){ + fprintf(stderr," Chosen!\n"); + device = i; + fprintf(stderr,"%s using %s as the host\n",d_device_name.c_str(), + Pa_GetHostApiInfo(deviceInfo->hostApi)->name), fflush(stderr); + found = true; + deviceInfo = Pa_GetDeviceInfo(device); + i = numDevices; // force loop exit + } + else + fprintf(stderr,"\n"),fflush(stderr); + } + + if (!found){ + bail("Failed to find specified device name", 0); + exit(1); + } + } + + + d_output_parameters.device = device; + d_output_parameters.channelCount = deviceInfo->maxOutputChannels; + d_output_parameters.sampleFormat = SAMPLE_FORMAT; + d_output_parameters.suggestedLatency = deviceInfo->defaultLowOutputLatency; + d_output_parameters.hostApiSpecificStreamInfo = NULL; + + // We fill in the real channelCount in check_topology when we know + // how many inputs are connected to us. + + // Now that we know the maximum number of channels (allegedly) + // supported by the h/w, we can compute a reasonable input + // signature. The portaudio specs say that they'll accept any + // number of channels from 1 to max. + set_input_signature(gr_make_io_signature(1, deviceInfo->maxOutputChannels, + sizeof (sample_t))); +} + + +bool +audio_portaudio_sink::check_topology (int ninputs, int noutputs) +{ + PaError err; + + if (Pa_IsStreamActive(d_stream)) + { + Pa_CloseStream(d_stream); + d_stream = 0; + d_reader.reset(); // boost::shared_ptr for d_reader = 0 + d_writer.reset(); // boost::shared_ptr for d_write = 0 + } + + d_output_parameters.channelCount = ninputs; // # of channels we're really using + +#if 1 + d_portaudio_buffer_size_frames = (int)(0.0213333333 * d_sampling_rate + 0.5); // Force 1024 frame buffers at 48000 + fprintf(stderr, "Latency = %8.5f, requested sampling_rate = %g\n", // Force latency to 21.3333333.. ms + 0.0213333333, (double)d_sampling_rate); +#endif + err = Pa_OpenStream(&d_stream, + NULL, // No input + &d_output_parameters, + d_sampling_rate, + d_portaudio_buffer_size_frames, + paClipOff, + &portaudio_sink_callback, + (void*)this); + + if (err != paNoError) { + output_error_msg ("OpenStream failed", err); + return false; + } + +#if 0 + const PaStreamInfo *psi = Pa_GetStreamInfo(d_stream); + + d_portaudio_buffer_size_frames = (int)(d_output_parameters.suggestedLatency * psi->sampleRate); + fprintf(stderr, "Latency = %7.4f, psi->sampleRate = %g\n", + d_output_parameters.suggestedLatency, psi->sampleRate); +#endif + + fprintf(stderr, "d_portaudio_buffer_size_frames = %d\n", d_portaudio_buffer_size_frames); + + assert(d_portaudio_buffer_size_frames != 0); + + create_ringbuffer(); + + err = Pa_StartStream(d_stream); + if (err != paNoError) { + output_error_msg ("StartStream failed", err); + return false; + } + + return true; +} + +audio_portaudio_sink::~audio_portaudio_sink () +{ + Pa_StopStream(d_stream); // wait for output to drain + Pa_CloseStream(d_stream); + Pa_Terminate(); +} + +/* + * This version consumes everything sent to it, blocking if required. + * I think this will allow us better control of the total buffering/latency + * in the audio path. + */ +int +audio_portaudio_sink::work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + const float **in = (const float **) &input_items[0]; + const unsigned nchan = d_output_parameters.channelCount; // # of channels == samples/frame + + int k; + + for (k = 0; k < noutput_items; ){ + int nframes = d_writer->space_available() / nchan; // How much space in ringbuffer + if (nframes == 0){ // no room... + if (d_ok_to_block){ + { + gruel::scoped_lock guard(d_ringbuffer_mutex); + while (!d_ringbuffer_ready) + d_ringbuffer_cond.wait(guard); + } + + continue; + } + else { + // There's no room and we're not allowed to block. + // (A USRP is most likely controlling the pacing through the pipeline.) + // We drop the samples on the ground, and say we processed them all ;) + // + // FIXME, there's probably room for a bit more finesse here. + return noutput_items; + } + } + + // We can write the smaller of the request and the room we've got + { + gruel::scoped_lock guard(d_ringbuffer_mutex); + + int nf = std::min(noutput_items - k, nframes); + float *p = (float *) d_writer->write_pointer(); + + for (int i = 0; i < nf; i++) + for (unsigned int c = 0; c < nchan; c++) + *p++ = in[c][k + i]; + + d_writer->update_write_pointer(nf * nchan); + k += nf; + + d_ringbuffer_ready = false; + } + } + + return k; // tell how many we actually did +} + +void +audio_portaudio_sink::output_error_msg (const char *msg, int err) +{ + fprintf (stderr, "audio_portaudio_sink[%s]: %s: %s\n", + d_device_name.c_str (), msg, Pa_GetErrorText(err)); +} + +void +audio_portaudio_sink::bail (const char *msg, int err) throw (std::runtime_error) +{ + output_error_msg (msg, err); + throw std::runtime_error ("audio_portaudio_sink"); +} diff --git a/gr-audio/lib/portaudio/audio_portaudio_sink.h b/gr-audio/lib/portaudio/audio_portaudio_sink.h new file mode 100644 index 000000000..6426a32ac --- /dev/null +++ b/gr-audio/lib/portaudio/audio_portaudio_sink.h @@ -0,0 +1,85 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006-2011 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ +#ifndef INCLUDED_AUDIO_PORTAUDIO_SINK_H +#define INCLUDED_AUDIO_PORTAUDIO_SINK_H + +#include <gr_audio_sink.h> +#include <gr_buffer.h> +#include <gruel/thread.h> +#include <string> +#include <portaudio.h> +#include <stdexcept> +//#include <gri_logger.h> + +PaStreamCallback portaudio_sink_callback; + + +/*! + * \ Audio sink using PORTAUDIO + * + * Input samples must be in the range [-1,1]. + */ +class audio_portaudio_sink : public audio_sink { + + friend PaStreamCallback portaudio_sink_callback; + + + unsigned int d_sampling_rate; + std::string d_device_name; + bool d_ok_to_block; + bool d_verbose; + + unsigned int d_portaudio_buffer_size_frames; // number of frames in a portaudio buffer + + PaStream *d_stream; + PaStreamParameters d_output_parameters; + + gr_buffer_sptr d_writer; // buffer used between work and callback + gr_buffer_reader_sptr d_reader; + + gruel::mutex d_ringbuffer_mutex; + gruel::condition_variable d_ringbuffer_cond; + bool d_ringbuffer_ready; + + // random stats + int d_nunderuns; // count of underruns + //gri_logger_sptr d_log; // handle to non-blocking logging instance + + void output_error_msg (const char *msg, int err); + void bail (const char *msg, int err) throw (std::runtime_error); + void create_ringbuffer(); + + +public: + audio_portaudio_sink (int sampling_rate, const std::string device_name, + bool ok_to_block); + + ~audio_portaudio_sink (); + + bool check_topology (int ninputs, int noutputs); + + int work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; + +#endif /* INCLUDED_AUDIO_PORTAUDIO_SINK_H */ diff --git a/gr-audio/lib/portaudio/audio_portaudio_source.cc b/gr-audio/lib/portaudio/audio_portaudio_source.cc new file mode 100644 index 000000000..bdb8b3b3d --- /dev/null +++ b/gr-audio/lib/portaudio/audio_portaudio_source.cc @@ -0,0 +1,374 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006-2011 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in he hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gr_audio_registry.h" +#include <audio_portaudio_source.h> +#include <gr_io_signature.h> +#include <gr_prefs.h> +#include <stdio.h> +#include <iostream> +#include <unistd.h> +#include <stdexcept> +#include <gri_portaudio.h> +#include <string.h> + +AUDIO_REGISTER_SOURCE(REG_PRIO_MED, portaudio)( + int sampling_rate, const std::string &device_name, bool ok_to_block +){ + return audio_source::sptr(new audio_portaudio_source(sampling_rate, device_name, ok_to_block)); +} + +//#define LOGGING 0 // define to 0 or 1 + +#define SAMPLE_FORMAT paFloat32 +typedef float sample_t; + +// Number of portaudio buffers in the ringbuffer +static const unsigned int N_BUFFERS = 4; + +static std::string +default_device_name () +{ + return gr_prefs::singleton()->get_string("audio_portaudio", "default_input_device", ""); +} + +void +audio_portaudio_source::create_ringbuffer(void) +{ + int bufsize_samples = d_portaudio_buffer_size_frames * d_input_parameters.channelCount; + + if (d_verbose) + fprintf(stderr, "ring buffer size = %d frames\n", + N_BUFFERS*bufsize_samples/d_input_parameters.channelCount); + + // FYI, the buffer indicies are in units of samples. + d_writer = gr_make_buffer(N_BUFFERS * bufsize_samples, sizeof(sample_t)); + d_reader = gr_buffer_add_reader(d_writer, 0); +} + +/* + * This routine will be called by the PortAudio engine when audio is needed. + * It may called at interrupt level on some machines so don't do anything + * that could mess up the system like calling malloc() or free(). + * + * Our job is to copy framesPerBuffer frames from inputBuffer. + */ +int +portaudio_source_callback (const void *inputBuffer, + void *outputBuffer, + unsigned long framesPerBuffer, + const PaStreamCallbackTimeInfo* timeInfo, + PaStreamCallbackFlags statusFlags, + void *arg) +{ + audio_portaudio_source *self = (audio_portaudio_source *)arg; + int nchan = self->d_input_parameters.channelCount; + int nframes_to_copy = framesPerBuffer; + int nframes_room = self->d_writer->space_available() / nchan; + + if (nframes_to_copy <= nframes_room){ // We've got room for the data .. + //if (LOGGING) + // self->d_log->printf("PAsrc cb: f/b = %4ld\n", framesPerBuffer); + + // copy from input buffer to ringbuffer + { + gruel::scoped_lock(d_ringbuffer_mutex); + + memcpy(self->d_writer->write_pointer(), + inputBuffer, + nframes_to_copy * nchan * sizeof(sample_t)); + self->d_writer->update_write_pointer(nframes_to_copy * nchan); + + // Tell the source thread there is new data in the ringbuffer. + self->d_ringbuffer_ready = true; + } + + self->d_ringbuffer_cond.notify_one(); + return paContinue; + } + + else { // overrun + self->d_noverruns++; + ssize_t r = ::write(2, "aO", 2); // FIXME change to non-blocking call + if(r == -1) { + perror("audio_portaudio_source::portaudio_source_callback write error to stderr."); + } + + self->d_ringbuffer_ready = false; + self->d_ringbuffer_cond.notify_one(); // Tell the sink to get going! + return paContinue; + } +} + + +// ---------------------------------------------------------------- + +audio_portaudio_source::audio_portaudio_source(int sampling_rate, + const std::string device_name, + bool ok_to_block) + : audio_source ("audio_portaudio_source", + gr_make_io_signature(0, 0, 0), + gr_make_io_signature(0, 0, 0)), + d_sampling_rate(sampling_rate), + d_device_name(device_name.empty() ? default_device_name() : device_name), + d_ok_to_block(ok_to_block), + d_verbose(gr_prefs::singleton()->get_bool("audio_portaudio", "verbose", false)), + d_portaudio_buffer_size_frames(0), + d_stream(0), + d_ringbuffer_mutex(), + d_ringbuffer_cond(), + d_ringbuffer_ready(false), + d_noverruns(0) +{ + memset(&d_input_parameters, 0, sizeof(d_input_parameters)); + //if (LOGGING) + // d_log = gri_logger::singleton(); + + PaError err; + int i, numDevices; + PaDeviceIndex device = 0; + const PaDeviceInfo *deviceInfo = NULL; + + + err = Pa_Initialize(); + if (err != paNoError) { + bail ("Initialize failed", err); + } + + if (d_verbose) + gri_print_devices(); + + numDevices = Pa_GetDeviceCount(); + if (numDevices < 0) + bail("Pa Device count failed", 0); + if (numDevices == 0) + bail("no devices available", 0); + + if (d_device_name.empty()) + { + // FIXME Get smarter about picking something + device = Pa_GetDefaultInputDevice(); + deviceInfo = Pa_GetDeviceInfo(device); + fprintf(stderr,"%s is the chosen device using %s as the host\n", + deviceInfo->name, Pa_GetHostApiInfo(deviceInfo->hostApi)->name); + } + else + { + bool found = false; + + for (i=0;i<numDevices;i++) { + deviceInfo = Pa_GetDeviceInfo( i ); + fprintf(stderr,"Testing device name: %s",deviceInfo->name); + if (deviceInfo->maxInputChannels <= 0) { + fprintf(stderr,"\n"); + continue; + } + if (strstr(deviceInfo->name, d_device_name.c_str())){ + fprintf(stderr," Chosen!\n"); + device = i; + fprintf(stderr,"%s using %s as the host\n",d_device_name.c_str(), + Pa_GetHostApiInfo(deviceInfo->hostApi)->name), fflush(stderr); + found = true; + deviceInfo = Pa_GetDeviceInfo(device); + i = numDevices; // force loop exit + } + else + fprintf(stderr,"\n"),fflush(stderr); + } + + if (!found){ + bail("Failed to find specified device name", 0); + } + } + + + d_input_parameters.device = device; + d_input_parameters.channelCount = deviceInfo->maxInputChannels; + d_input_parameters.sampleFormat = SAMPLE_FORMAT; + d_input_parameters.suggestedLatency = deviceInfo->defaultLowInputLatency; + d_input_parameters.hostApiSpecificStreamInfo = NULL; + + // We fill in the real channelCount in check_topology when we know + // how many inputs are connected to us. + + // Now that we know the maximum number of channels (allegedly) + // supported by the h/w, we can compute a reasonable output + // signature. The portaudio specs say that they'll accept any + // number of channels from 1 to max. + set_output_signature(gr_make_io_signature(1, deviceInfo->maxInputChannels, + sizeof (sample_t))); +} + + +bool +audio_portaudio_source::check_topology (int ninputs, int noutputs) +{ + PaError err; + + if (Pa_IsStreamActive(d_stream)) + { + Pa_CloseStream(d_stream); + d_stream = 0; + d_reader.reset(); // boost::shared_ptr for d_reader = 0 + d_writer.reset(); // boost::shared_ptr for d_write = 0 + } + + d_input_parameters.channelCount = noutputs; // # of channels we're really using + +#if 1 + d_portaudio_buffer_size_frames = (int)(0.0213333333 * d_sampling_rate + 0.5); // Force 512 frame buffers at 48000 + fprintf(stderr, "Latency = %8.5f, requested sampling_rate = %g\n", // Force latency to 21.3333333.. ms + 0.0213333333, (double)d_sampling_rate); +#endif + err = Pa_OpenStream(&d_stream, + &d_input_parameters, + NULL, // No output + d_sampling_rate, + d_portaudio_buffer_size_frames, + paClipOff, + &portaudio_source_callback, + (void*)this); + + if (err != paNoError) { + output_error_msg ("OpenStream failed", err); + return false; + } + +#if 0 + const PaStreamInfo *psi = Pa_GetStreamInfo(d_stream); + + d_portaudio_buffer_size_frames = (int)(d_input_parameters.suggestedLatency * psi->sampleRate); + fprintf(stderr, "Latency = %7.4f, psi->sampleRate = %g\n", + d_input_parameters.suggestedLatency, psi->sampleRate); +#endif + + fprintf(stderr, "d_portaudio_buffer_size_frames = %d\n", d_portaudio_buffer_size_frames); + + assert(d_portaudio_buffer_size_frames != 0); + + create_ringbuffer(); + + err = Pa_StartStream(d_stream); + if (err != paNoError) { + output_error_msg ("StartStream failed", err); + return false; + } + + return true; +} + +audio_portaudio_source::~audio_portaudio_source () +{ + Pa_StopStream(d_stream); // wait for output to drain + Pa_CloseStream(d_stream); + Pa_Terminate(); +} + +int +audio_portaudio_source::work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + float **out = (float **) &output_items[0]; + const unsigned nchan = d_input_parameters.channelCount; // # of channels == samples/frame + + int k; + for (k = 0; k < noutput_items; ){ + + int nframes = d_reader->items_available() / nchan; // # of frames in ringbuffer + if (nframes == 0){ // no data right now... + if (k > 0) // If we've produced anything so far, return that + return k; + + if (d_ok_to_block) { + gruel:: scoped_lock guard(d_ringbuffer_mutex); + while (d_ringbuffer_ready == false) + d_ringbuffer_cond.wait(guard); // block here, then try again + continue; + } + + assert(k == 0); + + // There's no data and we're not allowed to block. + // (A USRP is most likely controlling the pacing through the pipeline.) + // This is an underun. The scheduler wouldn't have called us if it + // had anything better to do. Thus we really need to produce some amount + // of "fill". + // + // There are lots of options for comfort noise, etc. + // FIXME We'll fill with zeros for now. Yes, it will "click"... + + // Fill with some frames of zeros + { + gruel::scoped_lock guard(d_ringbuffer_mutex); + + int nf = std::min(noutput_items - k, (int) d_portaudio_buffer_size_frames); + for (int i = 0; i < nf; i++){ + for (unsigned int c = 0; c < nchan; c++){ + out[c][k + i] = 0; + } + } + k += nf; + + d_ringbuffer_ready = false; + return k; + } + } + + // We can read the smaller of the request and what's in the buffer. + { + gruel::scoped_lock guard(d_ringbuffer_mutex); + + int nf = std::min(noutput_items - k, nframes); + + const float *p = (const float *) d_reader->read_pointer(); + for (int i = 0; i < nf; i++){ + for (unsigned int c = 0; c < nchan; c++){ + out[c][k + i] = *p++; + } + } + d_reader->update_read_pointer(nf * nchan); + k += nf; + d_ringbuffer_ready = false; + } + } + + return k; // tell how many we actually did +} + +void +audio_portaudio_source::output_error_msg (const char *msg, int err) +{ + fprintf (stderr, "audio_portaudio_source[%s]: %s: %s\n", + d_device_name.c_str (), msg, Pa_GetErrorText(err)); +} + +void +audio_portaudio_source::bail (const char *msg, int err) throw (std::runtime_error) +{ + output_error_msg (msg, err); + throw std::runtime_error ("audio_portaudio_source"); +} diff --git a/gr-audio/lib/portaudio/audio_portaudio_source.h b/gr-audio/lib/portaudio/audio_portaudio_source.h new file mode 100644 index 000000000..245b3410b --- /dev/null +++ b/gr-audio/lib/portaudio/audio_portaudio_source.h @@ -0,0 +1,83 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006-2011 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ +#ifndef INCLUDED_AUDIO_PORTAUDIO_SOURCE_H +#define INCLUDED_AUDIO_PORTAUDIO_SOURCE_H + +#include <gr_audio_source.h> +#include <gr_buffer.h> +#include <gruel/thread.h> +#include <string> +#include <portaudio.h> +#include <stdexcept> + +PaStreamCallback portaudio_source_callback; + + +/*! + * \ Audio source using PORTAUDIO + * + * Input samples must be in the range [-1,1]. + */ +class audio_portaudio_source : public audio_source { + + friend PaStreamCallback portaudio_source_callback; + + + unsigned int d_sampling_rate; + std::string d_device_name; + bool d_ok_to_block; + bool d_verbose; + + unsigned int d_portaudio_buffer_size_frames; // number of frames in a portaudio buffer + + PaStream *d_stream; + PaStreamParameters d_input_parameters; + + gr_buffer_sptr d_writer; // buffer used between work and callback + gr_buffer_reader_sptr d_reader; + + gruel::mutex d_ringbuffer_mutex; + gruel::condition_variable d_ringbuffer_cond; + bool d_ringbuffer_ready; + + // random stats + int d_noverruns; // count of overruns + + void output_error_msg (const char *msg, int err); + void bail (const char *msg, int err) throw (std::runtime_error); + void create_ringbuffer(); + + +public: + audio_portaudio_source (int sampling_rate, const std::string device_name, + bool ok_to_block); + + ~audio_portaudio_source (); + + bool check_topology (int ninputs, int noutputs); + + int work (int ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; + +#endif /* INCLUDED_AUDIO_PORTAUDIO_SOURCE_H */ diff --git a/gr-audio/lib/portaudio/gr-audio-portaudio.conf b/gr-audio/lib/portaudio/gr-audio-portaudio.conf new file mode 100644 index 000000000..0dd147443 --- /dev/null +++ b/gr-audio/lib/portaudio/gr-audio-portaudio.conf @@ -0,0 +1,10 @@ +# This file contains system wide configuration data for GNU Radio. +# You may override any setting on a per-user basis by editing +# ~/.gnuradio/config.conf + +[audio_portaudio] + +#default_input_device = hw:0,0 +#default_output_device = hw:0,0 + +verbose = false diff --git a/gr-audio/lib/portaudio/gri_portaudio.cc b/gr-audio/lib/portaudio/gri_portaudio.cc new file mode 100644 index 000000000..faa472337 --- /dev/null +++ b/gr-audio/lib/portaudio/gri_portaudio.cc @@ -0,0 +1,111 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gri_portaudio.h> +#include <portaudio.h> +#include <string.h> + + +PaDeviceIndex +gri_pa_find_device_by_name(const char *name) +{ + int i; + int numDevices; + const PaDeviceInfo *pdi; + int len = strlen( name ); + PaDeviceIndex result = paNoDevice; + numDevices = Pa_GetDeviceCount(); + for( i=0; i<numDevices; i++ ) + { + pdi = Pa_GetDeviceInfo( i ); + if( strncmp( name, pdi->name, len ) == 0 ) + { + result = i; + break; + } + } + return result; +} + + +void +gri_print_devices() +{ + int numDevices, defaultDisplayed, myDevice=0; + const PaDeviceInfo *deviceInfo; + + numDevices = Pa_GetDeviceCount(); + if (numDevices < 0) + return; + + printf("Number of devices found = %d\n", numDevices); + + for (int i=0; i < numDevices; i++ ) { + deviceInfo = Pa_GetDeviceInfo( i ); + printf( "--------------------------------------- device #%d\n", i ); + /* Mark global and API specific default devices */ + defaultDisplayed = 0; + if( i == Pa_GetDefaultInputDevice() ) + { + myDevice = i; + printf( "[ Default Input" ); + defaultDisplayed = 1; + } + else if( i == Pa_GetHostApiInfo( deviceInfo->hostApi )->defaultInputDevice ) + { + const PaHostApiInfo *hostInfo = Pa_GetHostApiInfo( deviceInfo->hostApi ); + printf( "[ Default %s Input", hostInfo->name ); + defaultDisplayed = 1; + } + + if( i == Pa_GetDefaultOutputDevice() ) + { + printf( (defaultDisplayed ? "," : "[") ); + printf( " Default Output" ); + defaultDisplayed = 1; + } + else if( i == Pa_GetHostApiInfo( deviceInfo->hostApi )->defaultOutputDevice ) + { + const PaHostApiInfo *hostInfo = Pa_GetHostApiInfo( deviceInfo->hostApi ); + printf( (defaultDisplayed ? "," : "[") ); + printf( " Default %s Output", hostInfo->name ); + defaultDisplayed = 1; + } + if( defaultDisplayed ) + printf( " ]\n" ); + + /* print device info fields */ + printf( "Name = %s\n", deviceInfo->name ); + printf( "Host API = %s\n", Pa_GetHostApiInfo( deviceInfo->hostApi )->name ); + printf( "Max inputs = %d", deviceInfo->maxInputChannels ); + printf( ", Max outputs = %d\n", deviceInfo->maxOutputChannels ); + + printf( "Default low input latency = %8.3f\n", deviceInfo->defaultLowInputLatency ); + printf( "Default low output latency = %8.3f\n", deviceInfo->defaultLowOutputLatency ); + printf( "Default high input latency = %8.3f\n", deviceInfo->defaultHighInputLatency ); + printf( "Default high output latency = %8.3f\n", deviceInfo->defaultHighOutputLatency ); + } +} diff --git a/gr-audio/lib/portaudio/gri_portaudio.h b/gr-audio/lib/portaudio/gri_portaudio.h new file mode 100644 index 000000000..36191e25a --- /dev/null +++ b/gr-audio/lib/portaudio/gri_portaudio.h @@ -0,0 +1,32 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GRI_PORTAUDIO_H +#define INCLUDED_GRI_PORTAUDIO_H + +#include <stdio.h> +#include <portaudio.h> + +PaDeviceIndex gri_pa_find_device_by_name(const char *name); +void gri_print_devices(); + +#endif /* INCLUDED_GRI_PORTAUDIO_H */ |