Decode a clip from a file of its own, not its whole sound (#349)
A clip was cut from its sound's file, so a game decoded the whole file for it and held it for as long as the page was open. A long recording played through a few short clips cost the recording. Every clip now has a copy of its cut as Opus, with Stream on or off, and a game decodes that instead. - StreamKeeper makes the copy of each clip whether its sound streams or not. Stream only decides whether a sound gets a copy of its own. - clipCopyTable says which cut decodes from which copy. The sound library loads the copy for a clip unless the whole file is decoded already. Then the clip is cut from that as before. - projectFiles decodes the whole file ahead only where something may play the sound itself: no clips, a clip without a fitting copy, or the sound's id anywhere in an asset that can name one. That last test walks the data, not the fields, so no place is missed. - A copy that cannot be had falls back to the whole file. A library that bakes keeps to the whole files, as the bake's fingerprint does. Tests: - Gate: CHECK EXIT 0, 280 files, 4319 tests (7 new: streams +4, sound-library +3). - e2e 34: ALL PASSED, 108 checks (2 new). With Stream off a fresh library decodes the ten-second reversed clip's copy and not the five-minute file. It is the cut made from the original to within 0.0091 a sample and 0.013 dB in any 20 ms. Once a node plays the sound itself, the whole file is decoded ahead again. - e2e 65: all passed with this change. - Counter-checks red as they should be: the library ignoring copies, the whole file always decoded ahead, clip copies only with Stream. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2764919bb8
commit
82e96e680f
10 files changed
+332
-42
No files matched your search
@@ -1,9 +1,12 @@
|
||||
import { validBake } from '../music/bake';
|
||||
import { streamsOf } from '../properties/sounds';
|
||||
import { clipCopyTable } from '../music/streams';
|
||||
import { cutOf, streamsOf } from '../properties/sounds';
|
||||
import type { AssetType } from './AssetTypes';
|
||||
import type { SoundAsset } from './Asset';
|
||||
import { isFontFile } from './font';
|
||||
import { celFilesOf } from './pixelart';
|
||||
import type { Project } from './Project';
|
||||
import { isSoundFile } from './sound';
|
||||
import { clipsOf, cutName, isSoundFile } from './sound';
|
||||
import { isTextureFile } from './texture';
|
||||
|
||||
/** The stored files a project draws and plays, per library – what is loaded before anything asks. */
|
||||
@@ -20,17 +23,57 @@ export interface ProjectFiles {
|
||||
* an upload that failed – is left out, as the libraries would never get it anyway. So is a bake that
|
||||
* no longer holds (#340): it would never be played. So is a sound that streams (#349): the sound
|
||||
* library fetches the start of its copy instead, and decoded ahead it would weigh what streaming saves.
|
||||
* A sound only played through its clips has their copies loaded rather than its whole file.
|
||||
*/
|
||||
export function projectFiles(project: Project): ProjectFiles {
|
||||
const files: ProjectFiles = { textures: [], fonts: [], cels: [], sounds: [] };
|
||||
const copies = clipCopyTable(project);
|
||||
let named: ReadonlySet<string> | null = null;
|
||||
const namedWhole = (asset: SoundAsset) => (named ??= namedStrings(project)).has(asset.id);
|
||||
for (const asset of project.assets) {
|
||||
if (asset.type === 'texture' && isTextureFile(asset.file)) files.textures.push(asset.file);
|
||||
else if (asset.type === 'font' && isFontFile(asset.file)) files.fonts.push(asset.file);
|
||||
else if (asset.type === 'pixelart') files.cels.push(...celFilesOf(asset));
|
||||
else if (asset.type === 'sound' && isSoundFile(asset.file) && !streamsOf(asset.props))
|
||||
files.sounds.push(asset.file);
|
||||
files.sounds.push(...decodedAhead(asset, copies, namedWhole));
|
||||
else if ((asset.type === 'pattern' || asset.type === 'song') && validBake(project, asset))
|
||||
files.sounds.push(asset.bake!.file);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
/**
|
||||
* What of a sound is decoded ahead: its file where something may play it whole – a sound without
|
||||
* clips, one the project names itself, one with a clip whose copy is not there yet – else the cut of
|
||||
* each clip, which the sound library plays from its copy.
|
||||
*/
|
||||
function decodedAhead(
|
||||
asset: SoundAsset,
|
||||
copies: ReadonlyMap<string, string>,
|
||||
namedWhole: (asset: SoundAsset) => boolean,
|
||||
): string[] {
|
||||
const cuts = clipsOf(asset).map((clip) => cutName(asset.file, cutOf(clip.props)));
|
||||
const whole = cuts.length === 0 || cuts.some((cut) => !copies.has(cut)) || namedWhole(asset);
|
||||
return whole ? [asset.file] : [...new Set(cuts)];
|
||||
}
|
||||
|
||||
/** What holds no reference to a sound, and holds a lot: not walked for one. */
|
||||
const UNNAMING: ReadonlySet<AssetType> = new Set(['sound', 'texture', 'pixelart', 'tilemap', 'font']);
|
||||
|
||||
/**
|
||||
* Every text the project holds outside the assets that cannot name a sound. Where a sound is played
|
||||
* whole – a sound field, an item of a song, a Sample, a Reverb's impulse – its id is one of them; a
|
||||
* clip has an id of its own. Looked at as data, not field by field, so no place is missed: one that
|
||||
* only looks like a reference loads the file as before.
|
||||
*/
|
||||
function namedStrings(project: Project): Set<string> {
|
||||
const seen = new Set<string>();
|
||||
const walk = (value: unknown): void => {
|
||||
if (typeof value === 'string') seen.add(value);
|
||||
else if (Array.isArray(value)) value.forEach(walk);
|
||||
else if (typeof value === 'object' && value !== null && !ArrayBuffer.isView(value))
|
||||
Object.values(value).forEach(walk);
|
||||
};
|
||||
for (const asset of project.assets) if (!UNNAMING.has(asset.type)) walk(asset);
|
||||
return seen;
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
/**
|
||||
* Sounds streamed rather than decoded whole (#349). A sound with `stream` on is played from a copy as
|
||||
* Opus in Ogg, made in the browser, and each of its clips from a copy of its own cut, so no clip needs
|
||||
* the whole file either. A bake (#340) is Opus already and always streams. What a game plays is still
|
||||
* named as before – the file, or the cut a clip is – and the table here says which of those names to
|
||||
* stream from which copy.
|
||||
* Opus in Ogg, made in the browser. Every clip of every sound has a copy of its own cut, so no clip
|
||||
* needs the whole file: streamed where its sound streams, decoded where it does not. A bake (#340) is
|
||||
* Opus already and always streams. What a game plays is still named as before – the file, or the cut
|
||||
* a clip is – and the tables here say which of those names to play from which copy.
|
||||
*/
|
||||
import type { SoundAsset, SoundClip, SoundStream } from '../model/Asset';
|
||||
import type { Project } from '../model/Project';
|
||||
@@ -97,6 +97,23 @@ function heldCopy(stream: SoundStream | undefined, played: string): StreamedFile
|
||||
return stream && stream.from === played ? { file: stream.file, frames: stream.frames } : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The cut each clip plays, with the copy it is decoded from instead of its sound's whole file – of a
|
||||
* sound that streams too, for where a stream cannot be had. A clip whose copy does not fit is left out.
|
||||
*/
|
||||
export function clipCopyTable(project: Project): Map<string, string> {
|
||||
const table = new Map<string, string>();
|
||||
for (const asset of project.assets) {
|
||||
if (asset.type !== 'sound' || !asset.file) continue;
|
||||
for (const clip of clipsOf(asset)) {
|
||||
const played = cutName(asset.file, cutOf(clip.props));
|
||||
const copy = played === asset.file ? null : heldCopy(clip.stream, played);
|
||||
if (copy) table.set(played, copy.file);
|
||||
}
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
/** A copy to make: of a sound, or of one of its clips, from what it plays now. */
|
||||
export interface StreamJob {
|
||||
assetId: string;
|
||||
@@ -104,7 +121,7 @@ export interface StreamJob {
|
||||
from: string;
|
||||
}
|
||||
|
||||
/** A copy that is to go: its sound no longer streams, or a clip of it has become the whole file. */
|
||||
/** A copy that is to go: its sound no longer streams or has no file, or a clip is the whole file. */
|
||||
export interface StreamDrop {
|
||||
assetId: string;
|
||||
clipId: string | null;
|
||||
@@ -116,16 +133,16 @@ export function streamWork(project: Project): { jobs: StreamJob[]; drops: Stream
|
||||
const drops: StreamDrop[] = [];
|
||||
for (const asset of project.assets) {
|
||||
if (asset.type !== 'sound') continue;
|
||||
const on = streamsOf(asset.props) && asset.file !== '';
|
||||
const keep = (owner: SoundAsset | SoundClip, clipId: string | null, from: string | null) => {
|
||||
if (from === null) {
|
||||
if (owner.stream) drops.push({ assetId: asset.id, clipId });
|
||||
} else if (owner.stream?.from !== from) jobs.push({ assetId: asset.id, clipId, from });
|
||||
};
|
||||
keep(asset, null, on ? asset.file : null);
|
||||
keep(asset, null, streamsOf(asset.props) && asset.file !== '' ? asset.file : null);
|
||||
// A clip has its copy whether its sound streams or not.
|
||||
for (const clip of clipsOf(asset)) {
|
||||
const played = cutName(asset.file, cutOf(clip.props));
|
||||
keep(clip, clip.id, on && played !== asset.file ? played : null);
|
||||
keep(clip, clip.id, asset.file !== '' && played !== asset.file ? played : null);
|
||||
}
|
||||
}
|
||||
return { jobs, drops };
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { findAssetOf } from '../../core/model/Project';
|
||||
import { clipsOf, findSound } from '../../core/model/sound';
|
||||
import { projectFiles } from '../../core/model/projectFiles';
|
||||
import { clipsOf, cutChannels, cutRange, findSound, parseCutName } from '../../core/model/sound';
|
||||
import { STREAM_RATE } from '../../core/music/streamQueue';
|
||||
import { playedPlan, streamTable } from '../../core/music/streams';
|
||||
import { clipCopyTable, playedPlan, streamTable } from '../../core/music/streams';
|
||||
import { playedSoundOf } from '../../core/properties/playedSound';
|
||||
import { soundLibrary } from '../../viewport/SoundLibrary';
|
||||
import { centreOf, type HookContext } from './hookHelpers';
|
||||
@@ -121,6 +122,49 @@ export function soundHooks({ state, editor }: HookContext) {
|
||||
skipped: played.skipped,
|
||||
};
|
||||
},
|
||||
/**
|
||||
* What a game decodes that has just opened the project, in a library of its own so nothing this
|
||||
* page holds counts: the names it decodes ahead, whether that decoded the whole file of a name's
|
||||
* sound, and what the name then plays held against its cut made from that file – samples, rate,
|
||||
* the loudest difference of a sample and of the level of any 20 ms.
|
||||
*/
|
||||
fresh: async (name: string) => {
|
||||
const library = soundLibrary.aside();
|
||||
library.setStreams(streamTable(state.project));
|
||||
library.setCopies(clipCopyTable(state.project));
|
||||
const ahead = projectFiles(state.project).sounds;
|
||||
await Promise.all(ahead.map((file) => library.load(file)));
|
||||
const { file, cut } = parseCutName(name);
|
||||
const whole = library.decoded(file) !== null;
|
||||
const played = await library.load(name);
|
||||
const original = await soundLibrary.decodeApart(file, STREAM_RATE);
|
||||
if (!played || !cut || played.sampleRate !== STREAM_RATE) return { ahead, whole, played: null };
|
||||
const [reference] = cutChannels(
|
||||
[original.getChannelData(0)],
|
||||
cutRange(original.length, STREAM_RATE, cut),
|
||||
);
|
||||
const heard = played.getChannelData(0);
|
||||
let worst = 0;
|
||||
let level = 0;
|
||||
const span = Math.round(0.02 * STREAM_RATE);
|
||||
for (let from = 0; from + span <= heard.length; from += span) {
|
||||
let own = 0;
|
||||
let wanted = 0;
|
||||
for (let i = from; i < from + span; i++) {
|
||||
worst = Math.max(worst, Math.abs(heard[i]! - (reference![i] ?? 0)));
|
||||
own += heard[i]! ** 2;
|
||||
wanted += (reference![i] ?? 0) ** 2;
|
||||
}
|
||||
// Only where it is heard: the fades start from silence.
|
||||
if (wanted / span > 1e-6) level = Math.max(level, Math.abs(10 * Math.log10(own / wanted)));
|
||||
}
|
||||
return {
|
||||
ahead,
|
||||
whole,
|
||||
played: { length: played.length, wanted: reference!.length, worst, level },
|
||||
decoded: library.stats().decoded,
|
||||
};
|
||||
},
|
||||
/** What a sound and each of its clips keep of their copies. */
|
||||
copies: (id: string) => {
|
||||
const asset = findAssetOf(state.project, id, 'sound');
|
||||
|
||||
@@ -14,10 +14,10 @@ import type { EditorState } from '../state';
|
||||
const SETTLE_MS = 600;
|
||||
|
||||
/**
|
||||
* Makes the copies streamed sounds are played from (#349). A moment after the project last changed,
|
||||
* each sound with `stream` on gets a copy of its file as Opus, and each of its clips one of its cut,
|
||||
* one at a time, from the file decoded for it and let go again. A copy no longer wanted is taken off
|
||||
* its sound and deleted once the project has been saved without it. Not while the game runs.
|
||||
* Makes the copies sounds and clips are played from (#349). A moment after the project last changed,
|
||||
* each sound with `stream` on gets a copy of its file as Opus, and each clip of any sound one of its
|
||||
* cut, one at a time, from the file decoded for it and let go again. A copy no longer wanted is taken
|
||||
* off its sound and deleted once the project has been saved without it. Not while the game runs.
|
||||
*/
|
||||
export class StreamKeeper {
|
||||
private job: { job: StreamJob; share: number } | null = null;
|
||||
@@ -77,7 +77,7 @@ export class StreamKeeper {
|
||||
if (error instanceof BakeCancelled) return this.later(0);
|
||||
this.failed.add(keyOf(next));
|
||||
this.state.setNotice(
|
||||
`No stream copy of ${this.label(next)}: ${error instanceof Error ? error.message : String(error)}`,
|
||||
`No copy of ${this.label(next)}: ${error instanceof Error ? error.message : String(error)}`,
|
||||
'error',
|
||||
);
|
||||
} finally {
|
||||
@@ -100,7 +100,7 @@ export class StreamKeeper {
|
||||
const before = Math.round(running.share * 100);
|
||||
running.share = share;
|
||||
const now = Math.round(share * 100);
|
||||
if (now !== before) this.state.setNotice(`Making a stream copy of ${label}: ${now}%`, 'quiet');
|
||||
if (now !== before) this.state.setNotice(`Making a copy of ${label}: ${now}%`, 'quiet');
|
||||
},
|
||||
cancelled: () => !this.stillWanted(job),
|
||||
});
|
||||
@@ -119,14 +119,14 @@ export class StreamKeeper {
|
||||
bytes: uploaded.bytes,
|
||||
});
|
||||
if (old && old !== uploaded.file && !this.named(old)) dropWhenSaved(this.state, this.sounds, old);
|
||||
this.state.setNotice(`Made a stream copy of ${label}`, 'quiet');
|
||||
this.state.setNotice(`Made a copy of ${label}`, 'quiet');
|
||||
}
|
||||
|
||||
/** Whether the sound still streams and still plays what the copy is being made of. */
|
||||
/** Whether the sound, or its clip, still plays what the copy is being made of – a sound still streams. */
|
||||
private stillWanted(job: StreamJob): boolean {
|
||||
const asset = this.sound(job.assetId);
|
||||
if (!asset || !streamsOf(asset.props)) return false;
|
||||
if (job.clipId === null) return asset.file === job.from;
|
||||
if (!asset) return false;
|
||||
if (job.clipId === null) return streamsOf(asset.props) && asset.file === job.from;
|
||||
const clip = clipsOf(asset).find((c) => c.id === job.clipId);
|
||||
return clip !== undefined && cutName(asset.file, cutOf(clip.props)) === job.from;
|
||||
}
|
||||
|
||||
@@ -378,6 +378,8 @@ export class SoundLibrary implements AudioSink {
|
||||
private readonly mixVolumes = new Map<string, number>();
|
||||
/** The names streamed (#349) and the copies they stream from, by name and by copy. */
|
||||
private streams: ReadonlyMap<string, StreamedFile> = new Map();
|
||||
/** The cut of each clip, with the copy it is decoded from rather than its whole file (#349). */
|
||||
private copies: ReadonlyMap<string, string> = new Map();
|
||||
private readonly streamFiles = new Map<string, StreamFile>();
|
||||
private readonly feeds = new Set<StreamFeed>();
|
||||
/** Every sample streamed so far that came too late to be heard, of the feeds gone (test hook). */
|
||||
@@ -398,6 +400,21 @@ export class SoundLibrary implements AudioSink {
|
||||
for (const name of table.keys()) void this.streamed(name)?.ready();
|
||||
}
|
||||
|
||||
/**
|
||||
* The copies clips are decoded from (#349), each under the cut it plays (`clipCopyTable`). A clip
|
||||
* whose sound's whole file is decoded anyway is cut from that as before. One that bakes keeps to the
|
||||
* whole files, as the fingerprint of a bake does.
|
||||
*/
|
||||
setCopies(table: ReadonlyMap<string, string>): void {
|
||||
this.copies = table;
|
||||
}
|
||||
|
||||
/** The copy a clip is decoded from, while it has one that has not failed. */
|
||||
private copyOf(name: string): string | null {
|
||||
const copy = this.render ? undefined : this.copies.get(name);
|
||||
return copy && !this.failed.has(copy) ? copy : null;
|
||||
}
|
||||
|
||||
/** The copy a name streams from, while it streams and the copy has not failed. */
|
||||
private streamed(name: string): StreamFile | null {
|
||||
const entry = this.streams.get(name);
|
||||
@@ -1755,10 +1772,14 @@ export class SoundLibrary implements AudioSink {
|
||||
|
||||
/**
|
||||
* Fetches and decodes once per file; a second play while it is in flight waits on the first. A cut
|
||||
* (#343) loads its file and is made from it.
|
||||
* (#343) loads its file and is made from it – the cut of a clip with a copy (#349) loads that
|
||||
* instead, unless the whole file is there already.
|
||||
*/
|
||||
async load(file: string): Promise<AudioBuffer | null> {
|
||||
if (parseCutName(file).cut) {
|
||||
const copy = this.copyOf(file);
|
||||
if (copy && !this.buffers.has(parseCutName(file).file) && (await this.load(copy)))
|
||||
return this.buffer(file);
|
||||
const whole = await this.load(parseCutName(file).file);
|
||||
return whole ? this.buffer(file) : null;
|
||||
}
|
||||
@@ -2331,6 +2352,13 @@ export class SoundLibrary implements AudioSink {
|
||||
};
|
||||
}
|
||||
|
||||
/** A library of its own, served from where this one is, that has decoded nothing yet (test hook). */
|
||||
aside(): SoundLibrary {
|
||||
const library = new SoundLibrary(this.guarded);
|
||||
library.configure(this.base);
|
||||
return library;
|
||||
}
|
||||
|
||||
/** All of the copy a name streams from, decoded as the feed decodes it (test hook). */
|
||||
async streamedWhole(name: string): Promise<Float32Array[] | null> {
|
||||
const file = this.streamed(name);
|
||||
@@ -2446,10 +2474,16 @@ export class SoundLibrary implements AudioSink {
|
||||
});
|
||||
}
|
||||
|
||||
/** What a name plays, a cut made from its file the first time; null while the file is not loaded. */
|
||||
/**
|
||||
* What a name plays: the copy of a clip (#349) once it is decoded, else a cut made from its file the
|
||||
* first time; null while neither is loaded.
|
||||
*/
|
||||
private buffer(name: string): AudioBuffer | null {
|
||||
const whole = this.buffers.get(name);
|
||||
if (whole) return whole;
|
||||
const copy = this.copyOf(name);
|
||||
const copied = copy ? this.buffers.get(copy) : undefined;
|
||||
if (copied) return copied;
|
||||
const held = this.cuts.get(name);
|
||||
if (held) {
|
||||
this.cuts.delete(name);
|
||||
@@ -2476,12 +2510,17 @@ export class SoundLibrary implements AudioSink {
|
||||
return made;
|
||||
}
|
||||
|
||||
/** The samples of its loaded file a name plays: all of them, or the stretch a cut keeps. */
|
||||
/**
|
||||
* The samples of its loaded file a name plays: all of them, or the stretch a cut keeps – all of a
|
||||
* clip's copy (#349), which is that stretch already.
|
||||
*/
|
||||
private rangeOf(name: string): (CutRange & { whole: AudioBuffer }) | null {
|
||||
const { file, cut } = parseCutName(name);
|
||||
const whole = this.buffers.get(file);
|
||||
const copy = this.copyOf(name);
|
||||
const copied = copy ? this.buffers.get(copy) : undefined;
|
||||
const whole = copied ?? this.buffers.get(file);
|
||||
if (!whole) return null;
|
||||
if (!cut)
|
||||
if (!cut || copied)
|
||||
return {
|
||||
whole,
|
||||
from: 0,
|
||||
@@ -2498,9 +2537,9 @@ export class SoundLibrary implements AudioSink {
|
||||
/** How long a sound a Reverb takes as its impulse response rings (#328): all it may, while unknown. */
|
||||
private readonly soundLength = (file: string): number => this.duration(file) ?? REVERB_MAX_DECAY;
|
||||
|
||||
/** Whether the file of a name could not be had: a cut of it cannot either. */
|
||||
/** Whether the file of a name could not be had: a cut of it cannot either, unless it has a copy. */
|
||||
private hasFailed(name: string): boolean {
|
||||
return this.failed.has(parseCutName(name).file);
|
||||
return this.copyOf(name) === null && this.failed.has(parseCutName(name).file);
|
||||
}
|
||||
|
||||
/** Loads the FX worklet (Pitch, Bitcrusher, Chip, Phaser, Flanger) into the context once; says whether it is there. */
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Renderer } from 'pixi.js';
|
||||
import type { Project } from '../core/model/Project';
|
||||
import { projectFiles } from '../core/model/projectFiles';
|
||||
import { streamTable } from '../core/music/streams';
|
||||
import { clipCopyTable, streamTable } from '../core/music/streams';
|
||||
import { requestRender } from '../ui';
|
||||
import { fontLibrary } from './FontLibrary';
|
||||
import { installPixelArtPictures } from './pixelArtPicture';
|
||||
@@ -47,7 +47,9 @@ export function preloadProjectFiles(project: Project): void {
|
||||
textureLibrary.preload(files.textures);
|
||||
fontLibrary.preload(files.fonts);
|
||||
pixelCels.preload(files.cels);
|
||||
// Which of them stream (#349) first, so their copies are fetched rather than they decoded.
|
||||
// Which of them stream (#349) first, so their copies are fetched rather than they decoded, and
|
||||
// which clips decode from a copy of their own rather than from their sound's whole file.
|
||||
soundLibrary.setStreams(streamTable(project));
|
||||
soundLibrary.setCopies(clipCopyTable(project));
|
||||
soundLibrary.preload(files.sounds);
|
||||
}
|
||||
Reference in new issue
Block a user