shithub: aubio

Download patch

ref: db120acffbf8fa0cc71e8bc8fba056c60a5f9ec1
parent: 54a2ca2f34f36fd9fedb79b07575541a68eee65c
parent: 6bbdcff5161f4e9c3cd1fb9e90fd802f8509a0b9
author: Paul Brossier <piem@piem.org>
date: Fri Mar 22 14:04:09 EDT 2013

Merge /Users/piem/projects/aubio/aubio into device

--- a/src/io/source.h
+++ b/src/io/source.h
@@ -25,7 +25,10 @@
 
   Media source to read blocks of consecutive audio samples from file
 
+  To write to file, use ::aubio_sink_t.
+
   \example io/test-source.c
+  \example io/test-source_multi.c
 
 */
 
--- a/src/io/source_apple_audio.c
+++ b/src/io/source_apple_audio.c
@@ -51,15 +51,30 @@
 extern void freeAudioBufferList(AudioBufferList *bufferList);
 extern CFURLRef getURLFromPath(const char * path);
 
+uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path);
+
 aubio_source_apple_audio_t * new_aubio_source_apple_audio(char_t * path, uint_t samplerate, uint_t block_size)
 {
   aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
 
-  s->path = path;
   s->block_size = block_size;
+  s->samplerate = samplerate;
 
+  if ( aubio_source_apple_audio_open ( s, path ) ) {
+    goto beach;
+  }
+  return s;
+
+beach:
+  AUBIO_FREE(s);
+  return NULL;
+}
+
+uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path)
+{
   OSStatus err = noErr;
   UInt32 propSize;
+  s->path = path;
 
   // open the resource url
   CFURLRef fileURL = getURLFromPath(path);
@@ -76,11 +91,11 @@
       kExtAudioFileProperty_FileDataFormat, &propSize, &fileFormat);
   if (err) { AUBIO_ERROR("error in ExtAudioFileGetProperty, %d\n", (int)err); goto beach;}
 
-  if (samplerate == 0) {
-    samplerate = fileFormat.mSampleRate;
+  if (s->samplerate == 0) {
+    s->samplerate = fileFormat.mSampleRate;
     //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
   }
-  s->samplerate = samplerate;
+
   s->source_samplerate = fileFormat.mSampleRate;
   s->channels = fileFormat.mChannelsPerFrame;
 
@@ -123,13 +138,13 @@
   }
 
   // compute the size of the segments needed to read the input file
-  UInt32 samples = s->block_size * clientFormat.mChannelsPerFrame;
-  Float64 rateRatio = clientFormat.mSampleRate / fileFormat.mSampleRate;
+  UInt32 samples = s->block_size * s->channels;
+  Float64 rateRatio = s->samplerate / s->source_samplerate;
   uint_t segmentSize= (uint_t)(samples * rateRatio + .5);
   if (rateRatio < 1.) {
     segmentSize = (uint_t)(samples / rateRatio + .5);
   } else if (rateRatio > 1.) {
-    AUBIO_WRN("up-sampling %s from %0.2fHz to %0.2fHz\n", s->path, fileFormat.mSampleRate, clientFormat.mSampleRate);
+    AUBIO_WRN("up-sampling %s from %0dHz to %0dHz\n", s->path, s->source_samplerate, s->samplerate);
   } else {
     assert ( segmentSize == samples );
     //AUBIO_DBG("not resampling, segmentSize %d, block_size %d\n", segmentSize, s->block_size);
@@ -136,13 +151,13 @@
   }
 
   // allocate the AudioBufferList
-  if (createAubioBufferList(&s->bufferList, s->channels, segmentSize)) err = -1;
+  if (createAubioBufferList(&s->bufferList, s->channels, segmentSize)) {
+    AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
+    goto beach;
+  }
 
-  return s;
- 
 beach:
-  AUBIO_FREE(s);
-  return NULL;
+  return err;
 }
 
 void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) {
@@ -204,12 +219,18 @@
   return;
 }
 
-void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
+uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s)
+{
   OSStatus err = noErr;
-  if (!s || !s->audioFile) { return; }
+  if (!s || !s->audioFile) { return 1; }
   err = ExtAudioFileDispose(s->audioFile);
   if (err) AUBIO_ERROR("error in ExtAudioFileDispose, %d\n", (int)err);
   s->audioFile = NULL;
+  return err;
+}
+
+void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
+  aubio_source_apple_audio_close (s);
   freeAudioBufferList(&s->bufferList);
   AUBIO_FREE(s);
   return;
--- /dev/null
+++ b/tests/src/io/test-source_seek.c
@@ -1,0 +1,63 @@
+#include <aubio.h>
+#include "utils_tests.h"
+
+int main (int argc, char **argv)
+{
+  uint_t err = 0;
+  if (argc < 2) {
+    err = 2;
+    PRINT_ERR("not enough arguments\n");
+    PRINT_MSG("read a wave file as a mono vector\n");
+    PRINT_MSG("usage: %s <source_path> [samplerate] [hop_size]\n", argv[0]);
+    PRINT_MSG("examples:\n");
+    PRINT_MSG(" - read file.wav at original samplerate\n");
+    PRINT_MSG("       %s file.wav\n", argv[0]);
+    PRINT_MSG(" - read file.wav at 32000Hz\n");
+    PRINT_MSG("       %s file.aif 32000\n", argv[0]);
+    PRINT_MSG(" - read file.wav at original samplerate with 4096 blocks\n");
+    PRINT_MSG("       %s file.wav 0 4096 \n", argv[0]);
+    return err;
+  }
+
+  uint_t samplerate = 0;
+  uint_t hop_size = 256;
+  uint_t n_frames = 0, read = 0;
+  if ( argc == 3 ) samplerate = atoi(argv[2]);
+  if ( argc == 4 ) hop_size = atoi(argv[3]);
+
+  char_t *source_path = argv[1];
+
+  fvec_t *vec = new_fvec(hop_size);
+
+  aubio_source_t* s = new_aubio_source(source_path, samplerate, hop_size);
+  if (!s) { err = 1; goto beach; }
+
+  if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(s);
+
+  do {
+    aubio_source_do(s, vec, &read);
+    //fvec_print (vec);
+    n_frames += read;
+  } while ( read == hop_size );
+
+  PRINT_MSG("read %d frames at %dHz (%d blocks) from %s\n", n_frames, samplerate,
+    n_frames / hop_size, source_path);
+
+  aubio_source_seek (s, 0);
+
+  n_frames = 0;
+  do {
+    aubio_source_do(s, vec, &read);
+    //fvec_print (vec);
+    n_frames += read;
+  } while ( read == hop_size );
+
+  PRINT_MSG("read %d frames at %dHz (%d blocks) from %s\n", n_frames, samplerate,
+    n_frames / hop_size, source_path);
+
+  del_aubio_source (s);
+beach:
+  del_fvec (vec);
+
+  return err;
+}