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 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <errno.h> |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | #include <cstdio> |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame^] | 16 | #include <type_traits> |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "common_audio/include/audio_util.h" |
| 19 | #include "common_audio/wav_header.h" |
| 20 | #include "rtc_base/checks.h" |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 21 | #include "rtc_base/logging.h" |
Niels Möller | a12c42a | 2018-07-25 16:05:48 +0200 | [diff] [blame] | 22 | #include "rtc_base/system/arch.h" |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame^] | 25 | namespace { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 26 | |
| 27 | // We write 16-bit PCM WAV files. |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame^] | 28 | constexpr WavFormat kWavFormat = kWavFormatPcm; |
| 29 | static_assert(std::is_trivially_destructible<WavFormat>::value, ""); |
| 30 | constexpr size_t kBytesPerSample = 2; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 31 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 32 | // Doesn't take ownership of the file handle and won't close it. |
| 33 | class ReadableWavFile : public ReadableWav { |
| 34 | public: |
| 35 | explicit ReadableWavFile(FILE* file) : file_(file) {} |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame^] | 36 | ReadableWavFile(const ReadableWavFile&) = delete; |
| 37 | ReadableWavFile& operator=(const ReadableWavFile&) = delete; |
Mirko Bonadei | 91df091 | 2018-07-17 11:08:15 +0200 | [diff] [blame] | 38 | size_t Read(void* buf, size_t num_bytes) override { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 39 | return fread(buf, 1, num_bytes, file_); |
| 40 | } |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame^] | 41 | bool Eof() const override { return feof(file_) != 0; } |
| 42 | bool SeekForward(uint32_t num_bytes) override { |
| 43 | return fseek(file_, num_bytes, SEEK_CUR) == 0; |
| 44 | } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 45 | |
| 46 | private: |
| 47 | FILE* file_; |
| 48 | }; |
| 49 | |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame^] | 50 | } // namespace |
| 51 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 52 | WavReader::WavReader(const std::string& filename) |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 53 | : WavReader(rtc::OpenPlatformFileReadOnly(filename)) {} |
| 54 | |
| 55 | WavReader::WavReader(rtc::PlatformFile file) { |
| 56 | RTC_CHECK_NE(file, rtc::kInvalidPlatformFileValue) |
| 57 | << "Invalid file. Could not create file handle for wav file."; |
| 58 | file_handle_ = rtc::FdopenPlatformFile(file, "rb"); |
| 59 | if (!file_handle_) { |
| 60 | RTC_LOG(LS_ERROR) << "Could not open wav file for reading: " << errno; |
| 61 | // Even though we failed to open a FILE*, the file is still open |
| 62 | // and needs to be closed. |
| 63 | if (!rtc::ClosePlatformFile(file)) { |
| 64 | RTC_LOG(LS_ERROR) << "Can't close file."; |
| 65 | } |
| 66 | FATAL() << "Could not open wav file for reading."; |
| 67 | } |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 68 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 69 | ReadableWavFile readable(file_handle_); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 70 | WavFormat format; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 71 | size_t bytes_per_sample; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 72 | RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, |
| 73 | &bytes_per_sample, &num_samples_)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 74 | num_samples_remaining_ = num_samples_; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 75 | RTC_CHECK_EQ(kWavFormat, format); |
| 76 | RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | WavReader::~WavReader() { |
| 80 | Close(); |
| 81 | } |
| 82 | |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 83 | int WavReader::sample_rate() const { |
| 84 | return sample_rate_; |
| 85 | } |
| 86 | |
| 87 | size_t WavReader::num_channels() const { |
| 88 | return num_channels_; |
| 89 | } |
| 90 | |
| 91 | size_t WavReader::num_samples() const { |
| 92 | return num_samples_; |
| 93 | } |
| 94 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 95 | size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { |
| 96 | #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 97 | #error "Need to convert samples to big-endian when reading from WAV file" |
| 98 | #endif |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 99 | // There could be metadata after the audio; ensure we don't read it. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 100 | num_samples = std::min(num_samples, num_samples_remaining_); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 101 | const size_t read = |
| 102 | fread(samples, sizeof(*samples), num_samples, file_handle_); |
| 103 | // 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] | 104 | RTC_CHECK(read == num_samples || feof(file_handle_)); |
| 105 | RTC_CHECK_LE(read, num_samples_remaining_); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 106 | num_samples_remaining_ -= read; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 107 | return read; |
| 108 | } |
| 109 | |
| 110 | size_t WavReader::ReadSamples(size_t num_samples, float* samples) { |
| 111 | static const size_t kChunksize = 4096 / sizeof(uint16_t); |
| 112 | size_t read = 0; |
| 113 | for (size_t i = 0; i < num_samples; i += kChunksize) { |
| 114 | int16_t isamples[kChunksize]; |
| 115 | size_t chunk = std::min(kChunksize, num_samples - i); |
| 116 | chunk = ReadSamples(chunk, isamples); |
| 117 | for (size_t j = 0; j < chunk; ++j) |
| 118 | samples[i + j] = isamples[j]; |
| 119 | read += chunk; |
| 120 | } |
| 121 | return read; |
| 122 | } |
| 123 | |
| 124 | void WavReader::Close() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 125 | RTC_CHECK_EQ(0, fclose(file_handle_)); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 126 | file_handle_ = nullptr; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 129 | WavWriter::WavWriter(const std::string& filename, |
| 130 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 131 | size_t num_channels) |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 132 | // Unlike plain fopen, CreatePlatformFile takes care of filename utf8 -> |
| 133 | // wchar conversion on windows. |
| 134 | : WavWriter(rtc::CreatePlatformFile(filename), sample_rate, num_channels) {} |
| 135 | |
| 136 | WavWriter::WavWriter(rtc::PlatformFile file, |
| 137 | int sample_rate, |
| 138 | size_t num_channels) |
| 139 | : sample_rate_(sample_rate), num_channels_(num_channels), num_samples_(0) { |
| 140 | // Handle errors from the CreatePlatformFile call in above constructor. |
| 141 | RTC_CHECK_NE(file, rtc::kInvalidPlatformFileValue) |
| 142 | << "Invalid file. Could not create wav file."; |
| 143 | file_handle_ = rtc::FdopenPlatformFile(file, "wb"); |
| 144 | if (!file_handle_) { |
| 145 | RTC_LOG(LS_ERROR) << "Could not open wav file for writing."; |
| 146 | // Even though we failed to open a FILE*, the file is still open |
| 147 | // and needs to be closed. |
| 148 | if (!rtc::ClosePlatformFile(file)) { |
| 149 | RTC_LOG(LS_ERROR) << "Can't close file."; |
| 150 | } |
| 151 | FATAL() << "Could not open wav file for writing."; |
| 152 | } |
| 153 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 154 | RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat, |
| 155 | kBytesPerSample, num_samples_)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 156 | |
| 157 | // Write a blank placeholder header, since we need to know the total number |
| 158 | // of samples before we can fill in the real data. |
| 159 | static const uint8_t blank_header[kWavHeaderSize] = {0}; |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 160 | RTC_CHECK_EQ(1, fwrite(blank_header, kWavHeaderSize, 1, file_handle_)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | WavWriter::~WavWriter() { |
| 164 | Close(); |
| 165 | } |
| 166 | |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 167 | int WavWriter::sample_rate() const { |
| 168 | return sample_rate_; |
| 169 | } |
| 170 | |
| 171 | size_t WavWriter::num_channels() const { |
| 172 | return num_channels_; |
| 173 | } |
| 174 | |
| 175 | size_t WavWriter::num_samples() const { |
| 176 | return num_samples_; |
| 177 | } |
| 178 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 179 | void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) { |
| 180 | #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 181 | #error "Need to convert samples to little-endian when writing to WAV file" |
| 182 | #endif |
| 183 | const size_t written = |
| 184 | fwrite(samples, sizeof(*samples), num_samples, file_handle_); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 185 | RTC_CHECK_EQ(num_samples, written); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 186 | num_samples_ += written; |
| 187 | RTC_CHECK(num_samples_ >= written); // detect size_t overflow |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void WavWriter::WriteSamples(const float* samples, size_t num_samples) { |
| 191 | static const size_t kChunksize = 4096 / sizeof(uint16_t); |
| 192 | for (size_t i = 0; i < num_samples; i += kChunksize) { |
| 193 | int16_t isamples[kChunksize]; |
| 194 | const size_t chunk = std::min(kChunksize, num_samples - i); |
| 195 | FloatS16ToS16(samples + i, chunk, isamples); |
| 196 | WriteSamples(isamples, chunk); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void WavWriter::Close() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 201 | RTC_CHECK_EQ(0, fseek(file_handle_, 0, SEEK_SET)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 202 | uint8_t header[kWavHeaderSize]; |
andrew@webrtc.org | f866b2d | 2014-11-03 18:20:06 +0000 | [diff] [blame] | 203 | WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat, |
| 204 | kBytesPerSample, num_samples_); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 205 | RTC_CHECK_EQ(1, fwrite(header, kWavHeaderSize, 1, file_handle_)); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 206 | RTC_CHECK_EQ(0, fclose(file_handle_)); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 207 | file_handle_ = nullptr; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | } // namespace webrtc |
| 211 | |
| 212 | rtc_WavWriter* rtc_WavOpen(const char* filename, |
| 213 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 214 | size_t num_channels) { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 215 | return reinterpret_cast<rtc_WavWriter*>( |
| 216 | new webrtc::WavWriter(filename, sample_rate, num_channels)); |
| 217 | } |
| 218 | |
| 219 | void rtc_WavClose(rtc_WavWriter* wf) { |
| 220 | delete reinterpret_cast<webrtc::WavWriter*>(wf); |
| 221 | } |
| 222 | |
| 223 | void rtc_WavWriteSamples(rtc_WavWriter* wf, |
| 224 | const float* samples, |
| 225 | size_t num_samples) { |
| 226 | reinterpret_cast<webrtc::WavWriter*>(wf)->WriteSamples(samples, num_samples); |
| 227 | } |
| 228 | |
| 229 | int rtc_WavSampleRate(const rtc_WavWriter* wf) { |
| 230 | return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate(); |
| 231 | } |
| 232 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 233 | size_t rtc_WavNumChannels(const rtc_WavWriter* wf) { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 234 | return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); |
| 235 | } |
| 236 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 237 | size_t rtc_WavNumSamples(const rtc_WavWriter* wf) { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 238 | return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); |
| 239 | } |