123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import { Engine } from "../Engines/engine.js";
- import { EngineStore } from "../Engines/engineStore.js";
- /**
- * It could be useful to isolate your music & sounds on several tracks to better manage volume on a grouped instance of sounds.
- * It will be also used in a future release to apply effects on a specific track.
- * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#using-sound-tracks
- */
- export class SoundTrack {
- /**
- * Creates a new sound track.
- * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#using-sound-tracks
- * @param scene Define the scene the sound track belongs to
- * @param options
- */
- constructor(scene, options = {}) {
- /**
- * The unique identifier of the sound track in the scene.
- */
- this.id = -1;
- this._isInitialized = false;
- scene = scene || EngineStore.LastCreatedScene;
- if (!scene) {
- return;
- }
- this._scene = scene;
- this.soundCollection = [];
- this._options = options;
- if (!this._options.mainTrack && this._scene.soundTracks) {
- this._scene.soundTracks.push(this);
- this.id = this._scene.soundTracks.length - 1;
- }
- }
- _initializeSoundTrackAudioGraph() {
- if (Engine.audioEngine?.canUseWebAudio && Engine.audioEngine.audioContext) {
- this._outputAudioNode = Engine.audioEngine.audioContext.createGain();
- this._outputAudioNode.connect(Engine.audioEngine.masterGain);
- if (this._options) {
- if (this._options.volume) {
- this._outputAudioNode.gain.value = this._options.volume;
- }
- }
- this._isInitialized = true;
- }
- }
- /**
- * Release the sound track and its associated resources
- */
- dispose() {
- if (Engine.audioEngine && Engine.audioEngine.canUseWebAudio) {
- if (this._connectedAnalyser) {
- this._connectedAnalyser.stopDebugCanvas();
- }
- while (this.soundCollection.length) {
- this.soundCollection[0].dispose();
- }
- if (this._outputAudioNode) {
- this._outputAudioNode.disconnect();
- }
- this._outputAudioNode = null;
- }
- }
- /**
- * Adds a sound to this sound track
- * @param sound define the sound to add
- * @ignoreNaming
- */
- addSound(sound) {
- if (!this._isInitialized) {
- this._initializeSoundTrackAudioGraph();
- }
- if (Engine.audioEngine?.canUseWebAudio && this._outputAudioNode) {
- sound.connectToSoundTrackAudioNode(this._outputAudioNode);
- }
- if (sound.soundTrackId !== undefined) {
- if (sound.soundTrackId === -1) {
- this._scene.mainSoundTrack.removeSound(sound);
- }
- else if (this._scene.soundTracks) {
- this._scene.soundTracks[sound.soundTrackId].removeSound(sound);
- }
- }
- this.soundCollection.push(sound);
- sound.soundTrackId = this.id;
- }
- /**
- * Removes a sound to this sound track
- * @param sound define the sound to remove
- * @ignoreNaming
- */
- removeSound(sound) {
- const index = this.soundCollection.indexOf(sound);
- if (index !== -1) {
- this.soundCollection.splice(index, 1);
- }
- }
- /**
- * Set a global volume for the full sound track.
- * @param newVolume Define the new volume of the sound track
- */
- setVolume(newVolume) {
- if (Engine.audioEngine?.canUseWebAudio && this._outputAudioNode) {
- this._outputAudioNode.gain.value = newVolume;
- }
- }
- /**
- * Switch the panning model to HRTF:
- * Renders a stereo output of higher quality than equalpower — it uses a convolution with measured impulse responses from human subjects.
- * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
- */
- switchPanningModelToHRTF() {
- if (Engine.audioEngine?.canUseWebAudio) {
- for (let i = 0; i < this.soundCollection.length; i++) {
- this.soundCollection[i].switchPanningModelToHRTF();
- }
- }
- }
- /**
- * Switch the panning model to Equal Power:
- * Represents the equal-power panning algorithm, generally regarded as simple and efficient. equalpower is the default value.
- * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
- */
- switchPanningModelToEqualPower() {
- if (Engine.audioEngine?.canUseWebAudio) {
- for (let i = 0; i < this.soundCollection.length; i++) {
- this.soundCollection[i].switchPanningModelToEqualPower();
- }
- }
- }
- /**
- * Connect the sound track to an audio analyser allowing some amazing
- * synchronization between the sounds/music and your visualization (VuMeter for instance).
- * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#using-the-analyser
- * @param analyser The analyser to connect to the engine
- */
- connectToAnalyser(analyser) {
- if (this._connectedAnalyser) {
- this._connectedAnalyser.stopDebugCanvas();
- }
- this._connectedAnalyser = analyser;
- if (Engine.audioEngine?.canUseWebAudio && this._outputAudioNode) {
- this._outputAudioNode.disconnect();
- this._connectedAnalyser.connectAudioNodes(this._outputAudioNode, Engine.audioEngine.masterGain);
- }
- }
- }
- //# sourceMappingURL=soundTrack.js.map
|