babylon.audioengine.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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.getGlobalVolume = function () {
  27. if (this.canUseWebAudio) {
  28. return this.masterGain.gain.value;
  29. } else {
  30. return -1;
  31. }
  32. };
  33. AudioEngine.prototype.setGlobalVolume = function (newVolume) {
  34. if (this.canUseWebAudio) {
  35. this.masterGain.gain.value = newVolume;
  36. }
  37. };
  38. AudioEngine.prototype.connectToAnalyser = function (analyser) {
  39. if (this.canUseWebAudio) {
  40. this.masterGain.disconnect();
  41. analyser.connectAudioNodes(this.masterGain, this.audioContext.destination);
  42. }
  43. };
  44. return AudioEngine;
  45. })();
  46. BABYLON.AudioEngine = AudioEngine;
  47. })(BABYLON || (BABYLON = {}));
  48. //# sourceMappingURL=babylon.audioengine.js.map