shithub: aubio

Download patch

ref: e8c4de2605e2db236009f0c59134aa3d71026d59
parent: 9ec63a0f95723491bbccb66d1994097edd586399
parent: 7816f2b8217539631d067cf49915677a783de77a
author: Paul Brossier <[email protected]>
date: Fri Dec 6 09:39:41 EST 2013

Merge branch 'avcodec' into develop

--- a/doc/web.cfg
+++ b/doc/web.cfg
@@ -702,8 +702,10 @@
 
 EXCLUDE                = ../src/aubio_priv.h \
                          ../src/mathutils .h \
+                         ../src/io/audio_unit.h \
                          ../src/io/source_sndfile.h \
                          ../src/io/source_apple_audio.h \
+                         ../src/io/source_avcodec.h \
                          ../src/io/sink_sndfile.h \
                          ../src/io/sink_apple_audio.h \
                          ../src/io/sndfileio.h \
--- a/python/lib/generator.py
+++ b/python/lib/generator.py
@@ -57,6 +57,7 @@
       'sink_sndfile',
       'source_apple_audio',
       'source_sndfile',
+      'source_avcodec',
       #'sampler',
       'audio_unit',
       ]
--- a/src/aubio.h
+++ b/src/aubio.h
@@ -193,6 +193,7 @@
 #include "mathutils.h"
 #include "io/source_sndfile.h"
 #include "io/source_apple_audio.h"
+#include "io/source_avcodec.h"
 #include "io/sink_sndfile.h"
 #include "io/sink_apple_audio.h"
 #include "io/sndfileio.h"
--- a/src/io/source.c
+++ b/src/io/source.c
@@ -23,28 +23,71 @@
 #include "fvec.h"
 #include "fmat.h"
 #include "io/source.h"
+#ifdef HAVE_AVCODEC
+#include "io/source_avcodec.h"
+#endif /* HAVE_AVCODEC */
 #ifdef __APPLE__
 #include "io/source_apple_audio.h"
 #endif /* __APPLE__ */
 #ifdef HAVE_SNDFILE
 #include "io/source_sndfile.h"
-#endif
+#endif /* HAVE_SNDFILE */
 
+typedef void (*aubio_source_do_t)(aubio_source_t * s, fvec_t * data, uint_t * read);
+typedef void (*aubio_source_do_multi_t)(aubio_source_t * s, fmat_t * data, uint_t * read);
+typedef uint_t (*aubio_source_get_samplerate_t)(aubio_source_t * s);
+typedef uint_t (*aubio_source_get_channels_t)(aubio_source_t * s);
+typedef uint_t (*aubio_source_seek_t)(aubio_source_t * s, uint_t seek);
+typedef uint_t (*del_aubio_source_t)(aubio_source_t * s);
+
 struct _aubio_source_t { 
   void *source;
+  aubio_source_do_t s_do;
+  aubio_source_do_multi_t s_do_multi;
+  aubio_source_get_samplerate_t s_get_samplerate;
+  aubio_source_get_channels_t s_get_channels;
+  aubio_source_seek_t s_seek;
+  del_aubio_source_t s_del;
 };
 
 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);
+#if HAVE_AVCODEC
+  s->source = (void *)new_aubio_source_avcodec(uri, samplerate, hop_size);
+  if (s->source) {
+    s->s_do = (aubio_source_do_t)(aubio_source_avcodec_do);
+    s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_avcodec_do_multi);
+    s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_avcodec_get_channels);
+    s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_avcodec_get_samplerate);
+    s->s_seek = (aubio_source_seek_t)(aubio_source_avcodec_seek);
+    s->s_del = (del_aubio_source_t)(del_aubio_source_avcodec);
+    return s;
+  }
+#endif /* HAVE_AVCODEC */
 #ifdef __APPLE__
   s->source = (void *)new_aubio_source_apple_audio(uri, samplerate, hop_size);
-  if (s->source) return s;
-#else /* __APPLE__ */
+  if (s->source) {
+    s->s_do = (aubio_source_do_t)(aubio_source_apple_audio_do);
+    s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_apple_audio_do_multi);
+    s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_apple_audio_get_channels);
+    s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_apple_audio_get_samplerate);
+    s->s_seek = (aubio_source_seek_t)(aubio_source_apple_audio_seek);
+    s->s_del = (del_aubio_source_t)(del_aubio_source_apple_audio);
+    return s;
+  }
+#endif /* __APPLE__ */
 #if HAVE_SNDFILE
   s->source = (void *)new_aubio_source_sndfile(uri, samplerate, hop_size);
-  if (s->source) return s;
+  if (s->source) {
+    s->s_do = (aubio_source_do_t)(aubio_source_sndfile_do);
+    s->s_do_multi = (aubio_source_do_multi_t)(aubio_source_sndfile_do_multi);
+    s->s_get_channels = (aubio_source_get_channels_t)(aubio_source_sndfile_get_channels);
+    s->s_get_samplerate = (aubio_source_get_samplerate_t)(aubio_source_sndfile_get_samplerate);
+    s->s_seek = (aubio_source_seek_t)(aubio_source_sndfile_seek);
+    s->s_del = (del_aubio_source_t)(del_aubio_source_sndfile);
+    return s;
+  }
 #endif /* HAVE_SNDFILE */
-#endif /* __APPLE__ */
   AUBIO_ERROR("failed creating aubio source with %s\n", uri);
   AUBIO_FREE(s);
   return NULL;
@@ -51,63 +94,27 @@
 }
 
 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__ */
+  s->s_do((void *)s->source, data, read);
 }
 
 void aubio_source_do_multi(aubio_source_t * s, fmat_t * data, uint_t * read) {
-#ifdef __APPLE__
-  aubio_source_apple_audio_do_multi((aubio_source_apple_audio_t *)s->source, data, read);
-#else /* __APPLE__ */
-#if HAVE_SNDFILE
-  aubio_source_sndfile_do_multi((aubio_source_sndfile_t *)s->source, data, read);
-#endif /* HAVE_SNDFILE */
-#endif /* __APPLE__ */
+  s->s_do_multi((void *)s->source, data, read);
 }
 
 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__ */
+  s->s_del((void *)s->source);
   AUBIO_FREE(s);
 }
 
 uint_t aubio_source_get_samplerate(aubio_source_t * s) {
-#ifdef __APPLE__
-  return aubio_source_apple_audio_get_samplerate((aubio_source_apple_audio_t *)s->source);
-#else /* __APPLE__ */
-#if HAVE_SNDFILE
-  return aubio_source_sndfile_get_samplerate((aubio_source_sndfile_t *)s->source);
-#endif /* HAVE_SNDFILE */
-#endif /* __APPLE__ */
+  return s->s_get_samplerate((void *)s->source);
 }
 
 uint_t aubio_source_get_channels(aubio_source_t * s) {
-#ifdef __APPLE__
-  return aubio_source_apple_audio_get_channels((aubio_source_apple_audio_t *)s->source);
-#else /* __APPLE__ */
-#if HAVE_SNDFILE
-  return aubio_source_sndfile_get_channels((aubio_source_sndfile_t *)s->source);
-#endif /* HAVE_SNDFILE */
-#endif /* __APPLE__ */
+  return s->s_get_channels((void *)s->source);
 }
 
 uint_t aubio_source_seek (aubio_source_t * s, uint_t seek ) {
-#ifdef __APPLE__
-  return aubio_source_apple_audio_seek ((aubio_source_apple_audio_t *)s->source, seek);
-#else /* __APPLE__ */
-#if HAVE_SNDFILE
-  return aubio_source_sndfile_seek ((aubio_source_sndfile_t *)s->source, seek);
-#endif /* HAVE_SNDFILE */
-#endif /* __APPLE__ */
+  return s->s_seek((void *)s->source, seek);
 }
--- a/src/io/source.h
+++ b/src/io/source.h
@@ -76,7 +76,7 @@
 
   \param s source object, created with ::new_aubio_source
   \param read_to ::fmat_t of data to read to
-  \param read upon returns, equals to number of frames actually read
+  \param[out] read upon returns, equals to number of frames actually read
 
   Upon returns, `read` contains the number of frames actually read from the
   source. `hop_size` if enough frames could be read, less otherwise.
--- /dev/null
+++ b/src/io/source_avcodec.c
@@ -1,0 +1,404 @@
+/*
+  Copyright (C) 2013 Paul Brossier <[email protected]>
+
+  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_AVCODEC
+
+#include <sndfile.h>
+#include <libavcodec/avcodec.h>
+#include <libavformat/avformat.h>
+#include <libavresample/avresample.h>
+#include <libavutil/opt.h>
+#include <stdlib.h>
+
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "fmat.h"
+#include "source_avcodec.h"
+
+#define AUBIO_AVCODEC_MAX_BUFFER_SIZE FF_MIN_BUFFER_SIZE
+
+struct _aubio_source_avcodec_t {
+  uint_t hop_size;
+  uint_t samplerate;
+  uint_t channels;
+
+  // some data about the file
+  char_t *path;
+  uint_t input_samplerate;
+  uint_t input_channels;
+
+  // avcodec stuff
+  AVFormatContext *avFormatCtx;
+  AVCodecContext *avCodecCtx;
+  AVFrame *avFrame;
+  AVAudioResampleContext *avr;
+  float *output;
+  uint_t read_samples;
+  uint_t read_index;
+  sint_t selected_stream;
+  uint_t eof;
+  uint_t multi;
+};
+
+/* convenience function to recreate the AVAudioResampleContext the first time _do
+ * or _do_multi is called */
+void aubio_source_avcodec_set_multi(aubio_source_avcodec_t * s, uint_t multi);
+
+aubio_source_avcodec_t * new_aubio_source_avcodec(char_t * path, uint_t samplerate, uint_t hop_size) {
+  aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t);
+  int err;
+  if (path == NULL) {
+    AUBIO_ERR("Aborted opening null path\n");
+    goto beach;
+  }
+  if ((sint_t)samplerate < 0) {
+    AUBIO_ERR("Can not open %s with samplerate %d\n", path, samplerate);
+    goto beach;
+  }
+  if ((sint_t)hop_size <= 0) {
+    AUBIO_ERR("Can not open %s with hop_size %d\n", path, hop_size);
+    goto beach;
+  }
+
+  s->hop_size = hop_size;
+  s->channels = 1;
+  s->path = path;
+
+  // register all formats and codecs
+  av_register_all();
+
+  // if path[0] != '/'
+  //avformat_network_init();
+
+  // try opening the file and get some info about it
+  AVFormatContext *avFormatCtx = s->avFormatCtx;
+  avFormatCtx = NULL;
+  if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) {
+    char errorstr[256];
+    av_strerror (err, errorstr, sizeof(errorstr));
+    AUBIO_ERR("Failed opening %s (%s)\n", s->path, errorstr);
+    goto beach;
+  }
+
+  // try to make sure max_analyze_duration is big enough for most songs
+  avFormatCtx->max_analyze_duration *= 100;
+
+  // retrieve stream information
+  if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) {
+    char errorstr[256];
+    av_strerror (err, errorstr, sizeof(errorstr));
+    AUBIO_ERR("Could not find stream information " "for %s (%s)\n", s->path,
+        errorstr);
+    goto beach;
+  }
+
+  // dump information about file onto standard error
+  //av_dump_format(avFormatCtx, 0, s->path, 0);
+
+  // look for the first audio stream
+  uint_t i;
+  sint_t selected_stream = -1;
+  for (i = 0; i < avFormatCtx->nb_streams; i++) {
+    if (avFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
+      if (selected_stream == -1) {
+        selected_stream = i;
+      } else {
+        AUBIO_WRN("More than one audio stream in %s, "
+            "taking the first one\n", s->path);
+      }
+    }
+  }
+  if (selected_stream == -1) {
+    AUBIO_ERR("No audio stream in %s\n", s->path);
+    goto beach;
+  }
+  //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
+  s->selected_stream = selected_stream;
+
+  AVCodecContext *avCodecCtx = s->avCodecCtx;
+  avCodecCtx = avFormatCtx->streams[selected_stream]->codec;
+  AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id);
+  if (codec == NULL) {
+    AUBIO_ERR("Could not find decoder for %s", s->path);
+    goto beach;
+  }
+
+  if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) {
+    char errorstr[256];
+    av_strerror (err, errorstr, sizeof(errorstr));
+    AUBIO_ERR("Could not load codec for %s (%s)\n", s->path, errorstr);
+    goto beach;
+  }
+
+  /* get input specs */
+  s->input_samplerate = avCodecCtx->sample_rate;
+  s->input_channels   = avCodecCtx->channels;
+  //AUBIO_DBG("input_samplerate: %d\n", s->input_samplerate);
+  //AUBIO_DBG("input_channels: %d\n", s->input_channels);
+
+  if (samplerate == 0) {
+    samplerate = s->input_samplerate;
+    //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
+  }
+  s->samplerate = samplerate;
+
+  AVFrame *avFrame = s->avFrame;
+  avFrame = avcodec_alloc_frame();
+  if (!avFrame) {
+    AUBIO_ERR("Could not allocate frame for (%s)\n", s->path);
+  }
+
+  /* allocate output for avr */
+  s->output = (float *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE * sizeof(float));
+
+  s->read_samples = 0;
+  s->read_index = 0;
+
+  s->avFormatCtx = avFormatCtx;
+  s->avCodecCtx = avCodecCtx;
+  s->avFrame = avFrame;
+
+  // default to mono output
+  aubio_source_avcodec_set_multi(s, 0);
+
+  s->eof = 0;
+  s->multi = 0;
+
+  //av_log_set_level(AV_LOG_QUIET);
+
+  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_avcodec(s);
+  return NULL;
+}
+
+void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) {
+  AVFormatContext *avFormatCtx = s->avFormatCtx;
+  AVCodecContext *avCodecCtx = s->avCodecCtx;
+  AVFrame *avFrame = s->avFrame;
+  AVPacket avPacket;
+  av_init_packet (&avPacket);
+  AVAudioResampleContext *avr = s->avr;
+  float *output = s->output;
+  *read_samples = 0;
+
+  do
+  {
+    int err = av_read_frame (avFormatCtx, &avPacket);
+    if (err == AVERROR_EOF) {
+      s->eof = 1;
+      goto beach;
+    }
+    if (err != 0) {
+      char errorstr[256];
+      av_strerror (err, errorstr, sizeof(errorstr));
+      AUBIO_ERR("Could not read frame in %s (%s)\n", s->path, errorstr);
+      goto beach;
+    }
+  } while (avPacket.stream_index != s->selected_stream);
+
+  int got_frame = 0;
+  int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket);
+
+  if (len < 0) {
+    AUBIO_ERR("Error while decoding %s\n", s->path);
+    goto beach;
+  }
+  if (got_frame == 0) {
+    //AUBIO_ERR("Could not get frame for (%s)\n", s->path);
+    goto beach;
+  }
+
+  int in_linesize = 0;
+  av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
+      avFrame->nb_samples, avCodecCtx->sample_fmt, 1);
+  int in_samples = avFrame->nb_samples;
+  int out_linesize = 0;
+  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE;
+  int out_samples = avresample_convert ( avr,
+        (uint8_t **)&output, out_linesize, max_out_samples,
+        (uint8_t **)avFrame->data, in_linesize, in_samples);
+  if (out_samples <= 0) {
+    AUBIO_ERR("No sample found while converting frame (%s)\n", s->path);
+    goto beach;
+  }
+
+  *read_samples = out_samples;
+
+beach:
+  s->avFormatCtx = avFormatCtx;
+  s->avCodecCtx = avCodecCtx;
+  s->avFrame = avFrame;
+  s->avr = avr;
+  s->output = output;
+
+  av_free_packet(&avPacket);
+}
+
+void aubio_source_avcodec_set_multi(aubio_source_avcodec_t * s, uint_t multi) {
+  if ( (multi != s->multi) || (s->avr == NULL) ) {
+    int64_t input_layout = av_get_default_channel_layout(s->input_channels);
+    int64_t mono_layout = av_get_default_channel_layout(1);
+    if (s->avr != NULL) {
+      avresample_close( s->avr );
+      av_free ( s->avr );
+      s->avr = NULL;
+    }
+    AVAudioResampleContext *avr = s->avr;
+    avr = avresample_alloc_context();
+
+    av_opt_set_int(avr, "in_channel_layout",  input_layout,           0);
+    if (multi) {
+    av_opt_set_int(avr, "out_channel_layout", input_layout,            0);
+    } else {
+    av_opt_set_int(avr, "out_channel_layout", mono_layout,            0);
+    }
+    av_opt_set_int(avr, "in_sample_rate",     s->input_samplerate,    0);
+    av_opt_set_int(avr, "out_sample_rate",    s->samplerate,          0);
+    av_opt_set_int(avr, "in_sample_fmt",      s->avCodecCtx->sample_fmt, 0);
+    av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLT,      0);
+    int err;
+    if ( ( err = avresample_open(avr) ) < 0) {
+      char errorstr[256];
+      av_strerror (err, errorstr, sizeof(errorstr));
+      AUBIO_ERR("Could not open AVAudioResampleContext for %s (%s)\n",
+          s->path, errorstr);
+      //goto beach;
+      return;
+    }
+    s->avr = avr;
+    s->multi = multi;
+  }
+}
+
+void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
+  if (s->multi == 1) aubio_source_avcodec_set_multi(s, 0);
+  uint_t i;
+  uint_t end = 0;
+  uint_t total_wrote = 0;
+  while (total_wrote < s->hop_size) {
+    end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
+    for (i = 0; i < end; i++) {
+      read_data->data[i + total_wrote] = s->output[i + s->read_index];
+    }
+    total_wrote += end;
+    if (total_wrote < s->hop_size) {
+      uint_t avcodec_read = 0;
+      aubio_source_avcodec_readframe(s, &avcodec_read);
+      s->read_samples = avcodec_read;
+      s->read_index = 0;
+      if (s->eof) {
+        break;
+      }
+    } else {
+      s->read_index += end;
+    }
+  }
+  if (total_wrote < s->hop_size) {
+    for (i = end; i < s->hop_size; i++) {
+      read_data->data[i] = 0.;
+    }
+  }
+  *read = total_wrote;
+}
+
+void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){
+  if (s->multi == 0) aubio_source_avcodec_set_multi(s, 1);
+  uint_t i,j;
+  uint_t end = 0;
+  uint_t total_wrote = 0;
+  while (total_wrote < s->hop_size) {
+    end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
+    for (j = 0; j < read_data->height; j++) {
+      for (i = 0; i < end; i++) {
+        read_data->data[j][i + total_wrote] =
+          s->output[(i + s->read_index) * s->input_channels + j];
+      }
+    }
+    total_wrote += end;
+    if (total_wrote < s->hop_size) {
+      uint_t avcodec_read = 0;
+      aubio_source_avcodec_readframe(s, &avcodec_read);
+      s->read_samples = avcodec_read;
+      s->read_index = 0;
+      if (s->eof) {
+        break;
+      }
+    } else {
+      s->read_index += end;
+    }
+  }
+  if (total_wrote < s->hop_size) {
+    for (j = 0; j < read_data->height; j++) {
+      for (i = end; i < s->hop_size; i++) {
+        read_data->data[j][i] = 0.;
+      }
+    }
+  }
+  *read = total_wrote;
+}
+
+uint_t aubio_source_avcodec_get_samplerate(aubio_source_avcodec_t * s) {
+  return s->samplerate;
+}
+
+uint_t aubio_source_avcodec_get_channels(aubio_source_avcodec_t * s) {
+  return s->input_channels;
+}
+
+uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) {
+  //uint_t resampled_pos = (uint_t)ROUND(pos * s->input_samplerate * 1. / s->samplerate);
+  AUBIO_ERR("Not implemented, _seek(%d) file %s", pos, s->path);
+  return -1; //sf_seek (s->handle, resampled_pos, SEEK_SET);
+}
+
+void del_aubio_source_avcodec(aubio_source_avcodec_t * s){
+  if (!s) return;
+  if (s->output != NULL) {
+    av_free(s->output);
+  }
+  if (s->avr != NULL) {
+    avresample_close( s->avr );
+    av_free ( s->avr );
+  }
+  s->avr = NULL;
+  if (s->avFrame != NULL) {
+    avcodec_free_frame( &(s->avFrame) );
+  }
+  s->avFrame = NULL;
+  if (s->avCodecCtx != NULL) {
+    avcodec_close ( s->avCodecCtx );
+  }
+  s->avCodecCtx = NULL;
+  if (s->avFormatCtx != NULL) {
+    avformat_close_input ( &(s->avFormatCtx) );
+  }
+  s->avFrame = NULL;
+  s->avFormatCtx = NULL;
+  AUBIO_FREE(s);
+}
+
+#endif /* HAVE_SNDFILE */
--- /dev/null
+++ b/src/io/source_avcodec.h
@@ -1,0 +1,134 @@
+/*
+  Copyright (C) 2013 Paul Brossier <[email protected]>
+
+  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_AVCODEC_H
+#define _AUBIO_SOURCE_AVCODEC_H
+
+/** \file
+
+  Read from file using [libavcodec](http://libav.org/)
+
+  Avoid including this file directly! Prefer using ::aubio_source_t instead to
+  make your code portable.
+
+  To write to file, use ::aubio_sink_t.
+
+  \example io/test-source_avcodec.c
+
+*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** avcodec media source object */
+typedef struct _aubio_source_avcodec_t aubio_source_avcodec_t;
+
+/**
+
+  create new ::aubio_source_avcodec_t
+
+  \param uri the file path or uri to read from
+  \param samplerate sampling rate to view the fie at
+  \param hop_size the size of the blocks to read from
+
+  Creates a new source object. If `0` is passed as `samplerate`, the sample
+  rate of the original file is used.
+
+  The samplerate of newly created source can be obtained using
+  ::aubio_source_avcodec_get_samplerate.
+
+*/
+aubio_source_avcodec_t * new_aubio_source_avcodec(char_t * uri, uint_t samplerate, uint_t hop_size);
+
+/**
+
+  read monophonic vector of length hop_size from source object
+
+  \param s source object, created with ::new_aubio_source_avcodec
+  \param read_to ::fvec_t of data to read to
+  \param[out] read upon returns, equals to number of frames actually read
+
+  Upon returns, `read` contains the number of frames actually read from the
+  source. `hop_size` if enough frames could be read, less otherwise.
+
+*/
+void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_to, uint_t * read);
+
+/**
+
+  read polyphonic vector of length hop_size from source object
+
+  \param s source object, created with ::new_aubio_source_avcodec
+  \param read_to ::fmat_t of data to read to
+  \param read upon returns, equals to number of frames actually read
+
+  Upon returns, `read` contains the number of frames actually read from the
+  source. `hop_size` if enough frames could be read, less otherwise.
+
+*/
+void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_to, uint_t * read);
+
+/**
+
+  get samplerate of source object
+
+  \param s source object, created with ::new_aubio_source_avcodec
+  \return samplerate, in Hz
+
+*/
+uint_t aubio_source_avcodec_get_samplerate(aubio_source_avcodec_t * s);
+
+/**
+
+  get number of channels of source object
+
+  \param s source object, created with ::new_aubio_source_avcodec
+  \return number of channels
+
+*/
+uint_t aubio_source_avcodec_get_channels (aubio_source_avcodec_t * s);
+
+/**
+
+  seek source object
+
+  \param s source object, created with ::new_aubio_source_avcodec
+  \param pos position to seek to, in frames
+
+  \return 0 if sucessful, non-zero on failure
+
+*/
+uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t *s, uint_t pos);
+
+/**
+
+  close source and cleanup memory
+
+  \param s source object, created with ::new_aubio_source_avcodec
+
+*/
+void del_aubio_source_avcodec(aubio_source_avcodec_t * s);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _AUBIO_SOURCE_AVCODEC_H */
--- a/src/io/source_sndfile.c
+++ b/src/io/source_sndfile.c
@@ -66,14 +66,22 @@
 
   if (path == NULL) {
     AUBIO_ERR("Aborted opening null path\n");
-    return NULL;
+    goto beach;
   }
+  if ((sint_t)samplerate < 0) {
+    AUBIO_ERR("Can not open %s with samplerate %d\n", path, samplerate);
+    goto beach;
+  }
+  if ((sint_t)hop_size <= 0) {
+    AUBIO_ERR("Can not open %s with hop_size %d\n", path, hop_size);
+    goto beach;
+  }
 
   s->hop_size = hop_size;
   s->channels = 1;
   s->path = path;
 
-  // try opening the file, geting the info in sfinfo
+  // try opening the file, getting the info in sfinfo
   SF_INFO sfinfo;
   AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
   s->handle = sf_open (s->path, SFM_READ, &sfinfo);
@@ -133,8 +141,8 @@
   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);
+  //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;
 }
--- a/src/wscript_build
+++ b/src/wscript_build
@@ -5,6 +5,10 @@
 uselib += ['FFTW3', 'FFTW3F']
 uselib += ['SAMPLERATE']
 uselib += ['SNDFILE']
+uselib += ['AVCODEC']
+uselib += ['AVFORMAT']
+uselib += ['AVRESAMPLE']
+uselib += ['AVUTIL']
 uselib += ['JACK']
 uselib += ['LASH']
 
--- a/tests/src/io/test-source.c
+++ b/tests/src/io/test-source.c
@@ -27,10 +27,11 @@
 
   char_t *source_path = argv[1];
 
-  fvec_t *vec = new_fvec(hop_size);
 
-  aubio_source_t* s = new_aubio_source(source_path, samplerate, hop_size);
+  aubio_source_t* s =
+    new_aubio_source(source_path, samplerate, hop_size);
   if (!s) { err = 1; goto beach; }
+  fvec_t *vec = new_fvec(hop_size);
 
   if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(s);
 
@@ -43,9 +44,8 @@
   PRINT_MSG("read %d frames at %dHz (%d blocks) from %s\n", n_frames, samplerate,
     n_frames / hop_size, source_path);
 
+  del_fvec (vec);
   del_aubio_source (s);
 beach:
-  del_fvec (vec);
-
   return err;
 }
--- a/tests/src/io/test-source_apple_audio.c
+++ b/tests/src/io/test-source_apple_audio.c
@@ -11,33 +11,47 @@
   if (argc < 2) {
     err = 2;
     PRINT_ERR("not enough arguments\n");
-    PRINT_MSG("usage: %s <source_path> [samplerate]\n", argv[0]);
+    PRINT_MSG("read a wave file as a mono vector\n");
+    PRINT_MSG("usage: %s <source_path> [samplerate] [hop_size]\n", argv[0]);
+    PRINT_MSG("examples:\n");
+    PRINT_MSG(" - read file.wav at original samplerate\n");
+    PRINT_MSG("       %s file.wav\n", argv[0]);
+    PRINT_MSG(" - read file.wav at 32000Hz\n");
+    PRINT_MSG("       %s file.aif 32000\n", argv[0]);
+    PRINT_MSG(" - read file.wav at original samplerate with 4096 blocks\n");
+    PRINT_MSG("       %s file.wav 0 4096 \n", argv[0]);
     return err;
   }
 
 #if __APPLE__
-  uint_t samplerate = 32000;
+  uint_t samplerate = 0;
   uint_t hop_size = 256;
   uint_t n_frames = 0, read = 0;
   if ( argc == 3 ) samplerate = atoi(argv[2]);
+  if ( argc == 4 ) hop_size = atoi(argv[3]);
 
   char_t *source_path = argv[1];
 
-  fvec_t *vec = new_fvec(hop_size);
-  aubio_source_apple_audio_t * s = new_aubio_source_apple_audio(source_path, samplerate, hop_size);
-  if (samplerate == 0 ) samplerate = aubio_source_apple_audio_get_samplerate(s);
 
+  aubio_source_apple_audio_t * s =
+    new_aubio_source_apple_audio(source_path, samplerate, hop_size);
   if (!s) { err = 1; goto beach; }
+  fvec_t *vec = new_fvec(hop_size);
 
+  if (samplerate == 0 ) samplerate = aubio_source_apple_audio_get_samplerate(s);
+
   do {
     aubio_source_apple_audio_do(s, vec, &read);
-    // fvec_print (vec);
+    fvec_print (vec);
     n_frames += read;
   } while ( read == hop_size );
 
-beach:
-  del_aubio_source_apple_audio (s);
+  PRINT_MSG("read %d frames at %dHz (%d blocks) from %s\n", n_frames, samplerate,
+    n_frames / hop_size, source_path);
+
   del_fvec (vec);
+  del_aubio_source_apple_audio (s);
+beach:
 #else
   err = 3;
   PRINT_ERR("aubio was not compiled with aubio_source_apple_audio\n");
--- /dev/null
+++ b/tests/src/io/test-source_avcodec.c
@@ -1,0 +1,61 @@
+#define AUBIO_UNSTABLE 1
+#include <aubio.h>
+#include "utils_tests.h"
+#include "config.h"
+
+// this file uses the unstable aubio api, please use aubio_source instead
+// see src/io/source.h and tests/src/source/test-source.c
+
+int main (int argc, char **argv)
+{
+  uint_t err = 0;
+  if (argc < 2) {
+    err = 2;
+    PRINT_ERR("not enough arguments\n");
+    PRINT_MSG("read a wave file as a mono vector\n");
+    PRINT_MSG("usage: %s <source_path> [samplerate] [hop_size]\n", argv[0]);
+    PRINT_MSG("examples:\n");
+    PRINT_MSG(" - read file.wav at original samplerate\n");
+    PRINT_MSG("       %s file.wav\n", argv[0]);
+    PRINT_MSG(" - read file.wav at 32000Hz\n");
+    PRINT_MSG("       %s file.aif 32000\n", argv[0]);
+    PRINT_MSG(" - read file.wav at original samplerate with 4096 blocks\n");
+    PRINT_MSG("       %s file.wav 0 4096 \n", argv[0]);
+    return err;
+  }
+
+#ifdef HAVE_AVCODEC
+  uint_t samplerate = 0;
+  uint_t hop_size = 256;
+  uint_t n_frames = 0, read = 0;
+  if ( argc == 3 ) samplerate = atoi(argv[2]);
+  if ( argc == 4 ) hop_size = atoi(argv[3]);
+
+  char_t *source_path = argv[1];
+
+
+  aubio_source_avcodec_t * s =
+    new_aubio_source_avcodec(source_path, samplerate, hop_size);
+  if (!s) { err = 1; goto beach; }
+  fvec_t *vec = new_fvec(hop_size);
+
+  if (samplerate == 0 ) samplerate = aubio_source_avcodec_get_samplerate(s);
+
+  do {
+    aubio_source_avcodec_do(s, vec, &read);
+    fvec_print (vec);
+    n_frames += read;
+  } while ( read == hop_size );
+
+  PRINT_MSG("read %d frames at %dHz (%d blocks) from %s\n", n_frames, samplerate,
+    n_frames / hop_size, source_path);
+
+  del_fvec (vec);
+  del_aubio_source_avcodec (s);
+beach:
+#else
+  err = 3;
+  PRINT_ERR("aubio was not compiled with aubio_source_avcodec\n");
+#endif /* HAVE_AVCODEC */
+  return err;
+}
--- a/tests/src/io/test-source_sndfile.c
+++ b/tests/src/io/test-source_sndfile.c
@@ -12,32 +12,47 @@
   if (argc < 2) {
     err = 2;
     PRINT_ERR("not enough arguments\n");
-    PRINT_MSG("usage: %s <source_path> [samplerate]\n", argv[0]);
+    PRINT_MSG("read a wave file as a mono vector\n");
+    PRINT_MSG("usage: %s <source_path> [samplerate] [hop_size]\n", argv[0]);
+    PRINT_MSG("examples:\n");
+    PRINT_MSG(" - read file.wav at original samplerate\n");
+    PRINT_MSG("       %s file.wav\n", argv[0]);
+    PRINT_MSG(" - read file.wav at 32000Hz\n");
+    PRINT_MSG("       %s file.aif 32000\n", argv[0]);
+    PRINT_MSG(" - read file.wav at original samplerate with 4096 blocks\n");
+    PRINT_MSG("       %s file.wav 0 4096 \n", argv[0]);
     return err;
   }
 
 #ifdef HAVE_SNDFILE
-  uint_t samplerate = 32000;
+  uint_t samplerate = 0;
   uint_t hop_size = 256;
   uint_t n_frames = 0, read = 0;
   if ( argc == 3 ) samplerate = atoi(argv[2]);
+  if ( argc == 4 ) hop_size = atoi(argv[3]);
 
   char_t *source_path = argv[1];
 
-  fvec_t *vec = new_fvec(hop_size);
-  aubio_source_sndfile_t * s = new_aubio_source_sndfile(source_path, samplerate, hop_size);
+
+  aubio_source_sndfile_t * s =
+    new_aubio_source_sndfile(source_path, samplerate, hop_size);
   if (!s) { err = 1; goto beach; }
+  fvec_t *vec = new_fvec(hop_size);
+
   if (samplerate == 0 ) samplerate = aubio_source_sndfile_get_samplerate(s);
 
   do {
     aubio_source_sndfile_do(s, vec, &read);
-    // fvec_print (vec);
+    fvec_print (vec);
     n_frames += read;
   } while ( read == hop_size );
 
+  PRINT_MSG("read %d frames at %dHz (%d blocks) from %s\n", n_frames, samplerate,
+    n_frames / hop_size, source_path);
+
+  del_fvec (vec);
   del_aubio_source_sndfile (s);
 beach:
-  del_fvec (vec);
 #else
   err = 3;
   PRINT_ERR("aubio was not compiled with aubio_source_sndfile\n");
--- a/wscript
+++ b/wscript
@@ -56,6 +56,8 @@
           help_str = 'compile with LASH (auto)', help_disable_str = 'disable LASH' )
   add_option_enable_disable(ctx, 'sndfile', default = None,
           help_str = 'compile with sndfile (auto)', help_disable_str = 'disable sndfile')
+  add_option_enable_disable(ctx, 'avcodec', default = None,
+          help_str = 'compile with libavcodec (auto)', help_disable_str = 'disable libavcodec')
   add_option_enable_disable(ctx, 'samplerate', default = None,
           help_str = 'compile with samplerate (auto)', help_disable_str = 'disable samplerate')
   add_option_enable_disable(ctx, 'memcpy', default = True,
@@ -198,6 +200,16 @@
   if (ctx.options.enable_lash != False):
     ctx.check_cfg(package = 'lash-1.0', atleast_version = '0.5.0',
     args = '--cflags --libs', uselib_store = 'LASH', mandatory = False)
+
+  if (ctx.options.enable_avcodec != False):
+    ctx.check_cfg(package = 'libavcodec', atleast_version = '54.35.0',
+    args = '--cflags --libs', uselib_store = 'AVCODEC', mandatory = False)
+    ctx.check_cfg(package = 'libavformat', atleast_version = '52.3.0',
+    args = '--cflags --libs', uselib_store = 'AVFORMAT', mandatory = False)
+    ctx.check_cfg(package = 'libavutil', atleast_version = '52.3.0',
+    args = '--cflags --libs', uselib_store = 'AVUTIL', mandatory = False)
+    ctx.check_cfg(package = 'libavresample', atleast_version = '1.0.1',
+    args = '--cflags --libs', uselib_store = 'AVRESAMPLE', mandatory = False)
 
   # write configuration header
   ctx.write_config_header('src/config.h')