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.
5 #include "ui/app_list/speech_ui_model.h"
11 // The default sound level, just gotten from the developer device.
12 const int16 kDefaultSoundLevel
= 200;
16 SpeechUIModel::SpeechUIModel(SpeechRecognitionState initial_state
)
17 : state_(initial_state
),
18 minimum_sound_level_(kDefaultSoundLevel
),
19 maximum_sound_level_(kDefaultSoundLevel
) {}
21 SpeechUIModel::~SpeechUIModel() {}
23 void SpeechUIModel::SetSpeechResult(const base::string16
& result
,
25 if (result_
== result
&& is_final_
== is_final
)
30 FOR_EACH_OBSERVER(SpeechUIModelObserver
,
32 OnSpeechResult(result
, is_final
));
35 void SpeechUIModel::UpdateSoundLevel(int16 level
) {
36 if (sound_level_
== level
)
41 // Tweak the sound level limits adaptively.
42 // - min is the minimum value during the speech recognition starts but speech
43 // itself hasn't started.
44 // - max is the maximum value when the user speaks.
45 if (state_
== SPEECH_RECOGNITION_IN_SPEECH
)
46 maximum_sound_level_
= std::max(level
, maximum_sound_level_
);
48 minimum_sound_level_
= std::min(level
, minimum_sound_level_
);
50 if (maximum_sound_level_
< minimum_sound_level_
) {
51 maximum_sound_level_
= std::max(
52 static_cast<int16
>(minimum_sound_level_
+ kDefaultSoundLevel
),
56 int16 range
= maximum_sound_level_
- minimum_sound_level_
;
57 uint8 visible_level
= 0;
59 int16 visible_level_in_range
=
60 std::min(std::max(minimum_sound_level_
, sound_level_
),
61 maximum_sound_level_
);
63 (visible_level_in_range
- minimum_sound_level_
) * kuint8max
/ range
;
66 FOR_EACH_OBSERVER(SpeechUIModelObserver
,
68 OnSpeechSoundLevelChanged(visible_level
));
71 void SpeechUIModel::SetSpeechRecognitionState(
72 SpeechRecognitionState new_state
) {
73 if (state_
== new_state
)
77 // Revert the min/max sound level to the default.
78 if (state_
!= SPEECH_RECOGNITION_RECOGNIZING
&&
79 state_
!= SPEECH_RECOGNITION_IN_SPEECH
) {
80 minimum_sound_level_
= kDefaultSoundLevel
;
81 maximum_sound_level_
= kDefaultSoundLevel
;
84 FOR_EACH_OBSERVER(SpeechUIModelObserver
,
86 OnSpeechRecognitionStateChanged(new_state
));
89 void SpeechUIModel::AddObserver(SpeechUIModelObserver
* observer
) {
90 observers_
.AddObserver(observer
);
93 void SpeechUIModel::RemoveObserver(SpeechUIModelObserver
* observer
) {
94 observers_
.RemoveObserver(observer
);
97 } // namespace app_list