This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsoundfile.cpp
295 lines (254 loc) · 5.92 KB
/
soundfile.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <cassert>
#include <iostream>
#include "soundfile.hpp"
namespace
{
const size_t MAX_FIXED_T_VAL=std::pow(2.0,(int)(8*sizeof(mad_fixed_t)-1)-1);
}
QString Soundfile::writeSound(const QString& fname, const real_vec& data,
int samplerate, int format)
{
if (format == -1)
{
format = guessFormat(fname);
if (!format)
return "Unsupported filetype for writing.";
}
SF_INFO check = {0, samplerate, 1, format, 0, 0};
if (!sf_format_check(&check))
return "Format didn't pass sf_format_check()"; // shouldn't happen
//XXX zmeni unicode nazvy
SndfileHandle file(fname.toLocal8Bit(), SFM_WRITE, format, 1, samplerate);
if (!file)
assert(false);
if (file.error())
return file.strError();
file.writef(&data[0], data.size());
return QString();
}
/// Returns the format specification guessed from the specified file extension.
int Soundfile::guessFormat(const QString& filename)
{
assert(!filename.isNull());
if (filename.endsWith(".wav"))
return SF_FORMAT_WAV|SF_FORMAT_PCM_16;
else if (filename.endsWith(".ogg"))
return SF_FORMAT_OGG|SF_FORMAT_VORBIS;
else if (filename.endsWith(".flac"))
return SF_FORMAT_FLAC|SF_FORMAT_PCM_16;
return 0;
}
Soundfile::Soundfile()
: data_(NULL)
{
}
Soundfile::Soundfile(const QString& filename)
{
load(filename);
}
real_vec Soundfile::read_channel(int channel)
{
return data_->read_channel(channel);
}
bool Soundfile::valid() const
{
return data_ != NULL;
}
const QString& Soundfile::error() const
{
return error_;
}
void Soundfile::load(const QString& filename)
{
if (filename.endsWith(".mp3"))
data_ = new MP3Data(filename);
else
data_ = new SndfileData(filename);
if (!data_->valid())
{
error_ = data_->error();
delete data_;
data_ = NULL;
}
}
void Soundfile::reset()
{
delete data_;
data_ = NULL;
}
const SoundfileData& Soundfile::data() const
{
assert(data_ != NULL);
return *data_;
}
// ----
SndfileData::SndfileData(const QString& filename)
{
file_ = SndfileHandle(filename.toLocal8Bit());
}
bool SndfileData::valid() const
{
return (file_ && !file_.error());
}
QString SndfileData::error() const
{
return file_.strError();
}
size_t SndfileData::frames() const
{
return file_.frames();
}
double SndfileData::length() const
{
return (double)frames()/samplerate();
}
int SndfileData::channels() const
{
return file_.channels();
}
int SndfileData::samplerate() const
{
return file_.samplerate();
}
real_vec SndfileData::read_channel(int channel)
{
assert(channel < channels());
real_vec buffer(frames()*channels());
file_.readf(&buffer[0], frames());
//size_t frames_read = file_.readf(&buffer[0], frames());
//assert(frames_read == frames());
file_.seek(0, SEEK_SET);
if (channels() > 1)
{
for (size_t i = channel, j = 0; j < frames(); i += channels(), ++j)
buffer[j] = buffer[i];
buffer.resize(frames());
}
return buffer;
}
SndfileData::~SndfileData()
{
}
// ---
MP3Data::MP3Data(const QString& fname)
: frames_(0)
, length_(0)
, samplerate_(0)
, channels_(0)
, filename_(fname)
{
get_mp3_stats();
}
void MP3Data::get_mp3_stats()
{
mad_stream stream;
mad_header header;
mad_stream_init(&stream);
mad_header_init(&header);
QFile file(filename_);
if (!file.open(QIODevice::ReadOnly))
{
error_ = "Error opening file.";
goto cleanup;
}
{
uchar* buf = file.map(0, file.size());
mad_stream_buffer(&stream, buf, file.size());
while (true)
{
if (mad_header_decode(&header, &stream) == -1)
{
if (stream.error == MAD_ERROR_LOSTSYNC)
continue;
else if (stream.error == MAD_ERROR_BUFLEN)
break;
else
{
error_ = "Error decoding mp3 headers!";
break;
}
}
frames_++;
length_ += header.duration.fraction;
if (!samplerate_)
{
samplerate_ = header.samplerate;
if (header.mode == MAD_MODE_SINGLE_CHANNEL)
channels_ = 1;
else
channels_ = 2;
}
}
length_ /= MAD_TIMER_RESOLUTION;
if (!samplerate_)
error_ = "Invalid mp3 file.";
}
cleanup:
mad_stream_finish(&stream);
mad_header_finish(&header);
}
QString MP3Data::error() const
{
return error_;
}
real_vec MP3Data::read_channel(int channel)
{
mad_stream stream;
mad_frame frame;
mad_synth synth;
mad_stream_init(&stream);
mad_frame_init(&frame);
mad_synth_init(&synth);
real_vec result;
QFile file(filename_);
if (!file.open(QIODevice::ReadOnly))
goto cleanup;
{
uchar* buf = file.map(0, file.size());
mad_stream_buffer(&stream, buf, file.size());
while (true)
{
if (mad_frame_decode(&frame, &stream) == -1)
{
if (stream.error == MAD_ERROR_LOSTSYNC)
continue;
else if (stream.error == MAD_ERROR_BUFLEN)
break;
else
{
error_ = "Error decoding mp3 file.";
break;
}
}
mad_synth_frame(&synth, &frame);
mad_fixed_t* samples = synth.pcm.samples[channel];
for (short i = 0; i < synth.pcm.length; ++i)
result.push_back((double)samples[i]/MAX_FIXED_T_VAL);
}
}
cleanup:
mad_synth_finish(&synth);
mad_frame_finish(&frame);
mad_stream_finish(&stream);
return result;
}
size_t MP3Data::frames() const
{
return frames_;
}
double MP3Data::length() const//in seconds
{
return length_;
}
int MP3Data::samplerate() const
{
return samplerate_;
}
int MP3Data::channels() const
{
return channels_;
}
bool MP3Data::valid() const
{
return error_.isEmpty();
}