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