[ci] add pip install to readthedocs.yaml
[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,
100         (read_data->length - source_read) * sizeof(smpl_t));
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) * sizeof(smpl_t));
112     }
113   }
114
115   // destination matrix has more channels than the file
116   // copy channels from the source to extra output channels
117   if (read_data->height > source_channels) {
118     for (i = source_channels; i < read_data->height; i++) {
119       AUBIO_MEMCPY(read_data->data[i], read_data->data[i % source_channels],
120           sizeof(smpl_t) * read_data->length);
121     }
122   }
123 }
124
125 uint_t
126 aubio_sink_validate_input_length(const char_t *kind, const char_t *path,
127     uint_t max_size, uint_t write_data_length, uint_t write)
128 {
129   uint_t can_write = write;
130
131   if (write > max_size) {
132     AUBIO_WRN("%s: partial write to %s, trying to write %d frames,"
133         " at most %d can be written at once\n", kind, path, write, max_size);
134     can_write = max_size;
135   }
136
137   if (can_write > write_data_length) {
138     AUBIO_WRN("%s: partial write to %s, trying to write %d frames,"
139         " but found input of length %d\n", kind, path, write,
140         write_data_length);
141     can_write = write_data_length;
142   }
143
144   return can_write;
145 }
146
147 uint_t
148 aubio_sink_validate_input_channels(const char_t *kind, const char_t *path,
149     uint_t sink_channels, uint_t write_data_height)
150 {
151   uint_t channels = sink_channels;
152   if (write_data_height < sink_channels) {
153     AUBIO_WRN("%s: partial write to %s, trying to write %d channels,"
154         " but found input of height %d\n", kind, path, sink_channels,
155         write_data_height);
156     channels = write_data_height;
157   }
158   return channels;
159 }