diff --git a/README.md b/README.md index d02469a..23f0c2e 100644 --- a/README.md +++ b/README.md @@ -20,12 +20,5 @@ This template should help get you started developing with Tauri, React and Types - Roll back on scroll when 2 scroll -- test en dto end connection - -Data: 10020001052f 31313131 10032a // lock - -Data: 10020001052d 31313131 100328 // unlock fail -Data: 10020100032d 3100 10031f - -Data: 10020001062d 0031313131 10032b // unlock success -Data: 10020100032d 0001 10032f \ No newline at end of file +- test end to end connection + \ No newline at end of file diff --git a/src/features/dsp408/components/ChannelSection/ChannelGraph.tsx b/src/features/dsp408/components/ChannelSection/ChannelGraph.tsx new file mode 100644 index 0000000..ba23f86 --- /dev/null +++ b/src/features/dsp408/components/ChannelSection/ChannelGraph.tsx @@ -0,0 +1,120 @@ +import { memo, useCallback, useMemo } from 'react'; +import { logSpace } from '../../utils/graph/curve'; +import { GRAPH_X_MIN, GRAPH_X_MAX, GRAPH_Y_MIN, GRAPH_Y_MAX } from '../../constants'; +import { PEQBand, PEQ, CrossoverFilter } from '../../types'; +import { sumCurvePoints, sumPhaseCurvePoints } from '../../utils/graph/curve'; +import { getPeqCurvePoints, getPeqPhasePoints } from '../../utils/graph/peq'; +import { getCrossoverCurvePoints, getCrossoverPhasePoints } from '../../utils/graph/crossover'; +import { drawCurves } from '../../utils/graph/draw'; +import LinearGraph from '../../../../components/LinearGraph/LinearGraph'; + +// Constant, so compute once instead of every render. +const GRAPH_FREQS = logSpace(GRAPH_X_MIN, GRAPH_X_MAX, 1000); +const formatTick = (v: number) => Math.round(v).toString(); + +type FilterGraphProps = { + bands: Record; + bypass: boolean; + hpFreq: number; + hpSlope: CrossoverFilter; + lpFreq: number; + lpSlope: CrossoverFilter; +}; + +export const MagnitudeGraph = memo(function MagnitudeGraph({ + bands, bypass, hpFreq, hpSlope, lpFreq, lpSlope, +}: FilterGraphProps) { + const points = useMemo( + () => + sumCurvePoints([ + getPeqCurvePoints({ freqs: GRAPH_FREQS, bands }), + getCrossoverCurvePoints({ + freqs: GRAPH_FREQS, + highPass: { frequency: hpFreq, filter_type: hpSlope }, + lowPass: { frequency: lpFreq, filter_type: lpSlope }, + }), + ]), + [bands, hpFreq, hpSlope, lpFreq, lpSlope] + ); + + const draw = useCallback( + ({ ctx, toPx }: any) => { + drawCurves({ + ctx, + toPx, + xMin: GRAPH_X_MIN, + xMax: GRAPH_X_MAX, + yMin: GRAPH_Y_MIN, + yMax: GRAPH_Y_MAX, + curves: [{ points, style: { strokeColor: '#e5e7eb', dimmed: bypass } }], + }); + }, + [points, bypass] + ); + + return ( + + ); +}); + +export const PhaseGraph = memo(function PhaseGraph({ + bands, bypass, hpFreq, hpSlope, lpFreq, lpSlope, +}: FilterGraphProps) { + const points = useMemo( + () => + sumPhaseCurvePoints([ + getPeqPhasePoints({ freqs: GRAPH_FREQS, bands }), + getCrossoverPhasePoints({ + freqs: GRAPH_FREQS, + highPass: { frequency: hpFreq, filter_type: hpSlope }, + lowPass: { frequency: lpFreq, filter_type: lpSlope }, + }), + ]), + [bands, hpFreq, hpSlope, lpFreq, lpSlope] + ); + + const draw = useCallback( + ({ ctx, toPx }: any) => { + drawCurves({ + ctx, + toPx, + xMin: GRAPH_X_MIN, + xMax: GRAPH_X_MAX, + yMin: -180, + yMax: 180, + curves: [{ points, style: { strokeColor: '#e5e7eb', dimmed: bypass } }], + }); + }, + [points, bypass] + ); + + return ( + + ); +}); \ No newline at end of file diff --git a/src/features/dsp408/components/ChannelSection/ChannelSection.tsx b/src/features/dsp408/components/ChannelSection/ChannelSection.tsx index aaada2a..ce07179 100644 --- a/src/features/dsp408/components/ChannelSection/ChannelSection.tsx +++ b/src/features/dsp408/components/ChannelSection/ChannelSection.tsx @@ -17,8 +17,7 @@ import { import { Channel, PEQBand, PEQFilter, PEQ, Crossover } from '../../types'; import { DSP408 as DSP } from '../../../../types'; import { CrossoverFilter } from '../../types'; -import { LinearGraph, LinearGraphHandle } from '../../../../components/LinearGraph/LinearGraph'; -import { useRef, useState, useEffect } from 'react'; +import { useRef, useState, useEffect, useMemo } from 'react'; import './ChannelSection.css'; import ChannelGroup from '../../../../components/ChannelGroup/ChannelGroup'; import Button from '../../../../components/Button/Button'; @@ -26,15 +25,8 @@ import { FREQUENCY_MAX, FREQUENCY_MIN, FREQUENCY_QUANTIZATION, - GRAPH_X_MAX, - GRAPH_X_MIN, - GRAPH_Y_MAX, - GRAPH_Y_MIN, } from '../../constants'; -import { drawCurves } from '../../utils/graph/draw'; -import { getPeqCurvePoints, getPeqPhasePoints } from '../../utils/graph/peq'; -import { logSpace, sumCurvePoints, sumPhaseCurvePoints } from '../../utils/graph/curve'; -import { getCrossoverCurvePoints, getCrossoverPhasePoints } from '../../utils/graph/crossover'; +import { MagnitudeGraph, PhaseGraph } from './ChannelGraph'; function nearestQValue(q: number, values: readonly number[] = PEAK_Q_VALUES): number { return values.reduce((nearest, current) => @@ -482,8 +474,6 @@ function ChannelSection({ const [liveLpFreq, setLiveLpFreq] = useState(FREQUENCY_MAX); const [livePeq, setLivePeq] = useState>>({}); - const magnitudeGraphRef = useRef(null); - // DSP values const isInput = 'Input' in channel; @@ -589,17 +579,20 @@ function ChannelSection({ return success; }; - // Diplay value (merge live and real) - const displayPeq: Record = { - ...ActivePeq.bands, - ...livePeq, + const displayPeq = useMemo( + () => ({ ...ActivePeq.bands, ...livePeq }) as Record, + [ActivePeq.bands, livePeq] + ); + + const graphProps = { + bands: displayPeq, + bypass: ActivePeq.bypass, + hpFreq: liveHpFreq, + hpSlope: ActiveHighPassFilter.slope, + lpFreq: liveLpFreq, + lpSlope: ActiveLowPassFilter.slope, }; - const highPassSpec = { frequency: liveHpFreq, filter_type: ActiveHighPassFilter.slope }; - const lowPassSpec = { frequency: liveLpFreq, filter_type: ActiveLowPassFilter.slope }; - - const freqs = logSpace(GRAPH_X_MIN, GRAPH_X_MAX, 1000); - return ( <>
@@ -611,84 +604,11 @@ function ChannelSection({ {(graphView === 'both' || graphView === 'magnitude') && (
- Math.round(v).toString()} - formatY={(v) => Math.round(v).toString()} - draw={({ ctx, toPx }) => { - drawCurves({ - ctx, - toPx, - xMin: GRAPH_X_MIN, - xMax: GRAPH_X_MAX, - yMin: GRAPH_Y_MIN, - yMax: GRAPH_Y_MAX, - curves: [ - { - points: sumCurvePoints([ - getPeqCurvePoints({ freqs, bands: displayPeq }), - getCrossoverCurvePoints({ - freqs, - highPass: highPassSpec, - lowPass: lowPassSpec, - }), - ]), - style: { strokeColor: '#e5e7eb', dimmed: ActivePeq.bypass }, - }, - ], - }); - }} - /> +
)} - {(graphView === 'both' || graphView === 'phase') && ( - Math.round(v).toString()} - formatY={(v) => Math.round(v).toString()} - draw={({ ctx, toPx }) => { - drawCurves({ - ctx, - toPx, - xMin: GRAPH_X_MIN, - xMax: GRAPH_X_MAX, - yMin: GRAPH_Y_MIN, - yMax: GRAPH_Y_MAX, - curves: [ - { - points: sumPhaseCurvePoints([ - getPeqPhasePoints({ freqs, bands: displayPeq }), - getCrossoverPhasePoints({ - freqs, - highPass: highPassSpec, - lowPass: lowPassSpec, - }), - ]), - style: { strokeColor: '#e5e7eb', dimmed: ActivePeq.bypass }, - }, - ], - }); - }} - /> - )} + {(graphView === 'both' || graphView === 'phase') && } {ActivePeq.bypass && (
diff --git a/src/features/dsp408/components/CompressorSection/CompressorGraph.tsx b/src/features/dsp408/components/CompressorSection/CompressorGraph.tsx new file mode 100644 index 0000000..d17a9f1 --- /dev/null +++ b/src/features/dsp408/components/CompressorSection/CompressorGraph.tsx @@ -0,0 +1,122 @@ +import { memo, useCallback, useMemo } from 'react'; +import { Ratio } from '../../types'; +import { RATIO_NUMERIC } from '../../constants'; +import LinearGraph from '../../../../components/LinearGraph/LinearGraph'; + +const formatTick = (v: number) => Math.round(v).toString(); + +// Compressor transfer curve, in dB. +// +// The knee is NOT a smooth/quadratic blend. It is three straight +// (piecewise-linear) segments: +// 1) input <= kneeStart -> 1:1 line (output = input) +// 2) kneeStart < input < kneeEnd -> a straight line connecting the +// point on segment (1) at kneeStart +// to the point on segment (3) at kneeEnd +// 3) input >= kneeEnd -> ratio line (output = threshold + (input-threshold)/ratio) +// +// kneeStart/kneeEnd are simply the threshold shifted down/up by half the +// knee width, so increasing "knee" widens the middle straight segment +// symmetrically around the threshold. +function compressorOutputDb(input: number, threshold: number, ratio: number, knee: number): number { + const kneeStart = threshold - knee / 2; + const kneeEnd = threshold + knee / 2; + + // No knee: simple two-segment (hard-knee) curve. + if (knee <= 0) { + return input <= threshold ? input : threshold + (input - threshold) / ratio; + } + + // Segment 1: below the knee, straight 1:1 line. + if (input <= kneeStart) { + return input; + } + + // Segment 3: above the knee, straight ratio line. + if (input >= kneeEnd) { + return threshold + (input - threshold) / ratio; + } + + // Segment 2: straight line between the two boundary points + // (kneeStart, kneeStart) and (kneeEnd, threshold + (kneeEnd - threshold) / ratio). + const y1 = kneeStart; + const y2 = threshold + (kneeEnd - threshold) / ratio; + const t = (input - kneeStart) / (kneeEnd - kneeStart); + return y1 + t * (y2 - y1); +} + +export const CompressorGraph = memo(function CompressorGraph({ + threshold, + ratio, + knee, +}: { + threshold: number; + ratio: Ratio; + knee: number; +}) { + const curvePoints = useMemo(() => { + const ratioNumeric = RATIO_NUMERIC[ratio]; + const steps = 120; + const points: [number, number][] = []; + for (let i = 0; i <= steps; i++) { + const x = -100 + (120 * i) / steps; + points.push([x, compressorOutputDb(x, threshold, ratioNumeric, knee)]); + } + return points; + }, [threshold, ratio, knee]); + + const draw = useCallback( + ({ ctx, toPx }: any) => { + // Unity reference line + ctx.strokeStyle = '#6b7280'; + ctx.lineWidth = 1; + ctx.setLineDash([4, 4]); + const [ux0, uy0] = toPx(-100, -100); + const [ux1, uy1] = toPx(20, 20); + 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(threshold, -100); + const [tx1, ty1] = toPx(threshold, 20); + ctx.beginPath(); + ctx.moveTo(tx0, ty0); + ctx.lineTo(tx1, ty1); + ctx.stroke(); + ctx.setLineDash([]); + + // Compression curve + ctx.strokeStyle = '#4ade80'; + 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(); + }, + [threshold, curvePoints] + ); + + return ( + + ); +}); \ No newline at end of file diff --git a/src/features/dsp408/components/CompressorSection/CompressorSection.tsx b/src/features/dsp408/components/CompressorSection/CompressorSection.tsx index d274239..1081edb 100644 --- a/src/features/dsp408/components/CompressorSection/CompressorSection.tsx +++ b/src/features/dsp408/components/CompressorSection/CompressorSection.tsx @@ -3,12 +3,12 @@ import VerticalSlider from '../../../../components/VerticalSlider/VerticalSlider import { OutputChannel, Ratio } from '../../types'; import { DSP408 as DSP } from '../../../../types'; import ChannelGroup from '../../../../components/ChannelGroup/ChannelGroup'; -import { LinearGraph } from '../../../../components/LinearGraph/LinearGraph'; +import { CompressorGraph } from './CompressorGraph'; import MeterBar from '../../../../components/MeterBar/MeterBar'; -import { useMemo, useState } from 'react'; +import { useState } from 'react'; import ChoiceStepper from '../../../../components/ChoiceStepper/ChoiceStepper'; import './CompressorSection.css'; -import { RATIO_ORDER, RATIO_LABEL, RATIO_NUMERIC, KNEE_VALUES } from '../../constants'; +import { RATIO_ORDER, RATIO_LABEL, KNEE_VALUES } from '../../constants'; const COMPRESSOR_SLIDER_PARAMS = [ { key: 'threshold', label: 'Threshold', min: -90, max: 20, step: 0.5, unit: ' dB' }, @@ -112,45 +112,7 @@ function CompressorPanel({ ); } -// Compressor transfer curve, in dB. -// -// The knee is NOT a smooth/quadratic blend. It is three straight -// (piecewise-linear) segments: -// 1) input <= kneeStart -> 1:1 line (output = input) -// 2) kneeStart < input < kneeEnd -> a straight line connecting the -// point on segment (1) at kneeStart -// to the point on segment (3) at kneeEnd -// 3) input >= kneeEnd -> ratio line (output = threshold + (input-threshold)/ratio) -// -// kneeStart/kneeEnd are simply the threshold shifted down/up by half the -// knee width, so increasing "knee" widens the middle straight segment -// symmetrically around the threshold. -function compressorOutputDb(input: number, threshold: number, ratio: number, knee: number): number { - const kneeStart = threshold - knee / 2; - const kneeEnd = threshold + knee / 2; - // No knee: simple two-segment (hard-knee) curve. - if (knee <= 0) { - return input <= threshold ? input : threshold + (input - threshold) / ratio; - } - - // Segment 1: below the knee, straight 1:1 line. - if (input <= kneeStart) { - return input; - } - - // Segment 3: above the knee, straight ratio line. - if (input >= kneeEnd) { - return threshold + (input - threshold) / ratio; - } - - // Segment 2: straight line between the two boundary points - // (kneeStart, kneeStart) and (kneeEnd, threshold + (kneeEnd - threshold) / ratio). - const y1 = kneeStart; - const y2 = threshold + (kneeEnd - threshold) / ratio; - const t = (input - kneeStart) / (kneeEnd - kneeStart); - return y1 + t * (y2 - y1); -} function CompressorSection({ dsp, set_compressor }: { dsp: DSP; set_compressor: SetCompressorFn }) { const outputs = [ @@ -207,70 +169,15 @@ function CompressorSection({ dsp, set_compressor }: { dsp: DSP; set_compressor: const activeOutputState = dsp.state.current_config.output_states[activeChannel]; const activeComp = liveComp[activeChannel] ?? (activeOutputState.compressor as CompressorValues); - const curvePoints = useMemo(() => { - const { threshold, ratio, knee } = activeComp; - const ratioNumeric = RATIO_NUMERIC[ratio]; - const steps = 120; - const points: [number, number][] = []; - for (let i = 0; i <= steps; i++) { - const x = -100 + (120 * i) / steps; - points.push([x, compressorOutputDb(x, threshold, ratioNumeric, knee)]); - } - return points; - }, [activeComp.threshold, activeComp.ratio, activeComp.knee]); - return ( <>
{activeOutputState.name} - RMS Compressor
- - 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(-100, -100); - const [ux1, uy1] = toPx(20, 20); - 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(activeComp.threshold, -100); - const [tx1, ty1] = toPx(activeComp.threshold, 20); - ctx.beginPath(); - ctx.moveTo(tx0, ty0); - ctx.lineTo(tx1, ty1); - ctx.stroke(); - ctx.setLineDash([]); - - // Compression curve - ctx.strokeStyle = '#4ade80'; - 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(); - }} +
diff --git a/src/features/dsp408/components/DelaySection/DelayGraph.tsx b/src/features/dsp408/components/DelaySection/DelayGraph.tsx new file mode 100644 index 0000000..2033eca --- /dev/null +++ b/src/features/dsp408/components/DelaySection/DelayGraph.tsx @@ -0,0 +1,73 @@ +import { memo, useCallback, useMemo } from 'react'; +import { DelayUnit } from '../../types'; +import { DELAY_RANGES } from '../../constants'; +import LinearGraph from '../../../../components/LinearGraph/LinearGraph'; +import { DELAY_UNIT_LABEL } from '../../constants'; + +const formatY = (v: number) => v.toFixed(0); + +type DelayRow = { label: string; delay: number }; + +const rowsEqual = (a: DelayRow[], b: DelayRow[]) => + a.length === b.length && + a.every((row, i) => row.label === b[i].label && row.delay === b[i].delay); + +export const DelayGraph = memo( + function DelayGraph({ + title, + rows, + unit, + }: { + title: string; + rows: DelayRow[]; + unit: DelayUnit; + }) { + const [min, max] = DELAY_RANGES[unit]; + const roundedMax = (Math.floor(max / 100) + 1) * 100; + + const labelByX = useMemo(() => { + const map = new Map(); + rows.forEach((row, i) => map.set(i + 1, row.label)); + return map; + }, [rows]); + + const formatX = useCallback((v: number) => labelByX.get(Math.round(v)) ?? '', [labelByX]); + + const draw = useCallback( + ({ ctx, toPx, plot }: any) => { + const barWidth = (plot.width / rows.length) * 0.55; + + rows.forEach((row, i) => { + const x = i + 1; + const [xPx, baseY] = toPx(x, min); + const [, topY] = toPx(x, row.delay); + + ctx.fillStyle = '#4ade80'; + ctx.fillRect(xPx - barWidth / 2, topY, barWidth, baseY - topY); + }); + }, + [rows, min] + ); + + return ( +
+
{title}
+ +
+ ); + }, + (prev, next) => + prev.title === next.title && prev.unit === next.unit && rowsEqual(prev.rows, next.rows) +); \ No newline at end of file diff --git a/src/features/dsp408/components/DelaySection/DelaySection.tsx b/src/features/dsp408/components/DelaySection/DelaySection.tsx index b8eaa7f..83daf17 100644 --- a/src/features/dsp408/components/DelaySection/DelaySection.tsx +++ b/src/features/dsp408/components/DelaySection/DelaySection.tsx @@ -3,16 +3,10 @@ import { InputChannel, Channel, OutputChannel, DelayUnit } from '../../types'; import { DSP408 as DSP } from '../../../../types'; import Button from '../../../../components/Button/Button'; import ChannelGroup from '../../../../components/ChannelGroup/ChannelGroup'; -import { LinearGraph } from '../../../../components/LinearGraph/LinearGraph'; -import { useMemo, useState } from 'react'; +import { DelayGraph } from './DelayGraph'; +import { useState } from 'react'; import './DelaySection.css'; -import { DELAY_RANGES } from '../../constants'; - -const UNIT_LABEL: Record = { - [DelayUnit.Millisecond]: 'ms', - [DelayUnit.Meter]: 'm', - [DelayUnit.Feet]: 'ft', -}; +import { DELAY_RANGES, DELAY_UNIT_LABEL } from '../../constants'; const UNIT_ORDER: DelayUnit[] = [DelayUnit.Millisecond, DelayUnit.Meter, DelayUnit.Feet]; @@ -60,7 +54,7 @@ function DelayPanel({ min={min} max={max} step={() => DELAY_STEP[unit]} - unit={' ' + UNIT_LABEL[unit]} + unit={' ' + DELAY_UNIT_LABEL[unit]} value={state.delay} onInput={(value) => onLiveValue(channel, value)} onChange={(value) => setDelay(channel, value)} @@ -69,54 +63,6 @@ function DelayPanel({ ); } -function DelayGraph({ - title, - rows, - unit, -}: { - title: string; - rows: { label: string; delay: number }[]; - unit: DelayUnit; -}) { - const [min, max] = DELAY_RANGES[unit]; - const roundedMax = (Math.floor(max / 100) + 1) * 100; - const labelByX = useMemo(() => { - const map = new Map(); - rows.forEach((row, i) => map.set(i + 1, row.label)); - return map; - }, [rows]); - - return ( -
-
{title}
- labelByX.get(Math.round(v)) ?? ''} - formatY={(v) => v.toFixed(0)} - draw={({ ctx, toPx, plot }) => { - const barWidth = (plot.width / rows.length) * 0.55; - - rows.forEach((row, i) => { - const x = i + 1; - const [xPx, baseY] = toPx(x, min); - const [, topY] = toPx(x, row.delay); - - ctx.fillStyle = '#4ade80'; - ctx.fillRect(xPx - barWidth / 2, topY, barWidth, baseY - topY); - }); - }} - /> -
- ); -} - function DelayUnitSelector({ unit, onSelect, @@ -140,7 +86,7 @@ function DelayUnitSelector({ minHeight: 0, }} > - {UNIT_LABEL[u]} + {DELAY_UNIT_LABEL[u]} ))}
diff --git a/src/features/dsp408/components/GainSection/GainGraph.tsx b/src/features/dsp408/components/GainSection/GainGraph.tsx new file mode 100644 index 0000000..fa9f22d --- /dev/null +++ b/src/features/dsp408/components/GainSection/GainGraph.tsx @@ -0,0 +1,144 @@ +import { memo, useCallback, useMemo } from 'react'; +import { GRAPH_FREQS, GRAPH_X_MAX, GRAPH_X_MIN, GRAPH_Y_MIN, Y_AXIS_STEP } from '../../constants'; +import { Curve, CurvePoint } from '../../utils/graph/curve'; +import { drawCurves } from '../../utils/graph/draw'; +import LinearGraph from '../../../../components/LinearGraph/LinearGraph'; + +// Expands [baseMin, baseMax] outward to fit whatever the curves actually +// contain (e.g. gain + geq + peq stacking past the nominal graph range), +// snapped to the axis step with one step of headroom so nothing touches +// the plot edge. Never shrinks below the base range. +function computeYAxisBounds( + curves: Curve[], + baseMin: number, + baseMax: number, + step: number +): { yMin: number; yMax: number } { + let dataMax = baseMax; + let dataMin = baseMin; + + for (const curve of curves) { + for (const { y } of curve.points) { + if (y > dataMax) dataMax = y; + if (y < dataMin) dataMin = y; + } + } + + const yMax = Math.ceil((dataMax + step) / step) * step; + const yMin = Math.floor((dataMin - step) / step) * step; + + return { yMin, yMax }; +} + +// fill:false everywhere — with multiple overlapping curves the fill/fade +// just muddies things; stroke-only makes each channel/stage legible. +function buildCurvesForChannels( + channels: T[], + selected: Set, + colors: Record, + showStages: boolean, + getCurves: (channel: T) => ChannelCurves +): Curve[] { + const curves: Curve[] = []; + + [...channels].reverse().forEach((channel) => { + if (!selected.has(channel)) return; + const { geq, peqCrossover, total } = getCurves(channel); + const color = colors[channel]; + + if (showStages) { + if (geq.length > 0) { + curves.push({ + points: geq, + style: { strokeColor: color, fill: false, dimmed: true, lineWidth: 1 }, + }); + } + if (peqCrossover.length > 0) { + curves.push({ + points: peqCrossover, + style: { strokeColor: color, fill: false, dimmed: true, lineWidth: 1 }, + }); + } + } + + curves.push({ points: total, style: { strokeColor: color, fill: false, lineWidth: 2 } }); + }); + + return curves; +} + +const formatX = (v: number) => Math.round(v).toString(); +export const formatOutputY = (v: number) => Math.round(v).toString(); +export const formatInputY = (v: number) => { + if (v === 0) return '0'; + if (v > 0) return v % 12 === 0 ? `+${Math.round(v)}` : ''; + return Math.abs(v) % 15 === 0 ? Math.round(v).toString() : ''; +}; + +export type ChannelCurves = { + geq: CurvePoint[]; + peqCrossover: CurvePoint[]; + total: CurvePoint[]; +}; + +type ResponseGraphProps = { + channels: T[]; + states: Record; + selected: Set; + colors: Record; + showStages: boolean; + liveGains: Partial>; + getChannelCurves: (state: S, freqs: number[], liveGain: number | undefined) => ChannelCurves; + formatY: (v: number) => string; +}; + +function ResponseGraphInner({ + channels, + states, + selected, + colors, + showStages, + liveGains, + getChannelCurves, + formatY, +}: ResponseGraphProps) { + const curves = useMemo( + () => + buildCurvesForChannels(channels, selected, colors, showStages, (ch) => + getChannelCurves(states[ch], GRAPH_FREQS, liveGains[ch]) + ), + [channels, selected, colors, showStages, states, liveGains, getChannelCurves] + ); + + const { yMin, yMax } = useMemo( + () => computeYAxisBounds(curves, GRAPH_Y_MIN, 12, Y_AXIS_STEP), + [curves] + ); + + const draw = useCallback( + ({ ctx, toPx }: any) => { + drawCurves({ ctx, toPx, xMin: GRAPH_X_MIN, xMax: GRAPH_X_MAX, yMin, yMax, curves }); + }, + [curves, yMin, yMax] + ); + + return ( + + ); +} + +// memo() erases the generics, so cast it back. +export const ResponseGraph = memo(ResponseGraphInner) as typeof ResponseGraphInner; \ No newline at end of file diff --git a/src/features/dsp408/components/GainSection/GainSection.tsx b/src/features/dsp408/components/GainSection/GainSection.tsx index e971708..f2f8191 100644 --- a/src/features/dsp408/components/GainSection/GainSection.tsx +++ b/src/features/dsp408/components/GainSection/GainSection.tsx @@ -6,52 +6,21 @@ import ChannelGroup from '../../../../components/ChannelGroup/ChannelGroup'; import './GainSection.css'; import { GEQ_BANDS_LABEL, - GRAPH_X_MAX, - GRAPH_X_MIN, - GRAPH_Y_MAX, - GRAPH_Y_MIN, INPUT_ORDER, OUTPUT_ORDER, + GEQ_CURVE_Q, + INPUT_COLORS, + OUTPUT_COLORS, } from '../../constants'; import { useState } from 'react'; -import LinearGraph from '../../../../components/LinearGraph/LinearGraph'; -import { drawCurves } from '../../utils/graph/draw'; -import { Curve, CurvePoint, sumCurvePoints } from '../../utils/graph/curve'; +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 { logSpace } from '../../utils/graph/curve'; - -const CHANNEL_COLORS = [ - '#4ade80', - '#60a5fa', - '#f472b6', - '#fbbf24', - '#a78bfa', - '#34d399', - '#f87171', - '#38bdf8', -]; - -const GEQ_CURVE_Q = 5; -const Y_AXIS_STEP = 3; // must match the graph's yStep - -function buildColorMap(channels: T[]): Record { - const map = {} as Record; - channels.forEach((ch, i) => { - map[ch] = CHANNEL_COLORS[i % CHANNEL_COLORS.length]; - }); - return map; -} +import { ChannelCurves, formatInputY, formatOutputY, ResponseGraph } from './GainGraph'; // ---- Curve construction ----------------------------------------------- -type ChannelCurves = { - geq: CurvePoint[]; - peqCrossover: CurvePoint[]; - total: CurvePoint[]; -}; - function getGainCurvePoints(freqs: number[], gainDb: number): CurvePoint[] { return freqs.map((x) => ({ x, y: gainDb })); } @@ -126,69 +95,6 @@ function getOutputChannelCurves( return { geq: [], peqCrossover, total }; } -// fill:false everywhere — with multiple overlapping curves the fill/fade -// just muddies things; stroke-only makes each channel/stage legible. -function buildCurvesForChannels( - channels: T[], - selected: Set, - colors: Record, - showStages: boolean, - getCurves: (channel: T) => ChannelCurves -): Curve[] { - const curves: Curve[] = []; - - [...channels].reverse().forEach((channel) => { - if (!selected.has(channel)) return; - const { geq, peqCrossover, total } = getCurves(channel); - const color = colors[channel]; - - if (showStages) { - if (geq.length > 0) { - curves.push({ - points: geq, - style: { strokeColor: color, fill: false, dimmed: true, lineWidth: 1 }, - }); - } - if (peqCrossover.length > 0) { - curves.push({ - points: peqCrossover, - style: { strokeColor: color, fill: false, dimmed: true, lineWidth: 1 }, - }); - } - } - - curves.push({ points: total, style: { strokeColor: color, fill: false, lineWidth: 2 } }); - }); - - return curves; -} - -// Expands [baseMin, baseMax] outward to fit whatever the curves actually -// contain (e.g. gain + geq + peq stacking past the nominal graph range), -// snapped to the axis step with one step of headroom so nothing touches -// the plot edge. Never shrinks below the base range. -function computeYAxisBounds( - curves: Curve[], - baseMin: number, - baseMax: number, - step: number -): { yMin: number; yMax: number } { - let dataMax = baseMax; - let dataMin = baseMin; - - for (const curve of curves) { - for (const { y } of curve.points) { - if (y > dataMax) dataMax = y; - if (y < dataMin) dataMin = y; - } - } - - const yMax = Math.ceil((dataMax + step) / step) * step; - const yMin = Math.floor((dataMin - step) / step) * step; - - return { yMin, yMax }; -} - // ---- Panels ------------------------------------------------------------- function GainPanel({ @@ -334,9 +240,6 @@ function GainSection({ setMute: (channel: Channel, muted: boolean) => Promise; setInverse: (channel: Channel, inverted: boolean) => Promise; }) { - const inputColors = buildColorMap(INPUT_ORDER); - const outputColors = buildColorMap(OUTPUT_ORDER); - const [selectedInputs, setSelectedInputs] = useState>(new Set(INPUT_ORDER)); const [selectedOutputs, setSelectedOutputs] = useState>(new Set(OUTPUT_ORDER)); @@ -402,47 +305,6 @@ function GainSection({ }); }; - const freqs = logSpace(GRAPH_X_MIN, GRAPH_X_MAX, 1000); - - const inputCurves = buildCurvesForChannels( - INPUT_ORDER, - selectedInputs, - inputColors, - showInputStages, - (channel) => - getInputChannelCurves( - dsp.state.current_config.input_states[channel], - freqs, - liveInputGains[channel] - ) - ); - - const outputCurves = buildCurvesForChannels( - OUTPUT_ORDER, - selectedOutputs, - outputColors, - showOutputStages, - (channel) => - getOutputChannelCurves( - dsp.state.current_config.output_states[channel], - freqs, - liveOutputGains[channel] - ) - ); - - const { yMin: inputYMin, yMax: inputYMax } = computeYAxisBounds( - inputCurves, - GRAPH_Y_MIN, - 12, - Y_AXIS_STEP - ); - const { yMin: outputYMin, yMax: outputYMax } = computeYAxisBounds( - outputCurves, - GRAPH_Y_MIN, - GRAPH_Y_MAX, - Y_AXIS_STEP - ); - return ( <>
@@ -461,45 +323,20 @@ function GainSection({
- Math.round(v).toString()} - formatY={(v) => { - // Always label 0. - if (v === 0) return '0'; - - // Positive side: label every 12 dB. - if (v > 0) { - return v % 12 === 0 ? `+${Math.round(v)}` : ''; - } - - // Negative side: label every 15 dB. - return Math.abs(v) % 15 === 0 ? Math.round(v).toString() : ''; - }} - draw={({ ctx, toPx }) => { - drawCurves({ - ctx, - toPx, - xMin: GRAPH_X_MIN, - xMax: GRAPH_X_MAX, - yMin: inputYMin, - yMax: inputYMax, - curves: inputCurves, - }); - }} +
@@ -519,34 +356,20 @@ function GainSection({
- Math.round(v).toString()} - formatY={(v) => Math.round(v).toString()} - draw={({ ctx, toPx }) => { - drawCurves({ - ctx, - toPx, - xMin: GRAPH_X_MIN, - xMax: GRAPH_X_MAX, - yMin: outputYMin, - yMax: outputYMax, - curves: outputCurves, - }); - }} + diff --git a/src/features/dsp408/components/GateSection/GateGraph.tsx b/src/features/dsp408/components/GateSection/GateGraph.tsx new file mode 100644 index 0000000..ed55c32 --- /dev/null +++ b/src/features/dsp408/components/GateSection/GateGraph.tsx @@ -0,0 +1,72 @@ +import { memo, useCallback, useMemo } from 'react'; +import LinearGraph from '../../../../components/LinearGraph/LinearGraph'; + +const formatTick = (v: number) => Math.round(v).toString(); + +export const GateGraph = memo(function GateGraph({ + threshold, + attack, + hold, + release, +}: { + threshold: number; + attack: number; + hold: number; + release: number; +}) { + const envelope = useMemo(() => { + const roll = Math.max(20, (attack + hold + release) * 0.08); + const t1 = roll; + const t2 = t1 + attack; + const t3 = t2 + hold; + const t4 = t3 + release; + const t5 = t4 + roll; + return { t0: 0, t1, t2, t3, t4, t5 }; + }, [attack, hold, release]); + + const draw = useCallback( + ({ toPx, ctx }: any) => { + const { t0, t1, t2, t3, t4, t5 } = envelope; + + ctx.strokeStyle = '#fbbf24'; + ctx.lineWidth = 1; + ctx.setLineDash([4, 4]); + const [tx0, ty] = toPx(t0, threshold); + const [tx1] = toPx(t5, threshold); + ctx.beginPath(); + ctx.moveTo(tx0, ty); + ctx.lineTo(tx1, ty); + ctx.stroke(); + ctx.setLineDash([]); + + ctx.strokeStyle = '#4ade80'; + ctx.lineWidth = 2; + ctx.lineJoin = 'round'; + const points: [number, number][] = [ + [t0, -90], [t1, -90], [t2, 0], [t3, 0], [t4, -90], [t5, -90], + ]; + ctx.beginPath(); + points.forEach(([t, g], i) => { + const [px, py] = toPx(t, g); + if (i === 0) ctx.moveTo(px, py); + else ctx.lineTo(px, py); + }); + ctx.stroke(); + }, + [envelope, threshold] + ); + + return ( + + ); +}); \ No newline at end of file diff --git a/src/features/dsp408/components/GateSection/GateSection.tsx b/src/features/dsp408/components/GateSection/GateSection.tsx index 17bb563..952b88d 100644 --- a/src/features/dsp408/components/GateSection/GateSection.tsx +++ b/src/features/dsp408/components/GateSection/GateSection.tsx @@ -3,9 +3,9 @@ import VerticalSlider from '../../../../components/VerticalSlider/VerticalSlider import { InputChannel } from '../../types'; import { DSP408 as DSP } from '../../../../types'; import ChannelGroup from '../../../../components/ChannelGroup/ChannelGroup'; -import { LinearGraph } from '../../../../components/LinearGraph/LinearGraph'; +import { GateGraph } from './GateGraph'; import MeterBar from '../../../../components/MeterBar/MeterBar'; -import { useMemo, useState } from 'react'; +import { useState } from 'react'; import './GateSection.css'; const GATE_PARAMS = [ @@ -112,65 +112,16 @@ function GateSection({ dsp, set_gate }: { dsp: DSP; set_gate: SetGateFn }) { const activeInputState = dsp.state.current_config.input_states[activeChannel]; const activeGate = liveGate[activeChannel] ?? activeInputState.gate; - const envelope = useMemo(() => { - const { threshold, attack, hold, release } = activeGate; - const roll = Math.max(20, (attack + hold + release) * 0.08); - const t0 = 0; - const t1 = t0 + roll; - const t2 = t1 + attack; - const t3 = t2 + hold; - const t4 = t3 + release; - const t5 = t4 + roll; - return { threshold, t0, t1, t2, t3, t4, t5 }; - }, [activeGate.threshold, activeGate.attack, activeGate.hold, activeGate.release]); - return ( <>
-
Frequency Response
- Math.round(v).toString()} - formatY={(v) => Math.round(v).toString()} - draw={({ toPx, ctx }) => { - const { threshold, t0, t1, t2, t3, t4, t5 } = envelope; - - ctx.strokeStyle = '#fbbf24'; - ctx.lineWidth = 1; - ctx.setLineDash([4, 4]); - const [tx0, ty] = toPx(t0, threshold); - const [tx1] = toPx(t5, threshold); - ctx.beginPath(); - ctx.moveTo(tx0, ty); - ctx.lineTo(tx1, ty); - ctx.stroke(); - ctx.setLineDash([]); - - ctx.strokeStyle = '#4ade80'; - ctx.lineWidth = 2; - ctx.lineJoin = 'round'; - const points: [number, number][] = [ - [t0, -90], - [t1, -90], - [t2, 0], - [t3, 0], - [t4, -90], - [t5, -90], - ]; - ctx.beginPath(); - points.forEach(([t, g], i) => { - const [px, py] = toPx(t, g); - if (i === 0) ctx.moveTo(px, py); - else ctx.lineTo(px, py); - }); - ctx.stroke(); - }} +
Gate Response
+
diff --git a/src/features/dsp408/components/GeqSection/GeqGraph.tsx b/src/features/dsp408/components/GeqSection/GeqGraph.tsx new file mode 100644 index 0000000..e68eb76 --- /dev/null +++ b/src/features/dsp408/components/GeqSection/GeqGraph.tsx @@ -0,0 +1,73 @@ +import { memo, useCallback, useMemo } from 'react'; +import { logSpace } from '../../utils/graph/curve'; +import { drawCurves } from '../../utils/graph/draw'; +import { GRAPH_X_MIN, GRAPH_X_MAX, GRAPH_Y_MIN, GRAPH_Y_MAX, GEQ_CURVE_Q } from '../../constants'; +import { DiscreteFrequency } from '../../types'; +import { GEQ_BANDS_LABEL } from '../../constants'; +import LinearGraph from '../../../../components/LinearGraph/LinearGraph'; +import { getGeqCurvePoints } from '../../utils/graph/geq'; +import { LiveBands } from './GeqSection'; + +const GEQ_FREQS = logSpace(GRAPH_X_MIN, GRAPH_X_MAX, 1000); + +export const GeqGraph = memo(function GeqGraph({ + title, + gains, + bypass, + liveBands, +}: { + title: string; + gains: Record; + bypass: boolean; + liveBands: LiveBands | undefined; +}) { + const curvePoints = useMemo(() => { + const values = GEQ_BANDS_LABEL.map( + ({ frequency }) => liveBands?.[frequency] ?? gains[frequency] ?? 0 + ); + return getGeqCurvePoints({ freqs: GEQ_FREQS, values, q: GEQ_CURVE_Q }); + }, [gains, liveBands]); + + const draw = useCallback( + ({ ctx, toPx }: any) => { + drawCurves({ + ctx, + toPx, + xMin: GRAPH_X_MIN, + xMax: GRAPH_X_MAX, + yMin: GRAPH_Y_MIN, + yMax: GRAPH_Y_MAX, + curves: [ + { + points: curvePoints, + style: { strokeColor: '#e5e7eb', dimmed: bypass }, + }, + ], + }); + }, + [curvePoints, bypass] + ); + + return ( +
+
{title}
+ + {bypass && ( +
+ BYPASSED +
+ )} +
+ ); +}); \ No newline at end of file diff --git a/src/features/dsp408/components/GeqSection/GeqSection.tsx b/src/features/dsp408/components/GeqSection/GeqSection.tsx index a7f8a74..f3ac8bc 100644 --- a/src/features/dsp408/components/GeqSection/GeqSection.tsx +++ b/src/features/dsp408/components/GeqSection/GeqSection.tsx @@ -3,26 +3,18 @@ import { InputChannel, DiscreteFrequency } from '../../types'; import { DSP408 as DSP } from '../../../../types'; import Button from '../../../../components/Button/Button'; import ChannelGroup from '../../../../components/ChannelGroup/ChannelGroup'; -import { LinearGraph } from '../../../../components/LinearGraph/LinearGraph'; -import { drawCurves } from '../../utils/graph/draw'; import { INPUT_ORDER, - GRAPH_X_MIN, - GRAPH_X_MAX, - GRAPH_Y_MIN, - GRAPH_Y_MAX, GEQ_GAIN_MIN, GEQ_GAIN_MAX, GEQ_GAIN_STEP, GEQ_BANDS_LABEL, - GEQ_CURVE_Q, } from '../../constants'; import { useState, useEffect } from 'react'; import './GeqSection.css'; -import { getGeqCurvePoints } from '../../utils/graph/geq'; -import { logSpace } from '../../utils/graph/curve'; +import { GeqGraph } from './GeqGraph'; -type LiveBands = Partial>; +export type LiveBands = Partial>; type SetGeqBandFn = ( input: InputChannel, @@ -69,64 +61,6 @@ function GeqBandPanel({ ); } -function GeqGraph({ - title, - channel, - dsp, - liveBands, -}: { - title: string; - channel: InputChannel; - dsp: DSP; - liveBands: LiveBands | undefined; -}) { - const geq = dsp.state.current_config.input_states[channel].geq; - - const values = GEQ_BANDS_LABEL.map( - ({ frequency }) => liveBands?.[frequency] ?? geq.gains[frequency] ?? 0 - ); - const freqs = logSpace(GRAPH_X_MIN, GRAPH_X_MAX, 1000); - - return ( -
-
{title}
- { - drawCurves({ - ctx, - toPx, - - xMin: GRAPH_X_MIN, - xMax: GRAPH_X_MAX, - yMin: GRAPH_Y_MIN, - yMax: GRAPH_Y_MAX, - curves: [ - { - points: getGeqCurvePoints({ freqs, values, q: GEQ_CURVE_Q }), - style: { strokeColor: '#e5e7eb', dimmed: geq.bypass }, - }, - ], - }); - }} - /> - {geq.bypass && ( -
- BYPASSED -
- )} -
- ); -} - function GeqChannelSelector({ channel, inputs, @@ -298,8 +232,8 @@ function GeqSection({
diff --git a/src/features/dsp408/components/LimiterSection/LimiterGraph.tsx b/src/features/dsp408/components/LimiterSection/LimiterGraph.tsx new file mode 100644 index 0000000..6b427ca --- /dev/null +++ b/src/features/dsp408/components/LimiterSection/LimiterGraph.tsx @@ -0,0 +1,76 @@ +import { memo, useCallback, useMemo } from 'react'; +import LinearGraph from '../../../../components/LinearGraph/LinearGraph'; + +const formatTick = (v: number) => Math.round(v).toString(); + +// Hard-knee brickwall limiter transfer curve, in dB. +function limiterOutputDb(input: number, threshold: number): number { + return input <= threshold ? input : threshold; +} + +export const LimiterGraph = memo(function LimiterGraph({ threshold }: { threshold: number }) { + const curvePoints = useMemo(() => { + 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; + }, [threshold]); + + const draw = useCallback( + ({ ctx, toPx }: any) => { + // 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(threshold, -60); + const [tx1, ty1] = toPx(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(); + }, + [threshold, curvePoints] + ); + + return ( + + ); +}); \ No newline at end of file diff --git a/src/features/dsp408/components/LimiterSection/LimiterSection.tsx b/src/features/dsp408/components/LimiterSection/LimiterSection.tsx index ba60c67..bf80a9b 100644 --- a/src/features/dsp408/components/LimiterSection/LimiterSection.tsx +++ b/src/features/dsp408/components/LimiterSection/LimiterSection.tsx @@ -3,9 +3,9 @@ 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 { LimiterGraph } from './LimiterGraph'; import MeterBar from '../../../../components/MeterBar/MeterBar'; -import { useMemo, useState } from 'react'; +import { useState } from 'react'; import './LimiterSection.css'; const LIMITER_SLIDER_PARAMS = [ @@ -74,11 +74,6 @@ function LimiterPanel({ ); } -// 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, @@ -122,70 +117,12 @@ function LimiterSection({ dsp, set_limiter }: { dsp: DSP; set_limiter: SetLimite 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 ( <>
{activeOutputState.name} - Limiter
- - 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(); - }} - /> +
diff --git a/src/features/dsp408/constants.ts b/src/features/dsp408/constants.ts index 82ce90e..cadcac0 100644 --- a/src/features/dsp408/constants.ts +++ b/src/features/dsp408/constants.ts @@ -8,6 +8,8 @@ import { DelayUnit, Ratio, } from './types'; +import { logSpace } from './utils/graph/curve'; +import { buildColorMap } from './utils/graph/draw'; export const INPUT_ORDER: InputChannel[] = [ InputChannel.InA, @@ -255,6 +257,12 @@ export const DELAY_RANGES: Record = { [DelayUnit.Feet]: [0, 766.329], }; +export const DELAY_UNIT_LABEL: Record = { + [DelayUnit.Millisecond]: 'ms', + [DelayUnit.Meter]: 'm', + [DelayUnit.Feet]: 'ft', +}; + // ------ Graph constants ------ const REAL_X_MIN = 20; const REAL_X_MAX = 20000; @@ -268,6 +276,22 @@ export const GRAPH_X_MIN = Math.floor(Math.pow(10, logMin - logRange * X_LOG_PAD export const GRAPH_X_MAX = Math.ceil(Math.pow(10, logMax + logRange * X_LOG_PADDING)); export const GRAPH_Y_MIN = -21; export const GRAPH_Y_MAX = 21; +export const Y_AXIS_STEP = 3; + +export const CHANNEL_COLORS = [ + '#4ade80', + '#60a5fa', + '#f472b6', + '#fbbf24', + '#a78bfa', + '#34d399', + '#f87171', + '#38bdf8', +]; + +export const INPUT_COLORS = buildColorMap(INPUT_ORDER); +export const OUTPUT_COLORS = buildColorMap(OUTPUT_ORDER); +export const GRAPH_FREQS = logSpace(GRAPH_X_MIN, GRAPH_X_MAX, 1000); // ------ App ------ export type SidebarId = diff --git a/src/features/dsp408/utils/graph/draw.ts b/src/features/dsp408/utils/graph/draw.ts index e97b575..438c5ca 100644 --- a/src/features/dsp408/utils/graph/draw.ts +++ b/src/features/dsp408/utils/graph/draw.ts @@ -1,3 +1,4 @@ +import { CHANNEL_COLORS } from '../../constants'; import { CurvePoint, CurveStyle, Curve, DEFAULT_CURVE_COLORS, sumCurvePoints } from './curve'; export { sumCurvePoints }; @@ -96,3 +97,11 @@ export function drawCurves({ ctx, toPx, xMin, xMax, yMin, yMax, curves }: DrawCu drawSingleCurve(ctx, toPx, curve.points, xMin, xMax, yMin, yMax, style); } } + +export function buildColorMap(channels: T[]): Record { + const map = {} as Record; + channels.forEach((ch, i) => { + map[ch] = CHANNEL_COLORS[i % CHANNEL_COLORS.length]; + }); + return map; +}