summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/audio_coding/neteq/dtmf_tone_generator_unittest.cc
blob: e843706dd3a035b5a1aff03f2e071a039bd5e28a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

// Unit tests for DtmfToneGenerator class.

#include "modules/audio_coding/neteq/dtmf_tone_generator.h"

#include <math.h>

#include "common_audio/include/audio_util.h"
#include "modules/audio_coding/neteq/audio_multi_vector.h"
#include "rtc_base/strings/string_builder.h"
#include "test/gtest.h"

namespace webrtc {

class DtmfToneGeneratorTest : public ::testing::Test {
 protected:
  static const double kLowFreqHz[16];
  static const double kHighFreqHz[16];
  // This is the attenuation applied to all cases.
  const double kBaseAttenuation = 16141.0 / 16384.0;
  const double k3dbAttenuation = 23171.0 / 32768;
  const int kNumSamples = 10;

  void TestAllTones(int fs_hz, int channels) {
    AudioMultiVector signal(channels);

    for (int event = 0; event <= 15; ++event) {
      rtc::StringBuilder ss;
      ss << "Checking event " << event << " at sample rate " << fs_hz;
      SCOPED_TRACE(ss.str());
      const int kAttenuation = 0;
      ASSERT_EQ(0, tone_gen_.Init(fs_hz, event, kAttenuation));
      EXPECT_TRUE(tone_gen_.initialized());
      EXPECT_EQ(kNumSamples, tone_gen_.Generate(kNumSamples, &signal));

      double f1 = kLowFreqHz[event];
      double f2 = kHighFreqHz[event];
      const double pi = 3.14159265358979323846;

      for (int n = 0; n < kNumSamples; ++n) {
        double x = k3dbAttenuation * sin(2.0 * pi * f1 / fs_hz * (-n - 1)) +
                   sin(2.0 * pi * f2 / fs_hz * (-n - 1));
        x *= kBaseAttenuation;
        x = ldexp(x, 14);  // Scale to Q14.
        for (int channel = 0; channel < channels; ++channel) {
          EXPECT_NEAR(x, static_cast<double>(signal[channel][n]), 25);
        }
      }

      tone_gen_.Reset();
      EXPECT_FALSE(tone_gen_.initialized());
    }
  }

  void TestAmplitudes(int fs_hz, int channels) {
    AudioMultiVector signal(channels);
    AudioMultiVector ref_signal(channels);

    const int event_vec[] = {0, 4, 9, 13};  // Test a few events.
    for (int e = 0; e < 4; ++e) {
      int event = event_vec[e];
      // Create full-scale reference.
      ASSERT_EQ(0, tone_gen_.Init(fs_hz, event, 0));  // 0 attenuation.
      EXPECT_EQ(kNumSamples, tone_gen_.Generate(kNumSamples, &ref_signal));
      // Test every 5 steps (to save time).
      for (int attenuation = 1; attenuation <= 63; attenuation += 5) {
        rtc::StringBuilder ss;
        ss << "Checking event " << event << " at sample rate " << fs_hz;
        ss << "; attenuation " << attenuation;
        SCOPED_TRACE(ss.str());
        ASSERT_EQ(0, tone_gen_.Init(fs_hz, event, attenuation));
        EXPECT_EQ(kNumSamples, tone_gen_.Generate(kNumSamples, &signal));
        for (int n = 0; n < kNumSamples; ++n) {
          double attenuation_factor =
              DbToRatio(-static_cast<float>(attenuation));
          // Verify that the attenuation is correct.
          for (int channel = 0; channel < channels; ++channel) {
            EXPECT_NEAR(attenuation_factor * ref_signal[channel][n],
                        signal[channel][n], 2);
          }
        }

        tone_gen_.Reset();
      }
    }
  }

  DtmfToneGenerator tone_gen_;
};

// Low and high frequencies for events 0 through 15.
const double DtmfToneGeneratorTest::kLowFreqHz[16] = {
    941.0, 697.0, 697.0, 697.0, 770.0, 770.0, 770.0, 852.0,
    852.0, 852.0, 941.0, 941.0, 697.0, 770.0, 852.0, 941.0};
const double DtmfToneGeneratorTest::kHighFreqHz[16] = {
    1336.0, 1209.0, 1336.0, 1477.0, 1209.0, 1336.0, 1477.0, 1209.0,
    1336.0, 1477.0, 1209.0, 1477.0, 1633.0, 1633.0, 1633.0, 1633.0};

TEST_F(DtmfToneGeneratorTest, Test8000Mono) {
  TestAllTones(8000, 1);
  TestAmplitudes(8000, 1);
}

TEST_F(DtmfToneGeneratorTest, Test16000Mono) {
  TestAllTones(16000, 1);
  TestAmplitudes(16000, 1);
}

TEST_F(DtmfToneGeneratorTest, Test32000Mono) {
  TestAllTones(32000, 1);
  TestAmplitudes(32000, 1);
}

TEST_F(DtmfToneGeneratorTest, Test48000Mono) {
  TestAllTones(48000, 1);
  TestAmplitudes(48000, 1);
}

TEST_F(DtmfToneGeneratorTest, Test8000Stereo) {
  TestAllTones(8000, 2);
  TestAmplitudes(8000, 2);
}

TEST_F(DtmfToneGeneratorTest, Test16000Stereo) {
  TestAllTones(16000, 2);
  TestAmplitudes(16000, 2);
}

TEST_F(DtmfToneGeneratorTest, Test32000Stereo) {
  TestAllTones(32000, 2);
  TestAmplitudes(32000, 2);
}

TEST_F(DtmfToneGeneratorTest, Test48000Stereo) {
  TestAllTones(48000, 2);
  TestAmplitudes(48000, 2);
}

TEST(DtmfToneGenerator, TestErrors) {
  DtmfToneGenerator tone_gen;
  const int kNumSamples = 10;
  AudioMultiVector signal(1);  // One channel.

  // Try to generate tones without initializing.
  EXPECT_EQ(DtmfToneGenerator::kNotInitialized,
            tone_gen.Generate(kNumSamples, &signal));

  const int fs = 16000;       // Valid sample rate.
  const int event = 7;        // Valid event.
  const int attenuation = 0;  // Valid attenuation.
  // Initialize with invalid event -1.
  EXPECT_EQ(DtmfToneGenerator::kParameterError,
            tone_gen.Init(fs, -1, attenuation));
  // Initialize with invalid event 16.
  EXPECT_EQ(DtmfToneGenerator::kParameterError,
            tone_gen.Init(fs, 16, attenuation));
  // Initialize with invalid attenuation -1.
  EXPECT_EQ(DtmfToneGenerator::kParameterError, tone_gen.Init(fs, event, -1));
  // Initialize with invalid attenuation 64.
  EXPECT_EQ(DtmfToneGenerator::kParameterError, tone_gen.Init(fs, event, 64));
  EXPECT_FALSE(tone_gen.initialized());  // Should still be uninitialized.

  // Initialize with valid parameters.
  ASSERT_EQ(0, tone_gen.Init(fs, event, attenuation));
  EXPECT_TRUE(tone_gen.initialized());
  // NULL pointer to destination.
  EXPECT_EQ(DtmfToneGenerator::kParameterError,
            tone_gen.Generate(kNumSamples, NULL));
}

}  // namespace webrtc