babylon.audioengine.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var AudioEngine = (function () {
  4. function AudioEngine() {
  5. this.audioContext = null;
  6. this.canUseWebAudio = false;
  7. try {
  8. if (typeof AudioContext !== 'undefined') {
  9. this.audioContext = new AudioContext();
  10. this.canUseWebAudio = true;
  11. } else if (typeof webkitAudioContext !== 'undefined') {
  12. this.audioContext = new webkitAudioContext();
  13. this.canUseWebAudio = true;
  14. }
  15. } catch (e) {
  16. this.canUseWebAudio = false;
  17. BABYLON.Tools.Error("Your browser doesn't support Web Audio.");
  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. AudioEngine.prototype.dispose = function () {
  27. this.canUseWebAudio = false;
  28. this.masterGain.disconnect();
  29. this.masterGain = null;
  30. this.audioContext = null;
  31. };
  32. AudioEngine.prototype.getGlobalVolume = function () {
  33. if (this.canUseWebAudio) {
  34. return this.masterGain.gain.value;
  35. } else {
  36. return -1;
  37. }
  38. };
  39. AudioEngine.prototype.setGlobalVolume = function (newVolume) {
  40. if (this.canUseWebAudio) {
  41. this.masterGain.gain.value = newVolume;
  42. }
  43. };
  44. AudioEngine.prototype.connectToAnalyser = function (analyser) {
  45. if (this.canUseWebAudio) {
  46. this.masterGain.disconnect();
  47. analyser.connectAudioNodes(this.masterGain, this.audioContext.destination);
  48. }
  49. };
  50. return AudioEngine;
  51. })();
  52. BABYLON.AudioEngine = AudioEngine;
  53. })(BABYLON || (BABYLON = {}));
  54. //# sourceMappingURL=babylon.audioengine.js.map