1 | /* |
---|
2 | Copyright (C) 2003 Paul Brossier |
---|
3 | |
---|
4 | This program is free software; you can redistribute it and/or modify |
---|
5 | it under the terms of the GNU General Public License as published by |
---|
6 | the Free Software Foundation; either version 2 of the License, or |
---|
7 | (at your option) any later version. |
---|
8 | |
---|
9 | This program is distributed in the hope that it will be useful, |
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | GNU General Public License for more details. |
---|
13 | |
---|
14 | You should have received a copy of the GNU General Public License |
---|
15 | along with this program; if not, write to the Free Software |
---|
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
17 | |
---|
18 | */ |
---|
19 | |
---|
20 | #include <string.h> |
---|
21 | |
---|
22 | #include <sndfile.h> |
---|
23 | |
---|
24 | #include "aubio_priv.h" |
---|
25 | #include "sample.h" |
---|
26 | #include "sndfileio.h" |
---|
27 | #include "mathutils.h" |
---|
28 | |
---|
29 | #define MAX_CHANNELS 6 |
---|
30 | #define MAX_SIZE 4096 |
---|
31 | |
---|
32 | struct _aubio_sndfile_t { |
---|
33 | SNDFILE *handle; |
---|
34 | int samplerate; |
---|
35 | int channels; |
---|
36 | int format; |
---|
37 | float *tmpdata; /** scratch pad for interleaving/deinterleaving. */ |
---|
38 | int size; /** store the size to check if realloc needed */ |
---|
39 | }; |
---|
40 | |
---|
41 | aubio_sndfile_t * new_aubio_sndfile_ro(const char* outputname) { |
---|
42 | aubio_sndfile_t * f = AUBIO_NEW(aubio_sndfile_t); |
---|
43 | SF_INFO sfinfo; |
---|
44 | AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo)); |
---|
45 | sfinfo.format = 0; |
---|
46 | |
---|
47 | f->handle = sf_open (outputname, SFM_READ, &sfinfo); |
---|
48 | |
---|
49 | if (f->handle == NULL) { |
---|
50 | AUBIO_ERR("Unable to open input file %s.\n", outputname); |
---|
51 | AUBIO_ERR("%s\n",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 | float *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 = fvec_get_channel(read,i); |
---|
153 | for (j=0; j<aread; j++) { |
---|
154 | pread[j] = 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 | float *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 = fvec_get_channel(write,i); |
---|
183 | for (j=0; j<frames; j++) { |
---|
184 | f->tmpdata[channels*j+i] = 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 | |
---|