babylon.audioengine.js 2.7 KB

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