blob: 37f249e7fa890a7840a3df21786f4d04fd11b9f0 [file] [log] [blame]
andrew@webrtc.orga3ed7132014-10-31 21:51:031/*
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 04:47:3111#include "common_audio/wav_file.h"
andrew@webrtc.orga3ed7132014-10-31 21:51:0312
13#include <algorithm>
14#include <cstdio>
15#include <limits>
aluebsb0ad43b2015-11-20 08:11:5316#include <sstream>
andrew@webrtc.orga3ed7132014-10-31 21:51:0317
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "common_audio/include/audio_util.h"
19#include "common_audio/wav_header.h"
20#include "rtc_base/checks.h"
Karl Wiberge40468b2017-11-22 09:42:2621#include "rtc_base/numerics/safe_conversions.h"
andrew@webrtc.orga3ed7132014-10-31 21:51:0322
23namespace webrtc {
24
25// We write 16-bit PCM WAV files.
26static const WavFormat kWavFormat = kWavFormatPcm;
pkasting25702cb2016-01-08 21:50:2727static const size_t kBytesPerSample = 2;
andrew@webrtc.orga3ed7132014-10-31 21:51:0328
andrew@webrtc.org048c5022014-12-16 20:17:2129// 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 08:11:5341std::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:0349WavReader::WavReader(const std::string& filename)
50 : file_handle_(fopen(filename.c_str(), "rb")) {
aluebsb0ad43b2015-11-20 08:11:5351 RTC_CHECK(file_handle_) << "Could not open wav file for reading.";
andrew@webrtc.orga3ed7132014-10-31 21:51:0352
andrew@webrtc.org048c5022014-12-16 20:17:2153 ReadableWavFile readable(file_handle_);
andrew@webrtc.orga3ed7132014-10-31 21:51:0354 WavFormat format;
pkasting25702cb2016-01-08 21:50:2755 size_t bytes_per_sample;
henrikg91d6ede2015-09-17 07:24:3456 RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format,
57 &bytes_per_sample, &num_samples_));
andrew@webrtc.org048c5022014-12-16 20:17:2158 num_samples_remaining_ = num_samples_;
henrikg91d6ede2015-09-17 07:24:3459 RTC_CHECK_EQ(kWavFormat, format);
60 RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample);
andrew@webrtc.orga3ed7132014-10-31 21:51:0361}
62
63WavReader::~WavReader() {
64 Close();
65}
66
peahd1f718b2016-02-22 10:13:2867int 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:0379size_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:2183 // There could be metadata after the audio; ensure we don't read it.
pkasting25702cb2016-01-08 21:50:2784 num_samples = std::min(num_samples, num_samples_remaining_);
andrew@webrtc.orga3ed7132014-10-31 21:51:0385 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 07:24:3488 RTC_CHECK(read == num_samples || feof(file_handle_));
89 RTC_CHECK_LE(read, num_samples_remaining_);
pkasting25702cb2016-01-08 21:50:2790 num_samples_remaining_ -= read;
andrew@webrtc.orga3ed7132014-10-31 21:51:0391 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 07:24:34109 RTC_CHECK_EQ(0, fclose(file_handle_));
deadbeef922246a2017-02-26 12:18:12110 file_handle_ = nullptr;
andrew@webrtc.orga3ed7132014-10-31 21:51:03111}
112
113WavWriter::WavWriter(const std::string& filename, int sample_rate,
Peter Kasting69558702016-01-13 00:26:35114 size_t num_channels)
andrew@webrtc.orga3ed7132014-10-31 21:51:03115 : sample_rate_(sample_rate),
116 num_channels_(num_channels),
117 num_samples_(0),
118 file_handle_(fopen(filename.c_str(), "wb")) {
aluebsb0ad43b2015-11-20 08:11:53119 RTC_CHECK(file_handle_) << "Could not open wav file for writing.";
henrikg91d6ede2015-09-17 07:24:34120 RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat,
121 kBytesPerSample, num_samples_));
andrew@webrtc.orga3ed7132014-10-31 21:51:03122
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 23:21:39126 RTC_CHECK_EQ(1, fwrite(blank_header, kWavHeaderSize, 1, file_handle_));
andrew@webrtc.orga3ed7132014-10-31 21:51:03127}
128
129WavWriter::~WavWriter() {
130 Close();
131}
132
peahd1f718b2016-02-22 10:13:28133int 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:03145void 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 07:24:34151 RTC_CHECK_EQ(num_samples, written);
pkasting25702cb2016-01-08 21:50:27152 num_samples_ += written;
153 RTC_CHECK(num_samples_ >= written); // detect size_t overflow
andrew@webrtc.orga3ed7132014-10-31 21:51:03154}
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 07:24:34167 RTC_CHECK_EQ(0, fseek(file_handle_, 0, SEEK_SET));
andrew@webrtc.orga3ed7132014-10-31 21:51:03168 uint8_t header[kWavHeaderSize];
andrew@webrtc.orgf866b2d2014-11-03 18:20:06169 WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat,
170 kBytesPerSample, num_samples_);
kwibergaf476c72016-11-28 23:21:39171 RTC_CHECK_EQ(1, fwrite(header, kWavHeaderSize, 1, file_handle_));
henrikg91d6ede2015-09-17 07:24:34172 RTC_CHECK_EQ(0, fclose(file_handle_));
deadbeef922246a2017-02-26 12:18:12173 file_handle_ = nullptr;
andrew@webrtc.orga3ed7132014-10-31 21:51:03174}
175
176} // namespace webrtc
177
178rtc_WavWriter* rtc_WavOpen(const char* filename,
179 int sample_rate,
Peter Kasting69558702016-01-13 00:26:35180 size_t num_channels) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03181 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-13 00:26:35199size_t rtc_WavNumChannels(const rtc_WavWriter* wf) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03200 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels();
201}
202
pkasting25702cb2016-01-08 21:50:27203size_t rtc_WavNumSamples(const rtc_WavWriter* wf) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03204 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples();
205}