wave-reader.cc
11.7 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// sherpa-onnx/csrc/wave-reader.cc
//
// Copyright (c) 2023 Xiaomi Corporation
#include "sherpa-onnx/csrc/wave-reader.h"
#include <cassert>
#include <cstdint>
#include <fstream>
#include <utility>
#include <vector>
#include "sherpa-onnx/csrc/macros.h"
namespace sherpa_onnx {
namespace {
// see http://soundfile.sapp.org/doc/WaveFormat/
//
// Note: We assume little endian here
// TODO(fangjun): Support big endian
struct WaveHeader {
// See
// https://en.wikipedia.org/wiki/WAV#Metadata
// and
// https://www.robotplanet.dk/audio/wav_meta_data/riff_mci.pdf
void SeekToDataChunk(std::istream &is) {
// a t a d
while (is && subchunk2_id != 0x61746164) {
// const char *p = reinterpret_cast<const char *>(&subchunk2_id);
// printf("Skip chunk (%x): %c%c%c%c of size: %d\n", subchunk2_id, p[0],
// p[1], p[2], p[3], subchunk2_size);
is.seekg(subchunk2_size, std::istream::cur);
is.read(reinterpret_cast<char *>(&subchunk2_id), sizeof(int32_t));
is.read(reinterpret_cast<char *>(&subchunk2_size), sizeof(int32_t));
}
}
int32_t chunk_id;
int32_t chunk_size;
int32_t format;
int32_t subchunk1_id;
int32_t subchunk1_size;
int16_t audio_format;
int16_t num_channels;
int32_t sample_rate;
int32_t byte_rate;
int16_t block_align;
int16_t bits_per_sample;
int32_t subchunk2_id; // a tag of this chunk
int32_t subchunk2_size; // size of subchunk2
};
static_assert(sizeof(WaveHeader) == 44);
/*
sox int16-1-channel-zh.wav -b 8 int8-1-channel-zh.wav
sox int16-1-channel-zh.wav -c 2 int16-2-channel-zh.wav
we use audacity to generate int32-1-channel-zh.wav and float32-1-channel-zh.wav
because sox uses WAVE_FORMAT_EXTENSIBLE, which is not easy to support
in sherpa-onnx.
*/
// Read a wave file of mono-channel.
// Return its samples normalized to the range [-1, 1).
std::vector<std::vector<float>> ReadWaveImpl(std::istream &is,
int32_t *sampling_rate,
bool *is_ok) {
WaveHeader header{};
is.read(reinterpret_cast<char *>(&header.chunk_id), sizeof(header.chunk_id));
// F F I R
if (header.chunk_id != 0x46464952) {
SHERPA_ONNX_LOGE("Expected chunk_id RIFF. Given: 0x%08x\n",
header.chunk_id);
*is_ok = false;
return {};
}
is.read(reinterpret_cast<char *>(&header.chunk_size),
sizeof(header.chunk_size));
is.read(reinterpret_cast<char *>(&header.format), sizeof(header.format));
// E V A W
if (header.format != 0x45564157) {
SHERPA_ONNX_LOGE("Expected format WAVE. Given: 0x%08x\n", header.format);
*is_ok = false;
return {};
}
is.read(reinterpret_cast<char *>(&header.subchunk1_id),
sizeof(header.subchunk1_id));
is.read(reinterpret_cast<char *>(&header.subchunk1_size),
sizeof(header.subchunk1_size));
if (header.subchunk1_id == 0x4b4e554a) {
// skip junk padding
is.seekg(header.subchunk1_size, std::istream::cur);
is.read(reinterpret_cast<char *>(&header.subchunk1_id),
sizeof(header.subchunk1_id));
is.read(reinterpret_cast<char *>(&header.subchunk1_size),
sizeof(header.subchunk1_size));
}
if (header.subchunk1_id != 0x20746d66) {
SHERPA_ONNX_LOGE("Expected subchunk1_id 0x20746d66. Given: 0x%08x\n",
header.subchunk1_id);
*is_ok = false;
return {};
}
// NAudio uses 18
// See https://github.com/naudio/NAudio/issues/1132
if (header.subchunk1_size != 16 &&
header.subchunk1_size != 18) { // 16 for PCM
SHERPA_ONNX_LOGE("Expected subchunk1_size 16. Given: %d\n",
header.subchunk1_size);
*is_ok = false;
return {};
}
is.read(reinterpret_cast<char *>(&header.audio_format),
sizeof(header.audio_format));
if (header.audio_format != 1 && header.audio_format != 3) {
// 1 for integer PCM
// 3 for floating point PCM
// see https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
// and https://github.com/microsoft/DirectXTK/wiki/Wave-Formats
SHERPA_ONNX_LOGE("Expected audio_format 1. Given: %d\n",
header.audio_format);
if (header.audio_format == static_cast<int16_t>(0xfffe)) {
SHERPA_ONNX_LOGE("We don't support WAVE_FORMAT_EXTENSIBLE files.");
}
*is_ok = false;
return {};
}
is.read(reinterpret_cast<char *>(&header.num_channels),
sizeof(header.num_channels));
is.read(reinterpret_cast<char *>(&header.sample_rate),
sizeof(header.sample_rate));
is.read(reinterpret_cast<char *>(&header.byte_rate),
sizeof(header.byte_rate));
is.read(reinterpret_cast<char *>(&header.block_align),
sizeof(header.block_align));
is.read(reinterpret_cast<char *>(&header.bits_per_sample),
sizeof(header.bits_per_sample));
if (header.byte_rate !=
(header.sample_rate * header.num_channels * header.bits_per_sample / 8)) {
SHERPA_ONNX_LOGE("Incorrect byte rate: %d. Expected: %d", header.byte_rate,
(header.sample_rate * header.num_channels *
header.bits_per_sample / 8));
*is_ok = false;
return {};
}
if (header.block_align !=
(header.num_channels * header.bits_per_sample / 8)) {
SHERPA_ONNX_LOGE("Incorrect block align: %d. Expected: %d\n",
header.block_align,
(header.num_channels * header.bits_per_sample / 8));
*is_ok = false;
return {};
}
if (header.bits_per_sample != 8 && header.bits_per_sample != 16 &&
header.bits_per_sample != 32) {
SHERPA_ONNX_LOGE("Expected bits_per_sample 8, 16 or 32. Given: %d\n",
header.bits_per_sample);
*is_ok = false;
return {};
}
if (header.subchunk1_size == 18) {
// this is for NAudio. It puts extra bytes after bits_per_sample
// See
// https://github.com/naudio/NAudio/blob/master/NAudio.Core/Wave/WaveFormats/WaveFormat.cs#L223
int16_t extra_size = -1;
is.read(reinterpret_cast<char *>(&extra_size), sizeof(int16_t));
if (extra_size != 0) {
SHERPA_ONNX_LOGE(
"Extra size should be 0 for wave from NAudio. Current extra size "
"%d\n",
extra_size);
*is_ok = false;
return {};
}
}
is.read(reinterpret_cast<char *>(&header.subchunk2_id),
sizeof(header.subchunk2_id));
is.read(reinterpret_cast<char *>(&header.subchunk2_size),
sizeof(header.subchunk2_size));
header.SeekToDataChunk(is);
if (!is) {
*is_ok = false;
return {};
}
*sampling_rate = header.sample_rate;
std::vector<std::vector<float>> ans(header.num_channels);
if (header.bits_per_sample == 16 && header.audio_format == 1) {
// header.subchunk2_size contains the number of bytes in the data.
// As we assume each sample contains two bytes, so it is divided by 2 here
std::vector<int16_t> samples(header.subchunk2_size / 2);
is.read(reinterpret_cast<char *>(samples.data()), header.subchunk2_size);
if (!is) {
SHERPA_ONNX_LOGE("Failed to read %d bytes", header.subchunk2_size);
*is_ok = false;
return {};
}
for (auto &v : ans) {
v.resize(samples.size() / header.num_channels);
}
// samples are interleaved
for (int32_t i = 0, k = 0; i < static_cast<int32_t>(samples.size());
i += header.num_channels, ++k) {
for (int32_t c = 0; c != header.num_channels; ++c) {
ans[c][k] = samples[i + c] / 32768.;
}
}
} else if (header.bits_per_sample == 8 && header.audio_format == 1) {
// number of samples == number of bytes for 8-bit encoded samples
//
// For 8-bit encoded samples, they are unsigned!
std::vector<uint8_t> samples(header.subchunk2_size);
is.read(reinterpret_cast<char *>(samples.data()), header.subchunk2_size);
if (!is) {
SHERPA_ONNX_LOGE("Failed to read %d bytes", header.subchunk2_size);
*is_ok = false;
return {};
}
for (auto &v : ans) {
v.resize(samples.size() / header.num_channels);
}
// samples are interleaved
for (int32_t i = 0, k = 0; i < static_cast<int32_t>(samples.size());
i += header.num_channels, ++k) {
for (int32_t c = 0; c != header.num_channels; ++c) {
// Note(fangjun): We want to normalize each sample into the range [-1,
// 1] Since each original sample is in the range [0, 256], dividing them
// by 128 converts them to the range [0, 2]; so after subtracting 1, we
// get the range [-1, 1]
//
ans[c][k] = samples[i + c] / 128. - 1;
}
}
} else if (header.bits_per_sample == 32 && header.audio_format == 1) {
// 32 here is for int32
//
// header.subchunk2_size contains the number of bytes in the data.
// As we assume each sample contains 4 bytes, so it is divided by 4 here
std::vector<int32_t> samples(header.subchunk2_size / 4);
is.read(reinterpret_cast<char *>(samples.data()), header.subchunk2_size);
if (!is) {
SHERPA_ONNX_LOGE("Failed to read %d bytes", header.subchunk2_size);
*is_ok = false;
return {};
}
for (auto &v : ans) {
v.resize(samples.size() / header.num_channels);
}
// samples are interleaved
for (int32_t i = 0, k = 0; i < static_cast<int32_t>(samples.size());
i += header.num_channels, ++k) {
for (int32_t c = 0; c != header.num_channels; ++c) {
ans[c][k] = static_cast<float>(samples[i + c]) / (1 << 31);
}
}
} else if (header.bits_per_sample == 32 && header.audio_format == 3) {
// 32 here is for float32
//
// header.subchunk2_size contains the number of bytes in the data.
// As we assume each sample contains 4 bytes, so it is divided by 4 here
std::vector<float> samples(header.subchunk2_size / 4);
is.read(reinterpret_cast<char *>(samples.data()), header.subchunk2_size);
if (!is) {
SHERPA_ONNX_LOGE("Failed to read %d bytes", header.subchunk2_size);
*is_ok = false;
return {};
}
for (auto &v : ans) {
v.resize(samples.size() / header.num_channels);
}
// samples are interleaved
for (int32_t i = 0, k = 0; i < static_cast<int32_t>(samples.size());
i += header.num_channels, ++k) {
for (int32_t c = 0; c != header.num_channels; ++c) {
ans[c][k] = samples[i + c];
}
}
} else {
SHERPA_ONNX_LOGE(
"Unsupported %d bits per sample and audio format: %d. Supported values "
"are: 8, 16, 32.",
header.bits_per_sample, header.audio_format);
*is_ok = false;
return {};
}
*is_ok = true;
return ans;
}
} // namespace
std::vector<float> ReadWave(const std::string &filename, int32_t *sampling_rate,
bool *is_ok) {
std::ifstream is(filename, std::ifstream::binary);
return ReadWave(is, sampling_rate, is_ok);
}
std::vector<float> ReadWave(std::istream &is, int32_t *sampling_rate,
bool *is_ok) {
auto samples = ReadWaveImpl(is, sampling_rate, is_ok);
if (samples.size() > 1) {
SHERPA_ONNX_LOGE(
"Warning: %d channels are found. We only use the first channel.\n",
static_cast<int32_t>(samples.size()));
}
return samples[0];
}
std::vector<std::vector<float>> ReadWaveMultiChannel(std::istream &is,
int32_t *sampling_rate,
bool *is_ok) {
auto samples = ReadWaveImpl(is, sampling_rate, is_ok);
return samples;
}
std::vector<std::vector<float>> ReadWaveMultiChannel(
const std::string &filename, int32_t *sampling_rate, bool *is_ok) {
std::ifstream is(filename, std::ifstream::binary);
return ReadWaveMultiChannel(is, sampling_rate, is_ok);
}
} // namespace sherpa_onnx