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