Index: src/io/sink_wavwrite.c
===================================================================
--- src/io/sink_wavwrite.c	(revision 222b176186fbbbed69a93f8c3f5065461078a242)
+++ src/io/sink_wavwrite.c	(revision 870ad7018d0370e0a040a5cafdc42c2b8f320d68)
@@ -25,6 +25,7 @@
 
 #include "aubio_priv.h"
-#include "sink_wavwrite.h"
 #include "fvec.h"
+#include "fmat.h"
+#include "io/sink_wavwrite.h"
 
 #include <errno.h>
@@ -47,4 +48,6 @@
 #endif
 
+uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s);
+
 struct _aubio_sink_wavwrite_t {
   char_t *path;
@@ -73,6 +76,4 @@
 aubio_sink_wavwrite_t * new_aubio_sink_wavwrite(char_t * path, uint_t samplerate) {
   aubio_sink_wavwrite_t * s = AUBIO_NEW(aubio_sink_wavwrite_t);
-  unsigned char buf[5];
-  uint_t byterate, blockalign;
 
   if (path == NULL) {
@@ -86,70 +87,25 @@
 
   s->path = path;
-  s->samplerate = samplerate;
   s->max_size = MAX_SIZE;
-  s->channels = 1;
   s->bitspersample = 16;
   s->total_frames_written = 0;
 
-  /* set output format */
-  s->fid = fopen((const char *)path, "wb");
-  if (!s->fid) {
-    AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno));
-    goto beach;
-  }
-
-  // ChunkID
-  fwrite("RIFF", 4, 1, s->fid);
-
-  // ChunkSize (0 for now, actual size will be written in _close)
-  fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
-
-  // Format
-  fwrite("WAVE", 4, 1, s->fid);
-
-  // Subchunk1ID
-  fwrite("fmt ", 4, 1, s->fid);
-
-  // Subchunk1Size
-  fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
-
-  // AudioFormat
-  fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
-
-  // NumChannels
-  fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
-
-  // SampleRate
-  fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
-
-  // ByteRate
-  byterate = s->samplerate * s->channels * s->bitspersample / 8;
-  fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
-
-  // BlockAlign
-  blockalign = s->channels * s->bitspersample / 8;
-  fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
-
-  // BitsPerSample
-  fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
-
-  // Subchunk2ID
-  fwrite("data", 4, 1, s->fid);
-
-  // Subchunk1Size (0 for now, actual size will be written in _close)
-  fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
-
-  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("sink_wavwrite: %d x %d exceeds maximum buffer size %d\n",
-        s->max_size, s->channels, MAX_CHANNELS * MAX_CHANNELS);
-    AUBIO_FREE(s);
-    return NULL;
-  }
-  s->scratch_data = AUBIO_ARRAY(unsigned short,s->scratch_size);
+  s->samplerate = 0;
+  s->channels = 0;
+
+  // negative samplerate given, abort
+  if ((sint_t)samplerate < 0) goto beach;
+  // zero samplerate given. do not open yet
+  if ((sint_t)samplerate == 0) return s;
+
+  s->samplerate = samplerate;
+  s->channels = 1;
+
+  if (aubio_sink_wavwrite_open(s) != AUBIO_OK) {
+    // open failed, abort
+    goto beach;
+  }
 
   return s;
-
 beach:
   //AUBIO_ERR("sink_wavwrite: failed creating %s with samplerate %dHz\n",
@@ -159,4 +115,103 @@
 }
 
+uint_t aubio_sink_wavwrite_preset_samplerate(aubio_sink_wavwrite_t *s, uint_t samplerate)
+{
+  if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
+  s->samplerate = samplerate;
+  // automatically open when both samplerate and channels have been set
+  if (s->samplerate != 0 && s->channels != 0) {
+    return aubio_sink_wavwrite_open(s);
+  }
+  return AUBIO_OK;
+}
+
+uint_t aubio_sink_wavwrite_preset_channels(aubio_sink_wavwrite_t *s, uint_t channels)
+{
+  if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
+  s->channels = channels;
+  // automatically open when both samplerate and channels have been set
+  if (s->samplerate != 0 && s->channels != 0) {
+    return aubio_sink_wavwrite_open(s);
+  }
+  return AUBIO_OK;
+}
+
+uint_t aubio_sink_wavwrite_get_samplerate(aubio_sink_wavwrite_t *s)
+{
+  return s->samplerate;
+}
+
+uint_t aubio_sink_wavwrite_get_channels(aubio_sink_wavwrite_t *s)
+{
+  return s->channels;
+}
+
+uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s) {
+  unsigned char buf[5];
+  uint_t byterate, blockalign;
+
+  /* open output file */
+  s->fid = fopen((const char *)s->path, "wb");
+  if (!s->fid) {
+    AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno));
+    goto beach;
+  }
+
+  // ChunkID
+  fwrite("RIFF", 4, 1, s->fid);
+
+  // ChunkSize (0 for now, actual size will be written in _close)
+  fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
+
+  // Format
+  fwrite("WAVE", 4, 1, s->fid);
+
+  // Subchunk1ID
+  fwrite("fmt ", 4, 1, s->fid);
+
+  // Subchunk1Size
+  fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
+
+  // AudioFormat
+  fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
+
+  // NumChannels
+  fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
+
+  // SampleRate
+  fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
+
+  // ByteRate
+  byterate = s->samplerate * s->channels * s->bitspersample / 8;
+  fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
+
+  // BlockAlign
+  blockalign = s->channels * s->bitspersample / 8;
+  fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
+
+  // BitsPerSample
+  fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
+
+  // Subchunk2ID
+  fwrite("data", 4, 1, s->fid);
+
+  // Subchunk1Size (0 for now, actual size will be written in _close)
+  fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
+
+  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("sink_wavwrite: %d x %d exceeds SIZE maximum buffer size %d\n",
+        s->max_size, s->channels, MAX_SIZE * MAX_CHANNELS);
+    goto beach;
+  }
+  s->scratch_data = AUBIO_ARRAY(unsigned short,s->scratch_size);
+
+  return AUBIO_OK;
+
+beach:
+  return AUBIO_FAIL;
+}
+
 
 void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){
@@ -177,4 +232,28 @@
     AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
         "but only %d could be written\n", write, s->path, written_frames);
+  }
+  s->total_frames_written += written_frames;
+  return;
+}
+
+void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){
+  uint_t c = 0, i = 0, written_frames = 0;
+
+  if (write > s->max_size) {
+    AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
+        "but only %d can be written at a time\n", write, s->path, s->max_size);
+    write = s->max_size;
+  }
+
+  for (c = 0; c < s->channels; c++) {
+    for (i = 0; i < write; i++) {
+      s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[c][i]));
+    }
+  }
+  written_frames = fwrite(s->scratch_data, 2, write * s->channels, s->fid);
+
+  if (written_frames != write * s->channels) {
+    AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
+        "but only %d could be written\n", write, s->path, written_frames / s->channels);
   }
   s->total_frames_written += written_frames;
Index: src/io/sink_wavwrite.h
===================================================================
--- src/io/sink_wavwrite.h	(revision 222b176186fbbbed69a93f8c3f5065461078a242)
+++ src/io/sink_wavwrite.h	(revision 870ad7018d0370e0a040a5cafdc42c2b8f320d68)
@@ -24,5 +24,5 @@
 /** \file
 
-  Write to file using [libsndfile](http://www.mega-nerd.com/libsndfile/)
+  Write to file using native file writer.
 
   Avoid including this file directly! Prefer using ::aubio_sink_t instead to
@@ -39,4 +39,5 @@
 #endif
 
+/** sink_wavwrite object */
 typedef struct _aubio_sink_wavwrite_t aubio_sink_wavwrite_t;
 
@@ -52,6 +53,64 @@
   Creates a new sink object.
 
+  If samplerate is set to 0, the creation of the file will be delayed until
+  both ::aubio_sink_preset_samplerate and ::aubio_sink_preset_channels have
+  been called.
+
 */
 aubio_sink_wavwrite_t * new_aubio_sink_wavwrite(char_t * uri, uint_t samplerate);
+
+/**
+
+  preset sink samplerate
+
+  \param s sink, created with ::new_aubio_sink_wavwrite
+  \param samplerate samplerate to preset the sink to, in Hz
+
+  \return 0 on success, 1 on error
+
+  Preset the samplerate of the sink. The file should have been created using a
+  samplerate of 0.
+
+  The file will be opened only when both samplerate and channels have been set.
+
+*/
+uint_t aubio_sink_wavwrite_preset_samplerate(aubio_sink_wavwrite_t *s, uint_t samplerate);
+
+/**
+
+  preset sink channels
+
+  \param s sink, created with ::new_aubio_sink_wavwrite
+  \param channels number of channels to preset the sink to
+
+  \return 0 on success, 1 on error
+
+  Preset the samplerate of the sink. The file should have been created using a
+  samplerate of 0.
+
+  The file will be opened only when both samplerate and channels have been set.
+
+*/
+uint_t aubio_sink_wavwrite_preset_channels(aubio_sink_wavwrite_t *s, uint_t channels);
+
+/**
+
+  get samplerate of sink object
+
+  \param s sink object, created with ::new_aubio_sink_wavwrite
+  \return samplerate, in Hz
+
+*/
+uint_t aubio_sink_wavwrite_get_samplerate(aubio_sink_wavwrite_t *s);
+
+/**
+
+  get channels of sink object
+
+  \param s sink object, created with ::new_aubio_sink_wavwrite
+  \return number of channels
+
+*/
+uint_t aubio_sink_wavwrite_get_channels(aubio_sink_wavwrite_t *s);
 
 /**
@@ -65,4 +124,15 @@
 */
 void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t * s, fvec_t * write_data, uint_t write);
+
+/**
+
+  write polyphonic vector of length hop_size to sink
+
+  \param s sink, created with ::new_aubio_sink_wavwrite
+  \param write_data ::fmat_t samples to write to sink
+  \param write number of frames to write
+
+*/
+void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t * s, fmat_t * write_data, uint_t write);
 
 /**
