babylon.analyser.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. module BABYLON {
  2. /**
  3. * Class used to work with sound analyzer using fast fourier transform (FFT)
  4. * @see http://doc.babylonjs.com/how_to/playing_sounds_and_music
  5. */
  6. export class Analyser {
  7. /**
  8. * Gets or sets the smoothing
  9. * @ignorenaming
  10. */
  11. public SMOOTHING = 0.75;
  12. /**
  13. * Gets or sets the FFT table size
  14. * @ignorenaming
  15. */
  16. public FFT_SIZE = 512;
  17. /**
  18. * Gets or sets the bar graph amplitude
  19. * @ignorenaming
  20. */
  21. public BARGRAPHAMPLITUDE = 256;
  22. /**
  23. * Gets or sets the position of the debug canvas
  24. * @ignorenaming
  25. */
  26. public DEBUGCANVASPOS = { x: 20, y: 20 };
  27. /**
  28. * Gets or sets the debug canvas size
  29. * @ignorenaming
  30. */
  31. public DEBUGCANVASSIZE = { width: 320, height: 200 }
  32. private _byteFreqs: Uint8Array;
  33. private _byteTime: Uint8Array;
  34. private _floatFreqs: Float32Array;
  35. private _webAudioAnalyser: AnalyserNode;
  36. private _debugCanvas: Nullable< HTMLCanvasElement>;
  37. private _debugCanvasContext: Nullable<CanvasRenderingContext2D>;
  38. private _scene: Scene;
  39. private _registerFunc: Nullable<() => void>;
  40. private _audioEngine: AudioEngine;
  41. /**
  42. * Creates a new analyser
  43. * @param scene defines hosting scene
  44. */
  45. constructor(scene: Scene) {
  46. this._scene = scene;
  47. this._audioEngine = Engine.audioEngine;
  48. if (this._audioEngine.canUseWebAudio && this._audioEngine.audioContext) {
  49. this._webAudioAnalyser = this._audioEngine.audioContext.createAnalyser();
  50. this._webAudioAnalyser.minDecibels = -140;
  51. this._webAudioAnalyser.maxDecibels = 0;
  52. this._byteFreqs = new Uint8Array(this._webAudioAnalyser.frequencyBinCount);
  53. this._byteTime = new Uint8Array(this._webAudioAnalyser.frequencyBinCount);
  54. this._floatFreqs = new Float32Array(this._webAudioAnalyser.frequencyBinCount);
  55. }
  56. }
  57. /**
  58. * Get the number of data values you will have to play with for the visualization
  59. * @see https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/frequencyBinCount
  60. * @returns a number
  61. */
  62. public getFrequencyBinCount(): number {
  63. if (this._audioEngine.canUseWebAudio) {
  64. return this._webAudioAnalyser.frequencyBinCount;
  65. }
  66. else {
  67. return 0;
  68. }
  69. }
  70. /**
  71. * Gets the current frequency data as a byte array
  72. * @see https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteFrequencyData
  73. * @returns a Uint8Array
  74. */
  75. public getByteFrequencyData(): Uint8Array {
  76. if (this._audioEngine.canUseWebAudio) {
  77. this._webAudioAnalyser.smoothingTimeConstant = this.SMOOTHING;
  78. this._webAudioAnalyser.fftSize = this.FFT_SIZE;
  79. this._webAudioAnalyser.getByteFrequencyData(this._byteFreqs);
  80. }
  81. return this._byteFreqs;
  82. }
  83. /**
  84. * Gets the current waveform as a byte array
  85. * @see https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteTimeDomainData
  86. * @returns a Uint8Array
  87. */
  88. public getByteTimeDomainData(): Uint8Array {
  89. if (this._audioEngine.canUseWebAudio) {
  90. this._webAudioAnalyser.smoothingTimeConstant = this.SMOOTHING;
  91. this._webAudioAnalyser.fftSize = this.FFT_SIZE;
  92. this._webAudioAnalyser.getByteTimeDomainData(this._byteTime);
  93. }
  94. return this._byteTime;
  95. }
  96. /**
  97. * Gets the current frequency data as a float array
  98. * @see https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteFrequencyData
  99. * @returns a Float32Array
  100. */
  101. public getFloatFrequencyData(): Float32Array {
  102. if (this._audioEngine.canUseWebAudio) {
  103. this._webAudioAnalyser.smoothingTimeConstant = this.SMOOTHING;
  104. this._webAudioAnalyser.fftSize = this.FFT_SIZE;
  105. this._webAudioAnalyser.getFloatFrequencyData(this._floatFreqs);
  106. }
  107. return this._floatFreqs;
  108. }
  109. /**
  110. * Renders the debug canvas
  111. */
  112. public drawDebugCanvas() {
  113. if (this._audioEngine.canUseWebAudio) {
  114. if (!this._debugCanvas) {
  115. this._debugCanvas = document.createElement("canvas");
  116. this._debugCanvas.width = this.DEBUGCANVASSIZE.width;
  117. this._debugCanvas.height = this.DEBUGCANVASSIZE.height;
  118. this._debugCanvas.style.position = "absolute";
  119. this._debugCanvas.style.top = this.DEBUGCANVASPOS.y + "px" ;
  120. this._debugCanvas.style.left = this.DEBUGCANVASPOS.x + "px" ;
  121. this._debugCanvasContext = this._debugCanvas.getContext("2d");
  122. document.body.appendChild(this._debugCanvas);
  123. this._registerFunc = () => {
  124. this.drawDebugCanvas();
  125. };
  126. this._scene.registerBeforeRender(this._registerFunc);
  127. }
  128. if (this._registerFunc && this._debugCanvasContext) {
  129. var workingArray = this.getByteFrequencyData();
  130. this._debugCanvasContext.fillStyle = 'rgb(0, 0, 0)';
  131. this._debugCanvasContext.fillRect(0, 0, this.DEBUGCANVASSIZE.width, this.DEBUGCANVASSIZE.height);
  132. // Draw the frequency domain chart.
  133. for (var i = 0; i < this.getFrequencyBinCount(); i++) {
  134. var value = workingArray[i];
  135. var percent = value / this.BARGRAPHAMPLITUDE;
  136. var height = this.DEBUGCANVASSIZE.height * percent;
  137. var offset = this.DEBUGCANVASSIZE.height - height - 1;
  138. var barWidth = this.DEBUGCANVASSIZE.width / this.getFrequencyBinCount();
  139. var hue = i / this.getFrequencyBinCount() * 360;
  140. this._debugCanvasContext.fillStyle = 'hsl(' + hue + ', 100%, 50%)';
  141. this._debugCanvasContext.fillRect(i * barWidth, offset, barWidth, height);
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Stops rendering the debug canvas and removes it
  148. */
  149. public stopDebugCanvas() {
  150. if (this._debugCanvas) {
  151. if (this._registerFunc) {
  152. this._scene.unregisterBeforeRender(this._registerFunc);
  153. this._registerFunc = null;
  154. }
  155. document.body.removeChild(this._debugCanvas);
  156. this._debugCanvas = null;
  157. this._debugCanvasContext = null;
  158. }
  159. }
  160. /**
  161. * Connects two audio nodes
  162. * @param inputAudioNode defines first node to connect
  163. * @param outputAudioNode defines second node to connect
  164. */
  165. public connectAudioNodes(inputAudioNode: AudioNode, outputAudioNode: AudioNode) {
  166. if (this._audioEngine.canUseWebAudio) {
  167. inputAudioNode.connect(this._webAudioAnalyser);
  168. this._webAudioAnalyser.connect(outputAudioNode);
  169. }
  170. }
  171. /**
  172. * Releases all associated resources
  173. */
  174. public dispose() {
  175. if (this._audioEngine.canUseWebAudio) {
  176. this._webAudioAnalyser.disconnect();
  177. }
  178. }
  179. }
  180. }