浏览代码

Fixing bug with WebAudio volume

David Catuhe 10 年之前
父节点
当前提交
30e1864f60

+ 13 - 4
Babylon/Audio/babylon.sound.js

@@ -162,6 +162,10 @@ var BABYLON;
                 this.refDistance = options.refDistance || this.refDistance;
                 this.distanceModel = options.distanceModel || this.distanceModel;
                 this._playbackRate = options.playbackRate || this._playbackRate;
+                this._updateSpatialParameters();
+                if (this.isPlaying) {
+                    this._soundSource.playbackRate.value = this._playbackRate;
+                }
             }
         };
         Sound.prototype._createSpatialParameters = function () {
@@ -170,6 +174,13 @@ var BABYLON;
                     this._panningModel = "HRTF";
                 }
                 this._soundPanner = BABYLON.Engine.audioEngine.audioContext.createPanner();
+                this._updateSpatialParameters();
+                this._soundPanner.connect(this._ouputAudioNode);
+                this._inputAudioNode = this._soundPanner;
+            }
+        };
+        Sound.prototype._updateSpatialParameters = function () {
+            if (this.spatialSound) {
                 if (this.useCustomAttenuation) {
                     // Tricks to disable in a way embedded Web Audio attenuation 
                     this._soundPanner.distanceModel = "linear";
@@ -185,8 +196,6 @@ var BABYLON;
                     this._soundPanner.rolloffFactor = this.rolloffFactor;
                     this._soundPanner.panningModel = this._panningModel;
                 }
-                this._soundPanner.connect(this._ouputAudioNode);
-                this._inputAudioNode = this._soundPanner;
             }
         };
         Sound.prototype.switchPanningModelToHRTF = function () {
@@ -321,7 +330,7 @@ var BABYLON;
             }
         };
         Sound.prototype.setVolume = function (newVolume, time) {
-            if (BABYLON.Engine.audioEngine.canUseWebAudio && !this.spatialSound) {
+            if (BABYLON.Engine.audioEngine.canUseWebAudio) {
                 if (time) {
                     this._soundGain.gain.linearRampToValueAtTime(this._volume, BABYLON.Engine.audioEngine.audioContext.currentTime);
                     this._soundGain.gain.linearRampToValueAtTime(newVolume, time);
@@ -345,8 +354,8 @@ var BABYLON;
             var _this = this;
             this._connectedMesh = meshToConnectTo;
             if (!this.spatialSound) {
-                this._createSpatialParameters();
                 this.spatialSound = true;
+                this._createSpatialParameters();
                 if (this.isPlaying && this.loop) {
                     this.stop();
                     this.play();

+ 13 - 4
Babylon/Audio/babylon.sound.ts

@@ -176,6 +176,10 @@
                 this.refDistance = options.refDistance || this.refDistance;
                 this.distanceModel = options.distanceModel || this.distanceModel;
                 this._playbackRate = options.playbackRate || this._playbackRate;
+                this._updateSpatialParameters();
+                if (this.isPlaying) {
+                    this._soundSource.playbackRate.value = this._playbackRate;
+                }
             }
         }
 
@@ -185,7 +189,14 @@
                     this._panningModel = "HRTF";
                 }
                 this._soundPanner = Engine.audioEngine.audioContext.createPanner();
+                this._updateSpatialParameters();
+                this._soundPanner.connect(this._ouputAudioNode);
+                this._inputAudioNode = this._soundPanner;
+            }
+        }
 
+        private _updateSpatialParameters() {
+            if (this.spatialSound) {
                 if (this.useCustomAttenuation) {
                     // Tricks to disable in a way embedded Web Audio attenuation 
                     this._soundPanner.distanceModel = "linear";
@@ -201,8 +212,6 @@
                     this._soundPanner.rolloffFactor = this.rolloffFactor;
                     this._soundPanner.panningModel = this._panningModel;
                 }
-                this._soundPanner.connect(this._ouputAudioNode);
-                this._inputAudioNode = this._soundPanner;
             }
         }
 
@@ -354,7 +363,7 @@
         }
 
         public setVolume(newVolume: number, time?: number) {
-            if (Engine.audioEngine.canUseWebAudio && !this.spatialSound) {
+            if (Engine.audioEngine.canUseWebAudio) {
                 if (time) {
                     this._soundGain.gain.linearRampToValueAtTime(this._volume, Engine.audioEngine.audioContext.currentTime);
                     this._soundGain.gain.linearRampToValueAtTime(newVolume, time);
@@ -380,8 +389,8 @@
         public attachToMesh(meshToConnectTo: AbstractMesh) {
             this._connectedMesh = meshToConnectTo;
             if (!this.spatialSound) {
-                this._createSpatialParameters();
                 this.spatialSound = true;
+                this._createSpatialParameters();
                 if (this.isPlaying && this.loop) {
                     this.stop();
                     this.play();

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

@@ -8,11 +8,11 @@ var BABYLON;
             this._audioEngine = BABYLON.Engine.audioEngine;
             this.soundCollection = new Array();
             if (this._audioEngine.canUseWebAudio) {
-                this._trackGain = this._audioEngine.audioContext.createGain();
-                this._trackGain.connect(this._audioEngine.masterGain);
+                this._outputAudioNode = this._audioEngine.audioContext.createGain();
+                this._outputAudioNode.connect(this._audioEngine.masterGain);
                 if (options) {
                     if (options.volume) {
-                        this._trackGain.gain.value = options.volume;
+                        this._outputAudioNode.gain.value = options.volume;
                     }
                     if (options.mainTrack) {
                         this._isMainTrack = options.mainTrack;
@@ -32,13 +32,13 @@ var BABYLON;
                 while (this.soundCollection.length) {
                     this.soundCollection[0].dispose();
                 }
-                this._trackGain.disconnect();
-                this._trackGain = null;
+                this._outputAudioNode.disconnect();
+                this._outputAudioNode = null;
             }
         };
         SoundTrack.prototype.AddSound = function (sound) {
             if (BABYLON.Engine.audioEngine.canUseWebAudio) {
-                sound.connectToSoundTrackAudioNode(this._trackGain);
+                sound.connectToSoundTrackAudioNode(this._outputAudioNode);
             }
             if (sound.soundTrackId) {
                 if (sound.soundTrackId === -1) {
@@ -59,7 +59,7 @@ var BABYLON;
         };
         SoundTrack.prototype.setVolume = function (newVolume) {
             if (this._audioEngine.canUseWebAudio) {
-                this._trackGain.gain.value = newVolume;
+                this._outputAudioNode.gain.value = newVolume;
             }
         };
         SoundTrack.prototype.switchPanningModelToHRTF = function () {
@@ -82,8 +82,8 @@ var BABYLON;
             }
             this._connectedAnalyser = analyser;
             if (this._audioEngine.canUseWebAudio) {
-                this._trackGain.disconnect();
-                this._connectedAnalyser.connectAudioNodes(this._trackGain, this._audioEngine.masterGain);
+                this._outputAudioNode.disconnect();
+                this._connectedAnalyser.connectAudioNodes(this._outputAudioNode, this._audioEngine.masterGain);
             }
         };
         return SoundTrack;

+ 11 - 10
Babylon/Audio/babylon.soundtrack.ts

@@ -1,7 +1,8 @@
 module BABYLON {
     export class SoundTrack {
         private _audioEngine: BABYLON.AudioEngine;
-        private _trackGain: GainNode;
+        private _outputAudioNode: GainNode;
+        private _inputAudioNode: AudioNode;
         private _trackConvolver: ConvolverNode;
         private _scene: BABYLON.Scene;
         public id: number = -1;
@@ -14,11 +15,11 @@
             this._audioEngine = Engine.audioEngine;
             this.soundCollection = new Array();
             if (this._audioEngine.canUseWebAudio) {
-                this._trackGain = this._audioEngine.audioContext.createGain();
-                this._trackGain.connect(this._audioEngine.masterGain);
+                this._outputAudioNode = this._audioEngine.audioContext.createGain();
+                this._outputAudioNode.connect(this._audioEngine.masterGain);
 
                 if (options) {
-                    if (options.volume) { this._trackGain.gain.value = options.volume; }
+                    if (options.volume) { this._outputAudioNode.gain.value = options.volume; }
                     if (options.mainTrack) { this._isMainTrack = options.mainTrack; }
                 }
             }
@@ -36,14 +37,14 @@
                 while (this.soundCollection.length) {
                     this.soundCollection[0].dispose();
                 }
-                this._trackGain.disconnect();
-                this._trackGain = null;
+                this._outputAudioNode.disconnect();
+                this._outputAudioNode = null;
             }
         }
 
         public AddSound(sound: BABYLON.Sound) {
             if (Engine.audioEngine.canUseWebAudio) {
-                sound.connectToSoundTrackAudioNode(this._trackGain);
+                sound.connectToSoundTrackAudioNode(this._outputAudioNode);
             }
             if (sound.soundTrackId) {
                 if (sound.soundTrackId === -1) {
@@ -67,7 +68,7 @@
 
         public setVolume(newVolume: number) {
             if (this._audioEngine.canUseWebAudio) {
-                this._trackGain.gain.value = newVolume;
+                this._outputAudioNode.gain.value = newVolume;
             }
         }
 
@@ -93,8 +94,8 @@
             }
             this._connectedAnalyser = analyser;
             if (this._audioEngine.canUseWebAudio) {
-                this._trackGain.disconnect();
-                this._connectedAnalyser.connectAudioNodes(this._trackGain, this._audioEngine.masterGain);
+                this._outputAudioNode.disconnect();
+                this._connectedAnalyser.connectAudioNodes(this._outputAudioNode, this._audioEngine.masterGain);
             }
         }
     }

+ 16 - 4
Babylon/Cameras/babylon.arcRotateCamera.js

@@ -1,4 +1,4 @@
-var __extends = this.__extends || function (d, b) {
+var __extends = (this && this.__extends) || function (d, b) {
     for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
     function __() { this.constructor = d; }
     __.prototype = b.prototype;
@@ -96,7 +96,11 @@ var BABYLON;
         ArcRotateCamera.prototype._isSynchronizedViewMatrix = function () {
             if (!_super.prototype._isSynchronizedViewMatrix.call(this))
                 return false;
-            return this._cache.target.equals(this._getTargetPosition()) && this._cache.alpha === this.alpha && this._cache.beta === this.beta && this._cache.radius === this.radius && this._cache.targetScreenOffset.equals(this.targetScreenOffset);
+            return this._cache.target.equals(this._getTargetPosition())
+                && this._cache.alpha === this.alpha
+                && this._cache.beta === this.beta
+                && this._cache.radius === this.radius
+                && this._cache.targetScreenOffset.equals(this.targetScreenOffset);
         };
         // Methods
         ArcRotateCamera.prototype.attachControl = function (element, noPreventDefault) {
@@ -191,7 +195,10 @@ var BABYLON;
                     }
                 };
                 this._onKeyDown = function (evt) {
-                    if (_this.keysUp.indexOf(evt.keyCode) !== -1 || _this.keysDown.indexOf(evt.keyCode) !== -1 || _this.keysLeft.indexOf(evt.keyCode) !== -1 || _this.keysRight.indexOf(evt.keyCode) !== -1) {
+                    if (_this.keysUp.indexOf(evt.keyCode) !== -1 ||
+                        _this.keysDown.indexOf(evt.keyCode) !== -1 ||
+                        _this.keysLeft.indexOf(evt.keyCode) !== -1 ||
+                        _this.keysRight.indexOf(evt.keyCode) !== -1) {
                         var index = _this._keys.indexOf(evt.keyCode);
                         if (index === -1) {
                             _this._keys.push(evt.keyCode);
@@ -204,7 +211,10 @@ var BABYLON;
                     }
                 };
                 this._onKeyUp = function (evt) {
-                    if (_this.keysUp.indexOf(evt.keyCode) !== -1 || _this.keysDown.indexOf(evt.keyCode) !== -1 || _this.keysLeft.indexOf(evt.keyCode) !== -1 || _this.keysRight.indexOf(evt.keyCode) !== -1) {
+                    if (_this.keysUp.indexOf(evt.keyCode) !== -1 ||
+                        _this.keysDown.indexOf(evt.keyCode) !== -1 ||
+                        _this.keysLeft.indexOf(evt.keyCode) !== -1 ||
+                        _this.keysRight.indexOf(evt.keyCode) !== -1) {
                         var index = _this._keys.indexOf(evt.keyCode);
                         if (index >= 0) {
                             _this._keys.splice(index, 1);
@@ -295,6 +305,7 @@ var BABYLON;
             if (this._collisionTriggered) {
                 return;
             }
+            // Keyboard
             for (var index = 0; index < this._keys.length; index++) {
                 var keyCode = this._keys[index];
                 if (this.keysLeft.indexOf(keyCode) !== -1) {
@@ -426,3 +437,4 @@ var BABYLON;
     })(BABYLON.Camera);
     BABYLON.ArcRotateCamera = ArcRotateCamera;
 })(BABYLON || (BABYLON = {}));
+//# sourceMappingURL=babylon.arcRotateCamera.js.map

+ 15 - 19
Babylon/Collisions/babylon.collisionCoordinator.js

@@ -44,7 +44,7 @@ var BABYLON;
                 };
                 var message = {
                     payload: payload,
-                    taskType: 1 /* UPDATE */
+                    taskType: WorkerTaskType.UPDATE
                 };
                 var serializable = [];
                 for (var id in payload.updatedGeometries) {
@@ -63,13 +63,13 @@ var BABYLON;
             };
             this._onMessageFromWorker = function (e) {
                 var returnData = e.data;
-                if (returnData.error != 0 /* SUCCESS */) {
+                if (returnData.error != WorkerReplyType.SUCCESS) {
                     //TODO what errors can be returned from the worker?
                     BABYLON.Tools.Warn("error returned from worker!");
                     return;
                 }
                 switch (returnData.taskType) {
-                    case 0 /* INIT */:
+                    case WorkerTaskType.INIT:
                         _this._init = true;
                         //Update the worked with ALL of the scene's current state
                         _this._scene.meshes.forEach(function (mesh) {
@@ -79,10 +79,10 @@ var BABYLON;
                             _this.onGeometryAdded(geometry);
                         });
                         break;
-                    case 1 /* UPDATE */:
+                    case WorkerTaskType.UPDATE:
                         _this._runningUpdated--;
                         break;
-                    case 2 /* COLLIDE */:
+                    case WorkerTaskType.COLLIDE:
                         _this._runningCollisionTask = false;
                         var returnPayload = returnData.payload;
                         if (!_this._collisionsCallbackArray[returnPayload.collisionId])
@@ -122,7 +122,7 @@ var BABYLON;
             };
             var message = {
                 payload: payload,
-                taskType: 2 /* COLLIDE */
+                taskType: WorkerTaskType.COLLIDE
             };
             this._worker.postMessage(message);
         };
@@ -134,7 +134,7 @@ var BABYLON;
             this._worker.onmessage = this._onMessageFromWorker;
             var message = {
                 payload: {},
-                taskType: 0 /* INIT */
+                taskType: WorkerTaskType.INIT
             };
             this._worker.postMessage(message);
         };
@@ -226,18 +226,12 @@ var BABYLON;
             //Legacy need no destruction method.
         };
         //No update in legacy mode
-        CollisionCoordinatorLegacy.prototype.onMeshAdded = function (mesh) {
-        };
-        CollisionCoordinatorLegacy.prototype.onMeshUpdated = function (mesh) {
-        };
-        CollisionCoordinatorLegacy.prototype.onMeshRemoved = function (mesh) {
-        };
-        CollisionCoordinatorLegacy.prototype.onGeometryAdded = function (geometry) {
-        };
-        CollisionCoordinatorLegacy.prototype.onGeometryUpdated = function (geometry) {
-        };
-        CollisionCoordinatorLegacy.prototype.onGeometryDeleted = function (geometry) {
-        };
+        CollisionCoordinatorLegacy.prototype.onMeshAdded = function (mesh) { };
+        CollisionCoordinatorLegacy.prototype.onMeshUpdated = function (mesh) { };
+        CollisionCoordinatorLegacy.prototype.onMeshRemoved = function (mesh) { };
+        CollisionCoordinatorLegacy.prototype.onGeometryAdded = function (geometry) { };
+        CollisionCoordinatorLegacy.prototype.onGeometryUpdated = function (geometry) { };
+        CollisionCoordinatorLegacy.prototype.onGeometryDeleted = function (geometry) { };
         CollisionCoordinatorLegacy.prototype._collideWithWorld = function (position, velocity, collider, maximumRetry, finalPosition, excludedMesh) {
             if (excludedMesh === void 0) { excludedMesh = null; }
             var closeDistance = BABYLON.Engine.CollisionsEpsilon * 10.0;
@@ -246,6 +240,7 @@ var BABYLON;
                 return;
             }
             collider._initialize(position, velocity, closeDistance);
+            // Check all meshes
             for (var index = 0; index < this._scene.meshes.length; index++) {
                 var mesh = this._scene.meshes[index];
                 if (mesh.isEnabled() && mesh.checkCollisions && mesh.subMeshes && mesh !== excludedMesh) {
@@ -270,3 +265,4 @@ var BABYLON;
     })();
     BABYLON.CollisionCoordinatorLegacy = CollisionCoordinatorLegacy;
 })(BABYLON || (BABYLON = {}));
+//# sourceMappingURL=babylon.collisionCoordinator.js.map

+ 11 - 9
Babylon/Collisions/babylon.collisionWorker.js

@@ -150,8 +150,8 @@ var BABYLON;
         CollisionDetectorTransferable.prototype.onInit = function (payload) {
             this._collisionCache = new CollisionCache();
             var reply = {
-                error: 0 /* SUCCESS */,
-                taskType: 0 /* INIT */
+                error: BABYLON.WorkerReplyType.SUCCESS,
+                taskType: BABYLON.WorkerTaskType.INIT
             };
             postMessage(reply, undefined);
         };
@@ -167,8 +167,8 @@ var BABYLON;
                 }
             }
             var replay = {
-                error: 0 /* SUCCESS */,
-                taskType: 1 /* UPDATE */
+                error: BABYLON.WorkerReplyType.SUCCESS,
+                taskType: BABYLON.WorkerTaskType.UPDATE
             };
             postMessage(replay, undefined);
         };
@@ -185,8 +185,8 @@ var BABYLON;
                 newPosition: finalPosition.asArray()
             };
             var reply = {
-                error: 0 /* SUCCESS */,
-                taskType: 2 /* COLLIDE */,
+                error: BABYLON.WorkerReplyType.SUCCESS,
+                taskType: BABYLON.WorkerTaskType.COLLIDE,
                 payload: replyPayload
             };
             postMessage(reply, undefined);
@@ -194,6 +194,7 @@ var BABYLON;
         return CollisionDetectorTransferable;
     })();
     BABYLON.CollisionDetectorTransferable = CollisionDetectorTransferable;
+    //check if we are in a web worker, as this code should NOT run on the main UI thread
     try {
         if (self && self instanceof WorkerGlobalScope) {
             //Window hack to allow including babylonjs native code. the <any> is for typescript.
@@ -208,13 +209,13 @@ var BABYLON;
             var onNewMessage = function (event) {
                 var message = event.data;
                 switch (message.taskType) {
-                    case 0 /* INIT */:
+                    case BABYLON.WorkerTaskType.INIT:
                         collisionDetector.onInit(message.payload);
                         break;
-                    case 2 /* COLLIDE */:
+                    case BABYLON.WorkerTaskType.COLLIDE:
                         collisionDetector.onCollision(message.payload);
                         break;
-                    case 1 /* UPDATE */:
+                    case BABYLON.WorkerTaskType.UPDATE:
                         collisionDetector.onUpdate(message.payload);
                         break;
                 }
@@ -226,3 +227,4 @@ var BABYLON;
         console.log("single worker init");
     }
 })(BABYLON || (BABYLON = {}));
+//# sourceMappingURL=babylon.collisionWorker.js.map

文件差异内容过多而无法显示
+ 47 - 33
babylon.2.1-rc.debug.js


文件差异内容过多而无法显示
+ 20 - 19
babylon.2.1-rc.js


文件差异内容过多而无法显示
+ 17 - 16
babylon.2.1-rc.noworker.js