1d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org/*
2d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *
4d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  Use of this source code is governed by a BSD-style license
5d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  that can be found in the LICENSE file in the root of the source
6d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  tree. An additional intellectual property rights grant can be found
7d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  in the file PATENTS.  All contributing project authors may
8d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org */
10d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
11d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// Unit tests for Normal class.
12d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
139c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/normal.h"
14d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
15d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org#include <vector>
16d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
173c0aae17f0e3a70fe90ecc6835926b66a3de18fbkjellander@webrtc.org#include "testing/gtest/include/gtest/gtest.h"
1800b8f6b3643332cce1ee711715f7fbb824d793cakwiberg@webrtc.org#include "webrtc/base/scoped_ptr.h"
19ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
20ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
219c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/background_noise.h"
229c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/expand.h"
239c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/mock/mock_decoder_database.h"
24ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/mock/mock_expand.h"
259c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/random_vector.h"
26bef77e234fa53a52b830b5833948711f75ab8bbbHenrik Lundin#include "webrtc/modules/audio_coding/neteq/statistics_calculator.h"
279c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/sync_buffer.h"
28ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org
29ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.orgusing ::testing::_;
30d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
31d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgnamespace webrtc {
32d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
33d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgTEST(Normal, CreateAndDestroy) {
34d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  MockDecoderDatabase db;
35d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  int fs = 8000;
36d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  size_t channels = 1;
37d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  BackgroundNoise bgn(channels);
38d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  SyncBuffer sync_buffer(1, 1000);
39d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  RandomVector random_vector;
40bef77e234fa53a52b830b5833948711f75ab8bbbHenrik Lundin  StatisticsCalculator statistics;
41bef77e234fa53a52b830b5833948711f75ab8bbbHenrik Lundin  Expand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs, channels);
42d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  Normal normal(fs, &db, bgn, &expand);
43d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  EXPECT_CALL(db, Die());  // Called when |db| goes out of scope.
44d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
45d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
46ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.orgTEST(Normal, AvoidDivideByZero) {
47ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  WebRtcSpl_Init();
48ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  MockDecoderDatabase db;
49ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  int fs = 8000;
50ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  size_t channels = 1;
51ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  BackgroundNoise bgn(channels);
52ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  SyncBuffer sync_buffer(1, 1000);
53ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  RandomVector random_vector;
54bef77e234fa53a52b830b5833948711f75ab8bbbHenrik Lundin  StatisticsCalculator statistics;
55bef77e234fa53a52b830b5833948711f75ab8bbbHenrik Lundin  MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs,
56bef77e234fa53a52b830b5833948711f75ab8bbbHenrik Lundin                    channels);
57ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  Normal normal(fs, &db, bgn, &expand);
58ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org
59ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  int16_t input[1000] = {0};
6000b8f6b3643332cce1ee711715f7fbb824d793cakwiberg@webrtc.org  rtc::scoped_ptr<int16_t[]> mute_factor_array(new int16_t[channels]);
61ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  for (size_t i = 0; i < channels; ++i) {
62ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org    mute_factor_array[i] = 16384;
63ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  }
64ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  AudioMultiVector output(channels);
65ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org
66ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  // Zero input length.
67ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  EXPECT_EQ(
68ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org      0,
69ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org      normal.Process(input, 0, kModeExpand, mute_factor_array.get(), &output));
70ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  EXPECT_EQ(0u, output.Size());
71ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org
72ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  // Try to make energy_length >> scaling = 0;
73ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  EXPECT_CALL(expand, SetParametersForNormalAfterExpand());
74ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  EXPECT_CALL(expand, Process(_));
75ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  EXPECT_CALL(expand, Reset());
76ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  // If input_size_samples < 64, then energy_length in Normal::Process() will
77ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  // be equal to input_size_samples. Since the input is all zeros, decoded_max
78ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  // will be zero, and scaling will be >= 6. Thus, energy_length >> scaling = 0,
79ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  // and using this as a denominator would lead to problems.
80ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  int input_size_samples = 63;
81ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  EXPECT_EQ(input_size_samples,
82ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org            normal.Process(input,
83ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org                           input_size_samples,
84ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org                           kModeExpand,
85ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org                           mute_factor_array.get(),
86ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org                           &output));
87ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org
88ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  EXPECT_CALL(db, Die());      // Called when |db| goes out of scope.
89ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  EXPECT_CALL(expand, Die());  // Called when |expand| goes out of scope.
90ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org}
91ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org
92ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.orgTEST(Normal, InputLengthAndChannelsDoNotMatch) {
93ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  WebRtcSpl_Init();
94ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  MockDecoderDatabase db;
95ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  int fs = 8000;
96ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  size_t channels = 2;
97ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  BackgroundNoise bgn(channels);
98ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  SyncBuffer sync_buffer(channels, 1000);
99ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  RandomVector random_vector;
100bef77e234fa53a52b830b5833948711f75ab8bbbHenrik Lundin  StatisticsCalculator statistics;
101bef77e234fa53a52b830b5833948711f75ab8bbbHenrik Lundin  MockExpand expand(&bgn, &sync_buffer, &random_vector, &statistics, fs,
102bef77e234fa53a52b830b5833948711f75ab8bbbHenrik Lundin                    channels);
103ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  Normal normal(fs, &db, bgn, &expand);
104ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org
105ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  int16_t input[1000] = {0};
10600b8f6b3643332cce1ee711715f7fbb824d793cakwiberg@webrtc.org  rtc::scoped_ptr<int16_t[]> mute_factor_array(new int16_t[channels]);
107ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  for (size_t i = 0; i < channels; ++i) {
108ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org    mute_factor_array[i] = 16384;
109ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  }
110ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  AudioMultiVector output(channels);
111ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org
112ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  // Let the number of samples be one sample less than 80 samples per channel.
113ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  size_t input_len = 80 * channels - 1;
114ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  EXPECT_EQ(
115ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org      0,
116ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org      normal.Process(
117ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org          input, input_len, kModeExpand, mute_factor_array.get(), &output));
118ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  EXPECT_EQ(0u, output.Size());
119ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org
120ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  EXPECT_CALL(db, Die());      // Called when |db| goes out of scope.
121ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  EXPECT_CALL(expand, Die());  // Called when |expand| goes out of scope.
122ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org}
123ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org
124d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org// TODO(hlundin): Write more tests.
125d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
126d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}  // namespace webrtc
127