babylon.audioengine.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. if (this.canUseWebAudio) {
  28. if (this._connectedAnalyser) {
  29. this._connectedAnalyser.stopDebugCanvas();
  30. }
  31. this.canUseWebAudio = false;
  32. this.masterGain.disconnect();
  33. this.masterGain = null;
  34. this.audioContext = null;
  35. }
  36. };
  37. AudioEngine.prototype.getGlobalVolume = function () {
  38. if (this.canUseWebAudio) {
  39. return this.masterGain.gain.value;
  40. } else {
  41. return -1;
  42. }
  43. };
  44. AudioEngine.prototype.setGlobalVolume = function (newVolume) {
  45. if (this.canUseWebAudio) {
  46. this.masterGain.gain.value = newVolume;
  47. }
  48. };
  49. AudioEngine.prototype.connectToAnalyser = function (analyser) {
  50. if (this._connectedAnalyser) {
  51. this._connectedAnalyser.stopDebugCanvas();
  52. }
  53. this._connectedAnalyser = analyser;
  54. if (this.canUseWebAudio) {
  55. this.masterGain.disconnect();
  56. this._connectedAnalyser.connectAudioNodes(this.masterGain, this.audioContext.destination);
  57. }
  58. };
  59. return AudioEngine;
  60. })();
  61. BABYLON.AudioEngine = AudioEngine;
  62. })(BABYLON || (BABYLON = {}));
  63. //# sourceMappingURL=babylon.audioengine.js.map