soundTrack.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { Engine } from "../Engines/engine.js";
  2. import { EngineStore } from "../Engines/engineStore.js";
  3. /**
  4. * It could be useful to isolate your music & sounds on several tracks to better manage volume on a grouped instance of sounds.
  5. * It will be also used in a future release to apply effects on a specific track.
  6. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#using-sound-tracks
  7. */
  8. export class SoundTrack {
  9. /**
  10. * Creates a new sound track.
  11. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#using-sound-tracks
  12. * @param scene Define the scene the sound track belongs to
  13. * @param options
  14. */
  15. constructor(scene, options = {}) {
  16. /**
  17. * The unique identifier of the sound track in the scene.
  18. */
  19. this.id = -1;
  20. this._isInitialized = false;
  21. scene = scene || EngineStore.LastCreatedScene;
  22. if (!scene) {
  23. return;
  24. }
  25. this._scene = scene;
  26. this.soundCollection = [];
  27. this._options = options;
  28. if (!this._options.mainTrack && this._scene.soundTracks) {
  29. this._scene.soundTracks.push(this);
  30. this.id = this._scene.soundTracks.length - 1;
  31. }
  32. }
  33. _initializeSoundTrackAudioGraph() {
  34. if (Engine.audioEngine?.canUseWebAudio && Engine.audioEngine.audioContext) {
  35. this._outputAudioNode = Engine.audioEngine.audioContext.createGain();
  36. this._outputAudioNode.connect(Engine.audioEngine.masterGain);
  37. if (this._options) {
  38. if (this._options.volume) {
  39. this._outputAudioNode.gain.value = this._options.volume;
  40. }
  41. }
  42. this._isInitialized = true;
  43. }
  44. }
  45. /**
  46. * Release the sound track and its associated resources
  47. */
  48. dispose() {
  49. if (Engine.audioEngine && Engine.audioEngine.canUseWebAudio) {
  50. if (this._connectedAnalyser) {
  51. this._connectedAnalyser.stopDebugCanvas();
  52. }
  53. while (this.soundCollection.length) {
  54. this.soundCollection[0].dispose();
  55. }
  56. if (this._outputAudioNode) {
  57. this._outputAudioNode.disconnect();
  58. }
  59. this._outputAudioNode = null;
  60. }
  61. }
  62. /**
  63. * Adds a sound to this sound track
  64. * @param sound define the sound to add
  65. * @ignoreNaming
  66. */
  67. addSound(sound) {
  68. if (!this._isInitialized) {
  69. this._initializeSoundTrackAudioGraph();
  70. }
  71. if (Engine.audioEngine?.canUseWebAudio && this._outputAudioNode) {
  72. sound.connectToSoundTrackAudioNode(this._outputAudioNode);
  73. }
  74. if (sound.soundTrackId !== undefined) {
  75. if (sound.soundTrackId === -1) {
  76. this._scene.mainSoundTrack.removeSound(sound);
  77. }
  78. else if (this._scene.soundTracks) {
  79. this._scene.soundTracks[sound.soundTrackId].removeSound(sound);
  80. }
  81. }
  82. this.soundCollection.push(sound);
  83. sound.soundTrackId = this.id;
  84. }
  85. /**
  86. * Removes a sound to this sound track
  87. * @param sound define the sound to remove
  88. * @ignoreNaming
  89. */
  90. removeSound(sound) {
  91. const index = this.soundCollection.indexOf(sound);
  92. if (index !== -1) {
  93. this.soundCollection.splice(index, 1);
  94. }
  95. }
  96. /**
  97. * Set a global volume for the full sound track.
  98. * @param newVolume Define the new volume of the sound track
  99. */
  100. setVolume(newVolume) {
  101. if (Engine.audioEngine?.canUseWebAudio && this._outputAudioNode) {
  102. this._outputAudioNode.gain.value = newVolume;
  103. }
  104. }
  105. /**
  106. * Switch the panning model to HRTF:
  107. * Renders a stereo output of higher quality than equalpower — it uses a convolution with measured impulse responses from human subjects.
  108. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
  109. */
  110. switchPanningModelToHRTF() {
  111. if (Engine.audioEngine?.canUseWebAudio) {
  112. for (let i = 0; i < this.soundCollection.length; i++) {
  113. this.soundCollection[i].switchPanningModelToHRTF();
  114. }
  115. }
  116. }
  117. /**
  118. * Switch the panning model to Equal Power:
  119. * Represents the equal-power panning algorithm, generally regarded as simple and efficient. equalpower is the default value.
  120. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
  121. */
  122. switchPanningModelToEqualPower() {
  123. if (Engine.audioEngine?.canUseWebAudio) {
  124. for (let i = 0; i < this.soundCollection.length; i++) {
  125. this.soundCollection[i].switchPanningModelToEqualPower();
  126. }
  127. }
  128. }
  129. /**
  130. * Connect the sound track to an audio analyser allowing some amazing
  131. * synchronization between the sounds/music and your visualization (VuMeter for instance).
  132. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#using-the-analyser
  133. * @param analyser The analyser to connect to the engine
  134. */
  135. connectToAnalyser(analyser) {
  136. if (this._connectedAnalyser) {
  137. this._connectedAnalyser.stopDebugCanvas();
  138. }
  139. this._connectedAnalyser = analyser;
  140. if (Engine.audioEngine?.canUseWebAudio && this._outputAudioNode) {
  141. this._outputAudioNode.disconnect();
  142. this._connectedAnalyser.connectAudioNodes(this._outputAudioNode, Engine.audioEngine.masterGain);
  143. }
  144. }
  145. }
  146. //# sourceMappingURL=soundTrack.js.map