src/io/*.{c,h}: added const qualifiers to unmodified pointers
[aubio.git] / src / io / source_wavread.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 #include "config.h"
22
23 #ifdef HAVE_WAVREAD
24
25 #include "aubio_priv.h"
26 #include "fvec.h"
27 #include "fmat.h"
28 #include "source_wavread.h"
29
30 #include <errno.h>
31
32 #define AUBIO_WAVREAD_BUFSIZE 1024
33
34 #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
35
36 struct _aubio_source_wavread_t {
37   uint_t hop_size;
38   uint_t samplerate;
39   uint_t channels;
40
41   // some data about the file
42   char_t *path;
43   uint_t input_samplerate;
44   uint_t input_channels;
45
46   // internal stuff
47   FILE *fid;
48
49   uint_t read_samples;
50   uint_t blockalign;
51   uint_t bitspersample;
52   uint_t read_index;
53   uint_t eof;
54
55   size_t seek_start;
56
57   unsigned char *short_output;
58   fmat_t *output;
59 };
60
61 unsigned int read_little_endian (unsigned char *buf, unsigned int length);
62 unsigned int read_little_endian (unsigned char *buf, unsigned int length) {
63   uint_t i, ret = 0;
64   for (i = 0; i < length; i++) {
65     ret += buf[i] << (i * 8);
66   }
67   return ret;
68 }
69
70 aubio_source_wavread_t * new_aubio_source_wavread(const char_t * path, uint_t samplerate, uint_t hop_size) {
71   aubio_source_wavread_t * s = AUBIO_NEW(aubio_source_wavread_t);
72   size_t bytes_read = 0, bytes_expected = 44;
73   unsigned char buf[5];
74   unsigned int format, channels, sr, byterate, blockalign, bitspersample;//, data_size;
75
76   if (path == NULL) {
77     AUBIO_ERR("source_wavread: Aborted opening null path\n");
78     goto beach;
79   }
80   if ((sint_t)samplerate < 0) {
81     AUBIO_ERR("source_wavread: Can not open %s with samplerate %d\n", path, samplerate);
82     goto beach;
83   }
84   if ((sint_t)hop_size <= 0) {
85     AUBIO_ERR("source_wavread: Can not open %s with hop_size %d\n", path, hop_size);
86     goto beach;
87   }
88
89   s->path = path;
90   s->samplerate = samplerate;
91   s->hop_size = hop_size;
92
93   s->fid = fopen((const char *)path, "rb");
94   if (!s->fid) {
95     AUBIO_ERR("source_wavread: Failed opening %s (System error: %s)\n", s->path, strerror(errno));
96     goto beach;
97   }
98
99   // ChunkID
100   bytes_read += fread(buf, 1, 4, s->fid);
101   buf[4] = '\0';
102   if ( strcmp((const char *)buf, "RIFF") != 0 ) {
103     AUBIO_ERR("source_wavread: could not find RIFF header in %s\n", s->path);
104     goto beach;
105   }
106
107   // ChunkSize
108   bytes_read += fread(buf, 1, 4, s->fid);
109
110   // Format
111   bytes_read += fread(buf, 1, 4, s->fid);
112   buf[4] = '\0';
113   if ( strcmp((const char *)buf, "WAVE") != 0 ) {
114     AUBIO_ERR("source_wavread: wrong format in RIFF header in %s\n", s->path);
115     goto beach;
116   }
117
118   // Subchunk1ID
119   bytes_read += fread(buf, 1, 4, s->fid);
120   buf[4] = '\0';
121   if ( strcmp((const char *)buf, "fmt ") != 0 ) {
122     AUBIO_ERR("source_wavread: fmt RIFF header in %s\n", s->path);
123     goto beach;
124   }
125
126   // Subchunk1Size
127   bytes_read += fread(buf, 1, 4, s->fid);
128   format = read_little_endian(buf, 4);
129   if ( format != 16 ) {
130     // TODO accept format 18
131     AUBIO_ERR("source_wavread: file %s is not encoded with PCM\n", s->path);
132     goto beach;
133   }
134   if ( buf[1] || buf[2] | buf[3] ) {
135     AUBIO_ERR("source_wavread: Subchunk1Size should be 0, in %s\n", s->path);
136     goto beach;
137   }
138
139   // AudioFormat
140   bytes_read += fread(buf, 1, 2, s->fid);
141   if ( buf[0] != 1 || buf[1] != 0) {
142     AUBIO_ERR("source_wavread: AudioFormat should be PCM, in %s\n", s->path);
143     goto beach;
144   }
145
146   // NumChannels
147   bytes_read += fread(buf, 1, 2, s->fid);
148   channels = read_little_endian(buf, 2);
149
150   // SampleRate
151   bytes_read += fread(buf, 1, 4, s->fid);
152   sr = read_little_endian(buf, 4);
153
154   // ByteRate
155   bytes_read += fread(buf, 1, 4, s->fid);
156   byterate = read_little_endian(buf, 4);
157
158   // BlockAlign
159   bytes_read += fread(buf, 1, 2, s->fid);
160   blockalign = read_little_endian(buf, 2);
161
162   // BitsPerSample
163   bytes_read += fread(buf, 1, 2, s->fid);
164   bitspersample = read_little_endian(buf, 2);
165 #if 0
166   if ( bitspersample != 16 ) {
167     AUBIO_ERR("source_wavread: can not process %dbit file %s\n",
168         bitspersample, s->path);
169     goto beach;
170   }
171 #endif
172
173   if ( byterate * 8 != sr * channels * bitspersample ) {
174     AUBIO_ERR("source_wavread: wrong byterate in %s\n", s->path);
175     goto beach;
176   }
177
178   if ( blockalign * 8 != channels * bitspersample ) {
179     AUBIO_ERR("source_wavread: wrong blockalign in %s\n", s->path);
180     goto beach;
181   }
182
183   s->input_samplerate = sr;
184   s->input_channels = channels;
185
186 #if 0
187   AUBIO_DBG("channels %d\n", channels);
188   AUBIO_DBG("sr %d\n", sr);
189   AUBIO_DBG("byterate %d\n", byterate);
190   AUBIO_DBG("blockalign %d\n", blockalign);
191   AUBIO_DBG("bitspersample %d\n", bitspersample);
192
193   AUBIO_DBG("found %d channels in %s\n", s->input_channels, s->path);
194   AUBIO_DBG("found %d samplerate in %s\n", s->input_samplerate, s->path);
195 #endif
196
197   if (samplerate == 0) {
198     s->samplerate = s->input_samplerate;
199   } else if (samplerate != s->input_samplerate) {
200     AUBIO_ERR("source_wavread: can not resample %s from %d to %dHz\n",
201         s->path, s->input_samplerate, samplerate);
202     goto beach;
203   }
204
205   // Subchunk2ID
206   bytes_read += fread(buf, 1, 4, s->fid);
207   buf[4] = '\0';
208   if ( strcmp((const char *)buf, "data") != 0 ) {
209     AUBIO_ERR("source_wavread: data RIFF header not found in %s\n", s->path);
210     goto beach;
211   }
212
213   // Subchunk2Size
214   bytes_read += fread(buf, 1, 4, s->fid);
215   //data_size = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
216   //AUBIO_MSG("found %d frames in %s\n", 8 * data_size / bitspersample / channels, s->path);
217
218   // check the total number of bytes read is correct
219   if ( bytes_read != bytes_expected ) {
220 #ifndef HAVE_WIN_HACKS
221     AUBIO_ERR("source_wavread: short read (%zd instead of %zd) in %s\n",
222         bytes_read, bytes_expected, s->path);
223 #else // mingw does not know about %zd...
224     AUBIO_ERR("source_wavread: short read (%d instead of %d) in %s\n",
225         (int)bytes_read, (int)bytes_expected, s->path);
226 #endif
227     goto beach;
228   }
229   s->seek_start = bytes_read;
230
231   s->output = new_fmat(s->input_channels, AUBIO_WAVREAD_BUFSIZE);
232   s->blockalign= blockalign;
233   s->bitspersample = bitspersample;
234
235   s->short_output = (unsigned char *)calloc(s->blockalign, AUBIO_WAVREAD_BUFSIZE);
236   s->read_index = 0;
237   s->read_samples = 0;
238   s->eof = 0;
239
240   return s;
241
242 beach:
243   //AUBIO_ERR("source_wavread: can not read %s at samplerate %dHz with a hop_size of %d\n",
244   //    s->path, s->samplerate, s->hop_size);
245   del_aubio_source_wavread(s);
246   return NULL;
247 }
248
249 void aubio_source_wavread_readframe(aubio_source_wavread_t *s, uint_t *wavread_read);
250
251 void aubio_source_wavread_readframe(aubio_source_wavread_t *s, uint_t *wavread_read) {
252   unsigned char *short_ptr = s->short_output;
253   size_t read = fread(short_ptr, s->blockalign, AUBIO_WAVREAD_BUFSIZE, s->fid);
254   uint_t i, j, b, bitspersample = s->bitspersample;
255   uint_t wrap_at = (1 << ( bitspersample - 1 ) );
256   uint_t wrap_with = (1 << bitspersample);
257   smpl_t scaler = 1. / wrap_at;
258   int signed_val = 0;
259   unsigned int unsigned_val = 0;
260
261   for (j = 0; j < read; j++) {
262     for (i = 0; i < s->input_channels; i++) {
263       unsigned_val = 0;
264       for (b = 0; b < bitspersample; b+=8 ) {
265         unsigned_val += *(short_ptr) << b;
266         short_ptr++;
267       }
268       signed_val = unsigned_val;
269       // FIXME why does 8 bit conversion maps [0;255] to [-128;127]
270       // instead of [0;127] to [0;127] and [128;255] to [-128;-1]
271       if (bitspersample == 8) signed_val -= wrap_at;
272       else if (unsigned_val >= wrap_at) signed_val = unsigned_val - wrap_with;
273       s->output->data[i][j] = signed_val * scaler;
274     }
275   }
276
277   *wavread_read = read;
278
279   if (read == 0) s->eof = 1;
280 }
281
282 void aubio_source_wavread_do(aubio_source_wavread_t * s, fvec_t * read_data, uint_t * read){
283   uint_t i, j;
284   uint_t end = 0;
285   uint_t total_wrote = 0;
286   while (total_wrote < s->hop_size) {
287     end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
288     for (i = 0; i < end; i++) {
289       read_data->data[i + total_wrote] = 0;
290       for (j = 0; j < s->input_channels; j++ ) {
291         read_data->data[i + total_wrote] += s->output->data[j][i + s->read_index];
292       }
293       read_data->data[i + total_wrote] /= (smpl_t)(s->input_channels);
294     }
295     total_wrote += end;
296     if (total_wrote < s->hop_size) {
297       uint_t wavread_read = 0;
298       aubio_source_wavread_readframe(s, &wavread_read);
299       s->read_samples = wavread_read;
300       s->read_index = 0;
301       if (s->eof) {
302         break;
303       }
304     } else {
305       s->read_index += end;
306     }
307   }
308   if (total_wrote < s->hop_size) {
309     for (i = end; i < s->hop_size; i++) {
310       read_data->data[i] = 0.;
311     }
312   }
313   *read = total_wrote;
314 }
315
316 void aubio_source_wavread_do_multi(aubio_source_wavread_t * s, fmat_t * read_data, uint_t * read){
317   uint_t i,j;
318   uint_t end = 0;
319   uint_t total_wrote = 0;
320   while (total_wrote < s->hop_size) {
321     end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
322     for (j = 0; j < read_data->height; j++) {
323       for (i = 0; i < end; i++) {
324         read_data->data[j][i + total_wrote] = s->output->data[j][i];
325       }
326     }
327     total_wrote += end;
328     if (total_wrote < s->hop_size) {
329       uint_t wavread_read = 0;
330       aubio_source_wavread_readframe(s, &wavread_read);
331       s->read_samples = wavread_read;
332       s->read_index = 0;
333       if (s->eof) {
334         break;
335       }
336     } else {
337       s->read_index += end;
338     }
339   }
340   if (total_wrote < s->hop_size) {
341     for (j = 0; j < read_data->height; j++) {
342       for (i = end; i < s->hop_size; i++) {
343         read_data->data[j][i] = 0.;
344       }
345     }
346   }
347   *read = total_wrote;
348 }
349
350 uint_t aubio_source_wavread_get_samplerate(aubio_source_wavread_t * s) {
351   return s->samplerate;
352 }
353
354 uint_t aubio_source_wavread_get_channels(aubio_source_wavread_t * s) {
355   return s->input_channels;
356 }
357
358 uint_t aubio_source_wavread_seek (aubio_source_wavread_t * s, uint_t pos) {
359   uint_t ret = 0;
360   if ((sint_t)pos < 0) {
361     return AUBIO_FAIL;
362   }
363   ret = fseek(s->fid, s->seek_start + pos * s->blockalign, SEEK_SET);
364   if (ret != 0) {
365     AUBIO_ERR("source_wavread: could not seek %s at %d (%s)\n", s->path, pos, strerror(errno));
366     return AUBIO_FAIL;
367   }
368   // reset some values
369   s->eof = 0;
370   s->read_index = 0;
371   return AUBIO_OK;
372 }
373
374 uint_t aubio_source_wavread_close (aubio_source_wavread_t * s) {
375   if (!s->fid) {
376     return AUBIO_FAIL;
377   }
378   if (fclose(s->fid)) {
379     AUBIO_ERR("source_wavread: could not close %s (%s)\n", s->path, strerror(errno));
380     return AUBIO_FAIL;
381   }
382   s->fid = NULL;
383   return AUBIO_OK;
384 }
385
386 void del_aubio_source_wavread(aubio_source_wavread_t * s) {
387   if (!s) return;
388   aubio_source_wavread_close(s);
389   if (s->short_output) AUBIO_FREE(s->short_output);
390   if (s->output) del_fmat(s->output);
391   AUBIO_FREE(s);
392 }
393
394 #endif /* HAVE_WAVREAD */