fix: dont render graphs on every poll
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
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<T extends string>(
|
||||
channels: T[],
|
||||
selected: Set<T>,
|
||||
colors: Record<T, string>,
|
||||
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 formatOutputY = (v: number) => Math.round(v).toString();
|
||||
export const formatInputY = (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<T extends string, S> = {
|
||||
channels: T[];
|
||||
states: Record<T, S>;
|
||||
selected: Set<T>;
|
||||
colors: Record<T, string>;
|
||||
showStages: boolean;
|
||||
liveGains: Partial<Record<T, number>>;
|
||||
getChannelCurves: (state: S, freqs: number[], liveGain: number | undefined) => ChannelCurves;
|
||||
formatY: (v: number) => string;
|
||||
};
|
||||
|
||||
function ResponseGraphInner<T extends string, S>({
|
||||
channels,
|
||||
states,
|
||||
selected,
|
||||
colors,
|
||||
showStages,
|
||||
liveGains,
|
||||
getChannelCurves,
|
||||
formatY,
|
||||
}: ResponseGraphProps<T, S>) {
|
||||
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 (
|
||||
<LinearGraph
|
||||
xMin={GRAPH_X_MIN}
|
||||
xMax={GRAPH_X_MAX}
|
||||
xScale="log"
|
||||
yMin={yMin}
|
||||
yMax={yMax}
|
||||
yStep={Y_AXIS_STEP}
|
||||
xLabel="Hz"
|
||||
yLabel="dB"
|
||||
showGrid={true}
|
||||
formatX={formatX}
|
||||
formatY={formatY}
|
||||
draw={draw}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// memo() erases the generics, so cast it back.
|
||||
export const ResponseGraph = memo(ResponseGraphInner) as typeof ResponseGraphInner;
|
||||
Reference in New Issue
Block a user