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/audio_converter.h" | 11 #include "webrtc/common_audio/audio_converter.h" |
12 | 12 |
13 #include <cstring> | 13 #include <cstring> |
14 | 14 |
15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
16 #include "webrtc/base/safe_conversions.h" | 16 #include "webrtc/base/safe_conversions.h" |
17 #include "webrtc/common_audio/channel_buffer.h" | 17 #include "webrtc/common_audio/channel_buffer.h" |
18 #include "webrtc/common_audio/resampler/push_sinc_resampler.h" | 18 #include "webrtc/common_audio/resampler/push_sinc_resampler.h" |
19 #include "webrtc/system_wrappers/interface/scoped_vector.h" | 19 #include "webrtc/system_wrappers/interface/scoped_vector.h" |
20 | 20 |
21 using rtc::checked_cast; | 21 using rtc::checked_cast; |
22 | 22 |
23 namespace webrtc { | 23 namespace webrtc { |
24 | 24 |
25 class CopyConverter : public AudioConverter { | 25 class CopyConverter : public AudioConverter { |
26 public: | 26 public: |
27 CopyConverter(int src_channels, size_t src_frames, int dst_channels, | 27 CopyConverter(size_t src_channels, size_t src_frames, size_t dst_channels, |
28 size_t dst_frames) | 28 size_t dst_frames) |
29 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {} | 29 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {} |
30 ~CopyConverter() override {}; | 30 ~CopyConverter() override {}; |
31 | 31 |
32 void Convert(const float* const* src, size_t src_size, float* const* dst, | 32 void Convert(const float* const* src, size_t src_size, float* const* dst, |
33 size_t dst_capacity) override { | 33 size_t dst_capacity) override { |
34 CheckSizes(src_size, dst_capacity); | 34 CheckSizes(src_size, dst_capacity); |
35 if (src != dst) { | 35 if (src != dst) { |
36 for (int i = 0; i < src_channels(); ++i) | 36 for (size_t i = 0; i < src_channels(); ++i) |
37 std::memcpy(dst[i], src[i], dst_frames() * sizeof(*dst[i])); | 37 std::memcpy(dst[i], src[i], dst_frames() * sizeof(*dst[i])); |
38 } | 38 } |
39 } | 39 } |
40 }; | 40 }; |
41 | 41 |
42 class UpmixConverter : public AudioConverter { | 42 class UpmixConverter : public AudioConverter { |
43 public: | 43 public: |
44 UpmixConverter(int src_channels, size_t src_frames, int dst_channels, | 44 UpmixConverter(size_t src_channels, size_t src_frames, size_t dst_channels, |
45 size_t dst_frames) | 45 size_t dst_frames) |
46 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {} | 46 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) {} |
47 ~UpmixConverter() override {}; | 47 ~UpmixConverter() override {}; |
48 | 48 |
49 void Convert(const float* const* src, size_t src_size, float* const* dst, | 49 void Convert(const float* const* src, size_t src_size, float* const* dst, |
50 size_t dst_capacity) override { | 50 size_t dst_capacity) override { |
51 CheckSizes(src_size, dst_capacity); | 51 CheckSizes(src_size, dst_capacity); |
52 for (size_t i = 0; i < dst_frames(); ++i) { | 52 for (size_t i = 0; i < dst_frames(); ++i) { |
53 const float value = src[0][i]; | 53 const float value = src[0][i]; |
54 for (int j = 0; j < dst_channels(); ++j) | 54 for (size_t j = 0; j < dst_channels(); ++j) |
55 dst[j][i] = value; | 55 dst[j][i] = value; |
56 } | 56 } |
57 } | 57 } |
58 }; | 58 }; |
59 | 59 |
60 class DownmixConverter : public AudioConverter { | 60 class DownmixConverter : public AudioConverter { |
61 public: | 61 public: |
62 DownmixConverter(int src_channels, size_t src_frames, int dst_channels, | 62 DownmixConverter(size_t src_channels, size_t src_frames, size_t dst_channels, |
63 size_t dst_frames) | 63 size_t dst_frames) |
64 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) { | 64 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) { |
65 } | 65 } |
66 ~DownmixConverter() override {}; | 66 ~DownmixConverter() override {}; |
67 | 67 |
68 void Convert(const float* const* src, size_t src_size, float* const* dst, | 68 void Convert(const float* const* src, size_t src_size, float* const* dst, |
69 size_t dst_capacity) override { | 69 size_t dst_capacity) override { |
70 CheckSizes(src_size, dst_capacity); | 70 CheckSizes(src_size, dst_capacity); |
71 float* dst_mono = dst[0]; | 71 float* dst_mono = dst[0]; |
72 for (size_t i = 0; i < src_frames(); ++i) { | 72 for (size_t i = 0; i < src_frames(); ++i) { |
73 float sum = 0; | 73 float sum = 0; |
74 for (int j = 0; j < src_channels(); ++j) | 74 for (size_t j = 0; j < src_channels(); ++j) |
75 sum += src[j][i]; | 75 sum += src[j][i]; |
76 dst_mono[i] = sum / src_channels(); | 76 dst_mono[i] = sum / src_channels(); |
77 } | 77 } |
78 } | 78 } |
79 }; | 79 }; |
80 | 80 |
81 class ResampleConverter : public AudioConverter { | 81 class ResampleConverter : public AudioConverter { |
82 public: | 82 public: |
83 ResampleConverter(int src_channels, size_t src_frames, int dst_channels, | 83 ResampleConverter(size_t src_channels, size_t src_frames, size_t dst_channels, |
84 size_t dst_frames) | 84 size_t dst_frames) |
85 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) { | 85 : AudioConverter(src_channels, src_frames, dst_channels, dst_frames) { |
86 resamplers_.reserve(src_channels); | 86 resamplers_.reserve(src_channels); |
87 for (int i = 0; i < src_channels; ++i) | 87 for (size_t i = 0; i < src_channels; ++i) |
88 resamplers_.push_back(new PushSincResampler(src_frames, dst_frames)); | 88 resamplers_.push_back(new PushSincResampler(src_frames, dst_frames)); |
89 } | 89 } |
90 ~ResampleConverter() override {}; | 90 ~ResampleConverter() override {}; |
91 | 91 |
92 void Convert(const float* const* src, size_t src_size, float* const* dst, | 92 void Convert(const float* const* src, size_t src_size, float* const* dst, |
93 size_t dst_capacity) override { | 93 size_t dst_capacity) override { |
94 CheckSizes(src_size, dst_capacity); | 94 CheckSizes(src_size, dst_capacity); |
95 for (size_t i = 0; i < resamplers_.size(); ++i) | 95 for (size_t i = 0; i < resamplers_.size(); ++i) |
96 resamplers_[i]->Resample(src[i], src_frames(), dst[i], dst_frames()); | 96 resamplers_[i]->Resample(src[i], src_frames(), dst[i], dst_frames()); |
97 } | 97 } |
(...skipping 30 matching lines...) Expand all Loading... |
128 } | 128 } |
129 converters_.back()->Convert(buffers_.back()->channels(), | 129 converters_.back()->Convert(buffers_.back()->channels(), |
130 buffers_.back()->size(), dst, dst_capacity); | 130 buffers_.back()->size(), dst, dst_capacity); |
131 } | 131 } |
132 | 132 |
133 private: | 133 private: |
134 ScopedVector<AudioConverter> converters_; | 134 ScopedVector<AudioConverter> converters_; |
135 ScopedVector<ChannelBuffer<float>> buffers_; | 135 ScopedVector<ChannelBuffer<float>> buffers_; |
136 }; | 136 }; |
137 | 137 |
138 rtc::scoped_ptr<AudioConverter> AudioConverter::Create(int src_channels, | 138 rtc::scoped_ptr<AudioConverter> AudioConverter::Create(size_t src_channels, |
139 size_t src_frames, | 139 size_t src_frames, |
140 int dst_channels, | 140 size_t dst_channels, |
141 size_t dst_frames) { | 141 size_t dst_frames) { |
142 rtc::scoped_ptr<AudioConverter> sp; | 142 rtc::scoped_ptr<AudioConverter> sp; |
143 if (src_channels > dst_channels) { | 143 if (src_channels > dst_channels) { |
144 if (src_frames != dst_frames) { | 144 if (src_frames != dst_frames) { |
145 ScopedVector<AudioConverter> converters; | 145 ScopedVector<AudioConverter> converters; |
146 converters.push_back(new DownmixConverter(src_channels, src_frames, | 146 converters.push_back(new DownmixConverter(src_channels, src_frames, |
147 dst_channels, src_frames)); | 147 dst_channels, src_frames)); |
148 converters.push_back(new ResampleConverter(dst_channels, src_frames, | 148 converters.push_back(new ResampleConverter(dst_channels, src_frames, |
149 dst_channels, dst_frames)); | 149 dst_channels, dst_frames)); |
150 sp.reset(new CompositionConverter(converters.Pass())); | 150 sp.reset(new CompositionConverter(converters.Pass())); |
(...skipping 24 matching lines...) Expand all Loading... |
175 return sp.Pass(); | 175 return sp.Pass(); |
176 } | 176 } |
177 | 177 |
178 // For CompositionConverter. | 178 // For CompositionConverter. |
179 AudioConverter::AudioConverter() | 179 AudioConverter::AudioConverter() |
180 : src_channels_(0), | 180 : src_channels_(0), |
181 src_frames_(0), | 181 src_frames_(0), |
182 dst_channels_(0), | 182 dst_channels_(0), |
183 dst_frames_(0) {} | 183 dst_frames_(0) {} |
184 | 184 |
185 AudioConverter::AudioConverter(int src_channels, size_t src_frames, | 185 AudioConverter::AudioConverter(size_t src_channels, size_t src_frames, |
186 int dst_channels, size_t dst_frames) | 186 size_t dst_channels, size_t dst_frames) |
187 : src_channels_(src_channels), | 187 : src_channels_(src_channels), |
188 src_frames_(src_frames), | 188 src_frames_(src_frames), |
189 dst_channels_(dst_channels), | 189 dst_channels_(dst_channels), |
190 dst_frames_(dst_frames) { | 190 dst_frames_(dst_frames) { |
191 CHECK(dst_channels == src_channels || dst_channels == 1 || src_channels == 1); | 191 CHECK(dst_channels == src_channels || dst_channels == 1 || src_channels == 1); |
192 } | 192 } |
193 | 193 |
194 void AudioConverter::CheckSizes(size_t src_size, size_t dst_capacity) const { | 194 void AudioConverter::CheckSizes(size_t src_size, size_t dst_capacity) const { |
195 CHECK_EQ(src_size, src_channels() * src_frames()); | 195 CHECK_EQ(src_size, src_channels() * src_frames()); |
196 CHECK_GE(dst_capacity, dst_channels() * dst_frames()); | 196 CHECK_GE(dst_capacity, dst_channels() * dst_frames()); |
197 } | 197 } |
198 | 198 |
199 } // namespace webrtc | 199 } // namespace webrtc |
OLD | NEW |