Ignore:
Timestamp:
Dec 20, 2018, 8:31:00 PM (6 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/crepe, fix/ffmpeg5, master
Children:
65e1ec6
Parents:
65628c4 (diff), e2f1e6d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'feature/sink_flac' into feature/autosink

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/sink_wavwrite.c

    r65628c4 rf56f795  
    163163  unsigned char buf[5];
    164164  uint_t byterate, blockalign;
     165  size_t written = 0;
    165166
    166167  /* open output file */
    167168  s->fid = fopen((const char *)s->path, "wb");
    168169  if (!s->fid) {
    169     AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno));
     170    char errorstr[256];
     171    AUBIO_STRERROR(errno, errorstr, sizeof(errorstr));
     172    AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, errorstr);
    170173    goto beach;
    171174  }
    172175
    173176  // ChunkID
    174   fwrite("RIFF", 4, 1, s->fid);
     177  written += fwrite("RIFF", 4, 1, s->fid);
    175178
    176179  // ChunkSize (0 for now, actual size will be written in _close)
    177   fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     180  written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
    178181
    179182  // Format
    180   fwrite("WAVE", 4, 1, s->fid);
     183  written += fwrite("WAVE", 4, 1, s->fid);
    181184
    182185  // Subchunk1ID
    183   fwrite("fmt ", 4, 1, s->fid);
     186  written += fwrite("fmt ", 4, 1, s->fid);
    184187
    185188  // Subchunk1Size
    186   fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
     189  written += fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
    187190
    188191  // AudioFormat
    189   fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
     192  written += fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
    190193
    191194  // NumChannels
    192   fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
     195  written += fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
    193196
    194197  // SampleRate
    195   fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
     198  written += fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
    196199
    197200  // ByteRate
    198201  byterate = s->samplerate * s->channels * s->bitspersample / 8;
    199   fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
     202  written += fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
    200203
    201204  // BlockAlign
    202205  blockalign = s->channels * s->bitspersample / 8;
    203   fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
     206  written += fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
    204207
    205208  // BitsPerSample
    206   fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
     209  written += fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
    207210
    208211  // Subchunk2ID
    209   fwrite("data", 4, 1, s->fid);
     212  written += fwrite("data", 4, 1, s->fid);
    210213
    211214  // Subchunk1Size (0 for now, actual size will be written in _close)
    212   fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     215  written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     216
     217  // fwrite(*, *, 1, s->fid) was called 13 times, check success
     218  if (written != 13) {
     219    char errorstr[256];
     220    AUBIO_STRERROR(errno, errorstr, sizeof(errorstr));
     221    AUBIO_WRN("sink_wavwrite: writing header to %s failed, expected %d"
     222        " write but got only %d (%s)\n", s->path, 13, written, errorstr);
     223    return AUBIO_FAIL;
     224  }
    213225
    214226  s->scratch_size = s->max_size * s->channels;
     
    227239}
    228240
     241static
     242void aubio_sink_wavwrite_write_frames(aubio_sink_wavwrite_t *s, uint_t write)
     243{
     244  uint_t written_frames = 0;
     245
     246  written_frames = fwrite(s->scratch_data, 2 * s->channels, write, s->fid);
     247
     248  if (written_frames != write) {
     249    char errorstr[256];
     250    AUBIO_STRERROR(errno, errorstr, sizeof(errorstr));
     251    AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, but only %d"
     252        " could be written (%s)\n", write, s->path, written_frames, errorstr);
     253  }
     254  s->total_frames_written += written_frames;
     255}
    229256
    230257void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){
    231   uint_t c = 0, i = 0, written_frames = 0;
     258  uint_t c = 0, i = 0;
    232259  uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
    233260      s->max_size, write_data->length, write);
     
    238265    }
    239266  }
    240   written_frames = fwrite(s->scratch_data, 2, length * s->channels, s->fid);
    241 
    242   if (written_frames != write) {
    243     AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
    244         "but only %d could be written\n", write, s->path, written_frames);
    245   }
    246   s->total_frames_written += written_frames;
    247   return;
     267
     268  aubio_sink_wavwrite_write_frames(s, length);
    248269}
    249270
    250271void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){
    251   uint_t c = 0, i = 0, written_frames = 0;
     272  uint_t c = 0, i = 0;
    252273
    253274  uint_t channels = aubio_sink_validate_input_channels("sink_wavwrite", s->path,
     
    261282    }
    262283  }
    263   written_frames = fwrite(s->scratch_data, 2, length * s->channels, s->fid);
    264 
    265   if (written_frames != write * s->channels) {
    266     AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
    267         "but only %d could be written\n", write, s->path, written_frames / s->channels);
    268   }
    269   s->total_frames_written += written_frames;
    270   return;
     284
     285  aubio_sink_wavwrite_write_frames(s, length);
    271286}
    272287
     
    274289  uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
    275290  unsigned char buf[5];
     291  size_t written = 0, err = 0;
    276292  if (!s->fid) return AUBIO_FAIL;
    277293  // ChunkSize
    278   fseek(s->fid, 4, SEEK_SET);
    279   fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
     294  err += fseek(s->fid, 4, SEEK_SET);
     295  written += fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
    280296  // Subchunk2Size
    281   fseek(s->fid, 40, SEEK_SET);
    282   fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
     297  err += fseek(s->fid, 40, SEEK_SET);
     298  written += fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
     299  if (written != 2 || err != 0) {
     300    char errorstr[256];
     301    AUBIO_STRERROR(errno, errorstr, sizeof(errorstr));
     302    AUBIO_WRN("sink_wavwrite: updating header of %s failed, expected %d"
     303        " write but got only %d (%s)\n", s->path, 2, written, errorstr);
     304  }
    283305  // close file
    284306  if (fclose(s->fid)) {
    285     AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno));
     307    char errorstr[256];
     308    AUBIO_STRERROR(errno, errorstr, sizeof(errorstr));
     309    AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, errorstr);
    286310  }
    287311  s->fid = NULL;
Note: See TracChangeset for help on using the changeset viewer.