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