From 79822038bcad765c48f6004c428d65dd3c483f2e Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Thu, 21 Mar 2013 19:41:54 -0500 Subject: [PATCH] add seek to src/io/source* --- src/io/source.c | 9 +++++++++ src/io/source.h | 12 ++++++++++++ src/io/source_apple_audio.c | 13 +++++++++++-- src/io/source_apple_audio.h | 12 ++++++++++++ src/io/source_sndfile.c | 5 +++++ src/io/source_sndfile.h | 12 ++++++++++++ 6 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/io/source.c b/src/io/source.c index aa91cbb6..5e932913 100644 --- a/src/io/source.c +++ b/src/io/source.c @@ -81,3 +81,12 @@ uint_t aubio_source_get_samplerate(aubio_source_t * s) { #endif /* __APPLE__ */ } +uint_t aubio_source_seek (aubio_source_t * s, uint_t seek ) { +#ifdef __APPLE__ + return aubio_source_apple_audio_seek ((aubio_source_apple_audio_t *)s->source, seek); +#else /* __APPLE__ */ +#if HAVE_SNDFILE + return aubio_source_sndfile_seek ((aubio_source_sndfile_t *)s->source, seek); +#endif /* HAVE_SNDFILE */ +#endif /* __APPLE__ */ +} diff --git a/src/io/source.h b/src/io/source.h index 76fa434c..d8ef74ec 100644 --- a/src/io/source.h +++ b/src/io/source.h @@ -77,6 +77,18 @@ void aubio_source_do(aubio_source_t * s, fvec_t * read_to, uint_t * read); */ uint_t aubio_source_get_samplerate(aubio_source_t * s); +/** + + seek source object + + \param s source object, created with ::new_aubio_source + \param pos position to seek to, in frames + + \return 0 if sucessful, non-zero on failure + +*/ +uint_t aubio_source_seek (aubio_source_t * s, uint_t pos); + /** close source and cleanup memory diff --git a/src/io/source_apple_audio.c b/src/io/source_apple_audio.c index 71f90aa9..fb310bfd 100644 --- a/src/io/source_apple_audio.c +++ b/src/io/source_apple_audio.c @@ -36,7 +36,8 @@ struct _aubio_source_apple_audio_t { uint_t channels; - uint_t samplerate; + uint_t samplerate; //< requested samplerate + uint_t source_samplerate; //< actual source samplerate uint_t block_size; char_t *path; @@ -80,6 +81,7 @@ aubio_source_apple_audio_t * new_aubio_source_apple_audio(char_t * path, uint_t //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate); } s->samplerate = samplerate; + s->source_samplerate = fileFormat.mSampleRate; AudioStreamBasicDescription clientFormat; propSize = sizeof(clientFormat); @@ -143,7 +145,7 @@ beach: void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) { UInt32 c, v, loadedPackets = s->block_size; OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList); - if (err) { AUBIO_ERROR("error in ExtAudioFileRead, %d\n", (int)err); goto beach;} + if (err) { AUBIO_ERROR("error in ExtAudioFileRead %s %d\n", s->path, (int)err); goto beach;} short *data = (short*)s->bufferList.mBuffers[0].mData; @@ -181,6 +183,13 @@ void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){ return; } +uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos) { + Float64 ratio = (Float64)(s->source_samplerate) / (Float64)(s->samplerate); + OSStatus err = ExtAudioFileSeek(s->audioFile, pos); + if (err) AUBIO_ERROR("source_apple_audio: error in ExtAudioFileSeek (%d)\n", (int)err); + return err; +} + uint_t aubio_source_apple_audio_get_samplerate(aubio_source_apple_audio_t * s) { return s->samplerate; } diff --git a/src/io/source_apple_audio.h b/src/io/source_apple_audio.h index 7af7e085..ee2c625b 100644 --- a/src/io/source_apple_audio.h +++ b/src/io/source_apple_audio.h @@ -83,6 +83,18 @@ void aubio_source_apple_audio_do(aubio_source_apple_audio_t * s, fvec_t * read_t */ uint_t aubio_source_apple_audio_get_samplerate(aubio_source_apple_audio_t * s); +/** + + seek source object + + \param s source object, created with ::new_aubio_source + \param pos position to seek to, in frames + + \return 0 if sucessful, non-zero on failure + +*/ +uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos); + /** close source and cleanup memory diff --git a/src/io/source_sndfile.c b/src/io/source_sndfile.c index 8472e2dd..a21cf606 100644 --- a/src/io/source_sndfile.c +++ b/src/io/source_sndfile.c @@ -176,6 +176,11 @@ uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) { return s->samplerate; } +uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) { + uint_t resampled_pos = (uint_t)ROUND(pos * s->input_samplerate * 1. / s->samplerate); + return sf_seek (s->handle, resampled_pos, SEEK_SET); +} + void del_aubio_source_sndfile(aubio_source_sndfile_t * s){ if (!s) return; if (sf_close(s->handle)) { diff --git a/src/io/source_sndfile.h b/src/io/source_sndfile.h index 80c89131..3aec3291 100644 --- a/src/io/source_sndfile.h +++ b/src/io/source_sndfile.h @@ -82,6 +82,18 @@ void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_to, uint_ */ uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s); +/** + + seek source object + + \param s source object, created with ::new_aubio_source_sndfile + \param pos position to seek to, in frames + + \return 0 if sucessful, non-zero on failure + +*/ +uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t *s, uint_t pos); + /** close source and cleanup memory -- 2.26.2