'use client'; import {memo,useEffect,useRef} from 'react'; import {peaks,peakRate} from '@/lib/studio/audio'; /** Browsers refuse canvases much wider than this; longer clips are drawn at this width and stretched. */ const MAX_CANVAS=16000; /** Draws the visible window of a buffer as a min/max waveform. */ function Waveform({buffer,offset,duration,width,height,color}:{buffer?:AudioBuffer;offset:number;duration:number;width:number;height:number;color:string}){ const canvas=useRef(null); useEffect(()=>{ const element=canvas.current; if(!element||!buffer||width<1)return; const scale=Math.min(window.devicePixelRatio||1,2); const w=Math.max(1,Math.min(MAX_CANVAS,Math.round(width*scale))),h=Math.max(1,Math.round(height*scale)); element.width=w;element.height=h; const draw=element.getContext('2d'); if(!draw)return; draw.clearRect(0,0,w,h); draw.fillStyle=color; const data=peaks(buffer),rate=peakRate(buffer),blocks=data.length/2,mid=h/2; for(let x=0;xhi)hi=data[i*2+1];} const top=mid-hi*mid*0.95,bottom=mid-lo*mid*0.95; draw.fillRect(x,top,1,Math.max(1,bottom-top)); } },[buffer,offset,duration,width,height,color]); return