shithub: aubio

Download patch

ref: cf19b8a8231deec641741007210ce57cd96332b5
parent: c8e08c28319dc063d7ee5de62c22362f7c4c184a
author: Paul Brossier <piem@piem.org>
date: Tue Nov 29 07:06:21 EST 2016

src/io/ioutils.h: add functions to check samplerate and channels, use in sink_*.c

--- a/src/aubio_priv.h
+++ b/src/aubio_priv.h
@@ -196,6 +196,9 @@
 #define AUBIO_QUIT(_s)               exit(_s)
 #define AUBIO_SPRINTF                sprintf
 
+#define AUBIO_MAX_SAMPLERATE (192000*8)
+#define AUBIO_MAX_CHANNELS 1024
+
 /* pi and 2*pi */
 #ifndef M_PI
 #define PI         (3.14159265358979323846)
--- /dev/null
+++ b/src/io/ioutils.c
@@ -1,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;
+}
--- /dev/null
+++ b/src/io/ioutils.h
@@ -1,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 */
--- a/src/io/sink_apple_audio.c
+++ b/src/io/sink_apple_audio.c
@@ -26,6 +26,7 @@
 #include "fvec.h"
 #include "fmat.h"
 #include "io/sink_apple_audio.h"
+#include "io/ioutils.h"
 
 // CFURLRef, CFURLCreateWithFileSystemPath, ...
 #include <CoreFoundation/CoreFoundation.h>
@@ -73,10 +74,14 @@
   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;
+  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;
   s->channels = 1;
@@ -94,7 +99,9 @@
 
 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
   if (s->samplerate != 0 && s->channels != 0) {
@@ -105,7 +112,9 @@
 
 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
   if (s->samplerate != 0 && s->channels != 0) {
--- a/src/io/sink_sndfile.c
+++ b/src/io/sink_sndfile.c
@@ -29,6 +29,7 @@
 #include "fvec.h"
 #include "fmat.h"
 #include "io/sink_sndfile.h"
+#include "io/ioutils.h"
 
 #define MAX_CHANNELS 6
 #define MAX_SIZE 4096
@@ -69,10 +70,14 @@
   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;
+  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;
   s->channels = 1;
@@ -89,7 +94,9 @@
 
 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
   if (s->samplerate != 0 && s->channels != 0) {
@@ -100,7 +107,9 @@
 
 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
   if (s->samplerate != 0 && s->channels != 0) {
--- a/src/io/sink_wavwrite.c
+++ b/src/io/sink_wavwrite.c
@@ -27,6 +27,7 @@
 #include "fvec.h"
 #include "fmat.h"
 #include "io/sink_wavwrite.h"
+#include "io/ioutils.h"
 
 #include <errno.h>
 
@@ -104,12 +105,14 @@
   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;
-  // 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;
   s->channels = 1;
@@ -129,7 +132,9 @@
 
 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
   if (s->samplerate != 0 && s->channels != 0) {
@@ -140,7 +145,9 @@
 
 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
   if (s->samplerate != 0 && s->channels != 0) {