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>
102 lines
3.2 KiB
TypeScript
102 lines
3.2 KiB
TypeScript
/**
|
||
* Musical time (#332): ticks, tempo and meter, and the seconds they come to. Ticks are whole numbers
|
||
* of a quarter note's share, so a grid, a triplet and a quintuplet all land on one exactly.
|
||
*/
|
||
|
||
/** As MIDI files and most DAWs count: a 1/32 triplet is 40, a 1/16 quintuplet 96. */
|
||
export const TICKS_PER_QUARTER = 480;
|
||
|
||
/** A tempo always counts quarter notes per minute, in 6/8 as in 4/4. */
|
||
export const DEFAULT_TEMPO = 120;
|
||
export const MIN_TEMPO = 20;
|
||
export const MAX_TEMPO = 400;
|
||
|
||
/** A time signature: `beats` notes of the value `unit` to the bar – 3/4, 6/8, 7/8. */
|
||
export interface Meter {
|
||
beats: number;
|
||
unit: number;
|
||
}
|
||
|
||
export const COMMON_TIME: Meter = { beats: 4, unit: 4 };
|
||
const UNITS = [1, 2, 4, 8, 16, 32];
|
||
const MAX_BEATS = 32;
|
||
|
||
/** A meter as written, or 4/4 where it is not one: a beat is a power of two, a bar 1 to 32 of them. */
|
||
export function heldMeter(meter: Meter): Meter {
|
||
const { beats, unit } = meter;
|
||
if (!Number.isInteger(beats) || beats < 1 || beats > MAX_BEATS || !UNITS.includes(unit)) return COMMON_TIME;
|
||
return { beats, unit };
|
||
}
|
||
|
||
/** The time signatures a song or a pattern is written in (#363, #364), one row for both numbers. */
|
||
const SIGNS: readonly string[] = [
|
||
'1/2',
|
||
'2/2',
|
||
'3/2',
|
||
'1/4',
|
||
'2/4',
|
||
'3/4',
|
||
'4/4',
|
||
'5/4',
|
||
'6/4',
|
||
'5/8',
|
||
'6/8',
|
||
'7/8',
|
||
'8/8',
|
||
'9/8',
|
||
];
|
||
export const DEFAULT_SIGN = '4/4';
|
||
export const SIGN_OPTIONS = SIGNS.map((sign) => ({ value: sign, label: sign }));
|
||
|
||
/** The meter of a sign; 4/4 where it is not one of `SIGNS`. */
|
||
export function signMeter(sign: string): Meter {
|
||
const [beats, unit] = (SIGNS.includes(sign) ? sign : DEFAULT_SIGN).split('/');
|
||
return heldMeter({ beats: Number(beats), unit: Number(unit) });
|
||
}
|
||
|
||
export function signOf(meter: Meter): string {
|
||
return `${meter.beats}/${meter.unit}`;
|
||
}
|
||
|
||
/** A tempo between `MIN_TEMPO` and `MAX_TEMPO`; 0 and anything that is not a number is the default. */
|
||
export function heldTempo(tempo: number): number {
|
||
if (!Number.isFinite(tempo) || tempo <= 0) return DEFAULT_TEMPO;
|
||
return Math.max(MIN_TEMPO, Math.min(MAX_TEMPO, tempo));
|
||
}
|
||
|
||
export function ticksPerBeat(meter: Meter): number {
|
||
return (TICKS_PER_QUARTER * 4) / meter.unit;
|
||
}
|
||
|
||
export function ticksPerBar(meter: Meter): number {
|
||
return meter.beats * ticksPerBeat(meter);
|
||
}
|
||
|
||
export function secondsOfTicks(ticks: number, tempo: number): number {
|
||
return (ticks * 60) / (tempo * TICKS_PER_QUARTER);
|
||
}
|
||
|
||
export function ticksOfSeconds(seconds: number, tempo: number): number {
|
||
return (seconds * tempo * TICKS_PER_QUARTER) / 60;
|
||
}
|
||
|
||
/** Seconds of a number of quarter notes: 1 is a quarter, 0.5 an eighth, 0.75 a dotted eighth. */
|
||
export function secondsOfQuarters(quarters: number, tempo: number): number {
|
||
return (quarters * 60) / tempo;
|
||
}
|
||
|
||
/** Where a tick stands as a musician counts it: bar and beat from 1, the tick inside the beat from 0. */
|
||
export interface Position {
|
||
bar: number;
|
||
beat: number;
|
||
tick: number;
|
||
}
|
||
|
||
export function positionOf(ticks: number, meter: Meter): Position {
|
||
const at = Math.max(0, Math.floor(ticks));
|
||
const bar = ticksPerBar(meter);
|
||
const beat = ticksPerBeat(meter);
|
||
const inBar = at % bar;
|
||
return { bar: Math.floor(at / bar) + 1, beat: Math.floor(inBar / beat) + 1, tick: inBar % beat };
|
||
}
|