forked from webrtc-sdk/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wav_file.cc
290 lines (247 loc) · 10.4 KB
/
wav_file.cc
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
/*
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "common_audio/wav_file.h"
#include <errno.h>
#include <algorithm>
#include <array>
#include <cstdio>
#include <type_traits>
#include <utility>
#include "common_audio/include/audio_util.h"
#include "rtc_base/checks.h"
#include "rtc_base/system/arch.h"
namespace webrtc {
namespace {
static_assert(std::is_trivially_destructible<WavFormat>::value, "");
// Checks whether the format is supported or not.
bool FormatSupported(WavFormat format) {
// Only PCM and IEEE Float formats are supported.
return format == WavFormat::kWavFormatPcm ||
format == WavFormat::kWavFormatIeeeFloat;
}
// Doesn't take ownership of the file handle and won't close it.
class WavHeaderFileReader : public WavHeaderReader {
public:
explicit WavHeaderFileReader(FileWrapper* file) : file_(file) {}
WavHeaderFileReader(const WavHeaderFileReader&) = delete;
WavHeaderFileReader& operator=(const WavHeaderFileReader&) = delete;
size_t Read(void* buf, size_t num_bytes) override {
size_t count = file_->Read(buf, num_bytes);
pos_ += count;
return count;
}
bool SeekForward(uint32_t num_bytes) override {
bool success = file_->SeekRelative(num_bytes);
if (success) {
pos_ += num_bytes;
}
return success;
}
int64_t GetPosition() override { return pos_; }
private:
FileWrapper* file_;
int64_t pos_ = 0;
};
constexpr size_t kMaxChunksize = 4096;
} // namespace
WavReader::WavReader(absl::string_view filename)
: WavReader(FileWrapper::OpenReadOnly(filename)) {}
WavReader::WavReader(FileWrapper file) : file_(std::move(file)) {
RTC_CHECK(file_.is_open())
<< "Invalid file. Could not create file handle for wav file.";
WavHeaderFileReader readable(&file_);
size_t bytes_per_sample;
RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format_,
&bytes_per_sample, &num_samples_in_file_,
&data_start_pos_));
num_unread_samples_ = num_samples_in_file_;
RTC_CHECK(FormatSupported(format_)) << "Non-implemented wav-format";
}
void WavReader::Reset() {
RTC_CHECK(file_.SeekTo(data_start_pos_))
<< "Failed to set position in the file to WAV data start position";
num_unread_samples_ = num_samples_in_file_;
}
size_t WavReader::ReadSamples(const size_t num_samples,
int16_t* const samples) {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to big-endian when reading from WAV file"
#endif
size_t num_samples_left_to_read = num_samples;
size_t next_chunk_start = 0;
while (num_samples_left_to_read > 0 && num_unread_samples_ > 0) {
const size_t chunk_size = std::min(
std::min(kMaxChunksize, num_samples_left_to_read), num_unread_samples_);
size_t num_bytes_read;
size_t num_samples_read;
if (format_ == WavFormat::kWavFormatIeeeFloat) {
std::array<float, kMaxChunksize> samples_to_convert;
num_bytes_read = file_.Read(samples_to_convert.data(),
chunk_size * sizeof(samples_to_convert[0]));
num_samples_read = num_bytes_read / sizeof(samples_to_convert[0]);
for (size_t j = 0; j < num_samples_read; ++j) {
samples[next_chunk_start + j] = FloatToS16(samples_to_convert[j]);
}
} else {
RTC_CHECK_EQ(format_, WavFormat::kWavFormatPcm);
num_bytes_read = file_.Read(&samples[next_chunk_start],
chunk_size * sizeof(samples[0]));
num_samples_read = num_bytes_read / sizeof(samples[0]);
}
RTC_CHECK(num_samples_read == 0 || (num_bytes_read % num_samples_read) == 0)
<< "Corrupt file: file ended in the middle of a sample.";
RTC_CHECK(num_samples_read == chunk_size || file_.ReadEof())
<< "Corrupt file: payload size does not match header.";
next_chunk_start += num_samples_read;
num_unread_samples_ -= num_samples_read;
num_samples_left_to_read -= num_samples_read;
}
return num_samples - num_samples_left_to_read;
}
size_t WavReader::ReadSamples(const size_t num_samples, float* const samples) {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to big-endian when reading from WAV file"
#endif
size_t num_samples_left_to_read = num_samples;
size_t next_chunk_start = 0;
while (num_samples_left_to_read > 0 && num_unread_samples_ > 0) {
const size_t chunk_size = std::min(
std::min(kMaxChunksize, num_samples_left_to_read), num_unread_samples_);
size_t num_bytes_read;
size_t num_samples_read;
if (format_ == WavFormat::kWavFormatPcm) {
std::array<int16_t, kMaxChunksize> samples_to_convert;
num_bytes_read = file_.Read(samples_to_convert.data(),
chunk_size * sizeof(samples_to_convert[0]));
num_samples_read = num_bytes_read / sizeof(samples_to_convert[0]);
for (size_t j = 0; j < num_samples_read; ++j) {
samples[next_chunk_start + j] =
static_cast<float>(samples_to_convert[j]);
}
} else {
RTC_CHECK_EQ(format_, WavFormat::kWavFormatIeeeFloat);
num_bytes_read = file_.Read(&samples[next_chunk_start],
chunk_size * sizeof(samples[0]));
num_samples_read = num_bytes_read / sizeof(samples[0]);
for (size_t j = 0; j < num_samples_read; ++j) {
samples[next_chunk_start + j] =
FloatToFloatS16(samples[next_chunk_start + j]);
}
}
RTC_CHECK(num_samples_read == 0 || (num_bytes_read % num_samples_read) == 0)
<< "Corrupt file: file ended in the middle of a sample.";
RTC_CHECK(num_samples_read == chunk_size || file_.ReadEof())
<< "Corrupt file: payload size does not match header.";
next_chunk_start += num_samples_read;
num_unread_samples_ -= num_samples_read;
num_samples_left_to_read -= num_samples_read;
}
return num_samples - num_samples_left_to_read;
}
void WavReader::Close() {
file_.Close();
}
WavWriter::WavWriter(absl::string_view filename,
int sample_rate,
size_t num_channels,
SampleFormat sample_format)
// Unlike plain fopen, OpenWriteOnly takes care of filename utf8 ->
// wchar conversion on windows.
: WavWriter(FileWrapper::OpenWriteOnly(filename),
sample_rate,
num_channels,
sample_format) {}
WavWriter::WavWriter(FileWrapper file,
int sample_rate,
size_t num_channels,
SampleFormat sample_format)
: sample_rate_(sample_rate),
num_channels_(num_channels),
num_samples_written_(0),
format_(sample_format == SampleFormat::kInt16
? WavFormat::kWavFormatPcm
: WavFormat::kWavFormatIeeeFloat),
file_(std::move(file)) {
// Handle errors from the OpenWriteOnly call in above constructor.
RTC_CHECK(file_.is_open()) << "Invalid file. Could not create wav file.";
RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, format_,
num_samples_written_));
// Write a blank placeholder header, since we need to know the total number
// of samples before we can fill in the real data.
static const uint8_t blank_header[MaxWavHeaderSize()] = {0};
RTC_CHECK(file_.Write(blank_header, WavHeaderSize(format_)));
}
void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to little-endian when writing to WAV file"
#endif
for (size_t i = 0; i < num_samples; i += kMaxChunksize) {
const size_t num_remaining_samples = num_samples - i;
const size_t num_samples_to_write =
std::min(kMaxChunksize, num_remaining_samples);
if (format_ == WavFormat::kWavFormatPcm) {
RTC_CHECK(
file_.Write(&samples[i], num_samples_to_write * sizeof(samples[0])));
} else {
RTC_CHECK_EQ(format_, WavFormat::kWavFormatIeeeFloat);
std::array<float, kMaxChunksize> converted_samples;
for (size_t j = 0; j < num_samples_to_write; ++j) {
converted_samples[j] = S16ToFloat(samples[i + j]);
}
RTC_CHECK(
file_.Write(converted_samples.data(),
num_samples_to_write * sizeof(converted_samples[0])));
}
num_samples_written_ += num_samples_to_write;
RTC_CHECK_GE(num_samples_written_,
num_samples_to_write); // detect size_t overflow
}
}
void WavWriter::WriteSamples(const float* samples, size_t num_samples) {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to little-endian when writing to WAV file"
#endif
for (size_t i = 0; i < num_samples; i += kMaxChunksize) {
const size_t num_remaining_samples = num_samples - i;
const size_t num_samples_to_write =
std::min(kMaxChunksize, num_remaining_samples);
if (format_ == WavFormat::kWavFormatPcm) {
std::array<int16_t, kMaxChunksize> converted_samples;
for (size_t j = 0; j < num_samples_to_write; ++j) {
converted_samples[j] = FloatS16ToS16(samples[i + j]);
}
RTC_CHECK(
file_.Write(converted_samples.data(),
num_samples_to_write * sizeof(converted_samples[0])));
} else {
RTC_CHECK_EQ(format_, WavFormat::kWavFormatIeeeFloat);
std::array<float, kMaxChunksize> converted_samples;
for (size_t j = 0; j < num_samples_to_write; ++j) {
converted_samples[j] = FloatS16ToFloat(samples[i + j]);
}
RTC_CHECK(
file_.Write(converted_samples.data(),
num_samples_to_write * sizeof(converted_samples[0])));
}
num_samples_written_ += num_samples_to_write;
RTC_CHECK(num_samples_written_ >=
num_samples_to_write); // detect size_t overflow
}
}
void WavWriter::Close() {
RTC_CHECK(file_.Rewind());
std::array<uint8_t, MaxWavHeaderSize()> header;
size_t header_size;
WriteWavHeader(num_channels_, sample_rate_, format_, num_samples_written_,
header.data(), &header_size);
RTC_CHECK(file_.Write(header.data(), header_size));
RTC_CHECK(file_.Close());
}
} // namespace webrtc