dsp_helper_unittest.cc (2997B)
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 "modules/audio_coding/neteq/dsp_helper.h" 12 13 #include <cstdint> 14 15 #include "modules/audio_coding/neteq/audio_multi_vector.h" 16 #include "test/gtest.h" 17 18 namespace webrtc { 19 20 TEST(DspHelper, RampSignalArray) { 21 static const int kLen = 100; 22 int16_t input[kLen]; 23 int16_t output[kLen]; 24 // Fill input with 1000. 25 for (int i = 0; i < kLen; ++i) { 26 input[i] = 1000; 27 } 28 int start_factor = 0; 29 // Ramp from 0 to 1 (in Q14) over the array. Note that `increment` is in Q20, 30 // while the factor is in Q14, hence the shift by 6. 31 int increment = (16384 << 6) / kLen; 32 33 // Test first method. 34 int stop_factor = 35 DspHelper::RampSignal(input, kLen, start_factor, increment, output); 36 EXPECT_EQ(16383, stop_factor); // Almost reach 1 in Q14. 37 for (int i = 0; i < kLen; ++i) { 38 EXPECT_EQ(1000 * i / kLen, output[i]); 39 } 40 41 // Test second method. (Note that this modifies `input`.) 42 stop_factor = DspHelper::RampSignal(input, kLen, start_factor, increment); 43 EXPECT_EQ(16383, stop_factor); // Almost reach 1 in Q14. 44 for (int i = 0; i < kLen; ++i) { 45 EXPECT_EQ(1000 * i / kLen, input[i]); 46 } 47 } 48 49 TEST(DspHelper, RampSignalAudioMultiVector) { 50 static const int kLen = 100; 51 static const int kChannels = 5; 52 AudioMultiVector input(kChannels, kLen * 3); 53 // Fill input with 1000. 54 for (int i = 0; i < kLen * 3; ++i) { 55 for (int channel = 0; channel < kChannels; ++channel) { 56 input[channel][i] = 1000; 57 } 58 } 59 // We want to start ramping at `start_index` and keep ramping for `kLen` 60 // samples. 61 int start_index = kLen; 62 int start_factor = 0; 63 // Ramp from 0 to 1 (in Q14) in `kLen` samples. Note that `increment` is in 64 // Q20, while the factor is in Q14, hence the shift by 6. 65 int increment = (16384 << 6) / kLen; 66 67 int stop_factor = 68 DspHelper::RampSignal(&input, start_index, kLen, start_factor, increment); 69 EXPECT_EQ(16383, stop_factor); // Almost reach 1 in Q14. 70 // Verify that the first `kLen` samples are left untouched. 71 int i; 72 for (i = 0; i < kLen; ++i) { 73 for (int channel = 0; channel < kChannels; ++channel) { 74 EXPECT_EQ(1000, input[channel][i]); 75 } 76 } 77 // Verify that the next block of `kLen` samples are ramped. 78 for (; i < 2 * kLen; ++i) { 79 for (int channel = 0; channel < kChannels; ++channel) { 80 EXPECT_EQ(1000 * (i - kLen) / kLen, input[channel][i]); 81 } 82 } 83 // Verify the last `kLen` samples are left untouched. 84 for (; i < 3 * kLen; ++i) { 85 for (int channel = 0; channel < kChannels; ++channel) { 86 EXPECT_EQ(1000, input[channel][i]); 87 } 88 } 89 } 90 } // namespace webrtc