From b4e143863574e3275dc895f5d193ee88ea1f5935 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Mon, 25 Apr 2016 16:45:22 +0200 Subject: [PATCH] src/io/source_wavread.h: add _get_duration --- src/io/source_wavread.c | 15 ++++++++++++++- src/io/source_wavread.h | 10 ++++++++++ tests/src/io/test-source_wavread.c | 8 ++++++-- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/io/source_wavread.c b/src/io/source_wavread.c index d958fbee..d1b1f3af 100644 --- a/src/io/source_wavread.c +++ b/src/io/source_wavread.c @@ -52,6 +52,8 @@ struct _aubio_source_wavread_t { uint_t read_index; uint_t eof; + uint_t duration; + size_t seek_start; unsigned char *short_output; @@ -71,7 +73,7 @@ aubio_source_wavread_t * new_aubio_source_wavread(const char_t * path, uint_t sa aubio_source_wavread_t * s = AUBIO_NEW(aubio_source_wavread_t); size_t bytes_read = 0, bytes_expected = 44; unsigned char buf[5]; - unsigned int format, channels, sr, byterate, blockalign, bitspersample;//, data_size; + unsigned int format, channels, sr, byterate, blockalign, duration, bitspersample;//, data_size; if (path == NULL) { AUBIO_ERR("source_wavread: Aborted opening null path\n"); @@ -215,6 +217,8 @@ aubio_source_wavread_t * new_aubio_source_wavread(const char_t * path, uint_t sa // Subchunk2Size bytes_read += fread(buf, 1, 4, s->fid); + duration = read_little_endian(buf, 4) / blockalign; + //data_size = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24); //AUBIO_MSG("found %d frames in %s\n", 8 * data_size / bitspersample / channels, s->path); @@ -235,6 +239,8 @@ aubio_source_wavread_t * new_aubio_source_wavread(const char_t * path, uint_t sa s->blockalign= blockalign; s->bitspersample = bitspersample; + s->duration = duration; + s->short_output = (unsigned char *)calloc(s->blockalign, AUBIO_WAVREAD_BUFSIZE); s->read_index = 0; s->read_samples = 0; @@ -374,6 +380,13 @@ uint_t aubio_source_wavread_seek (aubio_source_wavread_t * s, uint_t pos) { return AUBIO_OK; } +uint_t aubio_source_wavread_get_duration (const aubio_source_wavread_t * s) { + if (s && s->duration) { + return s->duration; + } + return 0; +} + uint_t aubio_source_wavread_close (aubio_source_wavread_t * s) { if (!s->fid) { return AUBIO_FAIL; diff --git a/src/io/source_wavread.h b/src/io/source_wavread.h index a0ec1414..65f752f9 100644 --- a/src/io/source_wavread.h +++ b/src/io/source_wavread.h @@ -125,6 +125,16 @@ uint_t aubio_source_wavread_seek (aubio_source_wavread_t *s, uint_t pos); /** + get the duration of source object, in frames + + \param s source object, created with ::new_aubio_source_sndfile + \return number of frames in file + +*/ +uint_t aubio_source_wavread_get_duration (const aubio_source_wavread_t *s); + +/** + close source \param s source object, created with ::new_aubio_source_wavread diff --git a/tests/src/io/test-source_wavread.c b/tests/src/io/test-source_wavread.c index 50da5894..23511cac 100644 --- a/tests/src/io/test-source_wavread.c +++ b/tests/src/io/test-source_wavread.c @@ -35,9 +35,12 @@ int main (int argc, char **argv) aubio_source_wavread_t * s = new_aubio_source_wavread(source_path, samplerate, hop_size); + if (!s) { err = 1; goto beach; } fvec_t *vec = new_fvec(hop_size); + uint_t n_frames_expected = aubio_source_wavread_get_duration(s); + samplerate = aubio_source_wavread_get_samplerate(s); do { @@ -46,8 +49,9 @@ int main (int argc, char **argv) n_frames += read; } while ( read == hop_size ); - PRINT_MSG("read %d frames at %dHz (%d blocks) from %s\n", n_frames, samplerate, - n_frames / hop_size, source_path); + PRINT_MSG("read %d frames (expected %d) at %dHz (%d blocks) from %s\n", + n_frames, n_frames_expected, samplerate, n_frames / hop_size, + source_path); del_fvec (vec); del_aubio_source_wavread (s); -- 2.11.0