2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/audio_processing/typing_detection.h"
15 TypingDetection::TypingDetection()
17 time_since_last_typing_(0),
19 counter_since_last_detection_update_(0),
20 detection_to_report_(false),
21 new_detection_to_report_(false),
23 cost_per_typing_(100),
24 reporting_threshold_(300),
27 report_detection_update_period_(1) {
30 TypingDetection::~TypingDetection() {}
32 bool TypingDetection::Process(bool key_pressed, bool vad_activity) {
38 // Keep track if time since last typing event
40 time_since_last_typing_ = 0;
42 ++time_since_last_typing_;
44 if (time_since_last_typing_ < type_event_delay_ &&
46 time_active_ < time_window_) {
47 penalty_counter_ += cost_per_typing_;
48 if (penalty_counter_ > reporting_threshold_)
49 new_detection_to_report_ = true;
52 if (penalty_counter_ > 0)
53 penalty_counter_ -= penalty_decay_;
55 if (++counter_since_last_detection_update_ ==
56 report_detection_update_period_) {
57 detection_to_report_ = new_detection_to_report_;
58 new_detection_to_report_ = false;
59 counter_since_last_detection_update_ = 0;
62 return detection_to_report_;
65 int TypingDetection::TimeSinceLastDetectionInSeconds() {
66 // Round to whole seconds.
67 return (time_since_last_typing_ + 50) / 100;
70 void TypingDetection::SetParameters(int time_window,
72 int reporting_threshold,
75 int report_detection_update_period) {
76 if (time_window) time_window_ = time_window;
78 if (cost_per_typing) cost_per_typing_ = cost_per_typing;
80 if (reporting_threshold) reporting_threshold_ = reporting_threshold;
82 if (penalty_decay) penalty_decay_ = penalty_decay;
84 if (type_event_delay) type_event_delay_ = type_event_delay;
86 if (report_detection_update_period)
87 report_detection_update_period_ = report_detection_update_period;