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"
35 #define FLOAT_TO_SHORT(x) (short)(x * 32768)
37 // swap endian of a short
38 #define SWAPS(x) ((x & 0xff) << 8) | ((x & 0xff00) >> 8)
40 // swap host to little endian
41 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
42 #define HTOLES(x) SWAPS(x)
43 #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
49 #define HTOLES(x) SWAPS(htons(x))
53 uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s);
55 struct _aubio_sink_wavwrite_t {
60 uint_t total_frames_written;
67 unsigned short *scratch_data;
70 static unsigned char *write_little_endian (unsigned int s, unsigned char *str,
73 static unsigned char *write_little_endian (unsigned int s, unsigned char *str,
77 for (i = 0; i < length; i++) {
78 str[i] = s >> (i * 8);
83 aubio_sink_wavwrite_t * new_aubio_sink_wavwrite(const char_t * path, uint_t samplerate) {
84 aubio_sink_wavwrite_t * s = AUBIO_NEW(aubio_sink_wavwrite_t);
87 AUBIO_ERR("sink_wavwrite: Aborted opening null path\n");
91 s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
92 strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
94 s->max_size = MAX_SIZE;
95 s->bitspersample = 16;
96 s->total_frames_written = 0;
101 // zero samplerate given. do not open yet
102 if ((sint_t)samplerate == 0) {
105 // invalid samplerate given, abort
106 if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) {
110 s->samplerate = samplerate;
113 if (aubio_sink_wavwrite_open(s) != AUBIO_OK) {
114 // open failed, abort
120 //AUBIO_ERR("sink_wavwrite: failed creating %s with samplerate %dHz\n",
121 // s->path, s->samplerate);
122 del_aubio_sink_wavwrite(s);
126 uint_t aubio_sink_wavwrite_preset_samplerate(aubio_sink_wavwrite_t *s, uint_t samplerate)
128 if (aubio_io_validate_samplerate("sink_wavwrite", s->path, samplerate)) {
131 s->samplerate = samplerate;
132 // automatically open when both samplerate and channels have been set
133 if (/* s->samplerate != 0 && */ s->channels != 0) {
134 return aubio_sink_wavwrite_open(s);
139 uint_t aubio_sink_wavwrite_preset_channels(aubio_sink_wavwrite_t *s, uint_t channels)
141 if (aubio_io_validate_channels("sink_wavwrite", s->path, channels)) {
144 s->channels = channels;
145 // automatically open when both samplerate and channels have been set
146 if (s->samplerate != 0 /* && s->channels != 0 */) {
147 return aubio_sink_wavwrite_open(s);
152 uint_t aubio_sink_wavwrite_get_samplerate(const aubio_sink_wavwrite_t *s)
154 return s->samplerate;
157 uint_t aubio_sink_wavwrite_get_channels(const aubio_sink_wavwrite_t *s)
162 uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s) {
163 unsigned char buf[5];
164 uint_t byterate, blockalign;
166 /* open output file */
167 s->fid = fopen((const char *)s->path, "wb");
169 AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno));
174 fwrite("RIFF", 4, 1, s->fid);
176 // ChunkSize (0 for now, actual size will be written in _close)
177 fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
180 fwrite("WAVE", 4, 1, s->fid);
183 fwrite("fmt ", 4, 1, s->fid);
186 fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
189 fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
192 fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
195 fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
198 byterate = s->samplerate * s->channels * s->bitspersample / 8;
199 fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
202 blockalign = s->channels * s->bitspersample / 8;
203 fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
206 fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
209 fwrite("data", 4, 1, s->fid);
211 // Subchunk1Size (0 for now, actual size will be written in _close)
212 fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
214 s->scratch_size = s->max_size * s->channels;
215 /* allocate data for de/interleaving reallocated when needed. */
216 if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) {
217 AUBIO_ERR("sink_wavwrite: %d x %d exceeds SIZE maximum buffer size %d\n",
218 s->max_size, s->channels, MAX_SIZE * AUBIO_MAX_CHANNELS);
221 s->scratch_data = AUBIO_ARRAY(unsigned short,s->scratch_size);
230 void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){
231 uint_t c = 0, i = 0, written_frames = 0;
232 uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
233 s->max_size, write_data->length, write);
235 for (c = 0; c < s->channels; c++) {
236 for (i = 0; i < length; i++) {
237 s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[i]));
240 written_frames = fwrite(s->scratch_data, 2, length * s->channels, s->fid);
242 if (written_frames != write) {
243 AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
244 "but only %d could be written\n", write, s->path, written_frames);
246 s->total_frames_written += written_frames;
250 void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){
251 uint_t c = 0, i = 0, written_frames = 0;
253 uint_t channels = aubio_sink_validate_input_channels("sink_wavwrite", s->path,
254 s->channels, write_data->height);
255 uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
256 s->max_size, write_data->length, write);
258 for (c = 0; c < channels; c++) {
259 for (i = 0; i < length; i++) {
260 s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[c][i]));
263 written_frames = fwrite(s->scratch_data, 2, length * s->channels, s->fid);
265 if (written_frames != write * s->channels) {
266 AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
267 "but only %d could be written\n", write, s->path, written_frames / s->channels);
269 s->total_frames_written += written_frames;
273 uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) {
274 uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
275 unsigned char buf[5];
276 if (!s->fid) return AUBIO_FAIL;
278 fseek(s->fid, 4, SEEK_SET);
279 fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
281 fseek(s->fid, 40, SEEK_SET);
282 fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
284 if (fclose(s->fid)) {
285 AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno));
291 void del_aubio_sink_wavwrite(aubio_sink_wavwrite_t * s){
294 aubio_sink_wavwrite_close(s);
298 AUBIO_FREE(s->scratch_data);
302 #endif /* HAVE_WAVWRITE */