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 | #include <string.h> |
---|
22 | |
---|
23 | #include <sndfile.h> |
---|
24 | |
---|
25 | #include "aubio_priv.h" |
---|
26 | #include "fvec.h" |
---|
27 | #include "sndfileio.h" |
---|
28 | #include "mathutils.h" |
---|
29 | |
---|
30 | #define MAX_CHANNELS 6 |
---|
31 | #define MAX_SIZE 4096 |
---|
32 | |
---|
33 | struct _aubio_sndfile_t { |
---|
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 | |
---|
42 | aubio_sndfile_t * new_aubio_sndfile_ro(const char* outputname) { |
---|
43 | aubio_sndfile_t * f = AUBIO_NEW(aubio_sndfile_t); |
---|
44 | SF_INFO sfinfo; |
---|
45 | AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo)); |
---|
46 | |
---|
47 | f->handle = sf_open (outputname, SFM_READ, &sfinfo); |
---|
48 | |
---|
49 | if (f->handle == NULL) { |
---|
50 | AUBIO_ERR("Failed opening %s: %s\n", outputname, |
---|
51 | sf_strerror (NULL)); /* libsndfile err msg */ |
---|
52 | return NULL; |
---|
53 | } |
---|
54 | |
---|
55 | if (sfinfo.channels > MAX_CHANNELS) { |
---|
56 | AUBIO_ERR("Not able to process more than %d channels\n", MAX_CHANNELS); |
---|
57 | return NULL; |
---|
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 | |
---|
70 | int aubio_sndfile_open_wo(aubio_sndfile_t * f, const char* inputname) { |
---|
71 | SF_INFO sfinfo; |
---|
72 | AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo)); |
---|
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 */ |
---|
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); |
---|
97 | f->samplerate = fmodel->samplerate; |
---|
98 | f->channels = fmodel->channels; |
---|
99 | f->format = fmodel->format; |
---|
100 | aubio_sndfile_open_wo(f, outputname); |
---|
101 | return f; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | /* return 0 if properly closed, 1 otherwise */ |
---|
106 | int del_aubio_sndfile(aubio_sndfile_t * f) { |
---|
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 */ |
---|
127 | int aubio_sndfile_read(aubio_sndfile_t * f, int frames, fvec_t * read) { |
---|
128 | sf_count_t read_frames; |
---|
129 | int i,j, channels = f->channels; |
---|
130 | int nsamples = frames*channels; |
---|
131 | int aread; |
---|
132 | smpl_t *pread; |
---|
133 | |
---|
134 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
135 | if (nsamples >= f->size) { |
---|
136 | AUBIO_ERR("Maximum aubio_sndfile_read buffer size exceeded."); |
---|
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++) { |
---|
152 | pread = (smpl_t *)fvec_get_channel(read,i); |
---|
153 | for (j=0; j<aread; j++) { |
---|
154 | pread[j] = (smpl_t)f->tmpdata[channels*j+i]; |
---|
155 | } |
---|
156 | } |
---|
157 | return aread; |
---|
158 | } |
---|
159 | |
---|
160 | /* write 'frames' samples to file from data |
---|
161 | * return the number of frames actually written |
---|
162 | */ |
---|
163 | int aubio_sndfile_write(aubio_sndfile_t * f, int frames, fvec_t * write) { |
---|
164 | sf_count_t written_frames = 0; |
---|
165 | int i, j, channels = f->channels; |
---|
166 | int nsamples = channels*frames; |
---|
167 | smpl_t *pwrite; |
---|
168 | |
---|
169 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
170 | if (nsamples >= f->size) { |
---|
171 | AUBIO_ERR("Maximum aubio_sndfile_write 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 | /* interleaving data */ |
---|
181 | for (i=0; i<channels; i++) { |
---|
182 | pwrite = (smpl_t *)fvec_get_channel(write,i); |
---|
183 | for (j=0; j<frames; j++) { |
---|
184 | f->tmpdata[channels*j+i] = (float)pwrite[j]; |
---|
185 | } |
---|
186 | } |
---|
187 | written_frames = sf_write_float (f->handle, f->tmpdata, nsamples); |
---|
188 | return written_frames/channels; |
---|
189 | } |
---|
190 | |
---|
191 | /******************************************************************* |
---|
192 | * |
---|
193 | * Get object info |
---|
194 | * |
---|
195 | */ |
---|
196 | |
---|
197 | uint_t aubio_sndfile_channels(aubio_sndfile_t * f) { |
---|
198 | return f->channels; |
---|
199 | } |
---|
200 | |
---|
201 | uint_t aubio_sndfile_samplerate(aubio_sndfile_t * f) { |
---|
202 | return f->samplerate; |
---|
203 | } |
---|
204 | |
---|
205 | void aubio_sndfile_info(aubio_sndfile_t * f) { |
---|
206 | AUBIO_DBG("srate : %d\n", f->samplerate); |
---|
207 | AUBIO_DBG("channels : %d\n", f->channels); |
---|
208 | AUBIO_DBG("format : %d\n", f->format); |
---|
209 | } |
---|
210 | |
---|