OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 memset(frame->data_, 0, sizeof(frame->data_)); | 67 memset(frame->data_, 0, sizeof(frame->data_)); |
68 frame->num_channels_ = 2; | 68 frame->num_channels_ = 2; |
69 frame->sample_rate_hz_ = sample_rate_hz; | 69 frame->sample_rate_hz_ = sample_rate_hz; |
70 frame->samples_per_channel_ = sample_rate_hz / 100; | 70 frame->samples_per_channel_ = sample_rate_hz / 100; |
71 for (size_t i = 0; i < frame->samples_per_channel_; i++) { | 71 for (size_t i = 0; i < frame->samples_per_channel_; i++) { |
72 frame->data_[i * 2] = static_cast<int16_t>(left * i); | 72 frame->data_[i * 2] = static_cast<int16_t>(left * i); |
73 frame->data_[i * 2 + 1] = static_cast<int16_t>(right * i); | 73 frame->data_[i * 2 + 1] = static_cast<int16_t>(right * i); |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 void SetQuadFrame(AudioFrame* frame, float ch1, float ch2, | |
78 float ch3, float ch4, int sample_rate_hz) { | |
aleloi
2017/02/24 10:39:32
Please change param order.
jens.nielsen
2017/02/24 14:55:49
Done.
| |
79 memset(frame->data_, 0, sizeof(frame->data_)); | |
80 frame->num_channels_ = 4; | |
81 frame->sample_rate_hz_ = sample_rate_hz; | |
82 frame->samples_per_channel_ = sample_rate_hz / 100; | |
aleloi
2017/02/24 10:39:32
Please use rtc::CheckedDivExact<int>(sample_rate_h
jens.nielsen
2017/02/24 14:55:49
Done.
| |
83 for (size_t i = 0; i < frame->samples_per_channel_; i++) { | |
84 frame->data_[i * 4] = static_cast<int16_t>(ch1 * i); | |
85 frame->data_[i * 4 + 1] = static_cast<int16_t>(ch2 * i); | |
86 frame->data_[i * 4 + 2] = static_cast<int16_t>(ch3 * i); | |
87 frame->data_[i * 4 + 3] = static_cast<int16_t>(ch4 * i); | |
88 } | |
89 } | |
90 | |
77 // Keep the existing sample rate. | 91 // Keep the existing sample rate. |
78 void SetStereoFrame(AudioFrame* frame, float left, float right) { | 92 void SetStereoFrame(AudioFrame* frame, float left, float right) { |
79 SetStereoFrame(frame, left, right, frame->sample_rate_hz_); | 93 SetStereoFrame(frame, left, right, frame->sample_rate_hz_); |
80 } | 94 } |
81 | 95 |
82 void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) { | 96 void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) { |
83 EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_); | 97 EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_); |
84 EXPECT_EQ(ref_frame.samples_per_channel_, test_frame.samples_per_channel_); | 98 EXPECT_EQ(ref_frame.samples_per_channel_, test_frame.samples_per_channel_); |
85 EXPECT_EQ(ref_frame.sample_rate_hz_, test_frame.sample_rate_hz_); | 99 EXPECT_EQ(ref_frame.sample_rate_hz_, test_frame.sample_rate_hz_); |
86 } | 100 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) { | 135 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) { |
122 EXPECT_EQ(ref_frame.data_[i], test_frame.data_[i]); | 136 EXPECT_EQ(ref_frame.data_[i], test_frame.data_[i]); |
123 } | 137 } |
124 } | 138 } |
125 | 139 |
126 void UtilityTest::RunResampleTest(int src_channels, | 140 void UtilityTest::RunResampleTest(int src_channels, |
127 int src_sample_rate_hz, | 141 int src_sample_rate_hz, |
128 int dst_channels, | 142 int dst_channels, |
129 int dst_sample_rate_hz) { | 143 int dst_sample_rate_hz) { |
130 PushResampler<int16_t> resampler; // Create a new one with every test. | 144 PushResampler<int16_t> resampler; // Create a new one with every test. |
131 const int16_t kSrcLeft = 30; // Shouldn't overflow for any used sample rate. | 145 const int16_t kSrcCh1 = 30; // Shouldn't overflow for any used sample rate. |
132 const int16_t kSrcRight = 15; | 146 const int16_t kSrcCh2 = 15; |
147 const int16_t kSrcCh3 = 22; | |
148 const int16_t kSrcCh4 = 8; | |
133 const float resampling_factor = (1.0 * src_sample_rate_hz) / | 149 const float resampling_factor = (1.0 * src_sample_rate_hz) / |
134 dst_sample_rate_hz; | 150 dst_sample_rate_hz; |
135 const float dst_left = resampling_factor * kSrcLeft; | 151 const float dst_ch1 = resampling_factor * kSrcCh1; |
136 const float dst_right = resampling_factor * kSrcRight; | 152 const float dst_ch2 = resampling_factor * kSrcCh2; |
137 const float dst_mono = (dst_left + dst_right) / 2; | 153 const float dst_ch3 = resampling_factor * kSrcCh3; |
154 const float dst_ch4 = resampling_factor * kSrcCh4; | |
155 const float dst_stereo_to_mono = (dst_ch1 + dst_ch2) / 2; | |
156 const float dst_quad_to_mono = (dst_ch1 + dst_ch2 + dst_ch3 + dst_ch4) / 4; | |
157 const float dst_quad_to_stereo_ch1 = (dst_ch1 + dst_ch2) / 2; | |
158 const float dst_quad_to_stereo_ch2 = (dst_ch3 + dst_ch4) / 2; | |
138 if (src_channels == 1) | 159 if (src_channels == 1) |
139 SetMonoFrame(&src_frame_, kSrcLeft, src_sample_rate_hz); | 160 SetMonoFrame(&src_frame_, kSrcCh1, src_sample_rate_hz); |
161 else if (src_channels == 2) | |
162 SetStereoFrame(&src_frame_, kSrcCh1, kSrcCh2, src_sample_rate_hz); | |
140 else | 163 else |
141 SetStereoFrame(&src_frame_, kSrcLeft, kSrcRight, src_sample_rate_hz); | 164 SetQuadFrame(&src_frame_, kSrcCh1, kSrcCh2, kSrcCh3, kSrcCh4, |
165 src_sample_rate_hz); | |
142 | 166 |
143 if (dst_channels == 1) { | 167 if (dst_channels == 1) { |
144 SetMonoFrame(&dst_frame_, 0, dst_sample_rate_hz); | 168 SetMonoFrame(&dst_frame_, 0, dst_sample_rate_hz); |
145 if (src_channels == 1) | 169 if (src_channels == 1) |
146 SetMonoFrame(&golden_frame_, dst_left, dst_sample_rate_hz); | 170 SetMonoFrame(&golden_frame_, dst_ch1, dst_sample_rate_hz); |
171 else if (src_channels == 2) | |
172 SetMonoFrame(&golden_frame_, dst_stereo_to_mono, dst_sample_rate_hz); | |
147 else | 173 else |
148 SetMonoFrame(&golden_frame_, dst_mono, dst_sample_rate_hz); | 174 SetMonoFrame(&golden_frame_, dst_quad_to_mono, dst_sample_rate_hz); |
149 } else { | 175 } else { |
150 SetStereoFrame(&dst_frame_, 0, 0, dst_sample_rate_hz); | 176 SetStereoFrame(&dst_frame_, 0, 0, dst_sample_rate_hz); |
151 if (src_channels == 1) | 177 if (src_channels == 1) |
152 SetStereoFrame(&golden_frame_, dst_left, dst_left, dst_sample_rate_hz); | 178 SetStereoFrame(&golden_frame_, dst_ch1, dst_ch1, dst_sample_rate_hz); |
179 else if (src_channels == 2) | |
180 SetStereoFrame(&golden_frame_, dst_ch1, dst_ch2, dst_sample_rate_hz); | |
153 else | 181 else |
154 SetStereoFrame(&golden_frame_, dst_left, dst_right, dst_sample_rate_hz); | 182 SetStereoFrame(&golden_frame_, dst_quad_to_stereo_ch1, |
183 dst_quad_to_stereo_ch2, dst_sample_rate_hz); | |
155 } | 184 } |
156 | 185 |
157 // The sinc resampler has a known delay, which we compute here. Multiplying by | 186 // The sinc resampler has a known delay, which we compute here. Multiplying by |
158 // two gives us a crude maximum for any resampling, as the old resampler | 187 // two gives us a crude maximum for any resampling, as the old resampler |
159 // typically (but not always) has lower delay. | 188 // typically (but not always) has lower delay. |
160 static const size_t kInputKernelDelaySamples = 16; | 189 static const size_t kInputKernelDelaySamples = 16; |
161 const size_t max_delay = static_cast<size_t>( | 190 const size_t max_delay = static_cast<size_t>( |
162 static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz * | 191 static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz * |
163 kInputKernelDelaySamples * dst_channels * 2); | 192 kInputKernelDelaySamples * dst_channels * 2); |
164 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later. | 193 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 SetMonoFrame(&dst_frame_, 0); | 229 SetMonoFrame(&dst_frame_, 0); |
201 SetStereoFrame(&src_frame_, 10, 20); | 230 SetStereoFrame(&src_frame_, 10, 20); |
202 SetMonoFrame(&golden_frame_, 15); | 231 SetMonoFrame(&golden_frame_, 15); |
203 RemixAndResample(src_frame_, &resampler_, &dst_frame_); | 232 RemixAndResample(src_frame_, &resampler_, &dst_frame_); |
204 VerifyFramesAreEqual(golden_frame_, dst_frame_); | 233 VerifyFramesAreEqual(golden_frame_, dst_frame_); |
205 } | 234 } |
206 | 235 |
207 TEST_F(UtilityTest, RemixAndResampleSucceeds) { | 236 TEST_F(UtilityTest, RemixAndResampleSucceeds) { |
208 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000}; | 237 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000}; |
209 const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates); | 238 const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates); |
210 const int kChannels[] = {1, 2}; | 239 const int kSrcChannels[] = {1, 2, 4}; |
211 const int kChannelsSize = sizeof(kChannels) / sizeof(*kChannels); | 240 const int kDstChannels[] = {1, 2}; |
241 | |
212 for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) { | 242 for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) { |
213 for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) { | 243 for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) { |
214 for (int src_channel = 0; src_channel < kChannelsSize; src_channel++) { | 244 for (int src_channel = 0; |
215 for (int dst_channel = 0; dst_channel < kChannelsSize; dst_channel++) { | 245 src_channel < sizeof(kSrcChannels) / sizeof(*kSrcChannels); |
hlundin-webrtc
2017/02/24 09:46:36
Use arraysize() from webrtc/base/arraysize.h.
jens.nielsen
2017/02/24 14:55:49
Done.
| |
216 RunResampleTest(kChannels[src_channel], kSampleRates[src_rate], | 246 src_channel++) { |
217 kChannels[dst_channel], kSampleRates[dst_rate]); | 247 for (int dst_channel = 0; |
248 dst_channel < sizeof(kDstChannels) / sizeof(*kDstChannels); | |
249 dst_channel++) { | |
250 RunResampleTest(kSrcChannels[src_channel], kSampleRates[src_rate], | |
251 kDstChannels[dst_channel], kSampleRates[dst_rate]); | |
218 } | 252 } |
219 } | 253 } |
220 } | 254 } |
221 } | 255 } |
222 } | 256 } |
223 | 257 |
224 } // namespace | 258 } // namespace |
225 } // namespace voe | 259 } // namespace voe |
226 } // namespace webrtc | 260 } // namespace webrtc |
OLD | NEW |