babylon.analyser.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 = scene.getEngine().getAudioEngine();
  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. return this._webAudioAnalyser.frequencyBinCount;
  23. };
  24. Analyser.prototype.getByteFrequencyData = function () {
  25. this._webAudioAnalyser.smoothingTimeConstant = this.SMOOTHING;
  26. this._webAudioAnalyser.fftSize = this.FFT_SIZE;
  27. this._webAudioAnalyser.getByteFrequencyData(this._byteFreqs);
  28. return this._byteFreqs;
  29. };
  30. Analyser.prototype.getByteTimeDomainData = function () {
  31. this._webAudioAnalyser.smoothingTimeConstant = this.SMOOTHING;
  32. this._webAudioAnalyser.fftSize = this.FFT_SIZE;
  33. this._webAudioAnalyser.getByteTimeDomainData(this._byteTime);
  34. return this._byteTime;
  35. };
  36. Analyser.prototype.getFloatFrequencyData = function () {
  37. this._webAudioAnalyser.smoothingTimeConstant = this.SMOOTHING;
  38. this._webAudioAnalyser.fftSize = this.FFT_SIZE;
  39. this._webAudioAnalyser.getFloatFrequencyData(this._floatFreqs);
  40. return this._floatFreqs;
  41. };
  42. Analyser.prototype.drawDebugCanvas = function () {
  43. var _this = this;
  44. if (this._audioEngine.canUseWebAudio) {
  45. if (!this._debugCanvas) {
  46. this._debugCanvas = document.createElement("canvas");
  47. this._debugCanvas.width = this.DEBUGCANVASSIZE.width;
  48. this._debugCanvas.height = this.DEBUGCANVASSIZE.height;
  49. this._debugCanvas.style.position = "absolute";
  50. this._debugCanvas.style.top = this.DEBUGCANVASPOS.y + "px";
  51. this._debugCanvas.style.left = this.DEBUGCANVASPOS.x + "px";
  52. this._debugCanvasContext = this._debugCanvas.getContext("2d");
  53. document.body.appendChild(this._debugCanvas);
  54. this._registerFunc = function () {
  55. _this.drawDebugCanvas();
  56. };
  57. this._scene.registerBeforeRender(this._registerFunc);
  58. }
  59. if (this._registerFunc) {
  60. var workingArray = this.getByteFrequencyData();
  61. this._debugCanvasContext.fillStyle = 'rgb(0, 0, 0)';
  62. this._debugCanvasContext.fillRect(0, 0, this.DEBUGCANVASSIZE.width, this.DEBUGCANVASSIZE.height);
  63. for (var i = 0; i < this.getFrequencyBinCount(); i++) {
  64. var value = workingArray[i];
  65. var percent = value / this.BARGRAPHAMPLITUDE;
  66. var height = this.DEBUGCANVASSIZE.height * percent;
  67. var offset = this.DEBUGCANVASSIZE.height - height - 1;
  68. var barWidth = this.DEBUGCANVASSIZE.width / this.getFrequencyBinCount();
  69. var hue = i / this.getFrequencyBinCount() * 360;
  70. this._debugCanvasContext.fillStyle = 'hsl(' + hue + ', 100%, 50%)';
  71. this._debugCanvasContext.fillRect(i * barWidth, offset, barWidth, height);
  72. }
  73. }
  74. }
  75. };
  76. Analyser.prototype.stopDebugCanvas = function () {
  77. if (this._debugCanvas) {
  78. this._scene.unregisterBeforeRender(this._registerFunc);
  79. this._registerFunc = null;
  80. document.body.removeChild(this._debugCanvas);
  81. this._debugCanvas = null;
  82. this._debugCanvasContext = null;
  83. }
  84. };
  85. Analyser.prototype.connectAudioNodes = function (inputAudioNode, outputAudioNode) {
  86. if (this._audioEngine.canUseWebAudio) {
  87. inputAudioNode.connect(this._webAudioAnalyser);
  88. this._webAudioAnalyser.connect(outputAudioNode);
  89. }
  90. };
  91. return Analyser;
  92. })();
  93. BABYLON.Analyser = Analyser;
  94. })(BABYLON || (BABYLON = {}));
  95. //# sourceMappingURL=babylon.analyser.js.map