shithub: aubio

Download patch

ref: 79822038bcad765c48f6004c428d65dd3c483f2e
parent: 2d3212aaa3cd67f59b3454c6263cf685a35e3d23
author: Paul Brossier <piem@piem.org>
date: Thu Mar 21 15:41:54 EDT 2013

add seek to src/io/source*

--- a/src/io/source.c
+++ b/src/io/source.c
@@ -81,3 +81,12 @@
 #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__ */
+}
--- a/src/io/source.h
+++ b/src/io/source.h
@@ -79,6 +79,18 @@
 
 /**
 
+  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
 
   \param s source object, created with ::new_aubio_source
--- 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_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 @@
 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;
 
@@ -179,6 +181,13 @@
   freeAudioBufferList(&s->bufferList);
   AUBIO_FREE(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) {
--- a/src/io/source_apple_audio.h
+++ b/src/io/source_apple_audio.h
@@ -85,6 +85,18 @@
 
 /**
 
+  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
 
   \param s source object, created with ::new_aubio_source_apple_audio
--- a/src/io/source_sndfile.c
+++ b/src/io/source_sndfile.c
@@ -176,6 +176,11 @@
   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)) {
--- a/src/io/source_sndfile.h
+++ b/src/io/source_sndfile.h
@@ -84,6 +84,18 @@
 
 /**
 
+  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
 
   \param s source object, created with ::new_aubio_source_sndfile