[sink_wavwrite] check fseek and fwrite return values
[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
23 uint_t
24 aubio_io_validate_samplerate(const char_t *kind, const char_t *path, uint_t samplerate)
25 {
26   if ((sint_t)(samplerate) <= 0) {
27     AUBIO_ERR("%s: failed creating %s, samplerate should be positive, not %d\n",
28         kind, path, samplerate);
29     return AUBIO_FAIL;
30   }
31   if ((sint_t)samplerate > AUBIO_MAX_SAMPLERATE) {
32     AUBIO_ERR("%s: failed creating %s, samplerate %dHz is too large\n",
33         kind, path, samplerate);
34     return AUBIO_FAIL;
35   }
36   return AUBIO_OK;
37 }
38
39 uint_t
40 aubio_io_validate_channels(const char_t *kind, const char_t *path, uint_t channels)
41 {
42   if ((sint_t)(channels) <= 0) {
43     AUBIO_ERR("sink_%s: failed creating %s, channels should be positive, not %d\n",
44         kind, path, channels);
45     return AUBIO_FAIL;
46   }
47   if (channels > AUBIO_MAX_CHANNELS) {
48     AUBIO_ERR("sink_%s: failed creating %s, too many channels (%d but %d available)\n",
49         kind, path, channels, AUBIO_MAX_CHANNELS);
50     return AUBIO_FAIL;
51   }
52   return AUBIO_OK;
53 }
54
55 uint_t
56 aubio_source_validate_input_length(const char_t *kind, const char_t *path,
57     uint_t hop_size, uint_t read_data_length)
58 {
59   uint_t length = hop_size;
60   if (hop_size < read_data_length) {
61     AUBIO_WRN("%s: partial read from %s, trying to read %d frames, but"
62         " hop_size is %d\n", kind, path, read_data_length, hop_size);
63   } else if (hop_size > read_data_length) {
64     AUBIO_WRN("%s: partial read from %s, trying to read %d frames into"
65         " a buffer of length %d\n", kind, path, hop_size, read_data_length);
66     length = read_data_length;
67   }
68   return length;
69 }
70
71 uint_t
72 aubio_source_validate_input_channels(const char_t *kind, const char_t *path,
73     uint_t source_channels, uint_t read_data_height)
74 {
75   uint_t channels = source_channels;
76   if (read_data_height < source_channels) {
77     AUBIO_WRN("%s: partial read from %s, trying to read %d channels,"
78         " but found output of height %d\n", kind, path, source_channels,
79         read_data_height);
80     channels = read_data_height;
81   } else if (read_data_height > source_channels) {
82     // do not show a warning when trying to read into more channels than
83     // the input source.
84 #if 0
85     AUBIO_WRN("%s: partial read from %s, trying to read %d channels,"
86         " but found output of height %d\n", kind, path, source_channels,
87         read_data_height);
88 #endif
89     channels = source_channels;
90   }
91   return channels;
92 }
93
94 uint_t
95 aubio_sink_validate_input_length(const char_t *kind, const char_t *path,
96     uint_t max_size, uint_t write_data_length, uint_t write)
97 {
98   uint_t can_write = write;
99
100   if (write > max_size) {
101     AUBIO_WRN("%s: partial write to %s, trying to write %d frames,"
102         " at most %d can be written at once\n", kind, path, write, max_size);
103     can_write = max_size;
104   }
105
106   if (can_write > write_data_length) {
107     AUBIO_WRN("%s: partial write to %s, trying to write %d frames,"
108         " but found input of length %d\n", kind, path, write,
109         write_data_length);
110     can_write = write_data_length;
111   }
112
113   return can_write;
114 }
115
116 uint_t
117 aubio_sink_validate_input_channels(const char_t *kind, const char_t *path,
118     uint_t sink_channels, uint_t write_data_height)
119 {
120   uint_t channels = sink_channels;
121   if (write_data_height < sink_channels) {
122     AUBIO_WRN("%s: partial write to %s, trying to write %d channels,"
123         " but found input of height %d\n", kind, path, sink_channels,
124         write_data_height);
125     channels = write_data_height;
126   }
127   return channels;
128 }