Files
dsp-thomann-interface/src/features/dsp408/components/LimiterSection/LimiterSection.tsx
T

198 lines
6.4 KiB
TypeScript

// 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 { LimiterGraph } from './LimiterGraph';
import MeterBar from '../../../../components/MeterBar/MeterBar';
import { useState } from 'react';
import './LimiterSection.css';
const LIMITER_SLIDER_PARAMS = [
{ key: 'threshold', label: 'Threshold', short: 'Thr', min: -90, max: 20, step: 0.5, unit: ' dB' },
{ key: 'attack', label: 'Attack', short: 'Atk', min: 1, max: 999, step: 1, unit: ' ms' },
{ key: 'release', label: 'Release', short: 'Rel', min: 10, max: 3000, step: 1, unit: ' ms' },
] 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>;
// Phone only: one view at a time (CSS hides the tabs on desktop)
type LimiterView = 'controls' | 'response';
const LIMITER_VIEWS: { id: LimiterView; label: string }[] = [
{ id: 'controls', label: 'Controls' },
{ id: 'response', label: 'Limiter response' },
];
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">
<span className="limiter-panel__label-long">{param.label}</span>
<span className="limiter-panel__label-short">{param.short}</span>
</div>
</div>
))}
</div>
</div>
);
}
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 [view, setView] = useState<LimiterView>('controls');
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);
return (
<div className="limiter-page" data-view={view}>
{/* Phone only (hidden on desktop by CSS): one view at a time */}
<div className="limiter-tabs" role="tablist" aria-label="Limiter views">
{LIMITER_VIEWS.map((v) => (
<button
key={v.id}
type="button"
role="tab"
aria-selected={view === v.id}
className="limiter-tabs__tab"
onClick={() => setView(v.id)}
>
{v.label}
</button>
))}
</div>
{/* Portrait phone only (CSS): the graph needs the width of landscape */}
<div className="limiter-rotate-hint">
Rotate your phone to landscape to see the limiter graph.
</div>
<div className="limiter-linear-graph-section">
<div className="limiter-graph-panel">
<div className="limiter-graph__title">{activeOutputState.name} - Limiter</div>
<LimiterGraph threshold={activeLim.threshold} />
</div>
<div className="limiter-meter-panel">
<div className="limiter-graph__meters">
{outputs.map((output) => {
const outputState = dsp.state.current_config.output_states[output];
return (
<MeterBar
key={output}
label={outputState.name}
meter={dsp.output_meters.meters[output]}
variant="output"
/>
);
})}
</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>
</div>
);
}
export default LimiterSection;