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> |
aluebs | b0ad43b | 2015-11-20 00:11:53 -0800 | [diff] [blame] | 16 | #include <sstream> |
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" |
| 21 | #include "rtc_base/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) {} |
| 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 | |
aluebs | b0ad43b | 2015-11-20 00:11:53 -0800 | [diff] [blame] | 41 | std::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.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 49 | WavReader::WavReader(const std::string& filename) |
| 50 | : file_handle_(fopen(filename.c_str(), "rb")) { |
aluebs | b0ad43b | 2015-11-20 00:11:53 -0800 | [diff] [blame] | 51 | RTC_CHECK(file_handle_) << "Could not open wav file for reading."; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 52 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 53 | ReadableWavFile readable(file_handle_); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 54 | WavFormat format; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 55 | size_t bytes_per_sample; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 56 | RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, |
| 57 | &bytes_per_sample, &num_samples_)); |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 58 | num_samples_remaining_ = num_samples_; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 59 | RTC_CHECK_EQ(kWavFormat, format); |
| 60 | RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | WavReader::~WavReader() { |
| 64 | Close(); |
| 65 | } |
| 66 | |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 67 | int WavReader::sample_rate() const { |
| 68 | return sample_rate_; |
| 69 | } |
| 70 | |
| 71 | size_t WavReader::num_channels() const { |
| 72 | return num_channels_; |
| 73 | } |
| 74 | |
| 75 | size_t WavReader::num_samples() const { |
| 76 | return num_samples_; |
| 77 | } |
| 78 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 79 | size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { |
| 80 | #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 81 | #error "Need to convert samples to big-endian when reading from WAV file" |
| 82 | #endif |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 83 | // There could be metadata after the audio; ensure we don't read it. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 84 | num_samples = std::min(num_samples, num_samples_remaining_); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 85 | const size_t read = |
| 86 | fread(samples, sizeof(*samples), num_samples, file_handle_); |
| 87 | // 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] | 88 | RTC_CHECK(read == num_samples || feof(file_handle_)); |
| 89 | RTC_CHECK_LE(read, num_samples_remaining_); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 90 | num_samples_remaining_ -= read; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 91 | return read; |
| 92 | } |
| 93 | |
| 94 | size_t WavReader::ReadSamples(size_t num_samples, float* samples) { |
| 95 | static const size_t kChunksize = 4096 / sizeof(uint16_t); |
| 96 | size_t read = 0; |
| 97 | for (size_t i = 0; i < num_samples; i += kChunksize) { |
| 98 | int16_t isamples[kChunksize]; |
| 99 | size_t chunk = std::min(kChunksize, num_samples - i); |
| 100 | chunk = ReadSamples(chunk, isamples); |
| 101 | for (size_t j = 0; j < chunk; ++j) |
| 102 | samples[i + j] = isamples[j]; |
| 103 | read += chunk; |
| 104 | } |
| 105 | return read; |
| 106 | } |
| 107 | |
| 108 | void WavReader::Close() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 109 | RTC_CHECK_EQ(0, fclose(file_handle_)); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 110 | file_handle_ = nullptr; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | WavWriter::WavWriter(const std::string& filename, int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 114 | size_t num_channels) |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 115 | : sample_rate_(sample_rate), |
| 116 | num_channels_(num_channels), |
| 117 | num_samples_(0), |
| 118 | file_handle_(fopen(filename.c_str(), "wb")) { |
aluebs | b0ad43b | 2015-11-20 00:11:53 -0800 | [diff] [blame] | 119 | RTC_CHECK(file_handle_) << "Could not open wav file for writing."; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 120 | RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat, |
| 121 | kBytesPerSample, num_samples_)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 122 | |
| 123 | // Write a blank placeholder header, since we need to know the total number |
| 124 | // of samples before we can fill in the real data. |
| 125 | static const uint8_t blank_header[kWavHeaderSize] = {0}; |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 126 | RTC_CHECK_EQ(1, fwrite(blank_header, kWavHeaderSize, 1, file_handle_)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | WavWriter::~WavWriter() { |
| 130 | Close(); |
| 131 | } |
| 132 | |
peah | d1f718b | 2016-02-22 02:13:28 -0800 | [diff] [blame] | 133 | int WavWriter::sample_rate() const { |
| 134 | return sample_rate_; |
| 135 | } |
| 136 | |
| 137 | size_t WavWriter::num_channels() const { |
| 138 | return num_channels_; |
| 139 | } |
| 140 | |
| 141 | size_t WavWriter::num_samples() const { |
| 142 | return num_samples_; |
| 143 | } |
| 144 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 145 | void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) { |
| 146 | #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 147 | #error "Need to convert samples to little-endian when writing to WAV file" |
| 148 | #endif |
| 149 | const size_t written = |
| 150 | fwrite(samples, sizeof(*samples), num_samples, file_handle_); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 151 | RTC_CHECK_EQ(num_samples, written); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 152 | num_samples_ += written; |
| 153 | RTC_CHECK(num_samples_ >= written); // detect size_t overflow |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void WavWriter::WriteSamples(const float* samples, size_t num_samples) { |
| 157 | static const size_t kChunksize = 4096 / sizeof(uint16_t); |
| 158 | for (size_t i = 0; i < num_samples; i += kChunksize) { |
| 159 | int16_t isamples[kChunksize]; |
| 160 | const size_t chunk = std::min(kChunksize, num_samples - i); |
| 161 | FloatS16ToS16(samples + i, chunk, isamples); |
| 162 | WriteSamples(isamples, chunk); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void WavWriter::Close() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 167 | RTC_CHECK_EQ(0, fseek(file_handle_, 0, SEEK_SET)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 168 | uint8_t header[kWavHeaderSize]; |
andrew@webrtc.org | f866b2d | 2014-11-03 18:20:06 +0000 | [diff] [blame] | 169 | WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat, |
| 170 | kBytesPerSample, num_samples_); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 171 | RTC_CHECK_EQ(1, fwrite(header, kWavHeaderSize, 1, file_handle_)); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 172 | RTC_CHECK_EQ(0, fclose(file_handle_)); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 173 | file_handle_ = nullptr; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | } // namespace webrtc |
| 177 | |
| 178 | rtc_WavWriter* rtc_WavOpen(const char* filename, |
| 179 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 180 | size_t num_channels) { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 181 | return reinterpret_cast<rtc_WavWriter*>( |
| 182 | new webrtc::WavWriter(filename, sample_rate, num_channels)); |
| 183 | } |
| 184 | |
| 185 | void rtc_WavClose(rtc_WavWriter* wf) { |
| 186 | delete reinterpret_cast<webrtc::WavWriter*>(wf); |
| 187 | } |
| 188 | |
| 189 | void rtc_WavWriteSamples(rtc_WavWriter* wf, |
| 190 | const float* samples, |
| 191 | size_t num_samples) { |
| 192 | reinterpret_cast<webrtc::WavWriter*>(wf)->WriteSamples(samples, num_samples); |
| 193 | } |
| 194 | |
| 195 | int rtc_WavSampleRate(const rtc_WavWriter* wf) { |
| 196 | return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate(); |
| 197 | } |
| 198 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 199 | size_t rtc_WavNumChannels(const rtc_WavWriter* wf) { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 200 | return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); |
| 201 | } |
| 202 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 203 | size_t rtc_WavNumSamples(const rtc_WavWriter* wf) { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 204 | return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); |
| 205 | } |