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 #include <sstream> | 16 #include <sstream> |
17 | 17 |
18 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
19 #include "webrtc/base/safe_conversions.h" | 19 #include "webrtc/base/safe_conversions.h" |
20 #include "webrtc/common_audio/include/audio_util.h" | 20 #include "webrtc/common_audio/include/audio_util.h" |
21 #include "webrtc/common_audio/wav_header.h" | 21 #include "webrtc/common_audio/wav_header.h" |
22 | 22 |
23 namespace webrtc { | 23 namespace webrtc { |
24 | 24 |
25 // We write 16-bit PCM WAV files. | 25 // We write 16-bit PCM WAV files. |
26 static const WavFormat kWavFormat = kWavFormatPcm; | 26 static const WavFormat kWavFormat = kWavFormatPcm; |
27 static const int kBytesPerSample = 2; | 27 static const size_t kBytesPerSample = 2; |
28 | 28 |
29 // Doesn't take ownership of the file handle and won't close it. | 29 // Doesn't take ownership of the file handle and won't close it. |
30 class ReadableWavFile : public ReadableWav { | 30 class ReadableWavFile : public ReadableWav { |
31 public: | 31 public: |
32 explicit ReadableWavFile(FILE* file) : file_(file) {} | 32 explicit ReadableWavFile(FILE* file) : file_(file) {} |
33 virtual size_t Read(void* buf, size_t num_bytes) { | 33 virtual size_t Read(void* buf, size_t num_bytes) { |
34 return fread(buf, 1, num_bytes, file_); | 34 return fread(buf, 1, num_bytes, file_); |
35 } | 35 } |
36 | 36 |
37 private: | 37 private: |
38 FILE* file_; | 38 FILE* file_; |
39 }; | 39 }; |
40 | 40 |
41 std::string WavFile::FormatAsString() const { | 41 std::string WavFile::FormatAsString() const { |
42 std::ostringstream s; | 42 std::ostringstream s; |
43 s << "Sample rate: " << sample_rate() << " Hz, Channels: " << num_channels() | 43 s << "Sample rate: " << sample_rate() << " Hz, Channels: " << num_channels() |
44 << ", Duration: " | 44 << ", Duration: " |
45 << (1.f * num_samples()) / (num_channels() * sample_rate()) << " s"; | 45 << (1.f * num_samples()) / (num_channels() * sample_rate()) << " s"; |
46 return s.str(); | 46 return s.str(); |
47 } | 47 } |
48 | 48 |
49 WavReader::WavReader(const std::string& filename) | 49 WavReader::WavReader(const std::string& filename) |
50 : file_handle_(fopen(filename.c_str(), "rb")) { | 50 : file_handle_(fopen(filename.c_str(), "rb")) { |
51 RTC_CHECK(file_handle_) << "Could not open wav file for reading."; | 51 RTC_CHECK(file_handle_) << "Could not open wav file for reading."; |
52 | 52 |
53 ReadableWavFile readable(file_handle_); | 53 ReadableWavFile readable(file_handle_); |
54 WavFormat format; | 54 WavFormat format; |
55 int bytes_per_sample; | 55 size_t bytes_per_sample; |
56 RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, | 56 RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, |
57 &bytes_per_sample, &num_samples_)); | 57 &bytes_per_sample, &num_samples_)); |
58 num_samples_remaining_ = num_samples_; | 58 num_samples_remaining_ = num_samples_; |
59 RTC_CHECK_EQ(kWavFormat, format); | 59 RTC_CHECK_EQ(kWavFormat, format); |
60 RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample); | 60 RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample); |
61 } | 61 } |
62 | 62 |
63 WavReader::~WavReader() { | 63 WavReader::~WavReader() { |
64 Close(); | 64 Close(); |
65 } | 65 } |
66 | 66 |
67 size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { | 67 size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { |
68 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN | 68 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
69 #error "Need to convert samples to big-endian when reading from WAV file" | 69 #error "Need to convert samples to big-endian when reading from WAV file" |
70 #endif | 70 #endif |
71 // There could be metadata after the audio; ensure we don't read it. | 71 // There could be metadata after the audio; ensure we don't read it. |
72 num_samples = std::min(rtc::checked_cast<uint32_t>(num_samples), | 72 num_samples = std::min(num_samples, num_samples_remaining_); |
73 num_samples_remaining_); | |
74 const size_t read = | 73 const size_t read = |
75 fread(samples, sizeof(*samples), num_samples, file_handle_); | 74 fread(samples, sizeof(*samples), num_samples, file_handle_); |
76 // If we didn't read what was requested, ensure we've reached the EOF. | 75 // If we didn't read what was requested, ensure we've reached the EOF. |
77 RTC_CHECK(read == num_samples || feof(file_handle_)); | 76 RTC_CHECK(read == num_samples || feof(file_handle_)); |
78 RTC_CHECK_LE(read, num_samples_remaining_); | 77 RTC_CHECK_LE(read, num_samples_remaining_); |
79 num_samples_remaining_ -= rtc::checked_cast<uint32_t>(read); | 78 num_samples_remaining_ -= read; |
80 return read; | 79 return read; |
81 } | 80 } |
82 | 81 |
83 size_t WavReader::ReadSamples(size_t num_samples, float* samples) { | 82 size_t WavReader::ReadSamples(size_t num_samples, float* samples) { |
84 static const size_t kChunksize = 4096 / sizeof(uint16_t); | 83 static const size_t kChunksize = 4096 / sizeof(uint16_t); |
85 size_t read = 0; | 84 size_t read = 0; |
86 for (size_t i = 0; i < num_samples; i += kChunksize) { | 85 for (size_t i = 0; i < num_samples; i += kChunksize) { |
87 int16_t isamples[kChunksize]; | 86 int16_t isamples[kChunksize]; |
88 size_t chunk = std::min(kChunksize, num_samples - i); | 87 size_t chunk = std::min(kChunksize, num_samples - i); |
89 chunk = ReadSamples(chunk, isamples); | 88 chunk = ReadSamples(chunk, isamples); |
(...skipping 29 matching lines...) Expand all Loading... |
119 Close(); | 118 Close(); |
120 } | 119 } |
121 | 120 |
122 void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) { | 121 void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) { |
123 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN | 122 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
124 #error "Need to convert samples to little-endian when writing to WAV file" | 123 #error "Need to convert samples to little-endian when writing to WAV file" |
125 #endif | 124 #endif |
126 const size_t written = | 125 const size_t written = |
127 fwrite(samples, sizeof(*samples), num_samples, file_handle_); | 126 fwrite(samples, sizeof(*samples), num_samples, file_handle_); |
128 RTC_CHECK_EQ(num_samples, written); | 127 RTC_CHECK_EQ(num_samples, written); |
129 num_samples_ += static_cast<uint32_t>(written); | 128 num_samples_ += written; |
130 RTC_CHECK(written <= std::numeric_limits<uint32_t>::max() || | 129 RTC_CHECK(num_samples_ >= written); // detect size_t overflow |
131 num_samples_ >= written); // detect uint32_t overflow | |
132 } | 130 } |
133 | 131 |
134 void WavWriter::WriteSamples(const float* samples, size_t num_samples) { | 132 void WavWriter::WriteSamples(const float* samples, size_t num_samples) { |
135 static const size_t kChunksize = 4096 / sizeof(uint16_t); | 133 static const size_t kChunksize = 4096 / sizeof(uint16_t); |
136 for (size_t i = 0; i < num_samples; i += kChunksize) { | 134 for (size_t i = 0; i < num_samples; i += kChunksize) { |
137 int16_t isamples[kChunksize]; | 135 int16_t isamples[kChunksize]; |
138 const size_t chunk = std::min(kChunksize, num_samples - i); | 136 const size_t chunk = std::min(kChunksize, num_samples - i); |
139 FloatS16ToS16(samples + i, chunk, isamples); | 137 FloatS16ToS16(samples + i, chunk, isamples); |
140 WriteSamples(isamples, chunk); | 138 WriteSamples(isamples, chunk); |
141 } | 139 } |
(...skipping 29 matching lines...) Expand all Loading... |
171 } | 169 } |
172 | 170 |
173 int rtc_WavSampleRate(const rtc_WavWriter* wf) { | 171 int rtc_WavSampleRate(const rtc_WavWriter* wf) { |
174 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate(); | 172 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate(); |
175 } | 173 } |
176 | 174 |
177 int rtc_WavNumChannels(const rtc_WavWriter* wf) { | 175 int rtc_WavNumChannels(const rtc_WavWriter* wf) { |
178 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); | 176 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); |
179 } | 177 } |
180 | 178 |
181 uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf) { | 179 size_t rtc_WavNumSamples(const rtc_WavWriter* wf) { |
182 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); | 180 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); |
183 } | 181 } |
OLD | NEW |