summaryrefslogtreecommitdiff
path: root/gr-audio-portaudio/src
diff options
context:
space:
mode:
Diffstat (limited to 'gr-audio-portaudio/src')
-rw-r--r--gr-audio-portaudio/src/Makefile.am4
-rw-r--r--gr-audio-portaudio/src/audio_portaudio_sink.cc71
-rw-r--r--gr-audio-portaudio/src/audio_portaudio_sink.h6
-rw-r--r--gr-audio-portaudio/src/audio_portaudio_source.cc86
-rw-r--r--gr-audio-portaudio/src/audio_portaudio_source.h13
-rwxr-xr-xgr-audio-portaudio/src/qa_portaudio.py6
6 files changed, 105 insertions, 81 deletions
diff --git a/gr-audio-portaudio/src/Makefile.am b/gr-audio-portaudio/src/Makefile.am
index 20213d16b..4fa098dcc 100644
--- a/gr-audio-portaudio/src/Makefile.am
+++ b/gr-audio-portaudio/src/Makefile.am
@@ -46,11 +46,9 @@ libgnuradio_audio_portaudio_la_SOURCES = \
libgnuradio_audio_portaudio_la_LIBADD = \
$(GNURADIO_CORE_LA) \
- $(OMNITHREAD_LA) \
$(PORTAUDIO_LIBS)
-libgnuradio_audio_portaudio_la_LDFLAGS = \
- $(NO_UNDEFINED)
+libgnuradio_audio_portaudio_la_LDFLAGS = $(NO_UNDEFINED) $(LTVERSIONFLAGS)
if PYTHON
diff --git a/gr-audio-portaudio/src/audio_portaudio_sink.cc b/gr-audio-portaudio/src/audio_portaudio_sink.cc
index 2e48feb4a..9a3baf40e 100644
--- a/gr-audio-portaudio/src/audio_portaudio_sink.cc
+++ b/gr-audio-portaudio/src/audio_portaudio_sink.cc
@@ -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
*
@@ -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;
}
}
@@ -119,7 +120,7 @@ portaudio_sink_callback (const void *inputBuffer,
audio_portaudio_sink_sptr
audio_portaudio_make_sink (int sampling_rate, const std::string dev, bool ok_to_block)
{
- return audio_portaudio_sink_sptr (new audio_portaudio_sink (sampling_rate,
+ return gnuradio::get_initial_sptr(new audio_portaudio_sink (sampling_rate,
dev, ok_to_block));
}
@@ -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..23801a8bf 100644
--- a/gr-audio-portaudio/src/audio_portaudio_source.cc
+++ b/gr-audio-portaudio/src/audio_portaudio_source.cc
@@ -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
*
@@ -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;
}
}
@@ -124,7 +119,7 @@ portaudio_source_callback (const void *inputBuffer,
audio_portaudio_source_sptr
audio_portaudio_make_source (int sampling_rate, const std::string dev, bool ok_to_block)
{
- return audio_portaudio_source_sptr (new audio_portaudio_source (sampling_rate,
+ return gnuradio::get_initial_sptr(new audio_portaudio_source (sampling_rate,
dev, ok_to_block));
}
@@ -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 ();
diff --git a/gr-audio-portaudio/src/qa_portaudio.py b/gr-audio-portaudio/src/qa_portaudio.py
index f6a54061e..20731f38d 100755
--- a/gr-audio-portaudio/src/qa_portaudio.py
+++ b/gr-audio-portaudio/src/qa_portaudio.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
-# Copyright 2005 Free Software Foundation, Inc.
+# Copyright 2005,2010 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
@@ -23,7 +23,7 @@
from gnuradio import gr, gr_unittest
import audio_portaudio
-class qa_portaudio (gr_unittest.TestCase):
+class test_audio_portaudio (gr_unittest.TestCase):
def setUp (self):
self.tb = gr.top_block ()
@@ -37,4 +37,4 @@ class qa_portaudio (gr_unittest.TestCase):
pass
if __name__ == '__main__':
- gr_unittest.main ()
+ gr_unittest.run(test_audio_portaudio, "test_audio_portaudio.xml")