Changeset fcd963a


Ignore:
Timestamp:
Dec 6, 2013, 8:34:15 PM (11 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
e1cdb89
Parents:
ce5e424
Message:

src/io/source_avcodec.c: add _multi, building AVAudioResampleContext the first time _do or _do_multi is run

Location:
src/io
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/io/source_avcodec.c

    rce5e424 rfcd963a  
    5858  sint_t selected_stream;
    5959  uint_t eof;
     60  uint_t multi;
    6061};
     62
     63/* convenience function to recreate the AVAudioResampleContext the first time _do
     64 * or _do_multi is called */
     65void aubio_source_avcodec_set_multi(aubio_source_avcodec_t * s, uint_t multi);
    6166
    6267aubio_source_avcodec_t * new_aubio_source_avcodec(char_t * path, uint_t samplerate, uint_t hop_size) {
     
    158163  s->samplerate = samplerate;
    159164
    160   int64_t input_layout = av_get_default_channel_layout(s->input_channels);
    161   int64_t mono_layout = av_get_default_channel_layout(1);
    162 
    163   AVAudioResampleContext *avr = s->avr;
    164   avr = avresample_alloc_context();
    165   av_opt_set_int(avr, "in_channel_layout",  input_layout,           0);
    166   av_opt_set_int(avr, "out_channel_layout", mono_layout,            0);
    167   av_opt_set_int(avr, "in_sample_rate",     s->input_samplerate,    0);
    168   av_opt_set_int(avr, "out_sample_rate",    s->samplerate,          0);
    169   av_opt_set_int(avr, "in_sample_fmt",      avCodecCtx->sample_fmt, 0);
    170   av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLTP,     0);
    171   if ( ( err = avresample_open(avr) ) < 0) {
    172     char errorstr[256];
    173     av_strerror (err, errorstr, sizeof(errorstr));
    174     AUBIO_ERR("Could not open AVAudioResampleContext for %s (%s)\n",
    175         s->path, errorstr);
    176     goto beach;
    177   }
    178 
    179165  AVFrame *avFrame = s->avFrame;
    180166  avFrame = avcodec_alloc_frame();
     
    192178  s->avCodecCtx = avCodecCtx;
    193179  s->avFrame = avFrame;
    194   s->avr = avr;
     180
     181  // default to mono output
     182  aubio_source_avcodec_set_multi(s, 0);
    195183
    196184  s->eof = 0;
     185  s->multi = 0;
    197186
    198187  //av_log_set_level(AV_LOG_QUIET);
     
    201190
    202191beach:
    203   AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
    204       s->path, s->samplerate, s->hop_size);
     192  //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
     193  //    s->path, s->samplerate, s->hop_size);
    205194  del_aubio_source_avcodec(s);
    206195  return NULL;
     
    247236  av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
    248237      avFrame->nb_samples, avCodecCtx->sample_fmt, 1);
    249   //AUBIO_WRN("Got data_size %d in_linesize %d frame for (%s)\n",
    250   //    data_size, in_linesize, s->path);
    251 
    252238  int in_samples = avFrame->nb_samples;
    253239  int out_linesize = 0;
     
    273259}
    274260
     261void aubio_source_avcodec_set_multi(aubio_source_avcodec_t * s, uint_t multi) {
     262  if ( (multi != s->multi) || (s->avr == NULL) ) {
     263    int64_t input_layout = av_get_default_channel_layout(s->input_channels);
     264    int64_t mono_layout = av_get_default_channel_layout(1);
     265    if (s->avr != NULL) {
     266      avresample_close( s->avr );
     267      av_free ( s->avr );
     268      s->avr = NULL;
     269    }
     270    AVAudioResampleContext *avr = s->avr;
     271    avr = avresample_alloc_context();
     272
     273    av_opt_set_int(avr, "in_channel_layout",  input_layout,           0);
     274    if (multi) {
     275    av_opt_set_int(avr, "out_channel_layout", input_layout,            0);
     276    } else {
     277    av_opt_set_int(avr, "out_channel_layout", mono_layout,            0);
     278    }
     279    av_opt_set_int(avr, "in_sample_rate",     s->input_samplerate,    0);
     280    av_opt_set_int(avr, "out_sample_rate",    s->samplerate,          0);
     281    av_opt_set_int(avr, "in_sample_fmt",      s->avCodecCtx->sample_fmt, 0);
     282    av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLT,      0);
     283    int err;
     284    if ( ( err = avresample_open(avr) ) < 0) {
     285      char errorstr[256];
     286      av_strerror (err, errorstr, sizeof(errorstr));
     287      AUBIO_ERR("Could not open AVAudioResampleContext for %s (%s)\n",
     288          s->path, errorstr);
     289      //goto beach;
     290      return;
     291    }
     292    s->avr = avr;
     293    s->multi = multi;
     294  }
     295}
     296
    275297void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
     298  if (s->multi == 1) aubio_source_avcodec_set_multi(s, 0);
    276299  uint_t i;
    277300  uint_t end = 0;
     
    304327
    305328void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){
    306   //uint_t i,j, input_channels = s->input_channels;
     329  if (s->multi == 0) aubio_source_avcodec_set_multi(s, 1);
     330  uint_t i,j;
     331  uint_t end = 0;
     332  uint_t total_wrote = 0;
     333  while (total_wrote < s->hop_size) {
     334    end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
     335    for (j = 0; j < read_data->height; j++) {
     336      for (i = 0; i < end; i++) {
     337        read_data->data[j][i + total_wrote] =
     338          s->output[(i + s->read_index) * s->input_channels + j];
     339      }
     340    }
     341    total_wrote += end;
     342    if (total_wrote < s->hop_size) {
     343      uint_t avcodec_read = 0;
     344      aubio_source_avcodec_readframe(s, &avcodec_read);
     345      s->read_samples = avcodec_read;
     346      s->read_index = 0;
     347      if (s->eof) {
     348        break;
     349      }
     350    } else {
     351      s->read_index += end;
     352    }
     353  }
     354  if (total_wrote < s->hop_size) {
     355    for (j = 0; j < read_data->height; j++) {
     356      for (i = end; i < s->hop_size; i++) {
     357        read_data->data[j][i] = 0.;
     358      }
     359    }
     360  }
     361  *read = total_wrote;
    307362}
    308363
     
    317372uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) {
    318373  //uint_t resampled_pos = (uint_t)ROUND(pos * s->input_samplerate * 1. / s->samplerate);
    319   return 0; //sf_seek (s->handle, resampled_pos, SEEK_SET);
     374  AUBIO_ERR("Not implemented, _seek(%d) file %s", pos, s->path);
     375  return -1; //sf_seek (s->handle, resampled_pos, SEEK_SET);
    320376}
    321377
  • src/io/source_avcodec.h

    rce5e424 rfcd963a  
    6565  \param s source object, created with ::new_aubio_source_avcodec
    6666  \param read_to ::fvec_t of data to read to
    67   \param read upon returns, equals to number of frames actually read
     67  \param[out] read upon returns, equals to number of frames actually read
    6868
    6969  Upon returns, `read` contains the number of frames actually read from the
Note: See TracChangeset for help on using the changeset viewer.