415 lines
12 KiB
TypeScript
415 lines
12 KiB
TypeScript
import VerticalSlider from '../../../../components/VerticalSlider/VerticalSlider';
|
|
import { InputChannel, Channel, OutputChannel, CrossoverFilter, PEQBand, PEQ } from '../../types';
|
|
import { DSP408 as DSP } from '../../../../types';
|
|
import Button from '../../../../components/Button/Button';
|
|
import ChannelGroup from '../../../../components/ChannelGroup/ChannelGroup';
|
|
import './GainSection.css';
|
|
import {
|
|
GEQ_BANDS_LABEL,
|
|
INPUT_ORDER,
|
|
OUTPUT_ORDER,
|
|
GEQ_CURVE_Q,
|
|
INPUT_COLORS,
|
|
OUTPUT_COLORS,
|
|
} from '../../constants';
|
|
import { useState } from 'react';
|
|
import { CurvePoint, sumCurvePoints } from '../../utils/graph/curve';
|
|
import { getGeqCurvePoints } from '../../utils/graph/geq';
|
|
import { getPeqCurvePoints } from '../../utils/graph/peq';
|
|
import { getCrossoverCurvePoints } from '../../utils/graph/crossover';
|
|
import { ChannelCurves, formatY, ResponseGraph } from './GainGraph';
|
|
|
|
// ---- Curve construction -----------------------------------------------
|
|
|
|
function getGainCurvePoints(freqs: number[], gainDb: number): CurvePoint[] {
|
|
return freqs.map((x) => ({ x, y: gainDb }));
|
|
}
|
|
|
|
function getPeqCrossoverPoints(
|
|
freqs: number[],
|
|
peqChain: {
|
|
bypass: boolean;
|
|
bands: Record<PEQBand, PEQ>;
|
|
},
|
|
crossover: {
|
|
high_pass: { frequency: number; slope: CrossoverFilter };
|
|
low_pass: { frequency: number; slope: CrossoverFilter };
|
|
}
|
|
) {
|
|
return sumCurvePoints([
|
|
...(peqChain.bypass
|
|
? []
|
|
: [
|
|
getPeqCurvePoints({
|
|
freqs,
|
|
bands: peqChain.bands,
|
|
}),
|
|
]),
|
|
getCrossoverCurvePoints({
|
|
freqs,
|
|
highPass: {
|
|
frequency: crossover.high_pass.frequency,
|
|
filter_type: crossover.high_pass.slope,
|
|
},
|
|
lowPass: {
|
|
frequency: crossover.low_pass.frequency,
|
|
filter_type: crossover.low_pass.slope,
|
|
},
|
|
}),
|
|
]);
|
|
}
|
|
|
|
function getInputChannelCurves(
|
|
state: DSP['state']['current_config']['input_states'][InputChannel],
|
|
freqs: number[],
|
|
liveGain: number | undefined
|
|
): ChannelCurves {
|
|
const gain = liveGain ?? state.gain;
|
|
|
|
const geqValues = GEQ_BANDS_LABEL.map(({ frequency }) => state.geq.gains[frequency] ?? 0);
|
|
|
|
const geq = state.geq.bypass
|
|
? []
|
|
: getGeqCurvePoints({
|
|
freqs,
|
|
values: geqValues,
|
|
q: GEQ_CURVE_Q,
|
|
});
|
|
|
|
const peqCrossover = getPeqCrossoverPoints(freqs, state.peq_chain, state.crossover);
|
|
|
|
const total = sumCurvePoints([geq, peqCrossover, getGainCurvePoints(freqs, gain)]);
|
|
|
|
return { geq, peqCrossover, total };
|
|
}
|
|
|
|
function getOutputChannelCurves(
|
|
state: DSP['state']['current_config']['output_states'][OutputChannel],
|
|
freqs: number[],
|
|
liveGain: number | undefined
|
|
): ChannelCurves {
|
|
const gain = liveGain ?? state.gain;
|
|
const peqCrossover = getPeqCrossoverPoints(freqs, state.peq_chain, state.crossover);
|
|
const total = sumCurvePoints([peqCrossover, getGainCurvePoints(freqs, gain)]);
|
|
|
|
return { geq: [], peqCrossover, total };
|
|
}
|
|
|
|
// ---- Panels -------------------------------------------------------------
|
|
|
|
function GainPanel({
|
|
dsp,
|
|
channel,
|
|
title,
|
|
setGain,
|
|
setMute,
|
|
setInverse,
|
|
onLiveGain,
|
|
}: {
|
|
dsp: DSP;
|
|
channel: Channel;
|
|
title: string;
|
|
setGain: (channel: Channel, gain: number) => Promise<boolean>;
|
|
setMute: (channel: Channel, muted: boolean) => Promise<boolean>;
|
|
setInverse: (channel: Channel, inverted: boolean) => Promise<boolean>;
|
|
onLiveGain: (value: number | null) => void;
|
|
}) {
|
|
const channelState =
|
|
'Input' in channel
|
|
? dsp.state?.current_config.input_states[channel.Input]
|
|
: dsp.state?.current_config.output_states[channel.Output];
|
|
|
|
const muted = channelState.mute;
|
|
const inverted = channelState.phase_inverted;
|
|
const gain = channelState.gain;
|
|
|
|
const toggleMute = async () => {
|
|
await setMute(channel, !muted);
|
|
};
|
|
|
|
const toggleInverse = async () => {
|
|
await setInverse(channel, !inverted);
|
|
};
|
|
|
|
const handleLiveValue = (value: number) => {
|
|
onLiveGain(value);
|
|
};
|
|
|
|
const commitGain = async (value: number) => {
|
|
const success = await setGain(channel, value);
|
|
if (success) {
|
|
onLiveGain(null);
|
|
}
|
|
return success;
|
|
};
|
|
|
|
return (
|
|
<div className="gain-panel">
|
|
<div className="gain-panel__title">{title}</div>
|
|
|
|
<VerticalSlider
|
|
min={-60}
|
|
max={12}
|
|
step={(v) => (v >= -20 ? 0.1 : 0.5)}
|
|
unit=" dB"
|
|
value={gain}
|
|
formatValue={(v, d) => (v > 0 ? `+${v.toFixed(d)}` : v.toFixed(d))}
|
|
onInput={handleLiveValue}
|
|
onChange={commitGain}
|
|
/>
|
|
|
|
<Button
|
|
variant="toggle"
|
|
className={muted ? 'active-danger' : ''}
|
|
onClick={toggleMute}
|
|
style={{
|
|
width: '85%',
|
|
paddingLeft: 0,
|
|
paddingRight: 0,
|
|
fontSize: '4cqh',
|
|
paddingTop: '2cqh',
|
|
paddingBottom: '2cqh',
|
|
minHeight: 0,
|
|
}}
|
|
>
|
|
{muted ? 'Muted' : 'Mute '}
|
|
</Button>
|
|
<Button
|
|
variant="toggle"
|
|
className={inverted ? 'active-warning' : ''}
|
|
onClick={toggleInverse}
|
|
style={{
|
|
width: '85%',
|
|
paddingLeft: 0,
|
|
paddingRight: 0,
|
|
fontSize: '4cqh',
|
|
paddingTop: '2cqh',
|
|
paddingBottom: '2cqh',
|
|
minHeight: 0,
|
|
}}
|
|
>
|
|
{inverted ? 'Inverted' : 'Invert'}
|
|
</Button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
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)}
|
|
title={labels[channel]}
|
|
>
|
|
<span className="magnitude-selector__swatch" />
|
|
<span className="magnitude-selector__label">{labels[channel]}</span>
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function GainSection({
|
|
dsp,
|
|
setGain,
|
|
setMute,
|
|
setInverse,
|
|
}: {
|
|
dsp: DSP;
|
|
setGain: (channel: Channel, gain: number) => Promise<boolean>;
|
|
setMute: (channel: Channel, muted: boolean) => Promise<boolean>;
|
|
setInverse: (channel: Channel, inverted: boolean) => Promise<boolean>;
|
|
}) {
|
|
const [selectedInputs, setSelectedInputs] = useState<Set<InputChannel>>(new Set(INPUT_ORDER));
|
|
const [selectedOutputs, setSelectedOutputs] = useState<Set<OutputChannel>>(new Set(OUTPUT_ORDER));
|
|
|
|
const [showInputStages, setShowInputStages] = useState(false);
|
|
const [showOutputStages, setShowOutputStages] = useState(false);
|
|
|
|
const [liveInputGains, setLiveInputGains] = useState<Partial<Record<InputChannel, number>>>({});
|
|
const [liveOutputGains, setLiveOutputGains] = useState<Partial<Record<OutputChannel, number>>>(
|
|
{}
|
|
);
|
|
|
|
const setLiveInputGain = (channel: InputChannel, value: number | null) => {
|
|
setLiveInputGains((prev) => {
|
|
if (value === null) {
|
|
const next = { ...prev };
|
|
delete next[channel];
|
|
return next;
|
|
}
|
|
return { ...prev, [channel]: value };
|
|
});
|
|
};
|
|
|
|
const setLiveOutputGain = (channel: OutputChannel, value: number | null) => {
|
|
setLiveOutputGains((prev) => {
|
|
if (value === null) {
|
|
const next = { ...prev };
|
|
delete next[channel];
|
|
return next;
|
|
}
|
|
return { ...prev, [channel]: value };
|
|
});
|
|
};
|
|
|
|
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 outputLabels: Record<OutputChannel, string> = OUTPUT_ORDER.reduce(
|
|
(acc, channel) => ({
|
|
...acc,
|
|
[channel]: dsp.state.current_config.output_states[channel].name,
|
|
}),
|
|
{} as Record<OutputChannel, 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;
|
|
});
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div className="gain-linear-graph-section">
|
|
<div className="gain-graph-panel">
|
|
<div className="gain-graph-title-row">
|
|
<div className="gain-graph-title-line">
|
|
<div className="gain-graph-title">Input Response</div>
|
|
<Button
|
|
variant="toggle"
|
|
className={`gain-stages-toggle ${showInputStages ? 'active-info' : ''}`}
|
|
onClick={() => setShowInputStages((v) => !v)}
|
|
>
|
|
Show stages
|
|
</Button>
|
|
</div>
|
|
<ChannelSelector
|
|
channels={INPUT_ORDER}
|
|
labels={inputLabels}
|
|
colors={INPUT_COLORS}
|
|
selected={selectedInputs}
|
|
onToggle={toggleInput}
|
|
/>
|
|
</div>
|
|
<ResponseGraph
|
|
channels={INPUT_ORDER}
|
|
states={dsp.state.current_config.input_states}
|
|
selected={selectedInputs}
|
|
colors={INPUT_COLORS}
|
|
showStages={showInputStages}
|
|
liveGains={liveInputGains}
|
|
getChannelCurves={getInputChannelCurves}
|
|
formatY={formatY}
|
|
/>
|
|
</div>
|
|
|
|
<div className="gain-graph-panel">
|
|
<div className="gain-graph-title-row">
|
|
<div className="gain-graph-title-line">
|
|
<div className="gain-graph-title">Output Response</div>
|
|
<Button
|
|
variant={showOutputStages ? 'outline' : 'ghost'}
|
|
className="gain-stages-toggle"
|
|
onClick={() => setShowOutputStages((v) => !v)}
|
|
>
|
|
Show stages
|
|
</Button>
|
|
</div>
|
|
|
|
<ChannelSelector
|
|
channels={OUTPUT_ORDER}
|
|
labels={outputLabels}
|
|
colors={OUTPUT_COLORS}
|
|
selected={selectedOutputs}
|
|
onToggle={toggleOutput}
|
|
/>
|
|
</div>
|
|
<ResponseGraph
|
|
channels={OUTPUT_ORDER}
|
|
states={dsp.state.current_config.output_states}
|
|
selected={selectedOutputs}
|
|
colors={OUTPUT_COLORS}
|
|
showStages={showOutputStages}
|
|
liveGains={liveOutputGains}
|
|
getChannelCurves={getOutputChannelCurves}
|
|
formatY={formatY}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="gain-section">
|
|
<div className="gain-panels">
|
|
<ChannelGroup title="Input">
|
|
{INPUT_ORDER.map((channel) => (
|
|
<GainPanel
|
|
key={channel}
|
|
dsp={dsp}
|
|
channel={{ Input: channel }}
|
|
title={dsp.state.current_config.input_states[channel].name}
|
|
setGain={setGain}
|
|
setMute={setMute}
|
|
setInverse={setInverse}
|
|
onLiveGain={(value) => setLiveInputGain(channel, value)}
|
|
/>
|
|
))}
|
|
</ChannelGroup>
|
|
|
|
<ChannelGroup title="Output">
|
|
{OUTPUT_ORDER.map((channel) => (
|
|
<GainPanel
|
|
key={channel}
|
|
dsp={dsp}
|
|
channel={{ Output: channel }}
|
|
title={dsp.state.current_config.output_states[channel].name}
|
|
setGain={setGain}
|
|
setMute={setMute}
|
|
setInverse={setInverse}
|
|
onLiveGain={(value) => setLiveOutputGain(channel, value)}
|
|
/>
|
|
))}
|
|
</ChannelGroup>
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default GainSection;
|