[afbd7e7] | 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 "fvec.h" |
---|
[3d2fe26] | 30 | #include "fmat.h" |
---|
| 31 | #include "source_sndfile.h" |
---|
[afbd7e7] | 32 | |
---|
[87dfc3f] | 33 | #include "temporal/resampler.h" |
---|
| 34 | |
---|
[afbd7e7] | 35 | #define MAX_CHANNELS 6 |
---|
| 36 | #define MAX_SIZE 4096 |
---|
[18c6b20] | 37 | #define MAX_SAMPLES MAX_CHANNELS * MAX_SIZE |
---|
[afbd7e7] | 38 | |
---|
[5bd806d] | 39 | #if !HAVE_AUBIO_DOUBLE |
---|
| 40 | #define aubio_sf_read_smpl sf_read_float |
---|
| 41 | #else /* HAVE_AUBIO_DOUBLE */ |
---|
| 42 | #define aubio_sf_read_smpl sf_read_double |
---|
| 43 | #endif /* HAVE_AUBIO_DOUBLE */ |
---|
| 44 | |
---|
[afbd7e7] | 45 | struct _aubio_source_sndfile_t { |
---|
| 46 | uint_t hop_size; |
---|
| 47 | uint_t samplerate; |
---|
| 48 | uint_t channels; |
---|
[18c6b20] | 49 | |
---|
| 50 | // some data about the file |
---|
| 51 | char_t *path; |
---|
| 52 | SNDFILE *handle; |
---|
[afbd7e7] | 53 | int input_samplerate; |
---|
| 54 | int input_channels; |
---|
| 55 | int input_format; |
---|
[c6e7ba1] | 56 | int duration; |
---|
[87dfc3f] | 57 | |
---|
[18c6b20] | 58 | // resampling stuff |
---|
[87dfc3f] | 59 | smpl_t ratio; |
---|
[18c6b20] | 60 | uint_t input_hop_size; |
---|
[87dfc3f] | 61 | #ifdef HAVE_SAMPLERATE |
---|
| 62 | aubio_resampler_t *resampler; |
---|
[18c6b20] | 63 | fvec_t *input_data; |
---|
[87dfc3f] | 64 | #endif /* HAVE_SAMPLERATE */ |
---|
[18c6b20] | 65 | |
---|
| 66 | // some temporary memory for sndfile to write at |
---|
| 67 | uint_t scratch_size; |
---|
[5bd806d] | 68 | smpl_t *scratch_data; |
---|
[afbd7e7] | 69 | }; |
---|
| 70 | |
---|
[ae5d58a] | 71 | aubio_source_sndfile_t * new_aubio_source_sndfile(const char_t * path, uint_t samplerate, uint_t hop_size) { |
---|
[afbd7e7] | 72 | aubio_source_sndfile_t * s = AUBIO_NEW(aubio_source_sndfile_t); |
---|
[67b05b4] | 73 | SF_INFO sfinfo; |
---|
[afbd7e7] | 74 | |
---|
| 75 | if (path == NULL) { |
---|
[491e6ea] | 76 | AUBIO_ERR("source_sndfile: Aborted opening null path\n"); |
---|
[447c673] | 77 | goto beach; |
---|
| 78 | } |
---|
| 79 | if ((sint_t)samplerate < 0) { |
---|
[491e6ea] | 80 | AUBIO_ERR("source_sndfile: Can not open %s with samplerate %d\n", path, samplerate); |
---|
[447c673] | 81 | goto beach; |
---|
| 82 | } |
---|
| 83 | if ((sint_t)hop_size <= 0) { |
---|
[491e6ea] | 84 | AUBIO_ERR("source_sndfile: Can not open %s with hop_size %d\n", path, hop_size); |
---|
[447c673] | 85 | goto beach; |
---|
[afbd7e7] | 86 | } |
---|
| 87 | |
---|
| 88 | s->hop_size = hop_size; |
---|
| 89 | s->channels = 1; |
---|
[b643a33] | 90 | |
---|
| 91 | if (s->path) AUBIO_FREE(s->path); |
---|
[d2be104] | 92 | s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1); |
---|
| 93 | strncpy(s->path, path, strnlen(path, PATH_MAX) + 1); |
---|
[afbd7e7] | 94 | |
---|
[3d1ca81] | 95 | // try opening the file, getting the info in sfinfo |
---|
[afbd7e7] | 96 | AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo)); |
---|
| 97 | s->handle = sf_open (s->path, SFM_READ, &sfinfo); |
---|
| 98 | |
---|
| 99 | if (s->handle == NULL) { |
---|
| 100 | /* show libsndfile err msg */ |
---|
[17d0f0e] | 101 | AUBIO_ERR("source_sndfile: Failed opening %s (%s)\n", s->path, |
---|
| 102 | sf_strerror (NULL)); |
---|
[18c6b20] | 103 | goto beach; |
---|
[a65d37a] | 104 | } |
---|
[afbd7e7] | 105 | |
---|
| 106 | /* get input specs */ |
---|
| 107 | s->input_samplerate = sfinfo.samplerate; |
---|
| 108 | s->input_channels = sfinfo.channels; |
---|
| 109 | s->input_format = sfinfo.format; |
---|
[c6e7ba1] | 110 | s->duration = sfinfo.frames; |
---|
[afbd7e7] | 111 | |
---|
[c8f411b] | 112 | if (samplerate == 0) { |
---|
[26a6af7] | 113 | s->samplerate = s->input_samplerate; |
---|
[4db8eab] | 114 | //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate); |
---|
[26a6af7] | 115 | } else { |
---|
| 116 | s->samplerate = samplerate; |
---|
[c8f411b] | 117 | } |
---|
[18c6b20] | 118 | /* compute input block size required before resampling */ |
---|
[5bd806d] | 119 | s->ratio = s->samplerate/(smpl_t)s->input_samplerate; |
---|
[18c6b20] | 120 | s->input_hop_size = (uint_t)FLOOR(s->hop_size / s->ratio + .5); |
---|
| 121 | |
---|
| 122 | if (s->input_hop_size * s->input_channels > MAX_SAMPLES) { |
---|
[491e6ea] | 123 | AUBIO_ERR("source_sndfile: Not able to process more than %d frames of %d channels\n", |
---|
[18c6b20] | 124 | MAX_SAMPLES / s->input_channels, s->input_channels); |
---|
| 125 | goto beach; |
---|
| 126 | } |
---|
[87dfc3f] | 127 | |
---|
| 128 | #ifdef HAVE_SAMPLERATE |
---|
[18c6b20] | 129 | s->resampler = NULL; |
---|
| 130 | s->input_data = NULL; |
---|
| 131 | if (s->ratio != 1) { |
---|
| 132 | s->input_data = new_fvec(s->input_hop_size); |
---|
[6fd8d7e] | 133 | s->resampler = new_aubio_resampler(s->ratio, 4); |
---|
[d22cc42] | 134 | if (s->ratio > 1) { |
---|
| 135 | // we would need to add a ring buffer for these |
---|
| 136 | if ( (uint_t)(s->input_hop_size * s->ratio + .5) != s->hop_size ) { |
---|
[491e6ea] | 137 | AUBIO_ERR("source_sndfile: can not upsample %s from %d to %d\n", s->path, |
---|
[018e511] | 138 | s->input_samplerate, s->samplerate); |
---|
[d22cc42] | 139 | goto beach; |
---|
| 140 | } |
---|
[491e6ea] | 141 | AUBIO_WRN("source_sndfile: upsampling %s from %d to %d\n", s->path, |
---|
[018e511] | 142 | s->input_samplerate, s->samplerate); |
---|
[87dfc3f] | 143 | } |
---|
[985d5c4] | 144 | s->duration = (uint_t)FLOOR(s->duration * s->ratio); |
---|
[18c6b20] | 145 | } |
---|
[87dfc3f] | 146 | #else |
---|
[18c6b20] | 147 | if (s->ratio != 1) { |
---|
[491e6ea] | 148 | AUBIO_ERR("source_sndfile: aubio was compiled without aubio_resampler\n"); |
---|
[18c6b20] | 149 | goto beach; |
---|
[afbd7e7] | 150 | } |
---|
[18c6b20] | 151 | #endif /* HAVE_SAMPLERATE */ |
---|
[87dfc3f] | 152 | |
---|
[afbd7e7] | 153 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
[18c6b20] | 154 | s->scratch_size = s->input_hop_size * s->input_channels; |
---|
[5bd806d] | 155 | s->scratch_data = AUBIO_ARRAY(smpl_t, s->scratch_size); |
---|
[afbd7e7] | 156 | |
---|
| 157 | return s; |
---|
[18c6b20] | 158 | |
---|
| 159 | beach: |
---|
[447c673] | 160 | //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n", |
---|
| 161 | // s->path, s->samplerate, s->hop_size); |
---|
[18c6b20] | 162 | del_aubio_source_sndfile(s); |
---|
| 163 | return NULL; |
---|
[afbd7e7] | 164 | } |
---|
| 165 | |
---|
| 166 | void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){ |
---|
[18c6b20] | 167 | uint_t i,j, input_channels = s->input_channels; |
---|
[2722dc7] | 168 | /* read from file into scratch_data */ |
---|
[5bd806d] | 169 | sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size); |
---|
[afbd7e7] | 170 | |
---|
[2722dc7] | 171 | /* where to store de-interleaved data */ |
---|
| 172 | smpl_t *ptr_data; |
---|
[87dfc3f] | 173 | #ifdef HAVE_SAMPLERATE |
---|
[18c6b20] | 174 | if (s->ratio != 1) { |
---|
[2722dc7] | 175 | ptr_data = s->input_data->data; |
---|
[87dfc3f] | 176 | } else |
---|
| 177 | #endif /* HAVE_SAMPLERATE */ |
---|
| 178 | { |
---|
[2722dc7] | 179 | ptr_data = read_data->data; |
---|
[87dfc3f] | 180 | } |
---|
[afbd7e7] | 181 | |
---|
| 182 | /* de-interleaving and down-mixing data */ |
---|
[18c6b20] | 183 | for (j = 0; j < read_samples / input_channels; j++) { |
---|
[2722dc7] | 184 | ptr_data[j] = 0; |
---|
[afbd7e7] | 185 | for (i = 0; i < input_channels; i++) { |
---|
[2722dc7] | 186 | ptr_data[j] += s->scratch_data[input_channels*j+i]; |
---|
[afbd7e7] | 187 | } |
---|
[2722dc7] | 188 | ptr_data[j] /= (smpl_t)input_channels; |
---|
[87dfc3f] | 189 | } |
---|
| 190 | |
---|
| 191 | #ifdef HAVE_SAMPLERATE |
---|
| 192 | if (s->resampler) { |
---|
[18c6b20] | 193 | aubio_resampler_do(s->resampler, s->input_data, read_data); |
---|
[afbd7e7] | 194 | } |
---|
[87dfc3f] | 195 | #endif /* HAVE_SAMPLERATE */ |
---|
| 196 | |
---|
[18c6b20] | 197 | *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5); |
---|
[18a378e] | 198 | |
---|
| 199 | if (*read < s->hop_size) { |
---|
| 200 | for (j = *read; j < s->hop_size; j++) { |
---|
[45f48576] | 201 | read_data->data[j] = 0; |
---|
[18a378e] | 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
[afbd7e7] | 205 | } |
---|
| 206 | |
---|
[4865e4b] | 207 | void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_data, uint_t * read){ |
---|
| 208 | uint_t i,j, input_channels = s->input_channels; |
---|
| 209 | /* do actual reading */ |
---|
[5bd806d] | 210 | sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size); |
---|
[4865e4b] | 211 | |
---|
[2722dc7] | 212 | /* where to store de-interleaved data */ |
---|
| 213 | smpl_t **ptr_data; |
---|
[4865e4b] | 214 | #ifdef HAVE_SAMPLERATE |
---|
| 215 | if (s->ratio != 1) { |
---|
[f33ab63] | 216 | AUBIO_ERR("source_sndfile: no multi channel resampling yet\n"); |
---|
[4865e4b] | 217 | return; |
---|
[2722dc7] | 218 | //ptr_data = s->input_data->data; |
---|
[4865e4b] | 219 | } else |
---|
| 220 | #endif /* HAVE_SAMPLERATE */ |
---|
| 221 | { |
---|
[2722dc7] | 222 | ptr_data = read_data->data; |
---|
[4865e4b] | 223 | } |
---|
| 224 | |
---|
[50e10a9] | 225 | if (read_data->height < input_channels) { |
---|
| 226 | // destination matrix has less channels than the file; copy only first |
---|
| 227 | // channels of the file, de-interleaving data |
---|
| 228 | for (j = 0; j < read_samples / input_channels; j++) { |
---|
| 229 | for (i = 0; i < read_data->height; i++) { |
---|
[5bd806d] | 230 | ptr_data[i][j] = s->scratch_data[j * input_channels + i]; |
---|
[50e10a9] | 231 | } |
---|
| 232 | } |
---|
| 233 | } else { |
---|
| 234 | // destination matrix has as many or more channels than the file; copy each |
---|
| 235 | // channel from the file to the destination matrix, de-interleaving data |
---|
| 236 | for (j = 0; j < read_samples / input_channels; j++) { |
---|
| 237 | for (i = 0; i < input_channels; i++) { |
---|
[5bd806d] | 238 | ptr_data[i][j] = s->scratch_data[j * input_channels + i]; |
---|
[50e10a9] | 239 | } |
---|
[4865e4b] | 240 | } |
---|
| 241 | } |
---|
[50e10a9] | 242 | |
---|
[6510866] | 243 | if (read_data->height > input_channels) { |
---|
[50e10a9] | 244 | // destination matrix has more channels than the file; copy last channel |
---|
| 245 | // of the file to each additional channels, de-interleaving data |
---|
[6510866] | 246 | for (j = 0; j < read_samples / input_channels; j++) { |
---|
[58c24e1] | 247 | for (i = input_channels; i < read_data->height; i++) { |
---|
[5bd806d] | 248 | ptr_data[i][j] = s->scratch_data[j * input_channels + (input_channels - 1)]; |
---|
[6510866] | 249 | } |
---|
| 250 | } |
---|
| 251 | } |
---|
[4865e4b] | 252 | |
---|
| 253 | #ifdef HAVE_SAMPLERATE |
---|
| 254 | if (s->resampler) { |
---|
[3d2fe26] | 255 | //aubio_resampler_do(s->resampler, s->input_data, read_data); |
---|
[4865e4b] | 256 | } |
---|
| 257 | #endif /* HAVE_SAMPLERATE */ |
---|
| 258 | |
---|
| 259 | *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5); |
---|
[18a378e] | 260 | |
---|
| 261 | if (*read < s->hop_size) { |
---|
[69440b8] | 262 | for (i = 0; i < read_data->height; i++) { |
---|
[18a378e] | 263 | for (j = *read; j < s->hop_size; j++) { |
---|
[45f48576] | 264 | read_data->data[i][j] = 0.; |
---|
[18a378e] | 265 | } |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
[4865e4b] | 269 | } |
---|
| 270 | |
---|
[063fa5a] | 271 | uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) { |
---|
| 272 | return s->samplerate; |
---|
| 273 | } |
---|
| 274 | |
---|
[4865e4b] | 275 | uint_t aubio_source_sndfile_get_channels(aubio_source_sndfile_t * s) { |
---|
| 276 | return s->input_channels; |
---|
| 277 | } |
---|
| 278 | |
---|
[c6e7ba1] | 279 | uint_t aubio_source_sndfile_get_duration (const aubio_source_sndfile_t * s) { |
---|
| 280 | if (s && s->duration) { |
---|
| 281 | return s->duration; |
---|
| 282 | } |
---|
| 283 | return 0; |
---|
| 284 | } |
---|
| 285 | |
---|
[7982203] | 286 | uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) { |
---|
[7bb5cef] | 287 | uint_t resampled_pos = (uint_t)ROUND(pos / s->ratio); |
---|
[f33ab63] | 288 | sf_count_t sf_ret = sf_seek (s->handle, resampled_pos, SEEK_SET); |
---|
| 289 | if (sf_ret == -1) { |
---|
| 290 | AUBIO_ERR("source_sndfile: Failed seeking %s at %d: %s\n", s->path, pos, sf_strerror (NULL)); |
---|
| 291 | return AUBIO_FAIL; |
---|
| 292 | } |
---|
| 293 | if (sf_ret != resampled_pos) { |
---|
[74dcddb] | 294 | AUBIO_ERR("source_sndfile: Tried seeking %s at %d, but got %d: %s\n", |
---|
| 295 | s->path, resampled_pos, (uint_t)sf_ret, sf_strerror (NULL)); |
---|
[f33ab63] | 296 | return AUBIO_FAIL; |
---|
| 297 | } |
---|
| 298 | return AUBIO_OK; |
---|
[7982203] | 299 | } |
---|
| 300 | |
---|
[422452b] | 301 | uint_t aubio_source_sndfile_close (aubio_source_sndfile_t *s) { |
---|
| 302 | if (!s->handle) { |
---|
| 303 | return AUBIO_FAIL; |
---|
[c038740] | 304 | } |
---|
[422452b] | 305 | if(sf_close(s->handle)) { |
---|
[f33ab63] | 306 | AUBIO_ERR("source_sndfile: Error closing file %s: %s\n", s->path, sf_strerror (NULL)); |
---|
[422452b] | 307 | return AUBIO_FAIL; |
---|
[afbd7e7] | 308 | } |
---|
[422452b] | 309 | return AUBIO_OK; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | void del_aubio_source_sndfile(aubio_source_sndfile_t * s){ |
---|
| 313 | if (!s) return; |
---|
[c038740] | 314 | aubio_source_sndfile_close(s); |
---|
[87dfc3f] | 315 | #ifdef HAVE_SAMPLERATE |
---|
[18c6b20] | 316 | if (s->resampler != NULL) { |
---|
[87dfc3f] | 317 | del_aubio_resampler(s->resampler); |
---|
| 318 | } |
---|
[18c6b20] | 319 | if (s->input_data) { |
---|
| 320 | del_fvec(s->input_data); |
---|
[87dfc3f] | 321 | } |
---|
| 322 | #endif /* HAVE_SAMPLERATE */ |
---|
[b643a33] | 323 | if (s->path) AUBIO_FREE(s->path); |
---|
[afbd7e7] | 324 | AUBIO_FREE(s->scratch_data); |
---|
| 325 | AUBIO_FREE(s); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | #endif /* HAVE_SNDFILE */ |
---|