babylon.audioengine.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. module BABYLON {
  2. export class AudioEngine {
  3. public audioContext: AudioContext = null;
  4. public canUseWebAudio: boolean = false;
  5. public masterGain: GainNode;
  6. constructor() {
  7. // creating the audio context
  8. try {
  9. if (typeof AudioContext !== 'undefined') {
  10. this.audioContext = new AudioContext();
  11. this.canUseWebAudio = true;
  12. } else if (typeof webkitAudioContext !== 'undefined') {
  13. this.audioContext = new webkitAudioContext();
  14. this.canUseWebAudio = true;
  15. }
  16. } catch (e) {
  17. this.canUseWebAudio = false;
  18. }
  19. // create a global volume gain node
  20. if (this.canUseWebAudio) {
  21. this.masterGain = this.audioContext.createGain();
  22. this.masterGain.gain.value = 1;
  23. this.masterGain.connect(this.audioContext.destination);
  24. }
  25. }
  26. public getGlobalVolume(): number {
  27. if (this.canUseWebAudio) {
  28. return this.masterGain.gain.value;
  29. }
  30. else {
  31. return -1;
  32. }
  33. }
  34. public setGlobalVolume(newVolume: number) {
  35. if (this.canUseWebAudio) {
  36. this.masterGain.gain.value = newVolume;
  37. }
  38. }
  39. }
  40. }