babylon.soundtrack.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. module BABYLON {
  2. export class SoundTrack {
  3. private _outputAudioNode: Nullable<GainNode>;
  4. private _scene: Scene;
  5. public id: number = -1;
  6. public soundCollection: Array<Sound>;
  7. private _isMainTrack: boolean = false;
  8. private _connectedAnalyser: Analyser;
  9. private _options: any;
  10. private _isInitialized = false;
  11. constructor(scene: Scene, options?: any) {
  12. this._scene = scene;
  13. this.soundCollection = new Array();
  14. this._options = options;
  15. if (!this._isMainTrack) {
  16. this._scene.soundTracks.push(this);
  17. this.id = this._scene.soundTracks.length - 1;
  18. }
  19. }
  20. private _initializeSoundTrackAudioGraph() {
  21. if (Engine.audioEngine.canUseWebAudio && Engine.audioEngine.audioContext) {
  22. this._outputAudioNode = Engine.audioEngine.audioContext.createGain();
  23. this._outputAudioNode.connect(Engine.audioEngine.masterGain);
  24. if (this._options) {
  25. if (this._options.volume) { this._outputAudioNode.gain.value = this._options.volume; }
  26. if (this._options.mainTrack) { this._isMainTrack = this._options.mainTrack; }
  27. }
  28. this._isInitialized = true;
  29. }
  30. }
  31. public dispose() {
  32. if (Engine.audioEngine.canUseWebAudio) {
  33. if (this._connectedAnalyser) {
  34. this._connectedAnalyser.stopDebugCanvas();
  35. }
  36. while (this.soundCollection.length) {
  37. this.soundCollection[0].dispose();
  38. }
  39. if (this._outputAudioNode) {
  40. this._outputAudioNode.disconnect();
  41. }
  42. this._outputAudioNode = null;
  43. }
  44. }
  45. public AddSound(sound: Sound) {
  46. if (!this._isInitialized) {
  47. this._initializeSoundTrackAudioGraph();
  48. }
  49. if (Engine.audioEngine.canUseWebAudio && this._outputAudioNode) {
  50. sound.connectToSoundTrackAudioNode(this._outputAudioNode);
  51. }
  52. if (sound.soundTrackId) {
  53. if (sound.soundTrackId === -1) {
  54. this._scene.mainSoundTrack.RemoveSound(sound);
  55. }
  56. else {
  57. this._scene.soundTracks[sound.soundTrackId].RemoveSound(sound);
  58. }
  59. }
  60. this.soundCollection.push(sound);
  61. sound.soundTrackId = this.id;
  62. }
  63. public RemoveSound(sound: Sound) {
  64. var index = this.soundCollection.indexOf(sound);
  65. if (index !== -1) {
  66. this.soundCollection.splice(index, 1);
  67. }
  68. }
  69. public setVolume(newVolume: number) {
  70. if (Engine.audioEngine.canUseWebAudio && this._outputAudioNode) {
  71. this._outputAudioNode.gain.value = newVolume;
  72. }
  73. }
  74. public switchPanningModelToHRTF() {
  75. if (Engine.audioEngine.canUseWebAudio) {
  76. for (var i = 0; i < this.soundCollection.length; i++) {
  77. this.soundCollection[i].switchPanningModelToHRTF();
  78. }
  79. }
  80. }
  81. public switchPanningModelToEqualPower() {
  82. if (Engine.audioEngine.canUseWebAudio) {
  83. for (var i = 0; i < this.soundCollection.length; i++) {
  84. this.soundCollection[i].switchPanningModelToEqualPower();
  85. }
  86. }
  87. }
  88. public connectToAnalyser(analyser: Analyser) {
  89. if (this._connectedAnalyser) {
  90. this._connectedAnalyser.stopDebugCanvas();
  91. }
  92. this._connectedAnalyser = analyser;
  93. if (Engine.audioEngine.canUseWebAudio && this._outputAudioNode) {
  94. this._outputAudioNode.disconnect();
  95. this._connectedAnalyser.connectAudioNodes(this._outputAudioNode, Engine.audioEngine.masterGain);
  96. }
  97. }
  98. }
  99. }