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/>.
26 #include "aubio_priv.h"
29 #include "io/sink_wavwrite.h"
33 #define MAX_CHANNELS 6
36 #define FLOAT_TO_SHORT(x) (short)(x * 32768)
38 // swap endian of a short
39 #define SWAPS(x) ((x & 0xff) << 8) | ((x & 0xff00) >> 8)
41 // swap host to little endian
42 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
43 #define HTOLES(x) SWAPS(x)
44 #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
50 #define HTOLES(x) SWAPS(htons(x))
54 uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s);
56 struct _aubio_sink_wavwrite_t {
61 uint_t total_frames_written;
68 unsigned short *scratch_data;
71 unsigned char *write_little_endian (unsigned int s, unsigned char *str, unsigned int length);
72 unsigned char *write_little_endian (unsigned int s, unsigned char *str, unsigned int length) {
74 for (i = 0; i < length; i++) {
75 str[i] = s >> (i * 8);
80 aubio_sink_wavwrite_t * new_aubio_sink_wavwrite(const char_t * path, uint_t samplerate) {
81 aubio_sink_wavwrite_t * s = AUBIO_NEW(aubio_sink_wavwrite_t);
84 AUBIO_ERR("sink_wavwrite: Aborted opening null path\n");
87 if ((sint_t)samplerate < 0) {
88 AUBIO_ERR("sink_wavwrite: Can not create %s with samplerate %d\n", path, samplerate);
93 s->max_size = MAX_SIZE;
94 s->bitspersample = 16;
95 s->total_frames_written = 0;
100 // negative samplerate given, abort
101 if ((sint_t)samplerate < 0) goto beach;
102 // zero samplerate given. do not open yet
103 if ((sint_t)samplerate == 0) return s;
104 // samplerate way too large, fail
105 if ((sint_t)samplerate > 192000 * 4) goto beach;
107 s->samplerate = samplerate;
110 if (aubio_sink_wavwrite_open(s) != AUBIO_OK) {
111 // open failed, abort
117 //AUBIO_ERR("sink_wavwrite: failed creating %s with samplerate %dHz\n",
118 // s->path, s->samplerate);
119 del_aubio_sink_wavwrite(s);
123 uint_t aubio_sink_wavwrite_preset_samplerate(aubio_sink_wavwrite_t *s, uint_t samplerate)
125 if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
126 s->samplerate = samplerate;
127 // automatically open when both samplerate and channels have been set
128 if (s->samplerate != 0 && s->channels != 0) {
129 return aubio_sink_wavwrite_open(s);
134 uint_t aubio_sink_wavwrite_preset_channels(aubio_sink_wavwrite_t *s, uint_t channels)
136 if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
137 s->channels = channels;
138 // automatically open when both samplerate and channels have been set
139 if (s->samplerate != 0 && s->channels != 0) {
140 return aubio_sink_wavwrite_open(s);
145 uint_t aubio_sink_wavwrite_get_samplerate(const aubio_sink_wavwrite_t *s)
147 return s->samplerate;
150 uint_t aubio_sink_wavwrite_get_channels(const aubio_sink_wavwrite_t *s)
155 uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s) {
156 unsigned char buf[5];
157 uint_t byterate, blockalign;
159 /* open output file */
160 s->fid = fopen((const char *)s->path, "wb");
162 AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno));
167 fwrite("RIFF", 4, 1, s->fid);
169 // ChunkSize (0 for now, actual size will be written in _close)
170 fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
173 fwrite("WAVE", 4, 1, s->fid);
176 fwrite("fmt ", 4, 1, s->fid);
179 fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
182 fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
185 fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
188 fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
191 byterate = s->samplerate * s->channels * s->bitspersample / 8;
192 fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
195 blockalign = s->channels * s->bitspersample / 8;
196 fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
199 fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
202 fwrite("data", 4, 1, s->fid);
204 // Subchunk1Size (0 for now, actual size will be written in _close)
205 fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
207 s->scratch_size = s->max_size * s->channels;
208 /* allocate data for de/interleaving reallocated when needed. */
209 if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
210 AUBIO_ERR("sink_wavwrite: %d x %d exceeds SIZE maximum buffer size %d\n",
211 s->max_size, s->channels, MAX_SIZE * MAX_CHANNELS);
214 s->scratch_data = AUBIO_ARRAY(unsigned short,s->scratch_size);
223 void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){
224 uint_t i = 0, written_frames = 0;
226 if (write > s->max_size) {
227 AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
228 "but only %d can be written at a time\n", write, s->path, s->max_size);
232 for (i = 0; i < write; i++) {
233 s->scratch_data[i] = HTOLES(FLOAT_TO_SHORT(write_data->data[i]));
235 written_frames = fwrite(s->scratch_data, 2, write, s->fid);
237 if (written_frames != write) {
238 AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
239 "but only %d could be written\n", write, s->path, written_frames);
241 s->total_frames_written += written_frames;
245 void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){
246 uint_t c = 0, i = 0, written_frames = 0;
248 if (write > s->max_size) {
249 AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
250 "but only %d can be written at a time\n", write, s->path, s->max_size);
254 for (c = 0; c < s->channels; c++) {
255 for (i = 0; i < write; i++) {
256 s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[c][i]));
259 written_frames = fwrite(s->scratch_data, 2, write * s->channels, s->fid);
261 if (written_frames != write * s->channels) {
262 AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
263 "but only %d could be written\n", write, s->path, written_frames / s->channels);
265 s->total_frames_written += written_frames;
269 uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) {
270 uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
271 unsigned char buf[5];
272 if (!s->fid) return AUBIO_FAIL;
274 fseek(s->fid, 4, SEEK_SET);
275 fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
277 fseek(s->fid, 40, SEEK_SET);
278 fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
280 if (fclose(s->fid)) {
281 AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno));
287 void del_aubio_sink_wavwrite(aubio_sink_wavwrite_t * s){
289 aubio_sink_wavwrite_close(s);
290 AUBIO_FREE(s->scratch_data);
294 #endif /* HAVE_WAVWRITE */