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 27 matching lines...) Expand all Loading... | |
38 | 38 |
39 PushResampler<int16_t> resampler_; | 39 PushResampler<int16_t> resampler_; |
40 AudioFrame src_frame_; | 40 AudioFrame src_frame_; |
41 AudioFrame dst_frame_; | 41 AudioFrame dst_frame_; |
42 AudioFrame golden_frame_; | 42 AudioFrame golden_frame_; |
43 }; | 43 }; |
44 | 44 |
45 // Sets the signal value to increase by |data| with every sample. Floats are | 45 // 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 | 46 // used so non-integer values result in rounding error, but not an accumulating |
47 // error. | 47 // error. |
48 void SetMonoFrame(AudioFrame* frame, float data, int sample_rate_hz) { | 48 void SetMonoFrame(float data, int sample_rate_hz, AudioFrame* frame) { |
49 memset(frame->data_, 0, sizeof(frame->data_)); | 49 memset(frame->data_, 0, sizeof(frame->data_)); |
50 frame->num_channels_ = 1; | 50 frame->num_channels_ = 1; |
51 frame->sample_rate_hz_ = sample_rate_hz; | 51 frame->sample_rate_hz_ = sample_rate_hz; |
52 frame->samples_per_channel_ = sample_rate_hz / 100; | 52 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100); |
53 for (size_t i = 0; i < frame->samples_per_channel_; i++) { | 53 for (size_t i = 0; i < frame->samples_per_channel_; i++) { |
54 frame->data_[i] = static_cast<int16_t>(data * i); | 54 frame->data_[i] = static_cast<int16_t>(data * i); |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 // Keep the existing sample rate. | 58 // Keep the existing sample rate. |
59 void SetMonoFrame(AudioFrame* frame, float data) { | 59 void SetMonoFrame(float data, AudioFrame* frame) { |
60 SetMonoFrame(frame, data, frame->sample_rate_hz_); | 60 SetMonoFrame(data, frame->sample_rate_hz_, frame); |
61 } | 61 } |
62 | 62 |
63 // Sets the signal value to increase by |left| and |right| with every sample in | 63 // Sets the signal value to increase by |left| and |right| with every sample in |
64 // each channel respectively. | 64 // each channel respectively. |
65 void SetStereoFrame(AudioFrame* frame, float left, float right, | 65 void SetStereoFrame(float left, |
66 int sample_rate_hz) { | 66 float right, |
67 int sample_rate_hz, | |
68 AudioFrame* frame) { | |
67 memset(frame->data_, 0, sizeof(frame->data_)); | 69 memset(frame->data_, 0, sizeof(frame->data_)); |
68 frame->num_channels_ = 2; | 70 frame->num_channels_ = 2; |
69 frame->sample_rate_hz_ = sample_rate_hz; | 71 frame->sample_rate_hz_ = sample_rate_hz; |
70 frame->samples_per_channel_ = sample_rate_hz / 100; | 72 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100); |
71 for (size_t i = 0; i < frame->samples_per_channel_; i++) { | 73 for (size_t i = 0; i < frame->samples_per_channel_; i++) { |
72 frame->data_[i * 2] = static_cast<int16_t>(left * i); | 74 frame->data_[i * 2] = static_cast<int16_t>(left * i); |
73 frame->data_[i * 2 + 1] = static_cast<int16_t>(right * i); | 75 frame->data_[i * 2 + 1] = static_cast<int16_t>(right * i); |
74 } | 76 } |
75 } | 77 } |
76 | 78 |
77 // Keep the existing sample rate. | 79 // Keep the existing sample rate. |
78 void SetStereoFrame(AudioFrame* frame, float left, float right) { | 80 void SetStereoFrame(float left, float right, AudioFrame* frame) { |
79 SetStereoFrame(frame, left, right, frame->sample_rate_hz_); | 81 SetStereoFrame(left, right, frame->sample_rate_hz_, frame); |
82 } | |
83 | |
84 // Sets the signal value to increase by |ch1|, |ch2|, |ch3|, |ch4| with every | |
85 // sample in each channel respectively. | |
86 void SetQuadFrame(float ch1, | |
87 float ch2, | |
88 float ch3, | |
89 float ch4, | |
90 int sample_rate_hz, | |
91 AudioFrame* frame) { | |
92 memset(frame->data_, 0, sizeof(frame->data_)); | |
93 frame->num_channels_ = 4; | |
94 frame->sample_rate_hz_ = sample_rate_hz; | |
95 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100); | |
96 for (size_t i = 0; i < frame->samples_per_channel_; i++) { | |
97 frame->data_[i * 4] = static_cast<int16_t>(ch1 * i); | |
98 frame->data_[i * 4 + 1] = static_cast<int16_t>(ch2 * i); | |
99 frame->data_[i * 4 + 2] = static_cast<int16_t>(ch3 * i); | |
100 frame->data_[i * 4 + 3] = static_cast<int16_t>(ch4 * i); | |
101 } | |
102 } | |
103 | |
104 // Keep the existing sample rate. | |
105 void SetQuadFrame(float ch1, | |
106 float ch2, | |
107 float ch3, | |
108 float ch4, | |
109 AudioFrame* frame) { | |
110 SetQuadFrame(ch1, ch2, ch3, ch4, frame->sample_rate_hz_, frame); | |
80 } | 111 } |
81 | 112 |
82 void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) { | 113 void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) { |
83 EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_); | 114 EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_); |
84 EXPECT_EQ(ref_frame.samples_per_channel_, test_frame.samples_per_channel_); | 115 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_); | 116 EXPECT_EQ(ref_frame.sample_rate_hz_, test_frame.sample_rate_hz_); |
86 } | 117 } |
87 | 118 |
88 // Computes the best SNR based on the error between |ref_frame| and | 119 // 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 | 120 // |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 Loading... | |
121 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) { | 152 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) { |
122 EXPECT_EQ(ref_frame.data_[i], test_frame.data_[i]); | 153 EXPECT_EQ(ref_frame.data_[i], test_frame.data_[i]); |
123 } | 154 } |
124 } | 155 } |
125 | 156 |
126 void UtilityTest::RunResampleTest(int src_channels, | 157 void UtilityTest::RunResampleTest(int src_channels, |
127 int src_sample_rate_hz, | 158 int src_sample_rate_hz, |
128 int dst_channels, | 159 int dst_channels, |
129 int dst_sample_rate_hz) { | 160 int dst_sample_rate_hz) { |
130 PushResampler<int16_t> resampler; // Create a new one with every test. | 161 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. | 162 const int16_t kSrcCh1 = 30; // Shouldn't overflow for any used sample rate. |
132 const int16_t kSrcRight = 15; | 163 const int16_t kSrcCh2 = 15; |
164 const int16_t kSrcCh3 = 22; | |
165 const int16_t kSrcCh4 = 8; | |
133 const float resampling_factor = (1.0 * src_sample_rate_hz) / | 166 const float resampling_factor = (1.0 * src_sample_rate_hz) / |
134 dst_sample_rate_hz; | 167 dst_sample_rate_hz; |
135 const float dst_left = resampling_factor * kSrcLeft; | 168 const float dst_ch1 = resampling_factor * kSrcCh1; |
136 const float dst_right = resampling_factor * kSrcRight; | 169 const float dst_ch2 = resampling_factor * kSrcCh2; |
137 const float dst_mono = (dst_left + dst_right) / 2; | 170 const float dst_ch3 = resampling_factor * kSrcCh3; |
171 const float dst_ch4 = resampling_factor * kSrcCh4; | |
172 const float dst_stereo_to_mono = (dst_ch1 + dst_ch2) / 2; | |
173 const float dst_quad_to_mono = (dst_ch1 + dst_ch2 + dst_ch3 + dst_ch4) / 4; | |
174 const float dst_quad_to_stereo_ch1 = (dst_ch1 + dst_ch2) / 2; | |
175 const float dst_quad_to_stereo_ch2 = (dst_ch3 + dst_ch4) / 2; | |
138 if (src_channels == 1) | 176 if (src_channels == 1) |
139 SetMonoFrame(&src_frame_, kSrcLeft, src_sample_rate_hz); | 177 SetMonoFrame(kSrcCh1, src_sample_rate_hz, &src_frame_); |
178 else if (src_channels == 2) | |
179 SetStereoFrame(kSrcCh1, kSrcCh2, src_sample_rate_hz, &src_frame_); | |
140 else | 180 else |
141 SetStereoFrame(&src_frame_, kSrcLeft, kSrcRight, src_sample_rate_hz); | 181 SetQuadFrame(kSrcCh1, kSrcCh2, kSrcCh3, kSrcCh4, src_sample_rate_hz, |
182 &src_frame_); | |
142 | 183 |
143 if (dst_channels == 1) { | 184 if (dst_channels == 1) { |
144 SetMonoFrame(&dst_frame_, 0, dst_sample_rate_hz); | 185 SetMonoFrame(0, dst_sample_rate_hz, &dst_frame_); |
145 if (src_channels == 1) | 186 if (src_channels == 1) |
146 SetMonoFrame(&golden_frame_, dst_left, dst_sample_rate_hz); | 187 SetMonoFrame(dst_ch1, dst_sample_rate_hz, &golden_frame_); |
188 else if (src_channels == 2) | |
189 SetMonoFrame(dst_stereo_to_mono, dst_sample_rate_hz, &golden_frame_); | |
147 else | 190 else |
148 SetMonoFrame(&golden_frame_, dst_mono, dst_sample_rate_hz); | 191 SetMonoFrame(dst_quad_to_mono, dst_sample_rate_hz, &golden_frame_); |
149 } else { | 192 } else { |
150 SetStereoFrame(&dst_frame_, 0, 0, dst_sample_rate_hz); | 193 SetStereoFrame(0, 0, dst_sample_rate_hz, &dst_frame_); |
151 if (src_channels == 1) | 194 if (src_channels == 1) |
152 SetStereoFrame(&golden_frame_, dst_left, dst_left, dst_sample_rate_hz); | 195 SetStereoFrame(dst_ch1, dst_ch1, dst_sample_rate_hz, &golden_frame_); |
196 else if (src_channels == 2) | |
197 SetStereoFrame(dst_ch1, dst_ch2, dst_sample_rate_hz, &golden_frame_); | |
153 else | 198 else |
154 SetStereoFrame(&golden_frame_, dst_left, dst_right, dst_sample_rate_hz); | 199 SetStereoFrame(dst_quad_to_stereo_ch1, dst_quad_to_stereo_ch2, |
200 dst_sample_rate_hz, &golden_frame_); | |
155 } | 201 } |
156 | 202 |
157 // The sinc resampler has a known delay, which we compute here. Multiplying by | 203 // 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 | 204 // two gives us a crude maximum for any resampling, as the old resampler |
159 // typically (but not always) has lower delay. | 205 // typically (but not always) has lower delay. |
160 static const size_t kInputKernelDelaySamples = 16; | 206 static const size_t kInputKernelDelaySamples = 16; |
161 const size_t max_delay = static_cast<size_t>( | 207 const size_t max_delay = static_cast<size_t>( |
162 static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz * | 208 static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz * |
163 kInputKernelDelaySamples * dst_channels * 2); | 209 kInputKernelDelaySamples * dst_channels * 2); |
164 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later. | 210 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); | 211 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz); |
166 RemixAndResample(src_frame_, &resampler, &dst_frame_); | 212 RemixAndResample(src_frame_, &resampler, &dst_frame_); |
167 | 213 |
168 if (src_sample_rate_hz == 96000 && dst_sample_rate_hz == 8000) { | 214 if (src_sample_rate_hz == 96000 && dst_sample_rate_hz == 8000) { |
169 // The sinc resampler gives poor SNR at this extreme conversion, but we | 215 // The sinc resampler gives poor SNR at this extreme conversion, but we |
170 // expect to see this rarely in practice. | 216 // expect to see this rarely in practice. |
171 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f); | 217 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f); |
172 } else { | 218 } else { |
173 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f); | 219 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f); |
174 } | 220 } |
175 } | 221 } |
176 | 222 |
177 TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) { | 223 TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) { |
178 // Stereo -> stereo. | 224 // Stereo -> stereo. |
179 SetStereoFrame(&src_frame_, 10, 10); | 225 SetStereoFrame(10, 10, &src_frame_); |
180 SetStereoFrame(&dst_frame_, 0, 0); | 226 SetStereoFrame(0, 0, &dst_frame_); |
181 RemixAndResample(src_frame_, &resampler_, &dst_frame_); | 227 RemixAndResample(src_frame_, &resampler_, &dst_frame_); |
182 VerifyFramesAreEqual(src_frame_, dst_frame_); | 228 VerifyFramesAreEqual(src_frame_, dst_frame_); |
183 | 229 |
184 // Mono -> mono. | 230 // Mono -> mono. |
185 SetMonoFrame(&src_frame_, 20); | 231 SetMonoFrame(20, &src_frame_); |
186 SetMonoFrame(&dst_frame_, 0); | 232 SetMonoFrame(0, &dst_frame_); |
187 RemixAndResample(src_frame_, &resampler_, &dst_frame_); | 233 RemixAndResample(src_frame_, &resampler_, &dst_frame_); |
188 VerifyFramesAreEqual(src_frame_, dst_frame_); | 234 VerifyFramesAreEqual(src_frame_, dst_frame_); |
189 } | 235 } |
190 | 236 |
191 TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) { | 237 TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) { |
192 // Stereo -> mono. | 238 // Stereo -> mono. |
193 SetStereoFrame(&dst_frame_, 0, 0); | 239 SetStereoFrame(0, 0, &dst_frame_); |
194 SetMonoFrame(&src_frame_, 10); | 240 SetMonoFrame(10, &src_frame_); |
195 SetStereoFrame(&golden_frame_, 10, 10); | 241 SetStereoFrame(10, 10, &golden_frame_); |
196 RemixAndResample(src_frame_, &resampler_, &dst_frame_); | 242 RemixAndResample(src_frame_, &resampler_, &dst_frame_); |
197 VerifyFramesAreEqual(dst_frame_, golden_frame_); | 243 VerifyFramesAreEqual(dst_frame_, golden_frame_); |
198 | 244 |
199 // Mono -> stereo. | 245 // Mono -> stereo. |
200 SetMonoFrame(&dst_frame_, 0); | 246 SetMonoFrame(0, &dst_frame_); |
201 SetStereoFrame(&src_frame_, 10, 20); | 247 SetStereoFrame(10, 20, &src_frame_); |
202 SetMonoFrame(&golden_frame_, 15); | 248 SetMonoFrame(15, &golden_frame_); |
203 RemixAndResample(src_frame_, &resampler_, &dst_frame_); | 249 RemixAndResample(src_frame_, &resampler_, &dst_frame_); |
204 VerifyFramesAreEqual(golden_frame_, dst_frame_); | 250 VerifyFramesAreEqual(golden_frame_, dst_frame_); |
205 } | 251 } |
206 | 252 |
207 TEST_F(UtilityTest, RemixAndResampleSucceeds) { | 253 TEST_F(UtilityTest, RemixAndResampleSucceeds) { |
208 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000}; | 254 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000}; |
209 const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates); | 255 const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates); |
210 const int kChannels[] = {1, 2}; | 256 const int kSrcChannels[] = {1, 2, 4}; |
211 const int kChannelsSize = sizeof(kChannels) / sizeof(*kChannels); | 257 const int kDstChannels[] = {1, 2}; |
258 | |
212 for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) { | 259 for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) { |
213 for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) { | 260 for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) { |
214 for (int src_channel = 0; src_channel < kChannelsSize; src_channel++) { | 261 for (int src_channel = 0; src_channel < arraysize(kSrcChannels); |
hlundin-webrtc
2017/02/27 07:52:03
Please, also #include "webrtc/base/arraysize.h". I
jens.nielsen
2017/02/27 13:28:15
Done.
| |
215 for (int dst_channel = 0; dst_channel < kChannelsSize; dst_channel++) { | 262 src_channel++) { |
216 RunResampleTest(kChannels[src_channel], kSampleRates[src_rate], | 263 for (int dst_channel = 0; dst_channel < arraysize(kDstChannels); |
217 kChannels[dst_channel], kSampleRates[dst_rate]); | 264 dst_channel++) { |
265 RunResampleTest(kSrcChannels[src_channel], kSampleRates[src_rate], | |
266 kDstChannels[dst_channel], kSampleRates[dst_rate]); | |
218 } | 267 } |
219 } | 268 } |
220 } | 269 } |
221 } | 270 } |
222 } | 271 } |
223 | 272 |
224 } // namespace | 273 } // namespace |
225 } // namespace voe | 274 } // namespace voe |
226 } // namespace webrtc | 275 } // namespace webrtc |
OLD | NEW |