Skip to content

Commit

Permalink
Implemented AlsaInput#avail AlsaInput#delay
Browse files Browse the repository at this point in the history
  • Loading branch information
wedesoft committed Nov 15, 2010
1 parent 6dec782 commit 6b4046e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]
Expand Down
56 changes: 56 additions & 0 deletions ext/alsainput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 );
}

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions ext/alsainput.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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;
Expand Down

0 comments on commit 6b4046e

Please sign in to comment.