Permalink
Browse files

Added 'AlsaOutput#prepare' again.

  • Loading branch information...
1 parent c0e05b3 commit bdbff81c3abce527414dacb5a8a9b90296b7c336 @wedesoft committed Nov 13, 2012
Showing with 29 additions and 5 deletions.
  1. +1 −1 Rakefile
  2. +26 −4 ext/alsaoutput.cc
  3. +2 −0 ext/alsaoutput.hh
View
@@ -7,7 +7,7 @@ require 'rake/loaders/makefile'
require 'rbconfig'
PKG_NAME = 'hornetseye-alsa'
-PKG_VERSION = '1.1.0'
+PKG_VERSION = '1.1.1'
CFG = RbConfig::CONFIG
CXX = ENV[ 'CXX' ] || 'g++'
RB_FILES = FileList[ 'lib/**/*.rb' ]
View
@@ -92,7 +92,7 @@ void AlsaOutput::write(SequencePtr frame) throw (Error)
int n = frame->size() / (2 * m_channels);
lock();
if (!m_data.get()) {
- if (m_threadInitialised) pthread_detach(m_thread);
+ if (m_threadInitialised) pthread_join(m_thread, NULL);
while(m_size < n) m_size = 2 * m_size;
m_data = boost::shared_array<short int>(new short int[m_size * m_channels]);
m_start = 0;
@@ -145,6 +145,17 @@ void AlsaOutput::drain(void) throw (Error)
snd_pcm_drain(m_pcmHandle);
}
+void AlsaOutput::prepare(void) throw (Error)
+{
+ ERRORMACRO( m_pcmHandle != NULL, Error, , "PCM device \"" << m_pcmName
+ << "\" is not open. Did you call \"close\" before?" );
+
+ int err = snd_pcm_prepare( m_pcmHandle );
+ ERRORMACRO( err >= 0, Error, , "Error preparing PCM device \"" << m_pcmName
+ << "\": " << snd_strerror( err ) );
+ // pthread_mutex_lock( &m_mutex );
+}
+
unsigned int AlsaOutput::rate(void)
{
return m_rate;
@@ -215,6 +226,7 @@ void AlsaOutput::threadFunc(void)
{
bool quit = false;
while (!quit) {
+ snd_pcm_wait(m_pcmHandle, 1000);
try {
lock();
int n = m_periodSize;
@@ -225,15 +237,13 @@ void AlsaOutput::threadFunc(void)
if (m_start + n > m_size) {
writei(m_data.get() + m_start * m_channels, m_size - m_start);
writei(m_data.get(), m_start + n - m_size);
- } else {
+ } else
writei(m_data.get() + m_start * m_channels, n);
- };
m_start += n;
m_count -= n;
} else
quit = true;
unlock();
- snd_pcm_wait(m_pcmHandle, 1000);
} catch (Error &e) {
quit = true;
unlock();
@@ -255,6 +265,7 @@ VALUE AlsaOutput::registerRubyClass( VALUE rbModule )
rb_define_method( cRubyClass, "write", RUBY_METHOD_FUNC( wrapWrite ), 1 );
rb_define_method( cRubyClass, "drop", RUBY_METHOD_FUNC( wrapDrop ), 0 );
rb_define_method( cRubyClass, "drain", RUBY_METHOD_FUNC( wrapDrain ), 0 );
+ rb_define_method( cRubyClass, "prepare", RUBY_METHOD_FUNC( wrapPrepare ), 0 );
rb_define_method( cRubyClass, "rate", RUBY_METHOD_FUNC( wrapRate ), 0 );
rb_define_method( cRubyClass, "channels", RUBY_METHOD_FUNC( wrapChannels ), 0 );
rb_define_method( cRubyClass, "delay", RUBY_METHOD_FUNC( wrapDelay ), 0 );
@@ -321,6 +332,17 @@ VALUE AlsaOutput::wrapDrain( VALUE rbSelf )
return rbSelf;
}
+VALUE AlsaOutput::wrapPrepare( VALUE rbSelf )
+{
+ try {
+ AlsaOutputPtr *self; Data_Get_Struct( rbSelf, AlsaOutputPtr, self );
+ (*self)->prepare();
+ } catch ( exception &e ) {
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
+ };
+ return rbSelf;
+}
+
VALUE AlsaOutput::wrapRate( VALUE rbSelf )
{
AlsaOutputPtr *self; Data_Get_Struct( rbSelf, AlsaOutputPtr, self );
View
@@ -32,6 +32,7 @@ public:
void write( SequencePtr sequence ) throw (Error);
void drop(void) throw (Error);
void drain(void) throw (Error);
+ void prepare(void) throw (Error);
unsigned int rate(void);
unsigned int channels(void);
int delay(void) throw (Error);
@@ -46,6 +47,7 @@ public:
static VALUE wrapWrite( VALUE rbSelf, VALUE rbSequence );
static VALUE wrapDrop( VALUE rbSelf );
static VALUE wrapDrain( VALUE rbSelf );
+ static VALUE wrapPrepare( VALUE rbSelf );
static VALUE wrapRate( VALUE rbSelf );
static VALUE wrapChannels( VALUE rbSelf );
static VALUE wrapDelay( VALUE rbSelf );

0 comments on commit bdbff81

Please sign in to comment.