Browse Source

Updating Web Audio for better static usage, adding setAudioBuffer, setPlaybackRate on Sound

davrous 10 years ago
parent
commit
33a231c49e

+ 1 - 1
Babylon/Audio/babylon.analyser.js

@@ -8,7 +8,7 @@ var BABYLON;
             this.DEBUGCANVASPOS = { x: 20, y: 20 };
             this.DEBUGCANVASSIZE = { width: 320, height: 200 };
             this._scene = scene;
-            this._audioEngine = scene.getEngine().getAudioEngine();
+            this._audioEngine = BABYLON.Engine.audioEngine;
             if (this._audioEngine.canUseWebAudio) {
                 this._webAudioAnalyser = this._audioEngine.audioContext.createAnalyser();
                 this._webAudioAnalyser.minDecibels = -140;

+ 1 - 1
Babylon/Audio/babylon.analyser.ts

@@ -18,7 +18,7 @@ module BABYLON {
 
         constructor(scene: BABYLON.Scene) {
             this._scene = scene;
-            this._audioEngine = scene.getEngine().getAudioEngine();
+            this._audioEngine = Engine.audioEngine;
             if (this._audioEngine.canUseWebAudio) {
                 this._webAudioAnalyser = this._audioEngine.audioContext.createAnalyser();
                 this._webAudioAnalyser.minDecibels = -140;

+ 2 - 3
Babylon/Audio/babylon.audioengine.js

@@ -4,6 +4,7 @@ var BABYLON;
         function AudioEngine() {
             this.audioContext = null;
             this.canUseWebAudio = false;
+            this.WarnedWebAudioUnsupported = false;
             try {
                 if (typeof AudioContext !== 'undefined') {
                     this.audioContext = new AudioContext();
@@ -13,9 +14,6 @@ var BABYLON;
                     this.audioContext = new webkitAudioContext();
                     this.canUseWebAudio = true;
                 }
-                else {
-                    BABYLON.Tools.Error("Web Audio is not supported by your browser.");
-                }
             }
             catch (e) {
                 this.canUseWebAudio = false;
@@ -39,6 +37,7 @@ var BABYLON;
                 }
                 this.masterGain.gain.value = 1;
             }
+            this.WarnedWebAudioUnsupported = false;
         };
         AudioEngine.prototype.getGlobalVolume = function () {
             if (this.canUseWebAudio) {

+ 2 - 3
Babylon/Audio/babylon.audioengine.ts

@@ -5,6 +5,7 @@
         public masterGain: GainNode;
 
         private _connectedAnalyser: Analyser;
+        public WarnedWebAudioUnsupported: boolean = false;
 
         constructor() {
             // creating the audio context 
@@ -16,9 +17,6 @@
                     this.audioContext = new webkitAudioContext();
                     this.canUseWebAudio = true;
                 }
-                else {
-                    BABYLON.Tools.Error("Web Audio is not supported by your browser.");
-                }
             } catch (e) {
                 this.canUseWebAudio = false;
                 BABYLON.Tools.Error("Web Audio: " + e.message);
@@ -43,6 +41,7 @@
                 }
                 this.masterGain.gain.value = 1;
             }
+            this.WarnedWebAudioUnsupported = false;
         }
 
         public getGlobalVolume(): number {

+ 47 - 26
Babylon/Audio/babylon.sound.js

@@ -19,7 +19,7 @@ var BABYLON;
             this.maxDistance = 100;
             this.distanceModel = "linear";
             this.panningModel = "HRTF";
-            this.playbackRate = 1;
+            this._playbackRate = 1;
             this._startTime = 0;
             this._startOffset = 0;
             this._position = BABYLON.Vector3.Zero();
@@ -36,7 +36,6 @@ var BABYLON;
             this._coneOuterGain = 0;
             this.name = name;
             this._scene = scene;
-            this._audioEngine = this._scene.getEngine().getAudioEngine();
             this._readyToPlayCallback = readyToPlayCallback;
             // Default custom attenuation function is a linear attenuation
             this._customAttenuationFunction = function (currentVolume, currentDistance, maxDistance, refDistance, rolloffFactor) {
@@ -61,10 +60,10 @@ var BABYLON;
                 this.refDistance = options.refDistance || 1;
                 this.distanceModel = options.distanceModel || "linear";
                 this.panningModel = options.panningModel || "HRTF";
-                this.playbackRate = options.playbackRate || 1;
+                this._playbackRate = options.playbackRate || 1;
             }
-            if (this._audioEngine.canUseWebAudio) {
-                this._soundGain = this._audioEngine.audioContext.createGain();
+            if (BABYLON.Engine.audioEngine.canUseWebAudio) {
+                this._soundGain = BABYLON.Engine.audioEngine.audioContext.createGain();
                 this._soundGain.gain.value = this._volume;
                 this._inputAudioNode = this._soundGain;
                 this._ouputAudioNode = this._soundGain;
@@ -72,23 +71,33 @@ var BABYLON;
                     this._createSpatialParameters();
                 }
                 this._scene.mainSoundTrack.AddSound(this);
-                if (typeof (urlOrArrayBuffer) === "string") {
-                    BABYLON.Tools.LoadFile(urlOrArrayBuffer, function (data) {
-                        _this._soundLoaded(data);
-                    }, null, null, true);
-                }
-                else {
-                    if (urlOrArrayBuffer instanceof ArrayBuffer) {
-                        this._soundLoaded(urlOrArrayBuffer);
+                // if no parameter is passed, you need to call setAudioBuffer yourself to prepare the sound
+                if (urlOrArrayBuffer) {
+                    // If it's an URL
+                    if (typeof (urlOrArrayBuffer) === "string") {
+                        BABYLON.Tools.LoadFile(urlOrArrayBuffer, function (data) {
+                            _this._soundLoaded(data);
+                        }, null, null, true);
                     }
                     else {
-                        BABYLON.Tools.Error("Parameter must be a URL to the sound or an ArrayBuffer of the sound.");
+                        if (urlOrArrayBuffer instanceof ArrayBuffer) {
+                            this._soundLoaded(urlOrArrayBuffer);
+                        }
+                        else {
+                            BABYLON.Tools.Error("Parameter must be a URL to the sound or an ArrayBuffer of the sound.");
+                        }
                     }
                 }
             }
+            else {
+                if (!BABYLON.Engine.audioEngine.WarnedWebAudioUnsupported) {
+                    BABYLON.Tools.Error("Web Audio is not supported by your browser.");
+                    BABYLON.Engine.audioEngine.WarnedWebAudioUnsupported = true;
+                }
+            }
         }
         Sound.prototype.dispose = function () {
-            if (this._audioEngine.canUseWebAudio && this._isReadyToPlay) {
+            if (BABYLON.Engine.audioEngine.canUseWebAudio && this._isReadyToPlay) {
                 if (this._isPlaying) {
                     this.stop();
                 }
@@ -117,7 +126,7 @@ var BABYLON;
         Sound.prototype._soundLoaded = function (audioData) {
             var _this = this;
             this._isLoaded = true;
-            this._audioEngine.audioContext.decodeAudioData(audioData, function (buffer) {
+            BABYLON.Engine.audioEngine.audioContext.decodeAudioData(audioData, function (buffer) {
                 _this._audioBuffer = buffer;
                 _this._isReadyToPlay = true;
                 if (_this.autoplay) {
@@ -130,6 +139,12 @@ var BABYLON;
                 BABYLON.Tools.Error("Error while decoding audio data: " + error.err);
             });
         };
+        Sound.prototype.setAudioBuffer = function (audioBuffer) {
+            if (BABYLON.Engine.audioEngine.canUseWebAudio) {
+                this._audioBuffer = audioBuffer;
+                this._isReadyToPlay = true;
+            }
+        };
         Sound.prototype.updateOptions = function (options) {
             if (options) {
                 this.loop = options.loop || this.loop;
@@ -139,12 +154,12 @@ var BABYLON;
                 this.refDistance = options.refDistance || this.refDistance;
                 this.distanceModel = options.distanceModel || this.distanceModel;
                 this.panningModel = options.panningModel || this.panningModel;
-                this.playbackRate = options.playbackRate || this.playbackRate;
+                this._playbackRate = options.playbackRate || this._playbackRate;
             }
         };
         Sound.prototype._createSpatialParameters = function () {
-            if (this._audioEngine.canUseWebAudio) {
-                this._soundPanner = this._audioEngine.audioContext.createPanner();
+            if (BABYLON.Engine.audioEngine.canUseWebAudio) {
+                this._soundPanner = BABYLON.Engine.audioEngine.audioContext.createPanner();
                 if (this.useCustomAttenuation) {
                     // Tricks to disable in a way embedded Web Audio attenuation 
                     this._soundPanner.distanceModel = "linear";
@@ -165,7 +180,7 @@ var BABYLON;
             }
         };
         Sound.prototype.connectToSoundTrackAudioNode = function (soundTrackAudioNode) {
-            if (this._audioEngine.canUseWebAudio) {
+            if (BABYLON.Engine.audioEngine.canUseWebAudio) {
                 this._ouputAudioNode.disconnect();
                 this._ouputAudioNode.connect(soundTrackAudioNode);
             }
@@ -224,7 +239,7 @@ var BABYLON;
         Sound.prototype.play = function (time) {
             if (this._isReadyToPlay) {
                 try {
-                    var startTime = time ? this._audioEngine.audioContext.currentTime + time : 0;
+                    var startTime = time ? BABYLON.Engine.audioEngine.audioContext.currentTime + time : 0;
                     if (!this._soundSource) {
                         if (this.spatialSound) {
                             this._soundPanner.setPosition(this._position.x, this._position.y, this._position.z);
@@ -241,11 +256,11 @@ var BABYLON;
                             }
                         }
                     }
-                    this._soundSource = this._audioEngine.audioContext.createBufferSource();
+                    this._soundSource = BABYLON.Engine.audioEngine.audioContext.createBufferSource();
                     this._soundSource.buffer = this._audioBuffer;
                     this._soundSource.connect(this._inputAudioNode);
                     this._soundSource.loop = this.loop;
-                    this._soundSource.playbackRate.value = this.playbackRate;
+                    this._soundSource.playbackRate.value = this._playbackRate;
                     this._startTime = startTime;
                     if (this.onended) {
                         this._soundSource.onended = this.onended;
@@ -264,7 +279,7 @@ var BABYLON;
         */
         Sound.prototype.stop = function (time) {
             if (this._isPlaying) {
-                var stopTime = time ? this._audioEngine.audioContext.currentTime + time : 0;
+                var stopTime = time ? BABYLON.Engine.audioEngine.audioContext.currentTime + time : 0;
                 this._soundSource.stop(stopTime);
                 this._isPlaying = false;
             }
@@ -272,15 +287,21 @@ var BABYLON;
         Sound.prototype.pause = function () {
             if (this._isPlaying) {
                 this._soundSource.stop(0);
-                this._startOffset += this._audioEngine.audioContext.currentTime - this._startTime;
+                this._startOffset += BABYLON.Engine.audioEngine.audioContext.currentTime - this._startTime;
             }
         };
         Sound.prototype.setVolume = function (newVolume) {
             this._volume = newVolume;
-            if (this._audioEngine.canUseWebAudio) {
+            if (BABYLON.Engine.audioEngine.canUseWebAudio) {
                 this._soundGain.gain.value = newVolume;
             }
         };
+        Sound.prototype.setPlaybackRate = function (newPlaybackRate) {
+            this._playbackRate = newPlaybackRate;
+            if (this._isPlaying) {
+                this._soundSource.playbackRate.value = this._playbackRate;
+            }
+        };
         Sound.prototype.getVolume = function () {
             return this._volume;
         };

+ 37 - 18
Babylon/Audio/babylon.sound.ts

@@ -11,8 +11,8 @@
         public maxDistance: number = 100;
         public distanceModel: string = "linear";
         public panningModel: string = "HRTF";
-        public playbackRate: number = 1;
         public onended: () => any;
+        private _playbackRate: number = 1;
         private _startTime: number = 0;
         private _startOffset: number = 0;
         private _position: Vector3 = Vector3.Zero();
@@ -22,7 +22,7 @@
         private _isReadyToPlay: boolean = false;
         private _isPlaying: boolean = false;
         private _isDirectional: boolean = false;
-        private _readyToPlayCallback;
+        private _readyToPlayCallback: () => any;
         private _audioBuffer: AudioBuffer;
         private _soundSource: AudioBufferSourceNode;
         private _soundPanner: PannerNode;
@@ -37,7 +37,7 @@
         private _scene: Scene;
         private _connectedMesh: AbstractMesh;
         private _customAttenuationFunction: (currentVolume: number, currentDistance: number, maxDistance: number, refDistance: number, rolloffFactor: number) => number;
-        private _registerFunc;
+        private _registerFunc: (connectedMesh: AbstractMesh) => any;
 
         /**
         * Create a sound and attach it to a scene
@@ -73,7 +73,7 @@
                 this.refDistance = options.refDistance || 1;
                 this.distanceModel = options.distanceModel || "linear";
                 this.panningModel = options.panningModel || "HRTF";
-                this.playbackRate = options.playbackRate || 1;
+                this._playbackRate = options.playbackRate || 1;
             }
 
             if (Engine.audioEngine.canUseWebAudio) {
@@ -85,18 +85,28 @@
                     this._createSpatialParameters();
                 }
                 this._scene.mainSoundTrack.AddSound(this);
-                if (typeof (urlOrArrayBuffer) === "string") {
-                    Tools.LoadFile(urlOrArrayBuffer, (data) => { this._soundLoaded(data); }, null, null, true);
-                }
-                else {
-                    if (urlOrArrayBuffer instanceof ArrayBuffer) {
-                        this._soundLoaded(urlOrArrayBuffer);
+                // if no parameter is passed, you need to call setAudioBuffer yourself to prepare the sound
+                if (urlOrArrayBuffer) {
+                    // If it's an URL
+                    if (typeof (urlOrArrayBuffer) === "string") {
+                        Tools.LoadFile(urlOrArrayBuffer,(data) => { this._soundLoaded(data); }, null, null, true);
                     }
                     else {
-                        Tools.Error("Parameter must be a URL to the sound or an ArrayBuffer of the sound.");
+                        if (urlOrArrayBuffer instanceof ArrayBuffer) {
+                            this._soundLoaded(urlOrArrayBuffer);
+                        }
+                        else {
+                            Tools.Error("Parameter must be a URL to the sound or an ArrayBuffer of the sound.");
+                        }
                     }
                 }
             }
+            else {
+                if (!Engine.audioEngine.WarnedWebAudioUnsupported) {
+                    BABYLON.Tools.Error("Web Audio is not supported by your browser.");
+                    Engine.audioEngine.WarnedWebAudioUnsupported = true;
+                }
+            }
         }
 
         public dispose() {
@@ -137,11 +147,13 @@
             }, (error) => { Tools.Error("Error while decoding audio data: " + error.err); });
         }
 
-        public setAudioBuffer(audioBuffer : AudioBuffer) : void{
-            this._audioBuffer = audioBuffer;
-            this._isReadyToPlay = true;
+        public setAudioBuffer(audioBuffer: AudioBuffer): void {
+            if (Engine.audioEngine.canUseWebAudio) {
+                this._audioBuffer = audioBuffer;
+                this._isReadyToPlay = true;
+            }
         }
-        
+
         public updateOptions(options) {
             if (options) {
                 this.loop = options.loop || this.loop;
@@ -151,7 +163,7 @@
                 this.refDistance = options.refDistance || this.refDistance;
                 this.distanceModel = options.distanceModel || this.distanceModel;
                 this.panningModel = options.panningModel || this.panningModel;
-                this.playbackRate = options.playbackRate || this.playbackRate;
+                this._playbackRate = options.playbackRate || this._playbackRate;
             }
         }
 
@@ -270,7 +282,7 @@
                     this._soundSource.buffer = this._audioBuffer;
                     this._soundSource.connect(this._inputAudioNode);
                     this._soundSource.loop = this.loop;
-                    this._soundSource.playbackRate.value = this.playbackRate;
+                    this._soundSource.playbackRate.value = this._playbackRate;
                     this._startTime = startTime;
                     if (this.onended) {
                         this._soundSource.onended = this.onended;
@@ -310,6 +322,13 @@
             }
         }
 
+        public setPlaybackRate(newPlaybackRate: number) {
+            this._playbackRate = newPlaybackRate;
+            if (this._isPlaying) {
+                this._soundSource.playbackRate.value = this._playbackRate;
+            }
+        }
+
         public getVolume(): number {
             return this._volume;
         }
@@ -335,4 +354,4 @@
             }
         }
     }
-}
+}

+ 1 - 1
Babylon/Audio/babylon.soundtrack.js

@@ -5,7 +5,7 @@ var BABYLON;
             this.id = -1;
             this._isMainTrack = false;
             this._scene = scene;
-            this._audioEngine = scene.getEngine().getAudioEngine();
+            this._audioEngine = BABYLON.Engine.audioEngine;
             this.soundCollection = new Array();
             if (this._audioEngine.canUseWebAudio) {
                 this._trackGain = this._audioEngine.audioContext.createGain();

+ 1 - 1
Babylon/Audio/babylon.soundtrack.ts

@@ -11,7 +11,7 @@
 
         constructor(scene: BABYLON.Scene, options?: any) {
             this._scene = scene;
-            this._audioEngine = scene.getEngine().getAudioEngine();
+            this._audioEngine = Engine.audioEngine;
             this.soundCollection = new Array();
             if (this._audioEngine.canUseWebAudio) {
                 this._trackGain = this._audioEngine.audioContext.createGain();

+ 3 - 2
Babylon/Loading/Plugins/babylon.babylonFileLoader.js

@@ -762,7 +762,8 @@ var BABYLON;
                 rolloffFactor: parsedSound.rolloffFactor,
                 refDistance: parsedSound.refDistance,
                 distanceModel: parsedSound.distanceModel,
-                panningModel: parsedSound.panningModel
+                panningModel: parsedSound.panningModel,
+                playbackRate: parsedSound.playbackRate
             };
             var newSound = new BABYLON.Sound(soundName, soundUrl, scene, function () {
                 scene._removePendingData(newSound);
@@ -1194,7 +1195,7 @@ var BABYLON;
                     }
                 }
                 // Sounds
-                if (parsedData.sounds && scene.getEngine().getAudioEngine().canUseWebAudio) {
+                if (parsedData.sounds && BABYLON.Engine.audioEngine.canUseWebAudio) {
                     for (index = 0; index < parsedData.sounds.length; index++) {
                         var parsedSound = parsedData.sounds[index];
                         parseSound(parsedSound, scene, rootUrl);

+ 3 - 2
Babylon/Loading/Plugins/babylon.babylonFileLoader.ts

@@ -953,7 +953,8 @@
             rolloffFactor: parsedSound.rolloffFactor,
             refDistance: parsedSound.refDistance,
             distanceModel: parsedSound.distanceModel,
-            panningModel: parsedSound.panningModel
+            panningModel: parsedSound.panningModel,
+            playbackRate: parsedSound.playbackRate
         };
 
         var newSound = new BABYLON.Sound(soundName, soundUrl, scene, () => { scene._removePendingData(newSound); }, options);
@@ -1468,7 +1469,7 @@
             }
 
             // Sounds
-            if (parsedData.sounds && scene.getEngine().getAudioEngine().canUseWebAudio) {
+            if (parsedData.sounds && Engine.audioEngine.canUseWebAudio) {
                 for (index = 0; index < parsedData.sounds.length; index++) {
                     var parsedSound = parsedData.sounds[index];
                     parseSound(parsedSound, scene, rootUrl);

+ 0 - 3
Babylon/babylon.engine.js

@@ -558,9 +558,6 @@ var BABYLON;
                 version: this._glVersion
             };
         };
-        Engine.prototype.getAudioEngine = function () {
-            return Engine.audioEngine;
-        };
         Engine.prototype.getAspectRatio = function (camera) {
             var viewport = camera.viewport;
             return (this.getRenderWidth() * viewport.width) / (this.getRenderHeight() * viewport.height);

+ 0 - 4
Babylon/babylon.engine.ts

@@ -646,10 +646,6 @@
             }
         }
 
-        public getAudioEngine(): AudioEngine {
-            return Engine.audioEngine;
-        }
-
         public getAspectRatio(camera: Camera): number {
             var viewport = camera.viewport;
             return (this.getRenderWidth() * viewport.width) / (this.getRenderHeight() * viewport.height);

File diff suppressed because it is too large
+ 241 - 39
babylon.2.0-beta.debug.js


File diff suppressed because it is too large
+ 13 - 13
babylon.2.0-beta.js


File diff suppressed because it is too large
+ 2542 - 2436
babylon.2.0.d.ts