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; }, 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; setMute: (channel: Channel, muted: boolean) => Promise; setInverse: (channel: Channel, inverted: boolean) => Promise; 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 (
{title}
(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} />
); } function ChannelSelector({ channels, labels, colors, selected, onToggle, }: { channels: T[]; labels: Record; colors: Record; selected: Set; onToggle: (channel: T) => void; }) { return (
{channels.map((channel) => { const isOn = selected.has(channel); return ( ); })}
); } function GainSection({ dsp, setGain, setMute, setInverse, }: { dsp: DSP; setGain: (channel: Channel, gain: number) => Promise; setMute: (channel: Channel, muted: boolean) => Promise; setInverse: (channel: Channel, inverted: boolean) => Promise; }) { const [selectedInputs, setSelectedInputs] = useState>(new Set(INPUT_ORDER)); const [selectedOutputs, setSelectedOutputs] = useState>(new Set(OUTPUT_ORDER)); const [showInputStages, setShowInputStages] = useState(false); const [showOutputStages, setShowOutputStages] = useState(false); const [liveInputGains, setLiveInputGains] = useState>>({}); const [liveOutputGains, setLiveOutputGains] = useState>>( {} ); 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 = INPUT_ORDER.reduce( (acc, channel) => ({ ...acc, [channel]: dsp.state.current_config.input_states[channel].name, }), {} as Record ); const outputLabels: Record = OUTPUT_ORDER.reduce( (acc, channel) => ({ ...acc, [channel]: dsp.state.current_config.output_states[channel].name, }), {} as Record ); 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 ( <>
Input Response
Output Response
{INPUT_ORDER.map((channel) => ( setLiveInputGain(channel, value)} /> ))} {OUTPUT_ORDER.map((channel) => ( setLiveOutputGain(channel, value)} /> ))}
); } export default GainSection;