Files
feuerwerk/tests/core/pitch-player.test.ts
T
adminandClaude Opus 5.5 8a4e4a6762
game-maker / suite (push) Failing after 2m40s
game-maker / gate (push) Successful in 3m14s
Play sounds as notes of an instrument asset #331
A sound could only be played as it was, one at a time. An instrument
is a node graph that makes a note a sound: Notes nodes bring the note
in, Samples play sound assets for it, the effects of a sound FX shape
it and the Output sums the layers. Its tab plays it from a keyboard,
a behavior with Play note. It is what patterns and songs (#330) will
play.

- New asset type instrument, under the heading Music, not placeable.
  A new one starts as Notes into a Sample into the Output.
- Notes are counted as MIDI does: 60 is C4, names like C#4, sharps
  only, the lowest C-1. Notes has From and To, outputs any, note,
  frequency and velocity, and with Single notes a trigger per note,
  each with an optional name for a drum kit.
- New port type trigger, only into the trigger of a source. Inputs
  can take any number of edges now (PortDef.many): a trigger, and
  the Output, which plays everything into it side by side.
- Sample plays a sound asset. Follow note shifts it by note - root
  through the pitch worklet of #317, so it keeps its length. Gate
  fades it out at the end of the note, One-shot plays through. Loop
  runs between Loop start and Loop end, also in the worklet.
- Parameters declares the instrument's own parameters with a
  default. Sound FX sends the sound through a sound FX asset, its
  parameters as inputs, the sound set in where its Input was.
- compileInstrument walks the graph once per note. A trigger is on
  only for the notes it stands for, a silent source silences what it
  alone feeds, a Mix with a silent side is the other side. velocity
  and the parameters stay open for the voice. Builds are cached per
  note until the graph or a sound or FX it names changes.
- The player takes a note as one voice of several layers, each way
  with its own file and volume. release fades the gated ways out,
  one-shots play on. playNote can release after a duration.
- Play note plays one note with velocity, volume, duration and
  detached, and hands out voice for Set sound FX parameters and
  Stop sound. Velocity is always loudness. Instrument parameters
  picks values for the instrument's parameters like Sound FX
  parameters does for an FX, renames follow.
- The tab has the graph, a header field per parameter, Stop, and
  128 keys below. Keys without sound are grey. A key plays while it
  is held. Focused, the computer keyboard plays as in a tracker by
  KeyboardEvent.code, the arrows move the octave.

Findings:
- A label of the keys set fill on the shared small text style and
  darkened every small label of the editor. Caught in a screenshot
  before the commit, the keys have a style of their own now.
- Suite task 523 on cf6799a failed two checks of e2e 34 that timed
  audio by the page's clock: a 2.95 s echo tail was gone after 1 s,
  and Quad in was further along than 0.7 s allow. Both time on the
  audio clock now through a new hook __gm.sound.clock().

Tests:
- instrument.test.ts (new, 22): notes, asset, palette, round trip,
  compile per note, drum kit, zones, Mix, velocity and parameters,
  Sound FX node with rename, problems, cache, Play note, the keys
- sound-library +7 (layers, release, duration, early release, loop
  region in buffer and worklet), pitch-player +2 (loop region),
  node-catalog +4, graph adjusted. 582 tests in the touched files.
- e2e 4 and 34 on melektaus-atx: ALL PASSED, 34 with 44 checks (+7)
- Gate on melektaus-atx: CHECK EXIT 0, 243 test files, 3750 tests
- Counter-checks: any always on, release ending one-shots, Sound FX
  inputs ignored, loop region ignored. 3, 2, 1 and 1 tests failed.
- Browser probe on stein2: keys, held key, Single notes names and
  the header looked at in screenshots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 10:07:40 +02:00

164 lines
7.1 KiB
TypeScript

/**
* The pitch of a sound FX in real time (#317): a sound played higher or lower keeps its length, also
* while the pitch moves. Measured on a sine, whose pitch is the number of times it rises through zero
* per second.
*/
import { describe, expect, it } from 'vitest';
import { PitchPlayer } from '../../src/core/soundfx/pitchPlayer';
const RATE = 48000;
const BLOCK = 128;
/** A frame at 48 kHz is 2048 samples, and a frame takes the pitch at the sample it starts on. */
const HALF_FRAME = 1024;
function sine(frequency: number, seconds: number, rate = RATE): Float32Array {
const samples = new Float32Array(Math.round(rate * seconds));
for (let i = 0; i < samples.length; i++) samples[i] = Math.sin((2 * Math.PI * frequency * i) / rate);
return samples;
}
/** Plays the sound to its end block by block, as a worklet does, the pitch from `semitones(sample)`. */
function render(
sound: Float32Array,
semitones: number | ((at: number) => number),
{
step = 1,
loop = false,
blocks = Infinity,
}: { step?: number; loop?: boolean | { start: number; end: number }; blocks?: number } = {},
): Float32Array {
const player = new PitchPlayer([sound], step, RATE, loop);
const out: number[] = [];
const block = new Float32Array(BLOCK);
const pitch = new Float32Array(BLOCK);
for (let b = 0; b < blocks; b++) {
for (let i = 0; i < BLOCK; i++)
pitch[i] = typeof semitones === 'number' ? semitones : semitones(out.length + i);
const going = player.render([block], typeof semitones === 'number' ? [semitones] : pitch);
out.push(...block);
if (!going) break;
}
return Float32Array.from(out);
}
/** Rises through zero per second between two samples. */
function frequencyOf(
samples: Float32Array,
from = samples.length / 4,
to = (samples.length * 3) / 4,
): number {
let rises = 0;
for (let i = Math.floor(from) + 1; i < to; i++) if (samples[i - 1]! < 0 && samples[i]! >= 0) rises++;
return rises / ((to - from) / RATE);
}
const loudest = (samples: Float32Array) => samples.reduce((max, v) => Math.max(max, Math.abs(v)), 0);
/** The last sample that is not silence, plus one: how long the sound is heard. */
function heard(samples: Float32Array): number {
let last = samples.length;
while (last > 0 && Math.abs(samples[last - 1]!) <= 1e-4) last--;
return last;
}
describe('the pitch player', () => {
it.each([
[12, 880],
[-12, 220],
[7, 440 * 2 ** (7 / 12)],
[0.5, 440 * 2 ** (0.5 / 12)],
[-30.5, 440 * 2 ** (-30.5 / 12)],
])('moves a 440 Hz tone by %d semitones to about %d Hz, at the same length', (semitones, expected) => {
const tone = sine(440, 1);
const shifted = render(tone, semitones);
expect(shifted.length).toBeGreaterThanOrEqual(tone.length);
expect(heard(shifted)).toBeLessThanOrEqual(tone.length);
expect(heard(shifted)).toBeGreaterThan(tone.length - 50);
expect(frequencyOf(shifted)).toBeGreaterThan(expected * 0.98);
expect(frequencyOf(shifted)).toBeLessThan(expected * 1.02);
expect(loudest(shifted)).toBeLessThan(1.2);
expect(loudest(shifted.subarray(RATE / 4, (RATE * 3) / 4))).toBeGreaterThan(0.7);
});
it('plays a sound at 0 semitones as it is, from the very first sample', () => {
const tone = sine(440, 0.5);
const same = render(tone, 0);
let off = 0;
for (let i = 0; i < tone.length; i++) off = Math.max(off, Math.abs(same[i]! - tone[i]!));
expect(off).toBeLessThan(1e-5);
expect(heard(same)).toBe(tone.length);
});
it('starts the pitched sound at once rather than after a frame', () => {
const shifted = render(sine(440, 0.5), 12);
expect(loudest(shifted.subarray(0, 480))).toBeGreaterThan(0.3);
});
it('keeps the length while the pitch sweeps down, the pitch half a frame behind the sweep', () => {
// A blip of a quarter second taken down two octaves over its own length.
const tone = sine(880, 0.25);
const sweep = (at: number) => (-24 * at) / tone.length;
const swept = render(tone, sweep);
expect(heard(swept)).toBeLessThanOrEqual(tone.length);
expect(heard(swept)).toBeGreaterThan(tone.length - 50);
for (const at of [tone.length / 4, (tone.length * 3) / 4]) {
const expected = 880 * 2 ** (sweep(at - HALF_FRAME) / 12);
const heardThere = frequencyOf(swept, at - 1200, at + 1200);
expect(heardThere).toBeGreaterThan(expected * 0.95);
expect(heardThere).toBeLessThan(expected * 1.05);
}
});
it('reads a sound at its own rate: an 8 kHz tone comes out at its pitch and length', () => {
const tone = sine(440, 0.5, 8000);
const played = render(tone, 12, { step: 8000 / RATE });
expect(heard(played)).toBeGreaterThan(RATE / 2 - 50);
expect(heard(played)).toBeLessThanOrEqual(RATE / 2);
expect(frequencyOf(played)).toBeGreaterThan(880 * 0.98);
expect(frequencyOf(played)).toBeLessThan(880 * 1.02);
});
it('reads on across the end of a looping sound, so its seam does not fade out, and never ends', () => {
const tone = sine(500, 0.25);
const looped = render(tone, 12, { loop: true, blocks: Math.ceil((tone.length * 2) / BLOCK) });
for (let from = tone.length - 960; from < tone.length + 960; from += 96)
expect(loudest(looped.subarray(from, from + 96))).toBeGreaterThan(0.8);
expect(heard(looped)).toBe(looped.length);
expect(heard(render(tone, 12))).toBeLessThanOrEqual(tone.length);
});
it('goes from 0 semitones to a pitch and back without a dip, and then plays the sound as it is', () => {
const tone = sine(440, 2);
// Up to 5 over 0.1 s from a third in, back down over 0.1 s from two thirds.
const ramp = (at: number) => Math.max(0, Math.min(1, (at - RATE / 3) / (RATE / 10)));
const moved = render(tone, (at) => 5 * (ramp(at) - ramp(at - RATE / 3)));
for (let from = 0; from + 480 <= tone.length; from += 480)
expect(loudest(moved.subarray(from, from + 480))).toBeGreaterThan(0.8);
const up = frequencyOf(moved, RATE * 0.5, RATE * 0.6);
expect(up).toBeGreaterThan(440 * 2 ** (5 / 12) * 0.98);
expect(up).toBeLessThan(440 * 2 ** (5 / 12) * 1.02);
let off = 0;
for (let i = RATE * 1.5; i < RATE * 2 - 50; i++) off = Math.max(off, Math.abs(moved[i]! - tone[i]!));
expect(off).toBeLessThan(1e-5);
});
});
describe('a loop between two points (#331)', () => {
it('plays up to the loop end, then on from the loop start, never ending', () => {
// A ramp, so every sample says where in the sound it was read.
const ramp = Float32Array.from({ length: 48000 }, (_, i) => i / 48000);
const out = render(ramp, 0, { loop: { start: 12000, end: 24000 }, blocks: 400 });
expect(out.length).toBe(400 * BLOCK);
expect(out[20000]).toBeCloseTo(20000 / 48000, 3);
// 30000 is 6000 past the end: 6000 past the start.
expect(out[30000]).toBeCloseTo(18000 / 48000, 3);
expect(out[40000]).toBeCloseTo(16000 / 48000, 3);
});
it('takes a loop that does not fit the sound as the whole sound', () => {
const ramp = Float32Array.from({ length: 4800 }, (_, i) => i / 4800);
const out = render(ramp, 0, { loop: { start: 3000, end: 9000 }, blocks: 60 });
expect(out[6000]).toBeCloseTo(1200 / 4800, 3);
});
});