Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / speech_ui_model.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/app_list/speech_ui_model.h"
6
7 #include <algorithm>
8
9 namespace app_list {
10
11 namespace {
12
13 // The default sound level, just gotten from the developer device.
14 const int16 kDefaultSoundLevel = 200;
15
16 }  // namespace
17
18 SpeechUIModel::SpeechUIModel()
19     : is_final_(false),
20       sound_level_(0),
21       state_(app_list::SPEECH_RECOGNITION_OFF),
22       minimum_sound_level_(kDefaultSoundLevel),
23       maximum_sound_level_(kDefaultSoundLevel) {
24 }
25
26 SpeechUIModel::~SpeechUIModel() {}
27
28 void SpeechUIModel::SetSpeechResult(const base::string16& result,
29                                     bool is_final) {
30   if (result_ == result && is_final_ == is_final)
31     return;
32
33   result_ = result;
34   is_final_ = is_final;
35   FOR_EACH_OBSERVER(SpeechUIModelObserver,
36                     observers_,
37                     OnSpeechResult(result, is_final));
38 }
39
40 void SpeechUIModel::UpdateSoundLevel(int16 level) {
41   if (sound_level_ == level)
42     return;
43
44   sound_level_ = level;
45
46   // Tweak the sound level limits adaptively.
47   // - min is the minimum value during the speech recognition starts but speech
48   //   itself hasn't started.
49   // - max is the maximum value when the user speaks.
50   if (state_ == SPEECH_RECOGNITION_IN_SPEECH)
51     maximum_sound_level_ = std::max(level, maximum_sound_level_);
52   else
53     minimum_sound_level_ = std::min(level, minimum_sound_level_);
54
55   if (maximum_sound_level_ < minimum_sound_level_) {
56     maximum_sound_level_ = std::max(
57         static_cast<int16>(minimum_sound_level_ + kDefaultSoundLevel),
58         kint16max);
59   }
60
61   int16 range = maximum_sound_level_ - minimum_sound_level_;
62   uint8 visible_level = 0;
63   if (range > 0) {
64     int16 visible_level_in_range =
65         std::min(std::max(minimum_sound_level_, sound_level_),
66                  maximum_sound_level_);
67     visible_level =
68         (visible_level_in_range - minimum_sound_level_) * kuint8max / range;
69   }
70
71   FOR_EACH_OBSERVER(SpeechUIModelObserver,
72                     observers_,
73                     OnSpeechSoundLevelChanged(visible_level));
74 }
75
76 void SpeechUIModel::SetSpeechRecognitionState(
77     SpeechRecognitionState new_state) {
78   if (state_ == new_state)
79     return;
80
81   state_ = new_state;
82   // Revert the min/max sound level to the default.
83   if (state_ != SPEECH_RECOGNITION_RECOGNIZING &&
84       state_ != SPEECH_RECOGNITION_IN_SPEECH) {
85     minimum_sound_level_ = kDefaultSoundLevel;
86     maximum_sound_level_ = kDefaultSoundLevel;
87   }
88
89   FOR_EACH_OBSERVER(SpeechUIModelObserver,
90                     observers_,
91                     OnSpeechRecognitionStateChanged(new_state));
92 }
93
94 void SpeechUIModel::AddObserver(SpeechUIModelObserver* observer) {
95   observers_.AddObserver(observer);
96 }
97
98 void SpeechUIModel::RemoveObserver(SpeechUIModelObserver* observer) {
99   observers_.RemoveObserver(observer);
100 }
101
102 }  // namespace app_list