1c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org/*
2c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org *
4c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org *  Use of this source code is governed by a BSD-style license
5c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org *  that can be found in the LICENSE file in the root of the source
6c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org *  tree. An additional intellectual property rights grant can be found
7c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org *  in the file PATENTS.  All contributing project authors may
8c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org */
10c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
11c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org#include "webrtc/modules/audio_processing/typing_detection.h"
12c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
13c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.orgnamespace webrtc {
14c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
15c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.orgTypingDetection::TypingDetection()
16c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org    : time_active_(0),
17c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      time_since_last_typing_(0),
18c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      penalty_counter_(0),
19c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      counter_since_last_detection_update_(0),
20c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      detection_to_report_(false),
21c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      new_detection_to_report_(false),
22c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      time_window_(10),
23c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      cost_per_typing_(100),
24c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      reporting_threshold_(300),
25c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      penalty_decay_(1),
26c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      type_event_delay_(2),
27c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      report_detection_update_period_(1) {
28c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org}
29c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
30c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.orgTypingDetection::~TypingDetection() {}
31c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
32c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.orgbool TypingDetection::Process(bool key_pressed, bool vad_activity) {
33c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  if (vad_activity)
34c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org    time_active_++;
35c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  else
36c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org    time_active_ = 0;
37c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
38c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  // Keep track if time since last typing event
39c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  if (key_pressed)
40c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org    time_since_last_typing_ = 0;
41c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  else
42c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org    ++time_since_last_typing_;
43c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
44c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  if (time_since_last_typing_ < type_event_delay_ &&
45c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      vad_activity &&
46c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      time_active_ < time_window_) {
47c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org    penalty_counter_ += cost_per_typing_;
48c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org    if (penalty_counter_ > reporting_threshold_)
49c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      new_detection_to_report_ = true;
50c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  }
51c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
52c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  if (penalty_counter_ > 0)
53c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org    penalty_counter_ -= penalty_decay_;
54c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
55c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  if (++counter_since_last_detection_update_ ==
56c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org      report_detection_update_period_) {
57c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org    detection_to_report_ = new_detection_to_report_;
58c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org    new_detection_to_report_ = false;
59c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org    counter_since_last_detection_update_ = 0;
60c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  }
61c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
62c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  return detection_to_report_;
63c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org}
64c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
65c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.orgint TypingDetection::TimeSinceLastDetectionInSeconds() {
66c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  // Round to whole seconds.
67c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  return (time_since_last_typing_ + 50) / 100;
68c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org}
69c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
70c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.orgvoid TypingDetection::SetParameters(int time_window,
71c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org                                    int cost_per_typing,
72c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org                                    int reporting_threshold,
73c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org                                    int penalty_decay,
74c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org                                    int type_event_delay,
75c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org                                    int report_detection_update_period) {
76c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  if (time_window) time_window_ = time_window;
77c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
78c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  if (cost_per_typing) cost_per_typing_ = cost_per_typing;
79c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
80c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  if (reporting_threshold) reporting_threshold_ = reporting_threshold;
81c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
82c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  if (penalty_decay) penalty_decay_ = penalty_decay;
83c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
84c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  if (type_event_delay) type_event_delay_ = type_event_delay;
85c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
86c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org  if (report_detection_update_period)
87c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org    report_detection_update_period_ = report_detection_update_period;
88c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org}
89c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org
90c13a5372fe8e8ef925b6867e7e0fb5a14254ea66henrikg@webrtc.org}  // namespace webrtc
91