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