feat: add input meter bars
This commit is contained in:
@@ -9,17 +9,30 @@ export interface Meter {
|
||||
type MeterBarProps = {
|
||||
label: string;
|
||||
meter: Meter;
|
||||
variant: 'input' | 'output';
|
||||
/** Output variant only */
|
||||
min?: number;
|
||||
max?: number;
|
||||
segments?: number;
|
||||
/** Intermediate dB values to mark on the scale, e.g. [0] or [-20, -6, 0]. */
|
||||
/** Intermediate dB values to mark on the scale. Defaults depend on variant. */
|
||||
ticks?: number[];
|
||||
};
|
||||
|
||||
function segmentColor(posRatioFromBottom: number) {
|
||||
if (posRatioFromBottom > 0.9) return '#f87171'; // red — near clip
|
||||
if (posRatioFromBottom > 0.7) return '#fbbf24'; // yellow — hot
|
||||
return '#4ade80'; // green — normal
|
||||
const GREEN = '#4ade80';
|
||||
const HOT = '#fbbf24';
|
||||
const RED = '#f87171';
|
||||
const LIMIT_ORANGE = '#fb923c';
|
||||
const OFF = 'rgba(255,255,255,0.08)';
|
||||
|
||||
function outputSegmentColor(posRatioFromBottom: number) {
|
||||
if (posRatioFromBottom > 0.9) return RED; // red — near clip
|
||||
if (posRatioFromBottom > 0.7) return HOT; // yellow — hot
|
||||
return GREEN; // green — normal
|
||||
}
|
||||
|
||||
// Input: green up to 0 dB, amber for the "over" steps (0-6, 6-12)
|
||||
function inputSegmentColor(lowerDb: number) {
|
||||
return lowerDb >= 0 ? HOT : GREEN;
|
||||
}
|
||||
|
||||
// Formats a dB value with an explicit '+' sign for positive values (e.g. +12, -60, 0)
|
||||
@@ -29,49 +42,65 @@ function formatDb(v: number, decimals = 0) {
|
||||
return v > 0 ? `+${fixed}` : fixed;
|
||||
}
|
||||
|
||||
const INPUT_BOUNDS = [-60, -50, -40, -30, -20, -10, 0, 6, 12];
|
||||
const INPUT_TICKS = [-50, -40, -30, -20, -10, 0, 6];
|
||||
const OUTPUT_TICKS = [0, -50, -40, -30, -20, -10];
|
||||
|
||||
function MeterBar({
|
||||
label,
|
||||
meter,
|
||||
min = -60,
|
||||
max = +10,
|
||||
segments = 7,
|
||||
ticks = [0, -50, -40, -30, -20, -10],
|
||||
variant = 'output',
|
||||
min: minProp,
|
||||
max: maxProp,
|
||||
segments: segmentsProp,
|
||||
ticks: ticksProp,
|
||||
}: MeterBarProps) {
|
||||
const { rms, peak, clip } = meter;
|
||||
const isInput = variant === 'input';
|
||||
|
||||
// Maps a raw dB value to a 0..1 ratio from the bottom of the ladder, clamped to range.
|
||||
const ratioOf = (v: number) => {
|
||||
const outMin = minProp ?? -60;
|
||||
const outMax = maxProp ?? 10;
|
||||
const outSegments = segmentsProp ?? 7;
|
||||
|
||||
const bounds: number[] = isInput
|
||||
? INPUT_BOUNDS
|
||||
: Array.from(
|
||||
{ length: outSegments + 1 },
|
||||
(_, i) => outMin + ((outMax - outMin) * i) / outSegments,
|
||||
);
|
||||
|
||||
const min = bounds[0];
|
||||
const max = bounds[bounds.length - 1];
|
||||
const segments = bounds.length - 1;
|
||||
const ticks = ticksProp ?? (isInput ? INPUT_TICKS : OUTPUT_TICKS);
|
||||
|
||||
// Maps a dB value to a position in rows from the bottom (0..segments)
|
||||
const posOf = (v: number) => {
|
||||
const clamped = Math.min(max, Math.max(min, v));
|
||||
return (clamped - min) / (max - min);
|
||||
for (let i = 0; i < segments; i++) {
|
||||
if (clamped <= bounds[i + 1]) {
|
||||
return i + (clamped - bounds[i]) / (bounds[i + 1] - bounds[i]);
|
||||
}
|
||||
}
|
||||
return segments;
|
||||
};
|
||||
|
||||
// --- RMS ladder (drives which LEDs are lit) ---
|
||||
const rmsRatio = ratioOf(rms);
|
||||
const litCount = Math.round(rmsRatio * segments);
|
||||
|
||||
// --- Peak marker (position only, doesn't affect lit count) ---
|
||||
const peakRatio = ratioOf(peak);
|
||||
|
||||
// --- Intermediate tick marks on the scale, e.g. a 0 dB reference line ---
|
||||
const litCount = Math.round(posOf(rms));
|
||||
const visibleTicks = ticks.filter((t) => t > min && t < max);
|
||||
|
||||
// --- Red "over" LED: lit whenever rms or peak crosses max ---
|
||||
// Red "clip" LED: lit whenever rms or peak crosses max
|
||||
const isOverMax = rms >= max || peak >= max;
|
||||
|
||||
// --- Orange "limit" LED: lit strictly from the meter's own clip flag.
|
||||
// clip is optional — some meters simply don't report a limiter/clip state.
|
||||
// Orange "limit" LED: output only
|
||||
const showLimitRow = !isInput;
|
||||
const hasLimiter = clip !== undefined;
|
||||
const isClipping = clip === true;
|
||||
|
||||
// The over-led and limit-led are rendered as two extra rows in the very
|
||||
// same flex column as the level segments (see meter-bar__leds below), so
|
||||
// they get identical row height automatically instead of a hardcoded one.
|
||||
// Because of that, the ladder itself only occupies `segments / totalRows`
|
||||
// of the column — the rest of the math (peak marker, scale ticks) has to
|
||||
// scale down to match.
|
||||
const totalRows = segments + 2;
|
||||
const totalRows = segments + (showLimitRow ? 2 : 1);
|
||||
const ladderFraction = segments / totalRows;
|
||||
const toColumnPercent = (ratio: number) => ratio * ladderFraction * 100;
|
||||
const toColumnPercent = (pos: number) => (pos / segments) * ladderFraction * 100;
|
||||
|
||||
// const peakBottomPercent = toColumnPercent(posOf(peak));
|
||||
|
||||
return (
|
||||
<div className="meter-bar">
|
||||
@@ -80,54 +109,50 @@ function MeterBar({
|
||||
<div className="meter-bar__body">
|
||||
<div className="meter-bar__leds-wrap">
|
||||
<div className="meter-bar__leds">
|
||||
<div
|
||||
className="meter-bar__indicator-row"
|
||||
style={{ visibility: hasLimiter ? 'visible' : 'hidden' }}
|
||||
>
|
||||
{showLimitRow && (
|
||||
<div
|
||||
className="meter-bar__led meter-bar__led--indicator"
|
||||
style={{
|
||||
backgroundColor: isClipping ? '#fb923c' : 'rgba(255,255,255,0.08)',
|
||||
}}
|
||||
title="Limit"
|
||||
/>
|
||||
<span>Limit</span>
|
||||
</div>
|
||||
className="meter-bar__indicator-row"
|
||||
style={{ visibility: hasLimiter ? 'visible' : 'hidden' }}
|
||||
>
|
||||
<div
|
||||
className="meter-bar__led meter-bar__led--indicator"
|
||||
style={{ backgroundColor: isClipping ? LIMIT_ORANGE : OFF }}
|
||||
title="Limit"
|
||||
/>
|
||||
<span>Limit</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="meter-bar__indicator-row">
|
||||
<div
|
||||
className="meter-bar__led meter-bar__led--indicator"
|
||||
style={{
|
||||
backgroundColor: isOverMax ? '#f87171' : 'rgba(255,255,255,0.08)',
|
||||
}}
|
||||
style={{ backgroundColor: isOverMax ? RED : OFF }}
|
||||
title="Clip"
|
||||
/>
|
||||
<span>Clip</span>
|
||||
</div>
|
||||
|
||||
{Array.from({ length: segments }).map((_, i) => {
|
||||
// i=0 renders at the top; convert to "position from bottom" for lighting + color
|
||||
const posFromBottom = segments - 1 - i;
|
||||
const lit = posFromBottom < litCount;
|
||||
const color = isInput
|
||||
? inputSegmentColor(bounds[posFromBottom])
|
||||
: outputSegmentColor(posFromBottom / segments);
|
||||
return (
|
||||
<div
|
||||
key={i}
|
||||
className="meter-bar__led"
|
||||
style={{
|
||||
backgroundColor: lit
|
||||
? segmentColor(posFromBottom / segments)
|
||||
: 'rgba(255,255,255,0.08)',
|
||||
}}
|
||||
style={{ backgroundColor: lit ? color : OFF }}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
<div
|
||||
{/* <div
|
||||
className="meter-bar__peak-marker"
|
||||
style={{ bottom: `${toColumnPercent(peakRatio)}%` }}
|
||||
style={{ bottom: `${peakBottomPercent}%` }}
|
||||
title={`Peak: ${formatDb(peak, 1)} dB`}
|
||||
/>
|
||||
/> */}
|
||||
</div>
|
||||
|
||||
<div className="meter-bar__scale">
|
||||
@@ -141,7 +166,7 @@ function MeterBar({
|
||||
<span
|
||||
key={t}
|
||||
className="meter-bar__scale-tick"
|
||||
style={{ bottom: `${toColumnPercent(ratioOf(t))}%` }}
|
||||
style={{ bottom: `${toColumnPercent(posOf(t))}%` }}
|
||||
>
|
||||
{formatDb(t)}
|
||||
</span>
|
||||
@@ -162,4 +187,4 @@ function MeterBar({
|
||||
);
|
||||
}
|
||||
|
||||
export default MeterBar;
|
||||
export default MeterBar;
|
||||
Reference in New Issue
Block a user