Index: src/io/sink.c
===================================================================
--- src/io/sink.c	(revision 07c76d008560c7d5d8cce8f74ca295575fb5ec2a)
+++ src/io/sink.c	(revision 135bc14f80c3316dd46e1a365fd98a9d0ed613df)
@@ -54,6 +54,84 @@
 };
 
+extern uint_t aubio_str_path_has_extension(const char_t *filename,
+    const char_t *pattern);
+
+#ifdef HAVE_VORBISENC
+typedef struct _aubio_sink_vorbis_t aubio_sink_vorbis_t;
+extern aubio_sink_vorbis_t * new_aubio_sink_vorbis(const char_t *uri,
+    uint_t samplerate);
+extern void del_aubio_sink_vorbis (aubio_sink_vorbis_t *s);
+extern uint_t aubio_sink_vorbis_open(aubio_sink_vorbis_t *s);
+extern uint_t aubio_sink_vorbis_close(aubio_sink_vorbis_t *s);
+extern uint_t aubio_sink_vorbis_preset_channels(aubio_sink_vorbis_t *s,
+    uint_t channels);
+extern uint_t aubio_sink_vorbis_preset_samplerate(aubio_sink_vorbis_t *s,
+    uint_t samplerate);
+extern uint_t aubio_sink_vorbis_get_channels(aubio_sink_vorbis_t *s);
+extern uint_t aubio_sink_vorbis_get_samplerate(aubio_sink_vorbis_t *s);
+extern void aubio_sink_vorbis_do(aubio_sink_vorbis_t *s, fvec_t*
+    write_data, uint_t write);
+extern void aubio_sink_vorbis_do_multi(aubio_sink_vorbis_t *s, fmat_t*
+    write_data, uint_t write);
+#endif /* HAVE_VORBISENC */
+
+#ifdef HAVE_FLAC
+typedef struct _aubio_sink_flac_t aubio_sink_flac_t;
+extern aubio_sink_flac_t * new_aubio_sink_flac(const char_t *uri,
+    uint_t samplerate);
+extern void del_aubio_sink_flac (aubio_sink_flac_t *s);
+extern uint_t aubio_sink_flac_open(aubio_sink_flac_t *s);
+extern uint_t aubio_sink_flac_close(aubio_sink_flac_t *s);
+extern uint_t aubio_sink_flac_preset_channels(aubio_sink_flac_t *s,
+    uint_t channels);
+extern uint_t aubio_sink_flac_preset_samplerate(aubio_sink_flac_t *s,
+    uint_t samplerate);
+extern uint_t aubio_sink_flac_get_channels(aubio_sink_flac_t *s);
+extern uint_t aubio_sink_flac_get_samplerate(aubio_sink_flac_t *s);
+extern void aubio_sink_flac_do(aubio_sink_flac_t *s, fvec_t*
+    write_data, uint_t write);
+extern void aubio_sink_flac_do_multi(aubio_sink_flac_t *s, fmat_t*
+    write_data, uint_t write);
+#endif /* HAVE_FLAC */
+
 aubio_sink_t * new_aubio_sink(const char_t * uri, uint_t samplerate) {
   aubio_sink_t * s = AUBIO_NEW(aubio_sink_t);
+
+#ifdef HAVE_VORBISENC
+  // check if this uri could be for us
+  if (aubio_str_path_has_extension(uri, "ogg")) {
+    s->sink = (void *)new_aubio_sink_vorbis(uri, samplerate);
+    if (s->sink) {
+      s->s_do = (aubio_sink_do_t)(aubio_sink_vorbis_do);
+      s->s_do_multi = (aubio_sink_do_multi_t)(aubio_sink_vorbis_do_multi);
+      s->s_preset_samplerate = (aubio_sink_preset_samplerate_t)(aubio_sink_vorbis_preset_samplerate);
+      s->s_preset_channels = (aubio_sink_preset_channels_t)(aubio_sink_vorbis_preset_channels);
+      s->s_get_samplerate = (aubio_sink_get_samplerate_t)(aubio_sink_vorbis_get_samplerate);
+      s->s_get_channels = (aubio_sink_get_channels_t)(aubio_sink_vorbis_get_channels);
+      s->s_close = (aubio_sink_close_t)(aubio_sink_vorbis_close);
+      s->s_del = (del_aubio_sink_t)(del_aubio_sink_vorbis);
+      return s;
+    }
+  }
+#endif /* HAVE_VORBISENC */
+
+#ifdef HAVE_FLAC
+  // check if this uri could be for us
+  if (aubio_str_path_has_extension(uri, "flac")) {
+    s->sink = (void *)new_aubio_sink_flac(uri, samplerate);
+    if (s->sink) {
+      s->s_do = (aubio_sink_do_t)(aubio_sink_flac_do);
+      s->s_do_multi = (aubio_sink_do_multi_t)(aubio_sink_flac_do_multi);
+      s->s_preset_samplerate = (aubio_sink_preset_samplerate_t)(aubio_sink_flac_preset_samplerate);
+      s->s_preset_channels = (aubio_sink_preset_channels_t)(aubio_sink_flac_preset_channels);
+      s->s_get_samplerate = (aubio_sink_get_samplerate_t)(aubio_sink_flac_get_samplerate);
+      s->s_get_channels = (aubio_sink_get_channels_t)(aubio_sink_flac_get_channels);
+      s->s_close = (aubio_sink_close_t)(aubio_sink_flac_close);
+      s->s_del = (del_aubio_sink_t)(del_aubio_sink_flac);
+      return s;
+    }
+  }
+#endif /* HAVE_FLAC */
+
 #ifdef HAVE_SINK_APPLE_AUDIO
   s->sink = (void *)new_aubio_sink_apple_audio(uri, samplerate);
@@ -100,5 +178,7 @@
 #if !defined(HAVE_WAVWRITE) && \
   !defined(HAVE_SNDFILE) && \
-  !defined(HAVE_SINK_APPLE_AUDIO)
+  !defined(HAVE_SINK_APPLE_AUDIO) && \
+  !defined(HAVE_VORBISENC) && \
+  !defined(HAVE_FLAC)
   AUBIO_ERROR("sink: failed creating '%s' at %dHz (no sink built-in)\n", uri, samplerate);
 #endif
Index: src/io/sink_apple_audio.c
===================================================================
--- src/io/sink_apple_audio.c	(revision 07c76d008560c7d5d8cce8f74ca295575fb5ec2a)
+++ src/io/sink_apple_audio.c	(revision 135bc14f80c3316dd46e1a365fd98a9d0ed613df)
@@ -39,4 +39,8 @@
 uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s);
 
+uint_t aubio_str_extension_matches(const char_t *ext,
+    const char_t *pattern);
+const char_t *aubio_str_get_extension(const char_t *filename);
+
 #define MAX_SIZE 4096 // the maximum number of frames that can be written at a time
 
@@ -53,4 +57,5 @@
   ExtAudioFileRef audioFile;
   bool async;
+  AudioFileTypeID fileType;
 };
 
@@ -71,4 +76,6 @@
   s->channels = 0;
 
+  aubio_sink_apple_audio_preset_format(s, aubio_str_get_extension(uri));
+
   // zero samplerate given. do not open yet
   if ((sint_t)samplerate == 0) {
@@ -121,4 +128,72 @@
 }
 
+uint_t aubio_sink_apple_audio_preset_format(aubio_sink_apple_audio_t *s,
+    const char_t *fmt)
+{
+  if (aubio_str_extension_matches(fmt, "wav")) {
+    s->fileType = kAudioFileWAVEType;
+  } else if (aubio_str_extension_matches(fmt, "m4a")
+      || aubio_str_extension_matches(fmt, "mp4") ) {
+    // use alac for "mp4" and "m4a"
+    s->fileType = kAudioFileM4AType;
+  } else if (aubio_str_extension_matches(fmt, "aac") ) {
+    // only use lossy codec for "aac"
+    s->fileType = kAudioFileMPEG4Type;
+  } else if (aubio_str_extension_matches(fmt, "aiff") ) {
+    // only use lossy codec for "aac"
+    s->fileType = kAudioFileAIFFType;
+  } else {
+    s->fileType = kAudioFileWAVEType;
+    if (fmt && strnlen(fmt, PATH_MAX)) {
+      AUBIO_WRN("sink_apple_audio: could not guess format for %s,"
+         " using default (wav)\n", s->path);
+      return AUBIO_FAIL;
+    }
+  }
+  return AUBIO_OK;
+}
+
+static void aubio_sink_apple_audio_set_client_format(aubio_sink_apple_audio_t* s,
+    AudioStreamBasicDescription *clientFormat)
+{
+  memset(clientFormat, 0, sizeof(AudioStreamBasicDescription));
+  // always set samplerate and channels first
+  clientFormat->mSampleRate       = (Float64)(s->samplerate);
+  clientFormat->mChannelsPerFrame = s->channels;
+
+  switch (s->fileType) {
+    case kAudioFileM4AType:
+      clientFormat->mFormatID         = kAudioFormatAppleLossless;
+      break;
+    case kAudioFileMPEG4Type:
+      clientFormat->mFormatID         = kAudioFormatMPEG4AAC;
+      clientFormat->mFormatFlags      = kMPEG4Object_AAC_Main;
+      clientFormat->mFormatFlags     |= kAppleLosslessFormatFlag_16BitSourceData;
+      clientFormat->mFramesPerPacket  = 1024;
+      break;
+    case kAudioFileWAVEType:
+      clientFormat->mFormatID         = kAudioFormatLinearPCM;
+      clientFormat->mFormatFlags      = kAudioFormatFlagIsSignedInteger;
+      clientFormat->mFormatFlags     |= kAudioFormatFlagIsPacked;
+      clientFormat->mBitsPerChannel   = sizeof(short) * 8;
+      clientFormat->mFramesPerPacket  = 1;
+      clientFormat->mBytesPerFrame    = clientFormat->mBitsPerChannel * clientFormat->mChannelsPerFrame / 8;
+      clientFormat->mBytesPerPacket   = clientFormat->mFramesPerPacket * clientFormat->mBytesPerFrame;
+      break;
+    case kAudioFileAIFFType:
+      clientFormat->mFormatID         = kAudioFormatLinearPCM;
+      clientFormat->mFormatFlags      = kAudioFormatFlagIsSignedInteger;
+      clientFormat->mFormatFlags     |= kAudioFormatFlagIsPacked;
+      clientFormat->mFormatFlags     |= kAudioFormatFlagIsBigEndian;
+      clientFormat->mBitsPerChannel   = sizeof(short) * 8;
+      clientFormat->mFramesPerPacket  = 1;
+      clientFormat->mBytesPerFrame    = clientFormat->mBitsPerChannel * clientFormat->mChannelsPerFrame / 8;
+      clientFormat->mBytesPerPacket   = clientFormat->mFramesPerPacket * clientFormat->mBytesPerFrame;
+      break;
+    default:
+      break;
+  }
+}
+
 uint_t aubio_sink_apple_audio_get_samplerate(const aubio_sink_apple_audio_t *s)
 {
@@ -135,17 +210,4 @@
   if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL;
 
-  AudioStreamBasicDescription 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 = createURLFromPath(s->path);
   bool overwrite = true;
@@ -162,6 +224,11 @@
   inputFormat.mBytesPerFrame    = inputFormat.mBitsPerChannel * inputFormat.mChannelsPerFrame / 8;
   inputFormat.mBytesPerPacket   = inputFormat.mFramesPerPacket * inputFormat.mBytesPerFrame;
+
+  // get the in-file format
+  AudioStreamBasicDescription clientFormat;
+  aubio_sink_apple_audio_set_client_format(s, &clientFormat);
+
   OSStatus err = noErr;
-  err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
+  err = ExtAudioFileCreateWithURL(fileURL, s->fileType, &clientFormat, NULL,
      overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
   CFRelease(fileURL);
@@ -173,4 +240,18 @@
     goto beach;
   }
+
+#if defined(kAppleSoftwareAudioCodecManufacturer)
+  // on iOS, set software based encoding before setting clientDataFormat
+  UInt32 codecManf = kAppleSoftwareAudioCodecManufacturer;
+  err = ExtAudioFileSetProperty(s->audioFile,
+      kExtAudioFileProperty_CodecManufacturer,
+      sizeof(UInt32), &codecManf);
+  if (err) {
+    char_t errorstr[20];
+    AUBIO_ERR("sink_apple_audio: error when trying to set sofware codec on %s "
+        "(%s)\n", s->path, getPrintableOSStatusError(errorstr, err));
+    goto beach;
+  }
+#endif
 
   err = ExtAudioFileSetProperty(s->audioFile,
Index: src/io/sink_apple_audio.h
===================================================================
--- src/io/sink_apple_audio.h	(revision 07c76d008560c7d5d8cce8f74ca295575fb5ec2a)
+++ src/io/sink_apple_audio.h	(revision 135bc14f80c3316dd46e1a365fd98a9d0ed613df)
@@ -97,4 +97,27 @@
 /**
 
+  preset sink format
+
+  \param s sink, created with ::new_aubio_sink_apple_audio
+  \param fmt format of the file to create
+
+  \return 0 on success, 1 on error
+
+  Preset the format of the sink. Supported format strings:
+   - "wav": WAVE, 16 bit (default)
+   - "aiff": AIFF, 16 bit
+   - "m4a" or "mp4": Apple Audio Lossless Codec (ALAC)
+   - "aac": Audio Advanced Codec, lossy
+
+  Full list of supported encoding format is available in Table 1-2 of
+  `Multimedia Programming Guide
+  <https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html>`_.
+
+ */
+uint_t aubio_sink_apple_audio_preset_format(aubio_sink_apple_audio_t *s,
+    const char_t *fmt);
+
+/**
+
   get samplerate of sink object
 
Index: src/io/sink_sndfile.c
===================================================================
--- src/io/sink_sndfile.c	(revision 07c76d008560c7d5d8cce8f74ca295575fb5ec2a)
+++ src/io/sink_sndfile.c	(revision 135bc14f80c3316dd46e1a365fd98a9d0ed613df)
@@ -49,7 +49,12 @@
   uint_t scratch_size;
   smpl_t *scratch_data;
+  int format;
 };
 
 uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s);
+
+uint_t aubio_str_extension_matches(const char_t *ext,
+    const char_t *pattern);
+const char_t *aubio_str_get_extension(const char_t *filename);
 
 aubio_sink_sndfile_t * new_aubio_sink_sndfile(const char_t * path, uint_t samplerate) {
@@ -67,4 +72,6 @@
   s->samplerate = 0;
   s->channels = 0;
+
+  aubio_sink_sndfile_preset_format(s, aubio_str_get_extension(path));
 
   // zero samplerate given. do not open yet
@@ -112,4 +119,28 @@
   if (s->samplerate != 0 /* && s->channels != 0 */) {
     return aubio_sink_sndfile_open(s);
+  }
+  return AUBIO_OK;
+}
+
+uint_t aubio_sink_sndfile_preset_format(aubio_sink_sndfile_t *s,
+    const char_t *fmt)
+{
+  if (aubio_str_extension_matches(fmt, "wav")) {
+    s->format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
+  } else if (aubio_str_extension_matches(fmt, "aiff")) {
+    s->format = SF_FORMAT_AIFF | SF_FORMAT_PCM_16;
+  } else if (aubio_str_extension_matches(fmt, "flac")) {
+    s->format = SF_FORMAT_FLAC | SF_FORMAT_PCM_16;
+  } else if (aubio_str_extension_matches(fmt, "ogg")) {
+    s->format = SF_FORMAT_OGG | SF_FORMAT_VORBIS;
+  } else if (atoi(fmt) > 0x010000) {
+    s->format = atoi(fmt);
+  } else {
+    s->format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
+    if (fmt && strnlen(fmt, PATH_MAX))  {
+      AUBIO_WRN("sink_sndfile: could not guess format %s for %s,"
+          " using default (wav)\n", fmt, s->path);
+      return AUBIO_FAIL;
+    }
   }
   return AUBIO_OK;
@@ -132,5 +163,5 @@
   sfinfo.samplerate = s->samplerate;
   sfinfo.channels   = s->channels;
-  sfinfo.format     = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
+  sfinfo.format     = s->format;
 
   /* try creating the file */
Index: src/io/sink_sndfile.h
===================================================================
--- src/io/sink_sndfile.h	(revision 07c76d008560c7d5d8cce8f74ca295575fb5ec2a)
+++ src/io/sink_sndfile.h	(revision 135bc14f80c3316dd46e1a365fd98a9d0ed613df)
@@ -96,4 +96,37 @@
 /**
 
+  preset sink format
+
+  \param s sink, created with ::new_aubio_sink_sndfile
+  \param fmt format of the file to create
+
+  \return 0 on success, 1 on error
+
+  Preset the format of the sink. Supported format strings:
+   - "wav": 16 bit (default)
+   - "aiff": aiff, 16 bit
+   - "flac": flac, 16 bit
+   - "ogg": ogg vorbis stream
+
+  Alternatively, any sndfile format can be set by passing the corresponding
+  integer as a string:
+
+  \code{.c}
+  char_t fmt[10];
+  snprintf(fmt, sizeof(fmt), "%d", SF_FORMAT_FLAC | SF_FORMAT_PCM_24);
+  aubio_sink_sndfile_preset_format(s, fmt);
+  \endcode
+
+  The file should have been created using a samplerate of 0.
+
+  This function should be called before aubio_sink_sndfile_preset_samplerate()
+  and aubio_sink_sndfile_preset_channels().
+
+*/
+uint_t aubio_sink_sndfile_preset_format(aubio_sink_sndfile_t *s,
+        const char_t* fmt);
+
+/**
+
   get samplerate of sink object
 
Index: src/utils/strutils.c
===================================================================
--- src/utils/strutils.c	(revision 135bc14f80c3316dd46e1a365fd98a9d0ed613df)
+++ src/utils/strutils.c	(revision 135bc14f80c3316dd46e1a365fd98a9d0ed613df)
@@ -0,0 +1,43 @@
+/*
+  Copyright (C) 2018 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 "aubio_priv.h"
+
+const char_t *aubio_str_get_extension(const char_t *filename)
+{
+  // find last occurence of dot character
+  const char_t *ext;
+  if (!filename) return NULL;
+  ext = strrchr(filename, '.');
+  if (!ext || ext == filename) return "";
+  else return ext + 1;
+}
+
+uint_t aubio_str_extension_matches(const char_t *ext, const char_t *pattern)
+{
+  return ext && pattern && (strncasecmp(ext, pattern, PATH_MAX) == 0);
+}
+
+uint_t aubio_str_path_has_extension(const char_t *filename,
+    const char_t *pattern)
+{
+  const char_t *ext = aubio_str_get_extension(filename);
+  return aubio_str_extension_matches(ext, pattern);
+}
