Stage 2 of #338. A game that syncs to its music has to know where the music is heard. This stage reads that on the audio clock and hands it to behaviors as events and as a position. - New event nodes On beat (playback, bar, beat, late), On bar (playback, bar, late) and On song marker (playback, marker, name, late). - New data node Song position with bar, beat, fraction, seconds and playing. - The nodes have the fields Song and Pattern. With neither picked, every playback counts. A linked playback input picks that one. Compositions do not count. - The Marker field offers the markers of all songs as Song / Marker, as a sound field offers its clips. - Everything follows what is heard: the audio clock minus the output latency. That holds for baked songs and for Play pattern too. - Only the last beat since the frame before fires, and only when it is at most half a beat late, 0.15 s at most. Bars and markers alike. - A copied song gets markers with ids of its own, so a Marker field never names two songs. - The audio clock is only asked while something plays. Asked in every frame, it made the AudioContext without a user gesture (#436). Tests: - beats 14 and music-events 6 new, 4 in song-play, 2 in song-markers, 2 catalog cases, the touched groups 756/756 - 16 counter-probes, all compile, all red - e2e 65 on atx: ALL PASSED, 71 checks, 65k2 new: 11 beats 1.1 to 2.4 and 2.1 again, none missing or twice, across the pause too. late 0.015 to 0.027 s. Counter-probe without the last position: red - gate on atx: CHECK EXIT 0, 334 files, 5121 tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
35 lines
1.6 KiB
TypeScript
35 lines
1.6 KiB
TypeScript
import type { NodeDef } from '../../NodeRegistry';
|
|
import { asNumber, asString } from '../../values';
|
|
import { patternPort } from '../action/PlayPattern';
|
|
import { songPort } from '../action/PlaySong';
|
|
import { PLAYBACK_INPUT, playbackPort } from '../events/musicEvents';
|
|
import { bool, num } from '../shared';
|
|
|
|
export const SongPosition: NodeDef = {
|
|
type: 'SongPosition',
|
|
label: 'Song position',
|
|
category: 'data',
|
|
description:
|
|
'Where a playing song or pattern stands, as it is heard: the output latency of the device taken ' +
|
|
'off, a baked song too, so what is drawn by it keeps to the sound. "bar" and "beat" count from 1 ' +
|
|
'as the ruler does, "fraction" is how far into the beat (0 to below 1), "seconds" how far from ' +
|
|
'the start of the song, back with the loop. Connect "playback" for that playing; else it is the ' +
|
|
'one started last of the Song or the Pattern picked (both picked, either), of every song and ' +
|
|
'pattern where none is. "playing" is off before its first note is heard and after its end; with ' +
|
|
'no such playing everything is 0.',
|
|
inputs: [songPort(), patternPort(), playbackPort()],
|
|
outputs: [num('bar'), num('beat'), num('fraction'), num('seconds'), bool('playing')],
|
|
params: [],
|
|
read: (ctx, _params, input) => {
|
|
const playback = input(PLAYBACK_INPUT);
|
|
const at = ctx.songPosition(
|
|
playback === null ? null : asNumber(playback),
|
|
asString(input('song')),
|
|
asString(input('pattern')),
|
|
);
|
|
return at
|
|
? { bar: at.bar, beat: at.beat, fraction: at.fraction, seconds: at.seconds, playing: at.playing }
|
|
: { bar: 0, beat: 0, fraction: 0, seconds: 0, playing: false };
|
|
},
|
|
};
|