babylon.audioEngine.js 4.9 KB

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