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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/BufferStream.h>
#include <AK/OwnPtr.h>
#include <LibAudio/WavLoader.h>
#include <LibCore/File.h>
#include <LibCore/IODeviceStreamReader.h>
#include <limits>
namespace Audio {
WavLoader::WavLoader(const StringView& path)
: m_file(Core::File::construct(path))
{
if (!m_file->open(Core::IODevice::ReadOnly)) {
m_error_string = String::format("Can't open file: %s", m_file->error_string());
return;
}
if (!parse_header())
return;
m_resampler = make<ResampleHelper>(m_sample_rate, 44100);
}
RefPtr<Buffer> WavLoader::get_more_samples(size_t max_bytes_to_read_from_input)
{
#ifdef AWAVLOADER_DEBUG
dbgprintf("Read WAV of format PCM with num_channels %u sample rate %u, bits per sample %u\n", m_num_channels, m_sample_rate, m_bits_per_sample);
#endif
auto raw_samples = m_file->read(max_bytes_to_read_from_input);
if (raw_samples.is_empty())
return nullptr;
auto buffer = Buffer::from_pcm_data(raw_samples, *m_resampler, m_num_channels, m_bits_per_sample);
//Buffer contains normalized samples, but m_loaded_samples should containt the ammount of actually loaded samples
m_loaded_samples += static_cast<int>(max_bytes_to_read_from_input) / (m_num_channels * (m_bits_per_sample / 8));
m_loaded_samples = min(m_total_samples, m_loaded_samples);
return buffer;
}
void WavLoader::seek(const int position)
{
if (position < 0 || position > m_total_samples)
return;
m_loaded_samples = position;
m_file->seek(position * m_num_channels * (m_bits_per_sample / 8));
}
void WavLoader::reset()
{
seek(0);
}
bool WavLoader::parse_header()
{
Core::IODeviceStreamReader stream(*m_file);
#define CHECK_OK(msg) \
do { \
if (stream.handle_read_failure()) { \
m_error_string = String::format("Premature stream EOF at %s", msg); \
return {}; \
} \
if (!ok) { \
m_error_string = String::format("Parsing failed: %s", msg); \
return {}; \
} else { \
dbgprintf("%s is OK!\n", msg); \
} \
} while (0);
bool ok = true;
u32 riff;
stream >> riff;
ok = ok && riff == 0x46464952; // "RIFF"
CHECK_OK("RIFF header");
u32 sz;
stream >> sz;
ok = ok && sz < 1024 * 1024 * 1024; // arbitrary
CHECK_OK("File size");
ASSERT(sz < 1024 * 1024 * 1024);
u32 wave;
stream >> wave;
ok = ok && wave == 0x45564157; // "WAVE"
CHECK_OK("WAVE header");
u32 fmt_id;
stream >> fmt_id;
ok = ok && fmt_id == 0x20746D66; // "FMT"
CHECK_OK("FMT header");
u32 fmt_size;
stream >> fmt_size;
ok = ok && fmt_size == 16;
CHECK_OK("FMT size");
ASSERT(fmt_size == 16);
u16 audio_format;
stream >> audio_format;
CHECK_OK("Audio format"); // incomplete read check
ok = ok && audio_format == 1; // WAVE_FORMAT_PCM
ASSERT(audio_format == 1);
CHECK_OK("Audio format"); // value check
stream >> m_num_channels;
ok = ok && (m_num_channels == 1 || m_num_channels == 2);
CHECK_OK("Channel count");
stream >> m_sample_rate;
CHECK_OK("Sample rate");
u32 byte_rate;
stream >> byte_rate;
CHECK_OK("Byte rate");
u16 block_align;
stream >> block_align;
CHECK_OK("Block align");
stream >> m_bits_per_sample;
CHECK_OK("Bits per sample"); // incomplete read check
ok = ok && (m_bits_per_sample == 8 || m_bits_per_sample == 16 || m_bits_per_sample == 24);
ASSERT(m_bits_per_sample == 8 || m_bits_per_sample == 16 || m_bits_per_sample == 24);
CHECK_OK("Bits per sample"); // value check
// Read chunks until we find DATA
bool found_data = false;
u32 data_sz = 0;
while (true) {
u32 chunk_id;
stream >> chunk_id;
CHECK_OK("Reading chunk ID searching for data");
stream >> data_sz;
CHECK_OK("Reading chunk size searching for data");
if (chunk_id == 0x61746164) { // DATA
found_data = true;
break;
}
}
ok = ok && found_data;
CHECK_OK("Found no data chunk");
ASSERT(found_data);
ok = ok && data_sz < INT32_MAX;
CHECK_OK("Data was too large");
int bytes_per_sample = (m_bits_per_sample / 8) * m_num_channels;
m_total_samples = data_sz / bytes_per_sample;
// Just make sure we're good before we read the data...
ASSERT(!stream.handle_read_failure());
return true;
}
ResampleHelper::ResampleHelper(double source, double target)
: m_ratio(source / target)
{
}
void ResampleHelper::process_sample(double sample_l, double sample_r)
{
m_last_sample_l = sample_l;
m_last_sample_r = sample_r;
m_current_ratio += 1;
}
bool ResampleHelper::read_sample(double& next_l, double& next_r)
{
if (m_current_ratio > 0) {
m_current_ratio -= m_ratio;
next_l = m_last_sample_l;
next_r = m_last_sample_r;
return true;
}
return false;
}
template<typename SampleReader>
static void read_samples_from_stream(BufferStream& stream, SampleReader read_sample, Vector<Sample>& samples, ResampleHelper& resampler, int num_channels)
{
double norm_l = 0;
double norm_r = 0;
switch (num_channels) {
case 1:
for (;;) {
while (resampler.read_sample(norm_l, norm_r)) {
samples.append(Sample(norm_l));
}
norm_l = read_sample(stream);
if (stream.handle_read_failure()) {
break;
}
resampler.process_sample(norm_l, norm_r);
}
break;
case 2:
for (;;) {
while (resampler.read_sample(norm_l, norm_r)) {
samples.append(Sample(norm_l, norm_r));
}
norm_l = read_sample(stream);
norm_r = read_sample(stream);
if (stream.handle_read_failure()) {
break;
}
resampler.process_sample(norm_l, norm_r);
}
break;
default:
ASSERT_NOT_REACHED();
}
}
static double read_norm_sample_24(BufferStream& stream)
{
u8 byte = 0;
stream >> byte;
u32 sample1 = byte;
stream >> byte;
u32 sample2 = byte;
stream >> byte;
u32 sample3 = byte;
i32 value = 0;
value = sample1 << 8;
value |= (sample2 << 16);
value |= (sample3 << 24);
return double(value) / std::numeric_limits<i32>::max();
}
static double read_norm_sample_16(BufferStream& stream)
{
i16 sample = 0;
stream >> sample;
return double(sample) / std::numeric_limits<i16>::max();
}
static double read_norm_sample_8(BufferStream& stream)
{
u8 sample = 0;
stream >> sample;
return double(sample) / std::numeric_limits<u8>::max();
}
// ### can't const this because BufferStream is non-const
// perhaps we need a reading class separate from the writing one, that can be
// entirely consted.
RefPtr<Buffer> Buffer::from_pcm_data(ByteBuffer& data, ResampleHelper& resampler, int num_channels, int bits_per_sample)
{
BufferStream stream(data);
Vector<Sample> fdata;
fdata.ensure_capacity(data.size() / (bits_per_sample / 8));
#ifdef AWAVLOADER_DEBUG
dbg() << "Reading " << bits_per_sample << " bits and " << num_channels << " channels, total bytes: " << data.size();
#endif
switch (bits_per_sample) {
case 8:
read_samples_from_stream(stream, read_norm_sample_8, fdata, resampler, num_channels);
break;
case 16:
read_samples_from_stream(stream, read_norm_sample_16, fdata, resampler, num_channels);
break;
case 24:
read_samples_from_stream(stream, read_norm_sample_24, fdata, resampler, num_channels);
break;
default:
ASSERT_NOT_REACHED();
}
// We should handle this in a better way above, but for now --
// just make sure we're good. Worst case we just write some 0s where they
// don't belong.
ASSERT(!stream.handle_read_failure());
return Buffer::create_with_samples(move(fdata));
}
}
|