1 | /* |
---|
2 | Copyright (C) 2014 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 | |
---|
22 | #include "config.h" |
---|
23 | |
---|
24 | #ifdef HAVE_WAVWRITE |
---|
25 | |
---|
26 | #include "aubio_priv.h" |
---|
27 | #include "fvec.h" |
---|
28 | #include "fmat.h" |
---|
29 | #include "io/sink_wavwrite.h" |
---|
30 | #include "io/ioutils.h" |
---|
31 | |
---|
32 | #include <errno.h> |
---|
33 | |
---|
34 | #define MAX_CHANNELS 6 |
---|
35 | #define MAX_SIZE 4096 |
---|
36 | |
---|
37 | #define FLOAT_TO_SHORT(x) (short)(x * 32768) |
---|
38 | |
---|
39 | // swap endian of a short |
---|
40 | #define SWAPS(x) ((x & 0xff) << 8) | ((x & 0xff00) >> 8) |
---|
41 | |
---|
42 | // swap host to little endian |
---|
43 | #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) |
---|
44 | #define HTOLES(x) SWAPS(x) |
---|
45 | #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) |
---|
46 | #define HTOLES(x) x |
---|
47 | #else |
---|
48 | #ifdef HAVE_WIN_HACKS |
---|
49 | #define HTOLES(x) x |
---|
50 | #else |
---|
51 | #define HTOLES(x) SWAPS(htons(x)) |
---|
52 | #endif |
---|
53 | #endif |
---|
54 | |
---|
55 | uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s); |
---|
56 | |
---|
57 | struct _aubio_sink_wavwrite_t { |
---|
58 | char_t *path; |
---|
59 | uint_t samplerate; |
---|
60 | uint_t channels; |
---|
61 | uint_t bitspersample; |
---|
62 | uint_t total_frames_written; |
---|
63 | |
---|
64 | FILE *fid; |
---|
65 | |
---|
66 | uint_t max_size; |
---|
67 | |
---|
68 | uint_t scratch_size; |
---|
69 | unsigned short *scratch_data; |
---|
70 | }; |
---|
71 | |
---|
72 | static unsigned char *write_little_endian (unsigned int s, unsigned char *str, |
---|
73 | unsigned int length); |
---|
74 | |
---|
75 | static unsigned char *write_little_endian (unsigned int s, unsigned char *str, |
---|
76 | unsigned int length) |
---|
77 | { |
---|
78 | uint_t i; |
---|
79 | for (i = 0; i < length; i++) { |
---|
80 | str[i] = s >> (i * 8); |
---|
81 | } |
---|
82 | return str; |
---|
83 | } |
---|
84 | |
---|
85 | aubio_sink_wavwrite_t * new_aubio_sink_wavwrite(const char_t * path, uint_t samplerate) { |
---|
86 | aubio_sink_wavwrite_t * s = AUBIO_NEW(aubio_sink_wavwrite_t); |
---|
87 | |
---|
88 | if (path == NULL) { |
---|
89 | AUBIO_ERR("sink_wavwrite: Aborted opening null path\n"); |
---|
90 | goto beach; |
---|
91 | } |
---|
92 | if ((sint_t)samplerate < 0) { |
---|
93 | AUBIO_ERR("sink_wavwrite: Can not create %s with samplerate %d\n", path, samplerate); |
---|
94 | goto beach; |
---|
95 | } |
---|
96 | |
---|
97 | if (s->path) AUBIO_FREE(s->path); |
---|
98 | s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1); |
---|
99 | strncpy(s->path, path, strnlen(path, PATH_MAX) + 1); |
---|
100 | |
---|
101 | s->max_size = MAX_SIZE; |
---|
102 | s->bitspersample = 16; |
---|
103 | s->total_frames_written = 0; |
---|
104 | |
---|
105 | s->samplerate = 0; |
---|
106 | s->channels = 0; |
---|
107 | |
---|
108 | // zero samplerate given. do not open yet |
---|
109 | if ((sint_t)samplerate == 0) { |
---|
110 | return s; |
---|
111 | } |
---|
112 | // invalid samplerate given, abort |
---|
113 | if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) { |
---|
114 | goto beach; |
---|
115 | } |
---|
116 | |
---|
117 | s->samplerate = samplerate; |
---|
118 | s->channels = 1; |
---|
119 | |
---|
120 | if (aubio_sink_wavwrite_open(s) != AUBIO_OK) { |
---|
121 | // open failed, abort |
---|
122 | goto beach; |
---|
123 | } |
---|
124 | |
---|
125 | return s; |
---|
126 | beach: |
---|
127 | //AUBIO_ERR("sink_wavwrite: failed creating %s with samplerate %dHz\n", |
---|
128 | // s->path, s->samplerate); |
---|
129 | del_aubio_sink_wavwrite(s); |
---|
130 | return NULL; |
---|
131 | } |
---|
132 | |
---|
133 | uint_t aubio_sink_wavwrite_preset_samplerate(aubio_sink_wavwrite_t *s, uint_t samplerate) |
---|
134 | { |
---|
135 | if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) { |
---|
136 | return AUBIO_FAIL; |
---|
137 | } |
---|
138 | s->samplerate = samplerate; |
---|
139 | // automatically open when both samplerate and channels have been set |
---|
140 | if (s->samplerate != 0 && s->channels != 0) { |
---|
141 | return aubio_sink_wavwrite_open(s); |
---|
142 | } |
---|
143 | return AUBIO_OK; |
---|
144 | } |
---|
145 | |
---|
146 | uint_t aubio_sink_wavwrite_preset_channels(aubio_sink_wavwrite_t *s, uint_t channels) |
---|
147 | { |
---|
148 | if (aubio_io_validate_channels("sink_wavwrite", s->path, channels)) { |
---|
149 | return AUBIO_FAIL; |
---|
150 | } |
---|
151 | s->channels = channels; |
---|
152 | // automatically open when both samplerate and channels have been set |
---|
153 | if (s->samplerate != 0 && s->channels != 0) { |
---|
154 | return aubio_sink_wavwrite_open(s); |
---|
155 | } |
---|
156 | return AUBIO_OK; |
---|
157 | } |
---|
158 | |
---|
159 | uint_t aubio_sink_wavwrite_get_samplerate(const aubio_sink_wavwrite_t *s) |
---|
160 | { |
---|
161 | return s->samplerate; |
---|
162 | } |
---|
163 | |
---|
164 | uint_t aubio_sink_wavwrite_get_channels(const aubio_sink_wavwrite_t *s) |
---|
165 | { |
---|
166 | return s->channels; |
---|
167 | } |
---|
168 | |
---|
169 | uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s) { |
---|
170 | unsigned char buf[5]; |
---|
171 | uint_t byterate, blockalign; |
---|
172 | |
---|
173 | /* open output file */ |
---|
174 | s->fid = fopen((const char *)s->path, "wb"); |
---|
175 | if (!s->fid) { |
---|
176 | AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno)); |
---|
177 | goto beach; |
---|
178 | } |
---|
179 | |
---|
180 | // ChunkID |
---|
181 | fwrite("RIFF", 4, 1, s->fid); |
---|
182 | |
---|
183 | // ChunkSize (0 for now, actual size will be written in _close) |
---|
184 | fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid); |
---|
185 | |
---|
186 | // Format |
---|
187 | fwrite("WAVE", 4, 1, s->fid); |
---|
188 | |
---|
189 | // Subchunk1ID |
---|
190 | fwrite("fmt ", 4, 1, s->fid); |
---|
191 | |
---|
192 | // Subchunk1Size |
---|
193 | fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid); |
---|
194 | |
---|
195 | // AudioFormat |
---|
196 | fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid); |
---|
197 | |
---|
198 | // NumChannels |
---|
199 | fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid); |
---|
200 | |
---|
201 | // SampleRate |
---|
202 | fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid); |
---|
203 | |
---|
204 | // ByteRate |
---|
205 | byterate = s->samplerate * s->channels * s->bitspersample / 8; |
---|
206 | fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid); |
---|
207 | |
---|
208 | // BlockAlign |
---|
209 | blockalign = s->channels * s->bitspersample / 8; |
---|
210 | fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid); |
---|
211 | |
---|
212 | // BitsPerSample |
---|
213 | fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid); |
---|
214 | |
---|
215 | // Subchunk2ID |
---|
216 | fwrite("data", 4, 1, s->fid); |
---|
217 | |
---|
218 | // Subchunk1Size (0 for now, actual size will be written in _close) |
---|
219 | fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid); |
---|
220 | |
---|
221 | s->scratch_size = s->max_size * s->channels; |
---|
222 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
223 | if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) { |
---|
224 | AUBIO_ERR("sink_wavwrite: %d x %d exceeds SIZE maximum buffer size %d\n", |
---|
225 | s->max_size, s->channels, MAX_SIZE * MAX_CHANNELS); |
---|
226 | goto beach; |
---|
227 | } |
---|
228 | s->scratch_data = AUBIO_ARRAY(unsigned short,s->scratch_size); |
---|
229 | |
---|
230 | return AUBIO_OK; |
---|
231 | |
---|
232 | beach: |
---|
233 | return AUBIO_FAIL; |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){ |
---|
238 | uint_t i = 0, written_frames = 0; |
---|
239 | |
---|
240 | if (write > s->max_size) { |
---|
241 | AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " |
---|
242 | "but only %d can be written at a time\n", write, s->path, s->max_size); |
---|
243 | write = s->max_size; |
---|
244 | } |
---|
245 | |
---|
246 | for (i = 0; i < write; i++) { |
---|
247 | s->scratch_data[i] = HTOLES(FLOAT_TO_SHORT(write_data->data[i])); |
---|
248 | } |
---|
249 | written_frames = fwrite(s->scratch_data, 2, write, s->fid); |
---|
250 | |
---|
251 | if (written_frames != write) { |
---|
252 | AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " |
---|
253 | "but only %d could be written\n", write, s->path, written_frames); |
---|
254 | } |
---|
255 | s->total_frames_written += written_frames; |
---|
256 | return; |
---|
257 | } |
---|
258 | |
---|
259 | void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){ |
---|
260 | uint_t c = 0, i = 0, written_frames = 0; |
---|
261 | |
---|
262 | if (write > s->max_size) { |
---|
263 | AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " |
---|
264 | "but only %d can be written at a time\n", write, s->path, s->max_size); |
---|
265 | write = s->max_size; |
---|
266 | } |
---|
267 | |
---|
268 | for (c = 0; c < s->channels; c++) { |
---|
269 | for (i = 0; i < write; i++) { |
---|
270 | s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[c][i])); |
---|
271 | } |
---|
272 | } |
---|
273 | written_frames = fwrite(s->scratch_data, 2, write * s->channels, s->fid); |
---|
274 | |
---|
275 | if (written_frames != write * s->channels) { |
---|
276 | AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " |
---|
277 | "but only %d could be written\n", write, s->path, written_frames / s->channels); |
---|
278 | } |
---|
279 | s->total_frames_written += written_frames; |
---|
280 | return; |
---|
281 | } |
---|
282 | |
---|
283 | uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) { |
---|
284 | uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8; |
---|
285 | unsigned char buf[5]; |
---|
286 | if (!s->fid) return AUBIO_FAIL; |
---|
287 | // ChunkSize |
---|
288 | fseek(s->fid, 4, SEEK_SET); |
---|
289 | fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid); |
---|
290 | // Subchunk2Size |
---|
291 | fseek(s->fid, 40, SEEK_SET); |
---|
292 | fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid); |
---|
293 | // close file |
---|
294 | if (fclose(s->fid)) { |
---|
295 | AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno)); |
---|
296 | } |
---|
297 | s->fid = NULL; |
---|
298 | return AUBIO_OK; |
---|
299 | } |
---|
300 | |
---|
301 | void del_aubio_sink_wavwrite(aubio_sink_wavwrite_t * s){ |
---|
302 | if (!s) return; |
---|
303 | aubio_sink_wavwrite_close(s); |
---|
304 | if (s->path) AUBIO_FREE(s->path); |
---|
305 | AUBIO_FREE(s->scratch_data); |
---|
306 | AUBIO_FREE(s); |
---|
307 | } |
---|
308 | |
---|
309 | #endif /* HAVE_WAVWRITE */ |
---|