Changeset 87dfc3f for src/io


Ignore:
Timestamp:
Jul 16, 2012, 2:55:13 AM (12 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:
7839c61
Parents:
e3b5962
Message:

src/io/source_sndfile.c: add support for resampling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/source_sndfile.c

    re3b5962 r87dfc3f  
    3030#include "fvec.h"
    3131
     32#include "temporal/resampler.h"
     33
    3234#define MAX_CHANNELS 6
    3335#define MAX_SIZE 4096
     
    4446  uint_t scratch_size;
    4547  smpl_t *scratch_data;
     48
     49  smpl_t ratio;
     50
     51#ifdef HAVE_SAMPLERATE
     52  aubio_resampler_t *resampler;
     53  fvec_t *resampled_data;
     54#endif /* HAVE_SAMPLERATE */
    4655};
    4756
     
    7079  }     
    7180
    72   if (sfinfo.channels > MAX_CHANNELS) { 
     81  if (sfinfo.channels > MAX_CHANNELS) {
    7382    AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS);
    7483    return NULL;
     
    8089  s->input_format     = sfinfo.format;
    8190
    82   if (s->samplerate != s->input_samplerate) {
    83     AUBIO_ERR("resampling not implemented yet\n");
     91  s->ratio = s->samplerate/(float)s->input_samplerate;
     92
     93  if (s->ratio != 1) {
     94#ifdef HAVE_SAMPLERATE
     95    s->resampler = new_aubio_resampler(s->ratio, 0);
     96    s->resampled_data = new_fvec(s->hop_size / s->ratio);
     97    if (s-> ratio > 1) {
     98      AUBIO_WRN("upsampling from %d to % d\n", s->input_samplerate, s->samplerate);
     99    }
     100#else
     101    AUBIO_ERR("can not read %s at samplerate %dHz\n", s->path, s->samplerate);
     102    AUBIO_ERR("aubio was compiled without aubio_resampler\n");
    84103    return NULL;
     104#endif /* HAVE_SAMPLERATE */
    85105  }
    86  
    87   s->scratch_size = s->hop_size*s->input_channels;
     106
     107  s->scratch_size = s->hop_size * s->input_channels / s->ratio;
    88108  /* allocate data for de/interleaving reallocated when needed. */
    89109  if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
     
    104124  read_frames = sf_read_float (s->handle, s->scratch_data, s->scratch_size);
    105125
    106   aread = (int)FLOOR(read_frames/(float)input_channels);
     126  smpl_t *data;
     127
     128#ifdef HAVE_SAMPLERATE
     129  if (s->resampler) {
     130    data = s->resampled_data->data;
     131  } else
     132#endif /* HAVE_SAMPLERATE */
     133  {
     134    data = read_data->data;
     135  }
     136  aread = (int)FLOOR(s->ratio * read_frames / (float)input_channels + .5);
    107137
    108138  /* de-interleaving and down-mixing data  */
    109   for (j = 0; j < aread; j++) {
    110     read_data->data[j] = 0;
     139  for (j = 0; j < read_frames; j++) {
     140    data[j] = 0;
    111141    for (i = 0; i < input_channels; i++) {
    112       read_data->data[j] += (smpl_t)s->scratch_data[input_channels*j+i];
     142      data[j] += (smpl_t)s->scratch_data[input_channels*j+i];
    113143    }
    114     read_data->data[j] /= (smpl_t)input_channels;
     144    data[j] /= (smpl_t)input_channels;
    115145  }
     146
     147#ifdef HAVE_SAMPLERATE
     148  if (s->resampler) {
     149    aubio_resampler_do(s->resampler, s->resampled_data, read_data);
     150    data = s->resampled_data->data;
     151  }
     152#endif /* HAVE_SAMPLERATE */
     153
    116154  *read = aread;
    117155}
     
    122160    AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
    123161  }
     162#ifdef HAVE_SAMPLERATE
     163  if (s->resampler) {
     164    del_aubio_resampler(s->resampler);
     165  }
     166  if (s->resampled_data) {
     167    del_fvec(s->resampled_data);
     168  }
     169#endif /* HAVE_SAMPLERATE */
    124170  AUBIO_FREE(s->scratch_data);
    125171  AUBIO_FREE(s);
Note: See TracChangeset for help on using the changeset viewer.