[io] use memset in padding helpers
[aubio.git] / src / io / ioutils.c
1 /*
2   Copyright (C) 2016 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
6   aubio is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   aubio is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "aubio_priv.h"
22 #include "fmat.h"
23
24 uint_t
25 aubio_io_validate_samplerate(const char_t *kind, const char_t *path, uint_t samplerate)
26 {
27   if ((sint_t)(samplerate) <= 0) {
28     AUBIO_ERR("%s: failed creating %s, samplerate should be positive, not %d\n",
29         kind, path, samplerate);
30     return AUBIO_FAIL;
31   }
32   if ((sint_t)samplerate > AUBIO_MAX_SAMPLERATE) {
33     AUBIO_ERR("%s: failed creating %s, samplerate %dHz is too large\n",
34         kind, path, samplerate);
35     return AUBIO_FAIL;
36   }
37   return AUBIO_OK;
38 }
39
40 uint_t
41 aubio_io_validate_channels(const char_t *kind, const char_t *path, uint_t channels)
42 {
43   if ((sint_t)(channels) <= 0) {
44     AUBIO_ERR("sink_%s: failed creating %s, channels should be positive, not %d\n",
45         kind, path, channels);
46     return AUBIO_FAIL;
47   }
48   if (channels > AUBIO_MAX_CHANNELS) {
49     AUBIO_ERR("sink_%s: failed creating %s, too many channels (%d but %d available)\n",
50         kind, path, channels, AUBIO_MAX_CHANNELS);
51     return AUBIO_FAIL;
52   }
53   return AUBIO_OK;
54 }
55
56 uint_t
57 aubio_source_validate_input_length(const char_t *kind, const char_t *path,
58     uint_t hop_size, uint_t read_data_length)
59 {
60   uint_t length = hop_size;
61   if (hop_size < read_data_length) {
62     AUBIO_WRN("%s: partial read from %s, trying to read %d frames, but"
63         " hop_size is %d\n", kind, path, read_data_length, hop_size);
64   } else if (hop_size > read_data_length) {
65     AUBIO_WRN("%s: partial read from %s, trying to read %d frames into"
66         " a buffer of length %d\n", kind, path, hop_size, read_data_length);
67     length = read_data_length;
68   }
69   return length;
70 }
71
72 uint_t
73 aubio_source_validate_input_channels(const char_t *kind, const char_t *path,
74     uint_t source_channels, uint_t read_data_height)
75 {
76   uint_t channels = source_channels;
77   if (read_data_height < source_channels) {
78     AUBIO_WRN("%s: partial read from %s, trying to read %d channels,"
79         " but found output of height %d\n", kind, path, source_channels,
80         read_data_height);
81     channels = read_data_height;
82   } else if (read_data_height > source_channels) {
83     // do not show a warning when trying to read into more channels than
84     // the input source.
85 #if 0
86     AUBIO_WRN("%s: partial read from %s, trying to read %d channels,"
87         " but found output of height %d\n", kind, path, source_channels,
88         read_data_height);
89 #endif
90     channels = source_channels;
91   }
92   return channels;
93 }
94
95 void
96 aubio_source_pad_output (fvec_t *read_data, uint_t source_read)
97 {
98   if (source_read < read_data->length) {
99     AUBIO_MEMSET(read_data->data + source_read, 0, read_data->length -
100         source_read);
101   }
102 }
103
104 void
105 aubio_source_pad_multi_output (fmat_t *read_data,
106     uint_t source_channels, uint_t source_read) {
107   uint_t i;
108   if (source_read < read_data->length) {
109     for (i = 0; i < read_data->height; i++) {
110       AUBIO_MEMSET(read_data->data[i] + source_read, 0,
111           read_data->length - source_read);
112     }
113   }
114 }
115
116 uint_t
117 aubio_sink_validate_input_length(const char_t *kind, const char_t *path,
118     uint_t max_size, uint_t write_data_length, uint_t write)
119 {
120   uint_t can_write = write;
121
122   if (write > max_size) {
123     AUBIO_WRN("%s: partial write to %s, trying to write %d frames,"
124         " at most %d can be written at once\n", kind, path, write, max_size);
125     can_write = max_size;
126   }
127
128   if (can_write > write_data_length) {
129     AUBIO_WRN("%s: partial write to %s, trying to write %d frames,"
130         " but found input of length %d\n", kind, path, write,
131         write_data_length);
132     can_write = write_data_length;
133   }
134
135   return can_write;
136 }
137
138 uint_t
139 aubio_sink_validate_input_channels(const char_t *kind, const char_t *path,
140     uint_t sink_channels, uint_t write_data_height)
141 {
142   uint_t channels = sink_channels;
143   if (write_data_height < sink_channels) {
144     AUBIO_WRN("%s: partial write to %s, trying to write %d channels,"
145         " but found input of height %d\n", kind, path, sink_channels,
146         write_data_height);
147     channels = write_data_height;
148   }
149   return channels;
150 }