diff options
author | Tom Rondeau <trondeau@vt.edu> | 2013-02-11 22:57:58 -0500 |
---|---|---|
committer | Tom Rondeau <trondeau@vt.edu> | 2013-02-11 22:57:58 -0500 |
commit | 0c8763a943dde47b4bbebf526c442509a9cf8bd4 (patch) | |
tree | fe1b7e6b77d8ca931e9b818ee7a792aef207d7fa | |
parent | 354c1219a22e13422b14d9bc14645e1296776d24 (diff) |
analog: wip: oss and jack converted and compiling.
C++ dial_tone example working.
-rw-r--r-- | gr-audio/examples/c++/dial_tone.cc | 2 | ||||
-rw-r--r-- | gr-audio/lib/jack/audio_jack_sink.cc | 319 | ||||
-rw-r--r-- | gr-audio/lib/jack/audio_jack_sink.h | 82 | ||||
-rw-r--r-- | gr-audio/lib/jack/audio_jack_source.cc | 321 | ||||
-rw-r--r-- | gr-audio/lib/jack/audio_jack_source.h | 82 | ||||
-rw-r--r-- | gr-audio/lib/oss/audio_oss_sink.cc | 220 | ||||
-rw-r--r-- | gr-audio/lib/oss/audio_oss_sink.h | 56 | ||||
-rw-r--r-- | gr-audio/lib/oss/audio_oss_source.cc | 245 | ||||
-rw-r--r-- | gr-audio/lib/oss/audio_oss_source.h | 61 | ||||
-rw-r--r-- | gr-audio/swig/audio_swig.i | 15 |
10 files changed, 719 insertions, 684 deletions
diff --git a/gr-audio/examples/c++/dial_tone.cc b/gr-audio/examples/c++/dial_tone.cc index 924e7953f1..f866edfdee 100644 --- a/gr-audio/examples/c++/dial_tone.cc +++ b/gr-audio/examples/c++/dial_tone.cc @@ -58,7 +58,7 @@ int main(int argc, char **argv) analog::sig_source_f::sptr src1 = analog::sig_source_f::make(rate, analog::GR_SIN_WAVE, 440, ampl); // Construct an audio sink to accept audio tones - audio_sink::sptr sink = audio_make_sink(rate); + audio::sink::sptr sink = audio::sink::make(rate); // Connect output #0 of src0 to input #0 of sink (left channel) tb->connect(src0, 0, sink, 0); diff --git a/gr-audio/lib/jack/audio_jack_sink.cc b/gr-audio/lib/jack/audio_jack_sink.cc index 9caabe8e2f..3b5c474a24 100644 --- a/gr-audio/lib/jack/audio_jack_sink.cc +++ b/gr-audio/lib/jack/audio_jack_sink.cc @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2005-2011 Free Software Foundation, Inc. + * Copyright 2005-2011,2013 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -37,200 +37,205 @@ #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)); -} +namespace gr { + namespace audio { -typedef jack_default_audio_sample_t sample_t; + AUDIO_REGISTER_SINK(REG_PRIO_MED, jack)(int sampling_rate, + const std::string &device_name, + bool ok_to_block) + { + return sink::sptr + (new 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; + // 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"); -} + 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); + int + jack_sink_process(jack_nframes_t nframes, void *arg) + { + jack_sink *self = (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; - } + 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); + char *buffer = (char *)jack_port_get_buffer(self->d_jack_output_port, nframes); - jack_ringbuffer_read (self->d_ringbuffer, buffer, read_size); + 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); - } + // 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) - : gr_sync_block ("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) -{ + return 0; + } + + // ---------------------------------------------------------------- + + jack_sink::jack_sink(int sampling_rate, + const std::string device_name, + bool ok_to_block) + : gr_sync_block("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); + 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"); + } - // 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_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. - // 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); - //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_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); - d_jack_buffer_size = jack_get_buffer_size (d_jack_client); + set_output_multiple(d_jack_buffer_size); - 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); - 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))); - 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); + } + } - jack_nframes_t sample_rate = jack_get_sample_rate (d_jack_client); + bool + jack_sink::check_topology (int ninputs, int noutputs) + { + if(ninputs != 1) + return false; - 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); - } -} + // 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; + } -bool -audio_jack_sink::check_topology (int ninputs, int noutputs) -{ - if (ninputs != 1) - return false; + jack_sink::~jack_sink() + { + jack_client_close(d_jack_client); + jack_ringbuffer_free(d_ringbuffer); + } - // tell the JACK server that we are ready to roll - if (jack_activate (d_jack_client)) - throw std::runtime_error ("audio_jack_sink"); + int + 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; - return true; -} + while(work_size > 0) { + unsigned int write_space; // bytes -audio_jack_sink::~audio_jack_sink () -{ - jack_client_close (d_jack_client); - jack_ringbuffer_free (d_ringbuffer); -} +#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 -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; + pthread_mutex_lock(&d_jack_process_lock); + while((write_space = jack_ringbuffer_write_space(d_ringbuffer)) < + d_jack_buffer_size*sizeof(sample_t)) { - while (work_size > 0) { - unsigned int write_space; // bytes + // 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 -#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 + write_space -= write_space%(d_jack_buffer_size*sizeof(sample_t)); + write_size = std::min(write_space, (unsigned int)work_size); - pthread_mutex_lock (&d_jack_process_lock); - while ((write_space=jack_ringbuffer_write_space (d_ringbuffer)) < - d_jack_buffer_size*sizeof(sample_t)) { + if(jack_ringbuffer_write(d_ringbuffer, (char *) input_items[0], + write_size) < write_size) { + bail("jack_ringbuffer_write failed", 0); + } + work_size -= write_size; + } - // wait until jack_sink_process() signals more room - pthread_cond_wait (&d_ringbuffer_ready, &d_jack_process_lock); + return noutput_items; } - 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); + void + 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); + } - if (jack_ringbuffer_write (d_ringbuffer, (char *) input_items[0], - write_size) < write_size) { - bail ("jack_ringbuffer_write failed", 0); + void + jack_sink::bail(const char *msg, int err) throw (std::runtime_error) + { + output_error_msg(msg, err); + throw std::runtime_error("audio_jack_sink"); } - 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"); -} + + } /* namespace audio */ +} /* namespace gr */ diff --git a/gr-audio/lib/jack/audio_jack_sink.h b/gr-audio/lib/jack/audio_jack_sink.h index ac6c770b54..2caecbd54c 100644 --- a/gr-audio/lib/jack/audio_jack_sink.h +++ b/gr-audio/lib/jack/audio_jack_sink.h @@ -23,58 +23,64 @@ #define INCLUDED_AUDIO_JACK_SINK_H #include <audio/sink.h> -#include <string> #include <jack/jack.h> #include <jack/ringbuffer.h> +#include <string> #include <stdexcept> -int jack_sink_process (jack_nframes_t nframes, void *arg); - -/*! - * \brief audio sink using JACK - * \ingroup audio_blk - * - * The sink has one input stream of floats. - * - * Input samples must be in the range [-1,1]. - */ -class audio_jack_sink : public audio_sink { +namespace gr { + namespace audio { - friend int jack_sink_process (jack_nframes_t nframes, void *arg); + int 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); + /*! + * \brief audio sink using JACK + * \ingroup audio_blk + * + * The sink has one input stream of floats. + * + * Input samples must be in the range [-1,1]. + */ + class jack_sink : public sink + { + friend int jack_sink_process(jack_nframes_t nframes, void *arg); - unsigned int d_sampling_rate; - std::string d_device_name; - bool d_ok_to_block; + // typedef for pointer to class work method + typedef int (jack_sink::*work_t)(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); - 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; + unsigned int d_sampling_rate; + std::string d_device_name; + bool d_ok_to_block; - // random stats - int d_nunderuns; // count of underruns + 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; - void output_error_msg (const char *msg, int err); - void bail (const char *msg, int err) throw (std::runtime_error); + // 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); + public: + jack_sink(int sampling_rate, + const std::string device_name, + bool ok_to_block); + ~jack_sink(); - ~audio_jack_sink (); + bool check_topology(int ninputs, int noutputs); - 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); + }; - int work (int noutput_items, - gr_vector_const_void_star &input_items, - gr_vector_void_star &output_items); -}; + } /* namespace audio */ +} /* namespace gr */ #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 index 137fd538e4..3c09081d57 100644 --- a/gr-audio/lib/jack/audio_jack_source.cc +++ b/gr-audio/lib/jack/audio_jack_source.cc @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2005,2006,2010 Free Software Foundation, Inc. + * Copyright 2005,2006,2010,2013 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -37,201 +37,204 @@ #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)); -} +namespace gr { + namespace audio { -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; + AUDIO_REGISTER_SOURCE(REG_PRIO_MED, jack)(int sampling_rate, + const std::string &device_name, + bool ok_to_block) + { + return source::sptr + (new jack_source(sampling_rate, device_name, ok_to_block)); + } -static std::string -default_device_name () -{ - return gr_prefs::singleton()->get_string("audio_jack", "default_input_device", "gr_source"); -} + 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; -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); + static std::string + default_device_name() + { + return gr_prefs::singleton()->get_string + ("audio_jack", "default_input_device", "gr_source"); + } - 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; - } + int + jack_source_process(jack_nframes_t nframes, void *arg) + { + jack_source *self = (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); + char *buffer = (char *)jack_port_get_buffer(self->d_jack_input_port, nframes); - jack_ringbuffer_write (self->d_ringbuffer, buffer, write_size); + 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); - } + // 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) - : gr_sync_block ("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) -{ + return 0; + } + + // ---------------------------------------------------------------- + + jack_source::jack_source(int sampling_rate, + const std::string device_name, + bool ok_to_block) + : gr_sync_block("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); + 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. + // 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"); + } - //jack_on_shutdown (d_jack_client, &jack_shutdown, (void*)this); + // 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); - d_jack_input_port = jack_port_register (d_jack_client, "in", - JACK_DEFAULT_AUDIO_TYPE, - JackPortIsInput, 0); + // 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_buffer_size = jack_get_buffer_size (d_jack_client); + d_jack_input_port = jack_port_register(d_jack_client, "in", + JACK_DEFAULT_AUDIO_TYPE, + JackPortIsInput, 0); - set_output_multiple (d_jack_buffer_size); + d_jack_buffer_size = jack_get_buffer_size(d_jack_client); - d_ringbuffer = jack_ringbuffer_create (N_BUFFERS*d_jack_buffer_size*sizeof(sample_t)); - if (d_ringbuffer == NULL) - bail ("jack_ringbuffer_create failed", 0); + set_output_multiple(d_jack_buffer_size); - assert(sizeof(float)==sizeof(sample_t)); - set_output_signature (gr_make_io_signature (1, 1, sizeof (sample_t))); + 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); - } -} + 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"); + bool + 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; -} + return true; + } -audio_jack_source::~audio_jack_source () -{ - jack_client_close (d_jack_client); - jack_ringbuffer_free (d_ringbuffer); -} + jack_source::~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; + int + 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); + // Minimize latency + noutput_items = std::min (noutput_items, (int)d_jack_buffer_size); - int work_size = noutput_items*sizeof(sample_t); + int work_size = noutput_items*sizeof(sample_t); - while (work_size > 0) { - unsigned int read_space; // bytes + 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)); - } + 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)) { + // 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); - // wait until jack_source_process() signals more data - pthread_cond_wait (&d_ringbuffer_ready, &d_jack_process_lock); + 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; } - 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); + void + 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); + } - if (jack_ringbuffer_read (d_ringbuffer, (char *) output_items[0], - read_size) < read_size) { - bail ("jack_ringbuffer_read failed", 0); + void + jack_source::bail(const char *msg, int err) throw (std::runtime_error) + { + output_error_msg(msg, err); + throw std::runtime_error("audio_jack_source"); } - 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"); -} + + } /* namespace audio */ +} /* namespace gr */ diff --git a/gr-audio/lib/jack/audio_jack_source.h b/gr-audio/lib/jack/audio_jack_source.h index 5b893a3d34..f096220b26 100644 --- a/gr-audio/lib/jack/audio_jack_source.h +++ b/gr-audio/lib/jack/audio_jack_source.h @@ -23,58 +23,64 @@ #define INCLUDED_AUDIO_JACK_SOURCE_H #include <audio/source.h> -#include <string> #include <jack/jack.h> #include <jack/ringbuffer.h> +#include <string> #include <stdexcept> -int jack_source_process (jack_nframes_t nframes, void *arg); - -/*! - * \brief audio source using JACK - * \ingroup audio_blk - * - * The source has one input stream of floats. - * - * Output samples will be in the range [-1,1]. - */ -class audio_jack_source : public audio_source { +namespace gr { + namespace audio { - friend int jack_source_process (jack_nframes_t nframes, void *arg); + 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); + /*! + * \brief audio source using JACK + * \ingroup audio_blk + * + * The source has one input stream of floats. + * + * Output samples will be in the range [-1,1]. + */ + class jack_source : public source + { + friend int jack_source_process(jack_nframes_t nframes, void *arg); - unsigned int d_sampling_rate; - std::string d_device_name; - bool d_ok_to_block; + // typedef for pointer to class work method + typedef int(jack_source::*work_t)(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); - 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; + unsigned int d_sampling_rate; + std::string d_device_name; + bool d_ok_to_block; - // random stats - int d_noverruns; // count of overruns + 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; - void output_error_msg (const char *msg, int err); - void bail (const char *msg, int err) throw (std::runtime_error); + // 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); + public: + jack_source(int sampling_rate, + const std::string device_name, + bool ok_to_block); + ~jack_source(); - ~audio_jack_source (); + bool check_topology(int ninputs, int noutputs); - 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); + }; - int work (int noutput_items, - gr_vector_const_void_star &input_items, - gr_vector_void_star &output_items); -}; + } /* namespace audio */ +} /* namespace gr */ #endif /* INCLUDED_AUDIO_JACK_SOURCE_H */ diff --git a/gr-audio/lib/oss/audio_oss_sink.cc b/gr-audio/lib/oss/audio_oss_sink.cc index 26b71be241..28567bae11 100644 --- a/gr-audio/lib/oss/audio_oss_sink.cc +++ b/gr-audio/lib/oss/audio_oss_sink.cc @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2004-2011 Free Software Foundation, Inc. + * Copyright 2004-2011,2013 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -38,124 +38,130 @@ #include <iostream> #include <stdexcept> -AUDIO_REGISTER_SINK(REG_PRIO_LOW, oss)( - int sampling_rate, const std::string &device_name, bool ok_to_block -){ - return audio_sink::sptr(new audio_oss_sink(sampling_rate, device_name, ok_to_block)); -} - -static std::string -default_device_name () -{ - return gr_prefs::singleton()->get_string("audio_oss", "default_output_device", "/dev/dsp"); -} - -audio_oss_sink::audio_oss_sink (int sampling_rate, - const std::string device_name, - bool ok_to_block) - : gr_sync_block ("audio_oss_sink", - gr_make_io_signature (1, 2, sizeof (float)), - gr_make_io_signature (0, 0, 0)), - d_sampling_rate (sampling_rate), - d_device_name (device_name.empty() ? default_device_name() : device_name), - d_fd (-1), d_buffer (0), d_chunk_size (0) -{ - if ((d_fd = open (d_device_name.c_str (), O_WRONLY)) < 0){ - fprintf (stderr, "audio_oss_sink: "); - perror (d_device_name.c_str ()); - throw std::runtime_error ("audio_oss_sink"); - } - - double CHUNK_TIME = - std::max(0.001, gr_prefs::singleton()->get_double("audio_oss", "latency", 0.005)); - - d_chunk_size = (int) (d_sampling_rate * CHUNK_TIME); - set_output_multiple (d_chunk_size); - - d_buffer = new short [d_chunk_size * 2]; - - int format = AFMT_S16_NE; - int orig_format = format; - if (ioctl (d_fd, SNDCTL_DSP_SETFMT, &format) < 0){ - std::cerr << "audio_oss_sink: " << d_device_name << " ioctl failed\n"; - perror (d_device_name.c_str ()); - throw std::runtime_error ("audio_oss_sink"); - } - - if (format != orig_format){ - fprintf (stderr, "audio_oss_sink: unable to support format %d\n", orig_format); - fprintf (stderr, " card requested %d instead.\n", format); - } - - // set to stereo no matter what. Some hardware only does stereo - int channels = 2; - if (ioctl (d_fd, SNDCTL_DSP_CHANNELS, &channels) < 0 || channels != 2){ - perror ("audio_oss_sink: could not set STEREO mode"); - throw std::runtime_error ("audio_oss_sink"); - } - - // set sampling freq - int sf = sampling_rate; - if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0){ - std::cerr << "audio_oss_sink: " - << d_device_name << ": invalid sampling_rate " - << sampling_rate << "\n"; - sampling_rate = 8000; - if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0){ - std::cerr << "audio_oss_sink: failed to set sampling_rate to 8000\n"; - throw std::runtime_error ("audio_oss_sink"); +namespace gr { + namespace audio { + + AUDIO_REGISTER_SINK(REG_PRIO_LOW, oss)(int sampling_rate, + const std::string &device_name, + bool ok_to_block) + { + return sink::sptr + (new oss_sink(sampling_rate, device_name, ok_to_block)); } - } -} -audio_oss_sink::~audio_oss_sink () -{ - close (d_fd); - delete [] d_buffer; -} + static std::string + default_device_name() + { + return gr_prefs::singleton()->get_string + ("audio_oss", "default_output_device", "/dev/dsp"); + } + oss_sink::oss_sink(int sampling_rate, + const std::string device_name, + bool ok_to_block) + : gr_sync_block("audio_oss_sink", + gr_make_io_signature(1, 2, sizeof(float)), + gr_make_io_signature(0, 0, 0)), + d_sampling_rate(sampling_rate), + d_device_name(device_name.empty() ? default_device_name() : device_name), + d_fd(-1), d_buffer(0), d_chunk_size(0) + { + if((d_fd = open(d_device_name.c_str(), O_WRONLY)) < 0) { + fprintf(stderr, "audio_oss_sink: "); + perror(d_device_name.c_str()); + throw std::runtime_error("audio_oss_sink"); + } -int -audio_oss_sink::work (int noutput_items, - gr_vector_const_void_star &input_items, - gr_vector_void_star &output_items) -{ - const float *f0, *f1; + double CHUNK_TIME = + std::max(0.001, + gr_prefs::singleton()->get_double("audio_oss", "latency", 0.005)); - switch (input_items.size ()){ + d_chunk_size = (int)(d_sampling_rate * CHUNK_TIME); + set_output_multiple(d_chunk_size); - case 1: // mono input + d_buffer = new short[d_chunk_size * 2]; - f0 = (const float *) input_items[0]; + int format = AFMT_S16_NE; + int orig_format = format; + if(ioctl(d_fd, SNDCTL_DSP_SETFMT, &format) < 0) { + std::cerr << "audio_oss_sink: " << d_device_name << " ioctl failed\n"; + perror(d_device_name.c_str ()); + throw std::runtime_error("audio_oss_sink"); + } - for (int i = 0; i < noutput_items; i += d_chunk_size){ - for (int j = 0; j < d_chunk_size; j++){ - d_buffer[2*j+0] = (short) (f0[j] * 32767); - d_buffer[2*j+1] = (short) (f0[j] * 32767); + if(format != orig_format) { + fprintf(stderr, "audio_oss_sink: unable to support format %d\n", orig_format); + fprintf(stderr, " card requested %d instead.\n", format); } - f0 += d_chunk_size; - if (write (d_fd, d_buffer, 2 * d_chunk_size * sizeof (short)) < 0) - perror ("audio_oss_sink: write"); - } - break; - case 2: // stereo input + // set to stereo no matter what. Some hardware only does stereo + int channels = 2; + if(ioctl(d_fd, SNDCTL_DSP_CHANNELS, &channels) < 0 || channels != 2) { + perror("audio_oss_sink: could not set STEREO mode"); + throw std::runtime_error("audio_oss_sink"); + } - f0 = (const float *) input_items[0]; - f1 = (const float *) input_items[1]; + // set sampling freq + int sf = sampling_rate; + if(ioctl(d_fd, SNDCTL_DSP_SPEED, &sf) < 0) { + std::cerr << "audio_oss_sink: " + << d_device_name << ": invalid sampling_rate " + << sampling_rate << "\n"; + sampling_rate = 8000; + if(ioctl(d_fd, SNDCTL_DSP_SPEED, &sf) < 0) { + std::cerr << "audio_oss_sink: failed to set sampling_rate to 8000\n"; + throw std::runtime_error("audio_oss_sink"); + } + } + } - for (int i = 0; i < noutput_items; i += d_chunk_size){ - for (int j = 0; j < d_chunk_size; j++){ - d_buffer[2*j+0] = (short) (f0[j] * 32767); - d_buffer[2*j+1] = (short) (f1[j] * 32767); + oss_sink::~oss_sink() + { + close(d_fd); + delete [] d_buffer; + } + + int + oss_sink::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + const float *f0, *f1; + + switch(input_items.size()) { + case 1: // mono input + f0 = (const float *)input_items[0]; + + for(int i = 0; i < noutput_items; i += d_chunk_size) { + for(int j = 0; j < d_chunk_size; j++) { + d_buffer[2*j+0] = (short) (f0[j] * 32767); + d_buffer[2*j+1] = (short) (f0[j] * 32767); + } + f0 += d_chunk_size; + if(write(d_fd, d_buffer, 2 * d_chunk_size * sizeof (short)) < 0) + perror("audio_oss_sink: write"); + } + break; + + case 2: // stereo input + f0 = (const float *) input_items[0]; + f1 = (const float *) input_items[1]; + + for(int i = 0; i < noutput_items; i += d_chunk_size) { + for(int j = 0; j < d_chunk_size; j++) { + d_buffer[2*j+0] = (short)(f0[j] * 32767); + d_buffer[2*j+1] = (short)(f1[j] * 32767); + } + f0 += d_chunk_size; + f1 += d_chunk_size; + if(write(d_fd, d_buffer, 2 * d_chunk_size * sizeof (short)) < 0) + perror("audio_oss_sink: write"); + } + break; } - f0 += d_chunk_size; - f1 += d_chunk_size; - if (write (d_fd, d_buffer, 2 * d_chunk_size * sizeof (short)) < 0) - perror ("audio_oss_sink: write"); + + return noutput_items; } - break; - } - return noutput_items; -} + } /* namespace audio */ +} /* namespace gr */ diff --git a/gr-audio/lib/oss/audio_oss_sink.h b/gr-audio/lib/oss/audio_oss_sink.h index 8f516f8d73..3bb5f4ee1a 100644 --- a/gr-audio/lib/oss/audio_oss_sink.h +++ b/gr-audio/lib/oss/audio_oss_sink.h @@ -26,30 +26,36 @@ #include <audio/sink.h> #include <string> -/*! - * \brief audio sink using OSS - * \ingroup audio_blk - * - * input signature is one or two streams of floats. - * Input samples must be in the range [-1,1]. - */ - -class audio_oss_sink : public audio_sink { - - int d_sampling_rate; - std::string d_device_name; - int d_fd; - short *d_buffer; - int d_chunk_size; - -public: - audio_oss_sink (int sampling_rate, const std::string device_name = "", bool ok_to_block = true); - - ~audio_oss_sink (); - - int work (int noutput_items, - gr_vector_const_void_star &input_items, - gr_vector_void_star &output_items); -}; +namespace gr { + namespace audio { + + /*! + * \brief audio sink using OSS + * \ingroup audio_blk + * + * input signature is one or two streams of floats. + * Input samples must be in the range [-1,1]. + */ + class oss_sink : public sink + { + int d_sampling_rate; + std::string d_device_name; + int d_fd; + short *d_buffer; + int d_chunk_size; + + public: + oss_sink(int sampling_rate, + const std::string device_name = "", + bool ok_to_block = true); + ~oss_sink(); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace audio */ +} /* namespace gr */ #endif /* INCLUDED_AUDIO_OSS_SINK_H */ diff --git a/gr-audio/lib/oss/audio_oss_source.cc b/gr-audio/lib/oss/audio_oss_source.cc index e186e30aea..5682594d13 100644 --- a/gr-audio/lib/oss/audio_oss_source.cc +++ b/gr-audio/lib/oss/audio_oss_source.cc @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2004-2011 Free Software Foundation, Inc. + * Copyright 2004-2011,2013 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -38,140 +38,149 @@ #include <iostream> #include <stdexcept> -AUDIO_REGISTER_SOURCE(REG_PRIO_LOW, oss)( - int sampling_rate, const std::string &device_name, bool ok_to_block -){ - return audio_source::sptr(new audio_oss_source(sampling_rate, device_name, ok_to_block)); -} - -static std::string -default_device_name () -{ - return gr_prefs::singleton()->get_string("audio_oss", "default_input_device", "/dev/dsp"); -} - -audio_oss_source::audio_oss_source (int sampling_rate, - const std::string device_name, - bool ok_to_block) - : gr_sync_block ("audio_oss_source", - gr_make_io_signature (0, 0, 0), - gr_make_io_signature (1, 2, sizeof (float))), - d_sampling_rate (sampling_rate), - d_device_name (device_name.empty() ? default_device_name() : device_name), - d_fd (-1), d_buffer (0), d_chunk_size (0) -{ - if ((d_fd = open (d_device_name.c_str (), O_RDONLY)) < 0){ - fprintf (stderr, "audio_oss_source: "); - perror (d_device_name.c_str ()); - throw std::runtime_error ("audio_oss_source"); - } - - double CHUNK_TIME = - std::max(0.001, gr_prefs::singleton()->get_double("audio_oss", "latency", 0.005)); - - d_chunk_size = (int) (d_sampling_rate * CHUNK_TIME); - set_output_multiple (d_chunk_size); - - d_buffer = new short [d_chunk_size * 2]; - - int format = AFMT_S16_NE; - int orig_format = format; - if (ioctl (d_fd, SNDCTL_DSP_SETFMT, &format) < 0){ - std::cerr << "audio_oss_source: " << d_device_name << " ioctl failed\n"; - perror (d_device_name.c_str ()); - throw std::runtime_error ("audio_oss_source"); - } - - if (format != orig_format){ - fprintf (stderr, "audio_oss_source: unable to support format %d\n", orig_format); - fprintf (stderr, " card requested %d instead.\n", format); - } - - // set to stereo no matter what. Some hardware only does stereo - int channels = 2; - if (ioctl (d_fd, SNDCTL_DSP_CHANNELS, &channels) < 0 || channels != 2){ - perror ("audio_oss_source: could not set STEREO mode"); - throw std::runtime_error ("audio_oss_source"); - } - - // set sampling freq - int sf = sampling_rate; - if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0){ - std::cerr << "audio_oss_source: " - << d_device_name << ": invalid sampling_rate " - << sampling_rate << "\n"; - sampling_rate = 8000; - if (ioctl (d_fd, SNDCTL_DSP_SPEED, &sf) < 0){ - std::cerr << "audio_oss_source: failed to set sampling_rate to 8000\n"; - throw std::runtime_error ("audio_oss_source"); +namespace gr { + namespace audio { + + AUDIO_REGISTER_SOURCE(REG_PRIO_LOW, oss)(int sampling_rate, + const std::string &device_name, + bool ok_to_block) + { + return source::sptr + (new oss_source(sampling_rate, device_name, ok_to_block)); } - } -} -audio_oss_source::~audio_oss_source () -{ - close (d_fd); - delete [] d_buffer; -} + static std::string + default_device_name() + { + return gr_prefs::singleton()->get_string + ("audio_oss", "default_input_device", "/dev/dsp"); + } -int -audio_oss_source::work (int noutput_items, - gr_vector_const_void_star &input_items, - gr_vector_void_star &output_items) -{ - float *f0 = (float *) output_items[0]; - float *f1 = (float *) output_items[1]; // will be invalid if this is mono output + oss_source::oss_source(int sampling_rate, + const std::string device_name, + bool ok_to_block) + : gr_sync_block("audio_oss_source", + gr_make_io_signature(0, 0, 0), + gr_make_io_signature(1, 2, sizeof(float))), + d_sampling_rate(sampling_rate), + d_device_name(device_name.empty() ? default_device_name() : device_name), + d_fd(-1), d_buffer(0), d_chunk_size(0) + { + if((d_fd = open(d_device_name.c_str(), O_RDONLY)) < 0) { + fprintf(stderr, "audio_oss_source: "); + perror(d_device_name.c_str()); + throw std::runtime_error("audio_oss_source"); + } - const int shorts_per_item = 2; // L + R - const int bytes_per_item = shorts_per_item * sizeof (short); + double CHUNK_TIME = + std::max(0.001, gr_prefs::singleton()->get_double("audio_oss", "latency", 0.005)); - // To minimize latency, never return more than CHUNK_TIME - // worth of samples per call to work. + d_chunk_size = (int)(d_sampling_rate * CHUNK_TIME); + set_output_multiple(d_chunk_size); - noutput_items = std::min (noutput_items, d_chunk_size); + d_buffer = new short[d_chunk_size * 2]; - int base = 0; - int ntogo = noutput_items; + int format = AFMT_S16_NE; + int orig_format = format; + if(ioctl(d_fd, SNDCTL_DSP_SETFMT, &format) < 0) { + std::cerr << "audio_oss_source: " << d_device_name << " ioctl failed\n"; + perror(d_device_name.c_str ()); + throw std::runtime_error("audio_oss_source"); + } - while (ntogo > 0){ - int nbytes = std::min (ntogo, d_chunk_size) * bytes_per_item; - int result_nbytes = read (d_fd, d_buffer, nbytes); + if(format != orig_format) { + fprintf(stderr, "audio_oss_source: unable to support format %d\n", orig_format); + fprintf(stderr, " card requested %d instead.\n", format); + } - if (result_nbytes < 0){ - perror ("audio_oss_source"); - return -1; // say we're done + // set to stereo no matter what. Some hardware only does stereo + int channels = 2; + if(ioctl(d_fd, SNDCTL_DSP_CHANNELS, &channels) < 0 || channels != 2) { + perror("audio_oss_source: could not set STEREO mode"); + throw std::runtime_error("audio_oss_source"); + } + + // set sampling freq + int sf = sampling_rate; + if(ioctl(d_fd, SNDCTL_DSP_SPEED, &sf) < 0) { + std::cerr << "audio_oss_source: " + << d_device_name << ": invalid sampling_rate " + << sampling_rate << "\n"; + sampling_rate = 8000; + if(ioctl(d_fd, SNDCTL_DSP_SPEED, &sf) < 0) { + std::cerr << "audio_oss_source: failed to set sampling_rate to 8000\n"; + throw std::runtime_error ("audio_oss_source"); + } + } } - if ((result_nbytes & (bytes_per_item - 1)) != 0){ - fprintf (stderr, "audio_oss_source: internal error.\n"); - throw std::runtime_error ("internal error"); + oss_source::~oss_source() + { + close(d_fd); + delete [] d_buffer; } - int result_nitems = result_nbytes / bytes_per_item; + int + oss_source::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + float *f0 = (float *)output_items[0]; + float *f1 = (float *)output_items[1]; // will be invalid if this is mono output + + const int shorts_per_item = 2; // L + R + const int bytes_per_item = shorts_per_item * sizeof(short); - // now unpack samples into output streams + // To minimize latency, never return more than CHUNK_TIME + // worth of samples per call to work. - switch (output_items.size ()){ - case 1: // mono output - for (int i = 0; i < result_nitems; i++){ - f0[base+i] = d_buffer[2*i+0] * (1.0 / 32767); - } - break; + noutput_items = std::min(noutput_items, d_chunk_size); - case 2: // stereo output - for (int i = 0; i < result_nitems; i++){ - f0[base+i] = d_buffer[2*i+0] * (1.0 / 32767); - f1[base+i] = d_buffer[2*i+1] * (1.0 / 32767); - } - break; + int base = 0; + int ntogo = noutput_items; - default: - assert (0); + while(ntogo > 0) { + int nbytes = std::min(ntogo, d_chunk_size) * bytes_per_item; + int result_nbytes = read(d_fd, d_buffer, nbytes); + + if(result_nbytes < 0) { + perror("audio_oss_source"); + return -1; // say we're done + } + + if((result_nbytes & (bytes_per_item - 1)) != 0) { + fprintf(stderr, "audio_oss_source: internal error.\n"); + throw std::runtime_error("internal error"); + } + + int result_nitems = result_nbytes / bytes_per_item; + + // now unpack samples into output streams + + switch(output_items.size()) { + case 1: // mono output + for(int i = 0; i < result_nitems; i++) { + f0[base+i] = d_buffer[2*i+0] * (1.0 / 32767); + } + break; + + case 2: // stereo output + for(int i = 0; i < result_nitems; i++) { + f0[base+i] = d_buffer[2*i+0] * (1.0 / 32767); + f1[base+i] = d_buffer[2*i+1] * (1.0 / 32767); + } + break; + + default: + assert(0); + } + + ntogo -= result_nitems; + base += result_nitems; } - ntogo -= result_nitems; - base += result_nitems; - } + return noutput_items - ntogo; + } - return noutput_items - ntogo; -} + } /* namespace audio */ +} /* namespace gr */ diff --git a/gr-audio/lib/oss/audio_oss_source.h b/gr-audio/lib/oss/audio_oss_source.h index 27739c5f44..bf5183bd32 100644 --- a/gr-audio/lib/oss/audio_oss_source.h +++ b/gr-audio/lib/oss/audio_oss_source.h @@ -26,34 +26,37 @@ #include <audio/source.h> #include <string> -/*! - * \brief audio source using OSS - * \ingroup audio_blk - * - * Output signature is one or two streams of floats. - * Output samples will be in the range [-1,1]. - */ - -class audio_oss_source : public audio_source { - - int d_sampling_rate; - std::string d_device_name; - int d_fd; - short *d_buffer; - int d_chunk_size; - -public: - audio_oss_source (int sampling_rate, - const std::string device_name = "", - bool ok_to_block = true); - - ~audio_oss_source (); - - int work (int noutput_items, - gr_vector_const_void_star &input_items, - gr_vector_void_star &output_items); -}; - - +namespace gr { + namespace audio { + + /*! + * \brief audio source using OSS + * \ingroup audio_blk + * + * Output signature is one or two streams of floats. + * Output samples will be in the range [-1,1]. + */ + class oss_source : public source + { + int d_sampling_rate; + std::string d_device_name; + int d_fd; + short *d_buffer; + int d_chunk_size; + + public: + oss_source(int sampling_rate, + const std::string device_name = "", + bool ok_to_block = true); + + ~oss_source(); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace audio */ +} /* namespace gr */ #endif /* INCLUDED_AUDIO_OSS_SOURCE_H */ diff --git a/gr-audio/swig/audio_swig.i b/gr-audio/swig/audio_swig.i index 6e5ca6c00a..449fb5da51 100644 --- a/gr-audio/swig/audio_swig.i +++ b/gr-audio/swig/audio_swig.i @@ -22,27 +22,18 @@ #define GR_AUDIO_API -//////////////////////////////////////////////////////////////////////// -// standard includes -//////////////////////////////////////////////////////////////////////// %include "gnuradio.i" //load generated python docstrings %include "audio_swig_doc.i" -//////////////////////////////////////////////////////////////////////// -// block headers -//////////////////////////////////////////////////////////////////////// %{ #include <audio/source.h> #include <audio/sink.h> %} -//////////////////////////////////////////////////////////////////////// -// block magic -//////////////////////////////////////////////////////////////////////// -GR_SWIG_BLOCK_MAGIC(audio,source) %include <audio/source.h> - -GR_SWIG_BLOCK_MAGIC(audio,sink) %include <audio/sink.h> + +GR_SWIG_BLOCK_MAGIC2(audio, source) +GR_SWIG_BLOCK_MAGIC2(audio, sink) |