babylon.analyser.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Analyser = (function () {
  4. function Analyser(scene) {
  5. this.SMOOTHING = 0.75;
  6. this.FFT_SIZE = 512;
  7. this.BARGRAPHAMPLITUDE = 256;
  8. this.DEBUGCANVASPOS = { x: 20, y: 20 };
  9. this.DEBUGCANVASSIZE = { width: 320, height: 200 };
  10. this._scene = scene;
  11. this._audioEngine = BABYLON.Engine.audioEngine;
  12. if (this._audioEngine.canUseWebAudio) {
  13. this._webAudioAnalyser = this._audioEngine.audioContext.createAnalyser();
  14. this._webAudioAnalyser.minDecibels = -140;
  15. this._webAudioAnalyser.maxDecibels = 0;
  16. this._byteFreqs = new Uint8Array(this._webAudioAnalyser.frequencyBinCount);
  17. this._byteTime = new Uint8Array(this._webAudioAnalyser.frequencyBinCount);
  18. this._floatFreqs = new Float32Array(this._webAudioAnalyser.frequencyBinCount);
  19. }
  20. }
  21. Analyser.prototype.getFrequencyBinCount = function () {
  22. if (this._audioEngine.canUseWebAudio) {
  23. return this._webAudioAnalyser.frequencyBinCount;
  24. }
  25. else {
  26. return 0;
  27. }
  28. };
  29. Analyser.prototype.getByteFrequencyData = function () {
  30. if (this._audioEngine.canUseWebAudio) {
  31. this._webAudioAnalyser.smoothingTimeConstant = this.SMOOTHING;
  32. this._webAudioAnalyser.fftSize = this.FFT_SIZE;
  33. this._webAudioAnalyser.getByteFrequencyData(this._byteFreqs);
  34. }
  35. return this._byteFreqs;
  36. };
  37. Analyser.prototype.getByteTimeDomainData = function () {
  38. if (this._audioEngine.canUseWebAudio) {
  39. this._webAudioAnalyser.smoothingTimeConstant = this.SMOOTHING;
  40. this._webAudioAnalyser.fftSize = this.FFT_SIZE;
  41. this._webAudioAnalyser.getByteTimeDomainData(this._byteTime);
  42. }
  43. return this._byteTime;
  44. };
  45. Analyser.prototype.getFloatFrequencyData = function () {
  46. if (this._audioEngine.canUseWebAudio) {
  47. this._webAudioAnalyser.smoothingTimeConstant = this.SMOOTHING;
  48. this._webAudioAnalyser.fftSize = this.FFT_SIZE;
  49. this._webAudioAnalyser.getFloatFrequencyData(this._floatFreqs);
  50. }
  51. return this._floatFreqs;
  52. };
  53. Analyser.prototype.drawDebugCanvas = function () {
  54. var _this = this;
  55. if (this._audioEngine.canUseWebAudio) {
  56. if (!this._debugCanvas) {
  57. this._debugCanvas = document.createElement("canvas");
  58. this._debugCanvas.width = this.DEBUGCANVASSIZE.width;
  59. this._debugCanvas.height = this.DEBUGCANVASSIZE.height;
  60. this._debugCanvas.style.position = "absolute";
  61. this._debugCanvas.style.top = this.DEBUGCANVASPOS.y + "px";
  62. this._debugCanvas.style.left = this.DEBUGCANVASPOS.x + "px";
  63. this._debugCanvasContext = this._debugCanvas.getContext("2d");
  64. document.body.appendChild(this._debugCanvas);
  65. this._registerFunc = function () {
  66. _this.drawDebugCanvas();
  67. };
  68. this._scene.registerBeforeRender(this._registerFunc);
  69. }
  70. if (this._registerFunc) {
  71. var workingArray = this.getByteFrequencyData();
  72. this._debugCanvasContext.fillStyle = 'rgb(0, 0, 0)';
  73. this._debugCanvasContext.fillRect(0, 0, this.DEBUGCANVASSIZE.width, this.DEBUGCANVASSIZE.height);
  74. // Draw the frequency domain chart.
  75. for (var i = 0; i < this.getFrequencyBinCount(); i++) {
  76. var value = workingArray[i];
  77. var percent = value / this.BARGRAPHAMPLITUDE;
  78. var height = this.DEBUGCANVASSIZE.height * percent;
  79. var offset = this.DEBUGCANVASSIZE.height - height - 1;
  80. var barWidth = this.DEBUGCANVASSIZE.width / this.getFrequencyBinCount();
  81. var hue = i / this.getFrequencyBinCount() * 360;
  82. this._debugCanvasContext.fillStyle = 'hsl(' + hue + ', 100%, 50%)';
  83. this._debugCanvasContext.fillRect(i * barWidth, offset, barWidth, height);
  84. }
  85. }
  86. }
  87. };
  88. Analyser.prototype.stopDebugCanvas = function () {
  89. if (this._debugCanvas) {
  90. this._scene.unregisterBeforeRender(this._registerFunc);
  91. this._registerFunc = null;
  92. document.body.removeChild(this._debugCanvas);
  93. this._debugCanvas = null;
  94. this._debugCanvasContext = null;
  95. }
  96. };
  97. Analyser.prototype.connectAudioNodes = function (inputAudioNode, outputAudioNode) {
  98. if (this._audioEngine.canUseWebAudio) {
  99. inputAudioNode.connect(this._webAudioAnalyser);
  100. this._webAudioAnalyser.connect(outputAudioNode);
  101. }
  102. };
  103. Analyser.prototype.dispose = function () {
  104. if (this._audioEngine.canUseWebAudio) {
  105. this._webAudioAnalyser.disconnect();
  106. }
  107. };
  108. return Analyser;
  109. })();
  110. BABYLON.Analyser = Analyser;
  111. })(BABYLON || (BABYLON = {}));
  112. //# sourceMappingURL=babylon.analyser.js.map