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