OLD | NEW |
1 // Copyright 2014 The ChromeOS IME Authors. All Rights Reserved. | 1 // Copyright 2015 The ChromeOS IME Authors. All Rights Reserved. |
2 // limitations under the License. | 2 // limitations under the License. |
3 // See the License for the specific language governing permissions and | 3 // See the License for the specific language governing permissions and |
4 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 4 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
5 // distributed under the License is distributed on an "AS-IS" BASIS, | 5 // distributed under the License is distributed on an "AS-IS" BASIS, |
6 // Unless required by applicable law or agreed to in writing, software | 6 // Unless required by applicable law or agreed to in writing, software |
7 // | 7 // |
8 // http://www.apache.org/licenses/LICENSE-2.0 | 8 // http://www.apache.org/licenses/LICENSE-2.0 |
9 // | 9 // |
10 // You may obtain a copy of the License at | 10 // You may obtain a copy of the License at |
11 // you may not use this file except in compliance with the License. | 11 // you may not use this file except in compliance with the License. |
12 // Licensed under the Apache License, Version 2.0 (the "License"); | 12 // Licensed under the Apache License, Version 2.0 (the "License"); |
13 // | 13 // |
14 goog.provide('i18n.input.chrome.inputview.SoundController'); | 14 goog.provide('i18n.input.chrome.SoundController'); |
15 | 15 |
16 goog.require('goog.Disposable'); | 16 goog.require('goog.Disposable'); |
17 goog.require('goog.dom'); | 17 goog.require('goog.dom'); |
18 goog.require('i18n.input.chrome.inputview.Sounds'); | 18 goog.require('i18n.input.chrome.inputview.Sounds'); |
19 goog.require('i18n.input.chrome.inputview.elements.ElementType'); | 19 goog.require('i18n.input.chrome.inputview.elements.ElementType'); |
20 | 20 |
21 goog.scope(function() { | 21 goog.scope(function() { |
22 var Sounds = i18n.input.chrome.inputview.Sounds; | 22 var Sounds = i18n.input.chrome.inputview.Sounds; |
23 var ElementType = i18n.input.chrome.inputview.elements.ElementType; | 23 var ElementType = i18n.input.chrome.inputview.elements.ElementType; |
24 var keyToSoundIdOnKeyUp = {}; | 24 var keyToSoundIdOnKeyUp = {}; |
25 var keyToSoundIdOnKeyRepeat = {}; | 25 var keyToSoundIdOnKeyRepeat = {}; |
26 | 26 |
27 | 27 |
| 28 |
28 /** | 29 /** |
29 * Sound controller for the keyboard. | 30 * Sound controller for the keyboard. |
30 * | 31 * |
31 * @param {!boolean} enabled Whether sounds is enabled by default. | 32 * @param {!boolean} enabled Whether sounds is enabled by default. |
32 * @param {?number} opt_volume The default volume for sound tracks. | 33 * @param {?number=} opt_volume The default volume for sound tracks. |
33 * @constructor | 34 * @constructor |
34 * @extends {goog.Disposable} | 35 * @extends {goog.Disposable} |
35 */ | 36 */ |
36 i18n.input.chrome.inputview.SoundController = function(enabled, opt_volume) { | 37 i18n.input.chrome.SoundController = function(enabled, opt_volume) { |
37 | 38 |
38 /** | 39 /** |
39 * Collection of all the sound pools. | 40 * Collection of all the sound pools. |
40 * | 41 * |
41 * @type {!Object.<string, !Object>} | 42 * @private {!Object.<string, !Object>} |
42 */ | 43 */ |
43 this.sounds_ = {}; | 44 this.sounds_ = {}; |
44 | 45 |
| 46 /** @private {boolean} */ |
45 this.enabled_ = enabled; | 47 this.enabled_ = enabled; |
46 | 48 |
47 /** | 49 /** |
48 * The default volume for all audio tracks. Tracks with volume 0 will be | 50 * The default volume for all audio tracks. Tracks with volume 0 will be |
49 * skipped. | 51 * skipped. |
50 * | 52 * |
51 * @type {number} | 53 * @private {number} |
52 */ | 54 */ |
53 this.volume_ = opt_volume || this.DEFAULT_VOLUME; | 55 this.volume_ = opt_volume || this.DEFAULT_VOLUME; |
54 | 56 |
55 if (enabled) | 57 if (enabled) { |
56 this.initialize(); | 58 this.initialize(); |
| 59 } |
57 }; | 60 }; |
58 goog.inherits(i18n.input.chrome.inputview.SoundController, goog.Disposable); | 61 goog.inherits(i18n.input.chrome.SoundController, goog.Disposable); |
59 | 62 |
60 | 63 |
61 var Controller = i18n.input.chrome.inputview.SoundController; | 64 var Controller = i18n.input.chrome.SoundController; |
62 | 65 |
63 | 66 |
64 /** | 67 /** |
65 * @define {number} The size of the pool to use for playing audio sounds. | 68 * @define {number} The size of the pool to use for playing audio sounds. |
66 */ | 69 */ |
67 Controller.prototype.POOL_SIZE = 10; | 70 Controller.prototype.POOL_SIZE = 10; |
68 | 71 |
69 | 72 |
70 /** | 73 /** |
71 * @define {number} The default audio track volume. | 74 * @define {number} The default audio track volume. |
72 */ | 75 */ |
73 Controller.prototype.DEFAULT_VOLUME = 0.6; | 76 Controller.prototype.DEFAULT_VOLUME = 0.6; |
74 | 77 |
75 | 78 |
| 79 /** @private {boolean} */ |
| 80 Controller.prototype.initialized_ = false; |
| 81 |
| 82 |
76 /** | 83 /** |
77 * Initializes the sound controller. | 84 * Initializes the sound controller. |
78 */ | 85 */ |
79 Controller.prototype.initialize = function() { | 86 Controller.prototype.initialize = function() { |
80 for (var sound in Sounds) { | 87 if (!this.initialized_) { |
| 88 for (var sound in Sounds) { |
81 this.addSound_(Sounds[sound]); | 89 this.addSound_(Sounds[sound]); |
| 90 } |
| 91 keyToSoundIdOnKeyUp[ElementType.BACKSPACE_KEY] = Sounds.NONE; |
| 92 keyToSoundIdOnKeyUp[ElementType.ENTER_KEY] = Sounds.RETURN; |
| 93 keyToSoundIdOnKeyUp[ElementType.SPACE_KEY] = Sounds.SPACEBAR; |
| 94 keyToSoundIdOnKeyRepeat[ElementType.BACKSPACE_KEY] = Sounds.DELETE; |
| 95 this.initialized_ = true; |
82 } | 96 } |
83 keyToSoundIdOnKeyUp[ElementType.BACKSPACE_KEY] = Sounds.NONE; | |
84 keyToSoundIdOnKeyUp[ElementType.ENTER_KEY] = Sounds.RETURN; | |
85 keyToSoundIdOnKeyUp[ElementType.SPACE_KEY] = Sounds.SPACEBAR; | |
86 keyToSoundIdOnKeyRepeat[ElementType.BACKSPACE_KEY] = Sounds.DELETE; | |
87 }; | 97 }; |
88 | 98 |
89 | 99 |
90 /** | 100 /** |
91 * Caches the specified sound on the keyboard. | 101 * Caches the specified sound on the keyboard. |
92 * | 102 * |
93 * @param {string} soundId The name of the .wav file in the "sounds" | 103 * @param {string} soundId The name of the .wav file in the "sounds" |
94 directory. | 104 directory. |
95 * @private | 105 * @private |
96 */ | 106 */ |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } | 140 } |
131 }; | 141 }; |
132 | 142 |
133 | 143 |
134 /** | 144 /** |
135 * Enables or disable playing sounds on keypress. | 145 * Enables or disable playing sounds on keypress. |
136 * @param {!boolean} enabled | 146 * @param {!boolean} enabled |
137 */ | 147 */ |
138 Controller.prototype.setEnabled = function(enabled) { | 148 Controller.prototype.setEnabled = function(enabled) { |
139 this.enabled_ = enabled; | 149 this.enabled_ = enabled; |
140 if (this.enabled_) | 150 if (this.enabled_) { |
141 this.initialize(); | 151 this.initialize(); |
| 152 } |
142 }; | 153 }; |
143 | 154 |
144 | 155 |
| 156 /** |
| 157 * Gets the flag whether sound controller is enabled or not. |
| 158 * |
| 159 * @return {!boolean} |
| 160 */ |
| 161 Controller.prototype.getEnabled = function() { |
| 162 return this.enabled_; |
| 163 }; |
| 164 |
| 165 |
145 /** | 166 /** |
146 * Sets the volume for all sounds on the keyboard. | 167 * Sets the volume for all sounds on the keyboard. |
147 * | 168 * |
148 * @param {number} volume The volume of the sounds. | 169 * @param {number} volume The volume of the sounds. |
149 */ | 170 */ |
150 Controller.prototype.setMasterVolume = function(volume) { | 171 Controller.prototype.setMasterVolume = function(volume) { |
151 this.volume_ = volume; | 172 this.volume_ = volume; |
152 for (var id in this.sounds_) { | 173 for (var id in this.sounds_) { |
153 this.setVolume(id, volume); | 174 this.setVolume(id, volume); |
154 } | 175 } |
155 }; | 176 }; |
156 | 177 |
157 | 178 |
158 /** | 179 /** |
159 * Plays the specified sound. | 180 * Plays the specified sound. |
160 * | 181 * |
161 * @param {string} soundId The id of the audio tag. | 182 * @param {string} soundId The id of the audio tag. |
162 * @private | 183 * @param {boolean=} opt_force Force to play sound whatever the enabled flags is |
| 184 * turned on. |
163 */ | 185 */ |
164 Controller.prototype.playSound_ = function(soundId) { | 186 Controller.prototype.playSound = function(soundId, opt_force) { |
| 187 if (opt_force) { |
| 188 this.initialize(); |
| 189 } |
165 // If master volume is zero, ignore the request. | 190 // If master volume is zero, ignore the request. |
166 if (!this.enabled_ || this.volume_ == 0 || soundId == Sounds.NONE) | 191 if (!opt_force && !this.enabled_ || this.volume_ == 0 || |
| 192 soundId == Sounds.NONE) { |
167 return; | 193 return; |
| 194 } |
168 var pool = this.sounds_[soundId]; | 195 var pool = this.sounds_[soundId]; |
169 if (!pool) { | 196 if (!pool) { |
170 console.error('Cannot find sound: ' + soundId); | 197 console.error('Cannot find sound: ' + soundId); |
171 return; | 198 return; |
172 } | 199 } |
173 // Search the sound pool for a free resource. | 200 // Search the sound pool for a free resource. |
174 for (var i = 0; i < pool.length; i++) { | 201 for (var i = 0; i < pool.length; i++) { |
175 if (pool[i].paused) { | 202 if (pool[i].paused) { |
176 pool[i].play(); | 203 pool[i].play(); |
177 return; | 204 return; |
178 } | 205 } |
179 } | 206 } |
180 }; | 207 }; |
181 | 208 |
182 | 209 |
183 /** | 210 /** |
184 * On key up. | 211 * On key up. |
185 * | 212 * |
186 * @param {ElementType} key The key released. | 213 * @param {ElementType} key The key released. |
187 */ | 214 */ |
188 Controller.prototype.onKeyUp = function(key) { | 215 Controller.prototype.onKeyUp = function(key) { |
189 var sound = keyToSoundIdOnKeyUp[key] || Sounds.STANDARD; | 216 var sound = keyToSoundIdOnKeyUp[key] || Sounds.STANDARD; |
190 this.playSound_(sound); | 217 this.playSound(sound); |
191 }; | 218 }; |
192 | 219 |
193 | 220 |
194 /** | 221 /** |
195 * On key repeat. | 222 * On key repeat. |
196 * | 223 * |
197 * @param {ElementType} key The key that is being repeated. | 224 * @param {ElementType} key The key that is being repeated. |
198 */ | 225 */ |
199 Controller.prototype.onKeyRepeat = function(key) { | 226 Controller.prototype.onKeyRepeat = function(key) { |
200 var sound = keyToSoundIdOnKeyRepeat[key] || Sounds.NONE; | 227 var sound = keyToSoundIdOnKeyRepeat[key] || Sounds.NONE; |
201 this.playSound_(sound); | 228 this.playSound(sound); |
202 }; | 229 }; |
203 | 230 |
204 | 231 |
205 /** @override */ | 232 /** @override */ |
206 Controller.prototype.disposeInternal = function() { | 233 Controller.prototype.disposeInternal = function() { |
207 for (var soundId in this.sounds_) { | 234 for (var soundId in this.sounds_) { |
208 var pool = this.sounds_[soundId]; | 235 var pool = this.sounds_[soundId]; |
209 for (var i = 0; i < pool.length; i++) { | 236 for (var i = 0; i < pool.length; i++) { |
210 var tag = pool[i]; | 237 var tag = pool[i]; |
211 if (tag && tag.loaded) { | 238 if (tag && tag.loaded) { |
212 tag.pause(); | 239 tag.pause(); |
213 tag.autoplay = false; | 240 tag.autoplay = false; |
214 tag.loop = false; | 241 tag.loop = false; |
215 tag.currentTime = 0; | 242 tag.currentTime = 0; |
216 } | 243 } |
217 } | 244 } |
218 delete this.sounds_[soundId]; | 245 delete this.sounds_[soundId]; |
219 } | 246 } |
220 this.sounds_ = {}; | 247 this.sounds_ = {}; |
221 keyToSoundIdOnKeyUp = {}; | 248 keyToSoundIdOnKeyUp = {}; |
222 keyToSoundIdOnKeyRepeat = {}; | 249 keyToSoundIdOnKeyRepeat = {}; |
223 goog.base(this, 'disposeInternal'); | 250 goog.base(this, 'disposeInternal'); |
224 }; | 251 }; |
225 | 252 |
226 }); // goog.scope | 253 }); // goog.scope |
OLD | NEW |