shithub: aubio

Download patch

ref: b5de3a95bcf4d0668cd4da6923865b861f0c3188
parent: b5bd70ca13122ac12a925e443a513310606751e3
author: Paul Brossier <piem@piem.org>
date: Sun Dec 16 14:08:15 EST 2018

[io] add helpers to validate input

--- a/src/io/ioutils.c
+++ b/src/io/ioutils.c
@@ -51,3 +51,39 @@
   }
   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;
+}
--- a/src/io/ioutils.h
+++ b/src/io/ioutils.h
@@ -53,6 +53,33 @@
 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