andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <math.h> |
| 12 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | #include "webrtc/voice_engine/output_mixer.h" |
| 15 | #include "webrtc/voice_engine/output_mixer_internal.h" |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | namespace voe { |
| 19 | namespace { |
| 20 | |
| 21 | class OutputMixerTest : public ::testing::Test { |
| 22 | protected: |
| 23 | OutputMixerTest() { |
| 24 | src_frame_.sample_rate_hz_ = 16000; |
| 25 | src_frame_.samples_per_channel_ = src_frame_.sample_rate_hz_ / 100; |
| 26 | src_frame_.num_channels_ = 1; |
andrew@webrtc.org | ae1a58b | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 27 | dst_frame_.CopyFrom(src_frame_); |
| 28 | golden_frame_.CopyFrom(src_frame_); |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void RunResampleTest(int src_channels, int src_sample_rate_hz, |
| 32 | int dst_channels, int dst_sample_rate_hz); |
| 33 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 34 | PushResampler resampler_; |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 35 | AudioFrame src_frame_; |
| 36 | AudioFrame dst_frame_; |
| 37 | AudioFrame golden_frame_; |
| 38 | }; |
| 39 | |
| 40 | // Sets the signal value to increase by |data| with every sample. Floats are |
| 41 | // used so non-integer values result in rounding error, but not an accumulating |
| 42 | // error. |
| 43 | void SetMonoFrame(AudioFrame* frame, float data, int sample_rate_hz) { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 44 | memset(frame->data_, 0, sizeof(frame->data_)); |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 45 | frame->num_channels_ = 1; |
| 46 | frame->sample_rate_hz_ = sample_rate_hz; |
| 47 | frame->samples_per_channel_ = sample_rate_hz / 100; |
| 48 | for (int i = 0; i < frame->samples_per_channel_; i++) { |
| 49 | frame->data_[i] = data * i; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Keep the existing sample rate. |
| 54 | void SetMonoFrame(AudioFrame* frame, float data) { |
| 55 | SetMonoFrame(frame, data, frame->sample_rate_hz_); |
| 56 | } |
| 57 | |
| 58 | // Sets the signal value to increase by |left| and |right| with every sample in |
| 59 | // each channel respectively. |
| 60 | void SetStereoFrame(AudioFrame* frame, float left, float right, |
| 61 | int sample_rate_hz) { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 62 | memset(frame->data_, 0, sizeof(frame->data_)); |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 63 | frame->num_channels_ = 2; |
| 64 | frame->sample_rate_hz_ = sample_rate_hz; |
| 65 | frame->samples_per_channel_ = sample_rate_hz / 100; |
| 66 | for (int i = 0; i < frame->samples_per_channel_; i++) { |
| 67 | frame->data_[i * 2] = left * i; |
| 68 | frame->data_[i * 2 + 1] = right * i; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Keep the existing sample rate. |
| 73 | void SetStereoFrame(AudioFrame* frame, float left, float right) { |
| 74 | SetStereoFrame(frame, left, right, frame->sample_rate_hz_); |
| 75 | } |
| 76 | |
| 77 | void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) { |
| 78 | EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_); |
| 79 | EXPECT_EQ(ref_frame.samples_per_channel_, test_frame.samples_per_channel_); |
| 80 | EXPECT_EQ(ref_frame.sample_rate_hz_, test_frame.sample_rate_hz_); |
| 81 | } |
| 82 | |
| 83 | // Computes the best SNR based on the error between |ref_frame| and |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 84 | // |test_frame|. It allows for up to a |max_delay| in samples between the |
| 85 | // signals to compensate for the resampling delay. |
| 86 | float ComputeSNR(const AudioFrame& ref_frame, const AudioFrame& test_frame, |
| 87 | int max_delay) { |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 88 | VerifyParams(ref_frame, test_frame); |
| 89 | float best_snr = 0; |
| 90 | int best_delay = 0; |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 91 | for (int delay = 0; delay <= max_delay; delay++) { |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 92 | float mse = 0; |
| 93 | float variance = 0; |
| 94 | for (int i = 0; i < ref_frame.samples_per_channel_ * |
| 95 | ref_frame.num_channels_ - delay; i++) { |
| 96 | int error = ref_frame.data_[i] - test_frame.data_[i + delay]; |
| 97 | mse += error * error; |
| 98 | variance += ref_frame.data_[i] * ref_frame.data_[i]; |
| 99 | } |
| 100 | float snr = 100; // We assign 100 dB to the zero-error case. |
| 101 | if (mse > 0) |
| 102 | snr = 10 * log10(variance / mse); |
| 103 | if (snr > best_snr) { |
| 104 | best_snr = snr; |
| 105 | best_delay = delay; |
| 106 | } |
| 107 | } |
| 108 | printf("SNR=%.1f dB at delay=%d\n", best_snr, best_delay); |
| 109 | return best_snr; |
| 110 | } |
| 111 | |
| 112 | void VerifyFramesAreEqual(const AudioFrame& ref_frame, |
| 113 | const AudioFrame& test_frame) { |
| 114 | VerifyParams(ref_frame, test_frame); |
| 115 | for (int i = 0; i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; |
| 116 | i++) { |
| 117 | EXPECT_EQ(ref_frame.data_[i], test_frame.data_[i]); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void OutputMixerTest::RunResampleTest(int src_channels, |
| 122 | int src_sample_rate_hz, |
| 123 | int dst_channels, |
| 124 | int dst_sample_rate_hz) { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 125 | PushResampler resampler; // Create a new one with every test. |
| 126 | const int16_t kSrcLeft = 30; // Shouldn't overflow for any used sample rate. |
| 127 | const int16_t kSrcRight = 15; |
| 128 | const float resampling_factor = (1.0 * src_sample_rate_hz) / |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 129 | dst_sample_rate_hz; |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 130 | const float dst_left = resampling_factor * kSrcLeft; |
| 131 | const float dst_right = resampling_factor * kSrcRight; |
| 132 | const float dst_mono = (dst_left + dst_right) / 2; |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 133 | if (src_channels == 1) |
| 134 | SetMonoFrame(&src_frame_, kSrcLeft, src_sample_rate_hz); |
| 135 | else |
| 136 | SetStereoFrame(&src_frame_, kSrcLeft, kSrcRight, src_sample_rate_hz); |
| 137 | |
| 138 | if (dst_channels == 1) { |
| 139 | SetMonoFrame(&dst_frame_, 0, dst_sample_rate_hz); |
| 140 | if (src_channels == 1) |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 141 | SetMonoFrame(&golden_frame_, dst_left, dst_sample_rate_hz); |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 142 | else |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 143 | SetMonoFrame(&golden_frame_, dst_mono, dst_sample_rate_hz); |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 144 | } else { |
| 145 | SetStereoFrame(&dst_frame_, 0, 0, dst_sample_rate_hz); |
| 146 | if (src_channels == 1) |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 147 | SetStereoFrame(&golden_frame_, dst_left, dst_left, dst_sample_rate_hz); |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 148 | else |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 149 | SetStereoFrame(&golden_frame_, dst_left, dst_right, dst_sample_rate_hz); |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 150 | } |
| 151 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 152 | // The sinc resampler has a known delay, which we compute here. Multiplying by |
| 153 | // two gives us a crude maximum for any resampling, as the old resampler |
| 154 | // typically (but not always) has lower delay. |
| 155 | static const int kInputKernelDelaySamples = 16; |
| 156 | const int max_delay = static_cast<double>(dst_sample_rate_hz) |
| 157 | / src_sample_rate_hz * kInputKernelDelaySamples * dst_channels * 2; |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 158 | printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later. |
| 159 | src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz); |
| 160 | EXPECT_EQ(0, RemixAndResample(src_frame_, &resampler, &dst_frame_)); |
andrew@webrtc.org | c1eb560 | 2013-06-03 19:00:29 +0000 | [diff] [blame^] | 161 | if (src_sample_rate_hz == 96000 && dst_sample_rate_hz == 8000) { |
| 162 | // The sinc resampler gives poor SNR at this extreme conversion, but we |
| 163 | // expect to see this rarely in practice. |
| 164 | EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f); |
| 165 | } else { |
| 166 | EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f); |
| 167 | } |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | TEST_F(OutputMixerTest, RemixAndResampleCopyFrameSucceeds) { |
| 171 | // Stereo -> stereo. |
| 172 | SetStereoFrame(&src_frame_, 10, 10); |
| 173 | SetStereoFrame(&dst_frame_, 0, 0); |
| 174 | EXPECT_EQ(0, RemixAndResample(src_frame_, &resampler_, &dst_frame_)); |
| 175 | VerifyFramesAreEqual(src_frame_, dst_frame_); |
| 176 | |
| 177 | // Mono -> mono. |
| 178 | SetMonoFrame(&src_frame_, 20); |
| 179 | SetMonoFrame(&dst_frame_, 0); |
| 180 | EXPECT_EQ(0, RemixAndResample(src_frame_, &resampler_, &dst_frame_)); |
| 181 | VerifyFramesAreEqual(src_frame_, dst_frame_); |
| 182 | } |
| 183 | |
| 184 | TEST_F(OutputMixerTest, RemixAndResampleMixingOnlySucceeds) { |
| 185 | // Stereo -> mono. |
| 186 | SetStereoFrame(&dst_frame_, 0, 0); |
| 187 | SetMonoFrame(&src_frame_, 10); |
| 188 | SetStereoFrame(&golden_frame_, 10, 10); |
| 189 | EXPECT_EQ(0, RemixAndResample(src_frame_, &resampler_, &dst_frame_)); |
| 190 | VerifyFramesAreEqual(dst_frame_, golden_frame_); |
| 191 | |
| 192 | // Mono -> stereo. |
| 193 | SetMonoFrame(&dst_frame_, 0); |
| 194 | SetStereoFrame(&src_frame_, 10, 20); |
| 195 | SetMonoFrame(&golden_frame_, 15); |
| 196 | EXPECT_EQ(0, RemixAndResample(src_frame_, &resampler_, &dst_frame_)); |
| 197 | VerifyFramesAreEqual(golden_frame_, dst_frame_); |
| 198 | } |
| 199 | |
| 200 | TEST_F(OutputMixerTest, RemixAndResampleSucceeds) { |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 201 | // TODO(ajm): convert this to the parameterized TEST_P style used in |
| 202 | // sinc_resampler_unittest.cc. We can then easily add tighter SNR thresholds. |
| 203 | const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000}; |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 204 | const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates); |
| 205 | const int kChannels[] = {1, 2}; |
| 206 | const int kChannelsSize = sizeof(kChannels) / sizeof(*kChannels); |
| 207 | for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) { |
| 208 | for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) { |
| 209 | for (int src_channel = 0; src_channel < kChannelsSize; src_channel++) { |
| 210 | for (int dst_channel = 0; dst_channel < kChannelsSize; dst_channel++) { |
| 211 | RunResampleTest(kChannels[src_channel], kSampleRates[src_rate], |
| 212 | kChannels[dst_channel], kSampleRates[dst_rate]); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | } // namespace |
| 220 | } // namespace voe |
| 221 | } // namespace webrtc |