ApplicationImpl cleanup, part 1:
[chromium-blink-merge.git] / third_party / google_input_tools / src / chrome / os / sounds / soundcontroller.js
blobc3f037cac32fc39c894ce95c6bd745bc70060e61
1 // Copyright 2015 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");
14 goog.provide('i18n.input.chrome.sounds.SoundController');
16 goog.require('goog.Disposable');
17 goog.require('goog.dom');
18 goog.require('i18n.input.chrome.inputview.elements.ElementType');
19 goog.require('i18n.input.chrome.sounds.Sounds');
21 goog.scope(function() {
22 var Sounds = i18n.input.chrome.sounds.Sounds;
23 var ElementType = i18n.input.chrome.inputview.elements.ElementType;
24 var keyToSoundIdOnKeyUp = {};
25 var keyToSoundIdOnKeyRepeat = {};
29 /**
30 * Sound controller for the keyboard.
32 * @param {!boolean} enabled Whether sounds is enabled by default.
33 * @param {?number=} opt_volume The default volume for sound tracks.
34 * @constructor
35 * @extends {goog.Disposable}
37 i18n.input.chrome.sounds.SoundController = function(enabled, opt_volume) {
39 /**
40 * Collection of all the sound pools.
42 * @private {!Object.<string, !Object>}
44 this.sounds_ = {};
46 /** @private {boolean} */
47 this.enabled_ = enabled;
49 /**
50 * The default volume for all audio tracks. Tracks with volume 0 will be
51 * skipped.
53 * @private {number}
55 this.volume_ = opt_volume || this.DEFAULT_VOLUME;
57 if (enabled) {
58 this.initialize();
61 goog.inherits(i18n.input.chrome.sounds.SoundController, goog.Disposable);
64 var Controller = i18n.input.chrome.sounds.SoundController;
67 /**
68 * @define {number} The size of the pool to use for playing audio sounds.
70 Controller.prototype.POOL_SIZE = 10;
73 /**
74 * @define {number} The default audio track volume.
76 Controller.prototype.DEFAULT_VOLUME = 0.6;
79 /** @private {boolean} */
80 Controller.prototype.initialized_ = false;
83 /**
84 * Initializes the sound controller.
86 Controller.prototype.initialize = function() {
87 if (!this.initialized_) {
88 for (var sound in Sounds) {
89 this.addSound_(Sounds[sound]);
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;
101 * Caches the specified sound on the keyboard.
103 * @param {string} soundId The name of the .wav file in the "sounds"
104 directory.
105 * @private
107 Controller.prototype.addSound_ = function(soundId) {
108 if (soundId == Sounds.NONE || this.sounds_[soundId])
109 return;
110 var pool = [];
111 // Create sound pool.
112 for (var i = 0; i < this.POOL_SIZE; i++) {
113 var audio = goog.dom.createDom('audio', {
114 preload: 'auto',
115 id: soundId,
116 src: 'sounds/' + soundId + '.wav',
117 volume: this.volume_
119 pool.push(audio);
121 this.sounds_[soundId] = pool;
126 * Sets the volume for the specified sound.
128 * @param {string} soundId The id of the sound.
129 * @param {number} volume The volume to set.
131 Controller.prototype.setVolume = function(soundId, volume) {
132 var pool = this.sounds_[soundId];
133 if (!pool) {
134 console.error('Cannot find sound: ' + soundId);
135 return;
137 // Change volume for all sounds in the pool.
138 for (var i = 0; i < pool.length; i++) {
139 pool[i].volume = volume;
145 * Enables or disable playing sounds on keypress.
146 * @param {!boolean} enabled
148 Controller.prototype.setEnabled = function(enabled) {
149 this.enabled_ = enabled;
150 if (this.enabled_) {
151 this.initialize();
157 * Gets the flag whether sound controller is enabled or not.
159 * @return {!boolean}
161 Controller.prototype.getEnabled = function() {
162 return this.enabled_;
167 * Sets the volume for all sounds on the keyboard.
169 * @param {number} volume The volume of the sounds.
171 Controller.prototype.setMasterVolume = function(volume) {
172 this.volume_ = volume;
173 for (var id in this.sounds_) {
174 this.setVolume(id, volume);
180 * Plays the specified sound.
182 * @param {string} soundId The id of the audio tag.
183 * @param {boolean=} opt_force Force to play sound whatever the enabled flags is
184 * turned on.
186 Controller.prototype.playSound = function(soundId, opt_force) {
187 if (opt_force) {
188 this.initialize();
190 // If master volume is zero, ignore the request.
191 if (!opt_force && !this.enabled_ || this.volume_ == 0 ||
192 soundId == Sounds.NONE) {
193 return;
195 var pool = this.sounds_[soundId];
196 if (!pool) {
197 console.error('Cannot find sound: ' + soundId);
198 return;
200 // Search the sound pool for a free resource.
201 for (var i = 0; i < pool.length; i++) {
202 if (pool[i].paused) {
203 pool[i].play();
204 return;
211 * On key up.
213 * @param {ElementType} key The key released.
215 Controller.prototype.onKeyUp = function(key) {
216 var sound = keyToSoundIdOnKeyUp[key] || Sounds.STANDARD;
217 this.playSound(sound);
222 * On key repeat.
224 * @param {ElementType} key The key that is being repeated.
226 Controller.prototype.onKeyRepeat = function(key) {
227 var sound = keyToSoundIdOnKeyRepeat[key] || Sounds.NONE;
228 this.playSound(sound);
232 /** @override */
233 Controller.prototype.disposeInternal = function() {
234 for (var soundId in this.sounds_) {
235 var pool = this.sounds_[soundId];
236 for (var i = 0; i < pool.length; i++) {
237 var tag = pool[i];
238 if (tag && tag.loaded) {
239 tag.pause();
240 tag.autoplay = false;
241 tag.loop = false;
242 tag.currentTime = 0;
245 delete this.sounds_[soundId];
247 this.sounds_ = {};
248 keyToSoundIdOnKeyUp = {};
249 keyToSoundIdOnKeyRepeat = {};
250 goog.base(this, 'disposeInternal');
253 }); // goog.scope