From b5de3a95bcf4d0668cd4da6923865b861f0c3188 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Sun, 16 Dec 2018 19:08:15 +0100 Subject: [PATCH] [io] add helpers to validate input --- src/io/ioutils.c | 36 ++++++++++++++++++++++++++++++++++++ src/io/ioutils.h | 27 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/io/ioutils.c b/src/io/ioutils.c index cce93c67..7828bebb 100644 --- a/src/io/ioutils.c +++ b/src/io/ioutils.c @@ -51,3 +51,39 @@ aubio_io_validate_channels(const char_t *kind, const char_t *path, uint_t channe } return AUBIO_OK; } + +uint_t +aubio_sink_validate_input_length(const char_t *kind, const char_t *path, + uint_t max_size, uint_t write_data_length, uint_t write) +{ + uint_t can_write = write; + + if (write > max_size) { + AUBIO_WRN("%s: partial write to %s, trying to write %d frames," + " at most %d can be written at once\n", kind, path, write, max_size); + can_write = max_size; + } + + if (can_write > write_data_length) { + AUBIO_WRN("%s: partial write to %s, trying to write %d frames," + " but found input of length %d\n", kind, path, write, + write_data_length); + can_write = write_data_length; + } + + return can_write; +} + +uint_t +aubio_sink_validate_input_channels(const char_t *kind, const char_t *path, + uint_t sink_channels, uint_t write_data_height) +{ + uint_t channels = sink_channels; + if (write_data_height < sink_channels) { + AUBIO_WRN("%s: partial write to %s, trying to write %d channels," + " but found input of height %d\n", kind, path, sink_channels, + write_data_height); + channels = write_data_height; + } + return channels; +} diff --git a/src/io/ioutils.h b/src/io/ioutils.h index 7eb65cbc..1db7b19b 100644 --- a/src/io/ioutils.h +++ b/src/io/ioutils.h @@ -53,6 +53,33 @@ uint_t aubio_io_validate_samplerate(const char_t *kind, const char_t *path, uint_t aubio_io_validate_channels(const char_t *kind, const char_t *path, uint_t channels); +/** validate length of input + + \param kind the object kind to report on + \param path the path to report on + \param max_size maximum number of frames that can be written + \param write_data_length actual length of input vector/matrix + \param write number of samples asked + + \return write or the maximum number of frames that can be written +*/ +uint_t +aubio_sink_validate_input_length(const char_t *kind, const char_t *path, + uint_t max_size, uint_t write_data_length, uint_t write); + +/** validate height of input + + \param kind the object kind to report on + \param path the path to report on + \param max_size maximum number of channels that can be written + \param write_data_height actual height of input matrix + + \return write_data_height or the maximum number of channels +*/ +uint_t +aubio_sink_validate_input_channels(const char_t *kind, const char_t *path, + uint_t sink_channels, uint_t write_data_height); + #ifdef __cplusplus } #endif -- 2.11.0