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: |
| 37 | explicit ReadableWavFile(FILE* 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 { |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 41 | return fread(buf, 1, num_bytes, file_); |
| 42 | } |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 43 | bool SeekForward(uint32_t num_bytes) override { |
| 44 | return fseek(file_, num_bytes, SEEK_CUR) == 0; |
| 45 | } |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | FILE* file_; |
| 49 | }; |
| 50 | |
Alessio Bazzica | a33c7af | 2018-11-08 12:16:11 +0100 | [diff] [blame] | 51 | } // namespace |
| 52 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 53 | WavReader::WavReader(const std::string& filename) |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 54 | : WavReader(rtc::OpenPlatformFileReadOnly(filename)) {} |
| 55 | |
| 56 | WavReader::WavReader(rtc::PlatformFile file) { |
| 57 | RTC_CHECK_NE(file, rtc::kInvalidPlatformFileValue) |
| 58 | << "Invalid file. Could not create file handle for wav file."; |
| 59 | file_handle_ = rtc::FdopenPlatformFile(file, "rb"); |
| 60 | if (!file_handle_) { |
| 61 | RTC_LOG(LS_ERROR) << "Could not open wav file for reading: " << errno; |
| 62 | // Even though we failed to open a FILE*, the file is still open |
| 63 | // and needs to be closed. |
| 64 | if (!rtc::ClosePlatformFile(file)) { |
| 65 | RTC_LOG(LS_ERROR) << "Can't close file."; |
| 66 | } |
| 67 | FATAL() << "Could not open wav file for reading."; |
| 68 | } |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 69 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 70 | ReadableWavFile readable(file_handle_); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 71 | WavFormat format; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 72 | size_t bytes_per_sample; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 73 | RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, |
| 74 | &bytes_per_sample, &num_samples_)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 75 | num_samples_remaining_ = num_samples_; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 76 | RTC_CHECK_EQ(kWavFormat, format); |
| 77 | RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample); |
Artem Titov | 153056b | 2019-04-16 16:49:32 +0200 | [diff] [blame] | 78 | RTC_CHECK_EQ(0, fgetpos(file_handle_, &data_start_pos_)) |
| 79 | << "Failed to get WAV data position from file"; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | WavReader::~WavReader() { |
| 83 | Close(); |
| 84 | } |
| 85 | |
Artem Titov | 153056b | 2019-04-16 16:49:32 +0200 | [diff] [blame] | 86 | void WavReader::Reset() { |
| 87 | RTC_CHECK_EQ(0, fsetpos(file_handle_, &data_start_pos_)) |
| 88 | << "Failed to set position in the file to WAV data start position"; |
| 89 | num_samples_remaining_ = num_samples_; |
| 90 | } |
| 91 | |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 92 | int WavReader::sample_rate() const { |
| 93 | return sample_rate_; |
| 94 | } |
| 95 | |
| 96 | size_t WavReader::num_channels() const { |
| 97 | return num_channels_; |
| 98 | } |
| 99 | |
| 100 | size_t WavReader::num_samples() const { |
| 101 | return num_samples_; |
| 102 | } |
| 103 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 104 | size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { |
| 105 | #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 106 | #error "Need to convert samples to big-endian when reading from WAV file" |
| 107 | #endif |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 108 | // There could be metadata after the audio; ensure we don't read it. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 109 | num_samples = std::min(num_samples, num_samples_remaining_); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 110 | const size_t read = |
| 111 | fread(samples, sizeof(*samples), num_samples, file_handle_); |
| 112 | // 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] | 113 | RTC_CHECK(read == num_samples || feof(file_handle_)); |
| 114 | RTC_CHECK_LE(read, num_samples_remaining_); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 115 | num_samples_remaining_ -= read; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 116 | return read; |
| 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() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 134 | RTC_CHECK_EQ(0, fclose(file_handle_)); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 135 | file_handle_ = nullptr; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 138 | WavWriter::WavWriter(const std::string& filename, |
| 139 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 140 | size_t num_channels) |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 141 | // Unlike plain fopen, CreatePlatformFile takes care of filename utf8 -> |
| 142 | // wchar conversion on windows. |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 143 | : WavWriter(FileWrapper::OpenWriteOnly(filename), |
| 144 | sample_rate, |
| 145 | num_channels) {} |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 146 | |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 147 | WavWriter::WavWriter(FileWrapper file, int sample_rate, size_t num_channels) |
| 148 | : sample_rate_(sample_rate), |
| 149 | num_channels_(num_channels), |
| 150 | num_samples_(0), |
| 151 | file_(std::move(file)) { |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 152 | // Handle errors from the CreatePlatformFile call in above constructor. |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 153 | RTC_CHECK(file_.is_open()) << "Invalid file. Could not create wav file."; |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 154 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 155 | RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat, |
| 156 | kBytesPerSample, num_samples_)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 157 | |
| 158 | // Write a blank placeholder header, since we need to know the total number |
| 159 | // of samples before we can fill in the real data. |
| 160 | static const uint8_t blank_header[kWavHeaderSize] = {0}; |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 161 | RTC_CHECK(file_.Write(blank_header, kWavHeaderSize)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | WavWriter::~WavWriter() { |
| 165 | Close(); |
| 166 | } |
| 167 | |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 168 | int WavWriter::sample_rate() const { |
| 169 | return sample_rate_; |
| 170 | } |
| 171 | |
| 172 | size_t WavWriter::num_channels() const { |
| 173 | return num_channels_; |
| 174 | } |
| 175 | |
| 176 | size_t WavWriter::num_samples() const { |
| 177 | return num_samples_; |
| 178 | } |
| 179 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 180 | void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) { |
| 181 | #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 182 | #error "Need to convert samples to little-endian when writing to WAV file" |
| 183 | #endif |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 184 | RTC_CHECK(file_.Write(samples, sizeof(*samples) * num_samples)); |
| 185 | num_samples_ += num_samples; |
| 186 | RTC_CHECK(num_samples_ >= num_samples); // detect size_t overflow |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void WavWriter::WriteSamples(const float* samples, size_t num_samples) { |
| 190 | static const size_t kChunksize = 4096 / sizeof(uint16_t); |
| 191 | for (size_t i = 0; i < num_samples; i += kChunksize) { |
| 192 | int16_t isamples[kChunksize]; |
| 193 | const size_t chunk = std::min(kChunksize, num_samples - i); |
| 194 | FloatS16ToS16(samples + i, chunk, isamples); |
| 195 | WriteSamples(isamples, chunk); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void WavWriter::Close() { |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 200 | RTC_CHECK(file_.Rewind()); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 201 | uint8_t header[kWavHeaderSize]; |
andrew@webrtc.org | f866b2d | 2014-11-03 18:20:06 +0000 | [diff] [blame] | 202 | WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat, |
| 203 | kBytesPerSample, num_samples_); |
Niels Möller | 19a1d50 | 2019-06-13 14:08:24 +0200 | [diff] [blame] | 204 | RTC_CHECK(file_.Write(header, kWavHeaderSize)); |
| 205 | RTC_CHECK(file_.Close()); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | } // namespace webrtc |