henrikg@webrtc.org | c693704 | 2014-01-30 09:50:46 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/typing_detection.h" |
henrikg@webrtc.org | c693704 | 2014-01-30 09:50:46 +0000 | [diff] [blame] | 12 | |
| 13 | namespace webrtc { |
| 14 | |
| 15 | 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), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 27 | report_detection_update_period_(1) {} |
henrikg@webrtc.org | c693704 | 2014-01-30 09:50:46 +0000 | [diff] [blame] | 28 | |
| 29 | TypingDetection::~TypingDetection() {} |
| 30 | |
| 31 | bool TypingDetection::Process(bool key_pressed, bool vad_activity) { |
| 32 | if (vad_activity) |
| 33 | time_active_++; |
| 34 | else |
| 35 | time_active_ = 0; |
| 36 | |
| 37 | // Keep track if time since last typing event |
| 38 | if (key_pressed) |
| 39 | time_since_last_typing_ = 0; |
| 40 | else |
| 41 | ++time_since_last_typing_; |
| 42 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 43 | if (time_since_last_typing_ < type_event_delay_ && vad_activity && |
henrikg@webrtc.org | c693704 | 2014-01-30 09:50:46 +0000 | [diff] [blame] | 44 | time_active_ < time_window_) { |
| 45 | penalty_counter_ += cost_per_typing_; |
| 46 | if (penalty_counter_ > reporting_threshold_) |
| 47 | new_detection_to_report_ = true; |
| 48 | } |
| 49 | |
| 50 | if (penalty_counter_ > 0) |
| 51 | penalty_counter_ -= penalty_decay_; |
| 52 | |
| 53 | if (++counter_since_last_detection_update_ == |
| 54 | report_detection_update_period_) { |
| 55 | detection_to_report_ = new_detection_to_report_; |
| 56 | new_detection_to_report_ = false; |
| 57 | counter_since_last_detection_update_ = 0; |
| 58 | } |
| 59 | |
| 60 | return detection_to_report_; |
| 61 | } |
| 62 | |
| 63 | int TypingDetection::TimeSinceLastDetectionInSeconds() { |
| 64 | // Round to whole seconds. |
| 65 | return (time_since_last_typing_ + 50) / 100; |
| 66 | } |
| 67 | |
| 68 | void TypingDetection::SetParameters(int time_window, |
| 69 | int cost_per_typing, |
| 70 | int reporting_threshold, |
| 71 | int penalty_decay, |
| 72 | int type_event_delay, |
| 73 | int report_detection_update_period) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 74 | if (time_window) |
| 75 | time_window_ = time_window; |
henrikg@webrtc.org | c693704 | 2014-01-30 09:50:46 +0000 | [diff] [blame] | 76 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 77 | if (cost_per_typing) |
| 78 | cost_per_typing_ = cost_per_typing; |
henrikg@webrtc.org | c693704 | 2014-01-30 09:50:46 +0000 | [diff] [blame] | 79 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 80 | if (reporting_threshold) |
| 81 | reporting_threshold_ = reporting_threshold; |
henrikg@webrtc.org | c693704 | 2014-01-30 09:50:46 +0000 | [diff] [blame] | 82 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 83 | if (penalty_decay) |
| 84 | penalty_decay_ = penalty_decay; |
henrikg@webrtc.org | c693704 | 2014-01-30 09:50:46 +0000 | [diff] [blame] | 85 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 86 | if (type_event_delay) |
| 87 | type_event_delay_ = type_event_delay; |
henrikg@webrtc.org | c693704 | 2014-01-30 09:50:46 +0000 | [diff] [blame] | 88 | |
| 89 | if (report_detection_update_period) |
| 90 | report_detection_update_period_ = report_detection_update_period; |
| 91 | } |
| 92 | |
| 93 | } // namespace webrtc |