[io] add helpers to validate input
authorPaul Brossier <piem@piem.org>
Sun, 16 Dec 2018 18:08:15 +0000 (19:08 +0100)
committerPaul Brossier <piem@piem.org>
Sun, 16 Dec 2018 18:08:15 +0000 (19:08 +0100)
src/io/ioutils.c
src/io/ioutils.h

index cce93c6..7828beb 100644 (file)
@@ -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;
+}
index 7eb65cb..1db7b19 100644 (file)
@@ -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