Line data Source code
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/typing_detection.h"
12 :
13 : namespace webrtc {
14 :
15 0 : TypingDetection::TypingDetection()
16 : : time_active_(0),
17 : time_since_last_typing_(0),
18 : penalty_counter_(0),
19 : counter_since_last_detection_update_(0),
20 : detection_to_report_(false),
21 : new_detection_to_report_(false),
22 : time_window_(10),
23 : cost_per_typing_(100),
24 : reporting_threshold_(300),
25 : penalty_decay_(1),
26 : type_event_delay_(2),
27 0 : report_detection_update_period_(1) {
28 0 : }
29 :
30 0 : TypingDetection::~TypingDetection() {}
31 :
32 0 : bool TypingDetection::Process(bool key_pressed, bool vad_activity) {
33 0 : if (vad_activity)
34 0 : time_active_++;
35 : else
36 0 : time_active_ = 0;
37 :
38 : // Keep track if time since last typing event
39 0 : if (key_pressed)
40 0 : time_since_last_typing_ = 0;
41 : else
42 0 : ++time_since_last_typing_;
43 :
44 0 : if (time_since_last_typing_ < type_event_delay_ &&
45 0 : vad_activity &&
46 0 : time_active_ < time_window_) {
47 0 : penalty_counter_ += cost_per_typing_;
48 0 : if (penalty_counter_ > reporting_threshold_)
49 0 : new_detection_to_report_ = true;
50 : }
51 :
52 0 : if (penalty_counter_ > 0)
53 0 : penalty_counter_ -= penalty_decay_;
54 :
55 0 : if (++counter_since_last_detection_update_ ==
56 0 : report_detection_update_period_) {
57 0 : detection_to_report_ = new_detection_to_report_;
58 0 : new_detection_to_report_ = false;
59 0 : counter_since_last_detection_update_ = 0;
60 : }
61 :
62 0 : return detection_to_report_;
63 : }
64 :
65 0 : int TypingDetection::TimeSinceLastDetectionInSeconds() {
66 : // Round to whole seconds.
67 0 : return (time_since_last_typing_ + 50) / 100;
68 : }
69 :
70 0 : void TypingDetection::SetParameters(int time_window,
71 : int cost_per_typing,
72 : int reporting_threshold,
73 : int penalty_decay,
74 : int type_event_delay,
75 : int report_detection_update_period) {
76 0 : if (time_window) time_window_ = time_window;
77 :
78 0 : if (cost_per_typing) cost_per_typing_ = cost_per_typing;
79 :
80 0 : if (reporting_threshold) reporting_threshold_ = reporting_threshold;
81 :
82 0 : if (penalty_decay) penalty_decay_ = penalty_decay;
83 :
84 0 : if (type_event_delay) type_event_delay_ = type_event_delay;
85 :
86 0 : if (report_detection_update_period)
87 0 : report_detection_update_period_ = report_detection_update_period;
88 0 : }
89 :
90 : } // namespace webrtc
|