Quellcode durchsuchen

Merge pull request #1213 from julien-moreau/master

Adding sound serialization
David Catuhe vor 9 Jahren
Ursprung
Commit
cd42106227
2 geänderte Dateien mit 71 neuen und 5 gelöschten Zeilen
  1. 33 3
      src/Audio/babylon.sound.js
  2. 38 2
      src/Audio/babylon.sound.ts

+ 33 - 3
src/Audio/babylon.sound.js

@@ -306,7 +306,7 @@ var BABYLON;
         * Play the sound
         * @param time (optional) Start the sound after X seconds. Start immediately (0) by default.
         */
-        Sound.prototype.play = function (time) {
+        Sound.prototype.play = function (time, offset) {
             var _this = this;
             if (this._isReadyToPlay && this._scene.audioEnabled) {
                 try {
@@ -348,7 +348,7 @@ var BABYLON;
                         this._soundSource.loop = this.loop;
                         this._soundSource.playbackRate.value = this._playbackRate;
                         this._soundSource.onended = function () { _this._onended(); };
-                        this._soundSource.start(startTime, this.isPaused ? this._startOffset % this._soundSource.buffer.duration : 0);
+                        this._soundSource.start(startTime, this.isPaused ? this._startOffset % this._soundSource.buffer.duration : offset ? offset : 0);
                     }
                     this._startTime = startTime;
                     this.isPlaying = true;
@@ -497,6 +497,36 @@ var BABYLON;
         Sound.prototype.getAudioBuffer = function () {
             return this._audioBuffer;
         };
+        Sound.prototype.serialize = function () {
+            var serializationObject = {
+                name: this.name,
+                url: this.name,
+                autoplay: this.autoplay,
+                loop: this.loop,
+                volume: this._volume,
+                spatialSound: this.spatialSound,
+                maxDistance: this.maxDistance,
+                rolloffFactor: this.rolloffFactor,
+                refDistance: this.refDistance,
+                distanceModel: this.distanceModel,
+                playbackRate: this._playbackRate,
+                panningModel: this._panningModel,
+                soundTrackId: this.soundTrackId
+            };
+            if (this.spatialSound) {
+                if (this._connectedMesh)
+                    serializationObject.connectedMeshId = this._connectedMesh.id;
+                serializationObject.position = this._position.asArray();
+                serializationObject.refDistance = this.refDistance;
+                serializationObject.distanceModel = this.distanceModel;
+                serializationObject.isDirectional = this._isDirectional;
+                serializationObject.localDirectionToMesh = this._localDirection.asArray();
+                serializationObject.coneInnerAngle = this._coneInnerAngle;
+                serializationObject.coneOuterAngle = this._coneOuterAngle;
+                serializationObject.coneOuterGain = this._coneOuterGain;
+            }
+            return serializationObject;
+        };
         Sound.Parse = function (parsedSound, scene, rootUrl, sourceSound) {
             var soundName = parsedSound.name;
             var soundUrl;
@@ -555,6 +585,6 @@ var BABYLON;
             return newSound;
         };
         return Sound;
-    })();
+    }());
     BABYLON.Sound = Sound;
 })(BABYLON || (BABYLON = {}));

+ 38 - 2
src/Audio/babylon.sound.ts

@@ -339,8 +339,9 @@
         /**
         * Play the sound
         * @param time (optional) Start the sound after X seconds. Start immediately (0) by default.
+        * @param offset (optional) Start the sound setting it at a specific time
         */
-        public play(time?: number) {
+        public play(time?: number, offset?: number) {
             if (this._isReadyToPlay && this._scene.audioEnabled) {
                 try {
                     if (this._startOffset < 0) {
@@ -381,7 +382,7 @@
                         this._soundSource.loop = this.loop;
                         this._soundSource.playbackRate.value = this._playbackRate;
                         this._soundSource.onended = () => { this._onended(); };
-                        this._soundSource.start(startTime, this.isPaused ? this._startOffset % this._soundSource.buffer.duration : 0);
+                        this._soundSource.start(startTime, this.isPaused ? this._startOffset % this._soundSource.buffer.duration : offset ? offset : 0);
                     }
                     this._startTime = startTime;
                     this.isPlaying = true;
@@ -542,6 +543,41 @@
             return this._audioBuffer;
         }
 
+        public serialize(): any {
+            var serializationObject: any = {
+                name: this.name,
+                url: this.name,
+                autoplay: this.autoplay,
+                loop: this.loop,
+                volume: this._volume,
+                spatialSound: this.spatialSound,
+                maxDistance: this.maxDistance,
+                rolloffFactor: this.rolloffFactor,
+                refDistance: this.refDistance,
+                distanceModel: this.distanceModel,
+                playbackRate: this._playbackRate,
+                panningModel: this._panningModel,
+                soundTrackId: this.soundTrackId
+            };
+
+            if (this.spatialSound) {
+                if (this._connectedMesh)
+                    serializationObject.connectedMeshId = this._connectedMesh.id;
+
+                serializationObject.position = this._position.asArray();
+                serializationObject.refDistance = this.refDistance;
+                serializationObject.distanceModel = this.distanceModel;
+
+                serializationObject.isDirectional = this._isDirectional;
+                serializationObject.localDirectionToMesh = this._localDirection.asArray();
+                serializationObject.coneInnerAngle = this._coneInnerAngle;
+                serializationObject.coneOuterAngle = this._coneOuterAngle;
+                serializationObject.coneOuterGain = this._coneOuterGain;
+            }
+
+            return serializationObject;
+        }
+
         public static Parse(parsedSound: any, scene: Scene, rootUrl: string, sourceSound?: Sound): Sound {
             var soundName = parsedSound.name;
             var soundUrl;