summaryrefslogtreecommitdiff
path: root/gr-audio-portaudio
diff options
context:
space:
mode:
authorJohnathan Corgan2010-04-26 22:13:34 -0700
committerJohnathan Corgan2010-04-26 22:13:34 -0700
commita178f23be75489c1507060a8b0e9e59cd358dfba (patch)
treea4cfa45867f51df5fdc0e1851c6688d7346f34ad /gr-audio-portaudio
parent665d7a850eeb46bfc6388a98e4a4107648cb6add (diff)
downloadgnuradio-a178f23be75489c1507060a8b0e9e59cd358dfba.tar.gz
gnuradio-a178f23be75489c1507060a8b0e9e59cd358dfba.tar.bz2
gnuradio-a178f23be75489c1507060a8b0e9e59cd358dfba.zip
Convert gr-audio-portaudio to Boost via gruel
Remove omnithread dependency in build for gr-audio-portaudio Remove unused debugging utility class in gnuradio-core (gri_logger)
Diffstat (limited to 'gr-audio-portaudio')
-rw-r--r--gr-audio-portaudio/src/Makefile.am1
-rw-r--r--gr-audio-portaudio/src/audio_portaudio_sink.cc67
-rw-r--r--gr-audio-portaudio/src/audio_portaudio_sink.h6
-rw-r--r--gr-audio-portaudio/src/audio_portaudio_source.cc82
-rw-r--r--gr-audio-portaudio/src/audio_portaudio_source.h13
5 files changed, 97 insertions, 72 deletions
diff --git a/gr-audio-portaudio/src/Makefile.am b/gr-audio-portaudio/src/Makefile.am
index 20213d16b..1df6bbcea 100644
--- a/gr-audio-portaudio/src/Makefile.am
+++ b/gr-audio-portaudio/src/Makefile.am
@@ -46,7 +46,6 @@ libgnuradio_audio_portaudio_la_SOURCES = \
libgnuradio_audio_portaudio_la_LIBADD = \
$(GNURADIO_CORE_LA) \
- $(OMNITHREAD_LA) \
$(PORTAUDIO_LIBS)
libgnuradio_audio_portaudio_la_LDFLAGS = \
diff --git a/gr-audio-portaudio/src/audio_portaudio_sink.cc b/gr-audio-portaudio/src/audio_portaudio_sink.cc
index 2e48feb4a..65a38f911 100644
--- a/gr-audio-portaudio/src/audio_portaudio_sink.cc
+++ b/gr-audio-portaudio/src/audio_portaudio_sink.cc
@@ -32,7 +32,6 @@
#include <unistd.h>
#include <stdexcept>
#include <gri_portaudio.h>
-#include <gnuradio/omnithread.h>
#include <string.h>
//#define LOGGING 0 // define to 0 or 1
@@ -84,31 +83,33 @@ portaudio_sink_callback (const void *inputBuffer,
int navail_samples = self->d_reader->items_available();
- if (nreqd_samples <= navail_samples){ // We've got enough data...
- //if (LOGGING)
- // self->d_log->printf("PAsink cb: f/b = %4ld\n", framesPerBuffer);
- // copy from ringbuffer into output buffer
- memcpy(outputBuffer,
- self->d_reader->read_pointer(),
- nreqd_samples * sizeof(sample_t));
- self->d_reader->update_read_pointer(nreqd_samples);
-
+ 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_ready.post();
+ self->d_ringbuffer_cond.notify_one();
return paContinue;
}
else { // underrun
- //if (LOGGING)
- // self->d_log->printf("PAsink cb: f/b = %4ld UNDERRUN\n", framesPerBuffer);
-
self->d_nunderuns++;
::write(2, "aU", 2); // FIXME change to non-blocking call
// FIXME we should transfer what we've got and pad the rest
memset(outputBuffer, 0, nreqd_samples * sizeof(sample_t));
- self->d_ringbuffer_ready.post(); // Tell the sink to get going!
+ self->d_ringbuffer_ready = true;
+ self->d_ringbuffer_cond.notify_one(); // Tell the sink to get going!
+
return paContinue;
}
}
@@ -135,7 +136,9 @@ audio_portaudio_sink::audio_portaudio_sink(int sampling_rate,
d_verbose(gr_prefs::singleton()->get_bool("audio_portaudio", "verbose", false)),
d_portaudio_buffer_size_frames(0),
d_stream(0),
- d_ringbuffer_ready(1, 1), // binary semaphore
+ d_ringbuffer_mutex(),
+ d_ringbuffer_cond(),
+ d_ringbuffer_ready(false),
d_nunderuns(0)
{
memset(&d_output_parameters, 0, sizeof(d_output_parameters));
@@ -297,12 +300,17 @@ audio_portaudio_sink::work (int noutput_items,
const unsigned nchan = d_output_parameters.channelCount; // # of channels == samples/frame
int k;
- for (k = 0; k < noutput_items; ){
+ 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){
- d_ringbuffer_ready.wait(); // block here, then try again
+ {
+ gruel::scoped_lock guard(d_ringbuffer_mutex);
+ while (!d_ringbuffer_ready)
+ d_ringbuffer_cond.wait(guard);
+ }
+
continue;
}
else {
@@ -316,16 +324,21 @@ audio_portaudio_sink::work (int noutput_items,
}
// We can write the smaller of the request and the room we've got
- 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];
- }
+ {
+ 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;
}
- d_writer->update_write_pointer(nf * nchan);
- k += nf;
}
return k; // tell how many we actually did
diff --git a/gr-audio-portaudio/src/audio_portaudio_sink.h b/gr-audio-portaudio/src/audio_portaudio_sink.h
index 1a0729799..71cbfcf9f 100644
--- a/gr-audio-portaudio/src/audio_portaudio_sink.h
+++ b/gr-audio-portaudio/src/audio_portaudio_sink.h
@@ -24,7 +24,7 @@
#include <gr_sync_block.h>
#include <gr_buffer.h>
-#include <gnuradio/omnithread.h>
+#include <gruel/thread.h>
#include <string>
#include <portaudio.h>
#include <stdexcept>
@@ -74,8 +74,10 @@ class audio_portaudio_sink : public gr_sync_block {
gr_buffer_sptr d_writer; // buffer used between work and callback
gr_buffer_reader_sptr d_reader;
- omni_semaphore d_ringbuffer_ready; // binary semaphore
+ gruel::mutex d_ringbuffer_mutex;
+ gruel::condition_variable d_ringbuffer_cond;
+ bool d_ringbuffer_ready;
// random stats
int d_nunderuns; // count of underruns
diff --git a/gr-audio-portaudio/src/audio_portaudio_source.cc b/gr-audio-portaudio/src/audio_portaudio_source.cc
index 9e883ad8a..484b7f1e5 100644
--- a/gr-audio-portaudio/src/audio_portaudio_source.cc
+++ b/gr-audio-portaudio/src/audio_portaudio_source.cc
@@ -32,7 +32,6 @@
#include <unistd.h>
#include <stdexcept>
#include <gri_portaudio.h>
-#include <gnuradio/omnithread.h>
#include <string.h>
//#define LOGGING 0 // define to 0 or 1
@@ -88,32 +87,28 @@ portaudio_source_callback (const void *inputBuffer,
// self->d_log->printf("PAsrc cb: f/b = %4ld\n", framesPerBuffer);
// copy from input buffer to ringbuffer
- memcpy(self->d_writer->write_pointer(),
- inputBuffer,
- nframes_to_copy * nchan * sizeof(sample_t));
- self->d_writer->update_write_pointer(nframes_to_copy * nchan);
+ {
+ 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.post();
+ // 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
- //if (LOGGING)
- // self->d_log->printf("PAsrc cb: f/b = %4ld OVERRUN\n", framesPerBuffer);
-
self->d_noverruns++;
::write(2, "aO", 2); // FIXME change to non-blocking call
-#if 0
- // copy any frames that will fit
- memcpy(self->d_writer->write_pointer(),
- inputBuffer,
- nframes_room * nchan * sizeof(sample_t));
- self->d_writer->update_write_pointer(nframes_room * nchan);
-#endif
-
- self->d_ringbuffer_ready.post(); // Tell the sink to get going!
+ self->d_ringbuffer_ready = false;
+ self->d_ringbuffer_cond.notify_one(); // Tell the sink to get going!
return paContinue;
}
}
@@ -140,7 +135,9 @@ audio_portaudio_source::audio_portaudio_source(int sampling_rate,
d_verbose(gr_prefs::singleton()->get_bool("audio_portaudio", "verbose", false)),
d_portaudio_buffer_size_frames(0),
d_stream(0),
- d_ringbuffer_ready(1, 1), // binary semaphore
+ d_ringbuffer_mutex(),
+ d_ringbuffer_cond(),
+ d_ringbuffer_ready(false),
d_noverruns(0)
{
memset(&d_input_parameters, 0, sizeof(d_input_parameters));
@@ -303,11 +300,13 @@ audio_portaudio_source::work (int noutput_items,
if (k > 0) // If we've produced anything so far, return that
return k;
- if (d_ok_to_block){
- d_ringbuffer_ready.wait(); // block here, then try again
+ 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.
@@ -320,27 +319,38 @@ audio_portaudio_source::work (int noutput_items,
// FIXME We'll fill with zeros for now. Yes, it will "click"...
// Fill with some frames of zeros
- 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;
+ {
+ 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;
}
- k += nf;
- return k;
}
// We can read the smaller of the request and what's in the buffer.
- int nf = std::min(noutput_items - k, nframes);
+ {
+ gruel::scoped_lock guard(d_ringbuffer_mutex);
- 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++;
+ 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;
}
- d_reader->update_read_pointer(nf * nchan);
- k += nf;
}
return k; // tell how many we actually did
diff --git a/gr-audio-portaudio/src/audio_portaudio_source.h b/gr-audio-portaudio/src/audio_portaudio_source.h
index 3102db739..31e70a127 100644
--- a/gr-audio-portaudio/src/audio_portaudio_source.h
+++ b/gr-audio-portaudio/src/audio_portaudio_source.h
@@ -1,6 +1,6 @@
/* -*- c++ -*- */
/*
- * Copyright 2006 Free Software Foundation, Inc.
+ * Copyright 2006.2010 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
@@ -24,11 +24,10 @@
#include <gr_sync_block.h>
#include <gr_buffer.h>
-#include <gnuradio/omnithread.h>
+#include <gruel/thread.h>
#include <string>
#include <portaudio.h>
#include <stdexcept>
-//#include <gri_logger.h>
class audio_portaudio_source;
typedef boost::shared_ptr<audio_portaudio_source> audio_portaudio_source_sptr;
@@ -74,11 +73,13 @@ class audio_portaudio_source : public gr_sync_block {
gr_buffer_sptr d_writer; // buffer used between work and callback
gr_buffer_reader_sptr d_reader;
- omni_semaphore d_ringbuffer_ready; // binary semaphore
+
+ gruel::mutex d_ringbuffer_mutex;
+ gruel::condition_variable d_ringbuffer_cond;
+ bool d_ringbuffer_ready;
// random stats
int d_noverruns; // count of overruns
- //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);
@@ -87,7 +88,7 @@ class audio_portaudio_source : public gr_sync_block {
protected:
audio_portaudio_source (int sampling_rate, const std::string device_name,
- bool ok_to_block);
+ bool ok_to_block);
public:
~audio_portaudio_source ();