Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/sink_wavwrite.c

    r3e1c482 r65a4fb4  
    2828#include "io/sink_wavwrite.h"
    2929#include "io/ioutils.h"
    30 
    31 #include <errno.h>
    3230
    3331#define MAX_SIZE 4096
     
    163161  unsigned char buf[5];
    164162  uint_t byterate, blockalign;
     163  size_t written = 0;
    165164
    166165  /* open output file */
    167166  s->fid = fopen((const char *)s->path, "wb");
    168167  if (!s->fid) {
    169     AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno));
     168    AUBIO_STRERR("sink_wavwrite: could not open %s (%s)\n", s->path, errorstr);
    170169    goto beach;
    171170  }
    172171
    173172  // ChunkID
    174   fwrite("RIFF", 4, 1, s->fid);
     173  written += fwrite("RIFF", 4, 1, s->fid);
    175174
    176175  // ChunkSize (0 for now, actual size will be written in _close)
    177   fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     176  written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
    178177
    179178  // Format
    180   fwrite("WAVE", 4, 1, s->fid);
     179  written += fwrite("WAVE", 4, 1, s->fid);
    181180
    182181  // Subchunk1ID
    183   fwrite("fmt ", 4, 1, s->fid);
     182  written += fwrite("fmt ", 4, 1, s->fid);
    184183
    185184  // Subchunk1Size
    186   fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
     185  written += fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
    187186
    188187  // AudioFormat
    189   fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
     188  written += fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
    190189
    191190  // NumChannels
    192   fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
     191  written += fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
    193192
    194193  // SampleRate
    195   fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
     194  written += fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
    196195
    197196  // ByteRate
    198197  byterate = s->samplerate * s->channels * s->bitspersample / 8;
    199   fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
     198  written += fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
    200199
    201200  // BlockAlign
    202201  blockalign = s->channels * s->bitspersample / 8;
    203   fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
     202  written += fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
    204203
    205204  // BitsPerSample
    206   fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
     205  written += fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
    207206
    208207  // Subchunk2ID
    209   fwrite("data", 4, 1, s->fid);
     208  written += fwrite("data", 4, 1, s->fid);
    210209
    211210  // Subchunk1Size (0 for now, actual size will be written in _close)
    212   fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     211  written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     212
     213  // fwrite(*, *, 1, s->fid) was called 13 times, check success
     214  if (written != 13 || fflush(s->fid)) {
     215    AUBIO_STRERR("sink_wavwrite: writing header to %s failed"
     216        " (wrote %d/%d, %s)\n", s->path, written, 13, errorstr);
     217    fclose(s->fid);
     218    s->fid = NULL;
     219    return AUBIO_FAIL;
     220  }
    213221
    214222  s->scratch_size = s->max_size * s->channels;
     
    227235}
    228236
     237static
     238void aubio_sink_wavwrite_write_frames(aubio_sink_wavwrite_t *s, uint_t write)
     239{
     240  uint_t written_frames = 0;
     241
     242  written_frames = fwrite(s->scratch_data, 2 * s->channels, write, s->fid);
     243
     244  if (written_frames != write) {
     245    AUBIO_STRERR("sink_wavwrite: trying to write %d frames to %s, but only %d"
     246        " could be written (%s)\n", write, s->path, written_frames, errorstr);
     247  }
     248  s->total_frames_written += written_frames;
     249}
    229250
    230251void 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;
     252  uint_t c = 0, i = 0;
    232253  uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
    233254      s->max_size, write_data->length, write);
     
    238259    }
    239260  }
    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;
     261
     262  aubio_sink_wavwrite_write_frames(s, length);
    248263}
    249264
    250265void 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;
     266  uint_t c = 0, i = 0;
    252267
    253268  uint_t channels = aubio_sink_validate_input_channels("sink_wavwrite", s->path,
     
    261276    }
    262277  }
    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;
     278
     279  aubio_sink_wavwrite_write_frames(s, length);
    271280}
    272281
     
    274283  uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
    275284  unsigned char buf[5];
     285  size_t written = 0, err = 0;
    276286  if (!s->fid) return AUBIO_FAIL;
    277287  // ChunkSize
    278   fseek(s->fid, 4, SEEK_SET);
    279   fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
     288  err += fseek(s->fid, 4, SEEK_SET);
     289  written += fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
    280290  // Subchunk2Size
    281   fseek(s->fid, 40, SEEK_SET);
    282   fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
     291  err += fseek(s->fid, 40, SEEK_SET);
     292  written += fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
     293  if (written != 2 || err != 0) {
     294    AUBIO_STRERR("sink_wavwrite: updating header of %s failed, expected %d"
     295        " write but got only %d (%s)\n", s->path, 2, written, errorstr);
     296  }
    283297  // close file
    284298  if (fclose(s->fid)) {
    285     AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno));
     299    AUBIO_STRERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, errorstr);
    286300  }
    287301  s->fid = NULL;
Note: See TracChangeset for help on using the changeset viewer.