babylon.sound.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Sound = (function () {
  4. /**
  5. * Create a sound and attach it to a scene
  6. * @param name Name of your sound
  7. * @param urlOrArrayBuffer Url to the sound to load async or ArrayBuffer
  8. * @param readyToPlayCallback Provide a callback function if you'd like to load your code once the sound is ready to be played
  9. * @param options Objects to provide with the current available options: autoplay, loop, volume, spatialSound, maxDistance, rolloffFactor, refDistance, distanceModel, panningModel, streaming
  10. */
  11. function Sound(name, urlOrArrayBuffer, scene, readyToPlayCallback, options) {
  12. var _this = this;
  13. this.autoplay = false;
  14. this.loop = false;
  15. this.useCustomAttenuation = false;
  16. this.spatialSound = false;
  17. this.refDistance = 1;
  18. this.rolloffFactor = 1;
  19. this.maxDistance = 100;
  20. this.distanceModel = "linear";
  21. this._panningModel = "equalpower";
  22. this._playbackRate = 1;
  23. this._streaming = false;
  24. this._startTime = 0;
  25. this._startOffset = 0;
  26. this._position = BABYLON.Vector3.Zero();
  27. this._localDirection = new BABYLON.Vector3(1, 0, 0);
  28. this._volume = 1;
  29. this._isLoaded = false;
  30. this._isReadyToPlay = false;
  31. this.isPlaying = false;
  32. this.isPaused = false;
  33. this._isDirectional = false;
  34. // Used if you'd like to create a directional sound.
  35. // If not set, the sound will be omnidirectional
  36. this._coneInnerAngle = 360;
  37. this._coneOuterAngle = 360;
  38. this._coneOuterGain = 0;
  39. this._isOutputConnected = false;
  40. this.name = name;
  41. this._scene = scene;
  42. this._readyToPlayCallback = readyToPlayCallback;
  43. // Default custom attenuation function is a linear attenuation
  44. this._customAttenuationFunction = function (currentVolume, currentDistance, maxDistance, refDistance, rolloffFactor) {
  45. if (currentDistance < maxDistance) {
  46. return currentVolume * (1 - currentDistance / maxDistance);
  47. }
  48. else {
  49. return 0;
  50. }
  51. };
  52. if (options) {
  53. this.autoplay = options.autoplay || false;
  54. this.loop = options.loop || false;
  55. // if volume === 0, we need another way to check this option
  56. if (options.volume !== undefined) {
  57. this._volume = options.volume;
  58. }
  59. this.spatialSound = options.spatialSound || false;
  60. this.maxDistance = options.maxDistance || 100;
  61. this.useCustomAttenuation = options.useCustomAttenuation || false;
  62. this.rolloffFactor = options.rolloffFactor || 1;
  63. this.refDistance = options.refDistance || 1;
  64. this.distanceModel = options.distanceModel || "linear";
  65. this._playbackRate = options.playbackRate || 1;
  66. this._streaming = options.streaming || false;
  67. }
  68. if (BABYLON.Engine.audioEngine.canUseWebAudio) {
  69. this._soundGain = BABYLON.Engine.audioEngine.audioContext.createGain();
  70. this._soundGain.gain.value = this._volume;
  71. this._inputAudioNode = this._soundGain;
  72. this._ouputAudioNode = this._soundGain;
  73. if (this.spatialSound) {
  74. this._createSpatialParameters();
  75. }
  76. this._scene.mainSoundTrack.AddSound(this);
  77. // if no parameter is passed, you need to call setAudioBuffer yourself to prepare the sound
  78. if (urlOrArrayBuffer) {
  79. // If it's an URL
  80. if (typeof (urlOrArrayBuffer) === "string") {
  81. // Loading sound using XHR2
  82. if (!this._streaming) {
  83. BABYLON.Tools.LoadFile(urlOrArrayBuffer, function (data) { _this._soundLoaded(data); }, null, this._scene.database, true);
  84. }
  85. else {
  86. this._htmlAudioElement = new Audio(urlOrArrayBuffer);
  87. this._htmlAudioElement.controls = false;
  88. this._htmlAudioElement.loop = this.loop;
  89. this._htmlAudioElement.crossOrigin = "anonymous";
  90. this._htmlAudioElement.preload = "auto";
  91. this._htmlAudioElement.addEventListener("canplaythrough", function () {
  92. _this._isReadyToPlay = true;
  93. if (_this.autoplay) {
  94. _this.play();
  95. }
  96. if (_this._readyToPlayCallback) {
  97. _this._readyToPlayCallback();
  98. }
  99. });
  100. document.body.appendChild(this._htmlAudioElement);
  101. }
  102. }
  103. else {
  104. if (urlOrArrayBuffer instanceof ArrayBuffer) {
  105. if (urlOrArrayBuffer.byteLength > 0) {
  106. this._soundLoaded(urlOrArrayBuffer);
  107. }
  108. }
  109. else {
  110. BABYLON.Tools.Error("Parameter must be a URL to the sound or an ArrayBuffer of the sound.");
  111. }
  112. }
  113. }
  114. }
  115. else {
  116. // Adding an empty sound to avoid breaking audio calls for non Web Audio browsers
  117. this._scene.mainSoundTrack.AddSound(this);
  118. if (!BABYLON.Engine.audioEngine.WarnedWebAudioUnsupported) {
  119. BABYLON.Tools.Error("Web Audio is not supported by your browser.");
  120. BABYLON.Engine.audioEngine.WarnedWebAudioUnsupported = true;
  121. }
  122. // Simulating a ready to play event to avoid breaking code for non web audio browsers
  123. if (this._readyToPlayCallback) {
  124. window.setTimeout(function () {
  125. _this._readyToPlayCallback();
  126. }, 1000);
  127. }
  128. }
  129. }
  130. Sound.prototype.dispose = function () {
  131. if (BABYLON.Engine.audioEngine.canUseWebAudio && this._isReadyToPlay) {
  132. if (this.isPlaying) {
  133. this.stop();
  134. }
  135. this._isReadyToPlay = false;
  136. if (this.soundTrackId === -1) {
  137. this._scene.mainSoundTrack.RemoveSound(this);
  138. }
  139. else {
  140. this._scene.soundTracks[this.soundTrackId].RemoveSound(this);
  141. }
  142. if (this._soundGain) {
  143. this._soundGain.disconnect();
  144. this._soundGain = null;
  145. }
  146. if (this._soundPanner) {
  147. this._soundPanner.disconnect();
  148. this._soundPanner = null;
  149. }
  150. if (this._soundSource) {
  151. this._soundSource.disconnect();
  152. this._soundSource = null;
  153. }
  154. this._audioBuffer = null;
  155. if (this._htmlAudioElement) {
  156. this._htmlAudioElement.pause();
  157. this._htmlAudioElement.src = "";
  158. document.body.removeChild(this._htmlAudioElement);
  159. }
  160. if (this._connectedMesh) {
  161. this._connectedMesh.unregisterAfterWorldMatrixUpdate(this._registerFunc);
  162. this._connectedMesh = null;
  163. }
  164. }
  165. };
  166. Sound.prototype._soundLoaded = function (audioData) {
  167. var _this = this;
  168. this._isLoaded = true;
  169. BABYLON.Engine.audioEngine.audioContext.decodeAudioData(audioData, function (buffer) {
  170. _this._audioBuffer = buffer;
  171. _this._isReadyToPlay = true;
  172. if (_this.autoplay) {
  173. _this.play();
  174. }
  175. if (_this._readyToPlayCallback) {
  176. _this._readyToPlayCallback();
  177. }
  178. }, function () { BABYLON.Tools.Error("Error while decoding audio data for: " + _this.name); });
  179. };
  180. Sound.prototype.setAudioBuffer = function (audioBuffer) {
  181. if (BABYLON.Engine.audioEngine.canUseWebAudio) {
  182. this._audioBuffer = audioBuffer;
  183. this._isReadyToPlay = true;
  184. }
  185. };
  186. Sound.prototype.updateOptions = function (options) {
  187. if (options) {
  188. this.loop = options.loop || this.loop;
  189. this.maxDistance = options.maxDistance || this.maxDistance;
  190. this.useCustomAttenuation = options.useCustomAttenuation || this.useCustomAttenuation;
  191. this.rolloffFactor = options.rolloffFactor || this.rolloffFactor;
  192. this.refDistance = options.refDistance || this.refDistance;
  193. this.distanceModel = options.distanceModel || this.distanceModel;
  194. this._playbackRate = options.playbackRate || this._playbackRate;
  195. this._updateSpatialParameters();
  196. if (this.isPlaying) {
  197. if (this._streaming) {
  198. this._htmlAudioElement.playbackRate = this._playbackRate;
  199. }
  200. else {
  201. this._soundSource.playbackRate.value = this._playbackRate;
  202. }
  203. }
  204. }
  205. };
  206. Sound.prototype._createSpatialParameters = function () {
  207. if (BABYLON.Engine.audioEngine.canUseWebAudio) {
  208. if (this._scene.headphone) {
  209. this._panningModel = "HRTF";
  210. }
  211. this._soundPanner = BABYLON.Engine.audioEngine.audioContext.createPanner();
  212. this._updateSpatialParameters();
  213. this._soundPanner.connect(this._ouputAudioNode);
  214. this._inputAudioNode = this._soundPanner;
  215. }
  216. };
  217. Sound.prototype._updateSpatialParameters = function () {
  218. if (this.spatialSound) {
  219. if (this.useCustomAttenuation) {
  220. // Tricks to disable in a way embedded Web Audio attenuation
  221. this._soundPanner.distanceModel = "linear";
  222. this._soundPanner.maxDistance = Number.MAX_VALUE;
  223. this._soundPanner.refDistance = 1;
  224. this._soundPanner.rolloffFactor = 1;
  225. this._soundPanner.panningModel = this._panningModel;
  226. }
  227. else {
  228. this._soundPanner.distanceModel = this.distanceModel;
  229. this._soundPanner.maxDistance = this.maxDistance;
  230. this._soundPanner.refDistance = this.refDistance;
  231. this._soundPanner.rolloffFactor = this.rolloffFactor;
  232. this._soundPanner.panningModel = this._panningModel;
  233. }
  234. }
  235. };
  236. Sound.prototype.switchPanningModelToHRTF = function () {
  237. this._panningModel = "HRTF";
  238. this._switchPanningModel();
  239. };
  240. Sound.prototype.switchPanningModelToEqualPower = function () {
  241. this._panningModel = "equalpower";
  242. this._switchPanningModel();
  243. };
  244. Sound.prototype._switchPanningModel = function () {
  245. if (BABYLON.Engine.audioEngine.canUseWebAudio && this.spatialSound) {
  246. this._soundPanner.panningModel = this._panningModel;
  247. }
  248. };
  249. Sound.prototype.connectToSoundTrackAudioNode = function (soundTrackAudioNode) {
  250. if (BABYLON.Engine.audioEngine.canUseWebAudio) {
  251. if (this._isOutputConnected) {
  252. this._ouputAudioNode.disconnect();
  253. }
  254. this._ouputAudioNode.connect(soundTrackAudioNode);
  255. this._isOutputConnected = true;
  256. }
  257. };
  258. /**
  259. * Transform this sound into a directional source
  260. * @param coneInnerAngle Size of the inner cone in degree
  261. * @param coneOuterAngle Size of the outer cone in degree
  262. * @param coneOuterGain Volume of the sound outside the outer cone (between 0.0 and 1.0)
  263. */
  264. Sound.prototype.setDirectionalCone = function (coneInnerAngle, coneOuterAngle, coneOuterGain) {
  265. if (coneOuterAngle < coneInnerAngle) {
  266. BABYLON.Tools.Error("setDirectionalCone(): outer angle of the cone must be superior or equal to the inner angle.");
  267. return;
  268. }
  269. this._coneInnerAngle = coneInnerAngle;
  270. this._coneOuterAngle = coneOuterAngle;
  271. this._coneOuterGain = coneOuterGain;
  272. this._isDirectional = true;
  273. if (this.isPlaying && this.loop) {
  274. this.stop();
  275. this.play();
  276. }
  277. };
  278. Sound.prototype.setPosition = function (newPosition) {
  279. this._position = newPosition;
  280. if (BABYLON.Engine.audioEngine.canUseWebAudio && this.spatialSound) {
  281. this._soundPanner.setPosition(this._position.x, this._position.y, this._position.z);
  282. }
  283. };
  284. Sound.prototype.setLocalDirectionToMesh = function (newLocalDirection) {
  285. this._localDirection = newLocalDirection;
  286. if (BABYLON.Engine.audioEngine.canUseWebAudio && this._connectedMesh && this.isPlaying) {
  287. this._updateDirection();
  288. }
  289. };
  290. Sound.prototype._updateDirection = function () {
  291. var mat = this._connectedMesh.getWorldMatrix();
  292. var direction = BABYLON.Vector3.TransformNormal(this._localDirection, mat);
  293. direction.normalize();
  294. this._soundPanner.setOrientation(direction.x, direction.y, direction.z);
  295. };
  296. Sound.prototype.updateDistanceFromListener = function () {
  297. if (BABYLON.Engine.audioEngine.canUseWebAudio && this._connectedMesh && this.useCustomAttenuation) {
  298. var distance = this._connectedMesh.getDistanceToCamera(this._scene.activeCamera);
  299. this._soundGain.gain.value = this._customAttenuationFunction(this._volume, distance, this.maxDistance, this.refDistance, this.rolloffFactor);
  300. }
  301. };
  302. Sound.prototype.setAttenuationFunction = function (callback) {
  303. this._customAttenuationFunction = callback;
  304. };
  305. /**
  306. * Play the sound
  307. * @param time (optional) Start the sound after X seconds. Start immediately (0) by default.
  308. */
  309. Sound.prototype.play = function (time, offset) {
  310. var _this = this;
  311. if (this._isReadyToPlay && this._scene.audioEnabled) {
  312. try {
  313. if (this._startOffset < 0) {
  314. time = -this._startOffset;
  315. this._startOffset = 0;
  316. }
  317. var startTime = time ? BABYLON.Engine.audioEngine.audioContext.currentTime + time : BABYLON.Engine.audioEngine.audioContext.currentTime;
  318. if (!this._soundSource || !this._streamingSource) {
  319. if (this.spatialSound) {
  320. this._soundPanner.setPosition(this._position.x, this._position.y, this._position.z);
  321. if (this._isDirectional) {
  322. this._soundPanner.coneInnerAngle = this._coneInnerAngle;
  323. this._soundPanner.coneOuterAngle = this._coneOuterAngle;
  324. this._soundPanner.coneOuterGain = this._coneOuterGain;
  325. if (this._connectedMesh) {
  326. this._updateDirection();
  327. }
  328. else {
  329. this._soundPanner.setOrientation(this._localDirection.x, this._localDirection.y, this._localDirection.z);
  330. }
  331. }
  332. }
  333. }
  334. if (this._streaming) {
  335. if (!this._streamingSource) {
  336. this._streamingSource = BABYLON.Engine.audioEngine.audioContext.createMediaElementSource(this._htmlAudioElement);
  337. this._htmlAudioElement.onended = function () { _this._onended(); };
  338. this._htmlAudioElement.playbackRate = this._playbackRate;
  339. }
  340. this._streamingSource.disconnect();
  341. this._streamingSource.connect(this._inputAudioNode);
  342. this._htmlAudioElement.play();
  343. }
  344. else {
  345. this._soundSource = BABYLON.Engine.audioEngine.audioContext.createBufferSource();
  346. this._soundSource.buffer = this._audioBuffer;
  347. this._soundSource.connect(this._inputAudioNode);
  348. this._soundSource.loop = this.loop;
  349. this._soundSource.playbackRate.value = this._playbackRate;
  350. this._soundSource.onended = function () { _this._onended(); };
  351. this._soundSource.start(startTime, this.isPaused ? this._startOffset % this._soundSource.buffer.duration : offset ? offset : 0);
  352. }
  353. this._startTime = startTime;
  354. this.isPlaying = true;
  355. this.isPaused = false;
  356. }
  357. catch (ex) {
  358. BABYLON.Tools.Error("Error while trying to play audio: " + this.name + ", " + ex.message);
  359. }
  360. }
  361. };
  362. Sound.prototype._onended = function () {
  363. this.isPlaying = false;
  364. if (this.onended) {
  365. this.onended();
  366. }
  367. };
  368. /**
  369. * Stop the sound
  370. * @param time (optional) Stop the sound after X seconds. Stop immediately (0) by default.
  371. */
  372. Sound.prototype.stop = function (time) {
  373. if (this.isPlaying) {
  374. if (this._streaming) {
  375. this._htmlAudioElement.pause();
  376. // Test needed for Firefox or it will generate an Invalid State Error
  377. if (this._htmlAudioElement.currentTime > 0) {
  378. this._htmlAudioElement.currentTime = 0;
  379. }
  380. }
  381. else {
  382. var stopTime = time ? BABYLON.Engine.audioEngine.audioContext.currentTime + time : BABYLON.Engine.audioEngine.audioContext.currentTime;
  383. this._soundSource.stop(stopTime);
  384. this._soundSource.onended = null;
  385. if (!this.isPaused) {
  386. this._startOffset = 0;
  387. }
  388. }
  389. this.isPlaying = false;
  390. }
  391. };
  392. Sound.prototype.pause = function () {
  393. if (this.isPlaying) {
  394. this.isPaused = true;
  395. if (this._streaming) {
  396. this._htmlAudioElement.pause();
  397. }
  398. else {
  399. this.stop(0);
  400. this._startOffset += BABYLON.Engine.audioEngine.audioContext.currentTime - this._startTime;
  401. }
  402. }
  403. };
  404. Sound.prototype.setVolume = function (newVolume, time) {
  405. if (BABYLON.Engine.audioEngine.canUseWebAudio) {
  406. if (time) {
  407. this._soundGain.gain.cancelScheduledValues(BABYLON.Engine.audioEngine.audioContext.currentTime);
  408. this._soundGain.gain.setValueAtTime(this._soundGain.gain.value, BABYLON.Engine.audioEngine.audioContext.currentTime);
  409. this._soundGain.gain.linearRampToValueAtTime(newVolume, BABYLON.Engine.audioEngine.audioContext.currentTime + time);
  410. }
  411. else {
  412. this._soundGain.gain.value = newVolume;
  413. }
  414. }
  415. this._volume = newVolume;
  416. };
  417. Sound.prototype.setPlaybackRate = function (newPlaybackRate) {
  418. this._playbackRate = newPlaybackRate;
  419. if (this.isPlaying) {
  420. if (this._streaming) {
  421. this._htmlAudioElement.playbackRate = this._playbackRate;
  422. }
  423. else {
  424. this._soundSource.playbackRate.value = this._playbackRate;
  425. }
  426. }
  427. };
  428. Sound.prototype.getVolume = function () {
  429. return this._volume;
  430. };
  431. Sound.prototype.attachToMesh = function (meshToConnectTo) {
  432. var _this = this;
  433. if (this._connectedMesh) {
  434. this._connectedMesh.unregisterAfterWorldMatrixUpdate(this._registerFunc);
  435. this._registerFunc = null;
  436. }
  437. this._connectedMesh = meshToConnectTo;
  438. if (!this.spatialSound) {
  439. this.spatialSound = true;
  440. this._createSpatialParameters();
  441. if (this.isPlaying && this.loop) {
  442. this.stop();
  443. this.play();
  444. }
  445. }
  446. this._onRegisterAfterWorldMatrixUpdate(this._connectedMesh);
  447. this._registerFunc = function (connectedMesh) { return _this._onRegisterAfterWorldMatrixUpdate(connectedMesh); };
  448. meshToConnectTo.registerAfterWorldMatrixUpdate(this._registerFunc);
  449. };
  450. Sound.prototype.detachFromMesh = function () {
  451. if (this._connectedMesh) {
  452. this._connectedMesh.unregisterAfterWorldMatrixUpdate(this._registerFunc);
  453. this._registerFunc = null;
  454. this._connectedMesh = null;
  455. }
  456. };
  457. Sound.prototype._onRegisterAfterWorldMatrixUpdate = function (connectedMesh) {
  458. this.setPosition(connectedMesh.getBoundingInfo().boundingSphere.centerWorld);
  459. if (BABYLON.Engine.audioEngine.canUseWebAudio && this._isDirectional && this.isPlaying) {
  460. this._updateDirection();
  461. }
  462. };
  463. Sound.prototype.clone = function () {
  464. var _this = this;
  465. if (!this._streaming) {
  466. var setBufferAndRun = function () {
  467. if (_this._isReadyToPlay) {
  468. clonedSound._audioBuffer = _this.getAudioBuffer();
  469. clonedSound._isReadyToPlay = true;
  470. if (clonedSound.autoplay) {
  471. clonedSound.play();
  472. }
  473. }
  474. else {
  475. window.setTimeout(setBufferAndRun, 300);
  476. }
  477. };
  478. var currentOptions = {
  479. autoplay: this.autoplay, loop: this.loop,
  480. volume: this._volume, spatialSound: this.spatialSound, maxDistance: this.maxDistance,
  481. useCustomAttenuation: this.useCustomAttenuation, rolloffFactor: this.rolloffFactor,
  482. refDistance: this.refDistance, distanceModel: this.distanceModel
  483. };
  484. var clonedSound = new Sound(this.name + "_cloned", new ArrayBuffer(0), this._scene, null, currentOptions);
  485. if (this.useCustomAttenuation) {
  486. clonedSound.setAttenuationFunction(this._customAttenuationFunction);
  487. }
  488. clonedSound.setPosition(this._position);
  489. clonedSound.setPlaybackRate(this._playbackRate);
  490. setBufferAndRun();
  491. return clonedSound;
  492. }
  493. else {
  494. return null;
  495. }
  496. };
  497. Sound.prototype.getAudioBuffer = function () {
  498. return this._audioBuffer;
  499. };
  500. Sound.prototype.serialize = function () {
  501. var serializationObject = {
  502. name: this.name,
  503. url: this.name,
  504. autoplay: this.autoplay,
  505. loop: this.loop,
  506. volume: this._volume,
  507. spatialSound: this.spatialSound,
  508. maxDistance: this.maxDistance,
  509. rolloffFactor: this.rolloffFactor,
  510. refDistance: this.refDistance,
  511. distanceModel: this.distanceModel,
  512. playbackRate: this._playbackRate,
  513. panningModel: this._panningModel,
  514. soundTrackId: this.soundTrackId
  515. };
  516. if (this.spatialSound) {
  517. if (this._connectedMesh)
  518. serializationObject.connectedMeshId = this._connectedMesh.id;
  519. serializationObject.position = this._position.asArray();
  520. serializationObject.refDistance = this.refDistance;
  521. serializationObject.distanceModel = this.distanceModel;
  522. serializationObject.isDirectional = this._isDirectional;
  523. serializationObject.localDirectionToMesh = this._localDirection.asArray();
  524. serializationObject.coneInnerAngle = this._coneInnerAngle;
  525. serializationObject.coneOuterAngle = this._coneOuterAngle;
  526. serializationObject.coneOuterGain = this._coneOuterGain;
  527. }
  528. return serializationObject;
  529. };
  530. Sound.Parse = function (parsedSound, scene, rootUrl, sourceSound) {
  531. var soundName = parsedSound.name;
  532. var soundUrl;
  533. if (parsedSound.url) {
  534. soundUrl = rootUrl + parsedSound.url;
  535. }
  536. else {
  537. soundUrl = rootUrl + soundName;
  538. }
  539. var options = {
  540. autoplay: parsedSound.autoplay, loop: parsedSound.loop, volume: parsedSound.volume,
  541. spatialSound: parsedSound.spatialSound, maxDistance: parsedSound.maxDistance,
  542. rolloffFactor: parsedSound.rolloffFactor,
  543. refDistance: parsedSound.refDistance,
  544. distanceModel: parsedSound.distanceModel,
  545. playbackRate: parsedSound.playbackRate
  546. };
  547. var newSound;
  548. if (!sourceSound) {
  549. newSound = new Sound(soundName, soundUrl, scene, function () { scene._removePendingData(newSound); }, options);
  550. scene._addPendingData(newSound);
  551. }
  552. else {
  553. var setBufferAndRun = function () {
  554. if (sourceSound._isReadyToPlay) {
  555. newSound._audioBuffer = sourceSound.getAudioBuffer();
  556. newSound._isReadyToPlay = true;
  557. if (newSound.autoplay) {
  558. newSound.play();
  559. }
  560. }
  561. else {
  562. window.setTimeout(setBufferAndRun, 300);
  563. }
  564. };
  565. newSound = new Sound(soundName, new ArrayBuffer(0), scene, null, options);
  566. setBufferAndRun();
  567. }
  568. if (parsedSound.position) {
  569. var soundPosition = BABYLON.Vector3.FromArray(parsedSound.position);
  570. newSound.setPosition(soundPosition);
  571. }
  572. if (parsedSound.isDirectional) {
  573. newSound.setDirectionalCone(parsedSound.coneInnerAngle || 360, parsedSound.coneOuterAngle || 360, parsedSound.coneOuterGain || 0);
  574. if (parsedSound.localDirectionToMesh) {
  575. var localDirectionToMesh = BABYLON.Vector3.FromArray(parsedSound.localDirectionToMesh);
  576. newSound.setLocalDirectionToMesh(localDirectionToMesh);
  577. }
  578. }
  579. if (parsedSound.connectedMeshId) {
  580. var connectedMesh = scene.getMeshByID(parsedSound.connectedMeshId);
  581. if (connectedMesh) {
  582. newSound.attachToMesh(connectedMesh);
  583. }
  584. }
  585. return newSound;
  586. };
  587. return Sound;
  588. }());
  589. BABYLON.Sound = Sound;
  590. })(BABYLON || (BABYLON = {}));