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
@@ -1,5 +1,5 @@
|
||||
import { edge, node } from '../lib/builders.mjs';
|
||||
import { pickAssetType } from './lib/scaffold.mjs';
|
||||
import { LANES, pickAssetType, scaffoldBehavior } from './lib/scaffold.mjs';
|
||||
|
||||
/**
|
||||
* Patterns and their piano roll (#333). "+ → Pattern" opens the tab; with an instrument picked its
|
||||
@@ -8,7 +8,7 @@ import { pickAssetType } from './lib/scaffold.mjs';
|
||||
* them, the arrows transpose, Delete and a right-click take notes away, Ctrl+C/V copies them to the
|
||||
* start set on the ruler, the lane sets a velocity, a drag on the ruler sets the loop. Space plays the
|
||||
* pattern round its loop through the scheduler, and a note drawn meanwhile is planned without a
|
||||
* restart. Nothing is heard in CI (muted browser): the witnesses are the model, the planned notes and
|
||||
* restart. A game plays it through Play pattern, looped or once. Nothing is heard in CI (muted browser): the witnesses are the model, the planned notes and
|
||||
* the sound counters, as in e2e 34.
|
||||
*
|
||||
* Everything it adds carries a `pt64` id or is the pattern it made, and goes again at the end.
|
||||
@@ -18,6 +18,7 @@ const INSTRUMENT = 'pt64-ins';
|
||||
|
||||
export async function pattern(t) {
|
||||
const { check, sleep, json, evaluate, mutate, click, clickText, press, drag, wheel, shot, showTab } = t;
|
||||
const { play, stop } = t;
|
||||
const stats = () => json('__gm.sound.stats()');
|
||||
const waitFor = async (read, done, tries = 25) => {
|
||||
let value = await read();
|
||||
@@ -335,7 +336,79 @@ export async function pattern(t) {
|
||||
JSON.stringify({ held, heldKey }),
|
||||
);
|
||||
|
||||
// 64l. The step takes back everything it made.
|
||||
// 64l. A game plays the pattern through Play pattern: a bar of quarters at 150 BPM, its notes at
|
||||
// their ticks on the audio clock and round and round, owned by the behavior. Ending the run
|
||||
// ends it; played once, it goes by itself after its bar.
|
||||
await mutate(`
|
||||
const p = __gm.state.project.assets.find((a) => a.id === ${JSON.stringify(id)});
|
||||
p.props.tempo = 150;
|
||||
p.props.bars = 1;
|
||||
p.props.beats = 4;
|
||||
p.props.beat = '4';
|
||||
p.notes = [60, 62, 64, 65].map((note, i) => ({ id: 'pt64-n' + i, tick: i * 480, length: 240, note, velocity: 1 }));`);
|
||||
await showTab('scene');
|
||||
const rig = await scaffoldBehavior(t, {
|
||||
assetId: 'pt64-logic',
|
||||
name: 'Music 64',
|
||||
boxId: 'pt64-box',
|
||||
logicId: 'pt64-brain',
|
||||
y: LANES.sound,
|
||||
graph: {
|
||||
nodes: [
|
||||
node('pt64-start', 'OnStart', 0, 0),
|
||||
node('pt64-play', 'PlayPattern', 260, 0, { pattern: id, volume: 1, loop: true }),
|
||||
],
|
||||
edges: [edge('pt64-e3', 'pt64-start', 'exec', 'pt64-play', 'exec')],
|
||||
},
|
||||
});
|
||||
const patterns = () => json('__gm.sound.patterns()');
|
||||
await evaluate('__gm.sound.stopAll()');
|
||||
await play();
|
||||
const game = await waitFor(patterns, (list) => list[0]?.planned.length >= 6, 40);
|
||||
const round = game[0]?.planned ?? [];
|
||||
const gaps = round.slice(1, 6).map((n, i) => +(n.at - round[i].at).toFixed(6));
|
||||
check(
|
||||
'Play pattern plays it in the game round and round, its quarters 0.4 s apart at 150 BPM, owned by the behavior',
|
||||
game.length === 1 &&
|
||||
game[0].name === `pattern:${id}` &&
|
||||
game[0].owner !== '' &&
|
||||
game[0].playing &&
|
||||
round
|
||||
.slice(0, 6)
|
||||
.map((n) => n.note)
|
||||
.join() === '60,62,64,65,60,62' &&
|
||||
round.every(
|
||||
(n) => n.instrument === INSTRUMENT && n.tempo === 150 && Math.abs(n.duration - 0.2) < 1e-9,
|
||||
) &&
|
||||
gaps.every((gap) => Math.abs(gap - 0.4) < 1e-6),
|
||||
JSON.stringify({ game: game.map((g) => ({ ...g, planned: g.planned.length })), gaps }),
|
||||
);
|
||||
await stop();
|
||||
const ended = await waitFor(
|
||||
async () => ({ patterns: await patterns(), stats: await stats() }),
|
||||
(s) => s.patterns.length === 0 && s.stats.playing === 0,
|
||||
);
|
||||
check(
|
||||
'stopping the game ends the pattern and its notes',
|
||||
ended.patterns.length === 0 && ended.stats.playing === 0,
|
||||
JSON.stringify(ended),
|
||||
);
|
||||
await mutate(`
|
||||
const graph = __gm.state.project.assets.find((a) => a.id === 'pt64-logic').graph;
|
||||
graph.nodes.find((n) => n.id === 'pt64-play').params.loop = false;`);
|
||||
await play();
|
||||
const once = await waitFor(patterns, (list) => list[0]?.planned.length >= 1, 40);
|
||||
const gone = await waitFor(patterns, (list) => list.length === 0, 40);
|
||||
const onceStats = await stats();
|
||||
await stop();
|
||||
check(
|
||||
'played once, it plays its bar and goes by itself while the game runs on',
|
||||
once.length === 1 && gone.length === 0 && once[0].name === `pattern:${id}` && onceStats.playing === 0,
|
||||
JSON.stringify({ once: once.map((g) => g.planned.length), gone, onceStats }),
|
||||
);
|
||||
const left = await rig.remove();
|
||||
|
||||
// 64m. The step takes back everything it made.
|
||||
await evaluate('__gm.sound.stopAll()');
|
||||
await mutate(`
|
||||
const gone = new Set(['pt64-snd', ${JSON.stringify(INSTRUMENT)}, ${JSON.stringify(id)}]);
|
||||
@@ -347,7 +420,9 @@ export async function pattern(t) {
|
||||
'the step leaves nothing of its own behind',
|
||||
(await json(
|
||||
`__gm.state.project.assets.filter((a) => a.id.startsWith('pt64') || a.type === 'pattern').length`,
|
||||
)) === 0,
|
||||
'',
|
||||
)) === 0 &&
|
||||
left.objects === 0 &&
|
||||
left.assets === 0,
|
||||
JSON.stringify(left),
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user