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