Index: src/aubio_priv.h
===================================================================
--- src/aubio_priv.h	(revision c8e08c28319dc063d7ee5de62c22362f7c4c184a)
+++ src/aubio_priv.h	(revision cf19b8a8231deec641741007210ce57cd96332b5)
@@ -197,4 +197,7 @@
 #define AUBIO_SPRINTF                sprintf
 
+#define AUBIO_MAX_SAMPLERATE (192000*8)
+#define AUBIO_MAX_CHANNELS 1024
+
 /* pi and 2*pi */
 #ifndef M_PI
Index: src/io/ioutils.c
===================================================================
--- src/io/ioutils.c	(revision cf19b8a8231deec641741007210ce57cd96332b5)
+++ src/io/ioutils.c	(revision cf19b8a8231deec641741007210ce57cd96332b5)
@@ -0,0 +1,54 @@
+/*
+  Copyright (C) 2016 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 "config.h"
+#include "aubio_priv.h"
+
+uint_t
+aubio_io_validate_samplerate(const char_t *kind, const char_t *path, uint_t samplerate)
+{
+  if ((sint_t)(samplerate) <= 0) {
+    AUBIO_ERR("%s: failed creating %s, samplerate should be positive, not %d\n",
+        kind, path, samplerate);
+    return AUBIO_FAIL;
+  }
+  if ((sint_t)samplerate > AUBIO_MAX_SAMPLERATE) {
+    AUBIO_ERR("%s: failed creating %s, samplerate %dHz is too large\n",
+        kind, path, samplerate);
+    return AUBIO_FAIL;
+  }
+  return AUBIO_OK;
+}
+
+uint_t
+aubio_io_validate_channels(const char_t *kind, const char_t *path, uint_t channels)
+{
+  if ((sint_t)(channels) <= 0) {
+    AUBIO_ERR("sink_%s: failed creating %s, channels should be positive, not %d\n",
+        kind, path, channels);
+    return AUBIO_FAIL;
+  }
+  if (channels > AUBIO_MAX_CHANNELS) {
+    AUBIO_ERR("sink_%s: failed creating %s, too many channels (%d but %d available)\n",
+        kind, path, channels, AUBIO_MAX_CHANNELS);
+    return AUBIO_FAIL;
+  }
+  return AUBIO_OK;
+}
Index: src/io/ioutils.h
===================================================================
--- src/io/ioutils.h	(revision cf19b8a8231deec641741007210ce57cd96332b5)
+++ src/io/ioutils.h	(revision cf19b8a8231deec641741007210ce57cd96332b5)
@@ -0,0 +1,38 @@
+/*
+  Copyright (C) 2016 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/>.
+
+*/
+
+#ifndef AUBIO_IOUTILS_H
+#define AUBIO_IOUTILS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+uint_t aubio_io_validate_samplerate(const char_t *kind, const char_t *path,
+    uint_t samplerate);
+
+uint_t aubio_io_validate_channels(const char_t *kind, const char_t *path,
+    uint_t channels);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* AUBIO_IOUTILS_H */
Index: src/io/sink_apple_audio.c
===================================================================
--- src/io/sink_apple_audio.c	(revision c8e08c28319dc063d7ee5de62c22362f7c4c184a)
+++ src/io/sink_apple_audio.c	(revision cf19b8a8231deec641741007210ce57cd96332b5)
@@ -27,4 +27,5 @@
 #include "fmat.h"
 #include "io/sink_apple_audio.h"
+#include "io/ioutils.h"
 
 // CFURLRef, CFURLCreateWithFileSystemPath, ...
@@ -74,8 +75,12 @@
   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;
+  if ((sint_t)samplerate == 0) {
+    return s;
+  }
+  // invalid samplerate given, abort
+  if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) {
+    goto beach;
+  }
 
   s->samplerate = samplerate;
@@ -95,5 +100,7 @@
 uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate)
 {
-  if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
+  if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) {
+    return AUBIO_FAIL;
+  }
   s->samplerate = samplerate;
   // automatically open when both samplerate and channels have been set
@@ -106,5 +113,7 @@
 uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels)
 {
-  if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
+  if (aubio_io_validate_channels("sink_apple_audio", s->path, channels)) {
+    return AUBIO_FAIL;
+  }
   s->channels = channels;
   // automatically open when both samplerate and channels have been set
Index: src/io/sink_sndfile.c
===================================================================
--- src/io/sink_sndfile.c	(revision c8e08c28319dc063d7ee5de62c22362f7c4c184a)
+++ src/io/sink_sndfile.c	(revision cf19b8a8231deec641741007210ce57cd96332b5)
@@ -30,4 +30,5 @@
 #include "fmat.h"
 #include "io/sink_sndfile.h"
+#include "io/ioutils.h"
 
 #define MAX_CHANNELS 6
@@ -70,8 +71,12 @@
   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;
+  if ((sint_t)samplerate == 0) {
+    return s;
+  }
+  // invalid samplerate given, abort
+  if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
+    goto beach;
+  }
 
   s->samplerate = samplerate;
@@ -90,5 +95,7 @@
 uint_t aubio_sink_sndfile_preset_samplerate(aubio_sink_sndfile_t *s, uint_t samplerate)
 {
-  if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
+  if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
+    return AUBIO_FAIL;
+  }
   s->samplerate = samplerate;
   // automatically open when both samplerate and channels have been set
@@ -101,5 +108,7 @@
 uint_t aubio_sink_sndfile_preset_channels(aubio_sink_sndfile_t *s, uint_t channels)
 {
-  if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
+  if (aubio_io_validate_channels("sink_sndfile", s->path, channels)) {
+    return AUBIO_FAIL;
+  }
   s->channels = channels;
   // automatically open when both samplerate and channels have been set
Index: src/io/sink_wavwrite.c
===================================================================
--- src/io/sink_wavwrite.c	(revision c8e08c28319dc063d7ee5de62c22362f7c4c184a)
+++ src/io/sink_wavwrite.c	(revision cf19b8a8231deec641741007210ce57cd96332b5)
@@ -28,4 +28,5 @@
 #include "fmat.h"
 #include "io/sink_wavwrite.h"
+#include "io/ioutils.h"
 
 #include <errno.h>
@@ -105,10 +106,12 @@
   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;
-  // samplerate way too large, fail
-  if ((sint_t)samplerate > 192000 * 4) goto beach;
+  if ((sint_t)samplerate == 0) {
+    return s;
+  }
+  // invalid samplerate given, abort
+  if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) {
+    goto beach;
+  }
 
   s->samplerate = samplerate;
@@ -130,5 +133,7 @@
 uint_t aubio_sink_wavwrite_preset_samplerate(aubio_sink_wavwrite_t *s, uint_t samplerate)
 {
-  if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
+  if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) {
+    return AUBIO_FAIL;
+  }
   s->samplerate = samplerate;
   // automatically open when both samplerate and channels have been set
@@ -141,5 +146,7 @@
 uint_t aubio_sink_wavwrite_preset_channels(aubio_sink_wavwrite_t *s, uint_t channels)
 {
-  if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
+  if (aubio_io_validate_channels("sink_wavwrite", s->path, channels)) {
+    return AUBIO_FAIL;
+  }
   s->channels = channels;
   // automatically open when both samplerate and channels have been set
