[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 | |
---|
| 39 | struct _aubio_source_sndfile_t { |
---|
| 40 | uint_t hop_size; |
---|
| 41 | uint_t samplerate; |
---|
| 42 | uint_t channels; |
---|
[18c6b20] | 43 | |
---|
| 44 | // some data about the file |
---|
| 45 | char_t *path; |
---|
| 46 | SNDFILE *handle; |
---|
[afbd7e7] | 47 | int input_samplerate; |
---|
| 48 | int input_channels; |
---|
| 49 | int input_format; |
---|
[87dfc3f] | 50 | |
---|
[18c6b20] | 51 | // resampling stuff |
---|
[87dfc3f] | 52 | smpl_t ratio; |
---|
[18c6b20] | 53 | uint_t input_hop_size; |
---|
[87dfc3f] | 54 | #ifdef HAVE_SAMPLERATE |
---|
| 55 | aubio_resampler_t *resampler; |
---|
[18c6b20] | 56 | fvec_t *input_data; |
---|
[87dfc3f] | 57 | #endif /* HAVE_SAMPLERATE */ |
---|
[18c6b20] | 58 | |
---|
| 59 | // some temporary memory for sndfile to write at |
---|
| 60 | uint_t scratch_size; |
---|
| 61 | smpl_t *scratch_data; |
---|
[afbd7e7] | 62 | }; |
---|
| 63 | |
---|
| 64 | aubio_source_sndfile_t * new_aubio_source_sndfile(char_t * path, uint_t samplerate, uint_t hop_size) { |
---|
| 65 | aubio_source_sndfile_t * s = AUBIO_NEW(aubio_source_sndfile_t); |
---|
| 66 | |
---|
| 67 | if (path == NULL) { |
---|
| 68 | AUBIO_ERR("Aborted opening null path\n"); |
---|
| 69 | return NULL; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | s->hop_size = hop_size; |
---|
| 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)); |
---|
[18c6b20] | 84 | goto beach; |
---|
[afbd7e7] | 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 | |
---|
[c8f411b] | 92 | if (samplerate == 0) { |
---|
| 93 | samplerate = s->input_samplerate; |
---|
[4db8eab] | 94 | //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate); |
---|
[c8f411b] | 95 | } |
---|
| 96 | s->samplerate = samplerate; |
---|
[18c6b20] | 97 | /* compute input block size required before resampling */ |
---|
[87dfc3f] | 98 | s->ratio = s->samplerate/(float)s->input_samplerate; |
---|
[18c6b20] | 99 | s->input_hop_size = (uint_t)FLOOR(s->hop_size / s->ratio + .5); |
---|
| 100 | |
---|
| 101 | if (s->input_hop_size * s->input_channels > MAX_SAMPLES) { |
---|
| 102 | AUBIO_ERR("Not able to process more than %d frames of %d channels\n", |
---|
| 103 | MAX_SAMPLES / s->input_channels, s->input_channels); |
---|
| 104 | goto beach; |
---|
| 105 | } |
---|
[87dfc3f] | 106 | |
---|
| 107 | #ifdef HAVE_SAMPLERATE |
---|
[18c6b20] | 108 | s->resampler = NULL; |
---|
| 109 | s->input_data = NULL; |
---|
| 110 | if (s->ratio != 1) { |
---|
| 111 | s->input_data = new_fvec(s->input_hop_size); |
---|
[6fd8d7e] | 112 | s->resampler = new_aubio_resampler(s->ratio, 4); |
---|
[d22cc42] | 113 | if (s->ratio > 1) { |
---|
| 114 | // we would need to add a ring buffer for these |
---|
| 115 | if ( (uint_t)(s->input_hop_size * s->ratio + .5) != s->hop_size ) { |
---|
| 116 | AUBIO_ERR("can not upsample from %d to %d\n", s->input_samplerate, s->samplerate); |
---|
| 117 | goto beach; |
---|
| 118 | } |
---|
[18c6b20] | 119 | AUBIO_WRN("upsampling %s from %d to % d\n", s->path, s->input_samplerate, s->samplerate); |
---|
[87dfc3f] | 120 | } |
---|
[18c6b20] | 121 | } |
---|
[87dfc3f] | 122 | #else |
---|
[18c6b20] | 123 | if (s->ratio != 1) { |
---|
[87dfc3f] | 124 | AUBIO_ERR("aubio was compiled without aubio_resampler\n"); |
---|
[18c6b20] | 125 | goto beach; |
---|
[afbd7e7] | 126 | } |
---|
[18c6b20] | 127 | #endif /* HAVE_SAMPLERATE */ |
---|
[87dfc3f] | 128 | |
---|
[afbd7e7] | 129 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
[18c6b20] | 130 | s->scratch_size = s->input_hop_size * s->input_channels; |
---|
[af86999] | 131 | s->scratch_data = AUBIO_ARRAY(float,s->scratch_size); |
---|
[afbd7e7] | 132 | |
---|
| 133 | return s; |
---|
[18c6b20] | 134 | |
---|
| 135 | beach: |
---|
[d22cc42] | 136 | AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n", |
---|
[18c6b20] | 137 | s->path, s->samplerate, s->hop_size); |
---|
| 138 | del_aubio_source_sndfile(s); |
---|
| 139 | return NULL; |
---|
[afbd7e7] | 140 | } |
---|
| 141 | |
---|
| 142 | void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){ |
---|
[18c6b20] | 143 | uint_t i,j, input_channels = s->input_channels; |
---|
[afbd7e7] | 144 | /* do actual reading */ |
---|
[18c6b20] | 145 | sf_count_t read_samples = sf_read_float (s->handle, s->scratch_data, s->scratch_size); |
---|
[afbd7e7] | 146 | |
---|
[87dfc3f] | 147 | smpl_t *data; |
---|
| 148 | |
---|
| 149 | #ifdef HAVE_SAMPLERATE |
---|
[18c6b20] | 150 | if (s->ratio != 1) { |
---|
| 151 | data = s->input_data->data; |
---|
[87dfc3f] | 152 | } else |
---|
| 153 | #endif /* HAVE_SAMPLERATE */ |
---|
| 154 | { |
---|
| 155 | data = read_data->data; |
---|
| 156 | } |
---|
[afbd7e7] | 157 | |
---|
| 158 | /* de-interleaving and down-mixing data */ |
---|
[18c6b20] | 159 | for (j = 0; j < read_samples / input_channels; j++) { |
---|
[87dfc3f] | 160 | data[j] = 0; |
---|
[afbd7e7] | 161 | for (i = 0; i < input_channels; i++) { |
---|
[4865e4b] | 162 | data[j] += s->scratch_data[input_channels*j+i]; |
---|
[afbd7e7] | 163 | } |
---|
[87dfc3f] | 164 | data[j] /= (smpl_t)input_channels; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | #ifdef HAVE_SAMPLERATE |
---|
| 168 | if (s->resampler) { |
---|
[18c6b20] | 169 | aubio_resampler_do(s->resampler, s->input_data, read_data); |
---|
[afbd7e7] | 170 | } |
---|
[87dfc3f] | 171 | #endif /* HAVE_SAMPLERATE */ |
---|
| 172 | |
---|
[18c6b20] | 173 | *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5); |
---|
[18a378e] | 174 | |
---|
| 175 | if (*read < s->hop_size) { |
---|
| 176 | for (j = *read; j < s->hop_size; j++) { |
---|
| 177 | data[j] = 0; |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
[afbd7e7] | 181 | } |
---|
| 182 | |
---|
[4865e4b] | 183 | void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_data, uint_t * read){ |
---|
| 184 | uint_t i,j, input_channels = s->input_channels; |
---|
| 185 | /* do actual reading */ |
---|
| 186 | sf_count_t read_samples = sf_read_float (s->handle, s->scratch_data, s->scratch_size); |
---|
| 187 | |
---|
| 188 | smpl_t **data; |
---|
| 189 | |
---|
| 190 | #ifdef HAVE_SAMPLERATE |
---|
| 191 | if (s->ratio != 1) { |
---|
| 192 | AUBIO_ERR("source_sndfile: no multi channel resampling yet"); |
---|
| 193 | return; |
---|
[3d2fe26] | 194 | //data = s->input_data->data; |
---|
[4865e4b] | 195 | } else |
---|
| 196 | #endif /* HAVE_SAMPLERATE */ |
---|
| 197 | { |
---|
| 198 | data = read_data->data; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /* de-interleaving data */ |
---|
| 202 | for (j = 0; j < read_samples / input_channels; j++) { |
---|
| 203 | for (i = 0; i < input_channels; i++) { |
---|
| 204 | data[i][j] = (smpl_t)s->scratch_data[input_channels*j+i]; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | #ifdef HAVE_SAMPLERATE |
---|
| 209 | if (s->resampler) { |
---|
[3d2fe26] | 210 | //aubio_resampler_do(s->resampler, s->input_data, read_data); |
---|
[4865e4b] | 211 | } |
---|
| 212 | #endif /* HAVE_SAMPLERATE */ |
---|
| 213 | |
---|
| 214 | *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5); |
---|
[18a378e] | 215 | |
---|
| 216 | if (*read < s->hop_size) { |
---|
| 217 | for (i = 0; i < input_channels; i++) { |
---|
| 218 | for (j = *read; j < s->hop_size; j++) { |
---|
| 219 | data[i][j] = 0.; |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | |
---|
[4865e4b] | 224 | } |
---|
| 225 | |
---|
[063fa5a] | 226 | uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) { |
---|
| 227 | return s->samplerate; |
---|
| 228 | } |
---|
| 229 | |
---|
[4865e4b] | 230 | uint_t aubio_source_sndfile_get_channels(aubio_source_sndfile_t * s) { |
---|
| 231 | return s->input_channels; |
---|
| 232 | } |
---|
| 233 | |
---|
[7982203] | 234 | uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) { |
---|
| 235 | uint_t resampled_pos = (uint_t)ROUND(pos * s->input_samplerate * 1. / s->samplerate); |
---|
| 236 | return sf_seek (s->handle, resampled_pos, SEEK_SET); |
---|
| 237 | } |
---|
| 238 | |
---|
[afbd7e7] | 239 | void del_aubio_source_sndfile(aubio_source_sndfile_t * s){ |
---|
[420962e] | 240 | if (!s) return; |
---|
[afbd7e7] | 241 | if (sf_close(s->handle)) { |
---|
| 242 | AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL)); |
---|
| 243 | } |
---|
[87dfc3f] | 244 | #ifdef HAVE_SAMPLERATE |
---|
[18c6b20] | 245 | if (s->resampler != NULL) { |
---|
[87dfc3f] | 246 | del_aubio_resampler(s->resampler); |
---|
| 247 | } |
---|
[18c6b20] | 248 | if (s->input_data) { |
---|
| 249 | del_fvec(s->input_data); |
---|
[87dfc3f] | 250 | } |
---|
| 251 | #endif /* HAVE_SAMPLERATE */ |
---|
[afbd7e7] | 252 | AUBIO_FREE(s->scratch_data); |
---|
| 253 | AUBIO_FREE(s); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | #endif /* HAVE_SNDFILE */ |
---|