Index: amples/sndfileio.c
===================================================================
--- examples/sndfileio.c	(revision c43c459d4fbd0a0ce9b67e6430fec10f9aa9a82b)
+++ 	(revision )
@@ -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 */
Index: amples/sndfileio.h
===================================================================
--- examples/sndfileio.h	(revision c43c459d4fbd0a0ce9b67e6430fec10f9aa9a82b)
+++ 	(revision )
@@ -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
-
Index: examples/utils.h
===================================================================
--- examples/utils.h	(revision c43c459d4fbd0a0ce9b67e6430fec10f9aa9a82b)
+++ examples/utils.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -28,5 +28,4 @@
 #include <aubio.h>
 #include "config.h"
-#include "sndfileio.h"
 #ifdef HAVE_JACK
 #include "jackio.h"
Index: examples/wscript_build
===================================================================
--- examples/wscript_build	(revision c43c459d4fbd0a0ce9b67e6430fec10f9aa9a82b)
+++ examples/wscript_build	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -2,18 +2,12 @@
 
 # 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',
Index: interfaces/python/demo_source.py
===================================================================
--- interfaces/python/demo_source.py	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ interfaces/python/demo_source.py	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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
Index: interfaces/python/gen_pyobject.py
===================================================================
--- interfaces/python/gen_pyobject.py	(revision c43c459d4fbd0a0ce9b67e6430fec10f9aa9a82b)
+++ interfaces/python/gen_pyobject.py	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -18,4 +18,8 @@
 maintaining this bizarre file.
 """
+
+param_numbers = {
+  'source': [0, 2],
+}
 
 # TODO
@@ -81,18 +85,19 @@
 # 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->hop_size', 'self->hop_size'],
+    'mfcc':         ['self->n_coeffs'],
+    'beattracking': ['self->hop_size'],
+    'tempo':        ['1'],
+    'peakpicker':   ['1'],
+    'source':       ['self->hop_size', '1'],
 }
 
@@ -123,4 +128,5 @@
     'ratio': '0.5',
     'method': '"default"',
+    'uri': '"none"',
     }
 
@@ -146,4 +152,5 @@
     'cvec_t*': 'PyAubio_CCvecToPyCvec',
     'smpl_t': 'PyFloat_FromDouble',
+    'uint_t*': 'PyInt_FromLong',
 }
 
@@ -257,4 +264,70 @@
     return s
 
+def gen_do_input_params(inputparams):
+  inputdefs = ''
+  parseinput = ''
+  inputrefs = ''
+  inputvecs = ''
+  pytypes = ''
+
+  if len(inputparams):
+    # build the parsing string for PyArg_ParseTuple
+    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])
+
+    inputvecs = "/* input vectors prototypes */\n  "
+    inputvecs += "\n  ".join(map(lambda p: p['type'] + ' ' + p['name'] + ";", inputparams))
+
+    parseinput = "/* input vectors parsing */\n  "
+    for p in inputparams:
+        inputvec = p['name']
+        inputdef = p['name'] + "_obj"
+        converter = aubiovecfrompyobj[p['type']]
+        parseinput += """%(inputvec)s = %(converter)s (%(inputdef)s);
+
+  if (%(inputvec)s == NULL) {
+    return NULL;
+  }""" % locals()
+
+    # build the string for the input objects references
+    inputrefs = ", ".join(["&" + p['name'] + "_obj" for p in inputparams])
+    # end of inputs strings
+  return inputdefs, parseinput, inputrefs, inputvecs, pytypes
+
+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:
+      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]
@@ -265,73 +338,37 @@
     # 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
-
-    # build strings for inputs, assuming there is only one input 
-    inputparams = [doparams[0]]
-    # build the parsing string for PyArg_ParseTuple
-    pytypes = "".join([aubio2pytypes[p['type']] for p in doparams[0:1]])
-
-    inputdefs = "/* input vectors python prototypes */\n  "
-    inputdefs += "\n  ".join(["PyObject * " + p['name'] + "_obj;" for p in inputparams])
-
-    inputvecs = "/* input vectors prototypes */\n  "
-    inputvecs += "\n  ".join(map(lambda p: p['type'] + ' ' + p['name'] + ";", inputparams))
-
-    parseinput = "/* input vectors parsing */\n  "
-    for p in inputparams:
-        inputvec = p['name']
-        inputdef = p['name'] + "_obj"
-        converter = aubiovecfrompyobj[p['type']]
-        parseinput += """%(inputvec)s = %(converter)s (%(inputdef)s);
-
-  if (%(inputvec)s == NULL) {
-    return NULL;
-  }""" % locals()
-
-    # build the string for the input objects references
-    inputrefs = ", ".join(["&" + p['name'] + "_obj" for p in inputparams])
-    # end of inputs strings
+
+    n_param = len(doparams)
+
+    if name in param_numbers.keys():
+      n_input_param, n_output_param = param_numbers[name]
+    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
-    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'] + ")"
+    # build the parameters for the  _do() call
+    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:
-        # no output
-        outputvecs = ""
-        outputcreate = ""
-        #returnval = "Py_None";
-        returnval = "return (PyObject *)" + aubiovectopyobj[p['type']] + " (" + p['name'] + ")"
-    # end of output strings
-
-    # build the parameters for the  _do() call
-    doparams_string = "self->o, " + ", ".join([p['name'] for p in doparams])
-
+      arg_parse_tuple = ""
     # put it all together
     s = """\
@@ -340,20 +377,18 @@
 Py_%(name)s_do(Py_%(name)s * self, PyObject * args)
 {
-  %(inputdefs)s
-  %(inputvecs)s
-  %(outputvecs)s
-
-  if (!PyArg_ParseTuple (args, "%(pytypes)s", %(inputrefs)s)) {
-    return NULL;
-  }
-
-  %(parseinput)s
+%(inputdefs)s
+%(inputvecs)s
+%(outputvecs)s
+
+%(arg_parse_tuple)s
+
+%(parseinput)s
   
-  %(outputcreate)s
+%(outputcreate)s
 
   /* compute _do function */
   %(funcname)s (%(doparams_string)s);
 
-  %(returnval)s;
+%(returnval)s;
 }
 """ % locals()
Index: interfaces/python/generator.py
===================================================================
--- interfaces/python/generator.py	(revision c43c459d4fbd0a0ce9b67e6430fec10f9aa9a82b)
+++ interfaces/python/generator.py	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -32,5 +32,15 @@
   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',
+      'sink_apple_audio',
+      'sink_sndfile',
+      'source_apple_audio',
+      'source_sndfile']
 
   write_msg("-- INFO: %d objects in total" % len(cpp_objects))
Index: interfaces/python/test_source.py
===================================================================
--- interfaces/python/test_source.py	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ interfaces/python/test_source.py	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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()
+
Index: src/aubio.h
===================================================================
--- src/aubio.h	(revision c43c459d4fbd0a0ce9b67e6430fec10f9aa9a82b)
+++ src/aubio.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -175,4 +175,11 @@
 #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
Index: src/io/sink.c
===================================================================
--- src/io/sink.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/sink.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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;
+}
Index: src/io/sink.h
===================================================================
--- src/io/sink.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/sink.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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 * method, 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 */
Index: src/io/sink_apple_audio.c
===================================================================
--- src/io/sink_apple_audio.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/sink_apple_audio.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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__ */
Index: src/io/sink_apple_audio.h
===================================================================
--- src/io/sink_apple_audio.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/sink_apple_audio.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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 */
Index: src/io/sink_sndfile.c
===================================================================
--- src/io/sink_sndfile.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/sink_sndfile.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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 */
Index: src/io/sink_sndfile.h
===================================================================
--- src/io/sink_sndfile.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/sink_sndfile.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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 */
Index: src/io/sndfileio.c
===================================================================
--- src/io/sndfileio.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/sndfileio.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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 */
Index: src/io/sndfileio.h
===================================================================
--- src/io/sndfileio.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/sndfileio.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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
+
Index: src/io/source.c
===================================================================
--- src/io/source.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/source.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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);
+}
+
Index: src/io/source.h
===================================================================
--- src/io/source.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/source.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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 */
Index: src/io/source_apple_audio.c
===================================================================
--- src/io/source_apple_audio.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/source_apple_audio.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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__ */
Index: src/io/source_apple_audio.h
===================================================================
--- src/io/source_apple_audio.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/source_apple_audio.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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 */
Index: src/io/source_sndfile.c
===================================================================
--- src/io/source_sndfile.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/source_sndfile.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,0 +1,174 @@
+/*
+  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
+
+struct _aubio_source_sndfile_t {
+  uint_t hop_size;
+  uint_t samplerate;
+  uint_t channels;
+  int input_samplerate;
+  int input_channels;
+  int input_format;
+  char_t *path;
+  SNDFILE *handle;
+  uint_t scratch_size;
+  smpl_t *scratch_data;
+
+  smpl_t ratio;
+
+#ifdef HAVE_SAMPLERATE
+  aubio_resampler_t *resampler;
+  fvec_t *resampled_data;
+#endif /* HAVE_SAMPLERATE */
+};
+
+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));
+    return NULL;
+  }	
+
+  if (sfinfo.channels > MAX_CHANNELS) {
+    AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS);
+    return NULL;
+  }
+
+  /* get input specs */
+  s->input_samplerate = sfinfo.samplerate;
+  s->input_channels   = sfinfo.channels;
+  s->input_format     = sfinfo.format;
+
+  s->ratio = s->samplerate/(float)s->input_samplerate;
+
+  if (s->ratio != 1) {
+#ifdef HAVE_SAMPLERATE
+    s->resampler = new_aubio_resampler(s->ratio, 0);
+    s->resampled_data = new_fvec(s->hop_size / s->ratio);
+    if (s-> ratio > 1) {
+      AUBIO_WRN("upsampling from %d to % d\n", s->input_samplerate, s->samplerate);
+    }
+#else
+    AUBIO_ERR("can not read %s at samplerate %dHz\n", s->path, s->samplerate);
+    AUBIO_ERR("aubio was compiled without aubio_resampler\n");
+    return NULL;
+#endif /* HAVE_SAMPLERATE */
+  }
+
+  s->scratch_size = s->hop_size * s->input_channels / s->ratio;
+  /* allocate data for de/interleaving reallocated when needed. */
+  if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
+    AUBIO_ERR("%d x %d exceeds maximum aubio_source_sndfile buffer size %d\n",
+        s->hop_size, s->input_channels, MAX_CHANNELS * MAX_CHANNELS);
+    return NULL;
+  }
+  s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
+
+  return s;
+}
+
+void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
+  sf_count_t read_frames;
+  int i,j, input_channels = s->input_channels;
+  int aread;
+  /* do actual reading */
+  read_frames = sf_read_float (s->handle, s->scratch_data, s->scratch_size);
+
+  smpl_t *data;
+
+#ifdef HAVE_SAMPLERATE
+  if (s->resampler) {
+    data = s->resampled_data->data;
+  } else
+#endif /* HAVE_SAMPLERATE */
+  {
+    data = read_data->data;
+  }
+  aread = (int)FLOOR(s->ratio * read_frames / (float)input_channels + .5);
+
+  /* de-interleaving and down-mixing data  */
+  for (j = 0; j < read_frames; 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->resampled_data, read_data);
+    data = s->resampled_data->data;
+  }
+#endif /* HAVE_SAMPLERATE */
+
+  *read = aread;
+}
+
+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) {
+    del_aubio_resampler(s->resampler);
+  }
+  if (s->resampled_data) {
+    del_fvec(s->resampled_data);
+  }
+#endif /* HAVE_SAMPLERATE */
+  AUBIO_FREE(s->scratch_data);
+  AUBIO_FREE(s);
+}
+
+#endif /* HAVE_SNDFILE */
Index: src/io/source_sndfile.h
===================================================================
--- src/io/source_sndfile.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/source_sndfile.h	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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 */
Index: src/io/utils_apple_audio.c
===================================================================
--- src/io/utils_apple_audio.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ src/io/utils_apple_audio.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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__ */
Index: src/wscript_build
===================================================================
--- src/wscript_build	(revision c43c459d4fbd0a0ce9b67e6430fec10f9aa9a82b)
+++ src/wscript_build	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -1,10 +1,16 @@
 # 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
Index: tests/src/io/test-sink.c
===================================================================
--- tests/src/io/test-sink.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ tests/src/io/test-sink.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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;
+}
+
Index: tests/src/io/test-sink_apple_audio_file.c
===================================================================
--- tests/src/io/test-sink_apple_audio_file.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ tests/src/io/test-sink_apple_audio_file.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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;
+}
+
Index: tests/src/io/test-sink_sndfile.c
===================================================================
--- tests/src/io/test-sink_sndfile.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ tests/src/io/test-sink_sndfile.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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;
+}
+
Index: tests/src/io/test-source.c
===================================================================
--- tests/src/io/test-source.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ tests/src/io/test-source.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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;
+}
+
Index: tests/src/io/test-source_apple_audio_file.c
===================================================================
--- tests/src/io/test-source_apple_audio_file.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ tests/src/io/test-source_apple_audio_file.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,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;
+}
+
Index: tests/src/io/test-source_sndfile.c
===================================================================
--- tests/src/io/test-source_sndfile.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
+++ tests/src/io/test-source_sndfile.c	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <aubio.h>
+#include "config.h"
+
+char_t *path = "/home/piem/archives/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav";
+
+int main(){
+#ifdef HAVE_SNDFILE
+  uint_t samplerate = 32000;
+  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) return -1;
+
+  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]);
+  }
+
+  del_aubio_source_sndfile(s);
+#else
+  fprintf(stderr, "ERR: aubio was not compiled with aubio_source_sndfile\n");
+#endif /* HAVE_SNDFILE */
+  return 0;
+}
+
Index: wscript
===================================================================
--- wscript	(revision c43c459d4fbd0a0ce9b67e6430fec10f9aa9a82b)
+++ wscript	(revision c6c9fe9b640547858570a41ebd3859c6fc52b450)
@@ -74,4 +74,5 @@
     ctx.env.CC = 'llvm-gcc-4.2'
     ctx.env.LINK_CC = 'llvm-gcc-4.2'
+    ctx.env.FRAMEWORK = ['CoreFoundation', 'AudioToolbox']
 
   # check for required headers
