Keep composition sound in step and give compositions a bus of their own

A composition's sound ran on the audio clock while its picture ran on
the frames. A frame counts at most 0.1 s, so after a hitch the sound
stayed ahead of the picture until the next stop. Songs in a trailer
drifted off the cut. The items of a composition also had no bus of
their own, and a video sprite could not pick where its sound goes.

- CompositionSound.follow compares the audio clock with the picture
  each frame. More than 40 ms apart, it starts the sound again where
  the picture is. This holds in the tab and in a run.
- A time scale other than 1 now restarts the sound again and again.
  It stays in time but comes in pieces.
- The sprite of a video item shows "Driven (by composition)" as a
  fixed Video clock row. Video time is hidden there, because the
  composition sets it every frame.
- Every composition has a bus in the mix and a node in the master
  routing. A new one goes to the master unless other compositions are
  linked elsewhere, apart from where the sounds go.
- Audio items pick From asset, To composition, Master or a bus. New
  items start with To composition. Items saved before keep From asset,
  so music already sent through a bus does not bypass it.
- Video sprites have a new row Video bus: From asset by default, the
  master or a bus. In a composition the default is the composition's
  bus, also for video sprites inside a container item.
- The chosen bus is part of the key of the video player, so a sprite
  with another bus gets a player of its own.

Found along the way:

- Six tests with full lists of sprite defaults expected the old rows
  and now list Video bus too.

Tests:
- tsc, eslint, Prettier clean
- Unit: 971 green in the 28 files touched
- npm run check on melektaus-atx: green, 309 files, 4804 tests
- e2e 68 (melektaus-atx): 13 checks green, 68i new. The sound item
  and the video item go into mix:cp68-cut, which goes to the master.
- e2e 69 (melektaus-atx): ALL PASSED, 23 checks, 69q and 69r new.
  After a hitch of 500 ms the sound restarted twice and then lay
  0.5 to 5 ms off the playhead.
- Counter-checks in unit tests: without the comparison the hitch test
  fails, without the bus in the preload the run test fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-14 10:31:16 +02:00
1 parent 875c76026a
commit 4d0a696327
37 files changed
+657 -83

No files matched your search

+1
View File
@@ -305,6 +305,7 @@ export interface CompositionAudio {
kind: CompositionAudioKind;
asset: string;
volume: number;
/** '' along the routing of its asset, `TO_COMPOSITION_BUS` into the composition's bus, else as a Play node picks. */
bus: string;
}
+15 -2
View File
@@ -4,7 +4,17 @@ import { findPixelFrame, pixelFrameRect } from './pixelart';
import { type PropertyBag, textOf, type Vec2 } from './Properties';
import { findAsset, type Project } from './Project';
import { storedFilePattern } from './storedFile';
import { findVideo, isVideoFile, videoKey, videoPlayOf, type VideoRef } from './video';
import { videoBusOf } from '../properties/videos';
import {
findVideo,
isVideoFile,
isVideoKey,
routedVideoKey,
videoKey,
videoMixBus,
videoPlayOf,
type VideoRef,
} from './video';
const FILE_PATTERN = storedFilePattern('png|jpg|webp|gif');
@@ -93,7 +103,10 @@ export function textureRefIds(asset: TextureAsset): string[] {
/** What a sprite wearing `props.texture` shows. */
export function resolveTexture(project: Project, props: PropertyBag): TextureRef | null {
return resolvePicture(project, textOf(props.texture ?? ''));
const ref = resolvePicture(project, textOf(props.texture ?? ''));
// A video heard on a bus the sprite picked plays in a player of its own (#418).
const bus = ref && isVideoKey(ref.file) ? videoMixBus(project, videoBusOf(props)) : null;
return bus ? { ...ref!, file: routedVideoKey(ref!.file, bus) } : ref;
}
/** A texture, a frame, a video or a clip of one, or a generating asset; any other asset's id is no picture. */
+33 -6
View File
@@ -1,6 +1,7 @@
import { FROM_ASSET_BUS, MASTER_MIX, mixBusName, pickedComposition } from '../properties/buses';
import { videoLoopsOf, videoSpanOf, videoVolumeOf } from '../properties/videos';
import type { VideoAsset, VideoClip } from './Asset';
import { findAsset, type Project } from './Project';
import { findAsset, findAssetOf, type Project } from './Project';
import { clipsOf } from './sound';
import { storedFilePattern } from './storedFile';
@@ -64,6 +65,8 @@ export interface VideoPlay {
end: number;
loop: boolean;
volume: number;
/** The bus of the mix its sound goes into, where a sprite picked one; along its routing where left out. */
bus?: string;
}
export function videoPlayOf(ref: VideoRef): VideoPlay {
@@ -107,12 +110,33 @@ const PREFIX = 'video:';
/**
* The name a playing video goes by where a picture is named by `TextureRef.file` (see
* `generated.ts`): `video:<file>#<start>~<end>~<loop>~<volume>`. No uploaded image looks like it,
* and one key is one player: two sprites wearing the same clip show the same picture.
* `generated.ts`): `video:<file>#<start>~<end>~<loop>~<volume>`, and `!<bus>` where its sound goes
* into a bus a sprite picked. No uploaded image looks like it, and one key is one player: two sprites
* wearing the same clip show the same picture.
*/
export function videoKey(play: VideoPlay): string {
const end = Number.isFinite(play.end) ? play.end : 0;
return `${PREFIX}${play.file}#${play.start}~${end}~${play.loop ? 1 : 0}~${play.volume}`;
const bus = play.bus ? `!${play.bus}` : '';
return `${PREFIX}${play.file}#${play.start}~${end}~${play.loop ? 1 : 0}~${play.volume}${bus}`;
}
/**
* The bus of the mix the Video bus of a sprite names: null along the routing of its video, the
* composition's for one it picked for a sprite of its items. A bus that is gone is the master.
*/
export function videoMixBus(project: Project, picked: string): string | null {
if (picked === '' || picked === FROM_ASSET_BUS) return null;
const comp = pickedComposition(picked);
if (comp !== null) return findAssetOf(project, comp, 'composition') ? mixBusName(comp) : null;
return findAssetOf(project, picked, 'bus') ? mixBusName(picked) : MASTER_MIX;
}
/** The key of the same video heard on `bus` (#418); along its routing for null. */
export function routedVideoKey(key: string, bus: string | null): string {
const play = parseVideoKey(key);
if (!play || isDrivenVideoKey(key)) return key;
const { bus: _, ...rest } = play;
return videoKey(bus ? { ...rest, bus } : rest);
}
/**
@@ -138,11 +162,14 @@ export function parseVideoKey(key: string): VideoPlay | null {
if (hash < 0) return null;
const file = key.slice(PREFIX.length, hash);
const at = key.indexOf('@', hash);
const parts = key.slice(hash + 1, at < 0 ? undefined : at).split('~');
const body = key.slice(hash + 1, at < 0 ? undefined : at);
const bang = body.indexOf('!');
const parts = (bang < 0 ? body : body.slice(0, bang)).split('~');
const [start, end, loop, volume] = parts.map(Number);
if (!isVideoFile(file) || parts.length !== 4 || ![start, end, loop, volume].every(Number.isFinite))
return null;
return { file, start: start!, end: end! > 0 ? end! : Infinity, loop: loop === 1, volume: volume! };
const play = { file, start: start!, end: end! > 0 ? end! : Infinity, loop: loop === 1, volume: volume! };
return bang < 0 ? play : { ...play, bus: body.slice(bang + 1) };
}
/** How far before its end a held clip shows its last picture: the end itself is the next one's. */
+2 -2
View File
@@ -13,7 +13,7 @@ import { compositionFps, compositionLength, compositionLoops, itemFades } from '
import { findAssetOf, type Project } from '../model/Project';
import { findSound } from '../model/sound';
import { pickedFx } from '../properties/assetFx';
import { MASTER_MIX, type MixRoute, playedRoute } from './mixer';
import { itemRoute, MASTER_MIX, type MixRoute } from './mixer';
import { fxVoiceOf } from './needs';
import { patternInstrument, patternLength, patternSequence, patternTempo } from './pattern';
import type { BusLevel } from './playbacks';
@@ -67,7 +67,7 @@ export function compositionPlayings(
if (!item.audio || !tracks.has(item.track)) continue;
const laid = laidOut(project, comp.id, item, fps);
if (!laid) continue;
const route = routed ? playedRoute(project, item.audio.bus, laid.routedAs) : MASTER_MIX;
const route = routed ? itemRoute(project, comp.id, item.audio.bus, laid.routedAs) : MASTER_MIX;
const key = JSON.stringify(route);
let playing = byRoute.get(key);
if (!playing)
+24 -11
View File
@@ -1,6 +1,13 @@
import type { SendTap } from '../graph/Graph';
import { findAssetOf, type Project } from '../model/Project';
import { BUS_FX_KEY, busVolumeOf, MASTER_BUS } from '../properties/buses';
import {
BUS_FX_KEY,
busVolumeOf,
MASTER_BUS,
MASTER_MIX,
mixBusName,
TO_COMPOSITION_BUS,
} from '../properties/buses';
import { coerceAs } from '../properties/kinds';
import type { FxVoice } from '../soundfx/nodes';
import { fxVoiceOf } from './needs';
@@ -9,8 +16,7 @@ import { sendOf } from './routing';
import { dbToGain } from './song';
import { DEFAULT_TEMPO } from './time';
/** What the sound library calls the master bus (#325); every other bus of the mix is `mix:<id>`. */
export const MASTER_MIX = 'mix';
export { MASTER_MIX, mixBusName };
/** One link of the master routing (#351) as the mix plays it: into that bus, at that level, from that tap. */
export interface MixSend {
@@ -36,11 +42,6 @@ export interface MixBus {
fx: FxVoice | null;
}
/** The name of a bus of the mix by the id of its asset; the master for '' and for `MASTER_BUS`. */
export function mixBusName(busId: string): string {
return busId === '' || busId === MASTER_BUS ? MASTER_MIX : `mix:${busId}`;
}
/**
* Where an asset goes by the master routing (#351). One the routing has no node for goes into the
* master, as every asset of a project written before there was a routing did. A link to what is
@@ -62,9 +63,10 @@ export function routeOf(project: Project, assetId: string): MixRoute {
const OWN_BUS_TYPES: readonly string[] = ['sound', 'instrument', 'video'];
/**
* The master and every bus of the project (#325), each where the routing sends it (#351); and a bus
* of its own for each sound, instrument and video the routing sends anywhere but straight into one
* bus, for all its voices to go through. A sound FX that does not compile is none.
* The master and every bus of the project (#325), each where the routing sends it (#351); a bus of
* its own for each sound, instrument and video the routing sends anywhere but straight into one bus,
* for all its voices to go through; and one for each composition, for its items. A sound FX that
* does not compile is none.
*/
export function mixOf(project: Project): MixBus[] {
const fxOf = (id: string) => fxVoiceOf(project, id, DEFAULT_TEMPO);
@@ -80,6 +82,9 @@ export function mixOf(project: Project): MixBus[] {
fx: fxOf(coerceAs(asset.props[BUS_FX_KEY], 'asset', '')),
},
];
// A composition's bus is where its items go (#418), whatever its routing.
if (asset.type === 'composition')
return [{ name: mixBusName(asset.id), volume: 1, ...routed(routeOf(project, asset.id)), fx: null }];
if (!OWN_BUS_TYPES.includes(asset.type) || !isRouted(asset)) return [];
const route = routeOf(project, asset.id);
return typeof route === 'string'
@@ -146,6 +151,14 @@ export function playedRoute(project: Project, picked: string, assetId: string):
return routeOf(project, assetId);
}
/**
* Where an audio item of a composition goes: into the composition's bus for `TO_COMPOSITION_BUS`,
* else as a node's pick (`playedRoute`) – along the routing of what it plays where it picks none.
*/
export function itemRoute(project: Project, compId: string, picked: string, assetId: string): MixRoute {
return picked === TO_COMPOSITION_BUS ? mixBusName(compId) : playedRoute(project, picked, assetId);
}
/** The bus of the mix a node reaches where an empty pick is the master: Set bus volume, Bus volume. */
export function reachedBus(project: Project, picked: string): string | null {
if (picked === '' || picked === MASTER_BUS) return MASTER_MIX;
+21 -8
View File
@@ -1,6 +1,6 @@
/**
* The master routing of a project (#351): a graph with a node per sound, instrument, pattern, song,
* video with sound and bus, and the master, its links the sends from one to the next. Its nodes follow the assets: one
* video with sound, composition and bus, and the master, its links the sends from one to the next. Its nodes follow the assets: one
* that comes gets a node of its own and the links most others of its type have, one that goes takes
* its node along.
*/
@@ -16,7 +16,15 @@ import { isEmbedded } from './embedded';
import { addLink, heldSend } from './routing';
import { ROUTED_ASSET, ROUTED_KIND, ROUTED_MASTER, ROUTED_NAME } from './routingNodes';
const ROUTED_TYPES: readonly AssetType[] = ['sound', 'instrument', 'pattern', 'song', 'video', 'bus'];
const ROUTED_TYPES: readonly AssetType[] = [
'sound',
'instrument',
'pattern',
'song',
'video',
'composition',
'bus',
];
/** What has its own sound FX and level on a bus of its own, and so may send from before them. */
const TAPPED_TYPES: readonly AssetType[] = ['bus', 'song'];
@@ -133,11 +141,11 @@ export function heldRouting(graph: Graph | undefined, assets: readonly Asset[],
}
/**
* The links a new sound, instrument, pattern, song or video takes, so it is heard where the others are: those
* of the one whose targets most others of its type share too – the latest of them on a tie –, or of
* all that plays where nothing of its type is linked yet. Videos and the music go by each other
* apart: a video where no other is linked goes to the master, not where the sounds go. Empty where
* nothing is, and for a bus.
* The links a new sound, instrument, pattern, song, video or composition takes, so it is heard where
* the others are: those of the one whose targets most others of its type share too – the latest of
* them on a tie –, or of all that plays where nothing of its type is linked yet. Videos, compositions
* and the music go by each other apart: a video where no other is linked goes to the master, not
* where the sounds go, and so does a composition, a mix of its own. Empty where nothing is, and for a bus.
*/
function linksLikeTheirs(
kind: string,
@@ -162,12 +170,17 @@ function linksLikeTheirs(
};
const playing = nodes.filter((node) => {
const of = routedKind(node);
return of !== '' && of !== 'bus' && (of === 'video') === (kind === 'video') && out.has(node.id);
return of !== '' && of !== 'bus' && peersOf(of) === peersOf(kind) && out.has(node.id);
});
const own = playing.filter((node) => routedKind(node) === kind);
return pick(own.length > 0 ? own : playing);
}
/** Whose links a new node may take: those of its own kind, else of what plays alike. */
function peersOf(kind: string): string {
return kind === 'video' || kind === 'composition' ? kind : 'music';
}
/** A link like another, out of a new node: as loud, and after the sound FX where it cannot tap before. */
function copiedLink(edge: GraphEdge, from: string, kind: string): GraphEdge {
const raw = heldSend(edge.send);
+4 -3
View File
@@ -41,8 +41,8 @@ export function isSongRoutingDef(def: NodeDef): boolean {
}
/**
* The nodes of the master routing (#351): one per sound, instrument, pattern, song, video with sound
* and bus of the project, named and typed after its asset, and the master. Only a bus and the master
* The nodes of the master routing (#351): one per sound, instrument, pattern, song, video with sound,
* composition and bus of the project, named and typed after its asset, and the master. Only a bus and the master
* take links in.
*/
export const ROUTED_ASSET = 'RoutedAsset';
@@ -56,6 +56,7 @@ const KIND_WORDS: Record<string, string> = {
pattern: 'Pattern',
song: 'Song',
video: 'Video',
composition: 'Composition',
bus: 'Bus',
};
@@ -64,7 +65,7 @@ registerNode({
label: 'Asset',
category: 'sound',
description:
'A sound, an instrument, a pattern, a song, a video or a bus of the project: all it plays, sent on to every link.',
'A sound, an instrument, a pattern, a song, a video, a composition or a bus of the project: all it plays, sent on to every link.',
inputs: [],
outputs: [],
params: [],
+4
View File
@@ -153,6 +153,10 @@ type Extras<K extends FlatKind> = K extends 'number'
assetTypes?: readonly AssetType[];
none?: string;
master?: boolean;
/** Offers `FROM_ASSET_BUS`, where empty means a composition's bus. */
fromAsset?: boolean;
/** Offers `TO_COMPOSITION_BUS`, where empty means the routing of the asset. */
toComposition?: boolean;
noFx?: boolean;
}
: K extends 'texture'
+9 -1
View File
@@ -4,7 +4,13 @@ import { findAsset, type Project } from '../model/Project';
import { clipsOf, findSound, soundRefName } from '../model/sound';
import { listedAssets, markedName } from '../music/embedded';
import { NO_FX } from './assetFx';
import { MASTER_BUS } from './buses';
import {
FROM_ASSET_BUS,
FROM_ASSET_LABEL,
MASTER_BUS,
TO_COMPOSITION_BUS,
TO_COMPOSITION_LABEL,
} from './buses';
export const NO_ASSET_LABEL = 'None';
export const MASTER_LABEL = 'Master';
@@ -17,6 +23,8 @@ export const MASTER_LABEL = 'Master';
export function assetRefLabel(project: Project | null, id: string): string {
if (id === '' || id === NO_FX) return NO_ASSET_LABEL;
if (id === MASTER_BUS) return MASTER_LABEL;
if (id === FROM_ASSET_BUS) return FROM_ASSET_LABEL;
if (id === TO_COMPOSITION_BUS) return TO_COMPOSITION_LABEL;
const asset = project ? findAsset(project, id) : undefined;
if (asset) return markedName(asset);
// A clip of a sound (#357) reads "Sound / Clip", as a frame does.
+33
View File
@@ -13,6 +13,39 @@ export const BUS_FX_KEY = 'fx';
*/
export const MASTER_BUS = '#master';
/** What the sound library calls the master bus (#325); every other bus of the mix is `mix:<id>`. */
export const MASTER_MIX = 'mix';
/** The name of a bus of the mix by the id of its asset; the master for '' and for `MASTER_BUS`. */
export function mixBusName(busId: string): string {
return busId === '' || busId === MASTER_BUS ? MASTER_MIX : `mix:${busId}`;
}
/**
* What a video sprite of a composition picks to go along the routing of its video, where empty sends
* it into the composition's own bus (#418).
*/
export const FROM_ASSET_BUS = '#asset';
export const FROM_ASSET_LABEL = 'From asset';
/** What an audio item picks to go into its composition's own bus, where empty goes along the routing of its asset. */
export const TO_COMPOSITION_BUS = '#composition';
export const TO_COMPOSITION_LABEL = 'To composition';
const COMPOSITION_BUS = '#composition:';
/**
* The Video bus a composition hands a sprite of its items that picked none, as it spawns them in a
* run: that composition's bus. Never saved.
*/
export function compositionBusPick(compId: string): string {
return COMPOSITION_BUS + compId;
}
/** The composition a Video bus names by `compositionBusPick`; null for any other. */
export function pickedComposition(picked: string): string | null {
return picked.startsWith(COMPOSITION_BUS) ? picked.slice(COMPOSITION_BUS.length) : null;
}
export function busVolumeOf(bag: PropertyBag): number {
return Math.max(0, Math.min(1, coerceAs(bag[BUS_VOLUME_KEY], 'number', 1)));
}
+20
View File
@@ -1,4 +1,5 @@
import { booleanOf, type PropertyBag } from '../model/Properties';
import { FROM_ASSET_LABEL } from './buses';
import { coerceAs } from './kinds';
import type { PropertyDef } from './PropertySchema';
@@ -23,6 +24,11 @@ export const VIDEO_CLOCK_KEY = 'videoClock';
export const VIDEO_TIME_KEY = 'videoTime';
/** How loud a driven video is heard through this sprite, 0–1, on top of the video's own Volume; a composition's fades set it. */
export const VIDEO_LEVEL_KEY = 'videoVolume';
/**
* The bus of the mix the video's sound goes into through this sprite: empty along the routing of the
* video, in a composition into its bus; `MASTER_BUS`, a bus, or `FROM_ASSET_BUS` for the routing there.
*/
export const VIDEO_BUS_KEY = 'videoBus';
export const VIDEO_CLOCK_OPTIONS = [
{ value: 'free', label: 'Free' },
@@ -60,6 +66,16 @@ export const SPRITE_VIDEO_ROWS: readonly PropertyDef[] = [
shownFor: { key: VIDEO_CLOCK_KEY, values: ['driven'] },
shownForVideo: 'texture',
},
{
key: VIDEO_BUS_KEY,
label: 'Video bus',
kind: 'asset',
assetTypes: ['bus'],
none: FROM_ASSET_LABEL,
master: true,
default: '',
shownForVideo: 'texture',
},
];
/** The rows of a clip of a video, set in the Video tab. */
@@ -84,6 +100,10 @@ export function videoTimeOf(bag: PropertyBag): number {
return coerceAs(bag[VIDEO_TIME_KEY], 'number', 0);
}
export function videoBusOf(bag: PropertyBag): string {
return coerceAs(bag[VIDEO_BUS_KEY], 'asset', '');
}
export function videoLevelOf(bag: PropertyBag): number {
return Math.min(1, Math.max(0, coerceAs(bag[VIDEO_LEVEL_KEY], 'number', 1)));
}