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// Unit tests for Normal class.
12
13#include "webrtc/modules/audio_coding/neteq/normal.h"
14
15#include <vector>
16
17#include "testing/gtest/include/gtest/gtest.h"
18#include "webrtc/base/scoped_ptr.h"
19#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
20#include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
21#include "webrtc/modules/audio_coding/neteq/background_noise.h"
22#include "webrtc/modules/audio_coding/neteq/expand.h"
23#include "webrtc/modules/audio_coding/neteq/mock/mock_decoder_database.h"
24#include "webrtc/modules/audio_coding/neteq/mock/mock_expand.h"
25#include "webrtc/modules/audio_coding/neteq/random_vector.h"
26#include "webrtc/modules/audio_coding/neteq/statistics_calculator.h"
27#include "webrtc/modules/audio_coding/neteq/sync_buffer.h"
28
29using ::testing::_;
30
31namespace webrtc {
32
33TEST(Normal, CreateAndDestroy) {
34  MockDecoderDatabase db;
35  int fs = 8000;
36  size_t channels = 1;
37  BackgroundNoise bgn(channels);
38  SyncBuffer sync_buffer(1, 1000);
39  RandomVector random_vector;
40  StatisticsCalculator statistics;
41  Expand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, channels);
42  Normal normal(fs, &db, bgn, &expand);
43  EXPECT_CALL(db, Die());  // Called when |db| goes out of scope.
44}
45
46TEST(Normal, AvoidDivideByZero) {
47  WebRtcSpl_Init();
48  MockDecoderDatabase db;
49  int fs = 8000;
50  size_t channels = 1;
51  BackgroundNoise bgn(channels);
52  SyncBuffer sync_buffer(1, 1000);
53  RandomVector random_vector;
54  StatisticsCalculator statistics;
55  MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs,
56                    channels);
57  Normal normal(fs, &db, bgn, &expand);
58
59  int16_t input[1000] = {0};
60  rtc::scoped_ptr<int16_t[]> mute_factor_array(new int16_t[channels]);
61  for (size_t i = 0; i < channels; ++i) {
62    mute_factor_array[i] = 16384;
63  }
64  AudioMultiVector output(channels);
65
66  // Zero input length.
67  EXPECT_EQ(
68      0,
69      normal.Process(input, 0, kModeExpand, mute_factor_array.get(), &output));
70  EXPECT_EQ(0u, output.Size());
71
72  // Try to make energy_length >> scaling = 0;
73  EXPECT_CALL(expand, SetParametersForNormalAfterExpand());
74  EXPECT_CALL(expand, Process(_));
75  EXPECT_CALL(expand, Reset());
76  // If input_size_samples < 64, then energy_length in Normal::Process() will
77  // be equal to input_size_samples. Since the input is all zeros, decoded_max
78  // will be zero, and scaling will be >= 6. Thus, energy_length >> scaling = 0,
79  // and using this as a denominator would lead to problems.
80  int input_size_samples = 63;
81  EXPECT_EQ(input_size_samples,
82            normal.Process(input,
83                           input_size_samples,
84                           kModeExpand,
85                           mute_factor_array.get(),
86                           &output));
87
88  EXPECT_CALL(db, Die());      // Called when |db| goes out of scope.
89  EXPECT_CALL(expand, Die());  // Called when |expand| goes out of scope.
90}
91
92TEST(Normal, InputLengthAndChannelsDoNotMatch) {
93  WebRtcSpl_Init();
94  MockDecoderDatabase db;
95  int fs = 8000;
96  size_t channels = 2;
97  BackgroundNoise bgn(channels);
98  SyncBuffer sync_buffer(channels, 1000);
99  RandomVector random_vector;
100  StatisticsCalculator statistics;
101  MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs,
102                    channels);
103  Normal normal(fs, &db, bgn, &expand);
104
105  int16_t input[1000] = {0};
106  rtc::scoped_ptr<int16_t[]> mute_factor_array(new int16_t[channels]);
107  for (size_t i = 0; i < channels; ++i) {
108    mute_factor_array[i] = 16384;
109  }
110  AudioMultiVector output(channels);
111
112  // Let the number of samples be one sample less than 80 samples per channel.
113  size_t input_len = 80 * channels - 1;
114  EXPECT_EQ(
115      0,
116      normal.Process(
117          input, input_len, kModeExpand, mute_factor_array.get(), &output));
118  EXPECT_EQ(0u, output.Size());
119
120  EXPECT_CALL(db, Die());      // Called when |db| goes out of scope.
121  EXPECT_CALL(expand, Die());  // Called when |expand| goes out of scope.
122}
123
124// TODO(hlundin): Write more tests.
125
126}  // namespace webrtc
127