From: Paul Brossier Date: Tue, 29 Nov 2016 11:06:21 +0000 (+0100) Subject: src/io/ioutils.h: add functions to check samplerate and channels, use in sink_*.c X-Git-Tag: 0.4.4~132 X-Git-Url: https://git.aubio.org/?p=aubio.git;a=commitdiff_plain;h=cf19b8a8231deec641741007210ce57cd96332b5 src/io/ioutils.h: add functions to check samplerate and channels, use in sink_*.c --- diff --git a/src/aubio_priv.h b/src/aubio_priv.h index 47fd64bb..72ba091d 100644 --- a/src/aubio_priv.h +++ b/src/aubio_priv.h @@ -196,6 +196,9 @@ uint_t aubio_log(sint_t level, const char_t *fmt, ...); #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) diff --git a/src/io/ioutils.c b/src/io/ioutils.c new file mode 100644 index 00000000..31bc2461 --- /dev/null +++ b/src/io/ioutils.c @@ -0,0 +1,54 @@ +/* + Copyright (C) 2016 Paul Brossier + + 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 . + +*/ + +#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; +} diff --git a/src/io/ioutils.h b/src/io/ioutils.h new file mode 100644 index 00000000..7d716afc --- /dev/null +++ b/src/io/ioutils.h @@ -0,0 +1,38 @@ +/* + Copyright (C) 2016 Paul Brossier + + 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 . + +*/ + +#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 */ diff --git a/src/io/sink_apple_audio.c b/src/io/sink_apple_audio.c index b6fd3585..c7e99fc6 100644 --- 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 @@ -73,10 +74,14 @@ aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(const char_t * uri, uint_t 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 @@ beach: 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_samplerate(aubio_sink_apple_audio_t *s, uin 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) { diff --git a/src/io/sink_sndfile.c b/src/io/sink_sndfile.c index 99392c3c..033c79f3 100644 --- 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 @@ aubio_sink_sndfile_t * new_aubio_sink_sndfile(const char_t * path, uint_t sample 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 @@ beach: 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_samplerate(aubio_sink_sndfile_t *s, uint_t samp 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) { diff --git a/src/io/sink_wavwrite.c b/src/io/sink_wavwrite.c index 1db98504..79ae402b 100644 --- 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 @@ -104,12 +105,14 @@ aubio_sink_wavwrite_t * new_aubio_sink_wavwrite(const char_t * path, uint_t samp 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 @@ beach: 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_samplerate(aubio_sink_wavwrite_t *s, uint_t sa 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) {