[96fb8ad] | 1 | /* |
---|
[6a2478c] | 2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 3 | |
---|
[6a2478c] | 4 | This file is part of aubio. |
---|
[96fb8ad] | 5 | |
---|
[6a2478c] | 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. |
---|
[96fb8ad] | 10 | |
---|
[6a2478c] | 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/>. |
---|
[96fb8ad] | 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include <string.h> |
---|
| 22 | |
---|
| 23 | #include <sndfile.h> |
---|
| 24 | |
---|
| 25 | #include "aubio_priv.h" |
---|
[6c7d49b] | 26 | #include "fvec.h" |
---|
[96fb8ad] | 27 | #include "sndfileio.h" |
---|
| 28 | #include "mathutils.h" |
---|
| 29 | |
---|
| 30 | #define MAX_CHANNELS 6 |
---|
| 31 | #define MAX_SIZE 4096 |
---|
| 32 | |
---|
[5e9c68a] | 33 | struct _aubio_sndfile_t { |
---|
[96fb8ad] | 34 | SNDFILE *handle; |
---|
| 35 | int samplerate; |
---|
| 36 | int channels; |
---|
| 37 | int format; |
---|
| 38 | float *tmpdata; /** scratch pad for interleaving/deinterleaving. */ |
---|
| 39 | int size; /** store the size to check if realloc needed */ |
---|
| 40 | }; |
---|
| 41 | |
---|
[5e9c68a] | 42 | aubio_sndfile_t * new_aubio_sndfile_ro(const char* outputname) { |
---|
| 43 | aubio_sndfile_t * f = AUBIO_NEW(aubio_sndfile_t); |
---|
[96fb8ad] | 44 | SF_INFO sfinfo; |
---|
| 45 | AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo)); |
---|
| 46 | |
---|
[e83c895] | 47 | f->handle = sf_open (outputname, SFM_READ, &sfinfo); |
---|
| 48 | |
---|
| 49 | if (f->handle == NULL) { |
---|
[6c25201] | 50 | AUBIO_ERR("Failed opening %s: %s\n", outputname, |
---|
| 51 | sf_strerror (NULL)); /* libsndfile err msg */ |
---|
[f382ac6] | 52 | return NULL; |
---|
[96fb8ad] | 53 | } |
---|
| 54 | |
---|
| 55 | if (sfinfo.channels > MAX_CHANNELS) { |
---|
| 56 | AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS); |
---|
[f382ac6] | 57 | return NULL; |
---|
[96fb8ad] | 58 | } |
---|
| 59 | |
---|
| 60 | f->size = MAX_SIZE*sfinfo.channels; |
---|
| 61 | f->tmpdata = AUBIO_ARRAY(float,f->size); |
---|
| 62 | /* get input specs */ |
---|
| 63 | f->samplerate = sfinfo.samplerate; |
---|
| 64 | f->channels = sfinfo.channels; |
---|
| 65 | f->format = sfinfo.format; |
---|
| 66 | |
---|
| 67 | return f; |
---|
| 68 | } |
---|
| 69 | |
---|
[5e9c68a] | 70 | int aubio_sndfile_open_wo(aubio_sndfile_t * f, const char* inputname) { |
---|
[96fb8ad] | 71 | SF_INFO sfinfo; |
---|
[e83c895] | 72 | AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo)); |
---|
[96fb8ad] | 73 | |
---|
| 74 | /* define file output spec */ |
---|
| 75 | sfinfo.samplerate = f->samplerate; |
---|
| 76 | sfinfo.channels = f->channels; |
---|
| 77 | sfinfo.format = f->format; |
---|
| 78 | |
---|
| 79 | if (! (f->handle = sf_open (inputname, SFM_WRITE, &sfinfo))) { |
---|
| 80 | AUBIO_ERR("Not able to open output file %s.\n", inputname); |
---|
| 81 | AUBIO_ERR("%s\n",sf_strerror (NULL)); /* libsndfile err msg */ |
---|
| 82 | AUBIO_QUIT(AUBIO_FAIL); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | if (sfinfo.channels > MAX_CHANNELS) { |
---|
| 86 | AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS); |
---|
| 87 | AUBIO_QUIT(AUBIO_FAIL); |
---|
| 88 | } |
---|
| 89 | f->size = MAX_SIZE*sfinfo.channels; |
---|
| 90 | f->tmpdata = AUBIO_ARRAY(float,f->size); |
---|
| 91 | return AUBIO_OK; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /* setup file struct from existing one */ |
---|
[5e9c68a] | 95 | aubio_sndfile_t * new_aubio_sndfile_wo(aubio_sndfile_t * fmodel, const char *outputname) { |
---|
| 96 | aubio_sndfile_t * f = AUBIO_NEW(aubio_sndfile_t); |
---|
[96fb8ad] | 97 | f->samplerate = fmodel->samplerate; |
---|
[4621cd6] | 98 | f->channels = 1; //fmodel->channels; |
---|
[96fb8ad] | 99 | f->format = fmodel->format; |
---|
[5e9c68a] | 100 | aubio_sndfile_open_wo(f, outputname); |
---|
[96fb8ad] | 101 | return f; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | /* return 0 if properly closed, 1 otherwise */ |
---|
[5e9c68a] | 106 | int del_aubio_sndfile(aubio_sndfile_t * f) { |
---|
[96fb8ad] | 107 | if (sf_close(f->handle)) { |
---|
| 108 | AUBIO_ERR("Error closing file."); |
---|
| 109 | puts (sf_strerror (NULL)); |
---|
| 110 | return 1; |
---|
| 111 | } |
---|
| 112 | AUBIO_FREE(f->tmpdata); |
---|
| 113 | AUBIO_FREE(f); |
---|
| 114 | //AUBIO_DBG("File closed.\n"); |
---|
| 115 | return 0; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /************************************************************** |
---|
| 119 | * |
---|
| 120 | * Read write methods |
---|
| 121 | * |
---|
| 122 | */ |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | /* read frames from file in data |
---|
| 126 | * return the number of frames actually read */ |
---|
[4621cd6] | 127 | int aubio_sndfile_read(aubio_sndfile_t * f, int frames, fvec_t ** read) { |
---|
[96fb8ad] | 128 | sf_count_t read_frames; |
---|
| 129 | int i,j, channels = f->channels; |
---|
| 130 | int nsamples = frames*channels; |
---|
| 131 | int aread; |
---|
[d69e37d] | 132 | smpl_t *pread; |
---|
[96fb8ad] | 133 | |
---|
| 134 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
| 135 | if (nsamples >= f->size) { |
---|
[5e9c68a] | 136 | AUBIO_ERR("Maximum aubio_sndfile_read buffer size exceeded."); |
---|
[96fb8ad] | 137 | return -1; |
---|
| 138 | /* |
---|
| 139 | AUBIO_FREE(f->tmpdata); |
---|
| 140 | f->tmpdata = AUBIO_ARRAY(float,nsamples); |
---|
| 141 | */ |
---|
| 142 | } |
---|
| 143 | //f->size = nsamples; |
---|
| 144 | |
---|
| 145 | /* do actual reading */ |
---|
| 146 | read_frames = sf_read_float (f->handle, f->tmpdata, nsamples); |
---|
| 147 | |
---|
| 148 | aread = (int)FLOOR(read_frames/(float)channels); |
---|
| 149 | |
---|
| 150 | /* de-interleaving data */ |
---|
| 151 | for (i=0; i<channels; i++) { |
---|
[4621cd6] | 152 | pread = (smpl_t *)fvec_get_data(read[i]); |
---|
[96fb8ad] | 153 | for (j=0; j<aread; j++) { |
---|
[d69e37d] | 154 | pread[j] = (smpl_t)f->tmpdata[channels*j+i]; |
---|
[96fb8ad] | 155 | } |
---|
| 156 | } |
---|
| 157 | return aread; |
---|
| 158 | } |
---|
| 159 | |
---|
[4621cd6] | 160 | int |
---|
| 161 | aubio_sndfile_read_mono (aubio_sndfile_t * f, int frames, fvec_t * read) |
---|
| 162 | { |
---|
| 163 | sf_count_t read_frames; |
---|
| 164 | int i, j, channels = f->channels; |
---|
| 165 | int nsamples = frames * channels; |
---|
| 166 | int aread; |
---|
| 167 | smpl_t *pread; |
---|
| 168 | |
---|
| 169 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
| 170 | if (nsamples >= f->size) { |
---|
| 171 | AUBIO_ERR ("Maximum aubio_sndfile_read buffer size exceeded."); |
---|
| 172 | return -1; |
---|
| 173 | /* |
---|
| 174 | AUBIO_FREE(f->tmpdata); |
---|
| 175 | f->tmpdata = AUBIO_ARRAY(float,nsamples); |
---|
| 176 | */ |
---|
| 177 | } |
---|
| 178 | //f->size = nsamples; |
---|
| 179 | |
---|
| 180 | /* do actual reading */ |
---|
| 181 | read_frames = sf_read_float (f->handle, f->tmpdata, nsamples); |
---|
| 182 | |
---|
| 183 | aread = (int) FLOOR (read_frames / (float) channels); |
---|
| 184 | |
---|
| 185 | /* de-interleaving data */ |
---|
| 186 | pread = (smpl_t *) fvec_get_data (read); |
---|
| 187 | for (i = 0; i < channels; i++) { |
---|
| 188 | for (j = 0; j < aread; j++) { |
---|
| 189 | pread[j] += (smpl_t) f->tmpdata[channels * j + i]; |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | for (j = 0; j < aread; j++) { |
---|
| 193 | pread[j] /= (smpl_t)channels; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | return aread; |
---|
| 197 | } |
---|
| 198 | |
---|
[96fb8ad] | 199 | /* write 'frames' samples to file from data |
---|
| 200 | * return the number of frames actually written |
---|
| 201 | */ |
---|
[4621cd6] | 202 | int aubio_sndfile_write(aubio_sndfile_t * f, int frames, fvec_t ** write) { |
---|
[96fb8ad] | 203 | sf_count_t written_frames = 0; |
---|
| 204 | int i, j, channels = f->channels; |
---|
| 205 | int nsamples = channels*frames; |
---|
[d69e37d] | 206 | smpl_t *pwrite; |
---|
[96fb8ad] | 207 | |
---|
| 208 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
| 209 | if (nsamples >= f->size) { |
---|
[5e9c68a] | 210 | AUBIO_ERR("Maximum aubio_sndfile_write buffer size exceeded."); |
---|
[96fb8ad] | 211 | return -1; |
---|
| 212 | /* |
---|
| 213 | AUBIO_FREE(f->tmpdata); |
---|
| 214 | f->tmpdata = AUBIO_ARRAY(float,nsamples); |
---|
| 215 | */ |
---|
| 216 | } |
---|
| 217 | //f->size = nsamples; |
---|
| 218 | |
---|
| 219 | /* interleaving data */ |
---|
| 220 | for (i=0; i<channels; i++) { |
---|
[4621cd6] | 221 | pwrite = (smpl_t *)fvec_get_data(write[i]); |
---|
[96fb8ad] | 222 | for (j=0; j<frames; j++) { |
---|
[d69e37d] | 223 | f->tmpdata[channels*j+i] = (float)pwrite[j]; |
---|
[96fb8ad] | 224 | } |
---|
| 225 | } |
---|
| 226 | written_frames = sf_write_float (f->handle, f->tmpdata, nsamples); |
---|
| 227 | return written_frames/channels; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | /******************************************************************* |
---|
| 231 | * |
---|
| 232 | * Get object info |
---|
| 233 | * |
---|
| 234 | */ |
---|
| 235 | |
---|
[5e9c68a] | 236 | uint_t aubio_sndfile_channels(aubio_sndfile_t * f) { |
---|
[96fb8ad] | 237 | return f->channels; |
---|
| 238 | } |
---|
| 239 | |
---|
[5e9c68a] | 240 | uint_t aubio_sndfile_samplerate(aubio_sndfile_t * f) { |
---|
[96fb8ad] | 241 | return f->samplerate; |
---|
| 242 | } |
---|
| 243 | |
---|
[5e9c68a] | 244 | void aubio_sndfile_info(aubio_sndfile_t * f) { |
---|
[96fb8ad] | 245 | AUBIO_DBG("srate : %d\n", f->samplerate); |
---|
| 246 | AUBIO_DBG("channels : %d\n", f->channels); |
---|
| 247 | AUBIO_DBG("format : %d\n", f->format); |
---|
| 248 | } |
---|
| 249 | |
---|