Files
dsp-thomann-interface/src/features/dsp408/components/GateSection/GateSection.tsx
T
2026-10-25 15:07:32 +01:00

165 lines
5.0 KiB
TypeScript

// GateSection.tsx
import VerticalSlider from '../../../../components/VerticalSlider/VerticalSlider';
import { InputChannel } from '../../types';
import { DSP408 as DSP } from '../../../../types';
import ChannelGroup from '../../../../components/ChannelGroup/ChannelGroup';
import { GateGraph } from './GateGraph';
import MeterBar from '../../../../components/MeterBar/MeterBar';
import { useState } from 'react';
import './GateSection.css';
const GATE_PARAMS = [
{ key: 'threshold', label: 'Threshold', min: -90, max: 0, step: 0.5, unit: ' dB' },
{ key: 'attack', label: 'Attack', min: 1, max: 999, step: 1, unit: ' ms' },
{ key: 'hold', label: 'Hold', min: 10, max: 999, step: 1, unit: ' ms' },
{ key: 'release', label: 'Release', min: 10, max: 3000, step: 1, unit: ' ms' },
] as const;
type GateParamKey = (typeof GATE_PARAMS)[number]['key'];
type GateValues = {
threshold: number;
attack: number;
hold: number;
release: number;
};
type SetGateFn = (
inputChannel: InputChannel,
threshold: number,
attack: number,
hold: number,
release: number
) => Promise<boolean>;
function GatePanel({
channel,
dsp,
setGate,
onLiveValue,
}: {
channel: InputChannel;
dsp: DSP;
setGate: SetGateFn;
onLiveValue: (channel: InputChannel, key: GateParamKey, value: number) => void;
}) {
const inputState = dsp.state.current_config.input_states[channel];
const gate = inputState.gate;
return (
<div className="gate-panel">
<div className="gate-panel__title">{inputState.name}</div>
<div className="gate-panel__grid">
{GATE_PARAMS.map((param) => (
<div className="gate-panel__cell" key={param.key}>
<VerticalSlider
min={param.min}
max={param.max}
step={() => param.step}
unit={param.unit}
value={gate[param.key]}
onInput={(value) => onLiveValue(channel, param.key, value)}
onChange={(value) => {
return setGate(
channel,
param.key === 'threshold' ? value : gate.threshold,
param.key === 'attack' ? value : gate.attack,
param.key === 'hold' ? value : gate.hold,
param.key === 'release' ? value : gate.release
);
}}
style={{ width: '100%', flex: '1 1 auto' }}
/>
<div className="gate-panel__cell-label">{param.label}</div>
</div>
))}
</div>
</div>
);
}
function GateSection({ dsp, set_gate }: { dsp: DSP; set_gate: SetGateFn }) {
const inputs = [InputChannel.InA, InputChannel.InB, InputChannel.InC, InputChannel.InD];
const [activeChannel, setActiveChannel] = useState<InputChannel>(inputs[0]);
const [liveGate, setLiveGate] = useState<Partial<Record<InputChannel, GateValues>>>({});
const handleLiveValue = (channel: InputChannel, key: GateParamKey, value: number) => {
setActiveChannel(channel);
setLiveGate((prev) => {
const base = prev[channel] ?? dsp.state.current_config.input_states[channel].gate;
return { ...prev, [channel]: { ...base, [key]: value } };
});
};
const handleSetGate: SetGateFn = async (channel, threshold, attack, hold, release) => {
setActiveChannel(channel);
const success = await set_gate(channel, threshold, attack, hold, release);
if (success) {
setLiveGate((prev) => {
if (!(channel in prev)) return prev;
const next = { ...prev };
delete next[channel];
return next;
});
}
return success;
};
const activeInputState = dsp.state.current_config.input_states[activeChannel];
const activeGate = liveGate[activeChannel] ?? activeInputState.gate;
return (
<>
<div className="gate-linear-graph-section">
<div className="gate-graph-panel">
<div className="gate-graph-title">Gate Response</div>
<GateGraph
threshold={activeGate.threshold}
attack={activeGate.attack}
hold={activeGate.hold}
release={activeGate.release}
/>
</div>
<div className="gate-meter-panel">
<div className="gate-graph__meters">
{inputs.map((input) => {
const inputState = dsp.state.current_config.input_states[input];
return (
<MeterBar
key={input}
label={inputState.name}
meter={dsp.input_meters.meters[input]}
variant='input'
/>
);
})}
</div>
</div>
</div>
<div className="gate-section">
<div className="gate-panels">
<ChannelGroup title="Input">
{inputs.map((input) => (
<GatePanel
key={String(input)}
channel={input}
dsp={dsp}
setGate={handleSetGate}
onLiveValue={handleLiveValue}
/>
))}
</ChannelGroup>
</div>
</div>
</>
);
}
export default GateSection;