refactor: folders/files to group dsp logic

This commit is contained in:
2026-08-11 23:17:47 +02:00
parent d91bcaa20f
commit 34fad6f905
35 changed files with 194 additions and 337 deletions
@@ -0,0 +1,238 @@
// LimiterSection.tsx
import VerticalSlider from '../../../../components/VerticalSlider/VerticalSlider';
import { OutputChannel } from '../../types';
import { DSP408 as DSP } from '../../../../types';
import ChannelGroup from '../../../../components/ChannelGroup/ChannelGroup';
import { LinearGraph } from '../../../../components/LinearGraph/LinearGraph';
import MeterBar from '../../../../components/MeterBar/MeterBar';
import { useMemo, useState } from 'react';
import './LimiterSection.css';
const LIMITER_SLIDER_PARAMS = [
{ key: 'threshold', label: 'Threshold', min: -90, max: 20, step: 0.5, unit: ' dB' },
{ key: 'attack', label: 'Attack', min: 1, max: 999, step: 1, unit: ' ms' },
{ key: 'release', label: 'Release', min: 10, max: 3000, step: 1, unit: ' ms' },
] as const;
// ASSUMPTION: output meter indices follow the 4 input channels (0-3), i.e.
// Out1 -> 4, Out2 -> 5, etc. Same mapping as CompressorSection — adjust if
// dsp.meters.channels ordering differs.
const OUTPUT_METER_INDEX: Record<OutputChannel, number> = {
[OutputChannel.Out1]: 4,
[OutputChannel.Out2]: 5,
[OutputChannel.Out3]: 6,
[OutputChannel.Out4]: 7,
[OutputChannel.Out5]: 8,
[OutputChannel.Out6]: 9,
[OutputChannel.Out7]: 10,
[OutputChannel.Out8]: 11,
} as const;
type LimiterParamKey = 'threshold' | 'attack' | 'release';
type LimiterValues = {
threshold: number;
attack: number;
release: number;
};
type SetLimiterFn = (
outputChannel: OutputChannel,
threshold: number,
attack: number,
release: number
) => Promise<boolean>;
function LimiterPanel({
channel,
dsp,
setLimiter,
onLiveValue,
}: {
channel: OutputChannel;
dsp: DSP;
setLimiter: SetLimiterFn;
onLiveValue: (channel: OutputChannel, key: LimiterParamKey, value: number) => void;
}) {
const outputState = dsp.state.current_config.output_states[channel];
// ASSUMPTION: limiter settings live at output_states[channel].limiter
const lim = outputState.limiter as LimiterValues;
const commit = (overrides: Partial<LimiterValues>) => {
const next = { ...lim, ...overrides };
return setLimiter(channel, next.threshold, next.attack, next.release);
};
return (
<div className="limiter-panel">
<div className="limiter-panel__title">{outputState.name}</div>
<div className="limiter-panel__grid">
{LIMITER_SLIDER_PARAMS.map((param) => (
<div className={`limiter-panel__cell limiter-panel__cell--${param.key}`} key={param.key}>
<VerticalSlider
min={param.min}
max={param.max}
step={() => param.step}
unit={param.unit}
value={lim[param.key]}
onInput={(value) => onLiveValue(channel, param.key, value)}
onChange={(value) => commit({ [param.key]: value } as Partial<LimiterValues>)}
style={{ width: '100%', flex: '1 1 auto' }}
/>
<div className="limiter-panel__cell-label">{param.label}</div>
</div>
))}
</div>
</div>
);
}
// Hard-knee brickwall limiter transfer curve, in dB.
function limiterOutputDb(input: number, threshold: number): number {
return input <= threshold ? input : threshold;
}
function LimiterSection({ dsp, set_limiter }: { dsp: DSP; set_limiter: SetLimiterFn }) {
const outputs = [
OutputChannel.Out1,
OutputChannel.Out2,
OutputChannel.Out3,
OutputChannel.Out4,
OutputChannel.Out5,
OutputChannel.Out6,
OutputChannel.Out7,
OutputChannel.Out8,
];
const [activeChannel, setActiveChannel] = useState<OutputChannel>(outputs[0]);
const [liveLim, setLiveLim] = useState<Partial<Record<OutputChannel, LimiterValues>>>({});
const handleLiveValue = (channel: OutputChannel, key: LimiterParamKey, value: number) => {
setActiveChannel(channel);
setLiveLim((prev) => {
const base =
prev[channel] ?? (dsp.state.current_config.output_states[channel].limiter as LimiterValues);
return { ...prev, [channel]: { ...base, [key]: value } };
});
};
const handleSetLimiter: SetLimiterFn = async (channel, threshold, attack, release) => {
setActiveChannel(channel);
const success = await set_limiter(channel, threshold, attack, release);
if (success) {
setLiveLim((prev) => {
if (!(channel in prev)) return prev;
const next = { ...prev };
delete next[channel];
return next;
});
}
return success;
};
const activeOutputState = dsp.state.current_config.output_states[activeChannel];
const activeLim = liveLim[activeChannel] ?? (activeOutputState.limiter as LimiterValues);
const curvePoints = useMemo(() => {
const { threshold } = activeLim;
const steps = 120;
const points: [number, number][] = [];
for (let i = 0; i <= steps; i++) {
const x = -60 + (60 * i) / steps;
points.push([x, limiterOutputDb(x, threshold)]);
}
return points;
}, [activeLim.threshold]);
return (
<>
<div className="limiter-linear-graph-section">
<div className="limiter-graph-panel">
<div className="limiter-graph__title">{activeOutputState.name} - Limiter</div>
<LinearGraph
xMin={-60}
xMax={0}
yMin={-60}
yMax={0}
xLabel="in dB"
yLabel="out dB"
formatX={(v) => Math.round(v).toString()}
formatY={(v) => Math.round(v).toString()}
draw={({ ctx, toPx }) => {
// Unity reference line
ctx.strokeStyle = '#6b7280';
ctx.lineWidth = 1;
ctx.setLineDash([4, 4]);
const [ux0, uy0] = toPx(-60, -60);
const [ux1, uy1] = toPx(0, 0);
ctx.beginPath();
ctx.moveTo(ux0, uy0);
ctx.lineTo(ux1, uy1);
ctx.stroke();
ctx.setLineDash([]);
// Threshold marker
ctx.strokeStyle = '#fbbf24';
ctx.lineWidth = 1;
ctx.setLineDash([2, 3]);
const [tx0, ty0] = toPx(activeLim.threshold, -60);
const [tx1, ty1] = toPx(activeLim.threshold, 0);
ctx.beginPath();
ctx.moveTo(tx0, ty0);
ctx.lineTo(tx1, ty1);
ctx.stroke();
ctx.setLineDash([]);
// Limiter curve
ctx.strokeStyle = '#f87171';
ctx.lineWidth = 2;
ctx.lineJoin = 'round';
ctx.beginPath();
curvePoints.forEach(([x, y], i) => {
const [px, py] = toPx(x, y);
if (i === 0) ctx.moveTo(px, py);
else ctx.lineTo(px, py);
});
ctx.stroke();
}}
/>
</div>
<div className="limiter-meter-panel">
<div className="limiter-graph__meters">
{outputs.map((output) => {
const outputState = dsp.state.current_config.output_states[output];
const idx = OUTPUT_METER_INDEX[output];
return (
<MeterBar key={output} label={outputState.name} meter={dsp.meters.channels[idx]} />
);
})}
</div>
</div>
</div>
<div className="limiter-section">
<div className="limiter-panels">
<ChannelGroup title="Output">
{outputs.map((output) => (
<LimiterPanel
key={String(output)}
channel={output}
dsp={dsp}
setLimiter={handleSetLimiter}
onLiveValue={handleLiveValue}
/>
))}
</ChannelGroup>
</div>
</div>
</>
);
}
export default LimiterSection;