Selaa lähdekoodia

Migrate audio out of Engine

sebastien 7 vuotta sitten
vanhempi
commit
f558f41ac3

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

@@ -38,7 +38,7 @@ module BABYLON {
         private _debugCanvasContext: Nullable<CanvasRenderingContext2D>;
         private _scene: Scene;
         private _registerFunc: Nullable<() => void>;
-        private _audioEngine: AudioEngine;
+        private _audioEngine: IAudioEngine;
 
         /**
          * Creates a new analyser

+ 43 - 2
src/Audio/babylon.audioEngine.ts

@@ -1,5 +1,45 @@
 module BABYLON {
-    export class AudioEngine {
+    /**
+     * This represents an audio engine and it is responsible
+     * to play, synchronize and analyse sounds throughout the  application.
+     */
+    export interface IAudioEngine extends IDisposable {
+        /**
+         * Gets whether the current host supports Web Audio and thus could create AudioContexts.
+         */
+        readonly canUseWebAudio: boolean;
+
+        /**
+         * Gets the current AudioContext if available.
+         */
+        readonly audioContext: Nullable<AudioContext>;
+
+        /**
+         * The master gain node defines the global audio volume of your audio engine.
+         */
+        readonly masterGain: GainNode;
+
+        /**
+         * Gets whether or not mp3 are supported by your browser.
+         */
+        readonly isMP3supported: boolean;
+
+        /**
+         * Gets whether or not ogg are supported by your browser.
+         */
+        readonly isOGGsupported: boolean;
+
+        /**
+         * Defines if Babylon should emit a warning if WebAudio is not supported.
+         * @ignoreNaming
+         */
+        WarnedWebAudioUnsupported: boolean;
+    }
+
+    // Sets the default audio engine to Babylon JS.
+    Engine.AudioEngineFactory = () => { return new AudioEngine(); };
+
+    export class AudioEngine implements IAudioEngine{
         private _audioContext: Nullable<AudioContext> = null;
         private _audioContextInitialized = false;
         public canUseWebAudio: boolean = false;
@@ -102,7 +142,8 @@
                     this._connectedAnalyser.stopDebugCanvas();
                     this._connectedAnalyser.dispose();
                     this.masterGain.disconnect();
-                    this.masterGain.connect(this._audioContext.destination);
+                    // Looks Strange to me.
+                    // this.masterGain.connect(this._audioContext.destination);
                     this._connectedAnalyser = null;
                 }
                 this.masterGain.gain.value = 1;

+ 12 - 4
src/Engine/babylon.engine.ts

@@ -667,7 +667,14 @@
          * @see http://doc.babylonjs.com/how_to/playing_sounds_and_music
          * @ignorenaming
          */
-        public static audioEngine: AudioEngine;
+        public static audioEngine: IAudioEngine;
+
+        /**
+         * Default AudioEngine Factory responsible of creating the Audio Engine.
+         * By default, this will create a BabylonJS Audio Engine if the workload
+         * has been embedded.
+         */
+        public static AudioEngineFactory: () => IAudioEngine;
 
         // Focus
         private _onFocus: () => void;
@@ -1176,8 +1183,9 @@
                 window.addEventListener('vrdisplaypointerunrestricted', this._onVRDisplayPointerUnrestricted, false);
             }
 
-            if (options.audioEngine && AudioEngine && !Engine.audioEngine) {
-                Engine.audioEngine = new AudioEngine();
+            // Create Audio Engine if needed.
+            if (!Engine.audioEngine && options.audioEngine && Engine.AudioEngineFactory) {
+                Engine.audioEngine = Engine.AudioEngineFactory();
             }
 
             // Prepare buffer pointers
@@ -6733,7 +6741,7 @@
             }
 
             // Release audio engine
-            if (Engine.audioEngine) {
+            if (Engine.Instances.length === 1 && Engine.audioEngine) {
                 Engine.audioEngine.dispose();
             }
 

+ 2 - 1
src/babylon.scene.ts

@@ -4617,7 +4617,7 @@
                 listeningCamera = this.activeCamera;
             }
 
-            if (listeningCamera && audioEngine.canUseWebAudio && audioEngine.audioContext) {
+            if (listeningCamera && audioEngine.audioContext) {
                 audioEngine.audioContext.listener.setPosition(listeningCamera.position.x, listeningCamera.position.y, listeningCamera.position.z);
                 // for VR cameras
                 if (listeningCamera.rigCameras && listeningCamera.rigCameras.length > 0) {
@@ -4630,6 +4630,7 @@
                 if (!isNaN(cameraDirection.x) && !isNaN(cameraDirection.y) && !isNaN(cameraDirection.z)) {
                     audioEngine.audioContext.listener.setOrientation(cameraDirection.x, cameraDirection.y, cameraDirection.z, 0, 1, 0);
                 }
+
                 var i: number;
                 for (i = 0; i < this.mainSoundTrack.soundCollection.length; i++) {
                     var sound = this.mainSoundTrack.soundCollection[i];