2 Copyright (C) 2012 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
28 #include "aubio_priv.h"
29 #include "source_sndfile.h"
32 #define MAX_CHANNELS 6
35 struct _aubio_source_sndfile_t {
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);
52 AUBIO_ERR("Aborted opening null path\n");
56 s->hop_size = hop_size;
57 s->samplerate = samplerate;
61 // try opening the file, geting the info in sfinfo
63 AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
64 s->handle = sf_open (s->path, SFM_READ, &sfinfo);
66 if (s->handle == NULL) {
67 /* show libsndfile err msg */
68 AUBIO_ERR("Failed opening %s: %s\n", s->path, sf_strerror (NULL));
72 if (sfinfo.channels > MAX_CHANNELS) {
73 AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS);
78 s->input_samplerate = sfinfo.samplerate;
79 s->input_channels = sfinfo.channels;
80 s->input_format = sfinfo.format;
82 if (s->samplerate != s->input_samplerate) {
83 AUBIO_ERR("resampling not implemented yet\n");
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) {
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);
94 s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
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;
103 /* do actual reading */
104 read_frames = sf_read_float (s->handle, s->scratch_data, s->scratch_size);
106 aread = (int)FLOOR(read_frames/(float)input_channels);
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];
114 read_data->data[j] /= (smpl_t)input_channels;
119 void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
121 if (sf_close(s->handle)) {
122 AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
124 AUBIO_FREE(s->scratch_data);
128 #endif /* HAVE_SNDFILE */