Permalink
Browse files

Implemented AlsaInput#avail AlsaInput#delay

  • Loading branch information...
1 parent 6dec782 commit 6b4046e4fcce23690f106b3e6d482621d4b98d42 @wedesoft committed Nov 15, 2010
Showing with 61 additions and 1 deletion.
  1. +1 −1 Rakefile
  2. +56 −0 ext/alsainput.cc
  3. +4 −0 ext/alsainput.hh
View
2 Rakefile
@@ -7,7 +7,7 @@ require 'rake/loaders/makefile'
require 'rbconfig'
PKG_NAME = 'hornetseye-alsa'
-PKG_VERSION = '0.2.0'
+PKG_VERSION = '0.3.0'
CFG = RbConfig::CONFIG
CXX = ENV[ 'CXX' ] || 'g++'
RB_FILES = FileList[ 'lib/**/*.rb' ]
View
56 ext/alsainput.cc
@@ -105,6 +105,36 @@ unsigned int AlsaInput::channels(void)
return m_channels;
}
+int AlsaInput::avail(void) throw (Error)
+{
+ ERRORMACRO( m_pcmHandle != NULL, Error, , "PCM device \"" << m_pcmName
+ << "\" is not open. Did you call \"close\" before?" );
+ snd_pcm_sframes_t frames;
+ int err = 0;
+ while ( ( frames = snd_pcm_avail( m_pcmHandle ) ) < 0 ) {
+ err = snd_pcm_recover( m_pcmHandle, frames, 1 );
+ ERRORMACRO( err >= 0, Error, , "Error querying number of available frames for "
+ "retrieval from PCM device \"" << m_pcmName << "\": "
+ << snd_strerror( err ) );
+ };
+ return frames;
+}
+
+int AlsaInput::delay(void) throw (Error)
+{
+ ERRORMACRO( m_pcmHandle != NULL, Error, , "PCM device \"" << m_pcmName
+ << "\" is not open. Did you call \"close\" before?" );
+ snd_pcm_sframes_t frames;
+ int err;
+ while ( ( err = snd_pcm_delay( m_pcmHandle, &frames ) ) < 0 ) {
+ err = snd_pcm_recover( m_pcmHandle, err, 1 );
+ ERRORMACRO( err >= 0, Error, , "Error querying number of available frames for "
+ "capture on PCM device \"" << m_pcmName << "\": "
+ << snd_strerror( err ) );
+ };
+ return frames;
+}
+
void AlsaInput::prepare(void) throw (Error)
{
ERRORMACRO( m_pcmHandle != NULL, Error, , "PCM device \"" << m_pcmName
@@ -123,6 +153,8 @@ VALUE AlsaInput::registerRubyClass( VALUE rbModule )
rb_define_method( cRubyClass, "read", RUBY_METHOD_FUNC( wrapRead ), 1 );
rb_define_method( cRubyClass, "rate", RUBY_METHOD_FUNC( wrapRate ), 0 );
rb_define_method( cRubyClass, "channels", RUBY_METHOD_FUNC( wrapChannels ), 0 );
+ rb_define_method( cRubyClass, "avail", RUBY_METHOD_FUNC( wrapAvail ), 0 );
+ rb_define_method( cRubyClass, "delay", RUBY_METHOD_FUNC( wrapDelay ), 0 );
rb_define_method( cRubyClass, "prepare", RUBY_METHOD_FUNC( wrapPrepare ), 0 );
}
@@ -180,6 +212,30 @@ VALUE AlsaInput::wrapChannels( VALUE rbSelf )
return UINT2NUM( (*self)->channels() );
}
+VALUE AlsaInput::wrapAvail( VALUE rbSelf )
+{
+ VALUE rbRetVal = Qnil;
+ try {
+ AlsaInputPtr *self; Data_Get_Struct( rbSelf, AlsaInputPtr, self );
+ rbRetVal = INT2NUM( (*self)->avail() );
+ } catch ( exception &e ) {
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
+ };
+ return rbRetVal;
+}
+
+VALUE AlsaInput::wrapDelay( VALUE rbSelf )
+{
+ VALUE rbRetVal = Qnil;
+ try {
+ AlsaInputPtr *self; Data_Get_Struct( rbSelf, AlsaInputPtr, self );
+ rbRetVal = INT2NUM( (*self)->delay() );
+ } catch ( exception &e ) {
+ rb_raise( rb_eRuntimeError, "%s", e.what() );
+ };
+ return rbRetVal;
+}
+
VALUE AlsaInput::wrapPrepare( VALUE rbSelf )
{
try {
View
4 ext/alsainput.hh
@@ -33,6 +33,8 @@ public:
SequencePtr read( int samples ) throw (Error);
unsigned int rate(void);
unsigned int channels(void);
+ int avail(void) throw (Error);
+ int delay(void) throw (Error);
void prepare(void) throw (Error);
static VALUE cRubyClass;
static VALUE registerRubyClass( VALUE rbModule );
@@ -43,6 +45,8 @@ public:
static VALUE wrapRead( VALUE rbSelf, VALUE rbSamples );
static VALUE wrapRate( VALUE rbSelf );
static VALUE wrapChannels( VALUE rbSelf );
+ static VALUE wrapAvail( VALUE rbSelf );
+ static VALUE wrapDelay( VALUE rbSelf );
static VALUE wrapPrepare( VALUE rbSelf );
protected:
snd_pcm_t *m_pcmHandle;

0 comments on commit 6b4046e

Please sign in to comment.