Ignore:
Timestamp:
Dec 17, 2018, 4:42:29 PM (5 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/crepe, fix/ffmpeg5, master
Children:
d286fe4
Parents:
2de7cfa (diff), 09b4be9 (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_vorbis.c

    r2de7cfa rd013a93  
    3636#include <errno.h> // errno
    3737#include <time.h> // time
     38
     39#define MAX_SIZE 2048
    3840
    3941struct _aubio_sink_vorbis_t {
     
    6769  aubio_sink_vorbis_t * s = AUBIO_NEW(aubio_sink_vorbis_t);
    6870
     71  if (!uri) {
     72    AUBIO_ERROR("sink_vorbis: Aborted opening null path\n");
     73    goto failure;
     74  }
     75
    6976  s->path = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX) + 1);
    7077  strncpy(s->path, uri, strnlen(uri, PATH_MAX) + 1);
     
    110117
    111118  s->fid = fopen((const char *)s->path, "wb");
    112   if (!s->fid) return AUBIO_FAIL;
     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  }
    113124
    114125  vorbis_info_init(&s->vi);
     
    134145  {
    135146    int ret = 0;
     147    size_t wrote;
    136148    ogg_packet header;
    137149    ogg_packet header_comm;
     
    150162      ret = ogg_stream_flush(&s->os, &s->og);
    151163      if (ret==0) break;
    152       fwrite(s->og.header, 1, s->og.header_len, s->fid);
    153       fwrite(s->og.body,   1, s->og.body_len,   s->fid);
     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      }
    154173    }
    155174  }
     
    164183    return AUBIO_FAIL;
    165184  s->samplerate = samplerate;
    166   if (s->samplerate != 0 && s->channels != 0)
     185  if (/* s->samplerate != 0 && */ s->channels != 0)
    167186    return aubio_sink_vorbis_open(s);
    168187  return AUBIO_OK;
     
    177196  s->channels = channels;
    178197  // automatically open when both samplerate and channels have been set
    179   if (s->samplerate != 0 && s->channels != 0) {
     198  if (s->samplerate != 0 /* && s->channels != 0 */) {
    180199    return aubio_sink_vorbis_open(s);
    181200  }
     
    195214void aubio_sink_vorbis_write(aubio_sink_vorbis_t *s)
    196215{
     216  int result;
     217  size_t wrote;
    197218  // pre-analysis
    198219  while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
     
    206227
    207228      while (1) {
    208         int result = ogg_stream_pageout (&s->os, &s->og);
     229        result = ogg_stream_pageout (&s->os, &s->og);
    209230        if (result == 0) break;
    210         fwrite(s->og.header, 1, s->og.header_len, s->fid);
    211         fwrite(s->og.body,   1, s->og.body_len,   s->fid);
     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        }
    212239        if (ogg_page_eos(&s->og)) break;
    213240      }
     
    220247{
    221248  uint_t c, v;
    222   float **buffer = vorbis_analysis_buffer(&s->vd, (long)write);
     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);
    223252  // fill buffer
    224253  if (!write) {
     
    229258  } else {
    230259    for (c = 0; c < s->channels; c++) {
    231       for (v = 0; v < write; v++) {
     260      for (v = 0; v < length; v++) {
    232261        buffer[c][v] = write_data->data[v];
    233262      }
    234263    }
    235264    // tell vorbis how many frames were written
    236     vorbis_analysis_wrote(&s->vd, (long)write);
     265    vorbis_analysis_wrote(&s->vd, (long)length);
    237266  }
    238267  // write to file
     
    244273{
    245274  uint_t c, v;
    246   uint_t channels = MIN(s->channels, write_data->height);
    247   float **buffer = vorbis_analysis_buffer(&s->vd, (long)write);
     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);
    248280  // fill buffer
    249281  if (!write) {
     
    254286  } else {
    255287    for (c = 0; c < channels; c++) {
    256       for (v = 0; v < write; v++) {
     288      for (v = 0; v < length; v++) {
    257289        buffer[c][v] = write_data->data[c][v];
    258290      }
     
    267299uint_t aubio_sink_vorbis_close (aubio_sink_vorbis_t *s)
    268300{
     301  if (!s->fid) return AUBIO_FAIL;
    269302  //mark the end of stream
    270303  vorbis_analysis_wrote(&s->vd, 0);
     
    272305  aubio_sink_vorbis_write(s);
    273306
    274   if (fclose(s->fid)) {
     307  if (s->fid && fclose(s->fid)) {
    275308    AUBIO_ERR("sink_vorbis: Error closing file %s (%s)\n",
    276309        s->path, strerror(errno));
    277310    return AUBIO_FAIL;
    278311  }
     312  s->fid = NULL;
    279313  return AUBIO_OK;
    280314}
Note: See TracChangeset for help on using the changeset viewer.