Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/sink_wavwrite.c

    r65a4fb4 r3e1c482  
    2828#include "io/sink_wavwrite.h"
    2929#include "io/ioutils.h"
     30
     31#include <errno.h>
    3032
    3133#define MAX_SIZE 4096
     
    161163  unsigned char buf[5];
    162164  uint_t byterate, blockalign;
    163   size_t written = 0;
    164165
    165166  /* open output file */
    166167  s->fid = fopen((const char *)s->path, "wb");
    167168  if (!s->fid) {
    168     AUBIO_STRERR("sink_wavwrite: could not open %s (%s)\n", s->path, errorstr);
     169    AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno));
    169170    goto beach;
    170171  }
    171172
    172173  // ChunkID
    173   written += fwrite("RIFF", 4, 1, s->fid);
     174  fwrite("RIFF", 4, 1, s->fid);
    174175
    175176  // ChunkSize (0 for now, actual size will be written in _close)
    176   written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     177  fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
    177178
    178179  // Format
    179   written += fwrite("WAVE", 4, 1, s->fid);
     180  fwrite("WAVE", 4, 1, s->fid);
    180181
    181182  // Subchunk1ID
    182   written += fwrite("fmt ", 4, 1, s->fid);
     183  fwrite("fmt ", 4, 1, s->fid);
    183184
    184185  // Subchunk1Size
    185   written += fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
     186  fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
    186187
    187188  // AudioFormat
    188   written += fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
     189  fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
    189190
    190191  // NumChannels
    191   written += fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
     192  fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
    192193
    193194  // SampleRate
    194   written += fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
     195  fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
    195196
    196197  // ByteRate
    197198  byterate = s->samplerate * s->channels * s->bitspersample / 8;
    198   written += fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
     199  fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
    199200
    200201  // BlockAlign
    201202  blockalign = s->channels * s->bitspersample / 8;
    202   written += fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
     203  fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
    203204
    204205  // BitsPerSample
    205   written += fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
     206  fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
    206207
    207208  // Subchunk2ID
    208   written += fwrite("data", 4, 1, s->fid);
     209  fwrite("data", 4, 1, s->fid);
    209210
    210211  // Subchunk1Size (0 for now, actual size will be written in _close)
    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   }
     212  fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
    221213
    222214  s->scratch_size = s->max_size * s->channels;
     
    235227}
    236228
    237 static
    238 void 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 }
    250229
    251230void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){
    252   uint_t c = 0, i = 0;
     231  uint_t c = 0, i = 0, written_frames = 0;
    253232  uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
    254233      s->max_size, write_data->length, write);
     
    259238    }
    260239  }
    261 
    262   aubio_sink_wavwrite_write_frames(s, length);
     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;
    263248}
    264249
    265250void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){
    266   uint_t c = 0, i = 0;
     251  uint_t c = 0, i = 0, written_frames = 0;
    267252
    268253  uint_t channels = aubio_sink_validate_input_channels("sink_wavwrite", s->path,
     
    276261    }
    277262  }
    278 
    279   aubio_sink_wavwrite_write_frames(s, length);
     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;
    280271}
    281272
     
    283274  uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
    284275  unsigned char buf[5];
    285   size_t written = 0, err = 0;
    286276  if (!s->fid) return AUBIO_FAIL;
    287277  // ChunkSize
    288   err += fseek(s->fid, 4, SEEK_SET);
    289   written += fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
     278  fseek(s->fid, 4, SEEK_SET);
     279  fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
    290280  // Subchunk2Size
    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   }
     281  fseek(s->fid, 40, SEEK_SET);
     282  fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
    297283  // close file
    298284  if (fclose(s->fid)) {
    299     AUBIO_STRERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, errorstr);
     285    AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno));
    300286  }
    301287  s->fid = NULL;
Note: See TracChangeset for help on using the changeset viewer.