babylon.soundtrack.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. module BABYLON {
  2. export class SoundTrack {
  3. private _audioEngine: BABYLON.AudioEngine;
  4. private _trackGain: GainNode;
  5. private _trackConvolver: ConvolverNode;
  6. private _scene: BABYLON.Scene;
  7. public id: number = -1;
  8. public soundCollection: Array<BABYLON.Sound>;
  9. private _isMainTrack: boolean = false;
  10. constructor(scene: BABYLON.Scene, options?: any) {
  11. this._scene = scene;
  12. this._audioEngine = scene.getEngine().getAudioEngine();
  13. this.soundCollection = new Array();
  14. if (this._audioEngine.canUseWebAudio) {
  15. this._trackGain = this._audioEngine.audioContext.createGain();
  16. //this._trackConvolver = this._audioEngine.audioContext.createConvolver();
  17. //this._trackConvolver.connect(this._trackGain);
  18. this._trackGain.connect(this._audioEngine.masterGain);
  19. if (options) {
  20. if (options.volume) { this._trackGain.gain.value = options.volume; }
  21. if (options.mainTrack) { this._isMainTrack = options.mainTrack; }
  22. }
  23. }
  24. if (!this._isMainTrack) {
  25. this._scene.soundTracks.push(this);
  26. this.id = this._scene.soundTracks.length - 1;
  27. }
  28. }
  29. public AddSound(sound: BABYLON.Sound) {
  30. sound.connectToSoundTrackAudioNode(this._trackGain);
  31. if (sound.soundTrackId) {
  32. if (sound.soundTrackId === -1) {
  33. this._scene.mainSoundTrack.RemoveSound(sound);
  34. }
  35. else {
  36. this._scene.soundTracks[sound.soundTrackId].RemoveSound(sound);
  37. }
  38. }
  39. this.soundCollection.push(sound);
  40. sound.soundTrackId = this.id;
  41. }
  42. public RemoveSound(sound: BABYLON.Sound) {
  43. var index = this.soundCollection.indexOf(sound);
  44. if (index !== -1) {
  45. this.soundCollection.splice(index, 1);
  46. }
  47. }
  48. public setVolume(newVolume: number) {
  49. if (this._audioEngine.canUseWebAudio) {
  50. this._trackGain.gain.value = newVolume;
  51. }
  52. }
  53. }
  54. }