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> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 15 | #include <algorithm> |
| 16 | #include <cstdio> |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 17 | #include <type_traits> |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 18 | #include <utility> |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "common_audio/include/audio_util.h" |
| 21 | #include "common_audio/wav_header.h" |
| 22 | #include "rtc_base/checks.h" |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 23 | #include "rtc_base/logging.h" |
Niels Möller | a12c42a | 2018-07-25 16:05:48 +0200 | [diff] [blame] | 24 | #include "rtc_base/system/arch.h" |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 27 | namespace { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 28 | |
| 29 | // We write 16-bit PCM WAV files. |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 30 | constexpr WavFormat kWavFormat = kWavFormatPcm; |
| 31 | static_assert(std::is_trivially_destructible<WavFormat>::value, ""); |
| 32 | constexpr size_t kBytesPerSample = 2; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 33 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 34 | // Doesn't take ownership of the file handle and won't close it. |
| 35 | class ReadableWavFile : public ReadableWav { |
| 36 | public: |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 37 | explicit ReadableWavFile(FileWrapper* file) : file_(file) {} |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 38 | ReadableWavFile(const ReadableWavFile&) = delete; |
| 39 | ReadableWavFile& operator=(const ReadableWavFile&) = delete; |
Mirko Bonadei | 91df091 | 2018-07-17 11:08:15 +0200 | [diff] [blame] | 40 | size_t Read(void* buf, size_t num_bytes) override { |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 41 | size_t count = file_->Read(buf, num_bytes); |
| 42 | pos_ += count; |
| 43 | return count; |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 44 | } |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 45 | bool SeekForward(uint32_t num_bytes) override { |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 46 | bool success = file_->SeekRelative(num_bytes); |
| 47 | if (success) { |
| 48 | pos_ += num_bytes; |
| 49 | } |
| 50 | return success; |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 51 | } |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 52 | int64_t GetPosition() { return pos_; } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 53 | |
| 54 | private: |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 55 | FileWrapper* file_; |
| 56 | int64_t pos_ = 0; |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 59 | } // namespace |
| 60 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 61 | WavReader::WavReader(const std::string& filename) |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 62 | : WavReader(FileWrapper::OpenReadOnly(filename)) {} |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 63 | |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 64 | WavReader::WavReader(FileWrapper file) : file_(std::move(file)) { |
| 65 | RTC_CHECK(file_.is_open()) |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 66 | << "Invalid file. Could not create file handle for wav file."; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 67 | |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 68 | ReadableWavFile readable(&file_); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 69 | WavFormat format; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 70 | size_t bytes_per_sample; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 71 | RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, |
| 72 | &bytes_per_sample, &num_samples_)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 73 | num_samples_remaining_ = num_samples_; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 74 | RTC_CHECK_EQ(kWavFormat, format); |
| 75 | RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample); |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 76 | data_start_pos_ = readable.GetPosition(); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | WavReader::~WavReader() { |
| 80 | Close(); |
| 81 | } |
| 82 | |
Artem Titov | 153056b | 2019-04-16 16:49:32 +0200 | [diff] [blame] | 83 | void WavReader::Reset() { |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 84 | RTC_CHECK(file_.SeekTo(data_start_pos_)) |
Artem Titov | 153056b | 2019-04-16 16:49:32 +0200 | [diff] [blame] | 85 | << "Failed to set position in the file to WAV data start position"; |
| 86 | num_samples_remaining_ = num_samples_; |
| 87 | } |
| 88 | |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 89 | int WavReader::sample_rate() const { |
| 90 | return sample_rate_; |
| 91 | } |
| 92 | |
| 93 | size_t WavReader::num_channels() const { |
| 94 | return num_channels_; |
| 95 | } |
| 96 | |
| 97 | size_t WavReader::num_samples() const { |
| 98 | return num_samples_; |
| 99 | } |
| 100 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 101 | size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { |
| 102 | #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 103 | #error "Need to convert samples to big-endian when reading from WAV file" |
| 104 | #endif |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 105 | // There could be metadata after the audio; ensure we don't read it. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 106 | num_samples = std::min(num_samples, num_samples_remaining_); |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 107 | const size_t num_bytes = num_samples * sizeof(*samples); |
| 108 | const size_t read_bytes = file_.Read(samples, num_bytes); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 109 | // If we didn't read what was requested, ensure we've reached the EOF. |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 110 | RTC_CHECK(read_bytes == num_bytes || file_.ReadEof()); |
| 111 | RTC_CHECK_EQ(read_bytes % 2, 0) |
| 112 | << "End of file in the middle of a 16-bit sample"; |
| 113 | const size_t read_samples = read_bytes / 2; |
| 114 | RTC_CHECK_LE(read_samples, num_samples_remaining_); |
| 115 | num_samples_remaining_ -= read_samples; |
| 116 | return read_samples; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | size_t WavReader::ReadSamples(size_t num_samples, float* samples) { |
| 120 | static const size_t kChunksize = 4096 / sizeof(uint16_t); |
| 121 | size_t read = 0; |
| 122 | for (size_t i = 0; i < num_samples; i += kChunksize) { |
| 123 | int16_t isamples[kChunksize]; |
| 124 | size_t chunk = std::min(kChunksize, num_samples - i); |
| 125 | chunk = ReadSamples(chunk, isamples); |
| 126 | for (size_t j = 0; j < chunk; ++j) |
| 127 | samples[i + j] = isamples[j]; |
| 128 | read += chunk; |
| 129 | } |
| 130 | return read; |
| 131 | } |
| 132 | |
| 133 | void WavReader::Close() { |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 134 | file_.Close(); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 137 | WavWriter::WavWriter(const std::string& filename, |
| 138 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 139 | size_t num_channels) |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 140 | // Unlike plain fopen, OpenWriteOnly takes care of filename utf8 -> |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 141 | // wchar conversion on windows. |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 142 | : WavWriter(FileWrapper::OpenWriteOnly(filename), |
| 143 | sample_rate, |
| 144 | num_channels) {} |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 145 | |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 146 | WavWriter::WavWriter(FileWrapper file, int sample_rate, size_t num_channels) |
| 147 | : sample_rate_(sample_rate), |
| 148 | num_channels_(num_channels), |
| 149 | num_samples_(0), |
| 150 | file_(std::move(file)) { |
Niels Möller | 7ba3b81 | 2019-08-06 09:58:56 +0200 | [diff] [blame] | 151 | // Handle errors from the OpenWriteOnly call in above constructor. |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 152 | RTC_CHECK(file_.is_open()) << "Invalid file. Could not create wav file."; |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 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}; |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 160 | RTC_CHECK(file_.Write(blank_header, kWavHeaderSize)); |
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 |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 183 | RTC_CHECK(file_.Write(samples, sizeof(*samples) * num_samples)); |
| 184 | num_samples_ += num_samples; |
| 185 | RTC_CHECK(num_samples_ >= num_samples); // detect size_t overflow |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void WavWriter::WriteSamples(const float* samples, size_t num_samples) { |
| 189 | static const size_t kChunksize = 4096 / sizeof(uint16_t); |
| 190 | for (size_t i = 0; i < num_samples; i += kChunksize) { |
| 191 | int16_t isamples[kChunksize]; |
| 192 | const size_t chunk = std::min(kChunksize, num_samples - i); |
| 193 | FloatS16ToS16(samples + i, chunk, isamples); |
| 194 | WriteSamples(isamples, chunk); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void WavWriter::Close() { |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 199 | RTC_CHECK(file_.Rewind()); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 200 | uint8_t header[kWavHeaderSize]; |
andrew@webrtc.org | f866b2d | 2014-11-03 18:20:06 +0000 | [diff] [blame] | 201 | WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat, |
| 202 | kBytesPerSample, num_samples_); |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 203 | RTC_CHECK(file_.Write(header, kWavHeaderSize)); |
| 204 | RTC_CHECK(file_.Close()); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | } // namespace webrtc |