|
@@ -6,7 +6,7 @@ var BABYLON;
|
|
|
* @param name Name of your sound
|
|
|
* @param urlOrArrayBuffer Url to the sound to load async or ArrayBuffer
|
|
|
* @param readyToPlayCallback Provide a callback function if you'd like to load your code once the sound is ready to be played
|
|
|
- * @param options Objects to provide with the current available options: autoplay, loop, volume, spatialSound, maxDistance, rolloffFactor, refDistance, distanceModel, panningModel
|
|
|
+ * @param options Objects to provide with the current available options: autoplay, loop, volume, spatialSound, maxDistance, rolloffFactor, refDistance, distanceModel, panningModel, streaming
|
|
|
*/
|
|
|
function Sound(name, urlOrArrayBuffer, scene, readyToPlayCallback, options) {
|
|
|
var _this = this;
|
|
@@ -103,7 +103,9 @@ var BABYLON;
|
|
|
}
|
|
|
else {
|
|
|
if (urlOrArrayBuffer instanceof ArrayBuffer) {
|
|
|
- this._soundLoaded(urlOrArrayBuffer);
|
|
|
+ if (urlOrArrayBuffer.byteLength > 0) {
|
|
|
+ this._soundLoaded(urlOrArrayBuffer);
|
|
|
+ }
|
|
|
}
|
|
|
else {
|
|
|
BABYLON.Tools.Error("Parameter must be a URL to the sound or an ArrayBuffer of the sound.");
|
|
@@ -420,6 +422,10 @@ var BABYLON;
|
|
|
};
|
|
|
Sound.prototype.attachToMesh = function (meshToConnectTo) {
|
|
|
var _this = this;
|
|
|
+ if (this._connectedMesh) {
|
|
|
+ this._connectedMesh.unregisterAfterWorldMatrixUpdate(this._registerFunc);
|
|
|
+ this._registerFunc = null;
|
|
|
+ }
|
|
|
this._connectedMesh = meshToConnectTo;
|
|
|
if (!this.spatialSound) {
|
|
|
this.spatialSound = true;
|
|
@@ -439,7 +445,44 @@ var BABYLON;
|
|
|
this._updateDirection();
|
|
|
}
|
|
|
};
|
|
|
- Sound.Parse = function (parsedSound, scene, rootUrl) {
|
|
|
+ Sound.prototype.clone = function () {
|
|
|
+ var _this = this;
|
|
|
+ if (!this._streaming) {
|
|
|
+ var setBufferAndRun = function () {
|
|
|
+ if (_this._isReadyToPlay) {
|
|
|
+ clonedSound._audioBuffer = _this.getAudioBuffer();
|
|
|
+ clonedSound._isReadyToPlay = true;
|
|
|
+ if (clonedSound.autoplay) {
|
|
|
+ clonedSound.play();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ window.setTimeout(setBufferAndRun, 300);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ var currentOptions = {
|
|
|
+ autoplay: this.autoplay, loop: this.loop,
|
|
|
+ volume: this._volume, spatialSound: this.spatialSound, maxDistance: this.maxDistance,
|
|
|
+ useCustomAttenuation: this.useCustomAttenuation, rolloffFactor: this.rolloffFactor,
|
|
|
+ refDistance: this.refDistance, distanceModel: this.distanceModel
|
|
|
+ };
|
|
|
+ var clonedSound = new Sound(this.name + "_cloned", new ArrayBuffer(0), this._scene, null, currentOptions);
|
|
|
+ if (this.useCustomAttenuation) {
|
|
|
+ clonedSound.setAttenuationFunction(this._customAttenuationFunction);
|
|
|
+ }
|
|
|
+ clonedSound.setPosition(this._position);
|
|
|
+ clonedSound.setPlaybackRate(this._playbackRate);
|
|
|
+ setBufferAndRun();
|
|
|
+ return clonedSound;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ Sound.prototype.getAudioBuffer = function () {
|
|
|
+ return this._audioBuffer;
|
|
|
+ };
|
|
|
+ Sound.Parse = function (parsedSound, scene, rootUrl, sourceSound) {
|
|
|
var soundName = parsedSound.name;
|
|
|
var soundUrl = rootUrl + soundName;
|
|
|
var options = {
|
|
@@ -450,8 +493,27 @@ var BABYLON;
|
|
|
distanceModel: parsedSound.distanceModel,
|
|
|
playbackRate: parsedSound.playbackRate
|
|
|
};
|
|
|
- var newSound = new Sound(soundName, soundUrl, scene, function () { scene._removePendingData(newSound); }, options);
|
|
|
- scene._addPendingData(newSound);
|
|
|
+ var newSound;
|
|
|
+ if (!sourceSound) {
|
|
|
+ newSound = new Sound(soundName, soundUrl, scene, function () { scene._removePendingData(newSound); }, options);
|
|
|
+ scene._addPendingData(newSound);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ var setBufferAndRun = function () {
|
|
|
+ if (sourceSound._isReadyToPlay) {
|
|
|
+ newSound._audioBuffer = sourceSound.getAudioBuffer();
|
|
|
+ newSound._isReadyToPlay = true;
|
|
|
+ if (newSound.autoplay) {
|
|
|
+ newSound.play();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ window.setTimeout(setBufferAndRun, 300);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ newSound = new Sound(soundName, new ArrayBuffer(0), scene, null, options);
|
|
|
+ setBufferAndRun();
|
|
|
+ }
|
|
|
if (parsedSound.position) {
|
|
|
var soundPosition = BABYLON.Vector3.FromArray(parsedSound.position);
|
|
|
newSound.setPosition(soundPosition);
|