Changeset 87636d0


Ignore:
Timestamp:
Mar 12, 2014, 4:32:32 AM (11 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
46148d3
Parents:
46b00690
Message:

src/io/source_wavread.c: use the return value of fread to detect short read and compute seek_start

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/source_wavread.c

    r46b00690 r87636d0  
    5353  uint_t eof;
    5454
     55  size_t seek_start;
     56
    5557  unsigned char *short_output;
    5658  fmat_t *output;
     
    6870aubio_source_wavread_t * new_aubio_source_wavread(char_t * path, uint_t samplerate, uint_t hop_size) {
    6971  aubio_source_wavread_t * s = AUBIO_NEW(aubio_source_wavread_t);
     72  size_t bytes_read = 0, bytes_expected = 44;
    7073  unsigned char buf[5];
    7174  unsigned int format, channels, sr, byterate, blockalign, bitspersample;//, data_size;
     
    9598
    9699  // ChunkID
    97   fread(buf, 4, 1, s->fid);
     100  bytes_read += fread(buf, 1, 4, s->fid);
    98101  buf[4] = '\0';
    99102  if ( strcmp((const char *)buf, "RIFF") != 0 ) {
     
    103106
    104107  // ChunkSize
    105   fread(buf, 4, 1, s->fid);
     108  bytes_read += fread(buf, 1, 4, s->fid);
    106109
    107110  // Format
    108   fread(buf, 4, 1, s->fid);
     111  bytes_read += fread(buf, 1, 4, s->fid);
    109112  buf[4] = '\0';
    110113  if ( strcmp((const char *)buf, "WAVE") != 0 ) {
     
    114117
    115118  // Subchunk1ID
    116   fread(buf, 4, 1, s->fid);
     119  bytes_read += fread(buf, 1, 4, s->fid);
    117120  buf[4] = '\0';
    118121  if ( strcmp((const char *)buf, "fmt ") != 0 ) {
     
    122125
    123126  // Subchunk1Size
    124   fread(buf, 4, 1, s->fid);
     127  bytes_read += fread(buf, 1, 4, s->fid);
    125128  format = read_little_endian(buf, 4);
    126129  if ( format != 16 ) {
     
    135138
    136139  // AudioFormat
    137   fread(buf, 2, 1, s->fid);
     140  bytes_read += fread(buf, 1, 2, s->fid);
    138141  if ( buf[0] != 1 || buf[1] != 0) {
    139142    AUBIO_ERR("source_wavread: AudioFormat should be PCM, in %s\n", s->path);
     
    142145
    143146  // NumChannels
    144   fread(buf, 2, 1, s->fid);
     147  bytes_read += fread(buf, 1, 2, s->fid);
    145148  channels = read_little_endian(buf, 2);
    146149
    147150  // SampleRate
    148   fread(buf, 4, 1, s->fid);
     151  bytes_read += fread(buf, 1, 4, s->fid);
    149152  sr = read_little_endian(buf, 4);
    150153
    151154  // ByteRate
    152   fread(buf, 4, 1, s->fid);
     155  bytes_read += fread(buf, 1, 4, s->fid);
    153156  byterate = read_little_endian(buf, 4);
    154157
    155158  // BlockAlign
    156   fread(buf, 2, 1, s->fid);
     159  bytes_read += fread(buf, 1, 2, s->fid);
    157160  blockalign = read_little_endian(buf, 2);
    158161
    159162  // BitsPerSample
    160   fread(buf, 2, 1, s->fid);
     163  bytes_read += fread(buf, 1, 2, s->fid);
    161164  bitspersample = read_little_endian(buf, 2);
    162165#if 0
     
    201204
    202205  // Subchunk2ID
    203   fread(buf, 4, 1, s->fid);
     206  bytes_read += fread(buf, 1, 4, s->fid);
    204207  buf[4] = '\0';
    205208  if ( strcmp((const char *)buf, "data") != 0 ) {
     
    209212
    210213  // Subchunk2Size
    211   fread(buf, 4, 1, s->fid);
     214  bytes_read += fread(buf, 1, 4, s->fid);
    212215  //data_size = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
    213216  //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    AUBIO_ERR("source_wavread: short read (%zd instead of %zd) in %s\n",
     221        bytes_read, bytes_expected, s->path);
     222    goto beach;
     223  }
     224  s->seek_start = bytes_read;
    214225
    215226  s->output = new_fmat(s->input_channels, AUBIO_WAVREAD_BUFSIZE);
     
    341352
    342353uint_t aubio_source_wavread_seek (aubio_source_wavread_t * s, uint_t pos) {
    343   uint_t ret = fseek(s->fid, 44 + pos * s->blockalign, SEEK_SET);
     354  uint_t ret = fseek(s->fid, s->seek_start + pos * s->blockalign, SEEK_SET);
    344355  s->eof = 0;
    345356  s->read_index = 0;
Note: See TracChangeset for help on using the changeset viewer.