source: src/io/source_sndfile.c @ 18c6b20

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since 18c6b20 was 18c6b20, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/io/source_sndfile.c: improve resampling, clarify

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21
22#include "config.h"
23
24#ifdef HAVE_SNDFILE
25
26#include <sndfile.h>
27
28#include "aubio_priv.h"
29#include "source_sndfile.h"
30#include "fvec.h"
31
32#include "temporal/resampler.h"
33
34#define MAX_CHANNELS 6
35#define MAX_SIZE 4096
36#define MAX_SAMPLES MAX_CHANNELS * MAX_SIZE
37
38struct _aubio_source_sndfile_t {
39  uint_t hop_size;
40  uint_t samplerate;
41  uint_t channels;
42
43  // some data about the file
44  char_t *path;
45  SNDFILE *handle;
46  int input_samplerate;
47  int input_channels;
48  int input_format;
49
50  // resampling stuff
51  smpl_t ratio;
52  uint_t input_hop_size;
53#ifdef HAVE_SAMPLERATE
54  aubio_resampler_t *resampler;
55  fvec_t *input_data;
56#endif /* HAVE_SAMPLERATE */
57
58  // some temporary memory for sndfile to write at
59  uint_t scratch_size;
60  smpl_t *scratch_data;
61};
62
63aubio_source_sndfile_t * new_aubio_source_sndfile(char_t * path, uint_t samplerate, uint_t hop_size) {
64  aubio_source_sndfile_t * s = AUBIO_NEW(aubio_source_sndfile_t);
65
66  if (path == NULL) {
67    AUBIO_ERR("Aborted opening null path\n");
68    return NULL;
69  }
70
71  s->hop_size = hop_size;
72  s->samplerate = samplerate;
73  s->channels = 1;
74  s->path = path;
75
76  // try opening the file, geting the info in sfinfo
77  SF_INFO sfinfo;
78  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
79  s->handle = sf_open (s->path, SFM_READ, &sfinfo);
80
81  if (s->handle == NULL) {
82    /* show libsndfile err msg */
83    AUBIO_ERR("Failed opening %s: %s\n", s->path, sf_strerror (NULL));
84    goto beach;
85  }     
86
87  /* get input specs */
88  s->input_samplerate = sfinfo.samplerate;
89  s->input_channels   = sfinfo.channels;
90  s->input_format     = sfinfo.format;
91
92  /* compute input block size required before resampling */
93  s->ratio = s->samplerate/(float)s->input_samplerate;
94  s->input_hop_size = (uint_t)FLOOR(s->hop_size / s->ratio + .5);
95
96  if (s->input_hop_size * s->input_channels > MAX_SAMPLES) {
97    AUBIO_ERR("Not able to process more than %d frames of %d channels\n",
98        MAX_SAMPLES / s->input_channels, s->input_channels);
99    goto beach;
100  }
101
102#ifdef HAVE_SAMPLERATE
103  s->resampler = NULL;
104  s->input_data = NULL;
105  if (s->ratio != 1) {
106    s->input_data = new_fvec(s->input_hop_size);
107    s->resampler = new_aubio_resampler(s->ratio, 0);
108    if (s-> ratio > 1) {
109      AUBIO_WRN("upsampling %s from %d to % d\n", s->path, s->input_samplerate, s->samplerate);
110    }
111  }
112#else
113  if (s->ratio != 1) {
114    AUBIO_ERR("aubio was compiled without aubio_resampler\n");
115    goto beach;
116  }
117#endif /* HAVE_SAMPLERATE */
118
119  /* allocate data for de/interleaving reallocated when needed. */
120  s->scratch_size = s->input_hop_size * s->input_channels;
121  s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
122
123  return s;
124
125beach:
126  AUBIO_ERR("can not read %s at samplerate %dHz with hop_size of %d\n",
127      s->path, s->samplerate, s->hop_size);
128  del_aubio_source_sndfile(s);
129  return NULL;
130}
131
132void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
133  uint_t i,j, input_channels = s->input_channels;
134  /* do actual reading */
135  sf_count_t read_samples = sf_read_float (s->handle, s->scratch_data, s->scratch_size);
136
137  smpl_t *data;
138
139#ifdef HAVE_SAMPLERATE
140  if (s->ratio != 1) {
141    data = s->input_data->data;
142  } else
143#endif /* HAVE_SAMPLERATE */
144  {
145    data = read_data->data;
146  }
147
148  /* de-interleaving and down-mixing data  */
149  for (j = 0; j < read_samples / input_channels; j++) {
150    data[j] = 0;
151    for (i = 0; i < input_channels; i++) {
152      data[j] += (smpl_t)s->scratch_data[input_channels*j+i];
153    }
154    data[j] /= (smpl_t)input_channels;
155  }
156
157#ifdef HAVE_SAMPLERATE
158  if (s->resampler) {
159    aubio_resampler_do(s->resampler, s->input_data, read_data);
160  }
161#endif /* HAVE_SAMPLERATE */
162
163  *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
164}
165
166void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
167  if (!s) return;
168  if (sf_close(s->handle)) {
169    AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
170  }
171#ifdef HAVE_SAMPLERATE
172  if (s->resampler != NULL) {
173    del_aubio_resampler(s->resampler);
174  }
175  if (s->input_data) {
176    del_fvec(s->input_data);
177  }
178#endif /* HAVE_SAMPLERATE */
179  AUBIO_FREE(s->scratch_data);
180  AUBIO_FREE(s);
181}
182
183#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.