1 /*
2  *  Copyright (c) 2014 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 "webrtc/modules/audio_processing/transient/transient_suppressor.h"
12 
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/modules/audio_processing/transient/common.h"
15 
16 namespace webrtc {
17 
TEST(TransientSuppressorTest,TypingDetectionLogicWorksAsExpectedForMono)18 TEST(TransientSuppressorTest, TypingDetectionLogicWorksAsExpectedForMono) {
19   static const int kNumChannels = 1;
20 
21   TransientSuppressor ts;
22   ts.Initialize(ts::kSampleRate16kHz, ts::kSampleRate16kHz, kNumChannels);
23 
24   // Each key-press enables detection.
25   EXPECT_FALSE(ts.detection_enabled_);
26   ts.UpdateKeypress(true);
27   EXPECT_TRUE(ts.detection_enabled_);
28 
29   // It takes four seconds without any key-press to disable the detection
30   for (int time_ms = 0; time_ms < 3990; time_ms += ts::kChunkSizeMs) {
31     ts.UpdateKeypress(false);
32     EXPECT_TRUE(ts.detection_enabled_);
33   }
34   ts.UpdateKeypress(false);
35   EXPECT_FALSE(ts.detection_enabled_);
36 
37   // Key-presses that are more than a second apart from each other don't enable
38   // suppression.
39   for (int i = 0; i < 100; ++i) {
40     EXPECT_FALSE(ts.suppression_enabled_);
41     ts.UpdateKeypress(true);
42     EXPECT_TRUE(ts.detection_enabled_);
43     EXPECT_FALSE(ts.suppression_enabled_);
44     for (int time_ms = 0; time_ms < 990; time_ms += ts::kChunkSizeMs) {
45       ts.UpdateKeypress(false);
46       EXPECT_TRUE(ts.detection_enabled_);
47       EXPECT_FALSE(ts.suppression_enabled_);
48     }
49     ts.UpdateKeypress(false);
50   }
51 
52   // Two consecutive key-presses is enough to enable the suppression.
53   ts.UpdateKeypress(true);
54   EXPECT_FALSE(ts.suppression_enabled_);
55   ts.UpdateKeypress(true);
56   EXPECT_TRUE(ts.suppression_enabled_);
57 
58   // Key-presses that are less than a second apart from each other don't disable
59   // detection nor suppression.
60   for (int i = 0; i < 100; ++i) {
61     for (int time_ms = 0; time_ms < 1000; time_ms += ts::kChunkSizeMs) {
62       ts.UpdateKeypress(false);
63       EXPECT_TRUE(ts.detection_enabled_);
64       EXPECT_TRUE(ts.suppression_enabled_);
65     }
66     ts.UpdateKeypress(true);
67     EXPECT_TRUE(ts.detection_enabled_);
68     EXPECT_TRUE(ts.suppression_enabled_);
69   }
70 
71   // It takes four seconds without any key-press to disable the detection and
72   // suppression.
73   for (int time_ms = 0; time_ms < 3990; time_ms += ts::kChunkSizeMs) {
74     ts.UpdateKeypress(false);
75     EXPECT_TRUE(ts.detection_enabled_);
76     EXPECT_TRUE(ts.suppression_enabled_);
77   }
78   for (int time_ms = 0; time_ms < 1000; time_ms += ts::kChunkSizeMs) {
79     ts.UpdateKeypress(false);
80     EXPECT_FALSE(ts.detection_enabled_);
81     EXPECT_FALSE(ts.suppression_enabled_);
82   }
83 }
84 
85 }  // namespace webrtc
86