Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: webrtc/voice_engine/utility_unittest.cc

Issue 2712743004: Support 4 channel mic in Windows Core Audio (Closed)
Patch Set: Fix another non-Windows build error Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 #include <math.h> 11 #include <math.h>
12 12
13 #include "webrtc/base/format_macros.h" 13 #include "webrtc/base/format_macros.h"
14 #include "webrtc/common_audio/resampler/include/push_resampler.h" 14 #include "webrtc/common_audio/resampler/include/push_resampler.h"
15 #include "webrtc/modules/include/module_common_types.h" 15 #include "webrtc/modules/include/module_common_types.h"
16 #include "webrtc/test/gtest.h" 16 #include "webrtc/test/gtest.h"
17 #include "webrtc/voice_engine/utility.h" 17 #include "webrtc/voice_engine/utility.h"
18 #include "webrtc/voice_engine/voice_engine_defines.h" 18 #include "webrtc/voice_engine/voice_engine_defines.h"
19 #include "webrtc/base/arraysize.h"
19 20
20 namespace webrtc { 21 namespace webrtc {
21 namespace voe { 22 namespace voe {
22 namespace { 23 namespace {
23 24
24 class UtilityTest : public ::testing::Test { 25 class UtilityTest : public ::testing::Test {
25 protected: 26 protected:
26 UtilityTest() { 27 UtilityTest() {
27 src_frame_.sample_rate_hz_ = 16000; 28 src_frame_.sample_rate_hz_ = 16000;
28 src_frame_.samples_per_channel_ = src_frame_.sample_rate_hz_ / 100; 29 src_frame_.samples_per_channel_ = src_frame_.sample_rate_hz_ / 100;
29 src_frame_.num_channels_ = 1; 30 src_frame_.num_channels_ = 1;
30 dst_frame_.CopyFrom(src_frame_); 31 dst_frame_.CopyFrom(src_frame_);
31 golden_frame_.CopyFrom(src_frame_); 32 golden_frame_.CopyFrom(src_frame_);
32 } 33 }
33 34
34 void RunResampleTest(int src_channels, 35 void RunResampleTest(int src_channels,
35 int src_sample_rate_hz, 36 int src_sample_rate_hz,
36 int dst_channels, 37 int dst_channels,
37 int dst_sample_rate_hz); 38 int dst_sample_rate_hz);
38 39
39 PushResampler<int16_t> resampler_; 40 PushResampler<int16_t> resampler_;
40 AudioFrame src_frame_; 41 AudioFrame src_frame_;
41 AudioFrame dst_frame_; 42 AudioFrame dst_frame_;
42 AudioFrame golden_frame_; 43 AudioFrame golden_frame_;
43 }; 44 };
44 45
45 // Sets the signal value to increase by |data| with every sample. Floats are 46 // Sets the signal value to increase by |data| with every sample. Floats are
46 // used so non-integer values result in rounding error, but not an accumulating 47 // used so non-integer values result in rounding error, but not an accumulating
47 // error. 48 // error.
48 void SetMonoFrame(AudioFrame* frame, float data, int sample_rate_hz) { 49 void SetMonoFrame(float data, int sample_rate_hz, AudioFrame* frame) {
49 memset(frame->data_, 0, sizeof(frame->data_)); 50 memset(frame->data_, 0, sizeof(frame->data_));
50 frame->num_channels_ = 1; 51 frame->num_channels_ = 1;
51 frame->sample_rate_hz_ = sample_rate_hz; 52 frame->sample_rate_hz_ = sample_rate_hz;
52 frame->samples_per_channel_ = sample_rate_hz / 100; 53 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
53 for (size_t i = 0; i < frame->samples_per_channel_; i++) { 54 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
54 frame->data_[i] = static_cast<int16_t>(data * i); 55 frame->data_[i] = static_cast<int16_t>(data * i);
55 } 56 }
56 } 57 }
57 58
58 // Keep the existing sample rate. 59 // Keep the existing sample rate.
59 void SetMonoFrame(AudioFrame* frame, float data) { 60 void SetMonoFrame(float data, AudioFrame* frame) {
60 SetMonoFrame(frame, data, frame->sample_rate_hz_); 61 SetMonoFrame(data, frame->sample_rate_hz_, frame);
61 } 62 }
62 63
63 // Sets the signal value to increase by |left| and |right| with every sample in 64 // Sets the signal value to increase by |left| and |right| with every sample in
64 // each channel respectively. 65 // each channel respectively.
65 void SetStereoFrame(AudioFrame* frame, float left, float right, 66 void SetStereoFrame(float left,
66 int sample_rate_hz) { 67 float right,
68 int sample_rate_hz,
69 AudioFrame* frame) {
67 memset(frame->data_, 0, sizeof(frame->data_)); 70 memset(frame->data_, 0, sizeof(frame->data_));
68 frame->num_channels_ = 2; 71 frame->num_channels_ = 2;
69 frame->sample_rate_hz_ = sample_rate_hz; 72 frame->sample_rate_hz_ = sample_rate_hz;
70 frame->samples_per_channel_ = sample_rate_hz / 100; 73 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
71 for (size_t i = 0; i < frame->samples_per_channel_; i++) { 74 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
72 frame->data_[i * 2] = static_cast<int16_t>(left * i); 75 frame->data_[i * 2] = static_cast<int16_t>(left * i);
73 frame->data_[i * 2 + 1] = static_cast<int16_t>(right * i); 76 frame->data_[i * 2 + 1] = static_cast<int16_t>(right * i);
74 } 77 }
75 } 78 }
76 79
77 // Keep the existing sample rate. 80 // Keep the existing sample rate.
78 void SetStereoFrame(AudioFrame* frame, float left, float right) { 81 void SetStereoFrame(float left, float right, AudioFrame* frame) {
79 SetStereoFrame(frame, left, right, frame->sample_rate_hz_); 82 SetStereoFrame(left, right, frame->sample_rate_hz_, frame);
83 }
84
85 // Sets the signal value to increase by |ch1|, |ch2|, |ch3|, |ch4| with every
86 // sample in each channel respectively.
87 void SetQuadFrame(float ch1,
88 float ch2,
89 float ch3,
90 float ch4,
91 int sample_rate_hz,
92 AudioFrame* frame) {
93 memset(frame->data_, 0, sizeof(frame->data_));
94 frame->num_channels_ = 4;
95 frame->sample_rate_hz_ = sample_rate_hz;
96 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
97 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
98 frame->data_[i * 4] = static_cast<int16_t>(ch1 * i);
99 frame->data_[i * 4 + 1] = static_cast<int16_t>(ch2 * i);
100 frame->data_[i * 4 + 2] = static_cast<int16_t>(ch3 * i);
101 frame->data_[i * 4 + 3] = static_cast<int16_t>(ch4 * i);
102 }
80 } 103 }
81 104
82 void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) { 105 void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) {
83 EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_); 106 EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_);
84 EXPECT_EQ(ref_frame.samples_per_channel_, test_frame.samples_per_channel_); 107 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_); 108 EXPECT_EQ(ref_frame.sample_rate_hz_, test_frame.sample_rate_hz_);
86 } 109 }
87 110
88 // Computes the best SNR based on the error between |ref_frame| and 111 // Computes the best SNR based on the error between |ref_frame| and
89 // |test_frame|. It allows for up to a |max_delay| in samples between the 112 // |test_frame|. It allows for up to a |max_delay| in samples between the
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) { 144 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) {
122 EXPECT_EQ(ref_frame.data_[i], test_frame.data_[i]); 145 EXPECT_EQ(ref_frame.data_[i], test_frame.data_[i]);
123 } 146 }
124 } 147 }
125 148
126 void UtilityTest::RunResampleTest(int src_channels, 149 void UtilityTest::RunResampleTest(int src_channels,
127 int src_sample_rate_hz, 150 int src_sample_rate_hz,
128 int dst_channels, 151 int dst_channels,
129 int dst_sample_rate_hz) { 152 int dst_sample_rate_hz) {
130 PushResampler<int16_t> resampler; // Create a new one with every test. 153 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. 154 const int16_t kSrcCh1 = 30; // Shouldn't overflow for any used sample rate.
132 const int16_t kSrcRight = 15; 155 const int16_t kSrcCh2 = 15;
156 const int16_t kSrcCh3 = 22;
157 const int16_t kSrcCh4 = 8;
133 const float resampling_factor = (1.0 * src_sample_rate_hz) / 158 const float resampling_factor = (1.0 * src_sample_rate_hz) /
134 dst_sample_rate_hz; 159 dst_sample_rate_hz;
135 const float dst_left = resampling_factor * kSrcLeft; 160 const float dst_ch1 = resampling_factor * kSrcCh1;
136 const float dst_right = resampling_factor * kSrcRight; 161 const float dst_ch2 = resampling_factor * kSrcCh2;
137 const float dst_mono = (dst_left + dst_right) / 2; 162 const float dst_ch3 = resampling_factor * kSrcCh3;
163 const float dst_ch4 = resampling_factor * kSrcCh4;
164 const float dst_stereo_to_mono = (dst_ch1 + dst_ch2) / 2;
165 const float dst_quad_to_mono = (dst_ch1 + dst_ch2 + dst_ch3 + dst_ch4) / 4;
166 const float dst_quad_to_stereo_ch1 = (dst_ch1 + dst_ch2) / 2;
167 const float dst_quad_to_stereo_ch2 = (dst_ch3 + dst_ch4) / 2;
138 if (src_channels == 1) 168 if (src_channels == 1)
139 SetMonoFrame(&src_frame_, kSrcLeft, src_sample_rate_hz); 169 SetMonoFrame(kSrcCh1, src_sample_rate_hz, &src_frame_);
170 else if (src_channels == 2)
171 SetStereoFrame(kSrcCh1, kSrcCh2, src_sample_rate_hz, &src_frame_);
140 else 172 else
141 SetStereoFrame(&src_frame_, kSrcLeft, kSrcRight, src_sample_rate_hz); 173 SetQuadFrame(kSrcCh1, kSrcCh2, kSrcCh3, kSrcCh4, src_sample_rate_hz,
174 &src_frame_);
142 175
143 if (dst_channels == 1) { 176 if (dst_channels == 1) {
144 SetMonoFrame(&dst_frame_, 0, dst_sample_rate_hz); 177 SetMonoFrame(0, dst_sample_rate_hz, &dst_frame_);
145 if (src_channels == 1) 178 if (src_channels == 1)
146 SetMonoFrame(&golden_frame_, dst_left, dst_sample_rate_hz); 179 SetMonoFrame(dst_ch1, dst_sample_rate_hz, &golden_frame_);
180 else if (src_channels == 2)
181 SetMonoFrame(dst_stereo_to_mono, dst_sample_rate_hz, &golden_frame_);
147 else 182 else
148 SetMonoFrame(&golden_frame_, dst_mono, dst_sample_rate_hz); 183 SetMonoFrame(dst_quad_to_mono, dst_sample_rate_hz, &golden_frame_);
149 } else { 184 } else {
150 SetStereoFrame(&dst_frame_, 0, 0, dst_sample_rate_hz); 185 SetStereoFrame(0, 0, dst_sample_rate_hz, &dst_frame_);
151 if (src_channels == 1) 186 if (src_channels == 1)
152 SetStereoFrame(&golden_frame_, dst_left, dst_left, dst_sample_rate_hz); 187 SetStereoFrame(dst_ch1, dst_ch1, dst_sample_rate_hz, &golden_frame_);
188 else if (src_channels == 2)
189 SetStereoFrame(dst_ch1, dst_ch2, dst_sample_rate_hz, &golden_frame_);
153 else 190 else
154 SetStereoFrame(&golden_frame_, dst_left, dst_right, dst_sample_rate_hz); 191 SetStereoFrame(dst_quad_to_stereo_ch1, dst_quad_to_stereo_ch2,
192 dst_sample_rate_hz, &golden_frame_);
155 } 193 }
156 194
157 // The sinc resampler has a known delay, which we compute here. Multiplying by 195 // 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 196 // two gives us a crude maximum for any resampling, as the old resampler
159 // typically (but not always) has lower delay. 197 // typically (but not always) has lower delay.
160 static const size_t kInputKernelDelaySamples = 16; 198 static const size_t kInputKernelDelaySamples = 16;
161 const size_t max_delay = static_cast<size_t>( 199 const size_t max_delay = static_cast<size_t>(
162 static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz * 200 static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz *
163 kInputKernelDelaySamples * dst_channels * 2); 201 kInputKernelDelaySamples * dst_channels * 2);
164 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later. 202 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later.
165 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz); 203 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
166 RemixAndResample(src_frame_, &resampler, &dst_frame_); 204 RemixAndResample(src_frame_, &resampler, &dst_frame_);
167 205
168 if (src_sample_rate_hz == 96000 && dst_sample_rate_hz == 8000) { 206 if (src_sample_rate_hz == 96000 && dst_sample_rate_hz == 8000) {
169 // The sinc resampler gives poor SNR at this extreme conversion, but we 207 // The sinc resampler gives poor SNR at this extreme conversion, but we
170 // expect to see this rarely in practice. 208 // expect to see this rarely in practice.
171 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f); 209 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f);
172 } else { 210 } else {
173 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f); 211 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f);
174 } 212 }
175 } 213 }
176 214
177 TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) { 215 TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) {
178 // Stereo -> stereo. 216 // Stereo -> stereo.
179 SetStereoFrame(&src_frame_, 10, 10); 217 SetStereoFrame(10, 10, &src_frame_);
180 SetStereoFrame(&dst_frame_, 0, 0); 218 SetStereoFrame(0, 0, &dst_frame_);
181 RemixAndResample(src_frame_, &resampler_, &dst_frame_); 219 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
182 VerifyFramesAreEqual(src_frame_, dst_frame_); 220 VerifyFramesAreEqual(src_frame_, dst_frame_);
183 221
184 // Mono -> mono. 222 // Mono -> mono.
185 SetMonoFrame(&src_frame_, 20); 223 SetMonoFrame(20, &src_frame_);
186 SetMonoFrame(&dst_frame_, 0); 224 SetMonoFrame(0, &dst_frame_);
187 RemixAndResample(src_frame_, &resampler_, &dst_frame_); 225 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
188 VerifyFramesAreEqual(src_frame_, dst_frame_); 226 VerifyFramesAreEqual(src_frame_, dst_frame_);
189 } 227 }
190 228
191 TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) { 229 TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) {
192 // Stereo -> mono. 230 // Stereo -> mono.
193 SetStereoFrame(&dst_frame_, 0, 0); 231 SetStereoFrame(0, 0, &dst_frame_);
194 SetMonoFrame(&src_frame_, 10); 232 SetMonoFrame(10, &src_frame_);
195 SetStereoFrame(&golden_frame_, 10, 10); 233 SetStereoFrame(10, 10, &golden_frame_);
196 RemixAndResample(src_frame_, &resampler_, &dst_frame_); 234 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
197 VerifyFramesAreEqual(dst_frame_, golden_frame_); 235 VerifyFramesAreEqual(dst_frame_, golden_frame_);
198 236
199 // Mono -> stereo. 237 // Mono -> stereo.
200 SetMonoFrame(&dst_frame_, 0); 238 SetMonoFrame(0, &dst_frame_);
201 SetStereoFrame(&src_frame_, 10, 20); 239 SetStereoFrame(10, 20, &src_frame_);
202 SetMonoFrame(&golden_frame_, 15); 240 SetMonoFrame(15, &golden_frame_);
203 RemixAndResample(src_frame_, &resampler_, &dst_frame_); 241 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
204 VerifyFramesAreEqual(golden_frame_, dst_frame_); 242 VerifyFramesAreEqual(golden_frame_, dst_frame_);
205 } 243 }
206 244
207 TEST_F(UtilityTest, RemixAndResampleSucceeds) { 245 TEST_F(UtilityTest, RemixAndResampleSucceeds) {
208 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000}; 246 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000};
209 const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates); 247 const int kSampleRatesSize = arraysize(kSampleRates);
210 const int kChannels[] = {1, 2}; 248 const int kSrcChannels[] = {1, 2, 4};
211 const int kChannelsSize = sizeof(kChannels) / sizeof(*kChannels); 249 const int kSrcChannelsSize = arraysize(kSrcChannels);
250 const int kDstChannels[] = {1, 2};
251 const int kDstChannelsSize = arraysize(kDstChannels);
252
212 for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) { 253 for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) {
213 for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) { 254 for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) {
214 for (int src_channel = 0; src_channel < kChannelsSize; src_channel++) { 255 for (int src_channel = 0; src_channel < kSrcChannelsSize;
215 for (int dst_channel = 0; dst_channel < kChannelsSize; dst_channel++) { 256 src_channel++) {
216 RunResampleTest(kChannels[src_channel], kSampleRates[src_rate], 257 for (int dst_channel = 0; dst_channel < kDstChannelsSize;
217 kChannels[dst_channel], kSampleRates[dst_rate]); 258 dst_channel++) {
259 RunResampleTest(kSrcChannels[src_channel], kSampleRates[src_rate],
260 kDstChannels[dst_channel], kSampleRates[dst_rate]);
218 } 261 }
219 } 262 }
220 } 263 }
221 } 264 }
222 } 265 }
223 266
224 } // namespace 267 } // namespace
225 } // namespace voe 268 } // namespace voe
226 } // namespace webrtc 269 } // namespace webrtc
OLDNEW
« webrtc/voice_engine/utility.cc ('K') | « webrtc/voice_engine/utility.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698