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