Changeset d013a93 for src


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

Location:
src
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • src/aubio_priv.h

    r2de7cfa rd013a93  
    368368#endif /* __STRICT_ANSI__ */
    369369
     370#if defined(DEBUG)
     371#include <assert.h>
     372#define AUBIO_ASSERT(x) assert(x)
     373#else
     374#define AUBIO_ASSERT(x)
     375#endif /* DEBUG */
     376
    370377#endif /* AUBIO_PRIV_H */
  • src/io/ioutils.c

    r2de7cfa rd013a93  
    5252  return AUBIO_OK;
    5353}
     54
     55uint_t
     56aubio_sink_validate_input_length(const char_t *kind, const char_t *path,
     57    uint_t max_size, uint_t write_data_length, uint_t write)
     58{
     59  uint_t can_write = write;
     60
     61  if (write > max_size) {
     62    AUBIO_WRN("%s: partial write to %s, trying to write %d frames,"
     63        " at most %d can be written at once\n", kind, path, write, max_size);
     64    can_write = max_size;
     65  }
     66
     67  if (can_write > write_data_length) {
     68    AUBIO_WRN("%s: partial write to %s, trying to write %d frames,"
     69        " but found input of length %d\n", kind, path, write,
     70        write_data_length);
     71    can_write = write_data_length;
     72  }
     73
     74  return can_write;
     75}
     76
     77uint_t
     78aubio_sink_validate_input_channels(const char_t *kind, const char_t *path,
     79    uint_t sink_channels, uint_t write_data_height)
     80{
     81  uint_t channels = sink_channels;
     82  if (write_data_height < sink_channels) {
     83    AUBIO_WRN("%s: partial write to %s, trying to write %d channels,"
     84        " but found input of height %d\n", kind, path, sink_channels,
     85        write_data_height);
     86    channels = write_data_height;
     87  }
     88  return channels;
     89}
  • src/io/ioutils.h

    r2de7cfa rd013a93  
    5454    uint_t channels);
    5555
     56/** validate length of input
     57
     58  \param kind       the object kind to report on
     59  \param path       the path to report on
     60  \param max_size   maximum number of frames that can be written
     61  \param write_data_length actual length of input vector/matrix
     62  \param write number of samples asked
     63
     64  \return write or the maximum number of frames that can be written
     65*/
     66uint_t
     67aubio_sink_validate_input_length(const char_t *kind, const char_t *path,
     68    uint_t max_size, uint_t write_data_length, uint_t write);
     69
     70/** validate height of input
     71
     72  \param kind       the object kind to report on
     73  \param path       the path to report on
     74  \param max_size   maximum number of channels that can be written
     75  \param write_data_height actual height of input matrix
     76
     77  \return write_data_height or the maximum number of channels
     78*/
     79uint_t
     80aubio_sink_validate_input_channels(const char_t *kind, const char_t *path,
     81    uint_t sink_channels, uint_t write_data_height);
     82
    5683#ifdef __cplusplus
    5784}
  • src/io/sink.c

    r2de7cfa rd013a93  
    195195  AUBIO_ERROR("sink: failed creating '%s' at %dHz (no sink built-in)\n", uri, samplerate);
    196196#endif
    197   AUBIO_FREE(s);
     197  del_aubio_sink(s);
    198198  return NULL;
    199199}
     
    228228
    229229void del_aubio_sink(aubio_sink_t * s) {
    230   if (!s) return;
    231   s->s_del((void *)s->sink);
     230  AUBIO_ASSERT(s);
     231  if (s->s_del && s->sink)
     232    s->s_del((void *)s->sink);
    232233  AUBIO_FREE(s);
    233   return;
    234 }
     234}
  • src/io/sink_apple_audio.c

    r2de7cfa rd013a93  
    6262  s->async = false;
    6363
    64   if ( (uri == NULL) || (strlen(uri) < 1) ) {
     64  if ( (uri == NULL) || (strnlen(uri, PATH_MAX) < 1) ) {
    6565    AUBIO_ERROR("sink_apple_audio: Aborted opening null path\n");
    6666    goto beach;
    6767  }
    68   if (s->path != NULL) AUBIO_FREE(s->path);
     68
    6969  s->path = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX) + 1);
    7070  strncpy(s->path, uri, strnlen(uri, PATH_MAX) + 1);
     
    9292  return s;
    9393beach:
    94   AUBIO_FREE(s);
     94  del_aubio_sink_apple_audio(s);
    9595  return NULL;
    9696}
     
    103103  s->samplerate = samplerate;
    104104  // automatically open when both samplerate and channels have been set
    105   if (s->samplerate != 0 && s->channels != 0) {
     105  if (/* s->samplerate != 0 && */ s->channels != 0) {
    106106    return aubio_sink_apple_audio_open(s);
    107107  }
     
    116116  s->channels = channels;
    117117  // automatically open when both samplerate and channels have been set
    118   if (s->samplerate != 0 && s->channels != 0) {
     118  if (s->samplerate != 0 /* && s->channels != 0 */) {
    119119    return aubio_sink_apple_audio_open(s);
    120120  }
     
    176176  UInt32 c, v;
    177177  short *data = (short*)s->bufferList.mBuffers[0].mData;
    178   if (write > s->max_frames) {
    179     AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
    180     write = s->max_frames;
    181   }
    182   smpl_t *buf = write_data->data;
    183 
    184   if (buf) {
    185       for (c = 0; c < s->channels; c++) {
    186           for (v = 0; v < write; v++) {
    187               data[v * s->channels + c] =
    188                   FLOAT_TO_SHORT(buf[ v * s->channels + c]);
    189           }
    190       }
    191   }
    192   aubio_sink_apple_audio_write(s, write);
     178  uint_t length = aubio_sink_validate_input_length("sink_apple_audio", s->path,
     179      s->max_frames, write_data->length, write);
     180
     181  for (c = 0; c < s->channels; c++) {
     182    for (v = 0; v < length; v++) {
     183      data[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[v]);
     184    }
     185  }
     186
     187  aubio_sink_apple_audio_write(s, length);
    193188}
    194189
     
    196191  UInt32 c, v;
    197192  short *data = (short*)s->bufferList.mBuffers[0].mData;
    198   if (write > s->max_frames) {
    199     AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
    200     write = s->max_frames;
    201   }
    202   smpl_t **buf = write_data->data;
    203 
    204   if (buf) {
    205       for (c = 0; c < s->channels; c++) {
    206           for (v = 0; v < write; v++) {
    207               data[v * s->channels + c] =
    208                   FLOAT_TO_SHORT(buf[c][v]);
    209           }
    210       }
    211   }
    212   aubio_sink_apple_audio_write(s, write);
     193  uint_t channels = aubio_sink_validate_input_channels("sink_apple_audio",
     194      s->path, s->channels, write_data->height);
     195  uint_t length = aubio_sink_validate_input_length("sink_apple_audio", s->path,
     196      s->max_frames, write_data->length, write);
     197
     198  for (c = 0; c < channels; c++) {
     199    for (v = 0; v < length; v++) {
     200      data[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[c][v]);
     201    }
     202  }
     203
     204  aubio_sink_apple_audio_write(s, length);
    213205}
    214206
     
    258250
    259251void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
    260   if (s->audioFile) aubio_sink_apple_audio_close (s);
    261   if (s->path) AUBIO_FREE(s->path);
     252  AUBIO_ASSERT(s);
     253  if (s->audioFile)
     254    aubio_sink_apple_audio_close (s);
     255  if (s->path)
     256    AUBIO_FREE(s->path);
    262257  freeAudioBufferList(&s->bufferList);
    263258  AUBIO_FREE(s);
    264   return;
    265259}
    266260
  • src/io/sink_flac.c

    r2de7cfa rd013a93  
    3535#include <FLAC/stream_encoder.h>
    3636
    37 #include <vorbis/vorbisenc.h>
    3837#include <string.h> // strerror
    3938#include <errno.h> // errno
     
    7978
    8079#if 0
    81 static void aubio_sink_vorbis_callback(const FLAC__StreamEncoder* encoder,
     80static void aubio_sink_flac_callback(const FLAC__StreamEncoder* encoder,
    8281    FLAC__uint64 bytes_written, FLAC__uint64 samples_written,
    8382    unsigned frames_writtten, unsigned total_frames_estimate,
     
    8988{
    9089  aubio_sink_flac_t * s = AUBIO_NEW(aubio_sink_flac_t);
     90
     91  if (!uri) {
     92    AUBIO_ERROR("sink_flac: Aborted opening null path\n");
     93    goto failure;
     94  }
    9195
    9296  s->path = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX) + 1);
     
    196200  init_status = FLAC__stream_encoder_init_file(s->encoder, s->path,
    197201      NULL, NULL);
    198       //aubio_sink_vorbis_callback, s);
     202      //aubio_sink_flac_callback, s);
    199203  if (init_status == FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE) {
    200204    AUBIO_ERR("sink_flac: failed initilizing encoder for %s"
     
    259263{
    260264  uint_t c, v;
     265  uint_t length = aubio_sink_validate_input_length("sink_flac", s->path,
     266      MAX_WRITE_SIZE, write_data->length, write);
    261267  // fill buffer
    262268  if (!write) {
    263269    return;
    264   } else if (write > MAX_WRITE_SIZE) {
    265     AUBIO_ERR("sink_flac: max_write request %dm asked for %d,"
    266        " failed writing to %s\n", write, MAX_WRITE_SIZE, s->path);
    267     return;
    268270  } else {
    269271    for (c = 0; c < s->channels; c++) {
    270       for (v = 0; v < write; v++) {
     272      for (v = 0; v < length; v++) {
    271273        s->buffer[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[v]);
    272274      }
     
    275277  // send to encoder
    276278  FLAC__stream_encoder_process_interleaved(s->encoder,
    277       (const FLAC__int32*)s->buffer, write);
     279      (const FLAC__int32*)s->buffer, length);
    278280}
    279281
     
    282284{
    283285  uint_t c, v;
    284   uint_t channels = MIN(s->channels, write_data->height);
     286  uint_t channels = aubio_sink_validate_input_channels("sink_flac", s->path,
     287      s->channels, write_data->height);
     288  uint_t length = aubio_sink_validate_input_length("sink_flac", s->path,
     289      MAX_WRITE_SIZE, write_data->length, write);
    285290  // fill buffer
    286291  if (!write) {
    287292    return;
    288   } else if (write > MAX_WRITE_SIZE) {
    289     AUBIO_ERR("sink_flac: max_write request %dm asked for %d,"
    290        " failed writing to %s\n", write, MAX_WRITE_SIZE, s->path);
    291     return;
    292293  } else {
    293294    for (c = 0; c < channels; c++) {
    294       for (v = 0; v < write; v++) {
     295      for (v = 0; v < length; v++) {
    295296        s->buffer[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[c][v]);
    296297      }
     
    298299    // send to encoder
    299300    FLAC__stream_encoder_process_interleaved(s->encoder,
    300         (const FLAC__int32*)s->buffer, write);
     301        (const FLAC__int32*)s->buffer, length);
    301302  }
    302303}
     
    305306{
    306307  uint_t ret = AUBIO_OK;
     308
     309  if (!s->fid) return AUBIO_FAIL;
    307310
    308311  if (s->encoder) {
     
    325328  }
    326329
    327   if (s->fid) {
    328     if (fclose(s->fid)) {
    329       AUBIO_ERR("sink_flac: Error closing file %s (%s)\n",
    330           s->path, strerror(errno));
    331       ret &= AUBIO_FAIL;
    332     }
    333   }
     330  if (s->fid && fclose(s->fid)) {
     331    AUBIO_ERR("sink_flac: Error closing file %s (%s)\n",
     332        s->path, strerror(errno));
     333    ret &= AUBIO_FAIL;
     334  }
     335  s->fid = NULL;
     336
    334337  return ret;
    335338}
    336339
    337340#if 0
    338 static void aubio_sink_vorbis_callback(const FLAC__StreamEncoder* encoder UNUSED,
     341static void aubio_sink_flac_callback(const FLAC__StreamEncoder* encoder UNUSED,
    339342    FLAC__uint64 bytes_written, FLAC__uint64 samples_written,
    340343    unsigned frames_written, unsigned total_frames_estimate,
  • src/io/sink_sndfile.c

    r2de7cfa rd013a93  
    5959  if (path == NULL) {
    6060    AUBIO_ERR("sink_sndfile: Aborted opening null path\n");
    61     return NULL;
    62   }
    63 
    64   if (s->path) AUBIO_FREE(s->path);
     61    goto beach;
     62  }
     63
    6564  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
    6665  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
     
    9897  s->samplerate = samplerate;
    9998  // automatically open when both samplerate and channels have been set
    100   if (s->samplerate != 0 && s->channels != 0) {
     99  if (/* s->samplerate != 0 && */ s->channels != 0) {
    101100    return aubio_sink_sndfile_open(s);
    102101  }
     
    111110  s->channels = channels;
    112111  // automatically open when both samplerate and channels have been set
    113   if (s->samplerate != 0 && s->channels != 0) {
     112  if (s->samplerate != 0 /* && s->channels != 0 */) {
    114113    return aubio_sink_sndfile_open(s);
    115114  }
     
    148147  /* allocate data for de/interleaving reallocated when needed. */
    149148  if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) {
    150     abort();
    151     AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
     149    AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum buffer size %d\n",
    152150        s->max_size, s->channels, MAX_SIZE * AUBIO_MAX_CHANNELS);
    153151    return AUBIO_FAIL;
     
    159157
    160158void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
    161   uint_t i, j, channels = s->channels;
    162   int nsamples = 0;
    163   smpl_t *pwrite;
     159  uint_t i, j;
    164160  sf_count_t written_frames;
    165 
    166   if (write > s->max_size) {
    167     AUBIO_WRN("sink_sndfile: trying to write %d frames, but only %d can be written at a time\n",
    168       write, s->max_size);
    169     write = s->max_size;
    170   }
    171 
    172   nsamples = channels * write;
     161  uint_t channels = s->channels;
     162  uint_t length = aubio_sink_validate_input_length("sink_sndfile", s->path,
     163      s->max_size, write_data->length, write);
     164  int nsamples = channels * length;
    173165
    174166  /* interleaving data  */
    175167  for ( i = 0; i < channels; i++) {
    176     pwrite = (smpl_t *)write_data->data;
    177     for (j = 0; j < write; j++) {
    178       s->scratch_data[channels*j+i] = pwrite[j];
     168    for (j = 0; j < length; j++) {
     169      s->scratch_data[channels*j+i] = write_data->data[j];
    179170    }
    180171  }
     
    189180
    190181void aubio_sink_sndfile_do_multi(aubio_sink_sndfile_t *s, fmat_t * write_data, uint_t write){
    191   uint_t i, j, channels = s->channels;
    192   int nsamples = 0;
    193   smpl_t *pwrite;
     182  uint_t i, j;
    194183  sf_count_t written_frames;
    195 
    196   if (write > s->max_size) {
    197     AUBIO_WRN("sink_sndfile: trying to write %d frames, but only %d can be written at a time\n",
    198       write, s->max_size);
    199     write = s->max_size;
    200   }
    201 
    202   nsamples = channels * write;
     184  uint_t channels = aubio_sink_validate_input_channels("sink_sndfile", s->path,
     185      s->channels, write_data->height);
     186  uint_t length = aubio_sink_validate_input_length("sink_sndfile", s->path,
     187      s->max_size, write_data->length, write);
     188  int nsamples = channels * length;
    203189
    204190  /* interleaving data  */
    205   for ( i = 0; i < write_data->height; i++) {
    206     pwrite = (smpl_t *)write_data->data[i];
    207     for (j = 0; j < write; j++) {
    208       s->scratch_data[channels*j+i] = pwrite[j];
     191  for ( i = 0; i < channels; i++) {
     192    for (j = 0; j < length; j++) {
     193      s->scratch_data[channels*j+i] = write_data->data[i][j];
    209194    }
    210195  }
     
    231216
    232217void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
    233   if (!s) return;
    234   if (s->path) AUBIO_FREE(s->path);
    235   aubio_sink_sndfile_close(s);
    236   AUBIO_FREE(s->scratch_data);
     218  AUBIO_ASSERT(s);
     219  if (s->handle)
     220    aubio_sink_sndfile_close(s);
     221  if (s->path)
     222    AUBIO_FREE(s->path);
     223  if (s->scratch_data)
     224    AUBIO_FREE(s->scratch_data);
    237225  AUBIO_FREE(s);
    238226}
  • 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}
  • src/io/sink_wavwrite.c

    r2de7cfa rd013a93  
    8888    goto beach;
    8989  }
    90   if ((sint_t)samplerate < 0) {
    91     AUBIO_ERR("sink_wavwrite: Can not create %s with samplerate %d\n", path, samplerate);
    92     goto beach;
    93   }
    94 
    95   if (s->path) AUBIO_FREE(s->path);
     90
    9691  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
    9792  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
     
    136131  s->samplerate = samplerate;
    137132  // automatically open when both samplerate and channels have been set
    138   if (s->samplerate != 0 && s->channels != 0) {
     133  if (/* s->samplerate != 0 && */ s->channels != 0) {
    139134    return aubio_sink_wavwrite_open(s);
    140135  }
     
    149144  s->channels = channels;
    150145  // automatically open when both samplerate and channels have been set
    151   if (s->samplerate != 0 && s->channels != 0) {
     146  if (s->samplerate != 0 /* && s->channels != 0 */) {
    152147    return aubio_sink_wavwrite_open(s);
    153148  }
     
    234229
    235230void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){
    236   uint_t i = 0, written_frames = 0;
    237 
    238   if (write > s->max_size) {
    239     AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
    240         "but only %d can be written at a time\n", write, s->path, s->max_size);
    241     write = s->max_size;
    242   }
    243 
    244   for (i = 0; i < write; i++) {
    245     s->scratch_data[i] = HTOLES(FLOAT_TO_SHORT(write_data->data[i]));
    246   }
    247   written_frames = fwrite(s->scratch_data, 2, write, s->fid);
     231  uint_t c = 0, i = 0, written_frames = 0;
     232  uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
     233      s->max_size, write_data->length, write);
     234
     235  for (c = 0; c < s->channels; c++) {
     236    for (i = 0; i < length; i++) {
     237      s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[i]));
     238    }
     239  }
     240  written_frames = fwrite(s->scratch_data, 2, length * s->channels, s->fid);
    248241
    249242  if (written_frames != write) {
     
    258251  uint_t c = 0, i = 0, written_frames = 0;
    259252
    260   if (write > s->max_size) {
    261     AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
    262         "but only %d can be written at a time\n", write, s->path, s->max_size);
    263     write = s->max_size;
    264   }
    265 
    266   for (c = 0; c < s->channels; c++) {
    267     for (i = 0; i < write; i++) {
     253  uint_t channels = aubio_sink_validate_input_channels("sink_wavwrite", s->path,
     254      s->channels, write_data->height);
     255  uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
     256      s->max_size, write_data->length, write);
     257
     258  for (c = 0; c < channels; c++) {
     259    for (i = 0; i < length; i++) {
    268260      s->scratch_data[i * s->channels + c] = HTOLES(FLOAT_TO_SHORT(write_data->data[c][i]));
    269261    }
    270262  }
    271   written_frames = fwrite(s->scratch_data, 2, write * s->channels, s->fid);
     263  written_frames = fwrite(s->scratch_data, 2, length * s->channels, s->fid);
    272264
    273265  if (written_frames != write * s->channels) {
     
    298290
    299291void del_aubio_sink_wavwrite(aubio_sink_wavwrite_t * s){
    300   if (!s) return;
    301   aubio_sink_wavwrite_close(s);
    302   if (s->path) AUBIO_FREE(s->path);
    303   AUBIO_FREE(s->scratch_data);
     292  AUBIO_ASSERT(s);
     293  if (s->fid)
     294    aubio_sink_wavwrite_close(s);
     295  if (s->path)
     296    AUBIO_FREE(s->path);
     297  if (s->scratch_data)
     298    AUBIO_FREE(s->scratch_data);
    304299  AUBIO_FREE(s);
    305300}
  • src/io/source.c

    r2de7cfa rd013a93  
    122122     " (no source built-in)\n", uri, samplerate, hop_size);
    123123#endif
    124   AUBIO_FREE(s);
     124  del_aubio_source(s);
    125125  return NULL;
    126126}
     
    139139
    140140void del_aubio_source(aubio_source_t * s) {
    141   if (!s) return;
    142   s->s_del((void *)s->source);
     141  AUBIO_ASSERT(s);
     142  if (s->s_del && s->source)
     143    s->s_del((void *)s->source);
    143144  AUBIO_FREE(s);
    144145}
  • src/io/source.h

    r2de7cfa rd013a93  
    6060
    6161  \example io/test-source.c
    62   \example io/test-source_multi.c
    6362
    6463*/
  • src/io/source_apple_audio.c

    r2de7cfa rd013a93  
    6060  aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
    6161
    62   if (path == NULL) {
     62  if (path == NULL || strnlen(path, PATH_MAX) < 1) {
    6363    AUBIO_ERROR("source_apple_audio: Aborted opening null path\n");
    6464    goto beach;
     
    8686
    8787beach:
    88   AUBIO_FREE(s);
     88  del_aubio_source_apple_audio(s);
    8989  return NULL;
    9090}
     
    9595  UInt32 propSize;
    9696
    97   if (s->path) AUBIO_FREE(s->path);
    9897  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
    9998  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
     
    294293
    295294void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
     295  AUBIO_ASSERT(s);
    296296  aubio_source_apple_audio_close (s);
    297297  if (s->path) AUBIO_FREE(s->path);
    298298  freeAudioBufferList(&s->bufferList);
    299299  AUBIO_FREE(s);
    300   return;
    301300}
    302301
  • src/io/source_avcodec.c

    r2de7cfa rd013a93  
    4545#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57,0,0)
    4646#define HAVE_AUBIO_LIBAVCODEC_DEPRECATED 1
     47#endif
     48
     49#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,3,102)
     50#define HAVE_AUBIO_LIBAVCODEC_TIMEBASE_FIX 1
    4751#endif
    4852
     
    144148  s->channels = 1;
    145149
    146   if (s->path) AUBIO_FREE(s->path);
    147150  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
    148151  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
     
    240243    goto beach;
    241244  }
     245#if HAVE_AUBIO_LIBAVCODEC_TIMEBASE_FIX
     246  // avoids 'skipped frames warning' with avecodec < 58, deprecated after
     247  av_codec_set_pkt_timebase(avCodecCtx,
     248      avFormatCtx->streams[selected_stream]->time_base);
     249#endif
    242250#endif
    243251
     
    631639
    632640void del_aubio_source_avcodec(aubio_source_avcodec_t * s){
    633   if (!s) return;
     641  AUBIO_ASSERT(s);
    634642  aubio_source_avcodec_close(s);
    635643  if (s->output != NULL) {
  • src/io/source_sndfile.c

    r2de7cfa rd013a93  
    8787  s->channels = 1;
    8888
    89   if (s->path) AUBIO_FREE(s->path);
    9089  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
    9190  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
     
    332331
    333332void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
    334   if (!s) return;
     333  AUBIO_ASSERT(s);
    335334  aubio_source_sndfile_close(s);
    336335#ifdef HAVE_SAMPLERATE
  • src/io/source_wavread.c

    r2de7cfa rd013a93  
    9292  }
    9393
    94   if (s->path) AUBIO_FREE(s->path);
    9594  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
    9695  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
     
    472471
    473472void del_aubio_source_wavread(aubio_source_wavread_t * s) {
    474   if (!s) return;
     473  AUBIO_ASSERT(s);
    475474  aubio_source_wavread_close(s);
    476475  if (s->short_output) AUBIO_FREE(s->short_output);
Note: See TracChangeset for help on using the changeset viewer.