shithub: aubio

Download patch

ref: 4c4c9f66bf722ba6b704fee887116f2533dd2363
parent: 7d50c5a789705625b5623686b5f7aa56248ddfb0
parent: bf34fbbc908e83d43a4ed9d1ef3d6bb7acaf3ba7
author: Paul Brossier <piem@piem.org>
date: Thu Jul 26 17:30:11 EDT 2012

Merge branch 'io' into develop

--- a/examples/sndfileio.c
+++ /dev/null
@@ -1,254 +1,0 @@
-/*
-  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
-
-  This file is part of aubio.
-
-  aubio is free software: you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation, either version 3 of the License, or
-  (at your option) any later version.
-
-  aubio is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
-
-*/
-
-#include "config.h"
-
-#ifdef HAVE_SNDFILE
-
-#include <string.h>
-
-#include <sndfile.h>
-
-#include "aubio_priv.h"
-#include "fvec.h"
-#include "sndfileio.h"
-#include "mathutils.h"
-
-#define MAX_CHANNELS 6
-#define MAX_SIZE 4096
-
-struct _aubio_sndfile_t {
-        SNDFILE *handle;
-        int samplerate;
-        int channels;
-        int format;
-        float *tmpdata; /** scratch pad for interleaving/deinterleaving. */
-        int size;       /** store the size to check if realloc needed */
-};
-
-aubio_sndfile_t * new_aubio_sndfile_ro(const char* outputname) {
-        aubio_sndfile_t * f = AUBIO_NEW(aubio_sndfile_t);
-        SF_INFO sfinfo;
-        AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
-
-        f->handle = sf_open (outputname, SFM_READ, &sfinfo);
-
-        if (f->handle == NULL) {
-                AUBIO_ERR("Failed opening %s: %s\n", outputname,
-                        sf_strerror (NULL)); /* libsndfile err msg */
-                return NULL;
-        }	
-
-        if (sfinfo.channels > MAX_CHANNELS) { 
-                AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS);
-                return NULL;
-        }
-
-        f->size       = MAX_SIZE*sfinfo.channels;
-        f->tmpdata    = AUBIO_ARRAY(float,f->size);
-        /* get input specs */
-        f->samplerate = sfinfo.samplerate;
-        f->channels   = sfinfo.channels;
-        f->format     = sfinfo.format;
-
-        return f;
-}
-
-int aubio_sndfile_open_wo(aubio_sndfile_t * f, const char* inputname) {
-        SF_INFO sfinfo;
-        AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
-
-        /* define file output spec */
-        sfinfo.samplerate = f->samplerate;
-        sfinfo.channels   = f->channels;
-        sfinfo.format     = f->format;
-
-        if (! (f->handle = sf_open (inputname, SFM_WRITE, &sfinfo))) {
-                AUBIO_ERR("Not able to open output file %s.\n", inputname);
-                AUBIO_ERR("%s\n",sf_strerror (NULL)); /* libsndfile err msg */
-                AUBIO_QUIT(AUBIO_FAIL);
-        }	
-
-        if (sfinfo.channels > MAX_CHANNELS) { 
-                AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS);
-                AUBIO_QUIT(AUBIO_FAIL);
-        }
-        f->size       = MAX_SIZE*sfinfo.channels;
-        f->tmpdata    = AUBIO_ARRAY(float,f->size);
-        return AUBIO_OK;
-}
-
-/* setup file struct from existing one */
-aubio_sndfile_t * new_aubio_sndfile_wo(aubio_sndfile_t * fmodel, const char *outputname) {
-        aubio_sndfile_t * f = AUBIO_NEW(aubio_sndfile_t);
-        f->samplerate    = fmodel->samplerate;
-        f->channels      = 1; //fmodel->channels;
-        f->format        = fmodel->format;
-        aubio_sndfile_open_wo(f, outputname);
-        return f;
-}
-
-
-/* return 0 if properly closed, 1 otherwise */
-int del_aubio_sndfile(aubio_sndfile_t * f) {
-        if (sf_close(f->handle)) {
-                AUBIO_ERR("Error closing file.");
-                puts (sf_strerror (NULL));
-                return 1;
-        }
-        AUBIO_FREE(f->tmpdata);
-        AUBIO_FREE(f);
-        //AUBIO_DBG("File closed.\n");
-        return 0;
-}
-
-/**************************************************************
- *
- * Read write methods 
- *
- */
-
-
-/* read frames from file in data 
- *  return the number of frames actually read */
-int aubio_sndfile_read(aubio_sndfile_t * f, int frames, fvec_t ** read) {
-        sf_count_t read_frames;
-        int i,j, channels = f->channels;
-        int nsamples = frames*channels;
-        int aread;
-        smpl_t *pread;	
-
-        /* allocate data for de/interleaving reallocated when needed. */
-        if (nsamples >= f->size) {
-                AUBIO_ERR("Maximum aubio_sndfile_read buffer size exceeded.");
-                return -1;
-                /*
-                AUBIO_FREE(f->tmpdata);
-                f->tmpdata = AUBIO_ARRAY(float,nsamples);
-                */
-        }
-        //f->size = nsamples;
-
-        /* do actual reading */
-        read_frames = sf_read_float (f->handle, f->tmpdata, nsamples);
-
-        aread = (int)FLOOR(read_frames/(float)channels);
-
-        /* de-interleaving data  */
-        for (i=0; i<channels; i++) {
-                pread = (smpl_t *)fvec_get_data(read[i]);
-                for (j=0; j<aread; j++) {
-                        pread[j] = (smpl_t)f->tmpdata[channels*j+i];
-                }
-        }
-        return aread;
-}
-
-int
-aubio_sndfile_read_mono (aubio_sndfile_t * f, int frames, fvec_t * read)
-{
-  sf_count_t read_frames;
-  int i, j, channels = f->channels;
-  int nsamples = frames * channels;
-  int aread;
-  smpl_t *pread;
-
-  /* allocate data for de/interleaving reallocated when needed. */
-  if (nsamples >= f->size) {
-    AUBIO_ERR ("Maximum aubio_sndfile_read buffer size exceeded.");
-    return -1;
-    /*
-    AUBIO_FREE(f->tmpdata);
-    f->tmpdata = AUBIO_ARRAY(float,nsamples);
-    */
-  }
-  //f->size = nsamples;
-
-  /* do actual reading */
-  read_frames = sf_read_float (f->handle, f->tmpdata, nsamples);
-
-  aread = (int) FLOOR (read_frames / (float) channels);
-
-  /* de-interleaving data  */
-  pread = (smpl_t *) fvec_get_data (read);
-  for (i = 0; i < channels; i++) {
-    for (j = 0; j < aread; j++) {
-      pread[j] += (smpl_t) f->tmpdata[channels * j + i];
-    }
-  }
-  for (j = 0; j < aread; j++) {
-    pread[j] /= (smpl_t)channels;
-  }
-
-  return aread;
-}
-
-/* write 'frames' samples to file from data 
- *   return the number of frames actually written 
- */
-int aubio_sndfile_write(aubio_sndfile_t * f, int frames, fvec_t ** write) {
-        sf_count_t written_frames = 0;
-        int i, j,	channels = f->channels;
-        int nsamples = channels*frames;
-        smpl_t *pwrite;
-
-        /* allocate data for de/interleaving reallocated when needed. */
-        if (nsamples >= f->size) {
-                AUBIO_ERR("Maximum aubio_sndfile_write buffer size exceeded.");
-                return -1;
-                /*
-                AUBIO_FREE(f->tmpdata);
-                f->tmpdata = AUBIO_ARRAY(float,nsamples);
-                */
-        }
-        //f->size = nsamples;
-
-        /* interleaving data  */
-        for (i=0; i<channels; i++) {
-                pwrite = (smpl_t *)fvec_get_data(write[i]);
-                for (j=0; j<frames; j++) {
-                        f->tmpdata[channels*j+i] = (float)pwrite[j];
-                }
-        }
-        written_frames = sf_write_float (f->handle, f->tmpdata, nsamples);
-        return written_frames/channels;
-}
-
-/*******************************************************************
- *
- * Get object info 
- *
- */
-
-uint_t aubio_sndfile_channels(aubio_sndfile_t * f) {
-        return f->channels;
-}
-
-uint_t aubio_sndfile_samplerate(aubio_sndfile_t * f) {
-        return f->samplerate;
-}
-
-void aubio_sndfile_info(aubio_sndfile_t * f) {
-        AUBIO_DBG("srate    : %d\n", f->samplerate);
-        AUBIO_DBG("channels : %d\n", f->channels);
-        AUBIO_DBG("format   : %d\n", f->format);
-}
-
-#endif /* HAVE_SNDFILE */
--- a/examples/sndfileio.h
+++ /dev/null
@@ -1,79 +1,0 @@
-/*
-  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
-
-  This file is part of aubio.
-
-  aubio is free software: you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation, either version 3 of the License, or
-  (at your option) any later version.
-
-  aubio is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
-
-*/
-
-#ifndef SNDFILEIO_H
-#define SNDFILEIO_H
-
-/** @file 
- * sndfile functions
- */
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * sndfile object
- */
-typedef struct _aubio_sndfile_t aubio_sndfile_t;
-/** 
- * Open a sound file for reading
- */
-aubio_sndfile_t * new_aubio_sndfile_ro (const char * inputfile);
-/**
- * Copy file model from previously opened sound file.
- */
-aubio_sndfile_t * new_aubio_sndfile_wo(aubio_sndfile_t * existingfile, const char * outputname);
-/** 
- * Open a sound file for writing
- */
-int aubio_sndfile_open_wo (aubio_sndfile_t * file, const char * outputname);
-/** 
- * Read frames data from file into an array of buffers
- */
-int aubio_sndfile_read(aubio_sndfile_t * file, int frames, fvec_t ** read);
-/** 
- * Read frames data from file into a single buffer
- */
-int aubio_sndfile_read_mono (aubio_sndfile_t * f, int frames, fvec_t * read);
-/** 
- * Write data of length frames to file
- */
-int aubio_sndfile_write(aubio_sndfile_t * file, int frames, fvec_t ** write);
-/**
- * Close file and delete file object
- */
-int del_aubio_sndfile(aubio_sndfile_t * file);
-/**
- * Return some files facts
- */
-void aubio_sndfile_info(aubio_sndfile_t * file);
-/**
- * Return number of channel in file
- */
-uint_t aubio_sndfile_channels(aubio_sndfile_t * file);
-uint_t aubio_sndfile_samplerate(aubio_sndfile_t * file);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
-
--- a/examples/utils.h
+++ b/examples/utils.h
@@ -27,7 +27,6 @@
 #include <string.h>             /* for strcmp */
 #include <aubio.h>
 #include "config.h"
-#include "sndfileio.h"
 #ifdef HAVE_JACK
 #include "jackio.h"
 #endif /* HAVE_JACK */
--- a/examples/wscript_build
+++ b/examples/wscript_build
@@ -1,20 +1,14 @@
 # vim:set syntax=python:
 
 # build examples
-sndfileio = ctx.new_task_gen(features = 'c',
-    includes = '../src',
-    source = ['sndfileio.c'],
-    target = 'sndfileio')
-
 utilsio = ctx.new_task_gen(name = 'utilsio', features = 'c',
       includes = '../src',
-      add_objects = 'sndfileio',
       source = ['utils.c', 'jackio.c'],
-      uselib = ['LASH', 'JACK', 'SNDFILE'],
+      uselib = ['LASH', 'JACK'],
       target = 'utilsio')
 
 # loop over all *.c filenames in examples to build them all
-for target_name in ctx.path.ant_glob('*.c', excl = ['utils.c', 'jackio.c', 'sndfileio.c']):
+for target_name in ctx.path.ant_glob('*.c', excl = ['utils.c', 'jackio.c']):
   ctx.new_task_gen(features = 'c cprogram',
       add_objects = 'utilsio',
       includes = '../src',
--- /dev/null
+++ b/interfaces/python/demo_sink.py
@@ -1,0 +1,17 @@
+#! /usr/bin/python
+
+import sys
+from aubio import source, sink
+
+if __name__ == '__main__':
+  if len(sys.argv) < 2:
+    print 'usage: %s <inputfile> <outputfile>' % sys.argv[0]
+    sys.exit(1)
+  f = source(sys.argv[1], 8000, 256)
+  g = sink(sys.argv[2], 8000)
+  total_frames, read = 0, 256
+  while read:
+    vec, read = f()
+    g(vec, read)
+    total_frames += read
+  print "read", total_frames / float(f.samplerate), "seconds from", f.uri
--- /dev/null
+++ b/interfaces/python/demo_source.py
@@ -1,0 +1,15 @@
+#! /usr/bin/python
+
+import sys
+from aubio import source
+
+if __name__ == '__main__':
+  if len(sys.argv) < 2:
+    print 'usage: %s <inputfile>' % sys.argv[0]
+    sys.exit(1)
+  f = source(sys.argv[1], 8000, 256)
+  total_frames, read = 0, 256
+  while read:
+    vec, read = f()
+    total_frames += read
+  print "read", total_frames / float(f.samplerate), "seconds from", f.uri
--- a/interfaces/python/gen_pyobject.py
+++ b/interfaces/python/gen_pyobject.py
@@ -18,6 +18,11 @@
 maintaining this bizarre file.
 """
 
+param_numbers = {
+  'source': [0, 2],
+  'sink':   [2, 0],
+}
+
 # TODO
 # do function: for now, only the following pattern is supported:
 # void aubio_<foo>_do (aubio_foo_t * o, 
@@ -80,20 +85,21 @@
 # the important bits: the size of the output for each objects. this data should
 # move into the C library at some point.
 defaultsizes = {
-    'resampler':    'input->length * self->ratio',
-    'specdesc':     '1',
-    'onset':        '1',
-    'pitchyin':     '1',
-    'pitchyinfft':  '1',
-    'pitchschmitt': '1',
-    'pitchmcomb':   '1',
-    'pitchfcomb':   '1',
-    'pitch':        '1',
-    'tss':          'self->hop_size',
-    'mfcc':         'self->n_coeffs',
-    'beattracking': 'self->hop_size',
-    'tempo':        '1',
-    'peakpicker':   '1',
+    'resampler':    ['input->length * self->ratio'],
+    'specdesc':     ['1'],
+    'onset':        ['1'],
+    'pitchyin':     ['1'],
+    'pitchyinfft':  ['1'],
+    'pitchschmitt': ['1'],
+    'pitchmcomb':   ['1'],
+    'pitchfcomb':   ['1'],
+    'pitch':        ['1'],
+    'tss':          ['self->win_size', 'self->win_size'],
+    'mfcc':         ['self->n_coeffs'],
+    'beattracking': ['self->hop_size'],
+    'tempo':        ['1'],
+    'peakpicker':   ['1'],
+    'source':       ['self->hop_size', '1'],
 }
 
 # default value for variables
@@ -122,6 +128,7 @@
     'thrs': '0.5',
     'ratio': '0.5',
     'method': '"default"',
+    'uri': '"none"',
     }
 
 # aubio to python
@@ -138,6 +145,7 @@
 aubiovecfrompyobj = {
     'fvec_t*': 'PyAubio_ArrayToCFvec',
     'cvec_t*': 'PyAubio_ArrayToCCvec',
+    'uint_t': '(uint_t)PyInt_AsLong',
 }
 
 # aubio to python
@@ -145,6 +153,8 @@
     'fvec_t*': 'PyAubio_CFvecToArray',
     'cvec_t*': 'PyAubio_CCvecToPyCvec',
     'smpl_t': 'PyFloat_FromDouble',
+    'uint_t*': 'PyInt_FromLong',
+    'uint_t': 'PyInt_FromLong',
 }
 
 def gen_new_init(newfunc, name):
@@ -256,83 +266,128 @@
 """ % locals()
     return s
 
-def gen_do(dofunc, name):
-    funcname = dofunc.split()[1].split('(')[0]
-    doparams = get_params_types_names(dofunc) 
-    # make sure the first parameter is the object
-    assert doparams[0]['type'] == "aubio_"+name+"_t*", \
-        "method is not in 'aubio_<name>_t"
-    # and remove it
-    doparams = doparams[1:]
-    # guess the input/output params, assuming we have less than 3
-    assert len(doparams) > 0, \
-        "no parameters for function do in object %s" % name
-    #assert (len(doparams) <= 2), \
-    #    "more than 3 parameters for do in object %s" % name
+def gen_do_input_params(inputparams):
+  inputdefs = ''
+  parseinput = ''
+  inputrefs = ''
+  inputvecs = ''
+  pytypes = ''
 
-    # build strings for inputs, assuming there is only one input 
-    inputparams = [doparams[0]]
+  if len(inputparams):
     # build the parsing string for PyArg_ParseTuple
-    pytypes = "".join([aubio2pytypes[p['type']] for p in doparams[0:1]])
+    pytypes = "".join([aubio2pytypes[p['type']] for p in inputparams])
 
-    inputdefs = "/* input vectors python prototypes */\n  "
-    inputdefs += "\n  ".join(["PyObject * " + p['name'] + "_obj;" for p in inputparams])
+    inputdefs = "  /* input vectors python prototypes */\n"
+    for p in inputparams:
+      if p['type'] != 'uint_t':
+        inputdefs += "  PyObject * " + p['name'] + "_obj;\n"
 
-    inputvecs = "/* input vectors prototypes */\n  "
+    inputvecs = "  /* input vectors prototypes */\n  "
     inputvecs += "\n  ".join(map(lambda p: p['type'] + ' ' + p['name'] + ";", inputparams))
 
-    parseinput = "/* input vectors parsing */\n  "
+    parseinput = "  /* input vectors parsing */\n  "
     for p in inputparams:
         inputvec = p['name']
-        inputdef = p['name'] + "_obj"
+        if p['type'] != 'uint_t':
+          inputdef = p['name'] + "_obj"
+        else:
+          inputdef = p['name']
         converter = aubiovecfrompyobj[p['type']]
-        parseinput += """%(inputvec)s = %(converter)s (%(inputdef)s);
+        if p['type'] != 'uint_t':
+          parseinput += """%(inputvec)s = %(converter)s (%(inputdef)s);
 
   if (%(inputvec)s == NULL) {
     return NULL;
-  }""" % locals()
+  }
 
+  """ % locals()
+
     # build the string for the input objects references
-    inputrefs = ", ".join(["&" + p['name'] + "_obj" for p in inputparams])
+    inputreflist = []
+    for p in inputparams:
+      if p['type'] != 'uint_t':
+        inputreflist += [ "&" + p['name'] + "_obj" ]
+      else:
+        inputreflist += [ "&" + p['name'] ]
+    inputrefs = ", ".join(inputreflist)
     # end of inputs strings
+  return inputdefs, parseinput, inputrefs, inputvecs, pytypes
 
-    # build strings for outputs
-    outputparams = doparams[1:]
-    if len(outputparams) >= 1:
-        #assert len(outputparams) == 1, \
-        #    "too many output parameters"
-        outputvecs = "\n  /* output vectors prototypes */\n  "
-        outputvecs += "\n  ".join([p['type'] + ' ' + p['name'] + ";" for p in outputparams])
-        params = {
-          'name': p['name'], 'pytype': p['type'], 'autype': p['type'][:-3],
-          'length': defaultsizes[name]}
-        outputcreate = "\n  ".join(["""/* creating output %(name)s as a new_%(autype)s of length %(length)s */""" % \
-            params for p in outputparams]) 
-        outputcreate += "\n"
-        outputcreate += "\n  ".join(["""  %(name)s = new_%(autype)s (%(length)s);""" % \
-            params for p in outputparams]) 
-        returnval = ""
-        if len(outputparams) > 1:
-            returnval += "PyObject *outputs = PyList_New(0);\n"
-            for p in outputparams:
-                returnval += "  PyList_Append( outputs, (PyObject *)" + aubiovectopyobj[p['type']] + " (" + p['name'] + ")" +");\n"
-            returnval += "  return outputs;"
-        else:
-            if defaultsizes[name] == '1':
-                returnval += "return (PyObject *)PyFloat_FromDouble(" + p['name'] + "->data[0])"
-            else:
-                returnval += "return (PyObject *)" + aubiovectopyobj[p['type']] + " (" + p['name'] + ")"
+def gen_do_output_params(outputparams, name):
+  outputvecs = ""
+  outputcreate = ""
+  if len(outputparams):
+    outputvecs = "  /* output vectors prototypes */\n"
+    for p in outputparams:
+      params = {
+        'name': p['name'], 'pytype': p['type'], 'autype': p['type'][:-3],
+        'length': defaultsizes[name].pop(0) }
+      if (p['type'] == 'uint_t*'):
+        outputvecs += '  uint_t' + ' ' + p['name'] + ";\n"
+        outputcreate += "  %(name)s = 0;\n" % params
+      else:
+        outputvecs += "  " + p['type'] + ' ' + p['name'] + ";\n"
+        outputcreate += "  /* creating output %(name)s as a new_%(autype)s of length %(length)s */\n" % params
+        outputcreate += "  %(name)s = new_%(autype)s (%(length)s);\n" % params
+
+  returnval = "";
+  if len(outputparams) > 1:
+    returnval += "  PyObject *outputs = PyList_New(0);\n"
+    for p in outputparams:
+      returnval += "  PyList_Append( outputs, (PyObject *)" + aubiovectopyobj[p['type']] + " (" + p['name'] + ")" +");\n"
+    returnval += "  return outputs;"
+  elif len(outputparams) == 1:
+    if defaultsizes[name] == '1':
+      returnval += "  return (PyObject *)PyFloat_FromDouble(" + p['name'] + "->data[0])"
     else:
-        # no output
-        outputvecs = ""
-        outputcreate = ""
-        #returnval = "Py_None";
-        returnval = "return (PyObject *)" + aubiovectopyobj[p['type']] + " (" + p['name'] + ")"
-    # end of output strings
+      returnval += "  return (PyObject *)" + aubiovectopyobj[p['type']] + " (" + p['name'] + ")"
+  else:
+    returnval = "  return Py_None;";
+  # end of output strings
+  return outputvecs, outputcreate, returnval
 
+def gen_do(dofunc, name):
+    funcname = dofunc.split()[1].split('(')[0]
+    doparams = get_params_types_names(dofunc) 
+    # make sure the first parameter is the object
+    assert doparams[0]['type'] == "aubio_"+name+"_t*", \
+        "method is not in 'aubio_<name>_t"
+    # and remove it
+    doparams = doparams[1:]
+
+    n_param = len(doparams)
+
+    if name in param_numbers.keys():
+      n_input_param, n_output_param = param_numbers[name]
+      print name, n_output_param
+    else:
+      n_input_param, n_output_param = 1, n_param - 1
+
+    assert n_output_param + n_input_param == n_param, "n_output_param + n_input_param != n_param for %s" % name
+
+    inputparams = doparams[:n_input_param]
+    outputparams = doparams[n_input_param:n_input_param + n_output_param]
+
+    inputdefs, parseinput, inputrefs, inputvecs, pytypes = gen_do_input_params(inputparams);
+    outputvecs, outputcreate, returnval = gen_do_output_params(outputparams, name)
+
+    # build strings for outputs
     # build the parameters for the  _do() call
-    doparams_string = "self->o, " + ", ".join([p['name'] for p in doparams])
+    doparams_string = "self->o"
+    for p in doparams:
+      if p['type'] == 'uint_t*':
+        doparams_string += ", &" + p['name']
+      else:
+        doparams_string += ", " + p['name']
 
+    if n_input_param:
+      arg_parse_tuple = """\
+  if (!PyArg_ParseTuple (args, "%(pytypes)s", %(inputrefs)s)) {
+    return NULL;
+  }
+""" % locals()
+    else:
+      arg_parse_tuple = ""
     # put it all together
     s = """\
 /* function Py_%(name)s_do */
@@ -339,22 +394,20 @@
 static PyObject * 
 Py_%(name)s_do(Py_%(name)s * self, PyObject * args)
 {
-  %(inputdefs)s
-  %(inputvecs)s
-  %(outputvecs)s
+%(inputdefs)s
+%(inputvecs)s
+%(outputvecs)s
 
-  if (!PyArg_ParseTuple (args, "%(pytypes)s", %(inputrefs)s)) {
-    return NULL;
-  }
+%(arg_parse_tuple)s
 
-  %(parseinput)s
+%(parseinput)s
   
-  %(outputcreate)s
+%(outputcreate)s
 
   /* compute _do function */
   %(funcname)s (%(doparams_string)s);
 
-  %(returnval)s;
+%(returnval)s;
 }
 """ % locals()
     return s
--- a/interfaces/python/generator.py
+++ b/interfaces/python/generator.py
@@ -33,7 +33,16 @@
 
   generated_objects = []
   cpp_output, cpp_objects = get_cpp_objects()
-  skip_objects = ['fft', 'pvoc', 'filter', 'filterbank', 'resampler']
+  skip_objects = ['fft',
+      'pvoc',
+      'filter',
+      'filterbank',
+      'resampler',
+      'sndfile',
+      'sink_apple_audio',
+      'sink_sndfile',
+      'source_apple_audio',
+      'source_sndfile']
 
   write_msg("-- INFO: %d objects in total" % len(cpp_objects))
 
--- /dev/null
+++ b/interfaces/python/test_source.py
@@ -1,0 +1,27 @@
+#! /usr/bin/python
+
+from numpy.testing import TestCase, assert_equal, assert_almost_equal
+from aubio import fvec, source
+from numpy import array
+
+path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav"
+
+class aubio_filter_test_case(TestCase):
+
+  def test_members(self):
+    f = source(path)
+    print dir(f)
+
+  def test_read(self):
+    f = source(path)
+    total_frames = 0
+    while True:
+      vec, read = f()
+      total_frames += read
+      if read < f.hop_size: break
+    print "read", total_frames / float(f.samplerate), " seconds from", path
+
+if __name__ == '__main__':
+  from unittest import main
+  main()
+
--- a/src/aubio.h
+++ b/src/aubio.h
@@ -174,6 +174,13 @@
 #include "onset/onset.h"
 #include "onset/peakpicker.h"
 #include "tempo/tempo.h"
+#include "io/sndfileio.h"
+#include "io/source.h"
+#include "io/source_sndfile.h"
+#include "io/source_apple_audio.h"
+#include "io/sink.h"
+#include "io/sink_sndfile.h"
+#include "io/sink_apple_audio.h"
 
 #if AUBIO_UNSTABLE
 #include "vecutils.h"
--- /dev/null
+++ b/src/io/sink.c
@@ -1,0 +1,73 @@
+/*
+  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "config.h"
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "io/sink.h"
+#ifdef __APPLE__
+#include "io/sink_apple_audio.h"
+#endif /* __APPLE__ */
+#ifdef HAVE_SNDFILE
+#include "io/sink_sndfile.h"
+#endif
+
+struct _aubio_sink_t { 
+  void *sink;
+};
+
+aubio_sink_t * new_aubio_sink(char_t * uri, uint_t samplerate) {
+  aubio_sink_t * s = AUBIO_NEW(aubio_sink_t);
+#ifdef __APPLE__
+  s->sink = (void *)new_aubio_sink_apple_audio(uri, samplerate);
+  if (s->sink) return s;
+#else /* __APPLE__ */
+#if HAVE_SNDFILE
+  s->sink = (void *)new_aubio_sink_sndfile(uri, samplerate);
+  if (s->sink) return s;
+#endif /* HAVE_SNDFILE */
+#endif /* __APPLE__ */
+  AUBIO_ERROR("failed creating aubio sink with %s", uri);
+  AUBIO_FREE(s);
+  return NULL;
+}
+
+void aubio_sink_do(aubio_sink_t * s, fvec_t * write_data, uint_t write) {
+#ifdef __APPLE__
+  aubio_sink_apple_audio_do((aubio_sink_apple_audio_t *)s->sink, write_data, write);
+#else /* __APPLE__ */
+#if HAVE_SNDFILE
+  aubio_sink_sndfile_do((aubio_sink_sndfile_t *)s->sink, write_data, write);
+#endif /* HAVE_SNDFILE */
+#endif /* __APPLE__ */
+}
+
+void del_aubio_sink(aubio_sink_t * s) {
+  if (!s) return;
+#ifdef __APPLE__
+  del_aubio_sink_apple_audio((aubio_sink_apple_audio_t *)s->sink);
+#else /* __APPLE__ */
+#if HAVE_SNDFILE
+  del_aubio_sink_sndfile((aubio_sink_sndfile_t *)s->sink);
+#endif /* HAVE_SNDFILE */
+#endif /* __APPLE__ */
+  AUBIO_FREE(s);
+  return;
+}
--- /dev/null
+++ b/src/io/sink.h
@@ -1,0 +1,43 @@
+/*
+  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef _AUBIO_SINK_H
+#define _AUBIO_SINK_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file
+
+  Media sink
+
+*/
+
+typedef struct _aubio_sink_t aubio_sink_t;
+aubio_sink_t * new_aubio_sink(char_t * uri, uint_t samplerate);
+void aubio_sink_do(aubio_sink_t * s, fvec_t * write_data, uint_t written);
+void del_aubio_sink(aubio_sink_t * s);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _AUBIO_SINK_H */
--- /dev/null
+++ b/src/io/sink_apple_audio.c
@@ -1,0 +1,135 @@
+/*
+  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "config.h"
+
+#ifdef __APPLE__
+
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "io/sink_apple_audio.h"
+
+// CFURLRef, CFURLCreateWithFileSystemPath, ...
+#include <CoreFoundation/CoreFoundation.h>
+// ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
+#include <AudioToolbox/AudioToolbox.h>
+
+#define FLOAT_TO_SHORT(x) (short)(x * 32768)
+
+extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
+extern void freeAudioBufferList(AudioBufferList *bufferList);
+extern CFURLRef getURLFromPath(const char * path);
+
+#define MAX_SIZE 4096 // the maximum number of frames that can be written at a time
+
+struct _aubio_sink_apple_audio_t { 
+  uint_t samplerate;
+  uint_t channels;
+  char_t *path;
+
+  uint_t max_frames;
+
+  AudioBufferList bufferList;
+  ExtAudioFileRef audioFile;
+};
+
+aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t samplerate) {
+  aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t);
+  s->samplerate;
+  s->channels = 1;
+  s->path = uri;
+  s->max_frames = MAX_SIZE;
+
+  AudioStreamBasicDescription clientFormat;
+  UInt32 propSize = sizeof(clientFormat);
+  memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
+  clientFormat.mFormatID         = kAudioFormatLinearPCM;
+  clientFormat.mSampleRate       = (Float64)(s->samplerate);
+  clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
+  clientFormat.mChannelsPerFrame = s->channels;
+  clientFormat.mBitsPerChannel   = sizeof(short) * 8;
+  clientFormat.mFramesPerPacket  = 1;
+  clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
+  clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
+  clientFormat.mReserved         = 0;
+
+  AudioFileTypeID fileType = kAudioFileWAVEType;
+  CFURLRef fileURL = getURLFromPath(uri);
+  bool overwrite = true;
+  OSStatus err = noErr;
+  err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
+     overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
+  if (err) {
+    AUBIO_ERR("error when trying to access %s, in ExtAudioFileOpenURL, %d\n", s->path, (int)err);
+    goto beach;
+  }
+  if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
+    AUBIO_ERR("error when creating buffer list for %s, out of memory? \n", s->path);
+    goto beach;
+  }
+  return s;
+
+beach:
+  AUBIO_FREE(s);
+  return NULL;
+}
+
+void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
+  OSStatus err = noErr;
+  UInt32 c, v;
+  bool async = true;
+  short *data = (short*)s->bufferList.mBuffers[0].mData;
+  if (write > s->max_frames) { 
+    write = s->max_frames;
+    AUBIO_WRN("trying to write %d frames, but only %d can be written at a time",
+      write, s->max_frames);
+  }
+  smpl_t *buf = write_data->data;
+
+  if (buf) {
+      for (c = 0; c < s->channels; c++) {
+          for (v = 0; v < write; v++) {
+              data[v * s->channels + c] =
+                  FLOAT_TO_SHORT(buf[ v * s->channels + c]);
+          }
+      }
+  }
+  if (async) {
+    err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
+    if (err) { AUBIO_ERROR("error in ExtAudioFileWriteAsync, %d\n", (int)err); }
+  } else {
+    err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
+    if (err) { AUBIO_ERROR("error in ExtAudioFileWrite, %d\n", (int)err); }
+  }
+  return;
+}
+
+void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
+  OSStatus err = noErr;
+  if (!s || !s->audioFile) { return; }
+  err = ExtAudioFileDispose(s->audioFile);
+  if (err) AUBIO_ERROR("error in ExtAudioFileDispose, %d\n", (int)err);
+  s->audioFile = NULL;
+  freeAudioBufferList(&s->bufferList);
+  AUBIO_FREE(s);
+  return;
+}
+
+#endif /* __APPLE__ */
--- /dev/null
+++ b/src/io/sink_apple_audio.h
@@ -1,0 +1,43 @@
+/*
+  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef _AUBIO_SINK_APPLE_AUDIO_H
+#define _AUBIO_SINK_APPLE_AUDIO_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file
+
+  Apple Audio Media
+
+*/
+
+typedef struct _aubio_sink_apple_audio_t aubio_sink_apple_audio_t;
+aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * method, uint_t samplerate);
+void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write);
+void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _AUBIO_SINK_APPLE_AUDIO_H */
--- /dev/null
+++ b/src/io/sink_sndfile.c
@@ -1,0 +1,124 @@
+/*
+  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "config.h"
+
+#ifdef HAVE_SNDFILE
+
+#include <sndfile.h>
+
+#include "aubio_priv.h"
+#include "sink_sndfile.h"
+#include "fvec.h"
+
+#define MAX_CHANNELS 6
+#define MAX_SIZE 4096
+
+struct _aubio_sink_sndfile_t {
+  uint_t samplerate;
+  uint_t channels;
+  char_t *path;
+
+  uint_t max_size;
+
+  SNDFILE *handle;
+  uint_t scratch_size;
+  smpl_t *scratch_data;
+};
+
+aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * path, uint_t samplerate) {
+  aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t);
+
+  if (path == NULL) {
+    AUBIO_ERR("Aborted opening null path\n");
+    return NULL;
+  }
+
+  s->samplerate = samplerate;
+  s->max_size = MAX_SIZE;
+  s->channels = 1;
+  s->path = path;
+
+  /* set output format */
+  SF_INFO sfinfo;
+  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
+  sfinfo.samplerate = s->samplerate;
+  sfinfo.channels   = s->channels;
+  sfinfo.format     = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
+
+  /* try creating the file */
+  s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
+
+  if (s->handle == NULL) {
+    /* show libsndfile err msg */
+    AUBIO_ERR("Failed opening %s. %s\n", s->path, sf_strerror (NULL));
+    return NULL;
+  }	
+
+  s->scratch_size = s->max_size*s->channels;
+  /* allocate data for de/interleaving reallocated when needed. */
+  if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
+    AUBIO_ERR("%d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
+        s->max_size, s->channels, MAX_CHANNELS * MAX_CHANNELS);
+    return NULL;
+  }
+  s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
+
+  return s;
+}
+
+void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
+  uint_t i, j,	channels = s->channels;
+  int nsamples = channels*write;
+  smpl_t *pwrite;
+
+  if (write > s->max_size) {
+    AUBIO_WRN("trying to write %d frames, but only %d can be written at a time",
+      write, s->max_size);
+    write = s->max_size;
+  }
+
+  /* interleaving data  */
+  for ( i = 0; i < channels; i++) {
+    pwrite = (smpl_t *)write_data->data;
+    for (j = 0; j < write; j++) {
+      s->scratch_data[channels*j+i] = pwrite[j];
+    }
+  }
+
+  sf_count_t written_frames = sf_write_float (s->handle, s->scratch_data, nsamples);
+  if (written_frames/channels != write) {
+    AUBIO_WRN("trying to write %d frames to %s, but only %d could be written",
+      write, s->path, (uint_t)written_frames);
+  }
+  return;
+}
+
+void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
+  if (!s) return;
+  if (sf_close(s->handle)) {
+    AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
+  }
+  AUBIO_FREE(s->scratch_data);
+  AUBIO_FREE(s);
+}
+
+#endif /* HAVE_SNDFILE */
--- /dev/null
+++ b/src/io/sink_sndfile.h
@@ -1,0 +1,43 @@
+/*
+  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef _AUBIO_SINK_SNDFILE_H
+#define _AUBIO_SINK_SNDFILE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file
+
+  sndfile sink
+
+*/
+
+typedef struct _aubio_sink_sndfile_t aubio_sink_sndfile_t;
+aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * method, uint_t samplerate);
+void aubio_sink_sndfile_do(aubio_sink_sndfile_t * s, fvec_t * write_data, uint_t write);
+void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _AUBIO_SINK_SNDFILE_H */
--- /dev/null
+++ b/src/io/sndfileio.c
@@ -1,0 +1,254 @@
+/*
+  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "config.h"
+
+#ifdef HAVE_SNDFILE
+
+#include <string.h>
+
+#include <sndfile.h>
+
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "sndfileio.h"
+#include "mathutils.h"
+
+#define MAX_CHANNELS 6
+#define MAX_SIZE 4096
+
+struct _aubio_sndfile_t {
+        SNDFILE *handle;
+        int samplerate;
+        int channels;
+        int format;
+        float *tmpdata; /** scratch pad for interleaving/deinterleaving. */
+        int size;       /** store the size to check if realloc needed */
+};
+
+aubio_sndfile_t * new_aubio_sndfile_ro(const char* outputname) {
+        aubio_sndfile_t * f = AUBIO_NEW(aubio_sndfile_t);
+        SF_INFO sfinfo;
+        AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
+
+        f->handle = sf_open (outputname, SFM_READ, &sfinfo);
+
+        if (f->handle == NULL) {
+                AUBIO_ERR("Failed opening %s: %s\n", outputname,
+                        sf_strerror (NULL)); /* libsndfile err msg */
+                return NULL;
+        }	
+
+        if (sfinfo.channels > MAX_CHANNELS) { 
+                AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS);
+                return NULL;
+        }
+
+        f->size       = MAX_SIZE*sfinfo.channels;
+        f->tmpdata    = AUBIO_ARRAY(float,f->size);
+        /* get input specs */
+        f->samplerate = sfinfo.samplerate;
+        f->channels   = sfinfo.channels;
+        f->format     = sfinfo.format;
+
+        return f;
+}
+
+int aubio_sndfile_open_wo(aubio_sndfile_t * f, const char* inputname) {
+        SF_INFO sfinfo;
+        AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
+
+        /* define file output spec */
+        sfinfo.samplerate = f->samplerate;
+        sfinfo.channels   = f->channels;
+        sfinfo.format     = f->format;
+
+        if (! (f->handle = sf_open (inputname, SFM_WRITE, &sfinfo))) {
+                AUBIO_ERR("Not able to open output file %s.\n", inputname);
+                AUBIO_ERR("%s\n",sf_strerror (NULL)); /* libsndfile err msg */
+                AUBIO_QUIT(AUBIO_FAIL);
+        }	
+
+        if (sfinfo.channels > MAX_CHANNELS) { 
+                AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS);
+                AUBIO_QUIT(AUBIO_FAIL);
+        }
+        f->size       = MAX_SIZE*sfinfo.channels;
+        f->tmpdata    = AUBIO_ARRAY(float,f->size);
+        return AUBIO_OK;
+}
+
+/* setup file struct from existing one */
+aubio_sndfile_t * new_aubio_sndfile_wo(aubio_sndfile_t * fmodel, const char *outputname) {
+        aubio_sndfile_t * f = AUBIO_NEW(aubio_sndfile_t);
+        f->samplerate    = fmodel->samplerate;
+        f->channels      = 1; //fmodel->channels;
+        f->format        = fmodel->format;
+        aubio_sndfile_open_wo(f, outputname);
+        return f;
+}
+
+
+/* return 0 if properly closed, 1 otherwise */
+int del_aubio_sndfile(aubio_sndfile_t * f) {
+        if (sf_close(f->handle)) {
+                AUBIO_ERR("Error closing file.");
+                puts (sf_strerror (NULL));
+                return 1;
+        }
+        AUBIO_FREE(f->tmpdata);
+        AUBIO_FREE(f);
+        //AUBIO_DBG("File closed.\n");
+        return 0;
+}
+
+/**************************************************************
+ *
+ * Read write methods 
+ *
+ */
+
+
+/* read frames from file in data 
+ *  return the number of frames actually read */
+int aubio_sndfile_read(aubio_sndfile_t * f, int frames, fvec_t ** read) {
+        sf_count_t read_frames;
+        int i,j, channels = f->channels;
+        int nsamples = frames*channels;
+        int aread;
+        smpl_t *pread;	
+
+        /* allocate data for de/interleaving reallocated when needed. */
+        if (nsamples >= f->size) {
+                AUBIO_ERR("Maximum aubio_sndfile_read buffer size exceeded.");
+                return -1;
+                /*
+                AUBIO_FREE(f->tmpdata);
+                f->tmpdata = AUBIO_ARRAY(float,nsamples);
+                */
+        }
+        //f->size = nsamples;
+
+        /* do actual reading */
+        read_frames = sf_read_float (f->handle, f->tmpdata, nsamples);
+
+        aread = (int)FLOOR(read_frames/(float)channels);
+
+        /* de-interleaving data  */
+        for (i=0; i<channels; i++) {
+                pread = (smpl_t *)fvec_get_data(read[i]);
+                for (j=0; j<aread; j++) {
+                        pread[j] = (smpl_t)f->tmpdata[channels*j+i];
+                }
+        }
+        return aread;
+}
+
+int
+aubio_sndfile_read_mono (aubio_sndfile_t * f, int frames, fvec_t * read)
+{
+  sf_count_t read_frames;
+  int i, j, channels = f->channels;
+  int nsamples = frames * channels;
+  int aread;
+  smpl_t *pread;
+
+  /* allocate data for de/interleaving reallocated when needed. */
+  if (nsamples >= f->size) {
+    AUBIO_ERR ("Maximum aubio_sndfile_read buffer size exceeded.");
+    return -1;
+    /*
+    AUBIO_FREE(f->tmpdata);
+    f->tmpdata = AUBIO_ARRAY(float,nsamples);
+    */
+  }
+  //f->size = nsamples;
+
+  /* do actual reading */
+  read_frames = sf_read_float (f->handle, f->tmpdata, nsamples);
+
+  aread = (int) FLOOR (read_frames / (float) channels);
+
+  /* de-interleaving data  */
+  pread = (smpl_t *) fvec_get_data (read);
+  for (i = 0; i < channels; i++) {
+    for (j = 0; j < aread; j++) {
+      pread[j] += (smpl_t) f->tmpdata[channels * j + i];
+    }
+  }
+  for (j = 0; j < aread; j++) {
+    pread[j] /= (smpl_t)channels;
+  }
+
+  return aread;
+}
+
+/* write 'frames' samples to file from data 
+ *   return the number of frames actually written 
+ */
+int aubio_sndfile_write(aubio_sndfile_t * f, int frames, fvec_t ** write) {
+        sf_count_t written_frames = 0;
+        int i, j,	channels = f->channels;
+        int nsamples = channels*frames;
+        smpl_t *pwrite;
+
+        /* allocate data for de/interleaving reallocated when needed. */
+        if (nsamples >= f->size) {
+                AUBIO_ERR("Maximum aubio_sndfile_write buffer size exceeded.");
+                return -1;
+                /*
+                AUBIO_FREE(f->tmpdata);
+                f->tmpdata = AUBIO_ARRAY(float,nsamples);
+                */
+        }
+        //f->size = nsamples;
+
+        /* interleaving data  */
+        for (i=0; i<channels; i++) {
+                pwrite = (smpl_t *)fvec_get_data(write[i]);
+                for (j=0; j<frames; j++) {
+                        f->tmpdata[channels*j+i] = (float)pwrite[j];
+                }
+        }
+        written_frames = sf_write_float (f->handle, f->tmpdata, nsamples);
+        return written_frames/channels;
+}
+
+/*******************************************************************
+ *
+ * Get object info 
+ *
+ */
+
+uint_t aubio_sndfile_channels(aubio_sndfile_t * f) {
+        return f->channels;
+}
+
+uint_t aubio_sndfile_samplerate(aubio_sndfile_t * f) {
+        return f->samplerate;
+}
+
+void aubio_sndfile_info(aubio_sndfile_t * f) {
+        AUBIO_DBG("srate    : %d\n", f->samplerate);
+        AUBIO_DBG("channels : %d\n", f->channels);
+        AUBIO_DBG("format   : %d\n", f->format);
+}
+
+#endif /* HAVE_SNDFILE */
--- /dev/null
+++ b/src/io/sndfileio.h
@@ -1,0 +1,79 @@
+/*
+  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef SNDFILEIO_H
+#define SNDFILEIO_H
+
+/** @file 
+ * sndfile functions
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * sndfile object
+ */
+typedef struct _aubio_sndfile_t aubio_sndfile_t;
+/** 
+ * Open a sound file for reading
+ */
+aubio_sndfile_t * new_aubio_sndfile_ro (const char * inputfile);
+/**
+ * Copy file model from previously opened sound file.
+ */
+aubio_sndfile_t * new_aubio_sndfile_wo(aubio_sndfile_t * existingfile, const char * outputname);
+/** 
+ * Open a sound file for writing
+ */
+int aubio_sndfile_open_wo (aubio_sndfile_t * file, const char * outputname);
+/** 
+ * Read frames data from file into an array of buffers
+ */
+int aubio_sndfile_read(aubio_sndfile_t * file, int frames, fvec_t ** read);
+/** 
+ * Read frames data from file into a single buffer
+ */
+int aubio_sndfile_read_mono (aubio_sndfile_t * f, int frames, fvec_t * read);
+/** 
+ * Write data of length frames to file
+ */
+int aubio_sndfile_write(aubio_sndfile_t * file, int frames, fvec_t ** write);
+/**
+ * Close file and delete file object
+ */
+int del_aubio_sndfile(aubio_sndfile_t * file);
+/**
+ * Return some files facts
+ */
+void aubio_sndfile_info(aubio_sndfile_t * file);
+/**
+ * Return number of channel in file
+ */
+uint_t aubio_sndfile_channels(aubio_sndfile_t * file);
+uint_t aubio_sndfile_samplerate(aubio_sndfile_t * file);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
--- /dev/null
+++ b/src/io/source.c
@@ -1,0 +1,73 @@
+/*
+  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "config.h"
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "io/source.h"
+#ifdef __APPLE__
+#include "io/source_apple_audio.h"
+#endif /* __APPLE__ */
+#ifdef HAVE_SNDFILE
+#include "io/source_sndfile.h"
+#endif
+
+struct _aubio_source_t { 
+  void *source;
+};
+
+aubio_source_t * new_aubio_source(char_t * uri, uint_t samplerate, uint_t hop_size) {
+  aubio_source_t * s = AUBIO_NEW(aubio_source_t);
+#ifdef __APPLE__
+  s->source = (void *)new_aubio_source_apple_audio(uri, samplerate, hop_size);
+  if (s->source) return s;
+#else /* __APPLE__ */
+#if HAVE_SNDFILE
+  s->source = (void *)new_aubio_source_sndfile(uri, samplerate, hop_size);
+  if (s->source) return s;
+#endif /* HAVE_SNDFILE */
+#endif /* __APPLE__ */
+  AUBIO_ERROR("failed creating aubio source with %s", uri);
+  AUBIO_FREE(s);
+  return NULL;
+}
+
+void aubio_source_do(aubio_source_t * s, fvec_t * data, uint_t * read) {
+#ifdef __APPLE__
+  aubio_source_apple_audio_do((aubio_source_apple_audio_t *)s->source, data, read);
+#else /* __APPLE__ */
+#if HAVE_SNDFILE
+  aubio_source_sndfile_do((aubio_source_sndfile_t *)s->source, data, read);
+#endif /* HAVE_SNDFILE */
+#endif /* __APPLE__ */
+}
+
+void del_aubio_source(aubio_source_t * s) {
+  if (!s) return;
+#ifdef __APPLE__
+  del_aubio_source_apple_audio((aubio_source_apple_audio_t *)s->source);
+#else /* __APPLE__ */
+#if HAVE_SNDFILE
+  del_aubio_source_sndfile((aubio_source_sndfile_t *)s->source);
+#endif /* HAVE_SNDFILE */
+#endif /* __APPLE__ */
+  AUBIO_FREE(s);
+}
+
--- /dev/null
+++ b/src/io/source.h
@@ -1,0 +1,43 @@
+/*
+  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef _AUBIO_SOURCE_H
+#define _AUBIO_SOURCE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** \file
+
+  Media source 
+
+*/
+
+typedef struct _aubio_source_t aubio_source_t;
+aubio_source_t * new_aubio_source(char_t * uri, uint_t samplerate, uint_t hop_size);
+void aubio_source_do(aubio_source_t * s, fvec_t * read_data, uint_t * read);
+void del_aubio_source(aubio_source_t * s);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _AUBIO_SOURCE_H */
--- /dev/null
+++ b/src/io/source_apple_audio.c
@@ -1,0 +1,178 @@
+/*
+  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifdef __APPLE__
+#include "config.h"
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "io/source_apple_audio.h"
+
+// ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
+#include <AudioToolbox/AudioToolbox.h>
+
+#define RT_BYTE1( a )      ( (a) & 0xff )
+#define RT_BYTE2( a )      ( ((a) >> 8) & 0xff )
+#define RT_BYTE3( a )      ( ((a) >> 16) & 0xff )
+#define RT_BYTE4( a )      ( ((a) >> 24) & 0xff )
+
+#define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
+
+struct _aubio_source_apple_audio_t {
+  uint_t channels;
+  uint_t samplerate;
+  uint_t block_size;
+
+  char_t *path;
+
+  ExtAudioFileRef audioFile;
+  AudioBufferList bufferList;
+};
+
+extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
+extern void freeAudioBufferList(AudioBufferList *bufferList);
+extern CFURLRef getURLFromPath(const char * 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->samplerate = samplerate;
+  s->block_size = block_size;
+  s->channels = 1;
+
+  OSStatus err = noErr;
+  UInt32 propSize;
+
+  AudioStreamBasicDescription clientFormat;
+  propSize = sizeof(clientFormat);
+  memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
+  clientFormat.mFormatID         = kAudioFormatLinearPCM;
+  clientFormat.mSampleRate       = (Float64)(s->samplerate);
+  clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
+  clientFormat.mChannelsPerFrame = s->channels;
+  clientFormat.mBitsPerChannel   = sizeof(short) * 8;
+  clientFormat.mFramesPerPacket  = 1;
+  clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
+  clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
+  clientFormat.mReserved         = 0;
+
+  // open the resource url
+  CFURLRef fileURL = getURLFromPath(path);
+  err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
+  if (err) { AUBIO_ERR("error when trying to access %s, in ExtAudioFileOpenURL, %d\n", s->path, (int)err); goto beach;}
+
+  // create an empty AudioStreamBasicDescription
+  AudioStreamBasicDescription fileFormat;
+  propSize = sizeof(fileFormat);
+  memset(&fileFormat, 0, sizeof(AudioStreamBasicDescription));
+
+  // fill it with the file description
+  err = ExtAudioFileGetProperty(s->audioFile,
+      kExtAudioFileProperty_FileDataFormat, &propSize, &fileFormat);
+  if (err) { AUBIO_ERROR("error in ExtAudioFileGetProperty, %d\n", (int)err); goto beach;}
+
+  // set the client format description
+  err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
+      propSize, &clientFormat);
+  if (err) { AUBIO_ERROR("error in ExtAudioFileSetProperty, %d\n", (int)err); goto beach;}
+
+#if 0
+  // print client and format descriptions
+  AUBIO_DBG("Opened %s\n", s->path);
+  AUBIO_DBG("file/client Format.mFormatID:        : %3c%c%c%c / %c%c%c%c\n",
+      (int)RT_BYTE4(fileFormat.mFormatID),   (int)RT_BYTE3(fileFormat.mFormatID),   (int)RT_BYTE2(fileFormat.mFormatID),   (int)RT_BYTE1(fileFormat.mFormatID),
+      (int)RT_BYTE4(clientFormat.mFormatID), (int)RT_BYTE3(clientFormat.mFormatID), (int)RT_BYTE2(clientFormat.mFormatID), (int)RT_BYTE1(clientFormat.mFormatID)
+      );
+
+  AUBIO_DBG("file/client Format.mSampleRate       : %6.0f / %.0f\n",     fileFormat.mSampleRate      ,      clientFormat.mSampleRate);
+  AUBIO_DBG("file/client Format.mFormatFlags      : %6d / %d\n",    (int)fileFormat.mFormatFlags     , (int)clientFormat.mFormatFlags);
+  AUBIO_DBG("file/client Format.mChannelsPerFrame : %6d / %d\n",    (int)fileFormat.mChannelsPerFrame, (int)clientFormat.mChannelsPerFrame);
+  AUBIO_DBG("file/client Format.mBitsPerChannel   : %6d / %d\n",    (int)fileFormat.mBitsPerChannel  , (int)clientFormat.mBitsPerChannel);
+  AUBIO_DBG("file/client Format.mFramesPerPacket  : %6d / %d\n",    (int)fileFormat.mFramesPerPacket , (int)clientFormat.mFramesPerPacket);
+  AUBIO_DBG("file/client Format.mBytesPerFrame    : %6d / %d\n",    (int)fileFormat.mBytesPerFrame   , (int)clientFormat.mBytesPerFrame);
+  AUBIO_DBG("file/client Format.mBytesPerPacket   : %6d / %d\n",    (int)fileFormat.mBytesPerPacket  , (int)clientFormat.mBytesPerPacket);
+  AUBIO_DBG("file/client Format.mReserved         : %6d / %d\n",    (int)fileFormat.mReserved        , (int)clientFormat.mReserved);
+#endif
+
+  // 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;
+  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);
+  } else {
+    assert (segmentSize == samples );
+    //AUBIO_DBG("not resampling, segmentSize %d, block_size %d\n", segmentSize, s->block_size);
+  }
+
+  // allocate the AudioBufferList
+  if (createAubioBufferList(&s->bufferList, s->channels, segmentSize)) err = -1;
+
+  return s;
+ 
+beach:
+  AUBIO_FREE(s);
+  return NULL;
+}
+
+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;}
+
+  smpl_t *buf = read_to->data;
+
+  short *data = (short*)s->bufferList.mBuffers[0].mData;
+
+  if (buf) {
+      for (c = 0; c < s->channels; c++) {
+          for (v = 0; v < s->block_size; v++) {
+              if (v < loadedPackets) {
+                  buf[v * s->channels + c] =
+                      SHORT_TO_FLOAT(data[ v * s->channels + c]);
+              } else {
+                  buf[v * s->channels + c] = 0.f;
+              }
+          }
+      }
+  }
+  //if (loadedPackets < s->block_size) return EOF;
+  *read = (uint_t)loadedPackets;
+  return;
+beach:
+  *read = 0;
+  return;
+}
+
+void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
+  OSStatus err = noErr;
+  if (!s || !s->audioFile) { return; }
+  err = ExtAudioFileDispose(s->audioFile);
+  if (err) AUBIO_ERROR("error in ExtAudioFileDispose, %d\n", (int)err);
+  s->audioFile = NULL;
+  freeAudioBufferList(&s->bufferList);
+  AUBIO_FREE(s);
+  return;
+}
+
+#endif /* __APPLE__ */
--- /dev/null
+++ b/src/io/source_apple_audio.h
@@ -1,0 +1,29 @@
+/*
+  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef _AUBIO_SOURCE_APPLE_AUDIO_H
+#define _AUBIO_SOURCE_APPLE_AUDIO_H
+
+typedef struct _aubio_source_apple_audio_t aubio_source_apple_audio_t;
+aubio_source_apple_audio_t * new_aubio_source_apple_audio(char_t * path, uint_t samplerate, uint_t block_size);
+void aubio_source_apple_audio_do(aubio_source_apple_audio_t * s, fvec_t * read_to, uint_t * read);
+void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s);
+
+#endif /* _AUBIO_SOURCE_APPLE_AUDIO_H */
--- /dev/null
+++ b/src/io/source_sndfile.c
@@ -1,0 +1,188 @@
+/*
+  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "config.h"
+
+#ifdef HAVE_SNDFILE
+
+#include <sndfile.h>
+
+#include "aubio_priv.h"
+#include "source_sndfile.h"
+#include "fvec.h"
+
+#include "temporal/resampler.h"
+
+#define MAX_CHANNELS 6
+#define MAX_SIZE 4096
+#define MAX_SAMPLES MAX_CHANNELS * MAX_SIZE
+
+struct _aubio_source_sndfile_t {
+  uint_t hop_size;
+  uint_t samplerate;
+  uint_t channels;
+
+  // some data about the file
+  char_t *path;
+  SNDFILE *handle;
+  int input_samplerate;
+  int input_channels;
+  int input_format;
+
+  // resampling stuff
+  smpl_t ratio;
+  uint_t input_hop_size;
+#ifdef HAVE_SAMPLERATE
+  aubio_resampler_t *resampler;
+  fvec_t *input_data;
+#endif /* HAVE_SAMPLERATE */
+
+  // some temporary memory for sndfile to write at
+  uint_t scratch_size;
+  smpl_t *scratch_data;
+};
+
+aubio_source_sndfile_t * new_aubio_source_sndfile(char_t * path, uint_t samplerate, uint_t hop_size) {
+  aubio_source_sndfile_t * s = AUBIO_NEW(aubio_source_sndfile_t);
+
+  if (path == NULL) {
+    AUBIO_ERR("Aborted opening null path\n");
+    return NULL;
+  }
+
+  s->hop_size = hop_size;
+  s->samplerate = samplerate;
+  s->channels = 1;
+  s->path = path;
+
+  // try opening the file, geting the info in sfinfo
+  SF_INFO sfinfo;
+  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
+  s->handle = sf_open (s->path, SFM_READ, &sfinfo);
+
+  if (s->handle == NULL) {
+    /* show libsndfile err msg */
+    AUBIO_ERR("Failed opening %s: %s\n", s->path, sf_strerror (NULL));
+    goto beach;
+  }	
+
+  /* get input specs */
+  s->input_samplerate = sfinfo.samplerate;
+  s->input_channels   = sfinfo.channels;
+  s->input_format     = sfinfo.format;
+
+  /* compute input block size required before resampling */
+  s->ratio = s->samplerate/(float)s->input_samplerate;
+  s->input_hop_size = (uint_t)FLOOR(s->hop_size / s->ratio + .5);
+
+  if (s->input_hop_size * s->input_channels > MAX_SAMPLES) {
+    AUBIO_ERR("Not able to process more than %d frames of %d channels\n",
+        MAX_SAMPLES / s->input_channels, s->input_channels);
+    goto beach;
+  }
+
+#ifdef HAVE_SAMPLERATE
+  s->resampler = NULL;
+  s->input_data = NULL;
+  if (s->ratio != 1) {
+    s->input_data = new_fvec(s->input_hop_size);
+    s->resampler = new_aubio_resampler(s->ratio, 4);
+    if (s->ratio > 1) {
+      // we would need to add a ring buffer for these
+      if ( (uint_t)(s->input_hop_size * s->ratio + .5)  != s->hop_size ) {
+        AUBIO_ERR("can not upsample from %d to %d\n", s->input_samplerate, s->samplerate);
+        goto beach;
+      }
+      AUBIO_WRN("upsampling %s from %d to % d\n", s->path, s->input_samplerate, s->samplerate);
+    }
+  }
+#else
+  if (s->ratio != 1) {
+    AUBIO_ERR("aubio was compiled without aubio_resampler\n");
+    goto beach;
+  }
+#endif /* HAVE_SAMPLERATE */
+
+  /* allocate data for de/interleaving reallocated when needed. */
+  s->scratch_size = s->input_hop_size * s->input_channels;
+  s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
+
+  return s;
+
+beach:
+  AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
+      s->path, s->samplerate, s->hop_size);
+  del_aubio_source_sndfile(s);
+  return NULL;
+}
+
+void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
+  uint_t i,j, input_channels = s->input_channels;
+  /* do actual reading */
+  sf_count_t read_samples = sf_read_float (s->handle, s->scratch_data, s->scratch_size);
+
+  smpl_t *data;
+
+#ifdef HAVE_SAMPLERATE
+  if (s->ratio != 1) {
+    data = s->input_data->data;
+  } else
+#endif /* HAVE_SAMPLERATE */
+  {
+    data = read_data->data;
+  }
+
+  /* de-interleaving and down-mixing data  */
+  for (j = 0; j < read_samples / input_channels; j++) {
+    data[j] = 0;
+    for (i = 0; i < input_channels; i++) {
+      data[j] += (smpl_t)s->scratch_data[input_channels*j+i];
+    }
+    data[j] /= (smpl_t)input_channels;
+  }
+
+#ifdef HAVE_SAMPLERATE
+  if (s->resampler) {
+    aubio_resampler_do(s->resampler, s->input_data, read_data);
+  }
+#endif /* HAVE_SAMPLERATE */
+
+  *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
+}
+
+void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
+  if (!s) return;
+  if (sf_close(s->handle)) {
+    AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
+  }
+#ifdef HAVE_SAMPLERATE
+  if (s->resampler != NULL) {
+    del_aubio_resampler(s->resampler);
+  }
+  if (s->input_data) {
+    del_fvec(s->input_data);
+  }
+#endif /* HAVE_SAMPLERATE */
+  AUBIO_FREE(s->scratch_data);
+  AUBIO_FREE(s);
+}
+
+#endif /* HAVE_SNDFILE */
--- /dev/null
+++ b/src/io/source_sndfile.h
@@ -1,0 +1,29 @@
+/*
+  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef _AUBIO_SOURCE_SNDFILE_H
+#define _AUBIO_SOURCE_SNDFILE_H
+
+typedef struct _aubio_source_sndfile_t aubio_source_sndfile_t;
+aubio_source_sndfile_t * new_aubio_source_sndfile(char_t * path, uint_t samplerate, uint_t block_size);
+void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_to, uint_t * read);
+void del_aubio_source_sndfile(aubio_source_sndfile_t * s);
+
+#endif /* _AUBIO_SOURCE_SNDFILE_H */
--- /dev/null
+++ b/src/io/utils_apple_audio.c
@@ -1,0 +1,40 @@
+#ifdef __APPLE__
+
+// CFURLRef, CFURLCreateWithFileSystemPath, ...
+#include <CoreFoundation/CoreFoundation.h>
+// ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
+#include <AudioToolbox/AudioToolbox.h>
+
+int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
+void freeAudioBufferList(AudioBufferList *bufferList);
+CFURLRef getURLFromPath(const char * path);
+
+int createAubioBufferList(AudioBufferList * bufferList, int channels, int segmentSize) {
+  bufferList->mNumberBuffers = 1;
+  bufferList->mBuffers[0].mNumberChannels = channels;
+  bufferList->mBuffers[0].mData = (short *)malloc(segmentSize * sizeof(short));
+  bufferList->mBuffers[0].mDataByteSize = segmentSize * sizeof(short);
+  return 0;
+}
+
+void freeAudioBufferList(AudioBufferList *bufferList) {
+  UInt32 i = 0;
+  if (!bufferList) return;
+  for (i = 0; i < bufferList->mNumberBuffers; i++) {
+    if (bufferList->mBuffers[i].mData) {
+      free (bufferList->mBuffers[i].mData);
+      bufferList->mBuffers[i].mData = NULL;
+    }
+  }
+  bufferList = NULL;
+}
+
+CFURLRef getURLFromPath(const char * path) {
+  CFStringRef cfTotalPath = CFStringCreateWithCString (kCFAllocatorDefault,
+      path, kCFStringEncodingUTF8);
+
+  return CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfTotalPath,
+      kCFURLPOSIXPathStyle, false);
+}
+
+#endif /* __APPLE__ */
--- a/src/wscript_build
+++ b/src/wscript_build
@@ -1,11 +1,17 @@
 # vim:set syntax=python:
 
-uselib = ['SAMPLERATE']
+source = ctx.path.ant_glob('*.c **/*.c')
+uselib = []
+
 if 'HAVE_FFTW3' in conf.get_env():
-  source = ctx.path.ant_glob('*.c **/*.c', excl = ['**/ooura_fft8g.c'])
+  source.filter(lambda x: not x.endswith('ooura_fft8g.c'))
   uselib += ['FFTW3', 'FFTW3F']
-else:
-  source = ctx.path.ant_glob('*.c **/*.c')
+
+if 'HAVE_SAMPLERATE':
+  uselib += ['SAMPLERATE']
+
+if 'HAVE_SNDFILE':
+  uselib += ['SNDFILE']
 
 # build libaubio
 ctx.shlib(
--- /dev/null
+++ b/tests/src/io/test-sink.c
@@ -1,0 +1,30 @@
+#include <stdio.h>
+#include <aubio.h>
+#include "config.h"
+
+char_t *path = "/home/piem/archives/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
+char_t *outpath = "/var/tmp/test.wav";
+
+int main(){
+  int err = 0;
+  uint_t samplerate = 44100;
+  uint_t hop_size = 512;
+  uint_t read = hop_size;
+  fvec_t *vec = new_fvec(hop_size);
+  aubio_source_t * i = new_aubio_source(path, samplerate, hop_size);
+  aubio_sink_t *   o = new_aubio_sink(outpath, samplerate);
+
+  if (!i || !o) { err = -1; goto beach; }
+
+  while ( read == hop_size ) {
+    aubio_source_do(i, vec, &read);
+    aubio_sink_do(o, vec, read);
+  }
+
+beach:
+  del_aubio_source(i);
+  del_aubio_sink(o);
+  del_fvec(vec);
+  return err;
+}
+
--- /dev/null
+++ b/tests/src/io/test-sink_apple_audio_file.c
@@ -1,0 +1,34 @@
+#include <stdio.h>
+#include <aubio.h>
+#include "config.h"
+
+char_t *path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
+char_t *outpath = "/var/tmp/test.wav";
+
+int main(){
+  int err = 0;
+#ifdef __APPLE__
+  uint_t samplerate = 44100;
+  uint_t hop_size = 512;
+  uint_t read = hop_size;
+  fvec_t *vec = new_fvec(hop_size);
+  aubio_source_apple_audio_t * i = new_aubio_source_apple_audio(path, samplerate, hop_size);
+  aubio_sink_apple_audio_t *   o = new_aubio_sink_apple_audio(outpath, samplerate);
+
+  if (!i || !o) { err = -1; goto beach; }
+
+  while ( read == hop_size ) {
+    aubio_source_apple_audio_do(i, vec, &read);
+    aubio_sink_apple_audio_do(o, vec, read);
+  }
+
+beach:
+  del_aubio_source_apple_audio(i);
+  del_aubio_sink_apple_audio(o);
+  del_fvec(vec);
+#else
+  fprintf(stderr, "ERR: aubio was not compiled with aubio_source_apple_audio\n");
+#endif /* __APPLE__ */
+  return err;
+}
+
--- /dev/null
+++ b/tests/src/io/test-sink_sndfile.c
@@ -1,0 +1,34 @@
+#include <stdio.h>
+#include <aubio.h>
+#include "config.h"
+
+char_t *path = "/home/piem/archives/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
+char_t *outpath = "/var/tmp/test.wav";
+
+int main(){
+  int err = 0;
+#ifdef HAVE_SNDFILE
+  uint_t samplerate = 44100;
+  uint_t hop_size = 512;
+  uint_t read = hop_size;
+  fvec_t *vec = new_fvec(hop_size);
+  aubio_source_sndfile_t * i = new_aubio_source_sndfile(path, samplerate, hop_size);
+  aubio_sink_sndfile_t *   o = new_aubio_sink_sndfile(outpath, samplerate);
+
+  if (!i || !o) { err = -1; goto beach; }
+
+  while ( read == hop_size ) {
+    aubio_source_sndfile_do(i, vec, &read);
+    aubio_sink_sndfile_do(o, vec, read);
+  }
+
+beach:
+  del_aubio_source_sndfile(i);
+  del_aubio_sink_sndfile(o);
+  del_fvec(vec);
+#else
+  fprintf(stderr, "ERR: aubio was not compiled with aubio_source_sndfile\n");
+#endif /* HAVE_SNDFILE */
+  return err;
+}
+
--- /dev/null
+++ b/tests/src/io/test-source.c
@@ -1,0 +1,25 @@
+#include <stdio.h>
+#include <aubio.h>
+
+char_t *path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
+//char_t *path = "/Users/piem/Downloads/Keziah Jones - Where's Life.mp3";
+
+int main(){
+  uint_t samplerate = 32000;
+  uint_t hop_size = 1024;
+  uint_t read = hop_size;
+  fvec_t *vec = new_fvec(hop_size);
+  aubio_source_t* s = new_aubio_source(path, samplerate, hop_size);
+
+  if (!s) return -1;
+
+  while ( read == hop_size ) {
+    aubio_source_do(s, vec, &read);
+    fprintf(stdout, "%d [%f, %f, ..., %f]\n", read, vec->data[0], vec->data[1], vec->data[read - 1]);
+  }
+
+  del_aubio_source(s);
+
+  return 0;
+}
+
--- /dev/null
+++ b/tests/src/io/test-source_apple_audio_file.c
@@ -1,0 +1,28 @@
+#include <stdio.h>
+#include <aubio.h>
+
+char_t *path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
+//char_t *path = "/Volumes/moving/moving/photos/gopro2/100GOPRO/GOPR4515.MP4";
+
+int main(){
+#ifdef __APPLE__
+  uint_t samplerate = 32000;
+  uint_t hop_size = 1024;
+  uint_t read = hop_size;
+  fvec_t *vec = new_fvec(hop_size);
+  aubio_source_apple_audio_t * s = new_aubio_source_apple_audio(path, samplerate, hop_size);
+
+  if (!s) return -1;
+
+  while ( read == hop_size ) {
+    aubio_source_apple_audio_do(s, vec, &read);
+    fprintf(stdout, "%d [%f, %f, ..., %f]\n", read, vec->data[0], vec->data[1], vec->data[read - 1]);
+  }
+
+  del_aubio_source_apple_audio(s);
+#else
+  fprintf(stderr, "ERR: aubio was not compiled with aubio_source_apple_audio\n");
+#endif /* __APPLE__ */
+  return 0;
+}
+
--- /dev/null
+++ b/tests/src/io/test-source_sndfile.c
@@ -1,0 +1,33 @@
+#include <stdio.h>
+#include <aubio.h>
+#include "config.h"
+
+char_t *path = "/home/piem/archives/samples/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
+
+int main(){
+  int err = 0;
+#ifdef HAVE_SNDFILE
+  uint_t samplerate = 8000;
+  uint_t hop_size = 512;
+  uint_t read = hop_size;
+  fvec_t *vec = new_fvec(hop_size);
+  aubio_source_sndfile_t * s = new_aubio_source_sndfile(path, samplerate, hop_size);
+
+  if (!s) { err = 1; goto beach; }
+
+  while ( read == hop_size ) {
+    aubio_source_sndfile_do(s, vec, &read);
+    if (read == 0) break;
+    fprintf(stdout, "%d [%f, %f, ..., %f]\n", read, vec->data[0], vec->data[1], vec->data[read - 1]);
+  }
+
+beach:
+  del_aubio_source_sndfile(s);
+  del_fvec(vec);
+#else
+  fprintf(stderr, "ERR: aubio was not compiled with aubio_source_sndfile\n");
+  err = 2;
+#endif /* HAVE_SNDFILE */
+  return err;
+}
+
--- a/wscript
+++ b/wscript
@@ -73,6 +73,7 @@
     ctx.env.LINKFLAGS += ['-arch', 'i386', '-arch', 'x86_64']
     ctx.env.CC = 'llvm-gcc-4.2'
     ctx.env.LINK_CC = 'llvm-gcc-4.2'
+    ctx.env.FRAMEWORK = ['CoreFoundation', 'AudioToolbox']
 
   # check for required headers
   ctx.check(header_name='stdlib.h')