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