babylon.analyser.ts 5.0 KB

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