Play sounds as notes of an instrument asset #331
game-maker / suite (push) Failing after 2m40s
game-maker / gate (push) Successful in 3m14s

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>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-06 10:07:40 +02:00
1 parent cf6799a784
commit 8a4e4a6762
54 files changed
+2561 -143

No files matched your search

+20 -5
View File
@@ -27,6 +27,20 @@ export interface FxDelay {
pingPong: boolean;
}
/**
* Where a way of an instrument's note gets its sound (#331): a file of its own, how loud and how it
* loops, and whether it ends with the note (`gate`) or plays through. A way of a sound FX has none –
* it plays the sound the voice was started with. `loopEnd` 0 is the end of the sound.
*/
export interface FxSource {
file: string;
volume: number;
loop: boolean;
loopStart: number;
loopEnd: number;
gate: boolean;
}
/**
* A sound on its way through the graph: how many semitones it is played up or down, and what it
* passes, in order. `gain` is a factor, a filter's `gain` is in dB as Web Audio takes it. `shifted`
@@ -35,7 +49,7 @@ export interface FxDelay {
* of what goes on through `stages`.
*/
export type FxChain =
| { pitch: number; shifted: boolean; stages: readonly FxStage[] }
| { pitch: number; shifted: boolean; stages: readonly FxStage[]; source?: FxSource }
| { mix: number; a: FxChain; b: FxChain; stages: readonly FxStage[] };
export const DRY: FxChain = { pitch: 0, shifted: false, stages: [] };
@@ -66,7 +80,7 @@ export type FxStagePlan =
/** A chain with its parameters still open: compiled once, filled in for every voice by `fxChainOf`. */
export type FxPlan =
| { pitch: readonly FxAmount[]; stages: readonly FxStagePlan[] }
| { pitch: readonly FxAmount[]; stages: readonly FxStagePlan[]; source?: FxSource }
| { mix: FxAmount; a: FxPlan; b: FxPlan; stages: readonly FxStagePlan[] };
export const EMPTY_PLAN: FxPlan = { pitch: [], stages: [] };
@@ -130,12 +144,12 @@ const isNone = (amount: FxAmount) => amount.param === undefined && amount.value
const arriving = (sounds: Readonly<Record<string, FxPlan>>) => sounds.in ?? EMPTY_PLAN;
function withStages(plan: FxPlan, stages: readonly FxStagePlan[]): FxPlan {
export function withStages(plan: FxPlan, stages: readonly FxStagePlan[]): FxPlan {
return stages.length === 0 ? plan : { ...plan, stages: [...plan.stages, ...stages] };
}
/** A pitch is made where the sound is read, so behind a Mix it goes to both sounds. */
function withPitch(plan: FxPlan, semitones: FxAmount): FxPlan {
export function withPitch(plan: FxPlan, semitones: FxAmount): FxPlan {
if ('mix' in plan) return { ...plan, a: withPitch(plan.a, semitones), b: withPitch(plan.b, semitones) };
return { ...plan, pitch: [...plan.pitch, semitones] };
}
@@ -352,7 +366,8 @@ export function fxChainOf(plan: FxPlan, params: FxParams = {}): FxChain {
const stages = stagesOf(plan, params);
if (!('mix' in plan)) {
const pitch = plan.pitch.reduce((sum, amount) => sum + amountOf(amount, params), 0);
return { pitch, shifted: plan.pitch.length > 0, stages };
const chain = { pitch, shifted: plan.pitch.length > 0, stages };
return plan.source ? { ...chain, source: plan.source } : chain;
}
return {
mix: amountOf(plan.mix, params),
+15 -6
View File
@@ -57,16 +57,19 @@ export class PitchPlayer {
private primed = false;
/** How many samples of the output the sound lasts; a looping one never ends. */
private readonly end: number;
/** Where a loop runs in the samples of the sound; null for a sound that does not loop. */
private readonly loop: { start: number; end: number } | null;
/**
* `channels` are the samples of the sound, `step` how many of them pass for one sample of the
* output (the sound's rate over the output's) and `rate` the output's rate, which sets the frame.
* `loop` repeats the whole sound, or the samples from `start` up to `end` (#331) once it gets there.
*/
constructor(
private readonly channels: readonly Float32Array[],
private readonly step: number,
rate: number,
private readonly loop: boolean,
loop: boolean | { start: number; end: number },
) {
// About 43 ms: a frame holds a few periods of a low note.
this.size = 2 ** Math.round(Math.log2(rate * 0.043));
@@ -91,6 +94,11 @@ export class PitchPlayer {
this.frameAt = -this.size;
const length = channels[0]?.length ?? 0;
this.end = loop ? Infinity : Math.ceil(length / step);
const region = loop === true ? { start: 0, end: length } : loop || null;
this.loop =
region && region.start >= 0 && region.end <= length && region.start < region.end
? region
: region && { start: 0, end: length };
}
/**
@@ -256,15 +264,16 @@ export class PitchPlayer {
private readFrame(c: number, from: number): Float64Array {
const { reading, size, step, loop } = this;
const samples = this.channels[c]!;
const length = samples.length;
const end = loop ? loop.end : samples.length;
for (let j = 0; j < size; j++) {
let position = (from + j) * step;
if (position < 0 || position >= length) {
if (!loop) {
if (position < 0 || position >= end) {
if (!loop || position < 0) {
reading[j] = 0;
continue;
}
position -= Math.floor(position / length) * length;
const span = loop.end - loop.start;
position = loop.start + ((position - loop.start) % span);
}
const below = Math.floor(position);
const fraction = position - below;
@@ -272,7 +281,7 @@ export class PitchPlayer {
reading[j] = samples[below]!;
continue;
}
const next = below + 1 < length ? samples[below + 1]! : loop ? samples[0]! : 0;
const next = below + 1 < end ? samples[below + 1]! : loop ? samples[loop.start]! : 0;
reading[j] = samples[below]! * (1 - fraction) + next * fraction;
}
return reading;