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 | 17 |
17 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
18 #include "webrtc/base/safe_conversions.h" | 19 #include "webrtc/base/safe_conversions.h" |
19 #include "webrtc/common_audio/include/audio_util.h" | 20 #include "webrtc/common_audio/include/audio_util.h" |
20 #include "webrtc/common_audio/wav_header.h" | 21 #include "webrtc/common_audio/wav_header.h" |
21 | 22 |
22 namespace webrtc { | 23 namespace webrtc { |
23 | 24 |
24 // We write 16-bit PCM WAV files. | 25 // We write 16-bit PCM WAV files. |
25 static const WavFormat kWavFormat = kWavFormatPcm; | 26 static const WavFormat kWavFormat = kWavFormatPcm; |
26 static const int kBytesPerSample = 2; | 27 static const int kBytesPerSample = 2; |
27 | 28 |
28 // 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. |
29 class ReadableWavFile : public ReadableWav { | 30 class ReadableWavFile : public ReadableWav { |
30 public: | 31 public: |
31 explicit ReadableWavFile(FILE* file) : file_(file) {} | 32 explicit ReadableWavFile(FILE* file) : file_(file) {} |
32 virtual size_t Read(void* buf, size_t num_bytes) { | 33 virtual size_t Read(void* buf, size_t num_bytes) { |
33 return fread(buf, 1, num_bytes, file_); | 34 return fread(buf, 1, num_bytes, file_); |
34 } | 35 } |
35 | 36 |
36 private: | 37 private: |
37 FILE* file_; | 38 FILE* file_; |
38 }; | 39 }; |
39 | 40 |
| 41 std::string WavFile::FormatAsString() const { |
| 42 std::ostringstream s; |
| 43 s << "Sample rate: " << sample_rate() << " Hz, Channels: " << num_channels() |
| 44 << ", Duration: " |
| 45 << (1.f * num_samples()) / (num_channels() * sample_rate()) << " s"; |
| 46 return s.str(); |
| 47 } |
| 48 |
40 WavReader::WavReader(const std::string& filename) | 49 WavReader::WavReader(const std::string& filename) |
41 : file_handle_(fopen(filename.c_str(), "rb")) { | 50 : file_handle_(fopen(filename.c_str(), "rb")) { |
42 RTC_CHECK(file_handle_ && "Could not open wav file for reading."); | 51 RTC_CHECK(file_handle_) << "Could not open wav file for reading."; |
43 | 52 |
44 ReadableWavFile readable(file_handle_); | 53 ReadableWavFile readable(file_handle_); |
45 WavFormat format; | 54 WavFormat format; |
46 int bytes_per_sample; | 55 int bytes_per_sample; |
47 RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, | 56 RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format, |
48 &bytes_per_sample, &num_samples_)); | 57 &bytes_per_sample, &num_samples_)); |
49 num_samples_remaining_ = num_samples_; | 58 num_samples_remaining_ = num_samples_; |
50 RTC_CHECK_EQ(kWavFormat, format); | 59 RTC_CHECK_EQ(kWavFormat, format); |
51 RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample); | 60 RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample); |
52 } | 61 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 RTC_CHECK_EQ(0, fclose(file_handle_)); | 98 RTC_CHECK_EQ(0, fclose(file_handle_)); |
90 file_handle_ = NULL; | 99 file_handle_ = NULL; |
91 } | 100 } |
92 | 101 |
93 WavWriter::WavWriter(const std::string& filename, int sample_rate, | 102 WavWriter::WavWriter(const std::string& filename, int sample_rate, |
94 int num_channels) | 103 int num_channels) |
95 : sample_rate_(sample_rate), | 104 : sample_rate_(sample_rate), |
96 num_channels_(num_channels), | 105 num_channels_(num_channels), |
97 num_samples_(0), | 106 num_samples_(0), |
98 file_handle_(fopen(filename.c_str(), "wb")) { | 107 file_handle_(fopen(filename.c_str(), "wb")) { |
99 RTC_CHECK(file_handle_ && "Could not open wav file for writing."); | 108 RTC_CHECK(file_handle_) << "Could not open wav file for writing."; |
100 RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat, | 109 RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat, |
101 kBytesPerSample, num_samples_)); | 110 kBytesPerSample, num_samples_)); |
102 | 111 |
103 // Write a blank placeholder header, since we need to know the total number | 112 // Write a blank placeholder header, since we need to know the total number |
104 // of samples before we can fill in the real data. | 113 // of samples before we can fill in the real data. |
105 static const uint8_t blank_header[kWavHeaderSize] = {0}; | 114 static const uint8_t blank_header[kWavHeaderSize] = {0}; |
106 RTC_CHECK_EQ(1u, fwrite(blank_header, kWavHeaderSize, 1, file_handle_)); | 115 RTC_CHECK_EQ(1u, fwrite(blank_header, kWavHeaderSize, 1, file_handle_)); |
107 } | 116 } |
108 | 117 |
109 WavWriter::~WavWriter() { | 118 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(); | 174 return reinterpret_cast<const webrtc::WavWriter*>(wf)->sample_rate(); |
166 } | 175 } |
167 | 176 |
168 int rtc_WavNumChannels(const rtc_WavWriter* wf) { | 177 int rtc_WavNumChannels(const rtc_WavWriter* wf) { |
169 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); | 178 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_channels(); |
170 } | 179 } |
171 | 180 |
172 uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf) { | 181 uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf) { |
173 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); | 182 return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples(); |
174 } | 183 } |
OLD | NEW |