Refactoring round 10 covers everything since round 9 (808e1ee): 110 commits with about 47 000 new lines, nearly all of them sound, sound FX, instruments, patterns and songs. Behaviour is unchanged. Duplicate code (jscpd over src, 50 tokens, TypeScript only, measured on808e1eeand here alike: 47 clones / 426 lines before, 33 / 245 after; round 9 itself ended at 22 / 160 by this count): - PreviewParams (new, editor/soundfx) is the header's parameter bar of the Sound FX and the Instrument tab: values, defaults, fields and the voices a moved field reaches. Both tabs had it line for line. - fxWorklet: an Effect base runs the bit crusher, phaser, flanger, limiter, LFO and bus shifter, a Source base the pitch player and the Chip. Start is the one wait for a voice's first sample, which four processors wrote out each. - nodes/shared has checkPort, detachedPort, fxParamsPort and letRingPort. The Play and Stop nodes of sounds, notes, patterns and songs spelled each port out, up to five times. - parameterValues builds both Sound FX parameters and Instrument parameters, which differed in name and text only. - SetLoopCommand sets the ruler loop of a pattern or a song. It replaces SetPatternLoopCommand and SetSongLoopCommand. - addLink in music/routing is the one rule for a link that both the song routing and the Master routing keep. - eqCurves works out each band and their sum for the EQ tab, which did that in its drawing code. It replaces eqCurveDb, which only a test called. - Smaller ones: the eight neighbour directions of a tile cell, and drawFloatingBox for the shadowed box of Popup and FloatWindow. - Left on purpose: Play pattern and Play song stay two parallel node definitions, and the smaller clones under ten lines stay. Architecture: - SoundLibrary.ts goes from 2897 to 2188 lines. What a stage of a sound FX becomes as Web Audio nodes, and how the knobs of a playing voice move, is now viewport/fxNodes.ts. None of it keeps state of the library. Dead code (TS language service, references per export and member): - soundRefIds, LFO_SHAPE_INDEX, TREMOLO_ROWS, VIBRATO_ROWS and REVERB_ROWS had no reader. The four routing node defs are registered without an unused exported name. - soundFileOf and portTypeOf were called by tests only. The tests now ask findSound and portOf, which the editor uses. DRY moves from src into the one test that uses it. - 51 values used only in their own file lose their `export`. Types and the node catalogues stay exported. - Kept on purpose: neighboursOf is the cell by cell reading the indexed neighboursIn is checked against, and PitchShifter.latency is the test's one view of how late the shifter is. Comments and docs: - Seven comments told history ("no longer", "any more", "as before", "of before #354"). Nine comment blocks ran far past the line width. Proof that this changed no code: the bundle was md5 identical, and the touched tests and e2e modules were identical after esbuild. - README: sound FX, instrument, pattern, song and bus in the asset list, the pages Instruments, Patterns and Songs, 1919 checks. - Wiki: see its own commit. Tests: Gate (melektaus-atx) 274 files, 4236 tests, CHECK EXIT 0 e2e 34, 64, 65 ALL PASSED, 180 checks Unit, touched areas 16 files, 804 tests No check was added, the e2e sum stays at 1919. Counter-checks, each put back by copy and compared with cmp: - addLink without the loop rule: 2 tests fail, one per routing. - eqCurves with the sum of the last band only: 1 test fails. - Start in the worklet never waiting: e2e 34 fails in exactly the four checks that hang on a voice's first sample (Flanger, Tremolo, Chip, a pitched note planned ahead). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
92 lines
3.0 KiB
TypeScript
92 lines
3.0 KiB
TypeScript
import { clamp } from '../../ui';
|
|
|
|
/** The scale of a level meter (#359) in dBFS: what is quieter shows nothing, past 0 it clips. */
|
|
const METER_MIN_DB = -48;
|
|
const METER_MAX_DB = 6;
|
|
/** From here on a meter is yellow; past 0 dBFS it is red. */
|
|
export const METER_WARM_DB = -6;
|
|
/** Seconds the loudest peak stays put before it falls. */
|
|
export const PEAK_HOLD = 1.5;
|
|
/** dB a second a level and a peak fall. */
|
|
export const METER_FALL = 24;
|
|
|
|
/** One side of a meter: what it shows now, the loudest peak and when it came, and whether it clipped. */
|
|
export interface MeterSide {
|
|
level: number;
|
|
peak: number;
|
|
peakAt: number;
|
|
clipped: boolean;
|
|
}
|
|
|
|
export const QUIET_SIDE: MeterSide = { level: -Infinity, peak: -Infinity, peakAt: 0, clipped: false };
|
|
|
|
export function gainToDb(gain: number): number {
|
|
return gain > 0 ? 20 * Math.log10(gain) : -Infinity;
|
|
}
|
|
|
|
/** How far up the meter a level is, 0 to 1. */
|
|
export function meterShare(db: number): number {
|
|
return clamp((db - METER_MIN_DB) / (METER_MAX_DB - METER_MIN_DB), 0, 1);
|
|
}
|
|
|
|
export type MeterZone = 'ok' | 'warm' | 'hot';
|
|
|
|
export function meterZone(db: number): MeterZone {
|
|
return db > 0 ? 'hot' : db >= METER_WARM_DB ? 'warm' : 'ok';
|
|
}
|
|
|
|
/**
|
|
* A side `dt` seconds on, at `now`, with `gain` the loudest sample since: the level jumps up to it
|
|
* and falls back slowly, the peak holds `PEAK_HOLD` seconds and then falls the same way. A sample
|
|
* past full scale lights the clip lamp, and it stays lit until it is cleared.
|
|
*/
|
|
export function stepSide(side: MeterSide, gain: number, now: number, dt: number): MeterSide {
|
|
const db = gainToDb(gain);
|
|
const fall = (from: number) => {
|
|
const to = from - METER_FALL * Math.max(0, dt);
|
|
return to < METER_MIN_DB ? -Infinity : to;
|
|
};
|
|
const level = Math.max(db, fall(side.level));
|
|
const fresh = db >= side.peak;
|
|
const held = now - side.peakAt < PEAK_HOLD;
|
|
return {
|
|
level,
|
|
peak: fresh ? db : held ? side.peak : Math.max(db, fall(side.peak)),
|
|
peakAt: fresh ? now : side.peakAt,
|
|
clipped: side.clipped || gain > 1,
|
|
};
|
|
}
|
|
|
|
/** Both sides of a meter, left and right. */
|
|
export type StereoMeter = readonly [MeterSide, MeterSide];
|
|
|
|
export const QUIET_METER: StereoMeter = [QUIET_SIDE, QUIET_SIDE];
|
|
|
|
export function stepMeter(
|
|
meter: StereoMeter,
|
|
gains: readonly [number, number],
|
|
now: number,
|
|
dt: number,
|
|
): StereoMeter {
|
|
return [stepSide(meter[0], gains[0], now, dt), stepSide(meter[1], gains[1], now, dt)];
|
|
}
|
|
|
|
/** What it shows goes; only the clip lamp stays as it was. */
|
|
export function hushMeter(meter: StereoMeter): StereoMeter {
|
|
return [
|
|
{ ...QUIET_SIDE, clipped: meter[0].clipped },
|
|
{ ...QUIET_SIDE, clipped: meter[1].clipped },
|
|
];
|
|
}
|
|
|
|
export function meterClipped(meter: StereoMeter): boolean {
|
|
return meter[0].clipped || meter[1].clipped;
|
|
}
|
|
|
|
/** "-3.2", "+1.5", "-inf": a peak as the Out meter writes it, in letters the UI font has. */
|
|
export function peakText(db: number): string {
|
|
if (!Number.isFinite(db)) return '-inf';
|
|
const shown = Math.round(db * 10) / 10;
|
|
return `${shown > 0 ? '+' : ''}${shown.toFixed(1)}`;
|
|
}
|