blob: 48dff3bd3e09dabe7965e9aacb6bc2372a0f38db [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>
aluebsb0ad43b2015-11-20 00:11:53 -080016#include <sstream>
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"
21#include "rtc_base/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) {}
33 virtual size_t Read(void* buf, size_t num_bytes) {
34 return fread(buf, 1, num_bytes, file_);
35 }
36
37 private:
38 FILE* file_;
39};
40
aluebsb0ad43b2015-11-20 00:11:53 -080041std::string WavFile::FormatAsString() const {
42 std::ostringstream s;
43 s << "Sample rate: " << sample_rate() << " Hz, Channels: " << num_channels()
44 << ", Duration: "
45 << (1.f * num_samples()) / (num_channels() * sample_rate()) << " s";
46 return s.str();
47}
48
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000049WavReader::WavReader(const std::string& filename)
50 : file_handle_(fopen(filename.c_str(), "rb")) {
aluebsb0ad43b2015-11-20 00:11:53 -080051 RTC_CHECK(file_handle_) << "Could not open wav file for reading.";
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000052
andrew@webrtc.org048c5022014-12-16 20:17:21 +000053 ReadableWavFile readable(file_handle_);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000054 WavFormat format;
pkasting25702cb2016-01-08 13:50:27 -080055 size_t bytes_per_sample;
henrikg91d6ede2015-09-17 00:24:34 -070056 RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format,
57 &bytes_per_sample, &num_samples_));
andrew@webrtc.org048c5022014-12-16 20:17:21 +000058 num_samples_remaining_ = num_samples_;
henrikg91d6ede2015-09-17 00:24:34 -070059 RTC_CHECK_EQ(kWavFormat, format);
60 RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000061}
62
63WavReader::~WavReader() {
64 Close();
65}
66
peahd1f718b2016-02-22 02:13:28 -080067int WavReader::sample_rate() const {
68 return sample_rate_;
69}
70
71size_t WavReader::num_channels() const {
72 return num_channels_;
73}
74
75size_t WavReader::num_samples() const {
76 return num_samples_;
77}
78
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000079size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) {
80#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
81#error "Need to convert samples to big-endian when reading from WAV file"
82#endif
andrew@webrtc.org048c5022014-12-16 20:17:21 +000083 // There could be metadata after the audio; ensure we don't read it.
pkasting25702cb2016-01-08 13:50:27 -080084 num_samples = std::min(num_samples, num_samples_remaining_);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000085 const size_t read =
86 fread(samples, sizeof(*samples), num_samples, file_handle_);
87 // If we didn't read what was requested, ensure we've reached the EOF.
henrikg91d6ede2015-09-17 00:24:34 -070088 RTC_CHECK(read == num_samples || feof(file_handle_));
89 RTC_CHECK_LE(read, num_samples_remaining_);
pkasting25702cb2016-01-08 13:50:27 -080090 num_samples_remaining_ -= read;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000091 return read;
92}
93
94size_t WavReader::ReadSamples(size_t num_samples, float* samples) {
95 static const size_t kChunksize = 4096 / sizeof(uint16_t);
96 size_t read = 0;
97 for (size_t i = 0; i < num_samples; i += kChunksize) {
98 int16_t isamples[kChunksize];
99 size_t chunk = std::min(kChunksize, num_samples - i);
100 chunk = ReadSamples(chunk, isamples);
101 for (size_t j = 0; j < chunk; ++j)
102 samples[i + j] = isamples[j];
103 read += chunk;
104 }
105 return read;
106}
107
108void WavReader::Close() {
henrikg91d6ede2015-09-17 00:24:34 -0700109 RTC_CHECK_EQ(0, fclose(file_handle_));
deadbeef922246a2017-02-26 04:18:12 -0800110 file_handle_ = nullptr;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000111}
112
113WavWriter::WavWriter(const std::string& filename, int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800114 size_t num_channels)
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000115 : sample_rate_(sample_rate),
116 num_channels_(num_channels),
117 num_samples_(0),
118 file_handle_(fopen(filename.c_str(), "wb")) {
aluebsb0ad43b2015-11-20 00:11:53 -0800119 RTC_CHECK(file_handle_) << "Could not open wav file for writing.";
henrikg91d6ede2015-09-17 00:24:34 -0700120 RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat,
121 kBytesPerSample, num_samples_));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000122
123 // Write a blank placeholder header, since we need to know the total number
124 // of samples before we can fill in the real data.
125 static const uint8_t blank_header[kWavHeaderSize] = {0};
kwibergaf476c72016-11-28 15:21:39 -0800126 RTC_CHECK_EQ(1, fwrite(blank_header, kWavHeaderSize, 1, file_handle_));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000127}
128
129WavWriter::~WavWriter() {
130 Close();
131}
132
peahd1f718b2016-02-22 02:13:28 -0800133int WavWriter::sample_rate() const {
134 return sample_rate_;
135}
136
137size_t WavWriter::num_channels() const {
138 return num_channels_;
139}
140
141size_t WavWriter::num_samples() const {
142 return num_samples_;
143}
144
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000145void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) {
146#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
147#error "Need to convert samples to little-endian when writing to WAV file"
148#endif
149 const size_t written =
150 fwrite(samples, sizeof(*samples), num_samples, file_handle_);
henrikg91d6ede2015-09-17 00:24:34 -0700151 RTC_CHECK_EQ(num_samples, written);
pkasting25702cb2016-01-08 13:50:27 -0800152 num_samples_ += written;
153 RTC_CHECK(num_samples_ >= written); // detect size_t overflow
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000154}
155
156void WavWriter::WriteSamples(const float* samples, size_t num_samples) {
157 static const size_t kChunksize = 4096 / sizeof(uint16_t);
158 for (size_t i = 0; i < num_samples; i += kChunksize) {
159 int16_t isamples[kChunksize];
160 const size_t chunk = std::min(kChunksize, num_samples - i);
161 FloatS16ToS16(samples + i, chunk, isamples);
162 WriteSamples(isamples, chunk);
163 }
164}
165
166void WavWriter::Close() {
henrikg91d6ede2015-09-17 00:24:34 -0700167 RTC_CHECK_EQ(0, fseek(file_handle_, 0, SEEK_SET));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000168 uint8_t header[kWavHeaderSize];
andrew@webrtc.orgf866b2d2014-11-03 18:20:06 +0000169 WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat,
170 kBytesPerSample, num_samples_);
kwibergaf476c72016-11-28 15:21:39 -0800171 RTC_CHECK_EQ(1, fwrite(header, kWavHeaderSize, 1, file_handle_));
henrikg91d6ede2015-09-17 00:24:34 -0700172 RTC_CHECK_EQ(0, fclose(file_handle_));
deadbeef922246a2017-02-26 04:18:12 -0800173 file_handle_ = nullptr;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000174}
175
176} // namespace webrtc
177
178rtc_WavWriter* rtc_WavOpen(const char* filename,
179 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800180 size_t num_channels) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000181 return reinterpret_cast<rtc_WavWriter*>(
182 new webrtc::WavWriter(filename, sample_rate, num_channels));
183}
184
185void rtc_WavClose(rtc_WavWriter* wf) {
186 delete reinterpret_cast<webrtc::WavWriter*>(wf);
187}
188
189void rtc_WavWriteSamples(rtc_WavWriter* wf,
190 const float* samples,
191 size_t num_samples) {
192 reinterpret_cast<webrtc::WavWriter*>(wf)->WriteSamples(samples, num_samples);
193}
194
195int rtc_WavSampleRate(const rtc_WavWriter* wf) {
196 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate();
197}
198
Peter Kasting69558702016-01-12 16:26:35 -0800199size_t rtc_WavNumChannels(const rtc_WavWriter* wf) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000200 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels();
201}
202
pkasting25702cb2016-01-08 13:50:27 -0800203size_t rtc_WavNumSamples(const rtc_WavWriter* wf) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000204 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples();
205}