import { memo, useCallback, useMemo } from 'react'; import { GRAPH_FREQS, GRAPH_X_MAX, GRAPH_X_MIN, GRAPH_Y_MIN, Y_AXIS_STEP } from '../../constants'; import { Curve, CurvePoint } from '../../utils/graph/curve'; import { drawCurves } from '../../utils/graph/draw'; import LinearGraph from '../../../../components/LinearGraph/LinearGraph'; // Expands [baseMin, baseMax] outward to fit whatever the curves actually // contain (e.g. gain + geq + peq stacking past the nominal graph range), // snapped to the axis step with one step of headroom so nothing touches // the plot edge. Never shrinks below the base range. function computeYAxisBounds( curves: Curve[], baseMin: number, baseMax: number, step: number ): { yMin: number; yMax: number } { let dataMax = baseMax; let dataMin = baseMin; for (const curve of curves) { for (const { y } of curve.points) { if (y > dataMax) dataMax = y; if (y < dataMin) dataMin = y; } } const yMax = Math.ceil((dataMax + step) / step) * step; const yMin = Math.floor((dataMin - step) / step) * step; return { yMin, yMax }; } // fill:false everywhere — with multiple overlapping curves the fill/fade // just muddies things; stroke-only makes each channel/stage legible. function buildCurvesForChannels( channels: T[], selected: Set, colors: Record, showStages: boolean, getCurves: (channel: T) => ChannelCurves ): Curve[] { const curves: Curve[] = []; [...channels].reverse().forEach((channel) => { if (!selected.has(channel)) return; const { geq, peqCrossover, total } = getCurves(channel); const color = colors[channel]; if (showStages) { if (geq.length > 0) { curves.push({ points: geq, style: { strokeColor: color, fill: false, dimmed: true, lineWidth: 1 }, }); } if (peqCrossover.length > 0) { curves.push({ points: peqCrossover, style: { strokeColor: color, fill: false, dimmed: true, lineWidth: 1 }, }); } } curves.push({ points: total, style: { strokeColor: color, fill: false, lineWidth: 2 } }); }); return curves; } const formatX = (v: number) => Math.round(v).toString(); export const formatY = (v: number) => { if (v === 0) return '0'; if (v > 0) return v % 12 === 0 ? `+${Math.round(v)}` : ''; return Math.abs(v) % 15 === 0 ? Math.round(v).toString() : ''; }; export type ChannelCurves = { geq: CurvePoint[]; peqCrossover: CurvePoint[]; total: CurvePoint[]; }; type ResponseGraphProps = { channels: T[]; states: Record; selected: Set; colors: Record; showStages: boolean; liveGains: Partial>; getChannelCurves: (state: S, freqs: number[], liveGain: number | undefined) => ChannelCurves; formatY: (v: number) => string; }; function ResponseGraphInner({ channels, states, selected, colors, showStages, liveGains, getChannelCurves, formatY, }: ResponseGraphProps) { const curves = useMemo( () => buildCurvesForChannels(channels, selected, colors, showStages, (ch) => getChannelCurves(states[ch], GRAPH_FREQS, liveGains[ch]) ), [channels, selected, colors, showStages, states, liveGains, getChannelCurves] ); const { yMin, yMax } = useMemo( () => computeYAxisBounds(curves, GRAPH_Y_MIN, 12, Y_AXIS_STEP), [curves] ); const draw = useCallback( ({ ctx, toPx }: any) => { drawCurves({ ctx, toPx, xMin: GRAPH_X_MIN, xMax: GRAPH_X_MAX, yMin, yMax, curves }); }, [curves, yMin, yMax] ); return ( ); } // memo() erases the generics, so cast it back. export const ResponseGraph = memo(ResponseGraphInner) as typeof ResponseGraphInner;