Index: src/io/source_avcodec.c
===================================================================
--- src/io/source_avcodec.c	(revision 7816f2b8217539631d067cf49915677a783de77a)
+++ src/io/source_avcodec.c	(revision 61ecd1a40f1e26ace95bdd90665ae9ae0d49bf7f)
@@ -61,7 +61,6 @@
 };
 
-/* 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);
+// hack to create or re-create the context the first time _do or _do_multi is called
+void aubio_source_avcodec_reset_resampler(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) {
@@ -180,5 +179,5 @@
 
   // default to mono output
-  aubio_source_avcodec_set_multi(s, 0);
+  aubio_source_avcodec_reset_resampler(s, 0);
 
   s->eof = 0;
@@ -196,71 +195,9 @@
 }
 
-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) {
+void aubio_source_avcodec_reset_resampler(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);
+    uint_t output_channels = multi ? s->input_channels : 1;
+    int64_t output_layout = av_get_default_channel_layout(output_channels);
     if (s->avr != NULL) {
       avresample_close( s->avr );
@@ -272,9 +209,6 @@
 
     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", output_layout,          0);
     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);
@@ -295,6 +229,69 @@
 }
 
+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_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
-  if (s->multi == 1) aubio_source_avcodec_set_multi(s, 0);
+  if (s->multi == 1) aubio_source_avcodec_reset_resampler(s, 0);
   uint_t i;
   uint_t end = 0;
@@ -327,5 +324,5 @@
 
 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);
+  if (s->multi == 0) aubio_source_avcodec_reset_resampler(s, 1);
   uint_t i,j;
   uint_t end = 0;
