/
audiofilereader.cpp
259 lines (201 loc) · 7.05 KB
/
audiofilereader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* audiofilereader.cpp -- implements a class to read samples
*
* 14-Jun-08 RBD
* 16-Jun-08 RBD revised to use libsndfile
*/
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "math.h"
#include <string>
#include "samplerate.h"
#include "audioreader.h"
#include "audiofilereader.h"
#include "constant.h"
#include <ctime>
#include <iostream>
#ifdef WIN32
#include <malloc.h>
#define bzero(addr, siz) memset(addr, 0, siz)
#define alloca _alloca
#endif
#define DEFAULT_CONVERTER SRC_SINC_MEDIUM_QUALITY
#define BUFFER_LEN 1024
double Audio_file_reader::get_sample_rate()
{
return sf_info.samplerate;
}
long Audio_file_reader::get_frames()
{
return total_frames;
}
long Audio_file_reader::read(float *data, long n)
{
// note that "samples_per_frame" is really "frames_per_window" in this
// context, so we're computing bytes per window
float *input_data = (float *) alloca(bytes_per_frame * samples_per_frame);
assert(input_data != NULL) ;
long frames_read = (long) sf_readf_float(sf, input_data, n);
long chans = sf_info.channels;
// now convert to mono and move to data
for (int frame = 0; frame < frames_read; frame++) {
float sum = 0;
for (int chan = 0; chan < sf_info.channels; chan++) {
// sum over channels within a frame
sum += input_data[frame * chans + chan];
}
// write the frame sum to result array
data[frame] = sum;
}
return frames_read;
}
bool Audio_file_reader::open(const char *filename, Feature_extractor &fe, int start_frame, int rsamplerate, bool verbose)
{
clock_t start, end;
bytes_per_frame = 0; // initialize now in case an error occurs
name[0] = 0;
bzero(&sf_info, sizeof(sf_info));
sf = sf_open(filename, SFM_READ, &sf_info);
if (!sf)
return false;
strncpy(name, filename, MAX_NAME_LEN);
name[MAX_NAME_LEN] = 0; // just in case
if (rsamplerate != 0) {
start = clock();
string rname = resample(rsamplerate);
end = clock();
cout << "Running Time for resample : " << (double) (end - start) / CLOCKS_PER_SEC << endl;
if (rname == "") {
return false;
}
sf = sf_open(rname.c_str(), SFM_READ, &sf_info);
}
total_frames = (long) sf_seek(sf, 0, SEEK_END);
sf_seek(sf, start_frame, SEEK_SET);
// we're going to read floats, but they might be multi-channel...
bytes_per_frame = sf_info.channels * sizeof(float);
calculate_parameters(fe, verbose);
return true;
}
bool Audio_file_reader::open(const char *filename, Feature_extractor &fe, int rsamplerate, bool verbose) {
return open(filename, fe, 0, rsamplerate, verbose);
}
string Audio_file_reader::resample(int new_sample_rate) {
sf_count_t count;
string rname;
SNDFILE *sf_rs = NULL;
double gain = 1.0, src_ratio = -1.0;
int samplerate = sf_info.samplerate;
if (samplerate <= new_sample_rate) {
printf("WARNING: The original file has sampling frequency %d less than the resampled frequency %d\n", samplerate, new_sample_rate);
}
int converter = DEFAULT_CONVERTER ;
#ifdef DEBUG
printf ("Input File : %s\n", name) ;
printf ("Sample Rate : %d\n", sf_info.samplerate) ;
printf ("Input Frames : %ld\n\n", (long) sf_info.frames) ;
#endif
src_ratio = (1.0 * new_sample_rate) / samplerate;
sf_info.samplerate = new_sample_rate ;
if (fabs (src_ratio - 1.0) < 1e-20)
{
printf ("Target samplerate and input samplerate are the same. Exiting.\n") ;
return "";
}
#ifdef DEBUG
printf ("SRC Ratio : %f\n", src_ratio) ;
printf ("Converter : %s\n\n", src_get_name (converter)) ;
#endif
if (src_is_valid_ratio (src_ratio) == 0)
{
printf ("Error : Sample rate change out of valid range.\n");
return "";
}
rname = "resample_" + string(name);
remove (rname.c_str()) ;
#ifdef DEBUG
printf ("Output file : %s\n", rname.c_str());
printf ("Sample Rate : %d\n", sf_info.samplerate) ;
#endif
if (sf_rs != NULL) {
sf_close (sf_rs);
}
if ((sf_rs = sf_open(rname.c_str(), SFM_WRITE, &sf_info)) == NULL) {
printf ("Error : Not able to open output file '%s'\n", rname.c_str());
sf_close (sf);
return "";
};
/* Update the file header after every write. */
sf_command (sf_rs, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE);
sf_command (sf_rs, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
count = sample_rate_convert (sf_rs, converter, src_ratio, sf_info.channels, &gain) ;
sf_close(sf_rs);
#ifdef DEBUG
printf ("Output Frames : %ld\n\n", (long) count) ;
#endif
return rname;
}
sf_count_t Audio_file_reader::sample_rate_convert (SNDFILE *sf_rs, int converter, double src_ratio, int channels, double * gain) {
static float input [BUFFER_LEN] ;
static float output [BUFFER_LEN] ;
SRC_STATE *src_state ;
SRC_DATA src_data ;
int error ;
sf_count_t output_count = 0 ;
sf_seek (sf, 0, SEEK_SET) ;
sf_seek (sf_rs, 0, SEEK_SET) ;
/* Initialize the sample rate converter. */
if ((src_state = src_new (converter, channels, &error)) == NULL)
{ printf ("\n\nError : src_new() failed : %s.\n\n", src_strerror (error)) ;
exit (1) ;
} ;
src_data.end_of_input = 0 ; /* Set this later. */
/* Start with zero to force load in while loop. */
src_data.input_frames = 0 ;
src_data.data_in = input ;
src_data.src_ratio = src_ratio ;
src_data.data_out = output ;
src_data.output_frames = BUFFER_LEN /channels ;
while (1)
{
/* If the input buffer is empty, refill it. */
if (src_data.input_frames == 0)
{
src_data.input_frames = sf_readf_float (sf, input, BUFFER_LEN / channels) ;
src_data.data_in = input ;
/* The last read will not be a full buffer, so snd_of_input. */
if (src_data.input_frames < BUFFER_LEN / channels)
src_data.end_of_input = SF_TRUE ;
};
if ((error = src_process (src_state, &src_data)))
{
printf ("\nError : %s\n", src_strerror (error)) ;
exit (1) ;
} ;
/* Terminate if done. */
if (src_data.end_of_input && src_data.output_frames_gen == 0)
break ;
/* Write output. */
sf_writef_float (sf_rs, output, src_data.output_frames_gen) ;
output_count += src_data.output_frames_gen ;
src_data.data_in += src_data.input_frames_used * channels ;
src_data.input_frames -= src_data.input_frames_used ;
} ;
src_state = src_delete (src_state) ;
return output_count ;
} /* sample_rate_convert */
void Audio_file_reader::close()
{
sf_close(sf);
}
void Audio_file_reader::print_info()
{
printf(" file name = %s\n", name);
double sample_rate = sf_info.samplerate;
printf(" sample rate = %g\n", sample_rate);
printf(" channels = %d\n", sf_info.channels);
/*=============================================================*/
printf(" total frames number is = %ld\n", total_frames);
printf(" audio duration = %g seconds\n", total_frames / sample_rate);
/*=============================================================*/
}