Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/sink_vorbis.c

    r0e6ad10 r6031419  
    3636#include <errno.h> // errno
    3737#include <time.h> // time
    38 
    39 #define MAX_SIZE 2048
    4038
    4139struct _aubio_sink_vorbis_t {
     
    6967  aubio_sink_vorbis_t * s = AUBIO_NEW(aubio_sink_vorbis_t);
    7068
    71   if (!uri) {
    72     AUBIO_ERROR("sink_vorbis: Aborted opening null path\n");
    73     goto failure;
    74   }
    75 
    7669  s->path = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX) + 1);
    7770  strncpy(s->path, uri, strnlen(uri, PATH_MAX) + 1);
     
    117110
    118111  s->fid = fopen((const char *)s->path, "wb");
    119   if (!s->fid) {
    120     AUBIO_ERR("sink_vorbis: Error opening file %s (%s)\n",
    121         s->path, strerror(errno));
    122     return AUBIO_FAIL;
    123   }
     112  if (!s->fid) return AUBIO_FAIL;
    124113
    125114  vorbis_info_init(&s->vi);
     
    145134  {
    146135    int ret = 0;
    147     size_t wrote;
    148136    ogg_packet header;
    149137    ogg_packet header_comm;
     
    162150      ret = ogg_stream_flush(&s->os, &s->og);
    163151      if (ret==0) break;
    164       wrote = fwrite(s->og.header, 1, s->og.header_len, s->fid);
    165       ret = (wrote == (unsigned)s->og.header_len);
    166       wrote = fwrite(s->og.body,   1, s->og.body_len,   s->fid);
    167       ret &= (wrote == (unsigned)s->og.body_len);
    168       if (ret == 0) {
    169         AUBIO_ERR("sink_vorbis: failed writing \'%s\' to disk (%s)\n",
    170             s->path, strerror(errno));
    171         return AUBIO_FAIL;
    172       }
     152      fwrite(s->og.header, 1, s->og.header_len, s->fid);
     153      fwrite(s->og.body,   1, s->og.body_len,   s->fid);
    173154    }
    174155  }
     
    183164    return AUBIO_FAIL;
    184165  s->samplerate = samplerate;
    185   if (/* s->samplerate != 0 && */ s->channels != 0)
     166  if (s->samplerate != 0 && s->channels != 0)
    186167    return aubio_sink_vorbis_open(s);
    187168  return AUBIO_OK;
     
    196177  s->channels = channels;
    197178  // automatically open when both samplerate and channels have been set
    198   if (s->samplerate != 0 /* && s->channels != 0 */) {
     179  if (s->samplerate != 0 && s->channels != 0) {
    199180    return aubio_sink_vorbis_open(s);
    200181  }
     
    214195void aubio_sink_vorbis_write(aubio_sink_vorbis_t *s)
    215196{
    216   int result;
    217   size_t wrote;
    218197  // pre-analysis
    219198  while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
     
    227206
    228207      while (1) {
    229         result = ogg_stream_pageout (&s->os, &s->og);
     208        int result = ogg_stream_pageout (&s->os, &s->og);
    230209        if (result == 0) break;
    231         wrote = fwrite(s->og.header, 1, s->og.header_len, s->fid);
    232         result = (wrote == (unsigned)s->og.header_len);
    233         wrote = fwrite(s->og.body, 1, s->og.body_len,     s->fid);
    234         result &= (wrote == (unsigned)s->og.body_len);
    235         if (result == 0) {
    236           AUBIO_WRN("sink_vorbis: failed writing \'%s\' to disk (%s)\n",
    237               s->path, strerror(errno));
    238         }
     210        fwrite(s->og.header, 1, s->og.header_len, s->fid);
     211        fwrite(s->og.body,   1, s->og.body_len,   s->fid);
    239212        if (ogg_page_eos(&s->og)) break;
    240213      }
     
    247220{
    248221  uint_t c, v;
    249   uint_t length = aubio_sink_validate_input_length("sink_vorbis", s->path,
    250       MAX_SIZE, write_data->length, write);
    251   float **buffer = vorbis_analysis_buffer(&s->vd, (long)length);
     222  float **buffer = vorbis_analysis_buffer(&s->vd, (long)write);
    252223  // fill buffer
    253224  if (!write) {
     
    258229  } else {
    259230    for (c = 0; c < s->channels; c++) {
    260       for (v = 0; v < length; v++) {
     231      for (v = 0; v < write; v++) {
    261232        buffer[c][v] = write_data->data[v];
    262233      }
    263234    }
    264235    // tell vorbis how many frames were written
    265     vorbis_analysis_wrote(&s->vd, (long)length);
     236    vorbis_analysis_wrote(&s->vd, (long)write);
    266237  }
    267238  // write to file
     
    273244{
    274245  uint_t c, v;
    275   uint_t channels = aubio_sink_validate_input_channels("sink_vorbis", s->path,
    276       s->channels, write_data->height);
    277   uint_t length = aubio_sink_validate_input_length("sink_vorbis", s->path,
    278       MAX_SIZE, write_data->length, write);
    279   float **buffer = vorbis_analysis_buffer(&s->vd, (long)length);
     246  uint_t channels = MIN(s->channels, write_data->height);
     247  float **buffer = vorbis_analysis_buffer(&s->vd, (long)write);
    280248  // fill buffer
    281249  if (!write) {
     
    286254  } else {
    287255    for (c = 0; c < channels; c++) {
    288       for (v = 0; v < length; v++) {
     256      for (v = 0; v < write; v++) {
    289257        buffer[c][v] = write_data->data[c][v];
    290258      }
     
    299267uint_t aubio_sink_vorbis_close (aubio_sink_vorbis_t *s)
    300268{
    301   if (!s->fid) return AUBIO_FAIL;
    302269  //mark the end of stream
    303270  vorbis_analysis_wrote(&s->vd, 0);
     
    305272  aubio_sink_vorbis_write(s);
    306273
    307   if (s->fid && fclose(s->fid)) {
     274  if (fclose(s->fid)) {
    308275    AUBIO_ERR("sink_vorbis: Error closing file %s (%s)\n",
    309276        s->path, strerror(errno));
    310277    return AUBIO_FAIL;
    311278  }
    312   s->fid = NULL;
    313279  return AUBIO_OK;
    314280}
Note: See TracChangeset for help on using the changeset viewer.