11var Class = require ( '../../utils/Class' ) ;
22var BaseSoundManager = require ( '../BaseSoundManager' ) ;
33var WebAudioSound = require ( './WebAudioSound' ) ;
4+
45/*!
56 * @author Pavle Goloskokovic <pgoloskokovic@gmail.com> (http://prunegames.com)
67 */
78var WebAudioSoundManager = new Class ( {
89 Extends : BaseSoundManager ,
10+
911 /**
1012 * Web Audio API implementation of the sound manager.
1113 *
1214 * @class Phaser.Sound.WebAudioSoundManager
1315 * @constructor
1416 * @param {Phaser.Game } game - Reference to the current game instance.
1517 */
16- initialize : function WebAudioSoundManager ( game ) {
18+ initialize : function WebAudioSoundManager ( game )
19+ {
1720 /**
1821 * The AudioContext being used for playback.
1922 *
2023 * @private
2124 * @property {AudioContext } context
2225 */
2326 this . context = this . createAudioContext ( game ) ;
27+
2428 /**
2529 * Gain node responsible for controlling global muting.
2630 *
2731 * @private
2832 * @property {GainNode } masterMuteNode
2933 */
3034 this . masterMuteNode = this . context . createGain ( ) ;
35+
3136 /**
3237 * Gain node responsible for controlling global volume.
3338 *
@@ -37,6 +42,7 @@ var WebAudioSoundManager = new Class({
3742 this . masterVolumeNode = this . context . createGain ( ) ;
3843 this . masterMuteNode . connect ( this . masterVolumeNode ) ;
3944 this . masterVolumeNode . connect ( this . context . destination ) ;
45+
4046 /**
4147 * Destination node for connecting individual sounds to.
4248 *
@@ -47,6 +53,7 @@ var WebAudioSoundManager = new Class({
4753 this . locked = this . context . state === 'suspended' && 'ontouchstart' in window ;
4854 BaseSoundManager . call ( this , game ) ;
4955 } ,
56+
5057 /**
5158 * Method responsible for instantiating and returning AudioContext instance.
5259 * If an instance of an AudioContext class was provided trough the game config,
@@ -59,13 +66,16 @@ var WebAudioSoundManager = new Class({
5966 * @param {Phaser.Game } game - Reference to the current game instance.
6067 * @returns {AudioContext } The AudioContext instance to be used for playback.
6168 */
62- createAudioContext : function ( game ) {
69+ createAudioContext : function ( game )
70+ {
6371 var audioConfig = game . config . audio ;
64- if ( audioConfig && audioConfig . context ) {
72+ if ( audioConfig && audioConfig . context )
73+ {
6574 return audioConfig . context ;
6675 }
6776 return new AudioContext ( ) ;
6877 } ,
78+
6979 /**
7080 * Adds a new sound into the sound manager.
7181 *
@@ -74,11 +84,13 @@ var WebAudioSoundManager = new Class({
7484 * @param {ISoundConfig } [config] - An optional config object containing default sound settings.
7585 * @returns {Phaser.Sound.WebAudioSound } The new sound instance.
7686 */
77- add : function ( key , config ) {
87+ add : function ( key , config )
88+ {
7889 var sound = new WebAudioSound ( this , key , config ) ;
7990 this . sounds . push ( sound ) ;
8091 return sound ;
8192 } ,
93+
8294 /**
8395 * Unlocks Web Audio API on iOS devices on the initial touch event.
8496 *
@@ -87,10 +99,13 @@ var WebAudioSoundManager = new Class({
8799 * @private
88100 * @method Phaser.Sound.WebAudioSoundManager#unlock
89101 */
90- unlock : function ( ) {
102+ unlock : function ( )
103+ {
91104 var _this = this ;
92- var unlock = function ( ) {
93- _this . context . resume ( ) . then ( function ( ) {
105+ var unlock = function ( )
106+ {
107+ _this . context . resume ( ) . then ( function ( )
108+ {
94109 document . body . removeEventListener ( 'touchstart' , unlock ) ;
95110 document . body . removeEventListener ( 'touchend' , unlock ) ;
96111 _this . unlocked = true ;
@@ -99,33 +114,39 @@ var WebAudioSoundManager = new Class({
99114 document . body . addEventListener ( 'touchstart' , unlock , false ) ;
100115 document . body . addEventListener ( 'touchend' , unlock , false ) ;
101116 } ,
117+
102118 /**
103119 * Method used internally for pausing sound manager if
104120 * Phaser.Sound.WebAudioSoundManager#pauseOnBlur is set to true.
105121 *
106122 * @protected
107123 * @method Phaser.Sound.WebAudioSoundManager#onBlur
108124 */
109- onBlur : function ( ) {
125+ onBlur : function ( )
126+ {
110127 this . context . suspend ( ) ;
111128 } ,
129+
112130 /**
113131 * Method used internally for resuming sound manager if
114132 * Phaser.Sound.WebAudioSoundManager#pauseOnBlur is set to true.
115133 *
116134 * @protected
117135 * @method Phaser.Sound.WebAudioSoundManager#onFocus
118136 */
119- onFocus : function ( ) {
137+ onFocus : function ( )
138+ {
120139 this . context . resume ( ) ;
121140 } ,
141+
122142 /**
123143 * Calls Phaser.Sound.BaseSoundManager#destroy method
124144 * and cleans up all Web Audio API related stuff.
125145 *
126146 * @method Phaser.Sound.WebAudioSoundManager#destroy
127147 */
128- destroy : function ( ) {
148+ destroy : function ( )
149+ {
129150 BaseSoundManager . prototype . destroy . call ( this ) ;
130151 this . destination = null ;
131152 this . masterVolumeNode . disconnect ( ) ;
@@ -136,18 +157,22 @@ var WebAudioSoundManager = new Class({
136157 this . context = null ;
137158 }
138159} ) ;
160+
139161/**
140162 * Global mute setting.
141163 *
142164 * @name Phaser.Sound.WebAudioSoundManager#mute
143165 * @property {boolean } mute
144166 */
145167Object . defineProperty ( WebAudioSoundManager . prototype , 'mute' , {
146- get : function ( ) {
168+ get : function ( )
169+ {
147170 return this . masterMuteNode . gain . value === 0 ;
148171 } ,
149- set : function ( value ) {
172+ set : function ( value )
173+ {
150174 this . masterMuteNode . gain . setValueAtTime ( value ? 0 : 1 , 0 ) ;
175+
151176 /**
152177 * @event Phaser.Sound.WebAudioSoundManager#mute
153178 * @param {Phaser.Sound.WebAudioSoundManager } soundManager - Reference to the sound manager that emitted event.
@@ -156,18 +181,22 @@ Object.defineProperty(WebAudioSoundManager.prototype, 'mute', {
156181 this . emit ( 'mute' , this , value ) ;
157182 }
158183} ) ;
184+
159185/**
160186 * Global volume setting.
161187 *
162188 * @name Phaser.Sound.WebAudioSoundManager#volume
163189 * @property {number } volume
164190 */
165191Object . defineProperty ( WebAudioSoundManager . prototype , 'volume' , {
166- get : function ( ) {
192+ get : function ( )
193+ {
167194 return this . masterVolumeNode . gain . value ;
168195 } ,
169- set : function ( value ) {
196+ set : function ( value )
197+ {
170198 this . masterVolumeNode . gain . setValueAtTime ( value , 0 ) ;
199+
171200 /**
172201 * @event Phaser.Sound.WebAudioSoundManager#volume
173202 * @param {Phaser.Sound.WebAudioSoundManager } soundManager - Reference to the sound manager that emitted event.
0 commit comments