| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/common_audio/wav_file.h" | 11 #include "webrtc/common_audio/wav_file.h" |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <cstdio> | 14 #include <cstdio> |
| 15 #include <limits> | 15 #include <limits> |
| 16 | 16 |
| 17 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
| 18 #include "webrtc/base/safe_conversions.h" | 18 #include "webrtc/base/safe_conversions.h" |
| 19 #include "webrtc/common_audio/include/audio_util.h" | 19 #include "webrtc/common_audio/include/audio_util.h" |
| 20 #include "webrtc/common_audio/wav_header.h" | 20 #include "webrtc/common_audio/wav_header.h" |
| 21 | 21 |
| 22 namespace webrtc { | 22 namespace webrtc { |
| 23 | 23 |
| 24 // We write 16-bit PCM WAV files. | 24 // We write 16-bit PCM WAV files. |
| 25 static const WavFormat kWavFormat = kWavFormatPcm; | 25 static const WavFormat kWavFormat = kWavFormatPcm; |
| 26 static const int kBytesPerSample = 2; | 26 static const size_t kBytesPerSample = 2; |
| 27 | 27 |
| 28 // Doesn't take ownership of the file handle and won't close it. | 28 // Doesn't take ownership of the file handle and won't close it. |
| 29 class ReadableWavFile : public ReadableWav { | 29 class ReadableWavFile : public ReadableWav { |
| 30 public: | 30 public: |
| 31 explicit ReadableWavFile(FILE* file) : file_(file) {} | 31 explicit ReadableWavFile(FILE* file) : file_(file) {} |
| 32 virtual size_t Read(void* buf, size_t num_bytes) { | 32 virtual size_t Read(void* buf, size_t num_bytes) { |
| 33 return fread(buf, 1, num_bytes, file_); | 33 return fread(buf, 1, num_bytes, file_); |
| 34 } | 34 } |
| 35 | 35 |
| 36 private: | 36 private: |
| 37 FILE* file_; | 37 FILE* file_; |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 WavReader::WavReader(const std::string& filename) | 40 WavReader::WavReader(const std::string& filename) |
| 41 : file_handle_(fopen(filename.c_str(), "rb")) { | 41 : file_handle_(fopen(filename.c_str(), "rb")) { |
| 42 CHECK(file_handle_ && "Could not open wav file for reading."); | 42 CHECK(file_handle_ && "Could not open wav file for reading."); |
| 43 | 43 |
| 44 ReadableWavFile readable(file_handle_); | 44 ReadableWavFile readable(file_handle_); |
| 45 WavFormat format; | 45 WavFormat format; |
| 46 int bytes_per_sample; | 46 size_t bytes_per_sample; |
| 47 CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, | 47 CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, |
| 48 &bytes_per_sample, &num_samples_)); | 48 &bytes_per_sample, &num_samples_)); |
| 49 num_samples_remaining_ = num_samples_; | 49 num_samples_remaining_ = num_samples_; |
| 50 CHECK_EQ(kWavFormat, format); | 50 CHECK_EQ(kWavFormat, format); |
| 51 CHECK_EQ(kBytesPerSample, bytes_per_sample); | 51 CHECK_EQ(kBytesPerSample, bytes_per_sample); |
| 52 } | 52 } |
| 53 | 53 |
| 54 WavReader::~WavReader() { | 54 WavReader::~WavReader() { |
| 55 Close(); | 55 Close(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { | 58 size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { |
| 59 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN | 59 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 60 #error "Need to convert samples to big-endian when reading from WAV file" | 60 #error "Need to convert samples to big-endian when reading from WAV file" |
| 61 #endif | 61 #endif |
| 62 // There could be metadata after the audio; ensure we don't read it. | 62 // There could be metadata after the audio; ensure we don't read it. |
| 63 num_samples = std::min(rtc::checked_cast<uint32_t>(num_samples), | 63 num_samples = std::min(num_samples, num_samples_remaining_); |
| 64 num_samples_remaining_); | |
| 65 const size_t read = | 64 const size_t read = |
| 66 fread(samples, sizeof(*samples), num_samples, file_handle_); | 65 fread(samples, sizeof(*samples), num_samples, file_handle_); |
| 67 // If we didn't read what was requested, ensure we've reached the EOF. | 66 // If we didn't read what was requested, ensure we've reached the EOF. |
| 68 CHECK(read == num_samples || feof(file_handle_)); | 67 CHECK(read == num_samples || feof(file_handle_)); |
| 69 CHECK_LE(read, num_samples_remaining_); | 68 CHECK_LE(read, num_samples_remaining_); |
| 70 num_samples_remaining_ -= rtc::checked_cast<uint32_t>(read); | 69 num_samples_remaining_ -= read; |
| 71 return read; | 70 return read; |
| 72 } | 71 } |
| 73 | 72 |
| 74 size_t WavReader::ReadSamples(size_t num_samples, float* samples) { | 73 size_t WavReader::ReadSamples(size_t num_samples, float* samples) { |
| 75 static const size_t kChunksize = 4096 / sizeof(uint16_t); | 74 static const size_t kChunksize = 4096 / sizeof(uint16_t); |
| 76 size_t read = 0; | 75 size_t read = 0; |
| 77 for (size_t i = 0; i < num_samples; i += kChunksize) { | 76 for (size_t i = 0; i < num_samples; i += kChunksize) { |
| 78 int16_t isamples[kChunksize]; | 77 int16_t isamples[kChunksize]; |
| 79 size_t chunk = std::min(kChunksize, num_samples - i); | 78 size_t chunk = std::min(kChunksize, num_samples - i); |
| 80 chunk = ReadSamples(chunk, isamples); | 79 chunk = ReadSamples(chunk, isamples); |
| 81 for (size_t j = 0; j < chunk; ++j) | 80 for (size_t j = 0; j < chunk; ++j) |
| 82 samples[i + j] = isamples[j]; | 81 samples[i + j] = isamples[j]; |
| 83 read += chunk; | 82 read += chunk; |
| 84 } | 83 } |
| 85 return read; | 84 return read; |
| 86 } | 85 } |
| 87 | 86 |
| 88 void WavReader::Close() { | 87 void WavReader::Close() { |
| 89 CHECK_EQ(0, fclose(file_handle_)); | 88 CHECK_EQ(0, fclose(file_handle_)); |
| 90 file_handle_ = NULL; | 89 file_handle_ = NULL; |
| 91 } | 90 } |
| 92 | 91 |
| 93 WavWriter::WavWriter(const std::string& filename, int sample_rate, | 92 WavWriter::WavWriter(const std::string& filename, int sample_rate, |
| 94 int num_channels) | 93 size_t num_channels) |
| 95 : sample_rate_(sample_rate), | 94 : sample_rate_(sample_rate), |
| 96 num_channels_(num_channels), | 95 num_channels_(num_channels), |
| 97 num_samples_(0), | 96 num_samples_(0), |
| 98 file_handle_(fopen(filename.c_str(), "wb")) { | 97 file_handle_(fopen(filename.c_str(), "wb")) { |
| 99 CHECK(file_handle_ && "Could not open wav file for writing."); | 98 CHECK(file_handle_ && "Could not open wav file for writing."); |
| 100 CHECK(CheckWavParameters(num_channels_, | 99 CHECK(CheckWavParameters(num_channels_, |
| 101 sample_rate_, | 100 sample_rate_, |
| 102 kWavFormat, | 101 kWavFormat, |
| 103 kBytesPerSample, | 102 kBytesPerSample, |
| 104 num_samples_)); | 103 num_samples_)); |
| 105 | 104 |
| 106 // Write a blank placeholder header, since we need to know the total number | 105 // Write a blank placeholder header, since we need to know the total number |
| 107 // of samples before we can fill in the real data. | 106 // of samples before we can fill in the real data. |
| 108 static const uint8_t blank_header[kWavHeaderSize] = {0}; | 107 static const uint8_t blank_header[kWavHeaderSize] = {0}; |
| 109 CHECK_EQ(1u, fwrite(blank_header, kWavHeaderSize, 1, file_handle_)); | 108 CHECK_EQ(1u, fwrite(blank_header, kWavHeaderSize, 1, file_handle_)); |
| 110 } | 109 } |
| 111 | 110 |
| 112 WavWriter::~WavWriter() { | 111 WavWriter::~WavWriter() { |
| 113 Close(); | 112 Close(); |
| 114 } | 113 } |
| 115 | 114 |
| 116 void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) { | 115 void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) { |
| 117 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN | 116 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 118 #error "Need to convert samples to little-endian when writing to WAV file" | 117 #error "Need to convert samples to little-endian when writing to WAV file" |
| 119 #endif | 118 #endif |
| 120 const size_t written = | 119 const size_t written = |
| 121 fwrite(samples, sizeof(*samples), num_samples, file_handle_); | 120 fwrite(samples, sizeof(*samples), num_samples, file_handle_); |
| 122 CHECK_EQ(num_samples, written); | 121 CHECK_EQ(num_samples, written); |
| 123 num_samples_ += static_cast<uint32_t>(written); | 122 num_samples_ += written; |
| 124 CHECK(written <= std::numeric_limits<uint32_t>::max() || | 123 CHECK(num_samples_ >= written); // detect size_t overflow |
| 125 num_samples_ >= written); // detect uint32_t overflow | |
| 126 } | 124 } |
| 127 | 125 |
| 128 void WavWriter::WriteSamples(const float* samples, size_t num_samples) { | 126 void WavWriter::WriteSamples(const float* samples, size_t num_samples) { |
| 129 static const size_t kChunksize = 4096 / sizeof(uint16_t); | 127 static const size_t kChunksize = 4096 / sizeof(uint16_t); |
| 130 for (size_t i = 0; i < num_samples; i += kChunksize) { | 128 for (size_t i = 0; i < num_samples; i += kChunksize) { |
| 131 int16_t isamples[kChunksize]; | 129 int16_t isamples[kChunksize]; |
| 132 const size_t chunk = std::min(kChunksize, num_samples - i); | 130 const size_t chunk = std::min(kChunksize, num_samples - i); |
| 133 FloatS16ToS16(samples + i, chunk, isamples); | 131 FloatS16ToS16(samples + i, chunk, isamples); |
| 134 WriteSamples(isamples, chunk); | 132 WriteSamples(isamples, chunk); |
| 135 } | 133 } |
| 136 } | 134 } |
| 137 | 135 |
| 138 void WavWriter::Close() { | 136 void WavWriter::Close() { |
| 139 CHECK_EQ(0, fseek(file_handle_, 0, SEEK_SET)); | 137 CHECK_EQ(0, fseek(file_handle_, 0, SEEK_SET)); |
| 140 uint8_t header[kWavHeaderSize]; | 138 uint8_t header[kWavHeaderSize]; |
| 141 WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat, | 139 WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat, |
| 142 kBytesPerSample, num_samples_); | 140 kBytesPerSample, num_samples_); |
| 143 CHECK_EQ(1u, fwrite(header, kWavHeaderSize, 1, file_handle_)); | 141 CHECK_EQ(1u, fwrite(header, kWavHeaderSize, 1, file_handle_)); |
| 144 CHECK_EQ(0, fclose(file_handle_)); | 142 CHECK_EQ(0, fclose(file_handle_)); |
| 145 file_handle_ = NULL; | 143 file_handle_ = NULL; |
| 146 } | 144 } |
| 147 | 145 |
| 148 } // namespace webrtc | 146 } // namespace webrtc |
| 149 | 147 |
| 150 rtc_WavWriter* rtc_WavOpen(const char* filename, | 148 rtc_WavWriter* rtc_WavOpen(const char* filename, |
| 151 int sample_rate, | 149 int sample_rate, |
| 152 int num_channels) { | 150 size_t num_channels) { |
| 153 return reinterpret_cast<rtc_WavWriter*>( | 151 return reinterpret_cast<rtc_WavWriter*>( |
| 154 new webrtc::WavWriter(filename, sample_rate, num_channels)); | 152 new webrtc::WavWriter(filename, sample_rate, num_channels)); |
| 155 } | 153 } |
| 156 | 154 |
| 157 void rtc_WavClose(rtc_WavWriter* wf) { | 155 void rtc_WavClose(rtc_WavWriter* wf) { |
| 158 delete reinterpret_cast<webrtc::WavWriter*>(wf); | 156 delete reinterpret_cast<webrtc::WavWriter*>(wf); |
| 159 } | 157 } |
| 160 | 158 |
| 161 void rtc_WavWriteSamples(rtc_WavWriter* wf, | 159 void rtc_WavWriteSamples(rtc_WavWriter* wf, |
| 162 const float* samples, | 160 const float* samples, |
| 163 size_t num_samples) { | 161 size_t num_samples) { |
| 164 reinterpret_cast<webrtc::WavWriter*>(wf)->WriteSamples(samples, num_samples); | 162 reinterpret_cast<webrtc::WavWriter*>(wf)->WriteSamples(samples, num_samples); |
| 165 } | 163 } |
| 166 | 164 |
| 167 int rtc_WavSampleRate(const rtc_WavWriter* wf) { | 165 int rtc_WavSampleRate(const rtc_WavWriter* wf) { |
| 168 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate(); | 166 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate(); |
| 169 } | 167 } |
| 170 | 168 |
| 171 int rtc_WavNumChannels(const rtc_WavWriter* wf) { | 169 size_t rtc_WavNumChannels(const rtc_WavWriter* wf) { |
| 172 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); | 170 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); |
| 173 } | 171 } |
| 174 | 172 |
| 175 uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf) { | 173 uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf) { |
| 176 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); | 174 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); |
| 177 } | 175 } |
| OLD | NEW |