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
@@ -269,6 +269,14 @@ export interface NodeContext {
|
||||
instrumentParams: FxParams,
|
||||
tempo: number,
|
||||
): number;
|
||||
/**
|
||||
* Plays a pattern asset through its instrument, at its own tempo, once or round and round, every
|
||||
* note at `volume`. Owned like a `playSound` voice. Returns its number, for `stopVoice`; 0 where
|
||||
* there is nothing to play.
|
||||
*/
|
||||
playPattern(patternId: string, volume: number, loop: boolean, detached: boolean): number;
|
||||
/** Stops every playing of that pattern, `''` every pattern; `letRing` lets its notes fall away in their release. */
|
||||
stopPattern(patternId: string, letRing: boolean): void;
|
||||
/**
|
||||
* How many seconds one run of that sound asset lasts, so a change can span it; 0 where it does not
|
||||
* exist or has not been loaded yet.
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import type { NodeDef, PortDef } from '../../NodeRegistry';
|
||||
import { asBoolean, asNumber, asString } from '../../values';
|
||||
import { exec, num, withDefault } from '../shared';
|
||||
|
||||
export const patternPort = (): PortDef => ({
|
||||
key: 'pattern',
|
||||
label: 'pattern',
|
||||
type: 'string',
|
||||
field: { key: 'pattern', label: 'Pattern', kind: 'asset', default: '', assetTypes: ['pattern'] },
|
||||
});
|
||||
|
||||
export const PlayPattern: NodeDef = {
|
||||
type: 'PlayPattern',
|
||||
label: 'Play pattern',
|
||||
category: 'action',
|
||||
description:
|
||||
'Plays a pattern asset through its instrument, at the tempo the pattern has. The whole pattern ' +
|
||||
'is played, from its start to its end: once, or with "loop" round and round until a Stop ' +
|
||||
'pattern ends it – the loop set on its ruler is only for editing. "volume" makes every note of ' +
|
||||
'it quieter. Each play is a playing of its own: firing this twice plays it twice, overlapping. ' +
|
||||
'It belongs to this object like a sound and stops when the object is destroyed, unless ' +
|
||||
'"detached" is on. "playback" is the number of this playing, for Stop pattern; 0 where the ' +
|
||||
'pattern has no instrument or no notes.',
|
||||
inputs: [
|
||||
exec(),
|
||||
patternPort(),
|
||||
withDefault(num('volume'), 1),
|
||||
{
|
||||
key: 'loop',
|
||||
label: 'loop',
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
field: { key: 'loop', label: 'Loop', kind: 'boolean', default: true },
|
||||
},
|
||||
{
|
||||
key: 'detached',
|
||||
label: 'detached',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
field: { key: 'detached', label: 'Detached (outlives this object)', kind: 'boolean', default: false },
|
||||
},
|
||||
],
|
||||
outputs: [exec(), num('playback')],
|
||||
params: [],
|
||||
run: (ctx, inputs) => {
|
||||
const pattern = asString(inputs.pattern);
|
||||
const playback = pattern
|
||||
? ctx.playPattern(pattern, asNumber(inputs.volume), asBoolean(inputs.loop), asBoolean(inputs.detached))
|
||||
: 0;
|
||||
return { next: 'exec', outputs: { playback } };
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
import type { NodeDef } from '../../NodeRegistry';
|
||||
import { asBoolean, asNumber, asString } from '../../values';
|
||||
import { exec, num } from '../shared';
|
||||
import { patternPort } from './PlayPattern';
|
||||
|
||||
export const StopPattern: NodeDef = {
|
||||
type: 'StopPattern',
|
||||
label: 'Stop pattern',
|
||||
category: 'action',
|
||||
description:
|
||||
'Stops every playing of a pattern, however many Play pattern started. With no pattern picked it ' +
|
||||
'stops every pattern, and leaves the sounds alone. With "playback" connected it stops only that ' +
|
||||
'one playing, the number Play pattern handed out, and nothing where it is 0. Its notes fade out ' +
|
||||
'in a few milliseconds; with "let ring" those of an instrument with an Envelope fall away in ' +
|
||||
'its release.',
|
||||
inputs: [
|
||||
exec(),
|
||||
patternPort(),
|
||||
// Not typed in: unconnected it is null, and only then does the pattern decide.
|
||||
{ ...num('playback'), inline: false },
|
||||
{
|
||||
key: 'letRing',
|
||||
label: 'let ring',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
field: { key: 'letRing', label: 'Let ring', kind: 'boolean', default: false },
|
||||
},
|
||||
],
|
||||
outputs: [exec()],
|
||||
params: [],
|
||||
run: (ctx, inputs) => {
|
||||
const letRing = asBoolean(inputs.letRing);
|
||||
if (inputs.playback === null || inputs.playback === undefined)
|
||||
ctx.stopPattern(asString(inputs.pattern), letRing);
|
||||
else {
|
||||
const playback = asNumber(inputs.playback);
|
||||
if (playback > 0) ctx.stopVoice(playback, letRing);
|
||||
}
|
||||
return { next: 'exec' };
|
||||
},
|
||||
};
|
||||
@@ -25,6 +25,8 @@ import { RestartGame } from './action/RestartGame';
|
||||
import { SetPosition } from './action/SetPosition';
|
||||
import { SetSoundFxParameters } from './action/SetSoundFxParameters';
|
||||
import { PlayNote } from './action/PlayNote';
|
||||
import { PlayPattern } from './action/PlayPattern';
|
||||
import { StopPattern } from './action/StopPattern';
|
||||
import { SetZ } from './action/SetZ';
|
||||
import { Spawn } from './action/Spawn';
|
||||
import { StopGame } from './action/StopGame';
|
||||
@@ -308,6 +310,8 @@ export const BUILT_IN_NODES: readonly NodeDef[] = [
|
||||
SoundFxParameters,
|
||||
PlayNote,
|
||||
InstrumentParameterValues,
|
||||
PlayPattern,
|
||||
StopPattern,
|
||||
LoadScene,
|
||||
Log,
|
||||
StopGame,
|
||||
|
||||
@@ -37,7 +37,8 @@ export interface SchedulerHost {
|
||||
/** Starts the note, gives back the number of its voice; 0 where nothing plays. */
|
||||
play(note: ScheduledNote): number;
|
||||
playing(voice: number): boolean;
|
||||
stop(voices: readonly number[]): void;
|
||||
/** Ends these voices; with `letRing` a note falls away in the release of its Envelope. */
|
||||
stop(voices: readonly number[], letRing?: boolean): void;
|
||||
/** Calls `pump` every `INTERVAL` seconds, also in a hidden tab, until the returned function is called. */
|
||||
wake(pump: () => void): () => void;
|
||||
}
|
||||
@@ -46,6 +47,8 @@ export interface SchedulerOptions {
|
||||
tempo?: number;
|
||||
meter?: Meter;
|
||||
loop?: LoopRange | null;
|
||||
/** Where it ends, in ticks of the sequence, when it does not loop: it plans nothing after that. */
|
||||
end?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,6 +61,9 @@ export class Scheduler {
|
||||
private tempo: number;
|
||||
private meter: Meter;
|
||||
private loop: LoopRange | null;
|
||||
private readonly end: number;
|
||||
/** Planned up to its end: it wakes no more, and is over once its last notes are. */
|
||||
private ended = false;
|
||||
/** Where the playhead started, in ticks of the sequence. */
|
||||
private from = 0;
|
||||
/** Where on the audio clock playing time 0 is; null while stopped. */
|
||||
@@ -77,12 +83,18 @@ export class Scheduler {
|
||||
this.tempo = heldTempo(options.tempo ?? DEFAULT_TEMPO);
|
||||
this.meter = heldMeter(options.meter ?? COMMON_TIME);
|
||||
this.loop = heldLoop(options.loop ?? null);
|
||||
this.end = options.end ?? Infinity;
|
||||
}
|
||||
|
||||
get playing(): boolean {
|
||||
return this.anchor !== null;
|
||||
}
|
||||
|
||||
/** Played to its end, and none of its notes is heard any more. */
|
||||
get done(): boolean {
|
||||
return this.ended && this.voices.every((voice) => !this.host.playing(voice));
|
||||
}
|
||||
|
||||
/** Plays from `from` ticks of the sequence, or from where it was last started or sent. */
|
||||
start(from = this.from): void {
|
||||
this.stop();
|
||||
@@ -90,18 +102,19 @@ export class Scheduler {
|
||||
this.anchor = this.host.now() + LEAD;
|
||||
this.until = 0;
|
||||
this.before = null;
|
||||
this.ended = false;
|
||||
this.sleep = this.host.wake(() => this.pump());
|
||||
this.pump();
|
||||
}
|
||||
|
||||
/** Every note it started fades out, those still to come with them. */
|
||||
stop(): void {
|
||||
/** Every note it started fades out, those still to come with them; with `letRing` in their release. */
|
||||
stop(letRing = false): void {
|
||||
this.sleep?.();
|
||||
this.sleep = null;
|
||||
if (this.anchor !== null) this.from = Math.floor(this.headAt(this.host.now()));
|
||||
this.anchor = null;
|
||||
this.before = null;
|
||||
if (this.voices.length > 0) this.host.stop(this.voices);
|
||||
if (this.voices.length > 0) this.host.stop(this.voices, letRing);
|
||||
this.voices = [];
|
||||
}
|
||||
|
||||
@@ -153,6 +166,11 @@ export class Scheduler {
|
||||
if (voice !== 0) this.voices.push(voice);
|
||||
}
|
||||
this.until = to;
|
||||
if (!this.loop && playheadAt(to, this.from, null) >= this.end) {
|
||||
this.ended = true;
|
||||
this.sleep?.();
|
||||
this.sleep = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** The playhead at a second of the audio clock; before the anchor, where it starts. */
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
import type { FxPlan } from '../soundfx/nodes';
|
||||
import { type ScheduledNote, Scheduler, type SchedulerHost } from './Scheduler';
|
||||
import type { Sequence } from './sequence';
|
||||
|
||||
/** What a game asks to hear of a pattern: its notes, how fast, how long, and whether round and round. */
|
||||
export interface SequencePlay {
|
||||
sequence: Sequence;
|
||||
tempo: number;
|
||||
/** In ticks: where it ends, and with `loop` where it starts again from 0. */
|
||||
length: number;
|
||||
loop: boolean;
|
||||
/** What its notes are made of, loaded before the first one is planned. */
|
||||
layers: readonly FxPlan[];
|
||||
/** Starts one note as the player was asked to, and gives back the number of its voice. */
|
||||
strike(note: ScheduledNote): number;
|
||||
}
|
||||
|
||||
export interface PlaybackHost extends Omit<SchedulerHost, 'play'> {
|
||||
prepare(layers: readonly FxPlan[]): Promise<void>;
|
||||
}
|
||||
|
||||
export interface Playback {
|
||||
id: number;
|
||||
/** What stands for the pattern, as a file stands for a sound. */
|
||||
name: string;
|
||||
owner: string;
|
||||
scheduler: Scheduler;
|
||||
/** The last notes planned, newest last, for a check to hold against the clock. */
|
||||
planned: ScheduledNote[];
|
||||
}
|
||||
|
||||
/**
|
||||
* The patterns a game plays (#336 ahead of songs): each on a scheduler of its own, numbered like a
|
||||
* voice, owned like one and stopped like one. One played to its end goes by itself.
|
||||
*/
|
||||
export class Playbacks {
|
||||
private list: Playback[] = [];
|
||||
|
||||
constructor(private readonly host: PlaybackHost) {}
|
||||
|
||||
/** Starts once its files are there; a stop that came first wins. */
|
||||
start(id: number, name: string, play: SequencePlay, owner: string): void {
|
||||
const planned: ScheduledNote[] = [];
|
||||
const strike = (note: ScheduledNote) => {
|
||||
planned.push(note);
|
||||
if (planned.length > 64) planned.shift();
|
||||
return play.strike(note);
|
||||
};
|
||||
const scheduler = new Scheduler({ ...this.host, play: strike }, () => play.sequence, {
|
||||
tempo: play.tempo,
|
||||
loop: play.loop ? { start: 0, end: play.length } : null,
|
||||
end: play.length,
|
||||
});
|
||||
const playback = { id, name, owner, scheduler, planned };
|
||||
this.list.push(playback);
|
||||
void this.host.prepare(play.layers).then(() => {
|
||||
if (this.list.includes(playback)) scheduler.start(0);
|
||||
});
|
||||
}
|
||||
|
||||
/** Whether that one is still to be heard: waiting on its files, playing, or its last notes ringing. */
|
||||
has(id: number): boolean {
|
||||
return this.current().some((playback) => playback.id === id);
|
||||
}
|
||||
|
||||
/** Those whose name is `name`, every one where it is `''`. */
|
||||
stopNamed(name: string, letRing = false): void {
|
||||
this.stopWhere((playback) => name === '' || playback.name === name, letRing);
|
||||
}
|
||||
|
||||
stopId(id: number, letRing = false): void {
|
||||
this.stopWhere((playback) => playback.id === id, letRing);
|
||||
}
|
||||
|
||||
/** An empty owner is no one, as for a voice: those were started to outlive whatever started them. */
|
||||
stopOwner(owner: string): void {
|
||||
if (owner !== '') this.stopWhere((playback) => playback.owner === owner, false);
|
||||
}
|
||||
|
||||
/** What is playing, oldest first (test hook). */
|
||||
current(): readonly Readonly<Playback>[] {
|
||||
this.list = this.list.filter((playback) => !playback.scheduler.done);
|
||||
return this.list;
|
||||
}
|
||||
|
||||
private stopWhere(going: (playback: Playback) => boolean, letRing: boolean): void {
|
||||
for (const playback of this.list.filter(going)) playback.scheduler.stop(letRing);
|
||||
this.list = this.list.filter((playback) => !going(playback));
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,8 @@ export function installDebugHook(app: Application, state: EditorState, editor: E
|
||||
stopAll: () => soundLibrary.stopAll(),
|
||||
/** What Stop sound asks for: every voice of that file, its echoes cut or left to ring (#324). */
|
||||
stop: (file: string, letRing: boolean) => soundLibrary.stop(file, letRing),
|
||||
/** The patterns a game plays, with the notes last planned of each. */
|
||||
patterns: () => soundLibrary.patterns(),
|
||||
/** The nodes a stage of a voice becomes, in any context – an offline one renders what it does. */
|
||||
stageNodes: (context: BaseAudioContext, stage: FxStage) => stageNodes(context, stage),
|
||||
/** What drives the two shares of a Mix "Both at center", in any context (unstarted). */
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { EaseShare } from '../../core/model/animation';
|
||||
import type { SequencePlay } from '../../core/music/playbacks';
|
||||
import type { FxParams, FxPlan, FxVoice } from '../../core/soundfx/nodes';
|
||||
|
||||
/**
|
||||
@@ -34,6 +35,15 @@ export interface AudioSink {
|
||||
duration: number,
|
||||
timing?: NoteTiming,
|
||||
): number;
|
||||
/**
|
||||
* Plays the notes of a pattern on the audio clock, each struck through `play.strike`. `name` stands
|
||||
* for the pattern as a file does for a sound, and the number it gives back is one no voice has:
|
||||
* `stopVoice` and `playing` take it like a voice's, `stopOwner` and stopping everything reach it.
|
||||
* Returns 0 where nothing is going to play.
|
||||
*/
|
||||
playSequence(name: string, play: SequencePlay, owner: string): number;
|
||||
/** Stops every pattern of that name, `''` every pattern; with `letRing` its notes fall away in their release. */
|
||||
stopSequence(name: string, letRing?: boolean): void;
|
||||
/**
|
||||
* The end of a note (#331): its ways that end with the note fade out, those that play through
|
||||
* go on. Nothing happens for a voice that is no note or not there.
|
||||
@@ -59,7 +69,7 @@ export interface AudioSink {
|
||||
*/
|
||||
tail(voice: number): number;
|
||||
/**
|
||||
* Silences every voice of that file; `''` silences all of them. A voice is faded out in a few
|
||||
* Silences every voice of that file; `''` silences all of them, patterns too. A voice is faded out in a few
|
||||
* milliseconds, its echoes with it – unless `letRing`, which stops the sound and lets them fade.
|
||||
*/
|
||||
stop(file: string, letRing?: boolean): void;
|
||||
@@ -81,6 +91,8 @@ export interface NoteTiming {
|
||||
export const SILENT_AUDIO: AudioSink = {
|
||||
play: () => 0,
|
||||
playNote: () => 0,
|
||||
playSequence: () => 0,
|
||||
stopSequence: () => {},
|
||||
release: () => {},
|
||||
tune: () => {},
|
||||
length: () => 0,
|
||||
|
||||
@@ -19,6 +19,7 @@ import { soundFxBuildFor } from '../../core/soundfx/compile';
|
||||
import { instrumentBuildFor, instrumentSourcesOf } from '../../core/instrument/compile';
|
||||
import { VELOCITY_PARAM } from '../../core/instrument/nodes';
|
||||
import { heldNote } from '../../core/instrument/notes';
|
||||
import { patternInstrument, patternLength, patternSequence, patternTempo } from '../../core/music/pattern';
|
||||
import { DRY_VOICE, type FxParams } from '../../core/soundfx/nodes';
|
||||
import type { Entity } from '../Entity';
|
||||
import { PhysicsSystem } from '../physics/PhysicsSystem';
|
||||
@@ -173,6 +174,10 @@ export function behaviorContext(
|
||||
playNote(run, instrumentId, { note, velocity, volume, duration }, detached ? '' : entity.id, params, {
|
||||
tempo,
|
||||
}),
|
||||
playPattern: (patternId, volume, loop, detached) =>
|
||||
playPattern(run, patternId, volume, loop, detached ? '' : entity.id),
|
||||
stopPattern: (patternId, letRing) =>
|
||||
run.audio.stopSequence(patternId && patternVoiceName(patternId), letRing),
|
||||
soundLength: (assetId) => {
|
||||
const file = soundFileOf(project, assetId);
|
||||
return file ? run.audio.length(file) : 0;
|
||||
@@ -289,6 +294,52 @@ export function instrumentVoiceName(instrumentId: string): string {
|
||||
return `instrument:${instrumentId}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* A pattern played by a game: its notes through its instrument at its tempo, the whole of it once or
|
||||
* round and round – the loop set on its ruler is for editing. Its notes take the volume and the owner
|
||||
* of the playing. Returns the number of the playing, 0 where there is no pattern or nothing to hear.
|
||||
*/
|
||||
function playPattern(
|
||||
{ project, audio }: Pick<ContextSources, 'project' | 'audio'>,
|
||||
patternId: string,
|
||||
volume: number,
|
||||
loop: boolean,
|
||||
owner: string,
|
||||
): number {
|
||||
const asset = findAssetOf(project, patternId, 'pattern');
|
||||
if (!asset) return 0;
|
||||
const sequence = patternSequence(asset);
|
||||
const instrument = findAssetOf(project, patternInstrument(asset.props), 'instrument');
|
||||
if (!instrument || sequence.notes.length === 0) return 0;
|
||||
const sources = instrumentSourcesOf(project);
|
||||
const pitches = [...new Set(sequence.notes.map((n) => n.note))];
|
||||
const level = Number.isFinite(volume) ? Math.max(0, Math.min(1, volume)) : 1;
|
||||
return audio.playSequence(
|
||||
patternVoiceName(patternId),
|
||||
{
|
||||
sequence,
|
||||
tempo: patternTempo(asset.props),
|
||||
length: patternLength(asset),
|
||||
loop,
|
||||
layers: pitches.flatMap((note) => instrumentBuildFor(instrument, note, sources).layers),
|
||||
strike: ({ instrument: id, note, velocity, duration, at, tempo }) =>
|
||||
playNote(
|
||||
{ project, audio },
|
||||
id,
|
||||
{ note, velocity, volume: level, duration },
|
||||
owner,
|
||||
{},
|
||||
{ at, tempo },
|
||||
),
|
||||
},
|
||||
owner,
|
||||
);
|
||||
}
|
||||
|
||||
export function patternVoiceName(patternId: string): string {
|
||||
return `pattern:${patternId}`;
|
||||
}
|
||||
|
||||
/** Where a ray of that length ends; where it has no direction, where it starts. */
|
||||
function pointAlong(from: Vec2, direction: Vec2, length: number): Vec2 {
|
||||
const d = Math.hypot(direction.x, direction.y);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
+2
-15
@@ -1,23 +1,10 @@
|
||||
import { INTERVAL, type ScheduledNote, Scheduler, type SchedulerOptions } from '../core/music/Scheduler';
|
||||
import { type ScheduledNote, Scheduler, type SchedulerOptions } from '../core/music/Scheduler';
|
||||
import type { Sequence } from '../core/music/sequence';
|
||||
import { instrumentBuildFor, instrumentSourcesOf } from '../core/instrument/compile';
|
||||
import { findAssetOf, type Project } from '../core/model/Project';
|
||||
import { playNote } from '../runtime/systems/behaviorContext';
|
||||
import TimerWorker from './musicTimer.ts?worker';
|
||||
import { soundLibrary } from './SoundLibrary';
|
||||
|
||||
/** Calls `pump` every `INTERVAL` from a worker; where there are no workers, from the page. */
|
||||
function wake(pump: () => void): () => void {
|
||||
const ms = INTERVAL * 1000;
|
||||
if (typeof Worker === 'undefined') {
|
||||
const timer = setInterval(pump, ms);
|
||||
return () => clearInterval(timer);
|
||||
}
|
||||
const worker = new TimerWorker();
|
||||
worker.onmessage = pump;
|
||||
worker.postMessage(ms);
|
||||
return () => worker.terminate();
|
||||
}
|
||||
import { wake } from './wake';
|
||||
|
||||
/** One planned note as the player was given it, and where the audio clock stood then (test hook). */
|
||||
export interface PlannedNote extends ScheduledNote {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { INTERVAL } from '../core/music/Scheduler';
|
||||
import TimerWorker from './musicTimer.ts?worker';
|
||||
|
||||
/** Calls `pump` every `INTERVAL` from a worker; where there are no workers, from the page. */
|
||||
export function wake(pump: () => void): () => void {
|
||||
const ms = INTERVAL * 1000;
|
||||
if (typeof Worker === 'undefined') {
|
||||
const timer = setInterval(pump, ms);
|
||||
return () => clearInterval(timer);
|
||||
}
|
||||
const worker = new TimerWorker();
|
||||
worker.onmessage = pump;
|
||||
worker.postMessage(ms);
|
||||
return () => worker.terminate();
|
||||
}
|
||||
Reference in new issue
Block a user