babylon.sound.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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
  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, null, true);
  84. }
  85. else {
  86. this._htmlAudioElement = new Audio();
  87. this._htmlAudioElement.src = urlOrArrayBuffer;
  88. this._htmlAudioElement.controls = false;
  89. this._htmlAudioElement.loop = this.loop;
  90. this._isReadyToPlay = true;
  91. document.body.appendChild(this._htmlAudioElement);
  92. // Simulating a ready to play event for consistent behavior with non streamed audio source
  93. if (this._readyToPlayCallback) {
  94. window.setTimeout(function () {
  95. _this._readyToPlayCallback();
  96. }, 1000);
  97. }
  98. if (this.autoplay) {
  99. this.play();
  100. }
  101. }
  102. }
  103. else {
  104. if (urlOrArrayBuffer instanceof ArrayBuffer) {
  105. this._soundLoaded(urlOrArrayBuffer);
  106. }
  107. else {
  108. BABYLON.Tools.Error("Parameter must be a URL to the sound or an ArrayBuffer of the sound.");
  109. }
  110. }
  111. }
  112. }
  113. else {
  114. // Adding an empty sound to avoid breaking audio calls for non Web Audio browsers
  115. this._scene.mainSoundTrack.AddSound(this);
  116. if (!BABYLON.Engine.audioEngine.WarnedWebAudioUnsupported) {
  117. BABYLON.Tools.Error("Web Audio is not supported by your browser.");
  118. BABYLON.Engine.audioEngine.WarnedWebAudioUnsupported = true;
  119. }
  120. // Simulating a ready to play event to avoid breaking code for non web audio browsers
  121. if (this._readyToPlayCallback) {
  122. window.setTimeout(function () {
  123. _this._readyToPlayCallback();
  124. }, 1000);
  125. }
  126. }
  127. }
  128. Sound.prototype.dispose = function () {
  129. if (BABYLON.Engine.audioEngine.canUseWebAudio && this._isReadyToPlay) {
  130. if (this.isPlaying) {
  131. this.stop();
  132. }
  133. this._isReadyToPlay = false;
  134. if (this.soundTrackId === -1) {
  135. this._scene.mainSoundTrack.RemoveSound(this);
  136. }
  137. else {
  138. this._scene.soundTracks[this.soundTrackId].RemoveSound(this);
  139. }
  140. if (this._soundGain) {
  141. this._soundGain.disconnect();
  142. this._soundGain = null;
  143. }
  144. if (this._soundPanner) {
  145. this._soundPanner.disconnect();
  146. this._soundPanner = null;
  147. }
  148. if (this._soundSource) {
  149. this._soundSource.disconnect();
  150. this._soundSource = null;
  151. }
  152. this._audioBuffer = null;
  153. if (this._htmlAudioElement) {
  154. this._htmlAudioElement.pause();
  155. this._htmlAudioElement.src = "";
  156. document.body.removeChild(this._htmlAudioElement);
  157. }
  158. if (this._connectedMesh) {
  159. this._connectedMesh.unregisterAfterWorldMatrixUpdate(this._registerFunc);
  160. this._connectedMesh = null;
  161. }
  162. }
  163. };
  164. Sound.prototype._soundLoaded = function (audioData) {
  165. var _this = this;
  166. this._isLoaded = true;
  167. BABYLON.Engine.audioEngine.audioContext.decodeAudioData(audioData, function (buffer) {
  168. _this._audioBuffer = buffer;
  169. _this._isReadyToPlay = true;
  170. if (_this.autoplay) {
  171. _this.play();
  172. }
  173. if (_this._readyToPlayCallback) {
  174. _this._readyToPlayCallback();
  175. }
  176. }, function () { BABYLON.Tools.Error("Error while decoding audio data for: " + _this.name); });
  177. };
  178. Sound.prototype.setAudioBuffer = function (audioBuffer) {
  179. if (BABYLON.Engine.audioEngine.canUseWebAudio) {
  180. this._audioBuffer = audioBuffer;
  181. this._isReadyToPlay = true;
  182. }
  183. };
  184. Sound.prototype.updateOptions = function (options) {
  185. if (options) {
  186. this.loop = options.loop || this.loop;
  187. this.maxDistance = options.maxDistance || this.maxDistance;
  188. this.useCustomAttenuation = options.useCustomAttenuation || this.useCustomAttenuation;
  189. this.rolloffFactor = options.rolloffFactor || this.rolloffFactor;
  190. this.refDistance = options.refDistance || this.refDistance;
  191. this.distanceModel = options.distanceModel || this.distanceModel;
  192. this._playbackRate = options.playbackRate || this._playbackRate;
  193. this._updateSpatialParameters();
  194. if (this.isPlaying) {
  195. if (this._streaming) {
  196. this._htmlAudioElement.playbackRate = this._playbackRate;
  197. }
  198. else {
  199. this._soundSource.playbackRate.value = this._playbackRate;
  200. }
  201. }
  202. }
  203. };
  204. Sound.prototype._createSpatialParameters = function () {
  205. if (BABYLON.Engine.audioEngine.canUseWebAudio) {
  206. if (this._scene.headphone) {
  207. this._panningModel = "HRTF";
  208. }
  209. this._soundPanner = BABYLON.Engine.audioEngine.audioContext.createPanner();
  210. this._updateSpatialParameters();
  211. this._soundPanner.connect(this._ouputAudioNode);
  212. this._inputAudioNode = this._soundPanner;
  213. }
  214. };
  215. Sound.prototype._updateSpatialParameters = function () {
  216. if (this.spatialSound) {
  217. if (this.useCustomAttenuation) {
  218. // Tricks to disable in a way embedded Web Audio attenuation
  219. this._soundPanner.distanceModel = "linear";
  220. this._soundPanner.maxDistance = Number.MAX_VALUE;
  221. this._soundPanner.refDistance = 1;
  222. this._soundPanner.rolloffFactor = 1;
  223. this._soundPanner.panningModel = this._panningModel;
  224. }
  225. else {
  226. this._soundPanner.distanceModel = this.distanceModel;
  227. this._soundPanner.maxDistance = this.maxDistance;
  228. this._soundPanner.refDistance = this.refDistance;
  229. this._soundPanner.rolloffFactor = this.rolloffFactor;
  230. this._soundPanner.panningModel = this._panningModel;
  231. }
  232. }
  233. };
  234. Sound.prototype.switchPanningModelToHRTF = function () {
  235. this._panningModel = "HRTF";
  236. this._switchPanningModel();
  237. };
  238. Sound.prototype.switchPanningModelToEqualPower = function () {
  239. this._panningModel = "equalpower";
  240. this._switchPanningModel();
  241. };
  242. Sound.prototype._switchPanningModel = function () {
  243. if (BABYLON.Engine.audioEngine.canUseWebAudio && this.spatialSound) {
  244. this._soundPanner.panningModel = this._panningModel;
  245. }
  246. };
  247. Sound.prototype.connectToSoundTrackAudioNode = function (soundTrackAudioNode) {
  248. if (BABYLON.Engine.audioEngine.canUseWebAudio) {
  249. if (this._isOutputConnected) {
  250. this._ouputAudioNode.disconnect();
  251. }
  252. this._ouputAudioNode.connect(soundTrackAudioNode);
  253. this._isOutputConnected = true;
  254. }
  255. };
  256. /**
  257. * Transform this sound into a directional source
  258. * @param coneInnerAngle Size of the inner cone in degree
  259. * @param coneOuterAngle Size of the outer cone in degree
  260. * @param coneOuterGain Volume of the sound outside the outer cone (between 0.0 and 1.0)
  261. */
  262. Sound.prototype.setDirectionalCone = function (coneInnerAngle, coneOuterAngle, coneOuterGain) {
  263. if (coneOuterAngle < coneInnerAngle) {
  264. BABYLON.Tools.Error("setDirectionalCone(): outer angle of the cone must be superior or equal to the inner angle.");
  265. return;
  266. }
  267. this._coneInnerAngle = coneInnerAngle;
  268. this._coneOuterAngle = coneOuterAngle;
  269. this._coneOuterGain = coneOuterGain;
  270. this._isDirectional = true;
  271. if (this.isPlaying && this.loop) {
  272. this.stop();
  273. this.play();
  274. }
  275. };
  276. Sound.prototype.setPosition = function (newPosition) {
  277. this._position = newPosition;
  278. if (BABYLON.Engine.audioEngine.canUseWebAudio && this.spatialSound) {
  279. this._soundPanner.setPosition(this._position.x, this._position.y, this._position.z);
  280. }
  281. };
  282. Sound.prototype.setLocalDirectionToMesh = function (newLocalDirection) {
  283. this._localDirection = newLocalDirection;
  284. if (BABYLON.Engine.audioEngine.canUseWebAudio && this._connectedMesh && this.isPlaying) {
  285. this._updateDirection();
  286. }
  287. };
  288. Sound.prototype._updateDirection = function () {
  289. var mat = this._connectedMesh.getWorldMatrix();
  290. var direction = BABYLON.Vector3.TransformNormal(this._localDirection, mat);
  291. direction.normalize();
  292. this._soundPanner.setOrientation(direction.x, direction.y, direction.z);
  293. };
  294. Sound.prototype.updateDistanceFromListener = function () {
  295. if (BABYLON.Engine.audioEngine.canUseWebAudio && this._connectedMesh && this.useCustomAttenuation) {
  296. var distance = this._connectedMesh.getDistanceToCamera(this._scene.activeCamera);
  297. this._soundGain.gain.value = this._customAttenuationFunction(this._volume, distance, this.maxDistance, this.refDistance, this.rolloffFactor);
  298. }
  299. };
  300. Sound.prototype.setAttenuationFunction = function (callback) {
  301. this._customAttenuationFunction = callback;
  302. };
  303. /**
  304. * Play the sound
  305. * @param time (optional) Start the sound after X seconds. Start immediately (0) by default.
  306. */
  307. Sound.prototype.play = function (time) {
  308. var _this = this;
  309. if (this._isReadyToPlay && this._scene.audioEnabled) {
  310. try {
  311. var startTime = time ? BABYLON.Engine.audioEngine.audioContext.currentTime + time : BABYLON.Engine.audioEngine.audioContext.currentTime;
  312. if (!this._soundSource || !this._streamingSource) {
  313. if (this.spatialSound) {
  314. this._soundPanner.setPosition(this._position.x, this._position.y, this._position.z);
  315. if (this._isDirectional) {
  316. this._soundPanner.coneInnerAngle = this._coneInnerAngle;
  317. this._soundPanner.coneOuterAngle = this._coneOuterAngle;
  318. this._soundPanner.coneOuterGain = this._coneOuterGain;
  319. if (this._connectedMesh) {
  320. this._updateDirection();
  321. }
  322. else {
  323. this._soundPanner.setOrientation(this._localDirection.x, this._localDirection.y, this._localDirection.z);
  324. }
  325. }
  326. }
  327. }
  328. if (this._streaming) {
  329. if (!this._streamingSource) {
  330. this._streamingSource = BABYLON.Engine.audioEngine.audioContext.createMediaElementSource(this._htmlAudioElement);
  331. this._htmlAudioElement.onended = function () { _this._onended(); };
  332. this._htmlAudioElement.playbackRate = this._playbackRate;
  333. }
  334. this._streamingSource.disconnect();
  335. this._streamingSource.connect(this._inputAudioNode);
  336. this._htmlAudioElement.play();
  337. }
  338. else {
  339. this._soundSource = BABYLON.Engine.audioEngine.audioContext.createBufferSource();
  340. this._soundSource.buffer = this._audioBuffer;
  341. this._soundSource.connect(this._inputAudioNode);
  342. this._soundSource.loop = this.loop;
  343. this._soundSource.playbackRate.value = this._playbackRate;
  344. this._soundSource.onended = function () { _this._onended(); };
  345. this._soundSource.start(this._startTime, this.isPaused ? this._startOffset % this._soundSource.buffer.duration : 0);
  346. }
  347. this._startTime = startTime;
  348. this.isPlaying = true;
  349. this.isPaused = false;
  350. }
  351. catch (ex) {
  352. BABYLON.Tools.Error("Error while trying to play audio: " + this.name + ", " + ex.message);
  353. }
  354. }
  355. };
  356. Sound.prototype._onended = function () {
  357. this.isPlaying = false;
  358. if (this.onended) {
  359. this.onended();
  360. }
  361. };
  362. /**
  363. * Stop the sound
  364. * @param time (optional) Stop the sound after X seconds. Stop immediately (0) by default.
  365. */
  366. Sound.prototype.stop = function (time) {
  367. if (this.isPlaying) {
  368. if (this._streaming) {
  369. this._htmlAudioElement.pause();
  370. // Test needed for Firefox or it will generate an Invalid State Error
  371. if (this._htmlAudioElement.currentTime > 0) {
  372. this._htmlAudioElement.currentTime = 0;
  373. }
  374. }
  375. else {
  376. var stopTime = time ? BABYLON.Engine.audioEngine.audioContext.currentTime + time : BABYLON.Engine.audioEngine.audioContext.currentTime;
  377. this._soundSource.stop(stopTime);
  378. }
  379. this.isPlaying = false;
  380. }
  381. };
  382. Sound.prototype.pause = function () {
  383. if (this.isPlaying) {
  384. if (this._streaming) {
  385. this._htmlAudioElement.pause();
  386. }
  387. else {
  388. this.stop(0);
  389. this._startOffset += BABYLON.Engine.audioEngine.audioContext.currentTime - this._startTime;
  390. }
  391. this.isPaused = true;
  392. }
  393. };
  394. Sound.prototype.setVolume = function (newVolume, time) {
  395. if (BABYLON.Engine.audioEngine.canUseWebAudio) {
  396. if (time) {
  397. this._soundGain.gain.linearRampToValueAtTime(this._volume, BABYLON.Engine.audioEngine.audioContext.currentTime);
  398. this._soundGain.gain.linearRampToValueAtTime(newVolume, time);
  399. }
  400. else {
  401. this._soundGain.gain.value = newVolume;
  402. }
  403. }
  404. this._volume = newVolume;
  405. };
  406. Sound.prototype.setPlaybackRate = function (newPlaybackRate) {
  407. this._playbackRate = newPlaybackRate;
  408. if (this.isPlaying) {
  409. if (this._streaming) {
  410. this._htmlAudioElement.playbackRate = this._playbackRate;
  411. }
  412. else {
  413. this._soundSource.playbackRate.value = this._playbackRate;
  414. }
  415. }
  416. };
  417. Sound.prototype.getVolume = function () {
  418. return this._volume;
  419. };
  420. Sound.prototype.attachToMesh = function (meshToConnectTo) {
  421. var _this = this;
  422. this._connectedMesh = meshToConnectTo;
  423. if (!this.spatialSound) {
  424. this.spatialSound = true;
  425. this._createSpatialParameters();
  426. if (this.isPlaying && this.loop) {
  427. this.stop();
  428. this.play();
  429. }
  430. }
  431. this._onRegisterAfterWorldMatrixUpdate(this._connectedMesh);
  432. this._registerFunc = function (connectedMesh) { return _this._onRegisterAfterWorldMatrixUpdate(connectedMesh); };
  433. meshToConnectTo.registerAfterWorldMatrixUpdate(this._registerFunc);
  434. };
  435. Sound.prototype._onRegisterAfterWorldMatrixUpdate = function (connectedMesh) {
  436. this.setPosition(connectedMesh.getBoundingInfo().boundingSphere.centerWorld);
  437. if (BABYLON.Engine.audioEngine.canUseWebAudio && this._isDirectional && this.isPlaying) {
  438. this._updateDirection();
  439. }
  440. };
  441. return Sound;
  442. })();
  443. BABYLON.Sound = Sound;
  444. })(BABYLON || (BABYLON = {}));