Index: src/io/sink_apple_audio.c
===================================================================
--- src/io/sink_apple_audio.c	(revision ff6d1b6e931ea03c7056f081ea7427f040460fc5)
+++ src/io/sink_apple_audio.c	(revision ee463f9a179b719a8f31a247dec4ffdfa1439401)
@@ -32,7 +32,5 @@
 #include <AudioToolbox/AudioToolbox.h>
 
-#define FLOAT_TO_SHORT(x) (short)(x * 32768)
-
-extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
+extern int createAudioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
 extern void freeAudioBufferList(AudioBufferList *bufferList);
 extern CFURLRef createURLFromPath(const char * path);
@@ -77,4 +75,5 @@
     return s;
   }
+
   // invalid samplerate given, abort
   if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) {
@@ -151,4 +150,16 @@
   CFURLRef fileURL = createURLFromPath(s->path);
   bool overwrite = true;
+
+  // set the in-memory format
+  AudioStreamBasicDescription inputFormat;
+  memset(&inputFormat, 0, sizeof(AudioStreamBasicDescription));
+  inputFormat.mFormatID         = kAudioFormatLinearPCM;
+  inputFormat.mSampleRate       = (Float64)(s->samplerate);
+  inputFormat.mFormatFlags      = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
+  inputFormat.mChannelsPerFrame = s->channels;
+  inputFormat.mBitsPerChannel   = sizeof(smpl_t) * 8;
+  inputFormat.mFramesPerPacket  = 1;
+  inputFormat.mBytesPerFrame    = inputFormat.mBitsPerChannel * inputFormat.mChannelsPerFrame / 8;
+  inputFormat.mBytesPerPacket   = inputFormat.mFramesPerPacket * inputFormat.mBytesPerFrame;
   OSStatus err = noErr;
   err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
@@ -162,5 +173,16 @@
     goto beach;
   }
-  if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
+
+  err = ExtAudioFileSetProperty(s->audioFile,
+      kExtAudioFileProperty_ClientDataFormat,
+      sizeof(AudioStreamBasicDescription), &inputFormat);
+  if (err) {
+    char_t errorstr[20];
+    AUBIO_ERR("sink_apple_audio: error when trying to set output format on %s "
+        "(%s)\n", s->path, getPrintableOSStatusError(errorstr, err));
+    goto beach;
+  }
+
+  if (createAudioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
     AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, "
         "out of memory? \n", s->path);
@@ -175,5 +197,5 @@
 void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
   UInt32 c, v;
-  short *data = (short*)s->bufferList.mBuffers[0].mData;
+  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
   uint_t length = aubio_sink_validate_input_length("sink_apple_audio", s->path,
       s->max_frames, write_data->length, write);
@@ -181,5 +203,5 @@
   for (c = 0; c < s->channels; c++) {
     for (v = 0; v < length; v++) {
-      data[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[v]);
+      data[v * s->channels + c] = write_data->data[v];
     }
   }
@@ -190,5 +212,5 @@
 void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) {
   UInt32 c, v;
-  short *data = (short*)s->bufferList.mBuffers[0].mData;
+  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
   uint_t channels = aubio_sink_validate_input_channels("sink_apple_audio",
       s->path, s->channels, write_data->height);
@@ -198,5 +220,5 @@
   for (c = 0; c < channels; c++) {
     for (v = 0; v < length; v++) {
-      data[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[c][v]);
+      data[v * s->channels + c] = write_data->data[c][v];
     }
   }
@@ -207,4 +229,8 @@
 void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write) {
   OSStatus err = noErr;
+  // set mDataByteSize to match the number of frames to be written
+  // see https://www.mail-archive.com/coreaudio-api@lists.apple.com/msg01109.html
+  s->bufferList.mBuffers[0].mDataByteSize = write * s->channels
+    * sizeof(smpl_t);
   if (s->async) {
     err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
