blob: ac11bcdd7b0a59db39eee30c9853e90dad711ffe [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
11#include "webrtc/common_audio/wav_file.h"
12
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
18#include "webrtc/base/checks.h"
andrew@webrtc.org0ab42bc2014-12-17 22:56:09 +000019#include "webrtc/base/safe_conversions.h"
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000020#include "webrtc/common_audio/include/audio_util.h"
21#include "webrtc/common_audio/wav_header.h"
22
23namespace webrtc {
24
25// We write 16-bit PCM WAV files.
26static const WavFormat kWavFormat = kWavFormatPcm;
27static const int kBytesPerSample = 2;
28
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;
55 int 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
67size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) {
68#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
69#error "Need to convert samples to big-endian when reading from WAV file"
70#endif
andrew@webrtc.org048c5022014-12-16 20:17:21 +000071 // There could be metadata after the audio; ensure we don't read it.
andrew@webrtc.org0ab42bc2014-12-17 22:56:09 +000072 num_samples = std::min(rtc::checked_cast<uint32_t>(num_samples),
andrew@webrtc.org048c5022014-12-16 20:17:21 +000073 num_samples_remaining_);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000074 const size_t read =
75 fread(samples, sizeof(*samples), num_samples, file_handle_);
76 // If we didn't read what was requested, ensure we've reached the EOF.
henrikg91d6ede2015-09-17 00:24:34 -070077 RTC_CHECK(read == num_samples || feof(file_handle_));
78 RTC_CHECK_LE(read, num_samples_remaining_);
andrew@webrtc.org0ab42bc2014-12-17 22:56:09 +000079 num_samples_remaining_ -= rtc::checked_cast<uint32_t>(read);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000080 return read;
81}
82
83size_t WavReader::ReadSamples(size_t num_samples, float* samples) {
84 static const size_t kChunksize = 4096 / sizeof(uint16_t);
85 size_t read = 0;
86 for (size_t i = 0; i < num_samples; i += kChunksize) {
87 int16_t isamples[kChunksize];
88 size_t chunk = std::min(kChunksize, num_samples - i);
89 chunk = ReadSamples(chunk, isamples);
90 for (size_t j = 0; j < chunk; ++j)
91 samples[i + j] = isamples[j];
92 read += chunk;
93 }
94 return read;
95}
96
97void WavReader::Close() {
henrikg91d6ede2015-09-17 00:24:34 -070098 RTC_CHECK_EQ(0, fclose(file_handle_));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000099 file_handle_ = NULL;
100}
101
102WavWriter::WavWriter(const std::string& filename, int sample_rate,
103 int num_channels)
104 : sample_rate_(sample_rate),
105 num_channels_(num_channels),
106 num_samples_(0),
107 file_handle_(fopen(filename.c_str(), "wb")) {
aluebsb0ad43b2015-11-20 00:11:53 -0800108 RTC_CHECK(file_handle_) << "Could not open wav file for writing.";
henrikg91d6ede2015-09-17 00:24:34 -0700109 RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat,
110 kBytesPerSample, num_samples_));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000111
112 // Write a blank placeholder header, since we need to know the total number
113 // of samples before we can fill in the real data.
114 static const uint8_t blank_header[kWavHeaderSize] = {0};
henrikg91d6ede2015-09-17 00:24:34 -0700115 RTC_CHECK_EQ(1u, fwrite(blank_header, kWavHeaderSize, 1, file_handle_));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000116}
117
118WavWriter::~WavWriter() {
119 Close();
120}
121
122void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) {
123#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
124#error "Need to convert samples to little-endian when writing to WAV file"
125#endif
126 const size_t written =
127 fwrite(samples, sizeof(*samples), num_samples, file_handle_);
henrikg91d6ede2015-09-17 00:24:34 -0700128 RTC_CHECK_EQ(num_samples, written);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000129 num_samples_ += static_cast<uint32_t>(written);
henrikg91d6ede2015-09-17 00:24:34 -0700130 RTC_CHECK(written <= std::numeric_limits<uint32_t>::max() ||
131 num_samples_ >= written); // detect uint32_t overflow
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000132}
133
134void WavWriter::WriteSamples(const float* samples, size_t num_samples) {
135 static const size_t kChunksize = 4096 / sizeof(uint16_t);
136 for (size_t i = 0; i < num_samples; i += kChunksize) {
137 int16_t isamples[kChunksize];
138 const size_t chunk = std::min(kChunksize, num_samples - i);
139 FloatS16ToS16(samples + i, chunk, isamples);
140 WriteSamples(isamples, chunk);
141 }
142}
143
144void WavWriter::Close() {
henrikg91d6ede2015-09-17 00:24:34 -0700145 RTC_CHECK_EQ(0, fseek(file_handle_, 0, SEEK_SET));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000146 uint8_t header[kWavHeaderSize];
andrew@webrtc.orgf866b2d2014-11-03 18:20:06 +0000147 WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat,
148 kBytesPerSample, num_samples_);
henrikg91d6ede2015-09-17 00:24:34 -0700149 RTC_CHECK_EQ(1u, fwrite(header, kWavHeaderSize, 1, file_handle_));
150 RTC_CHECK_EQ(0, fclose(file_handle_));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000151 file_handle_ = NULL;
152}
153
154} // namespace webrtc
155
156rtc_WavWriter* rtc_WavOpen(const char* filename,
157 int sample_rate,
158 int num_channels) {
159 return reinterpret_cast<rtc_WavWriter*>(
160 new webrtc::WavWriter(filename, sample_rate, num_channels));
161}
162
163void rtc_WavClose(rtc_WavWriter* wf) {
164 delete reinterpret_cast<webrtc::WavWriter*>(wf);
165}
166
167void rtc_WavWriteSamples(rtc_WavWriter* wf,
168 const float* samples,
169 size_t num_samples) {
170 reinterpret_cast<webrtc::WavWriter*>(wf)->WriteSamples(samples, num_samples);
171}
172
173int rtc_WavSampleRate(const rtc_WavWriter* wf) {
174 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate();
175}
176
177int rtc_WavNumChannels(const rtc_WavWriter* wf) {
178 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels();
179}
180
181uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf) {
182 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples();
183}