Play and stop a pattern from a behavior in the game #336
A pattern could only be heard in its tab. Songs come with #334, and only then can #336 play them in a game. Until then a game plays a single pattern, looped or once, through two new nodes. - Play pattern plays the whole pattern through its instrument at its own tempo. The loop on the ruler is only for editing. Inputs are pattern, volume, loop (on by default) and detached. It gives out playback, 0 where there is no instrument or no note. - Stop pattern stops one pattern, every pattern for none, or one playback by its number. Let ring lets notes with an Envelope fall away in their release. - A playing is numbered and owned like a voice. Destroying the object, Stop sound with nothing named and the end of the run stop it. Stop sound with a voice takes a playback too. - Each playing runs on a scheduler of its own (core/music/playbacks). It waits for its files first, and a stop that came earlier wins. - The scheduler has an end for a sequence that does not loop. It stops waking there and is done once its last notes are. - The worker clock moved to viewport/wake.ts, so SoundLibrary can use it without importing music.ts. Tests: - pattern-play.test.ts (new): 8 tests, node catalog +7 cases - e2e 64 (melektaus-atx): ALL PASSED, 23 checks (+3) - Gate (melektaus-atx): CHECK EXIT 0, 248 files, 3854 tests - Counter-checks: no end in the scheduler, and no owner for the playing, each fail 1 test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4ea752feef
commit
2a3b90b017
19 files changed
+745
-28
No files matched your search
@@ -17,10 +17,13 @@ import {
|
||||
} from '../core/soundfx/nodes';
|
||||
import { envelopeLevel, modCurve, releaseTime, SETTLE } from '../core/instrument/envelope';
|
||||
import type { EaseShare } from '../core/model/animation';
|
||||
import { Playbacks, type SequencePlay } from '../core/music/playbacks';
|
||||
import type { ScheduledNote } from '../core/music/Scheduler';
|
||||
import { DEFAULT_TEMPO } from '../core/music/time';
|
||||
import type { AudioSink, NoteTiming } from '../runtime/systems/Audio';
|
||||
import type { PitchOptions } from './pitchWorklet';
|
||||
import pitchWorklet from './pitchWorklet.ts?worker&url';
|
||||
import { wake } from './wake';
|
||||
|
||||
const DEFAULT_BASE = '/game-maker/data/sounds';
|
||||
/** Seconds a changed parameter takes to get most of the way there: quick, but no click (#315). */
|
||||
@@ -226,6 +229,17 @@ export class SoundLibrary implements AudioSink {
|
||||
/** The same counter per owner, for the same reason – a behavior destroyed while bytes were in
|
||||
* flight must not be given its voice afterwards. */
|
||||
private readonly ownerStops = new Map<string, number>();
|
||||
/** The patterns a game plays, each on a scheduler of its own. */
|
||||
private readonly sequences = new Playbacks({
|
||||
now: () => this.time(),
|
||||
latency: () => this.latency(),
|
||||
playing: (id) => this.playing(id),
|
||||
stop: (voices, letRing) => {
|
||||
for (const id of voices) this.stopVoice(id, letRing);
|
||||
},
|
||||
wake,
|
||||
prepare: (layers) => this.prepare(layers),
|
||||
});
|
||||
|
||||
/** Where the files are served from. Set once at start-up, before anything plays. */
|
||||
configure(base: string): void {
|
||||
@@ -266,6 +280,29 @@ export class SoundLibrary implements AudioSink {
|
||||
return this.begin({ id: 0, file: name, files, volume, loop: false, owner, fx, duration, at });
|
||||
}
|
||||
|
||||
/** A sequence without notes plays nothing, and is given no number. */
|
||||
playSequence(name: string, play: SequencePlay, owner = ''): number {
|
||||
if (play.sequence.notes.length === 0) return 0;
|
||||
const id = this.nextId++;
|
||||
this.sequences.start(id, name, play, owner);
|
||||
return id;
|
||||
}
|
||||
|
||||
stopSequence(name: string, letRing = false): void {
|
||||
this.sequences.stopNamed(name, letRing);
|
||||
}
|
||||
|
||||
/** The patterns playing, oldest first, with the notes last planned of each (test hook). */
|
||||
patterns(): { id: number; name: string; owner: string; playing: boolean; planned: ScheduledNote[] }[] {
|
||||
return this.sequences.current().map(({ id, name, owner, scheduler, planned }) => ({
|
||||
id,
|
||||
name,
|
||||
owner,
|
||||
playing: scheduler.playing,
|
||||
planned: [...planned],
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* The end of a note (#331): the ways that end with it fade out, a looping one that does not leaves
|
||||
* its loop, the others play on, and the voice goes once they have all ended – or rings on first
|
||||
@@ -388,20 +425,26 @@ export class SoundLibrary implements AudioSink {
|
||||
return voice ? this.tailOf(voice.fx) : 0;
|
||||
}
|
||||
|
||||
/** Whether a voice is still heard – or waiting on its bytes to be. */
|
||||
/** Whether a voice is still heard – or waiting on its bytes to be. A pattern counts as one. */
|
||||
playing(id: number): boolean {
|
||||
return this.voices.some((voice) => voice.id === id) || this.pending.some((other) => other.id === id);
|
||||
return (
|
||||
this.voices.some((voice) => voice.id === id) ||
|
||||
this.pending.some((other) => other.id === id) ||
|
||||
this.sequences.has(id)
|
||||
);
|
||||
}
|
||||
|
||||
/** `letRing` stops the sound itself and lets its echoes fade as they would (#324). */
|
||||
stop(file: string, letRing = false): void {
|
||||
if (file === '') this.sequences.stopNamed('', letRing);
|
||||
this.silence(file === '' ? this.voices : this.voices.filter((voice) => voice.file === file), letRing);
|
||||
if (file === '') this.allStops += 1;
|
||||
else this.stops.set(file, (this.stops.get(file) ?? 0) + 1);
|
||||
}
|
||||
|
||||
/** One voice by its number, also one still waiting on its bytes. */
|
||||
/** One voice by its number, also one still waiting on its bytes, or a pattern. */
|
||||
stopVoice(id: number, letRing = false): void {
|
||||
this.sequences.stopId(id, letRing);
|
||||
const waiting = this.pending.find((other) => other.id === id);
|
||||
if (waiting) waiting.stopped = true;
|
||||
this.silence(
|
||||
@@ -416,6 +459,7 @@ export class SoundLibrary implements AudioSink {
|
||||
*/
|
||||
stopOwner(owner: string): void {
|
||||
if (owner === '') return;
|
||||
this.sequences.stopOwner(owner);
|
||||
this.silence(this.voices.filter((voice) => voice.owner === owner));
|
||||
this.ownerStops.set(owner, (this.ownerStops.get(owner) ?? 0) + 1);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user