blob: 1b8bcbd923cb537389194bf0f7ac5c8a9e4ecc37 [file] [log] [blame]
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +00001/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "common_audio/wav_file.h"
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <errno.h>
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000014#include <algorithm>
15#include <cstdio>
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +010016#include <type_traits>
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/include/audio_util.h"
19#include "common_audio/wav_header.h"
20#include "rtc_base/checks.h"
Artem Titove62f6002018-03-19 15:40:00 +010021#include "rtc_base/logging.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020022#include "rtc_base/system/arch.h"
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000023
24namespace webrtc {
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +010025namespace {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000026
27// We write 16-bit PCM WAV files.
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +010028constexpr WavFormat kWavFormat = kWavFormatPcm;
29static_assert(std::is_trivially_destructible<WavFormat>::value, "");
30constexpr size_t kBytesPerSample = 2;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000031
andrew@webrtc.org048c5022014-12-16 20:17:21 +000032// Doesn't take ownership of the file handle and won't close it.
33class ReadableWavFile : public ReadableWav {
34 public:
35 explicit ReadableWavFile(FILE* file) : file_(file) {}
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +010036 ReadableWavFile(const ReadableWavFile&) = delete;
37 ReadableWavFile& operator=(const ReadableWavFile&) = delete;
Mirko Bonadei91df0912018-07-17 11:08:15 +020038 size_t Read(void* buf, size_t num_bytes) override {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000039 return fread(buf, 1, num_bytes, file_);
40 }
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +010041 bool Eof() const override { return feof(file_) != 0; }
42 bool SeekForward(uint32_t num_bytes) override {
43 return fseek(file_, num_bytes, SEEK_CUR) == 0;
44 }
andrew@webrtc.org048c5022014-12-16 20:17:21 +000045
46 private:
47 FILE* file_;
48};
49
Alessio Bazzicaa33c7af2018-11-08 12:16:11 +010050} // namespace
51
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000052WavReader::WavReader(const std::string& filename)
Artem Titove62f6002018-03-19 15:40:00 +010053 : WavReader(rtc::OpenPlatformFileReadOnly(filename)) {}
54
55WavReader::WavReader(rtc::PlatformFile file) {
56 RTC_CHECK_NE(file, rtc::kInvalidPlatformFileValue)
57 << "Invalid file. Could not create file handle for wav file.";
58 file_handle_ = rtc::FdopenPlatformFile(file, "rb");
59 if (!file_handle_) {
60 RTC_LOG(LS_ERROR) << "Could not open wav file for reading: " << errno;
61 // Even though we failed to open a FILE*, the file is still open
62 // and needs to be closed.
63 if (!rtc::ClosePlatformFile(file)) {
64 RTC_LOG(LS_ERROR) << "Can't close file.";
65 }
66 FATAL() << "Could not open wav file for reading.";
67 }
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000068
andrew@webrtc.org048c5022014-12-16 20:17:21 +000069 ReadableWavFile readable(file_handle_);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000070 WavFormat format;
pkasting25702cb2016-01-08 13:50:27 -080071 size_t bytes_per_sample;
henrikg91d6ede2015-09-17 00:24:34 -070072 RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format,
73 &bytes_per_sample, &num_samples_));
andrew@webrtc.org048c5022014-12-16 20:17:21 +000074 num_samples_remaining_ = num_samples_;
henrikg91d6ede2015-09-17 00:24:34 -070075 RTC_CHECK_EQ(kWavFormat, format);
76 RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000077}
78
79WavReader::~WavReader() {
80 Close();
81}
82
peahd1f718b2016-02-22 02:13:28 -080083int WavReader::sample_rate() const {
84 return sample_rate_;
85}
86
87size_t WavReader::num_channels() const {
88 return num_channels_;
89}
90
91size_t WavReader::num_samples() const {
92 return num_samples_;
93}
94
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000095size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) {
96#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
97#error "Need to convert samples to big-endian when reading from WAV file"
98#endif
andrew@webrtc.org048c5022014-12-16 20:17:21 +000099 // There could be metadata after the audio; ensure we don't read it.
pkasting25702cb2016-01-08 13:50:27 -0800100 num_samples = std::min(num_samples, num_samples_remaining_);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000101 const size_t read =
102 fread(samples, sizeof(*samples), num_samples, file_handle_);
103 // If we didn't read what was requested, ensure we've reached the EOF.
henrikg91d6ede2015-09-17 00:24:34 -0700104 RTC_CHECK(read == num_samples || feof(file_handle_));
105 RTC_CHECK_LE(read, num_samples_remaining_);
pkasting25702cb2016-01-08 13:50:27 -0800106 num_samples_remaining_ -= read;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000107 return read;
108}
109
110size_t WavReader::ReadSamples(size_t num_samples, float* samples) {
111 static const size_t kChunksize = 4096 / sizeof(uint16_t);
112 size_t read = 0;
113 for (size_t i = 0; i < num_samples; i += kChunksize) {
114 int16_t isamples[kChunksize];
115 size_t chunk = std::min(kChunksize, num_samples - i);
116 chunk = ReadSamples(chunk, isamples);
117 for (size_t j = 0; j < chunk; ++j)
118 samples[i + j] = isamples[j];
119 read += chunk;
120 }
121 return read;
122}
123
124void WavReader::Close() {
henrikg91d6ede2015-09-17 00:24:34 -0700125 RTC_CHECK_EQ(0, fclose(file_handle_));
deadbeef922246a2017-02-26 04:18:12 -0800126 file_handle_ = nullptr;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000127}
128
Artem Titove62f6002018-03-19 15:40:00 +0100129WavWriter::WavWriter(const std::string& filename,
130 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800131 size_t num_channels)
Artem Titove62f6002018-03-19 15:40:00 +0100132 // Unlike plain fopen, CreatePlatformFile takes care of filename utf8 ->
133 // wchar conversion on windows.
134 : WavWriter(rtc::CreatePlatformFile(filename), sample_rate, num_channels) {}
135
136WavWriter::WavWriter(rtc::PlatformFile file,
137 int sample_rate,
138 size_t num_channels)
139 : sample_rate_(sample_rate), num_channels_(num_channels), num_samples_(0) {
140 // Handle errors from the CreatePlatformFile call in above constructor.
141 RTC_CHECK_NE(file, rtc::kInvalidPlatformFileValue)
142 << "Invalid file. Could not create wav file.";
143 file_handle_ = rtc::FdopenPlatformFile(file, "wb");
144 if (!file_handle_) {
145 RTC_LOG(LS_ERROR) << "Could not open wav file for writing.";
146 // Even though we failed to open a FILE*, the file is still open
147 // and needs to be closed.
148 if (!rtc::ClosePlatformFile(file)) {
149 RTC_LOG(LS_ERROR) << "Can't close file.";
150 }
151 FATAL() << "Could not open wav file for writing.";
152 }
153
henrikg91d6ede2015-09-17 00:24:34 -0700154 RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat,
155 kBytesPerSample, num_samples_));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000156
157 // Write a blank placeholder header, since we need to know the total number
158 // of samples before we can fill in the real data.
159 static const uint8_t blank_header[kWavHeaderSize] = {0};
kwibergaf476c72016-11-28 15:21:39 -0800160 RTC_CHECK_EQ(1, fwrite(blank_header, kWavHeaderSize, 1, file_handle_));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000161}
162
163WavWriter::~WavWriter() {
164 Close();
165}
166
peahd1f718b2016-02-22 02:13:28 -0800167int WavWriter::sample_rate() const {
168 return sample_rate_;
169}
170
171size_t WavWriter::num_channels() const {
172 return num_channels_;
173}
174
175size_t WavWriter::num_samples() const {
176 return num_samples_;
177}
178
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000179void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) {
180#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
181#error "Need to convert samples to little-endian when writing to WAV file"
182#endif
183 const size_t written =
184 fwrite(samples, sizeof(*samples), num_samples, file_handle_);
henrikg91d6ede2015-09-17 00:24:34 -0700185 RTC_CHECK_EQ(num_samples, written);
pkasting25702cb2016-01-08 13:50:27 -0800186 num_samples_ += written;
187 RTC_CHECK(num_samples_ >= written); // detect size_t overflow
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000188}
189
190void WavWriter::WriteSamples(const float* samples, size_t num_samples) {
191 static const size_t kChunksize = 4096 / sizeof(uint16_t);
192 for (size_t i = 0; i < num_samples; i += kChunksize) {
193 int16_t isamples[kChunksize];
194 const size_t chunk = std::min(kChunksize, num_samples - i);
195 FloatS16ToS16(samples + i, chunk, isamples);
196 WriteSamples(isamples, chunk);
197 }
198}
199
200void WavWriter::Close() {
henrikg91d6ede2015-09-17 00:24:34 -0700201 RTC_CHECK_EQ(0, fseek(file_handle_, 0, SEEK_SET));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000202 uint8_t header[kWavHeaderSize];
andrew@webrtc.orgf866b2d2014-11-03 18:20:06 +0000203 WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat,
204 kBytesPerSample, num_samples_);
kwibergaf476c72016-11-28 15:21:39 -0800205 RTC_CHECK_EQ(1, fwrite(header, kWavHeaderSize, 1, file_handle_));
henrikg91d6ede2015-09-17 00:24:34 -0700206 RTC_CHECK_EQ(0, fclose(file_handle_));
deadbeef922246a2017-02-26 04:18:12 -0800207 file_handle_ = nullptr;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000208}
209
210} // namespace webrtc
211
212rtc_WavWriter* rtc_WavOpen(const char* filename,
213 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800214 size_t num_channels) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000215 return reinterpret_cast<rtc_WavWriter*>(
216 new webrtc::WavWriter(filename, sample_rate, num_channels));
217}
218
219void rtc_WavClose(rtc_WavWriter* wf) {
220 delete reinterpret_cast<webrtc::WavWriter*>(wf);
221}
222
223void rtc_WavWriteSamples(rtc_WavWriter* wf,
224 const float* samples,
225 size_t num_samples) {
226 reinterpret_cast<webrtc::WavWriter*>(wf)->WriteSamples(samples, num_samples);
227}
228
229int rtc_WavSampleRate(const rtc_WavWriter* wf) {
230 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate();
231}
232
Peter Kasting69558702016-01-12 16:26:35 -0800233size_t rtc_WavNumChannels(const rtc_WavWriter* wf) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000234 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels();
235}
236
pkasting25702cb2016-01-08 13:50:27 -0800237size_t rtc_WavNumSamples(const rtc_WavWriter* wf) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000238 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples();
239}