OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/app_list/speech_ui_model.h" | 5 #include "ui/app_list/speech_ui_model.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <limits> |
8 | 11 |
9 namespace app_list { | 12 namespace app_list { |
10 | 13 |
11 namespace { | 14 namespace { |
12 | 15 |
13 // The default sound level, just gotten from the developer device. | 16 // The default sound level, just gotten from the developer device. |
14 const int16 kDefaultSoundLevel = 200; | 17 const int16_t kDefaultSoundLevel = 200; |
15 | 18 |
16 } // namespace | 19 } // namespace |
17 | 20 |
18 SpeechUIModel::SpeechUIModel() | 21 SpeechUIModel::SpeechUIModel() |
19 : is_final_(false), | 22 : is_final_(false), |
20 sound_level_(0), | 23 sound_level_(0), |
21 state_(app_list::SPEECH_RECOGNITION_OFF), | 24 state_(app_list::SPEECH_RECOGNITION_OFF), |
22 minimum_sound_level_(kDefaultSoundLevel), | 25 minimum_sound_level_(kDefaultSoundLevel), |
23 maximum_sound_level_(kDefaultSoundLevel) { | 26 maximum_sound_level_(kDefaultSoundLevel) { |
24 } | 27 } |
25 | 28 |
26 SpeechUIModel::~SpeechUIModel() {} | 29 SpeechUIModel::~SpeechUIModel() {} |
27 | 30 |
28 void SpeechUIModel::SetSpeechResult(const base::string16& result, | 31 void SpeechUIModel::SetSpeechResult(const base::string16& result, |
29 bool is_final) { | 32 bool is_final) { |
30 if (result_ == result && is_final_ == is_final) | 33 if (result_ == result && is_final_ == is_final) |
31 return; | 34 return; |
32 | 35 |
33 result_ = result; | 36 result_ = result; |
34 is_final_ = is_final; | 37 is_final_ = is_final; |
35 FOR_EACH_OBSERVER(SpeechUIModelObserver, | 38 FOR_EACH_OBSERVER(SpeechUIModelObserver, |
36 observers_, | 39 observers_, |
37 OnSpeechResult(result, is_final)); | 40 OnSpeechResult(result, is_final)); |
38 } | 41 } |
39 | 42 |
40 void SpeechUIModel::UpdateSoundLevel(int16 level) { | 43 void SpeechUIModel::UpdateSoundLevel(int16_t level) { |
41 if (sound_level_ == level) | 44 if (sound_level_ == level) |
42 return; | 45 return; |
43 | 46 |
44 sound_level_ = level; | 47 sound_level_ = level; |
45 | 48 |
46 // Tweak the sound level limits adaptively. | 49 // Tweak the sound level limits adaptively. |
47 // - min is the minimum value during the speech recognition starts but speech | 50 // - min is the minimum value during the speech recognition starts but speech |
48 // itself hasn't started. | 51 // itself hasn't started. |
49 // - max is the maximum value when the user speaks. | 52 // - max is the maximum value when the user speaks. |
50 if (state_ == SPEECH_RECOGNITION_IN_SPEECH) | 53 if (state_ == SPEECH_RECOGNITION_IN_SPEECH) |
51 maximum_sound_level_ = std::max(level, maximum_sound_level_); | 54 maximum_sound_level_ = std::max(level, maximum_sound_level_); |
52 else | 55 else |
53 minimum_sound_level_ = std::min(level, minimum_sound_level_); | 56 minimum_sound_level_ = std::min(level, minimum_sound_level_); |
54 | 57 |
55 if (maximum_sound_level_ < minimum_sound_level_) { | 58 if (maximum_sound_level_ < minimum_sound_level_) { |
56 maximum_sound_level_ = std::max( | 59 maximum_sound_level_ = std::max( |
57 static_cast<int16>(minimum_sound_level_ + kDefaultSoundLevel), | 60 static_cast<int16_t>(minimum_sound_level_ + kDefaultSoundLevel), |
58 kint16max); | 61 std::numeric_limits<int16_t>::max()); |
59 } | 62 } |
60 | 63 |
61 int16 range = maximum_sound_level_ - minimum_sound_level_; | 64 int16_t range = maximum_sound_level_ - minimum_sound_level_; |
62 uint8 visible_level = 0; | 65 uint8_t visible_level = 0; |
63 if (range > 0) { | 66 if (range > 0) { |
64 int16 visible_level_in_range = | 67 int16_t visible_level_in_range = std::min( |
65 std::min(std::max(minimum_sound_level_, sound_level_), | 68 std::max(minimum_sound_level_, sound_level_), maximum_sound_level_); |
66 maximum_sound_level_); | 69 visible_level = (visible_level_in_range - minimum_sound_level_) * |
67 visible_level = | 70 std::numeric_limits<uint8_t>::max() / range; |
68 (visible_level_in_range - minimum_sound_level_) * kuint8max / range; | |
69 } | 71 } |
70 | 72 |
71 FOR_EACH_OBSERVER(SpeechUIModelObserver, | 73 FOR_EACH_OBSERVER(SpeechUIModelObserver, |
72 observers_, | 74 observers_, |
73 OnSpeechSoundLevelChanged(visible_level)); | 75 OnSpeechSoundLevelChanged(visible_level)); |
74 } | 76 } |
75 | 77 |
76 void SpeechUIModel::SetSpeechRecognitionState(SpeechRecognitionState new_state, | 78 void SpeechUIModel::SetSpeechRecognitionState(SpeechRecognitionState new_state, |
77 bool always_show_ui) { | 79 bool always_show_ui) { |
78 // Don't show the speech view on a change to a network error or if the state | 80 // Don't show the speech view on a change to a network error or if the state |
(...skipping 20 matching lines...) Expand all Loading... |
99 | 101 |
100 void SpeechUIModel::AddObserver(SpeechUIModelObserver* observer) { | 102 void SpeechUIModel::AddObserver(SpeechUIModelObserver* observer) { |
101 observers_.AddObserver(observer); | 103 observers_.AddObserver(observer); |
102 } | 104 } |
103 | 105 |
104 void SpeechUIModel::RemoveObserver(SpeechUIModelObserver* observer) { | 106 void SpeechUIModel::RemoveObserver(SpeechUIModelObserver* observer) { |
105 observers_.RemoveObserver(observer); | 107 observers_.RemoveObserver(observer); |
106 } | 108 } |
107 | 109 |
108 } // namespace app_list | 110 } // namespace app_list |
OLD | NEW |