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 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 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 |
67 size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { | 79 size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { |
68 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN | 80 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
69 #error "Need to convert samples to big-endian when reading from WAV file" | 81 #error "Need to convert samples to big-endian when reading from WAV file" |
70 #endif | 82 #endif |
71 // There could be metadata after the audio; ensure we don't read it. | 83 // There could be metadata after the audio; ensure we don't read it. |
72 num_samples = std::min(num_samples, num_samples_remaining_); | 84 num_samples = std::min(num_samples, num_samples_remaining_); |
73 const size_t read = | 85 const size_t read = |
74 fread(samples, sizeof(*samples), num_samples, file_handle_); | 86 fread(samples, sizeof(*samples), num_samples, file_handle_); |
75 // If we didn't read what was requested, ensure we've reached the EOF. | 87 // If we didn't read what was requested, ensure we've reached the EOF. |
76 RTC_CHECK(read == num_samples || feof(file_handle_)); | 88 RTC_CHECK(read == num_samples || feof(file_handle_)); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 // Write a blank placeholder header, since we need to know the total number | 123 // Write a blank placeholder header, since we need to know the total number |
112 // of samples before we can fill in the real data. | 124 // of samples before we can fill in the real data. |
113 static const uint8_t blank_header[kWavHeaderSize] = {0}; | 125 static const uint8_t blank_header[kWavHeaderSize] = {0}; |
114 RTC_CHECK_EQ(1u, fwrite(blank_header, kWavHeaderSize, 1, file_handle_)); | 126 RTC_CHECK_EQ(1u, fwrite(blank_header, kWavHeaderSize, 1, file_handle_)); |
115 } | 127 } |
116 | 128 |
117 WavWriter::~WavWriter() { | 129 WavWriter::~WavWriter() { |
118 Close(); | 130 Close(); |
119 } | 131 } |
120 | 132 |
| 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 |
121 void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) { | 145 void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) { |
122 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN | 146 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
123 #error "Need to convert samples to little-endian when writing to WAV file" | 147 #error "Need to convert samples to little-endian when writing to WAV file" |
124 #endif | 148 #endif |
125 const size_t written = | 149 const size_t written = |
126 fwrite(samples, sizeof(*samples), num_samples, file_handle_); | 150 fwrite(samples, sizeof(*samples), num_samples, file_handle_); |
127 RTC_CHECK_EQ(num_samples, written); | 151 RTC_CHECK_EQ(num_samples, written); |
128 num_samples_ += written; | 152 num_samples_ += written; |
129 RTC_CHECK(num_samples_ >= written); // detect size_t overflow | 153 RTC_CHECK(num_samples_ >= written); // detect size_t overflow |
130 } | 154 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate(); | 196 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate(); |
173 } | 197 } |
174 | 198 |
175 size_t rtc_WavNumChannels(const rtc_WavWriter* wf) { | 199 size_t rtc_WavNumChannels(const rtc_WavWriter* wf) { |
176 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); | 200 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); |
177 } | 201 } |
178 | 202 |
179 size_t rtc_WavNumSamples(const rtc_WavWriter* wf) { | 203 size_t rtc_WavNumSamples(const rtc_WavWriter* wf) { |
180 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); | 204 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); |
181 } | 205 } |
OLD | NEW |