2 Copyright (C) 2014 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
22 #include "aubio_priv.h"
28 #include "io/sink_wavwrite.h"
29 #include "io/ioutils.h"
33 #define FLOAT_TO_SHORT(x) (short)(x * 32768)
35 // swap endian of a short
36 #define SWAPS(x) ((x & 0xff) << 8) | ((x & 0xff00) >> 8)
38 // swap host to little endian
39 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
40 #define HTOLES(x) SWAPS(x)
41 #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
47 #define HTOLES(x) SWAPS(htons(x))
51 uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s);
53 struct _aubio_sink_wavwrite_t {
58 uint_t total_frames_written;
65 unsigned short *scratch_data;
68 static unsigned char *write_little_endian (unsigned int s, unsigned char *str,
71 static unsigned char *write_little_endian (unsigned int s, unsigned char *str,
75 for (i = 0; i < length; i++) {
76 str[i] = s >> (i * 8);
81 aubio_sink_wavwrite_t * new_aubio_sink_wavwrite(const char_t * path, uint_t samplerate) {
82 aubio_sink_wavwrite_t * s = AUBIO_NEW(aubio_sink_wavwrite_t);
85 AUBIO_ERR("sink_wavwrite: Aborted opening null path\n");
89 s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
90 strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
92 s->max_size = MAX_SIZE;
93 s->bitspersample = 16;
94 s->total_frames_written = 0;
99 // zero samplerate given. do not open yet
100 if ((sint_t)samplerate == 0) {
103 // invalid samplerate given, abort
104 if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) {
108 s->samplerate = samplerate;
111 if (aubio_sink_wavwrite_open(s) != AUBIO_OK) {
112 // open failed, abort
118 //AUBIO_ERR("sink_wavwrite: failed creating %s with samplerate %dHz\n",
119 // s->path, s->samplerate);
120 del_aubio_sink_wavwrite(s);
124 uint_t aubio_sink_wavwrite_preset_samplerate(aubio_sink_wavwrite_t *s, uint_t samplerate)
126 if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) {
129 s->samplerate = samplerate;
130 // automatically open when both samplerate and channels have been set
131 if (/* s->samplerate != 0 && */ s->channels != 0) {
132 return aubio_sink_wavwrite_open(s);
137 uint_t aubio_sink_wavwrite_preset_channels(aubio_sink_wavwrite_t *s, uint_t channels)
139 if (aubio_io_validate_channels("sink_wavwrite", s->path, channels)) {
142 s->channels = channels;
143 // automatically open when both samplerate and channels have been set
144 if (s->samplerate != 0 /* && s->channels != 0 */) {
145 return aubio_sink_wavwrite_open(s);
150 uint_t aubio_sink_wavwrite_get_samplerate(const aubio_sink_wavwrite_t *s)
152 return s->samplerate;
155 uint_t aubio_sink_wavwrite_get_channels(const aubio_sink_wavwrite_t *s)
160 uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s) {
161 unsigned char buf[5];
162 uint_t byterate, blockalign;
165 /* open output file */
166 s->fid = fopen((const char *)s->path, "wb");
168 AUBIO_STRERR("sink_wavwrite: could not open %s (%s)\n", s->path, errorstr);
173 written += fwrite("RIFF", 4, 1, s->fid);
175 // ChunkSize (0 for now, actual size will be written in _close)
176 written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
179 written += fwrite("WAVE", 4, 1, s->fid);
182 written += fwrite("fmt ", 4, 1, s->fid);
185 written += fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
188 written += fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
191 written += fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
194 written += fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
197 byterate = s->samplerate * s->channels * s->bitspersample / 8;
198 written += fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
201 blockalign = s->channels * s->bitspersample / 8;
202 written += fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
205 written += fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
208 written += fwrite("data", 4, 1, s->fid);
210 // Subchunk1Size (0 for now, actual size will be written in _close)
211 written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
213 // fwrite(*, *, 1, s->fid) was called 13 times, check success
214 if (written != 13 || fflush(s->fid)) {
215 AUBIO_STRERR("sink_wavwrite: writing header to %s failed"
216 " (wrote %d/%d, %s)\n", s->path, written, 13, errorstr);
222 s->scratch_size = s->max_size * s->channels;
223 /* allocate data for de/interleaving reallocated when needed. */
224 if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) {
225 AUBIO_ERR("sink_wavwrite: %d x %d exceeds SIZE maximum buffer size %d\n",
226 s->max_size, s->channels, MAX_SIZE * AUBIO_MAX_CHANNELS);
229 s->scratch_data = AUBIO_ARRAY(unsigned short,s->scratch_size);
238 void aubio_sink_wavwrite_write_frames(aubio_sink_wavwrite_t *s, uint_t write)
240 uint_t written_frames = 0;
242 written_frames = fwrite(s->scratch_data, 2 * s->channels, write, s->fid);
244 if (written_frames != write) {
245 AUBIO_STRERR("sink_wavwrite: trying to write %d frames to %s, but only %d"
246 " could be written (%s)\n", write, s->path, written_frames, errorstr);
248 s->total_frames_written += written_frames;
251 void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){
253 uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
254 s->max_size, write_data->length, write);
256 for (c = 0; c < s->channels; c++) {
257 for (i = 0; i < length; i++) {
258 s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[i]));
262 aubio_sink_wavwrite_write_frames(s, length);
265 void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){
268 uint_t channels = aubio_sink_validate_input_channels("sink_wavwrite", s->path,
269 s->channels, write_data->height);
270 uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
271 s->max_size, write_data->length, write);
273 for (c = 0; c < channels; c++) {
274 for (i = 0; i < length; i++) {
275 s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[c][i]));
279 aubio_sink_wavwrite_write_frames(s, length);
282 uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) {
283 uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
284 unsigned char buf[5];
285 size_t written = 0, err = 0;
286 if (!s->fid) return AUBIO_FAIL;
288 err += fseek(s->fid, 4, SEEK_SET);
289 written += fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
291 err += fseek(s->fid, 40, SEEK_SET);
292 written += fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
293 if (written != 2 || err != 0) {
294 AUBIO_STRERR("sink_wavwrite: updating header of %s failed, expected %d"
295 " write but got only %d (%s)\n", s->path, 2, written, errorstr);
298 if (fclose(s->fid)) {
299 AUBIO_STRERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, errorstr);
305 void del_aubio_sink_wavwrite(aubio_sink_wavwrite_t * s){
308 aubio_sink_wavwrite_close(s);
312 AUBIO_FREE(s->scratch_data);
316 #endif /* HAVE_WAVWRITE */