[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 "source_sndfile.h" |
---|
| 30 | #include "fvec.h" |
---|
| 31 | |
---|
[87dfc3f] | 32 | #include "temporal/resampler.h" |
---|
| 33 | |
---|
[afbd7e7] | 34 | #define MAX_CHANNELS 6 |
---|
| 35 | #define MAX_SIZE 4096 |
---|
| 36 | |
---|
| 37 | struct _aubio_source_sndfile_t { |
---|
| 38 | uint_t hop_size; |
---|
| 39 | uint_t samplerate; |
---|
| 40 | uint_t channels; |
---|
| 41 | int input_samplerate; |
---|
| 42 | int input_channels; |
---|
| 43 | int input_format; |
---|
| 44 | char_t *path; |
---|
| 45 | SNDFILE *handle; |
---|
| 46 | uint_t scratch_size; |
---|
| 47 | smpl_t *scratch_data; |
---|
[87dfc3f] | 48 | |
---|
| 49 | smpl_t ratio; |
---|
| 50 | |
---|
| 51 | #ifdef HAVE_SAMPLERATE |
---|
| 52 | aubio_resampler_t *resampler; |
---|
| 53 | fvec_t *resampled_data; |
---|
| 54 | #endif /* HAVE_SAMPLERATE */ |
---|
[afbd7e7] | 55 | }; |
---|
| 56 | |
---|
| 57 | aubio_source_sndfile_t * new_aubio_source_sndfile(char_t * path, uint_t samplerate, uint_t hop_size) { |
---|
| 58 | aubio_source_sndfile_t * s = AUBIO_NEW(aubio_source_sndfile_t); |
---|
| 59 | |
---|
| 60 | if (path == NULL) { |
---|
| 61 | AUBIO_ERR("Aborted opening null path\n"); |
---|
| 62 | return NULL; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | s->hop_size = hop_size; |
---|
| 66 | s->samplerate = samplerate; |
---|
| 67 | s->channels = 1; |
---|
| 68 | s->path = path; |
---|
| 69 | |
---|
| 70 | // try opening the file, geting the info in sfinfo |
---|
| 71 | SF_INFO sfinfo; |
---|
| 72 | AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo)); |
---|
| 73 | s->handle = sf_open (s->path, SFM_READ, &sfinfo); |
---|
| 74 | |
---|
| 75 | if (s->handle == NULL) { |
---|
| 76 | /* show libsndfile err msg */ |
---|
| 77 | AUBIO_ERR("Failed opening %s: %s\n", s->path, sf_strerror (NULL)); |
---|
| 78 | return NULL; |
---|
| 79 | } |
---|
| 80 | |
---|
[87dfc3f] | 81 | if (sfinfo.channels > MAX_CHANNELS) { |
---|
[afbd7e7] | 82 | AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS); |
---|
| 83 | return NULL; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /* get input specs */ |
---|
| 87 | s->input_samplerate = sfinfo.samplerate; |
---|
| 88 | s->input_channels = sfinfo.channels; |
---|
| 89 | s->input_format = sfinfo.format; |
---|
| 90 | |
---|
[87dfc3f] | 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"); |
---|
[afbd7e7] | 103 | return NULL; |
---|
[87dfc3f] | 104 | #endif /* HAVE_SAMPLERATE */ |
---|
[afbd7e7] | 105 | } |
---|
[87dfc3f] | 106 | |
---|
| 107 | s->scratch_size = s->hop_size * s->input_channels / s->ratio; |
---|
[afbd7e7] | 108 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
| 109 | if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) { |
---|
[af86999] | 110 | AUBIO_ERR("%d x %d exceeds maximum aubio_source_sndfile buffer size %d\n", |
---|
| 111 | s->hop_size, s->input_channels, MAX_CHANNELS * MAX_CHANNELS); |
---|
[afbd7e7] | 112 | return NULL; |
---|
| 113 | } |
---|
[af86999] | 114 | s->scratch_data = AUBIO_ARRAY(float,s->scratch_size); |
---|
[afbd7e7] | 115 | |
---|
| 116 | return s; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){ |
---|
| 120 | sf_count_t read_frames; |
---|
| 121 | int i,j, input_channels = s->input_channels; |
---|
| 122 | int aread; |
---|
| 123 | /* do actual reading */ |
---|
| 124 | read_frames = sf_read_float (s->handle, s->scratch_data, s->scratch_size); |
---|
| 125 | |
---|
[87dfc3f] | 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); |
---|
[afbd7e7] | 137 | |
---|
| 138 | /* de-interleaving and down-mixing data */ |
---|
[87dfc3f] | 139 | for (j = 0; j < read_frames; j++) { |
---|
| 140 | data[j] = 0; |
---|
[afbd7e7] | 141 | for (i = 0; i < input_channels; i++) { |
---|
[87dfc3f] | 142 | data[j] += (smpl_t)s->scratch_data[input_channels*j+i]; |
---|
[afbd7e7] | 143 | } |
---|
[87dfc3f] | 144 | data[j] /= (smpl_t)input_channels; |
---|
| 145 | } |
---|
| 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; |
---|
[afbd7e7] | 151 | } |
---|
[87dfc3f] | 152 | #endif /* HAVE_SAMPLERATE */ |
---|
| 153 | |
---|
[afbd7e7] | 154 | *read = aread; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | void del_aubio_source_sndfile(aubio_source_sndfile_t * s){ |
---|
[420962e] | 158 | if (!s) return; |
---|
[afbd7e7] | 159 | if (sf_close(s->handle)) { |
---|
| 160 | AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL)); |
---|
| 161 | } |
---|
[87dfc3f] | 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 */ |
---|
[afbd7e7] | 170 | AUBIO_FREE(s->scratch_data); |
---|
| 171 | AUBIO_FREE(s); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | #endif /* HAVE_SNDFILE */ |
---|