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
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.inputview.SoundController');
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');
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 = {};
29 * Sound controller for the keyboard.
31 * @param {!boolean} enabled Whether sounds is enabled by default.
32 * @param {?number} opt_volume The default volume for sound tracks.
34 * @extends {goog.Disposable}
36 i18n.input.chrome.inputview.SoundController = function(enabled, opt_volume) {
39 * Collection of all the sound pools.
41 * @type {!Object.<string, !Object>}
45 this.enabled_ = enabled;
48 * The default volume for all audio tracks. Tracks with volume 0 will be
53 this.volume_ = opt_volume || this.DEFAULT_VOLUME;
58 goog.inherits(i18n.input.chrome.inputview.SoundController, goog.Disposable);
61 var Controller = i18n.input.chrome.inputview.SoundController;
65 * @define {number} The size of the pool to use for playing audio sounds.
67 Controller.prototype.POOL_SIZE = 10;
71 * @define {number} The default audio track volume.
73 Controller.prototype.DEFAULT_VOLUME = 0.6;
77 * Initializes the sound controller.
79 Controller.prototype.initialize = function() {
80 for (var sound in Sounds) {
81 this.addSound_(Sounds[sound]);
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;
91 * Caches the specified sound on the keyboard.
93 * @param {string} soundId The name of the .wav file in the "sounds"
97 Controller.prototype.addSound_ = function(soundId) {
98 if (soundId == Sounds.NONE || this.sounds_[soundId])
101 // Create sound pool.
102 for (var i = 0; i < this.POOL_SIZE; i++) {
103 var audio = goog.dom.createDom('audio', {
106 src: 'sounds/' + soundId + '.wav',
111 this.sounds_[soundId] = pool;
116 * Sets the volume for the specified sound.
118 * @param {string} soundId The id of the sound.
119 * @param {number} volume The volume to set.
121 Controller.prototype.setVolume = function(soundId, volume) {
122 var pool = this.sounds_[soundId];
124 console.error('Cannot find sound: ' + soundId);
127 // Change volume for all sounds in the pool.
128 for (var i = 0; i < pool.length; i++) {
129 pool[i].volume = volume;
135 * Enables or disable playing sounds on keypress.
136 * @param {!boolean} enabled
138 Controller.prototype.setEnabled = function(enabled) {
139 this.enabled_ = enabled;
146 * Sets the volume for all sounds on the keyboard.
148 * @param {number} volume The volume of the sounds.
150 Controller.prototype.setMasterVolume = function(volume) {
151 this.volume_ = volume;
152 for (var id in this.sounds_) {
153 this.setVolume(id, volume);
159 * Plays the specified sound.
161 * @param {string} soundId The id of the audio tag.
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)
168 var pool = this.sounds_[soundId];
170 console.error('Cannot find sound: ' + soundId);
173 // Search the sound pool for a free resource.
174 for (var i = 0; i < pool.length; i++) {
175 if (pool[i].paused) {
186 * @param {ElementType} key The key released.
188 Controller.prototype.onKeyUp = function(key) {
189 var sound = keyToSoundIdOnKeyUp[key] || Sounds.STANDARD;
190 this.playSound_(sound);
197 * @param {ElementType} key The key that is being repeated.
199 Controller.prototype.onKeyRepeat = function(key) {
200 var sound = keyToSoundIdOnKeyRepeat[key] || Sounds.NONE;
201 this.playSound_(sound);
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++) {
211 if (tag && tag.loaded) {
213 tag.autoplay = false;
218 delete this.sounds_[soundId];
221 keyToSoundIdOnKeyUp = {};
222 keyToSoundIdOnKeyRepeat = {};
223 goog.base(this, 'disposeInternal');