blob: 21b64a8d3041e26d9dba137db0fc21e6e361cae0 [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
13#include <algorithm>
14#include <cstdio>
15#include <limits>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "common_audio/include/audio_util.h"
18#include "common_audio/wav_header.h"
19#include "rtc_base/checks.h"
Artem Titove62f6002018-03-19 15:40:00 +010020#include "rtc_base/logging.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010021#include "rtc_base/numerics/safe_conversions.h"
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000022
23namespace webrtc {
24
25// We write 16-bit PCM WAV files.
26static const WavFormat kWavFormat = kWavFormatPcm;
pkasting25702cb2016-01-08 13:50:27 -080027static const size_t kBytesPerSample = 2;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000028
andrew@webrtc.org048c5022014-12-16 20:17:21 +000029// Doesn't take ownership of the file handle and won't close it.
30class ReadableWavFile : public ReadableWav {
31 public:
32 explicit ReadableWavFile(FILE* file) : file_(file) {}
Mirko Bonadei91df0912018-07-17 11:08:15 +020033 size_t Read(void* buf, size_t num_bytes) override {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000034 return fread(buf, 1, num_bytes, file_);
35 }
36
37 private:
38 FILE* file_;
39};
40
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000041WavReader::WavReader(const std::string& filename)
Artem Titove62f6002018-03-19 15:40:00 +010042 : WavReader(rtc::OpenPlatformFileReadOnly(filename)) {}
43
44WavReader::WavReader(rtc::PlatformFile file) {
45 RTC_CHECK_NE(file, rtc::kInvalidPlatformFileValue)
46 << "Invalid file. Could not create file handle for wav file.";
47 file_handle_ = rtc::FdopenPlatformFile(file, "rb");
48 if (!file_handle_) {
49 RTC_LOG(LS_ERROR) << "Could not open wav file for reading: " << errno;
50 // Even though we failed to open a FILE*, the file is still open
51 // and needs to be closed.
52 if (!rtc::ClosePlatformFile(file)) {
53 RTC_LOG(LS_ERROR) << "Can't close file.";
54 }
55 FATAL() << "Could not open wav file for reading.";
56 }
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000057
andrew@webrtc.org048c5022014-12-16 20:17:21 +000058 ReadableWavFile readable(file_handle_);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000059 WavFormat format;
pkasting25702cb2016-01-08 13:50:27 -080060 size_t bytes_per_sample;
henrikg91d6ede2015-09-17 00:24:34 -070061 RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format,
62 &bytes_per_sample, &num_samples_));
andrew@webrtc.org048c5022014-12-16 20:17:21 +000063 num_samples_remaining_ = num_samples_;
henrikg91d6ede2015-09-17 00:24:34 -070064 RTC_CHECK_EQ(kWavFormat, format);
65 RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000066}
67
68WavReader::~WavReader() {
69 Close();
70}
71
peahd1f718b2016-02-22 02:13:28 -080072int WavReader::sample_rate() const {
73 return sample_rate_;
74}
75
76size_t WavReader::num_channels() const {
77 return num_channels_;
78}
79
80size_t WavReader::num_samples() const {
81 return num_samples_;
82}
83
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000084size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) {
85#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
86#error "Need to convert samples to big-endian when reading from WAV file"
87#endif
andrew@webrtc.org048c5022014-12-16 20:17:21 +000088 // There could be metadata after the audio; ensure we don't read it.
pkasting25702cb2016-01-08 13:50:27 -080089 num_samples = std::min(num_samples, num_samples_remaining_);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000090 const size_t read =
91 fread(samples, sizeof(*samples), num_samples, file_handle_);
92 // If we didn't read what was requested, ensure we've reached the EOF.
henrikg91d6ede2015-09-17 00:24:34 -070093 RTC_CHECK(read == num_samples || feof(file_handle_));
94 RTC_CHECK_LE(read, num_samples_remaining_);
pkasting25702cb2016-01-08 13:50:27 -080095 num_samples_remaining_ -= read;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000096 return read;
97}
98
99size_t WavReader::ReadSamples(size_t num_samples, float* samples) {
100 static const size_t kChunksize = 4096 / sizeof(uint16_t);
101 size_t read = 0;
102 for (size_t i = 0; i < num_samples; i += kChunksize) {
103 int16_t isamples[kChunksize];
104 size_t chunk = std::min(kChunksize, num_samples - i);
105 chunk = ReadSamples(chunk, isamples);
106 for (size_t j = 0; j < chunk; ++j)
107 samples[i + j] = isamples[j];
108 read += chunk;
109 }
110 return read;
111}
112
113void WavReader::Close() {
henrikg91d6ede2015-09-17 00:24:34 -0700114 RTC_CHECK_EQ(0, fclose(file_handle_));
deadbeef922246a2017-02-26 04:18:12 -0800115 file_handle_ = nullptr;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000116}
117
Artem Titove62f6002018-03-19 15:40:00 +0100118WavWriter::WavWriter(const std::string& filename,
119 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800120 size_t num_channels)
Artem Titove62f6002018-03-19 15:40:00 +0100121 // Unlike plain fopen, CreatePlatformFile takes care of filename utf8 ->
122 // wchar conversion on windows.
123 : WavWriter(rtc::CreatePlatformFile(filename), sample_rate, num_channels) {}
124
125WavWriter::WavWriter(rtc::PlatformFile file,
126 int sample_rate,
127 size_t num_channels)
128 : sample_rate_(sample_rate), num_channels_(num_channels), num_samples_(0) {
129 // Handle errors from the CreatePlatformFile call in above constructor.
130 RTC_CHECK_NE(file, rtc::kInvalidPlatformFileValue)
131 << "Invalid file. Could not create wav file.";
132 file_handle_ = rtc::FdopenPlatformFile(file, "wb");
133 if (!file_handle_) {
134 RTC_LOG(LS_ERROR) << "Could not open wav file for writing.";
135 // Even though we failed to open a FILE*, the file is still open
136 // and needs to be closed.
137 if (!rtc::ClosePlatformFile(file)) {
138 RTC_LOG(LS_ERROR) << "Can't close file.";
139 }
140 FATAL() << "Could not open wav file for writing.";
141 }
142
henrikg91d6ede2015-09-17 00:24:34 -0700143 RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat,
144 kBytesPerSample, num_samples_));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000145
146 // Write a blank placeholder header, since we need to know the total number
147 // of samples before we can fill in the real data.
148 static const uint8_t blank_header[kWavHeaderSize] = {0};
kwibergaf476c72016-11-28 15:21:39 -0800149 RTC_CHECK_EQ(1, fwrite(blank_header, kWavHeaderSize, 1, file_handle_));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000150}
151
152WavWriter::~WavWriter() {
153 Close();
154}
155
peahd1f718b2016-02-22 02:13:28 -0800156int WavWriter::sample_rate() const {
157 return sample_rate_;
158}
159
160size_t WavWriter::num_channels() const {
161 return num_channels_;
162}
163
164size_t WavWriter::num_samples() const {
165 return num_samples_;
166}
167
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000168void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) {
169#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
170#error "Need to convert samples to little-endian when writing to WAV file"
171#endif
172 const size_t written =
173 fwrite(samples, sizeof(*samples), num_samples, file_handle_);
henrikg91d6ede2015-09-17 00:24:34 -0700174 RTC_CHECK_EQ(num_samples, written);
pkasting25702cb2016-01-08 13:50:27 -0800175 num_samples_ += written;
176 RTC_CHECK(num_samples_ >= written); // detect size_t overflow
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000177}
178
179void WavWriter::WriteSamples(const float* samples, size_t num_samples) {
180 static const size_t kChunksize = 4096 / sizeof(uint16_t);
181 for (size_t i = 0; i < num_samples; i += kChunksize) {
182 int16_t isamples[kChunksize];
183 const size_t chunk = std::min(kChunksize, num_samples - i);
184 FloatS16ToS16(samples + i, chunk, isamples);
185 WriteSamples(isamples, chunk);
186 }
187}
188
189void WavWriter::Close() {
henrikg91d6ede2015-09-17 00:24:34 -0700190 RTC_CHECK_EQ(0, fseek(file_handle_, 0, SEEK_SET));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000191 uint8_t header[kWavHeaderSize];
andrew@webrtc.orgf866b2d2014-11-03 18:20:06 +0000192 WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat,
193 kBytesPerSample, num_samples_);
kwibergaf476c72016-11-28 15:21:39 -0800194 RTC_CHECK_EQ(1, fwrite(header, kWavHeaderSize, 1, file_handle_));
henrikg91d6ede2015-09-17 00:24:34 -0700195 RTC_CHECK_EQ(0, fclose(file_handle_));
deadbeef922246a2017-02-26 04:18:12 -0800196 file_handle_ = nullptr;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000197}
198
199} // namespace webrtc
200
201rtc_WavWriter* rtc_WavOpen(const char* filename,
202 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800203 size_t num_channels) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000204 return reinterpret_cast<rtc_WavWriter*>(
205 new webrtc::WavWriter(filename, sample_rate, num_channels));
206}
207
208void rtc_WavClose(rtc_WavWriter* wf) {
209 delete reinterpret_cast<webrtc::WavWriter*>(wf);
210}
211
212void rtc_WavWriteSamples(rtc_WavWriter* wf,
213 const float* samples,
214 size_t num_samples) {
215 reinterpret_cast<webrtc::WavWriter*>(wf)->WriteSamples(samples, num_samples);
216}
217
218int rtc_WavSampleRate(const rtc_WavWriter* wf) {
219 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate();
220}
221
Peter Kasting69558702016-01-12 16:26:35 -0800222size_t rtc_WavNumChannels(const rtc_WavWriter* wf) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000223 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels();
224}
225
pkasting25702cb2016-01-08 13:50:27 -0800226size_t rtc_WavNumSamples(const rtc_WavWriter* wf) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000227 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples();
228}