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 19 matching lines...) Expand all Loading... |
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 std::string WavFile::FormatAsString() const { |
| 41 std::ostringstream s; |
| 42 s << "Sample rate: " << sample_rate() << " Hz, Channels: " << num_channels() |
| 43 << ", Duration: " |
| 44 << (1.f * num_samples()) / (num_channels() * sample_rate()) << " s"; |
| 45 return s.str(); |
| 46 } |
| 47 |
40 WavReader::WavReader(const std::string& filename) | 48 WavReader::WavReader(const std::string& filename) |
41 : file_handle_(fopen(filename.c_str(), "rb")) { | 49 : file_handle_(fopen(filename.c_str(), "rb")) { |
42 RTC_CHECK(file_handle_ && "Could not open wav file for reading."); | 50 RTC_CHECK(file_handle_) << "Could not open wav file for reading."; |
43 | 51 |
44 ReadableWavFile readable(file_handle_); | 52 ReadableWavFile readable(file_handle_); |
45 WavFormat format; | 53 WavFormat format; |
46 int bytes_per_sample; | 54 int bytes_per_sample; |
47 RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, | 55 RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, |
48 &bytes_per_sample, &num_samples_)); | 56 &bytes_per_sample, &num_samples_)); |
49 num_samples_remaining_ = num_samples_; | 57 num_samples_remaining_ = num_samples_; |
50 RTC_CHECK_EQ(kWavFormat, format); | 58 RTC_CHECK_EQ(kWavFormat, format); |
51 RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample); | 59 RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample); |
52 } | 60 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 RTC_CHECK_EQ(0, fclose(file_handle_)); | 97 RTC_CHECK_EQ(0, fclose(file_handle_)); |
90 file_handle_ = NULL; | 98 file_handle_ = NULL; |
91 } | 99 } |
92 | 100 |
93 WavWriter::WavWriter(const std::string& filename, int sample_rate, | 101 WavWriter::WavWriter(const std::string& filename, int sample_rate, |
94 int num_channels) | 102 int num_channels) |
95 : sample_rate_(sample_rate), | 103 : sample_rate_(sample_rate), |
96 num_channels_(num_channels), | 104 num_channels_(num_channels), |
97 num_samples_(0), | 105 num_samples_(0), |
98 file_handle_(fopen(filename.c_str(), "wb")) { | 106 file_handle_(fopen(filename.c_str(), "wb")) { |
99 RTC_CHECK(file_handle_ && "Could not open wav file for writing."); | 107 RTC_CHECK(file_handle_) << "Could not open wav file for writing."; |
100 RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat, | 108 RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat, |
101 kBytesPerSample, num_samples_)); | 109 kBytesPerSample, num_samples_)); |
102 | 110 |
103 // Write a blank placeholder header, since we need to know the total number | 111 // Write a blank placeholder header, since we need to know the total number |
104 // of samples before we can fill in the real data. | 112 // of samples before we can fill in the real data. |
105 static const uint8_t blank_header[kWavHeaderSize] = {0}; | 113 static const uint8_t blank_header[kWavHeaderSize] = {0}; |
106 RTC_CHECK_EQ(1u, fwrite(blank_header, kWavHeaderSize, 1, file_handle_)); | 114 RTC_CHECK_EQ(1u, fwrite(blank_header, kWavHeaderSize, 1, file_handle_)); |
107 } | 115 } |
108 | 116 |
109 WavWriter::~WavWriter() { | 117 WavWriter::~WavWriter() { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate(); | 173 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate(); |
166 } | 174 } |
167 | 175 |
168 int rtc_WavNumChannels(const rtc_WavWriter* wf) { | 176 int rtc_WavNumChannels(const rtc_WavWriter* wf) { |
169 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); | 177 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); |
170 } | 178 } |
171 | 179 |
172 uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf) { | 180 uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf) { |
173 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); | 181 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); |
174 } | 182 } |
OLD | NEW |