@@ -33,6 +33,8 @@ const FFT = forwardRef(
3333 const fftBufferRef = useRef < number [ ] [ ] > ( Array . from ( { length : 16 } , ( ) => [ ] ) ) ;
3434 const [ fftData , setFftData ] = useState < number [ ] [ ] > ( Array . from ( { length : 16 } , ( ) => [ ] ) ) ;
3535 const fftSize = Math . pow ( 2 , Math . round ( Math . log2 ( currentSamplingRate / 2 ) ) ) ;
36+ const sampleupdateref = useRef < number > ( 50 ) ;
37+ sampleupdateref . current = currentSamplingRate / 10 ;
3638 const canvasRef = useRef < HTMLCanvasElement > ( null ) ;
3739 const containerRef = useRef < HTMLDivElement > ( null ) ;
3840 const { theme } = useTheme ( ) ;
@@ -54,7 +56,6 @@ const FFT = forwardRef(
5456 : 'border rounded-xl bg-gray-600 text-primary-foreground' }
5557 ` ;
5658
57-
5859 let samplesReceived = 0 ;
5960 class SmoothingFilter {
6061 private bufferSize : number ;
@@ -111,7 +112,6 @@ const FFT = forwardRef(
111112 const freqStep = currentSamplingRate / fftSize ;
112113 const startIndex = Math . max ( 1 , Math . floor ( startFreq / freqStep ) ) ;
113114 const endIndex = Math . min ( Math . floor ( endFreq / freqStep ) , magnitudes . length - 1 ) ;
114-
115115 let power = 0 ;
116116 for ( let i = startIndex ; i <= endIndex ; i ++ ) {
117117 power += magnitudes [ i ] * magnitudes [ i ] ;
@@ -161,7 +161,7 @@ const FFT = forwardRef(
161161 ) ;
162162 }
163163 } ;
164- const filter = new SmoothingFilter ( 128 , 1 ) ;
164+ const filter = new SmoothingFilter ( ( currentSamplingRate / sampleupdateref . current ) * 2 , 1 ) ;
165165
166166 useImperativeHandle (
167167 ref ,
@@ -178,7 +178,7 @@ const FFT = forwardRef(
178178 samplesReceived ++ ;
179179
180180 // Trigger FFT computation more frequently
181- if ( samplesReceived % 15 === 0 ) { // Changed from 25 to 5
181+ if ( samplesReceived % sampleupdateref . current === 0 ) { // Changed from 25 to 5
182182 const processedBuffer = fftBufferRef . current [ i ] . slice ( 0 , fftSize ) ;
183183 const floatInput = new Float32Array ( processedBuffer ) ;
184184 const fftMags = fftProcessor . computeMagnitudes ( floatInput ) ;
@@ -299,7 +299,6 @@ const FFT = forwardRef(
299299
300300 try {
301301 const wglp = new WebglPlot ( canvas ) ;
302- console . log ( "WebglPlot created:" , wglp ) ;
303302 wglp . gScaleY = Zoom ;
304303 const lineColor = theme === "dark" ? new ColorRGBA ( 1 , 2 , 2 , 1 ) : new ColorRGBA ( 0 , 0 , 0 , 1 ) ; // Adjust colors as needed
305304
@@ -308,7 +307,6 @@ const FFT = forwardRef(
308307 line . lineSpaceX ( - 1 , 2 / dataPointCountRef . current ) ;
309308 wglp . addLine ( line ) ;
310309 newWglPlots . push ( wglp ) ;
311- console . log ( newWglPlots )
312310 linesRef . current = [ line ] ;
313311 wglPlotsref . current = [ wglp ] ;
314312 } catch ( error ) {
@@ -328,8 +326,6 @@ const FFT = forwardRef(
328326
329327 const updatePlot = useCallback ( ( data : number , Zoom : number ) => {
330328 if ( ! wglPlotsref . current [ 0 ] || ! linesRef . current [ 0 ] ) {
331- console . log ( linesRef . current [ 0 ] ) ;
332- console . log ( wglPlotsref . current [ 0 ] ) ;
333329 return ;
334330 }
335331 const line = linesRef . current [ 0 ] ;
@@ -391,13 +387,8 @@ const FFT = forwardRef(
391387
392388 const xScale = ( width - leftMargin - 10 ) / displayPoints ;
393389
394- let yMax = 1 ; // Default to prevent division by zero
395- yMax = Math . max ( ...fftData [ 0 ] ) ;
396- // fftData.forEach((channelData) => {
397- // if (channelData.length > 0) {
398- // yMax = Math.max(yMax, ...channelData.slice(0, displayPoints));
399- // }
400- // });
390+ let yMax = 1 ; //Default to prevent division by zero
391+ yMax = Math . max ( ...fftData . flat ( ) ) ;
401392
402393 const yScale = ( height - bottomMargin - 10 ) / yMax ;
403394
@@ -408,8 +399,7 @@ const FFT = forwardRef(
408399 const x = leftMargin + i * xScale ;
409400 const y = height - bottomMargin - channelData [ i ] * yScale ;
410401
411- if ( i === 0 ) ctx . moveTo ( x , y ) ;
412- else ctx . lineTo ( x , y ) ;
402+ ctx . lineTo ( x , y ) ;
413403 }
414404 ctx . stroke ( ) ;
415405 } ) ;
@@ -422,7 +412,7 @@ const FFT = forwardRef(
422412 for ( let i = 0 ; i <= 5 ; i ++ ) {
423413 const labelY =
424414 height - bottomMargin - ( i / 5 ) * ( height - bottomMargin - 10 ) ;
425- ctx . fillText ( ( ( yMax * i ) / 5 ) . toFixed ( 1 ) , leftMargin - 5 , labelY ) ;
415+ ctx . fillText ( ( ( yMax * i ) / 5 ) . toFixed ( 3 ) , leftMargin - 5 , labelY ) ;
426416 }
427417
428418 ctx . textAlign = "center" ;
0 commit comments