andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "common_audio/wav_file.h" |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <cstdio> |
| 15 | #include <limits> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "common_audio/include/audio_util.h" |
| 18 | #include "common_audio/wav_header.h" |
| 19 | #include "rtc_base/checks.h" |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 20 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 21 | #include "rtc_base/numerics/safe_conversions.h" |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | // We write 16-bit PCM WAV files. |
| 26 | static const WavFormat kWavFormat = kWavFormatPcm; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 27 | static const size_t kBytesPerSample = 2; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 28 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 29 | // Doesn't take ownership of the file handle and won't close it. |
| 30 | class ReadableWavFile : public ReadableWav { |
| 31 | public: |
| 32 | explicit ReadableWavFile(FILE* file) : file_(file) {} |
Mirko Bonadei | 91df091 | 2018-07-17 11:08:15 +0200 | [diff] [blame] | 33 | size_t Read(void* buf, size_t num_bytes) override { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 34 | return fread(buf, 1, num_bytes, file_); |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | FILE* file_; |
| 39 | }; |
| 40 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 41 | WavReader::WavReader(const std::string& filename) |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 42 | : WavReader(rtc::OpenPlatformFileReadOnly(filename)) {} |
| 43 | |
| 44 | WavReader::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.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 57 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 58 | ReadableWavFile readable(file_handle_); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 59 | WavFormat format; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 60 | size_t bytes_per_sample; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 61 | RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, |
| 62 | &bytes_per_sample, &num_samples_)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 63 | num_samples_remaining_ = num_samples_; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 64 | RTC_CHECK_EQ(kWavFormat, format); |
| 65 | RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | WavReader::~WavReader() { |
| 69 | Close(); |
| 70 | } |
| 71 | |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 72 | int WavReader::sample_rate() const { |
| 73 | return sample_rate_; |
| 74 | } |
| 75 | |
| 76 | size_t WavReader::num_channels() const { |
| 77 | return num_channels_; |
| 78 | } |
| 79 | |
| 80 | size_t WavReader::num_samples() const { |
| 81 | return num_samples_; |
| 82 | } |
| 83 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 84 | size_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.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 88 | // There could be metadata after the audio; ensure we don't read it. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 89 | num_samples = std::min(num_samples, num_samples_remaining_); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 90 | 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. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 93 | RTC_CHECK(read == num_samples || feof(file_handle_)); |
| 94 | RTC_CHECK_LE(read, num_samples_remaining_); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 95 | num_samples_remaining_ -= read; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 96 | return read; |
| 97 | } |
| 98 | |
| 99 | size_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 | |
| 113 | void WavReader::Close() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 114 | RTC_CHECK_EQ(0, fclose(file_handle_)); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 115 | file_handle_ = nullptr; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 118 | WavWriter::WavWriter(const std::string& filename, |
| 119 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 120 | size_t num_channels) |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 121 | // Unlike plain fopen, CreatePlatformFile takes care of filename utf8 -> |
| 122 | // wchar conversion on windows. |
| 123 | : WavWriter(rtc::CreatePlatformFile(filename), sample_rate, num_channels) {} |
| 124 | |
| 125 | WavWriter::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 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 143 | RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat, |
| 144 | kBytesPerSample, num_samples_)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 145 | |
| 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}; |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 149 | RTC_CHECK_EQ(1, fwrite(blank_header, kWavHeaderSize, 1, file_handle_)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | WavWriter::~WavWriter() { |
| 153 | Close(); |
| 154 | } |
| 155 | |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 156 | int WavWriter::sample_rate() const { |
| 157 | return sample_rate_; |
| 158 | } |
| 159 | |
| 160 | size_t WavWriter::num_channels() const { |
| 161 | return num_channels_; |
| 162 | } |
| 163 | |
| 164 | size_t WavWriter::num_samples() const { |
| 165 | return num_samples_; |
| 166 | } |
| 167 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 168 | void 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_); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 174 | RTC_CHECK_EQ(num_samples, written); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 175 | num_samples_ += written; |
| 176 | RTC_CHECK(num_samples_ >= written); // detect size_t overflow |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void 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 | |
| 189 | void WavWriter::Close() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 190 | RTC_CHECK_EQ(0, fseek(file_handle_, 0, SEEK_SET)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 191 | uint8_t header[kWavHeaderSize]; |
andrew@webrtc.org | f866b2d | 2014-11-03 18:20:06 +0000 | [diff] [blame] | 192 | WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat, |
| 193 | kBytesPerSample, num_samples_); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 194 | RTC_CHECK_EQ(1, fwrite(header, kWavHeaderSize, 1, file_handle_)); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 195 | RTC_CHECK_EQ(0, fclose(file_handle_)); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 196 | file_handle_ = nullptr; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | } // namespace webrtc |
| 200 | |
| 201 | rtc_WavWriter* rtc_WavOpen(const char* filename, |
| 202 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 203 | size_t num_channels) { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 204 | return reinterpret_cast<rtc_WavWriter*>( |
| 205 | new webrtc::WavWriter(filename, sample_rate, num_channels)); |
| 206 | } |
| 207 | |
| 208 | void rtc_WavClose(rtc_WavWriter* wf) { |
| 209 | delete reinterpret_cast<webrtc::WavWriter*>(wf); |
| 210 | } |
| 211 | |
| 212 | void 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 | |
| 218 | int rtc_WavSampleRate(const rtc_WavWriter* wf) { |
| 219 | return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate(); |
| 220 | } |
| 221 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 222 | size_t rtc_WavNumChannels(const rtc_WavWriter* wf) { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 223 | return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); |
| 224 | } |
| 225 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 226 | size_t rtc_WavNumSamples(const rtc_WavWriter* wf) { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 227 | return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); |
| 228 | } |