refactor: graph to use a set of points instead of taking the whole dsp oobject

This commit is contained in:
2026-08-31 20:14:07 +02:00
parent 21728b5e6d
commit 75ac349790
13 changed files with 717 additions and 823 deletions
@@ -103,3 +103,52 @@
flex: 1;
min-height: 0;
}
.gain-graph-title-row {
flex: 0 0 auto;
display: flex;
align-items: center;
justify-content: space-between;
gap: 1cqh;
padding: 1cqh 1.5cqh;
flex-wrap: wrap;
}
.gain-graph-title-row .gain-graph-title {
padding: 0;
}
.magnitude-selector {
display: flex;
flex-wrap: wrap;
gap: 0.5cqh;
}
.magnitude-selector__chip {
display: flex;
align-items: center;
gap: 0.6cqh;
font-family: var(--font-ui);
font-size: 1.8cqh;
padding: 0.4cqh 1cqh;
background: var(--bg-void);
color: var(--text-muted);
border: var(--border-width) solid var(--border-hairline);
border-radius: var(--radius-sm);
cursor: pointer;
opacity: 0.5;
}
.magnitude-selector__chip.active {
opacity: 1;
color: var(--text-primary);
border-color: var(--chip-color);
}
.magnitude-selector__swatch {
width: 1.2cqh;
height: 1.2cqh;
border-radius: 50%;
background: var(--chip-color);
flex: 0 0 auto;
}
@@ -6,7 +6,36 @@ import ChannelGroup from '../../../../components/ChannelGroup/ChannelGroup';
import { LinearGraph } from '../../../../components/LinearGraph/LinearGraph';
import './GainSection.css';
import { DiscreteFrequency } from '../../types';
import { DISCRETE_FREQUENCIES } from '../../constants';
import {
DISCRETE_FREQUENCIES,
GRAPH_X_MAX,
GRAPH_X_MIN,
GRAPH_Y_MAX,
GRAPH_Y_MIN,
INPUT_ORDER,
OUTPUT_ORDER,
} from '../../constants';
import { useState } from 'react';
// import { OverlayChannelSpec, buildInputMagnitudeOverlayDraw } from '../../utils/overlayCurve';
const CHANNEL_COLORS = [
'#4ade80',
'#60a5fa',
'#f472b6',
'#fbbf24',
'#a78bfa',
'#34d399',
'#f87171',
'#38bdf8',
];
function buildColorMap<T extends string>(channels: T[]): Record<T, string> {
const map = {} as Record<T, string>;
channels.forEach((ch, i) => {
map[ch] = CHANNEL_COLORS[i % CHANNEL_COLORS.length];
});
return map;
}
function GainPanel({
dsp,
@@ -90,6 +119,40 @@ function GainPanel({
);
}
function ChannelSelector<T extends string>({
channels,
labels,
colors,
selected,
onToggle,
}: {
channels: T[];
labels: Record<T, string>;
colors: Record<T, string>;
selected: Set<T>;
onToggle: (channel: T) => void;
}) {
return (
<div className="magnitude-selector">
{channels.map((channel) => {
const isOn = selected.has(channel);
return (
<button
key={channel}
type="button"
className={`magnitude-selector__chip${isOn ? ' active' : ''}`}
style={{ '--chip-color': colors[channel] } as React.CSSProperties}
onClick={() => onToggle(channel)}
>
<span className="magnitude-selector__swatch" />
{labels[channel]}
</button>
);
})}
</div>
);
}
function GainSection({
dsp,
setGain,
@@ -101,87 +164,81 @@ function GainSection({
setMute: (channel: Channel, muted: boolean) => Promise<boolean>;
setInverse: (channel: Channel, inverted: boolean) => Promise<boolean>;
}) {
const inputs: InputChannel[] = [
InputChannel.InA,
InputChannel.InB,
InputChannel.InC,
InputChannel.InD,
];
const outputs: { channel: Channel; title: string }[] = [
{ channel: { Output: OutputChannel.Out1 }, title: 'Out 1' },
{ channel: { Output: OutputChannel.Out2 }, title: 'Out 2' },
{ channel: { Output: OutputChannel.Out3 }, title: 'Out 3' },
{ channel: { Output: OutputChannel.Out4 }, title: 'Out 4' },
{ channel: { Output: OutputChannel.Out5 }, title: 'Out 5' },
{ channel: { Output: OutputChannel.Out6 }, title: 'Out 6' },
{ channel: { Output: OutputChannel.Out7 }, title: 'Out 7' },
{ channel: { Output: OutputChannel.Out8 }, title: 'Out 8' },
];
const currentChannel = dsp.state.current_config.input_states[InputChannel.InA];
const inputColors = buildColorMap(INPUT_ORDER);
const outputColors = buildColorMap(OUTPUT_ORDER);
const [selectedInputs, setSelectedInputs] = useState<Set<InputChannel>>(new Set(INPUT_ORDER));
const [selectedOutputs, setSelectedOutputs] = useState<Set<OutputChannel>>(new Set(OUTPUT_ORDER));
const inputLabels: Record<InputChannel, string> = INPUT_ORDER.reduce(
(acc, channel) => ({
...acc,
[channel]: dsp.state.current_config.input_states[channel].name,
}),
{} as Record<InputChannel, string>
);
const toggleInput = (channel: InputChannel) => {
setSelectedInputs((prev) => {
const next = new Set(prev);
next.has(channel) ? next.delete(channel) : next.add(channel);
return next;
});
};
const toggleOutput = (channel: OutputChannel) => {
setSelectedOutputs((prev) => {
const next = new Set(prev);
next.has(channel) ? next.delete(channel) : next.add(channel);
return next;
});
};
// const inputOverlaySpecs: OverlayChannelSpec<InputChannel>[] = INPUT_ORDER.filter((c) =>
// selectedInputs.has(c)
// ).map((channel) => ({ channel, color: inputColors[channel] }));
// const outputOverlaySpecs: OverlayChannelSpec<OutputChannel>[] = OUTPUT_ORDER.filter((c) =>
// selectedOutputs.has(c)
// ).map((channel) => ({ channel, color: outputColors[channel] }));
return (
<>
<div className="gain-linear-graph-section">
<div className="gain-graph-panel">
<div className="gain-graph-title">Frequency Response</div>
<LinearGraph
xMin={10}
xMax={30000}
<div className="gain-graph-title-row">
<div className="gain-graph-title">Input Magnitude</div>
<ChannelSelector
channels={INPUT_ORDER}
labels={inputLabels}
colors={inputColors}
selected={selectedInputs}
onToggle={toggleInput}
/>
</div>
{/* <LinearGraph
xMin={GRAPH_X_MIN}
xMax={GRAPH_X_MAX}
xScale="log"
yMin={-18}
yMax={18}
yMin={GRAPH_Y_MIN}
yMax={GRAPH_Y_MAX}
yStep={3}
xLabel="Hz"
yLabel="dB"
draw={({ ctx, toPx }) => {
const geq = currentChannel.geq;
if (geq.bypass) {
return;
}
const points = Object.entries(geq.gains)
.map(([frequency, gain]) => ({
frequency: DISCRETE_FREQUENCIES[frequency as DiscreteFrequency],
gain,
}))
.sort((a, b) => a.frequency - b.frequency)
.map(({ frequency, gain }) => {
const [x, y] = toPx(frequency, gain);
return { x, y };
});
if (points.length < 2) {
return;
}
ctx.strokeStyle = '#4ade80';
ctx.lineWidth = 2;
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.beginPath();
ctx.moveTo(points[0].x, points[0].y);
for (let i = 1; i < points.length; i++) {
const previous = points[i - 1];
const current = points[i];
const midX = (previous.x + current.x) / 2;
ctx.quadraticCurveTo(previous.x, previous.y, midX, (previous.y + current.y) / 2);
ctx.quadraticCurveTo(current.x, current.y, current.x, current.y);
}
ctx.stroke();
}}
/>
showGrid={true}
formatX={(v) => Math.round(v).toString()}
formatY={(v) => Math.round(v).toString()}
draw={buildInputMagnitudeOverlayDraw({
channels: inputOverlaySpecs,
getGains: (channel) => dsp.state.current_config.input_states[channel].geq.gains,
getBypass: (channel) => dsp.state.current_config.input_states[channel].geq.bypass,
})}
/> */}
</div>
<div className="gain-graph-panel">
{/* <div className="gain-graph-panel">
<div className="gain-graph-title">Phase Response</div>
<LinearGraph
xMin={20}
@@ -202,13 +259,13 @@ function GainSection({
ctx.stroke();
}}
/>
</div>
</div> */}
</div>
<div className="gain-section">
<div className="gain-panels">
<ChannelGroup title="Input">
{inputs.map((channel) => (
{INPUT_ORDER.map((channel) => (
<GainPanel
key={channel}
dsp={dsp}
@@ -222,12 +279,12 @@ function GainSection({
</ChannelGroup>
<ChannelGroup title="Output">
{outputs.map(({ channel, title }) => (
{OUTPUT_ORDER.map((channel) => (
<GainPanel
key={title}
key={channel}
dsp={dsp}
channel={channel}
title={title}
channel={{ Output: channel }}
title={dsp.state.current_config.output_states[channel].name}
setGain={setGain}
setMute={setMute}
setInverse={setInverse}