[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 | |
---|
| 32 | #define MAX_CHANNELS 6 |
---|
| 33 | #define MAX_SIZE 4096 |
---|
| 34 | |
---|
| 35 | struct _aubio_source_sndfile_t { |
---|
| 36 | uint_t hop_size; |
---|
| 37 | uint_t samplerate; |
---|
| 38 | uint_t channels; |
---|
| 39 | int input_samplerate; |
---|
| 40 | int input_channels; |
---|
| 41 | int input_format; |
---|
| 42 | char_t *path; |
---|
| 43 | SNDFILE *handle; |
---|
| 44 | uint_t scratch_size; |
---|
| 45 | smpl_t *scratch_data; |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | aubio_source_sndfile_t * new_aubio_source_sndfile(char_t * path, uint_t samplerate, uint_t hop_size) { |
---|
| 49 | aubio_source_sndfile_t * s = AUBIO_NEW(aubio_source_sndfile_t); |
---|
| 50 | |
---|
| 51 | if (path == NULL) { |
---|
| 52 | AUBIO_ERR("Aborted opening null path\n"); |
---|
| 53 | return NULL; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | s->hop_size = hop_size; |
---|
| 57 | s->samplerate = samplerate; |
---|
| 58 | s->channels = 1; |
---|
| 59 | s->path = path; |
---|
| 60 | |
---|
| 61 | // try opening the file, geting the info in sfinfo |
---|
| 62 | SF_INFO sfinfo; |
---|
| 63 | AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo)); |
---|
| 64 | s->handle = sf_open (s->path, SFM_READ, &sfinfo); |
---|
| 65 | |
---|
| 66 | if (s->handle == NULL) { |
---|
| 67 | /* show libsndfile err msg */ |
---|
| 68 | AUBIO_ERR("Failed opening %s: %s\n", s->path, sf_strerror (NULL)); |
---|
| 69 | return NULL; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | if (sfinfo.channels > MAX_CHANNELS) { |
---|
| 73 | AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS); |
---|
| 74 | return NULL; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /* get input specs */ |
---|
| 78 | s->input_samplerate = sfinfo.samplerate; |
---|
| 79 | s->input_channels = sfinfo.channels; |
---|
| 80 | s->input_format = sfinfo.format; |
---|
| 81 | |
---|
| 82 | if (s->samplerate != s->input_samplerate) { |
---|
| 83 | AUBIO_ERR("resampling not implemented yet\n"); |
---|
| 84 | return NULL; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | s->scratch_size = s->hop_size*s->input_channels; |
---|
| 88 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
| 89 | if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) { |
---|
[af86999] | 90 | AUBIO_ERR("%d x %d exceeds maximum aubio_source_sndfile buffer size %d\n", |
---|
| 91 | s->hop_size, s->input_channels, MAX_CHANNELS * MAX_CHANNELS); |
---|
[afbd7e7] | 92 | return NULL; |
---|
| 93 | } |
---|
[af86999] | 94 | s->scratch_data = AUBIO_ARRAY(float,s->scratch_size); |
---|
[afbd7e7] | 95 | |
---|
| 96 | return s; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){ |
---|
| 100 | sf_count_t read_frames; |
---|
| 101 | int i,j, input_channels = s->input_channels; |
---|
| 102 | int aread; |
---|
| 103 | /* do actual reading */ |
---|
| 104 | read_frames = sf_read_float (s->handle, s->scratch_data, s->scratch_size); |
---|
| 105 | |
---|
| 106 | aread = (int)FLOOR(read_frames/(float)input_channels); |
---|
| 107 | |
---|
| 108 | /* de-interleaving and down-mixing data */ |
---|
| 109 | for (j = 0; j < aread; j++) { |
---|
| 110 | read_data->data[j] = 0; |
---|
| 111 | for (i = 0; i < input_channels; i++) { |
---|
| 112 | read_data->data[j] += (smpl_t)s->scratch_data[input_channels*j+i]; |
---|
| 113 | } |
---|
| 114 | read_data->data[j] /= (smpl_t)input_channels; |
---|
| 115 | } |
---|
| 116 | *read = aread; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | void del_aubio_source_sndfile(aubio_source_sndfile_t * s){ |
---|
[420962e] | 120 | if (!s) return; |
---|
[afbd7e7] | 121 | if (sf_close(s->handle)) { |
---|
| 122 | AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL)); |
---|
| 123 | } |
---|
| 124 | AUBIO_FREE(s->scratch_data); |
---|
| 125 | AUBIO_FREE(s); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | #endif /* HAVE_SNDFILE */ |
---|