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