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
8 // http://www.apache.org/licenses/LICENSE-2.0
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 = {};
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.
35 * @extends {goog.Disposable}
37 i18n.input.chrome.sounds.SoundController = function(enabled, opt_volume) {
40 * Collection of all the sound pools.
42 * @private {!Object.<string, !Object>}
46 /** @private {boolean} */
47 this.enabled_ = enabled;
50 * The default volume for all audio tracks. Tracks with volume 0 will be
55 this.volume_ = opt_volume || this.DEFAULT_VOLUME;
61 goog.inherits(i18n.input.chrome.sounds.SoundController, goog.Disposable);
64 var Controller = i18n.input.chrome.sounds.SoundController;
68 * @define {number} The size of the pool to use for playing audio sounds.
70 Controller.prototype.POOL_SIZE = 10;
74 * @define {number} The default audio track volume.
76 Controller.prototype.DEFAULT_VOLUME = 0.6;
79 /** @private {boolean} */
80 Controller.prototype.initialized_ = false;
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"
107 Controller.prototype.addSound_ = function(soundId) {
108 if (soundId == Sounds.NONE || this.sounds_[soundId])
111 // Create sound pool.
112 for (var i = 0; i < this.POOL_SIZE; i++) {
113 var audio = goog.dom.createDom('audio', {
116 src: 'sounds/' + soundId + '.wav',
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];
134 console.error('Cannot find sound: ' + soundId);
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;
157 * Gets the flag whether sound controller is enabled or not.
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
186 Controller.prototype.playSound = function(soundId, opt_force) {
190 // If master volume is zero, ignore the request.
191 if (!opt_force && !this.enabled_ || this.volume_ == 0 ||
192 soundId == Sounds.NONE) {
195 var pool = this.sounds_[soundId];
197 console.error('Cannot find sound: ' + soundId);
200 // Search the sound pool for a free resource.
201 for (var i = 0; i < pool.length; i++) {
202 if (pool[i].paused) {
213 * @param {ElementType} key The key released.
215 Controller.prototype.onKeyUp = function(key) {
216 var sound = keyToSoundIdOnKeyUp[key] || Sounds.STANDARD;
217 this.playSound(sound);
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);
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++) {
238 if (tag && tag.loaded) {
240 tag.autoplay = false;
245 delete this.sounds_[soundId];
248 keyToSoundIdOnKeyUp = {};
249 keyToSoundIdOnKeyRepeat = {};
250 goog.base(this, 'disposeInternal');