babylon.audioEngine.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var AudioEngine = (function () {
  4. function AudioEngine() {
  5. this._audioContext = null;
  6. this._audioContextInitialized = false;
  7. this.canUseWebAudio = false;
  8. this.WarnedWebAudioUnsupported = false;
  9. this.unlocked = false;
  10. if (typeof window.AudioContext !== 'undefined' || typeof window.webkitAudioContext !== 'undefined') {
  11. window.AudioContext = window.AudioContext || window.webkitAudioContext;
  12. this.canUseWebAudio = true;
  13. }
  14. if (/iPad|iPhone|iPod/.test(navigator.platform)) {
  15. this._unlockiOSaudio();
  16. }
  17. else {
  18. this.unlocked = true;
  19. }
  20. }
  21. Object.defineProperty(AudioEngine.prototype, "audioContext", {
  22. get: function () {
  23. if (!this._audioContextInitialized) {
  24. this._initializeAudioContext();
  25. }
  26. return this._audioContext;
  27. },
  28. enumerable: true,
  29. configurable: true
  30. });
  31. AudioEngine.prototype._unlockiOSaudio = function () {
  32. var _this = this;
  33. var unlockaudio = function () {
  34. var buffer = _this.audioContext.createBuffer(1, 1, 22050);
  35. var source = _this.audioContext.createBufferSource();
  36. source.buffer = buffer;
  37. source.connect(_this.audioContext.destination);
  38. source.start(0);
  39. setTimeout(function () {
  40. if ((source.playbackState === source.PLAYING_STATE || source.playbackState === source.FINISHED_STATE)) {
  41. _this.unlocked = true;
  42. window.removeEventListener('touchend', unlockaudio, false);
  43. if (_this.onAudioUnlocked) {
  44. _this.onAudioUnlocked();
  45. }
  46. }
  47. }, 0);
  48. };
  49. window.addEventListener('touchend', unlockaudio, false);
  50. };
  51. AudioEngine.prototype._initializeAudioContext = function () {
  52. try {
  53. if (this.canUseWebAudio) {
  54. this._audioContext = new AudioContext();
  55. // create a global volume gain node
  56. this.masterGain = this._audioContext.createGain();
  57. this.masterGain.gain.value = 1;
  58. this.masterGain.connect(this._audioContext.destination);
  59. this._audioContextInitialized = true;
  60. }
  61. }
  62. catch (e) {
  63. this.canUseWebAudio = false;
  64. BABYLON.Tools.Error("Web Audio: " + e.message);
  65. }
  66. };
  67. AudioEngine.prototype.dispose = function () {
  68. if (this.canUseWebAudio && this._audioContextInitialized) {
  69. if (this._connectedAnalyser) {
  70. this._connectedAnalyser.stopDebugCanvas();
  71. this._connectedAnalyser.dispose();
  72. this.masterGain.disconnect();
  73. this.masterGain.connect(this._audioContext.destination);
  74. this._connectedAnalyser = null;
  75. }
  76. this.masterGain.gain.value = 1;
  77. }
  78. this.WarnedWebAudioUnsupported = false;
  79. };
  80. AudioEngine.prototype.getGlobalVolume = function () {
  81. if (this.canUseWebAudio && this._audioContextInitialized) {
  82. return this.masterGain.gain.value;
  83. }
  84. else {
  85. return -1;
  86. }
  87. };
  88. AudioEngine.prototype.setGlobalVolume = function (newVolume) {
  89. if (this.canUseWebAudio && this._audioContextInitialized) {
  90. this.masterGain.gain.value = newVolume;
  91. }
  92. };
  93. AudioEngine.prototype.connectToAnalyser = function (analyser) {
  94. if (this._connectedAnalyser) {
  95. this._connectedAnalyser.stopDebugCanvas();
  96. }
  97. if (this.canUseWebAudio && this._audioContextInitialized) {
  98. this._connectedAnalyser = analyser;
  99. this.masterGain.disconnect();
  100. this._connectedAnalyser.connectAudioNodes(this.masterGain, this._audioContext.destination);
  101. }
  102. };
  103. return AudioEngine;
  104. }());
  105. BABYLON.AudioEngine = AudioEngine;
  106. })(BABYLON || (BABYLON = {}));