babylon.soundtrack.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var SoundTrack = (function () {
  4. function SoundTrack(scene, options) {
  5. this.id = -1;
  6. this._isMainTrack = false;
  7. this._scene = scene;
  8. this._audioEngine = BABYLON.Engine.audioEngine;
  9. this.soundCollection = new Array();
  10. if (this._audioEngine.canUseWebAudio) {
  11. this._trackGain = this._audioEngine.audioContext.createGain();
  12. this._trackGain.connect(this._audioEngine.masterGain);
  13. if (options) {
  14. if (options.volume) {
  15. this._trackGain.gain.value = options.volume;
  16. }
  17. if (options.mainTrack) {
  18. this._isMainTrack = options.mainTrack;
  19. }
  20. }
  21. }
  22. if (!this._isMainTrack) {
  23. this._scene.soundTracks.push(this);
  24. this.id = this._scene.soundTracks.length - 1;
  25. }
  26. }
  27. SoundTrack.prototype.dispose = function () {
  28. if (this._audioEngine.canUseWebAudio) {
  29. if (this._connectedAnalyser) {
  30. this._connectedAnalyser.stopDebugCanvas();
  31. }
  32. while (this.soundCollection.length) {
  33. this.soundCollection[0].dispose();
  34. }
  35. this._trackGain.disconnect();
  36. this._trackGain = null;
  37. }
  38. };
  39. SoundTrack.prototype.AddSound = function (sound) {
  40. if (BABYLON.Engine.audioEngine.canUseWebAudio) {
  41. sound.connectToSoundTrackAudioNode(this._trackGain);
  42. }
  43. if (sound.soundTrackId) {
  44. if (sound.soundTrackId === -1) {
  45. this._scene.mainSoundTrack.RemoveSound(sound);
  46. }
  47. else {
  48. this._scene.soundTracks[sound.soundTrackId].RemoveSound(sound);
  49. }
  50. }
  51. this.soundCollection.push(sound);
  52. sound.soundTrackId = this.id;
  53. };
  54. SoundTrack.prototype.RemoveSound = function (sound) {
  55. var index = this.soundCollection.indexOf(sound);
  56. if (index !== -1) {
  57. this.soundCollection.splice(index, 1);
  58. }
  59. };
  60. SoundTrack.prototype.setVolume = function (newVolume) {
  61. if (this._audioEngine.canUseWebAudio) {
  62. this._trackGain.gain.value = newVolume;
  63. }
  64. };
  65. SoundTrack.prototype.switchPanningModelToHRTF = function () {
  66. if (BABYLON.Engine.audioEngine.canUseWebAudio) {
  67. for (var i = 0; i < this.soundCollection.length; i++) {
  68. this.soundCollection[i].switchPanningModelToHRTF();
  69. }
  70. }
  71. };
  72. SoundTrack.prototype.switchPanningModelToEqualPower = function () {
  73. if (BABYLON.Engine.audioEngine.canUseWebAudio) {
  74. for (var i = 0; i < this.soundCollection.length; i++) {
  75. this.soundCollection[i].switchPanningModelToEqualPower();
  76. }
  77. }
  78. };
  79. SoundTrack.prototype.connectToAnalyser = function (analyser) {
  80. if (this._connectedAnalyser) {
  81. this._connectedAnalyser.stopDebugCanvas();
  82. }
  83. this._connectedAnalyser = analyser;
  84. if (this._audioEngine.canUseWebAudio) {
  85. this._trackGain.disconnect();
  86. this._connectedAnalyser.connectAudioNodes(this._trackGain, this._audioEngine.masterGain);
  87. }
  88. };
  89. return SoundTrack;
  90. })();
  91. BABYLON.SoundTrack = SoundTrack;
  92. })(BABYLON || (BABYLON = {}));
  93. //# sourceMappingURL=babylon.soundtrack.js.map