d99c16289c83c86e85a527f89c0d8cce378b1d70
[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
31 #include <errno.h>
32
33 #define MAX_CHANNELS 6
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 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) {
73   uint_t i;
74   for (i = 0; i < length; i++) {
75     str[i] = s >> (i * 8);
76   }
77   return str;
78 }
79
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);
82
83   if (path == NULL) {
84     AUBIO_ERR("sink_wavwrite: Aborted opening null path\n");
85     goto beach;
86   }
87   if ((sint_t)samplerate < 0) {
88     AUBIO_ERR("sink_wavwrite: Can not create %s with samplerate %d\n", path, samplerate);
89     goto beach;
90   }
91
92   if (s->path) AUBIO_FREE(s->path);
93   s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX));
94   strncpy(s->path, path, strnlen(path, PATH_MAX));
95
96   s->max_size = MAX_SIZE;
97   s->bitspersample = 16;
98   s->total_frames_written = 0;
99
100   s->samplerate = 0;
101   s->channels = 0;
102
103   // negative samplerate given, abort
104   if ((sint_t)samplerate < 0) goto beach;
105   // zero samplerate given. do not open yet
106   if ((sint_t)samplerate == 0) return s;
107   // samplerate way too large, fail
108   if ((sint_t)samplerate > 192000 * 4) goto beach;
109
110   s->samplerate = samplerate;
111   s->channels = 1;
112
113   if (aubio_sink_wavwrite_open(s) != AUBIO_OK) {
114     // open failed, abort
115     goto beach;
116   }
117
118   return s;
119 beach:
120   //AUBIO_ERR("sink_wavwrite: failed creating %s with samplerate %dHz\n",
121   //    s->path, s->samplerate);
122   del_aubio_sink_wavwrite(s);
123   return NULL;
124 }
125
126 uint_t aubio_sink_wavwrite_preset_samplerate(aubio_sink_wavwrite_t *s, uint_t samplerate)
127 {
128   if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
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);
133   }
134   return AUBIO_OK;
135 }
136
137 uint_t aubio_sink_wavwrite_preset_channels(aubio_sink_wavwrite_t *s, uint_t channels)
138 {
139   if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
140   s->channels = channels;
141   // automatically open when both samplerate and channels have been set
142   if (s->samplerate != 0 && s->channels != 0) {
143     return aubio_sink_wavwrite_open(s);
144   }
145   return AUBIO_OK;
146 }
147
148 uint_t aubio_sink_wavwrite_get_samplerate(const aubio_sink_wavwrite_t *s)
149 {
150   return s->samplerate;
151 }
152
153 uint_t aubio_sink_wavwrite_get_channels(const aubio_sink_wavwrite_t *s)
154 {
155   return s->channels;
156 }
157
158 uint_t aubio_sink_wavwrite_open(aubio_sink_wavwrite_t *s) {
159   unsigned char buf[5];
160   uint_t byterate, blockalign;
161
162   /* open output file */
163   s->fid = fopen((const char *)s->path, "wb");
164   if (!s->fid) {
165     AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno));
166     goto beach;
167   }
168
169   // ChunkID
170   fwrite("RIFF", 4, 1, s->fid);
171
172   // ChunkSize (0 for now, actual size will be written in _close)
173   fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
174
175   // Format
176   fwrite("WAVE", 4, 1, s->fid);
177
178   // Subchunk1ID
179   fwrite("fmt ", 4, 1, s->fid);
180
181   // Subchunk1Size
182   fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
183
184   // AudioFormat
185   fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
186
187   // NumChannels
188   fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
189
190   // SampleRate
191   fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
192
193   // ByteRate
194   byterate = s->samplerate * s->channels * s->bitspersample / 8;
195   fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
196
197   // BlockAlign
198   blockalign = s->channels * s->bitspersample / 8;
199   fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
200
201   // BitsPerSample
202   fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
203
204   // Subchunk2ID
205   fwrite("data", 4, 1, s->fid);
206
207   // Subchunk1Size (0 for now, actual size will be written in _close)
208   fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
209
210   s->scratch_size = s->max_size * s->channels;
211   /* allocate data for de/interleaving reallocated when needed. */
212   if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
213     AUBIO_ERR("sink_wavwrite: %d x %d exceeds SIZE maximum buffer size %d\n",
214         s->max_size, s->channels, MAX_SIZE * MAX_CHANNELS);
215     goto beach;
216   }
217   s->scratch_data = AUBIO_ARRAY(unsigned short,s->scratch_size);
218
219   return AUBIO_OK;
220
221 beach:
222   return AUBIO_FAIL;
223 }
224
225
226 void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){
227   uint_t i = 0, written_frames = 0;
228
229   if (write > s->max_size) {
230     AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
231         "but only %d can be written at a time\n", write, s->path, s->max_size);
232     write = s->max_size;
233   }
234
235   for (i = 0; i < write; i++) {
236     s->scratch_data[i] = HTOLES(FLOAT_TO_SHORT(write_data->data[i]));
237   }
238   written_frames = fwrite(s->scratch_data, 2, write, s->fid);
239
240   if (written_frames != write) {
241     AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
242         "but only %d could be written\n", write, s->path, written_frames);
243   }
244   s->total_frames_written += written_frames;
245   return;
246 }
247
248 void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){
249   uint_t c = 0, i = 0, written_frames = 0;
250
251   if (write > s->max_size) {
252     AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
253         "but only %d can be written at a time\n", write, s->path, s->max_size);
254     write = s->max_size;
255   }
256
257   for (c = 0; c < s->channels; c++) {
258     for (i = 0; i < write; i++) {
259       s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[c][i]));
260     }
261   }
262   written_frames = fwrite(s->scratch_data, 2, write * s->channels, s->fid);
263
264   if (written_frames != write * s->channels) {
265     AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
266         "but only %d could be written\n", write, s->path, written_frames / s->channels);
267   }
268   s->total_frames_written += written_frames;
269   return;
270 }
271
272 uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) {
273   uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
274   unsigned char buf[5];
275   if (!s->fid) return AUBIO_FAIL;
276   // ChunkSize
277   fseek(s->fid, 4, SEEK_SET);
278   fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
279   // Subchunk2Size
280   fseek(s->fid, 40, SEEK_SET);
281   fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
282   // close file
283   if (fclose(s->fid)) {
284     AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno));
285   }
286   s->fid = NULL;
287   return AUBIO_OK;
288 }
289
290 void del_aubio_sink_wavwrite(aubio_sink_wavwrite_t * s){
291   if (!s) return;
292   aubio_sink_wavwrite_close(s);
293   if (s->path) AUBIO_FREE(s->path);
294   AUBIO_FREE(s->scratch_data);
295   AUBIO_FREE(s);
296 }
297
298 #endif /* HAVE_WAVWRITE */