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 #ifndef MODULES_AUDIO_PROCESSING_TYPING_DETECTION_H_
12 #define MODULES_AUDIO_PROCESSING_TYPING_DETECTION_H_
13 
14 #include "rtc_base/system/rtc_export.h"
15 
16 namespace webrtc {
17 
18 class RTC_EXPORT TypingDetection {
19  public:
20   TypingDetection();
21   virtual ~TypingDetection();
22 
23   // Run the detection algortihm. Shall be called every 10 ms. Returns true if
24   // typing is detected, or false if not, based on the update period as set with
25   // SetParameters(). See |report_detection_update_period_| description below.
26   bool Process(bool key_pressed, bool vad_activity);
27 
28   // Gets the time in seconds since the last detection.
29   int TimeSinceLastDetectionInSeconds();
30 
31   // Sets the algorithm parameters. A parameter value of 0 leaves it unchanged.
32   // See the correspondning member variables below for descriptions.
33   void SetParameters(int time_window,
34                      int cost_per_typing,
35                      int reporting_threshold,
36                      int penalty_decay,
37                      int type_event_delay,
38                      int report_detection_update_period);
39 
40  private:
41   int time_active_;
42   int time_since_last_typing_;
43   int penalty_counter_;
44 
45   // Counter since last time the detection status reported by Process() was
46   // updated. See also |report_detection_update_period_|.
47   int counter_since_last_detection_update_;
48 
49   // The detection status to report. Updated every
50   // |report_detection_update_period_| call to Process().
51   bool detection_to_report_;
52 
53   // What |detection_to_report_| should be set to next time it is updated.
54   bool new_detection_to_report_;
55 
56   // Settable threshold values.
57 
58   // Number of 10 ms slots accepted to count as a hit.
59   int time_window_;
60 
61   // Penalty added for a typing + activity coincide.
62   int cost_per_typing_;
63 
64   // Threshold for |penalty_counter_|.
65   int reporting_threshold_;
66 
67   // How much we reduce |penalty_counter_| every 10 ms.
68   int penalty_decay_;
69 
70   // How old typing events we allow.
71   int type_event_delay_;
72 
73   // Settable update period.
74 
75   // Number of 10 ms slots between each update of the detection status returned
76   // by Process(). This inertia added to the algorithm is usually desirable and
77   // provided so that consumers of the class don't have to implement that
78   // themselves if they don't wish.
79   // If set to 1, each call to Process() will return the detection status for
80   // that 10 ms slot.
81   // If set to N (where N > 1), the detection status returned from Process()
82   // will remain the same until Process() has been called N times. Then, if none
83   // of the last N calls to Process() has detected typing for each respective
84   // 10 ms slot, Process() will return false. If at least one of the last N
85   // calls has detected typing, Process() will return true. And that returned
86   // status will then remain the same until the next N calls have been done.
87   int report_detection_update_period_;
88 };
89 
90 }  // namespace webrtc
91 
92 #endif  // #ifndef MODULES_AUDIO_PROCESSING_TYPING_DETECTION_H_
93