babylon.analyser.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if (this._audioEngine.canUseWebAudio) {
  31. return this._webAudioAnalyser.frequencyBinCount;
  32. }
  33. else {
  34. return 0;
  35. }
  36. }
  37. public getByteFrequencyData(): Uint8Array {
  38. if (this._audioEngine.canUseWebAudio) {
  39. this._webAudioAnalyser.smoothingTimeConstant = this.SMOOTHING;
  40. this._webAudioAnalyser.fftSize = this.FFT_SIZE;
  41. this._webAudioAnalyser.getByteFrequencyData(this._byteFreqs);
  42. }
  43. return this._byteFreqs;
  44. }
  45. public getByteTimeDomainData(): Uint8Array {
  46. if (this._audioEngine.canUseWebAudio) {
  47. this._webAudioAnalyser.smoothingTimeConstant = this.SMOOTHING;
  48. this._webAudioAnalyser.fftSize = this.FFT_SIZE;
  49. this._webAudioAnalyser.getByteTimeDomainData(this._byteTime);
  50. }
  51. return this._byteTime;
  52. }
  53. public getFloatFrequencyData(): Uint8Array {
  54. if (this._audioEngine.canUseWebAudio) {
  55. this._webAudioAnalyser.smoothingTimeConstant = this.SMOOTHING;
  56. this._webAudioAnalyser.fftSize = this.FFT_SIZE;
  57. this._webAudioAnalyser.getFloatFrequencyData(this._floatFreqs);
  58. }
  59. return this._floatFreqs;
  60. }
  61. public drawDebugCanvas() {
  62. if (this._audioEngine.canUseWebAudio) {
  63. if (!this._debugCanvas) {
  64. this._debugCanvas = document.createElement("canvas");
  65. this._debugCanvas.width = this.DEBUGCANVASSIZE.width;
  66. this._debugCanvas.height = this.DEBUGCANVASSIZE.height;
  67. this._debugCanvas.style.position = "absolute";
  68. this._debugCanvas.style.top = this.DEBUGCANVASPOS.y + "px" ;
  69. this._debugCanvas.style.left = this.DEBUGCANVASPOS.x + "px" ;
  70. this._debugCanvasContext = this._debugCanvas.getContext("2d");
  71. document.body.appendChild(this._debugCanvas);
  72. this._registerFunc = () => {
  73. this.drawDebugCanvas();
  74. };
  75. this._scene.registerBeforeRender(this._registerFunc);
  76. }
  77. if (this._registerFunc) {
  78. var workingArray = this.getByteFrequencyData();
  79. this._debugCanvasContext.fillStyle = 'rgb(0, 0, 0)';
  80. this._debugCanvasContext.fillRect(0, 0, this.DEBUGCANVASSIZE.width, this.DEBUGCANVASSIZE.height);
  81. // Draw the frequency domain chart.
  82. for (var i = 0; i < this.getFrequencyBinCount(); i++) {
  83. var value = workingArray[i];
  84. var percent = value / this.BARGRAPHAMPLITUDE;
  85. var height = this.DEBUGCANVASSIZE.height * percent;
  86. var offset = this.DEBUGCANVASSIZE.height - height - 1;
  87. var barWidth = this.DEBUGCANVASSIZE.width / this.getFrequencyBinCount();
  88. var hue = i / this.getFrequencyBinCount() * 360;
  89. this._debugCanvasContext.fillStyle = 'hsl(' + hue + ', 100%, 50%)';
  90. this._debugCanvasContext.fillRect(i * barWidth, offset, barWidth, height);
  91. }
  92. }
  93. }
  94. }
  95. public stopDebugCanvas() {
  96. if (this._debugCanvas) {
  97. this._scene.unregisterBeforeRender(this._registerFunc);
  98. this._registerFunc = null;
  99. document.body.removeChild(this._debugCanvas);
  100. this._debugCanvas = null;
  101. this._debugCanvasContext = null;
  102. }
  103. }
  104. public connectAudioNodes(inputAudioNode: AudioNode, outputAudioNode: AudioNode) {
  105. if (this._audioEngine.canUseWebAudio) {
  106. inputAudioNode.connect(this._webAudioAnalyser);
  107. this._webAudioAnalyser.connect(outputAudioNode);
  108. }
  109. }
  110. public dispose() {
  111. if (this._audioEngine.canUseWebAudio) {
  112. this._webAudioAnalyser.disconnect();
  113. }
  114. }
  115. }
  116. }