babylon.audioengine.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var AudioEngine = (function () {
  4. function AudioEngine() {
  5. this.audioContext = null;
  6. this.canUseWebAudio = false;
  7. this.WarnedWebAudioUnsupported = false;
  8. try {
  9. if (typeof AudioContext !== 'undefined') {
  10. this.audioContext = new AudioContext();
  11. this.canUseWebAudio = true;
  12. }
  13. else if (typeof webkitAudioContext !== 'undefined') {
  14. this.audioContext = new webkitAudioContext();
  15. this.canUseWebAudio = true;
  16. }
  17. }
  18. catch (e) {
  19. this.canUseWebAudio = false;
  20. BABYLON.Tools.Error("Web Audio: " + e.message);
  21. }
  22. // create a global volume gain node
  23. if (this.canUseWebAudio) {
  24. this.masterGain = this.audioContext.createGain();
  25. this.masterGain.gain.value = 1;
  26. this.masterGain.connect(this.audioContext.destination);
  27. }
  28. }
  29. AudioEngine.prototype.dispose = function () {
  30. if (this.canUseWebAudio) {
  31. if (this._connectedAnalyser) {
  32. this._connectedAnalyser.stopDebugCanvas();
  33. this._connectedAnalyser.dispose();
  34. this.masterGain.disconnect();
  35. this.masterGain.connect(this.audioContext.destination);
  36. this._connectedAnalyser = null;
  37. }
  38. this.masterGain.gain.value = 1;
  39. }
  40. this.WarnedWebAudioUnsupported = false;
  41. };
  42. AudioEngine.prototype.getGlobalVolume = function () {
  43. if (this.canUseWebAudio) {
  44. return this.masterGain.gain.value;
  45. }
  46. else {
  47. return -1;
  48. }
  49. };
  50. AudioEngine.prototype.setGlobalVolume = function (newVolume) {
  51. if (this.canUseWebAudio) {
  52. this.masterGain.gain.value = newVolume;
  53. }
  54. };
  55. AudioEngine.prototype.connectToAnalyser = function (analyser) {
  56. if (this._connectedAnalyser) {
  57. this._connectedAnalyser.stopDebugCanvas();
  58. }
  59. this._connectedAnalyser = analyser;
  60. if (this.canUseWebAudio) {
  61. this.masterGain.disconnect();
  62. this._connectedAnalyser.connectAudioNodes(this.masterGain, this.audioContext.destination);
  63. }
  64. };
  65. return AudioEngine;
  66. })();
  67. BABYLON.AudioEngine = AudioEngine;
  68. })(BABYLON || (BABYLON = {}));
  69. //# sourceMappingURL=babylon.audioengine.js.map