Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

Pad the last shot's song-audio window with silence past the song's end

Claude2a16fb56b616
diff --git a/server/services/federatedMedia/sourceAudio.js b/server/services/federatedMedia/sourceAudio.js
index 7d66494aac..968e5b6b01 100644
--- a/server/services/federatedMedia/sourceAudio.js
+++ b/server/services/federatedMedia/sourceAudio.js
@@ -111,8 +111,12 @@ async function prepareWindow({ project, scene, numFrames, fps }) {
const audioFilePath = join(PATHS.uploads, `mv-source-audio-${randomUUID()}.wav`);
try {
const endSample = startSample + sampleCount;
+ // The frame canvas rounds up to the model's stride, so the last shot's
+ // window can run a few ms past the end of the recording. Pad that tail
+ // with silence (apad) so the clip still has exactly sampleCount samples,
+ // the length the wire contract and the frame count both depend on.
const result = await runFfmpegProcess({ bin, args: ['-v', 'error', '-i', source, '-vn',
- '-af', `aresample=48000,atrim=start_sample=${startSample}:end_sample=${endSample},asetpts=PTS-STARTPTS`,
+ '-af', `aresample=48000,atrim=start_sample=${startSample}:end_sample=${endSample},asetpts=PTS-STARTPTS,apad=whole_len=${sampleCount}`,
'-ar', '48000', '-ac', '2', '-c:a', 'pcm_s16le', '-y', audioFilePath] });
const info = result.ok ? pcmAudioInfo(await readFile(audioFilePath)) : null;
if (info?.sampleCount !== sampleCount || await sha256File(source) !== sourceSha256) {
diff --git a/server/services/federatedMedia/sourceAudio.test.js b/server/services/federatedMedia/sourceAudio.test.js
index 683fcfe049..b220e3ffa5 100644
--- a/server/services/federatedMedia/sourceAudio.test.js
+++ b/server/services/federatedMedia/sourceAudio.test.js
@@ -2,6 +2,7 @@ vi.mock('../../lib/maintenanceAdmission.js', () => ({ maintenance: { run: (_kind
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { mkdtemp, readFile, rm, symlink, utimes, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
+import { execFileSync } from 'node:child_process';
import { tmpdir } from 'node:os';
import { PATHS, sha256Text } from '../../lib/fileUtils.js';
import { federatedMediaVideoJobSubmissionSchema } from '../../lib/validation.js';
@@ -13,6 +14,8 @@ const mocks = vi.hoisted(() => ({ master: '', run: vi.fn() }));
vi.mock('../musicVideo/render.js', () => ({ resolveMasterAudioPath: async () => mocks.master }));
vi.mock('../../lib/ffmpeg.js', () => ({ findFfmpeg: async () => 'fixture-ffmpeg', runFfmpegProcess: mocks.run }));
+const hasFfmpeg = (() => { try { execFileSync('ffmpeg', ['-version'], { stdio: 'ignore' }); return true; } catch { return false; } })();
+
function wav(samples = 48000) {
const bytes = Buffer.alloc(44 + samples * 4);
bytes.write('RIFF'); bytes.writeUInt32LE(bytes.length - 8, 4); bytes.write('WAVEfmt ', 8);
@@ -70,7 +73,7 @@ describe('supplied-audio transport boundary', () => {
it('cuts the requested sample window and refuses a short decode before enqueue', async () => {
const audio = await prepareSourceAudioWindow({ project: {}, scene: { startSec: 2 }, numFrames: 25, fps: 24 });
- expect(mocks.run.mock.calls[0][0].args).toContain('aresample=48000,atrim=start_sample=96000:end_sample=144000,asetpts=PTS-STARTPTS');
+ expect(mocks.run.mock.calls[0][0].args).toContain('aresample=48000,atrim=start_sample=96000:end_sample=144000,asetpts=PTS-STARTPTS,apad=whole_len=48000');
expect(audio.audioConditioning).toEqual({ ...provenance(await readFile(audio.audioFilePath)), sourceSha256: sha256Text(wav()) });
// The recording fixture hashes to the same bytes as the one-second slice.
expect(audio.audioConditioning.sourceSha256).toBe(sha256Text(wav()));
@@ -99,4 +102,21 @@ describe('supplied-audio transport boundary', () => {
await writeFile(mocks.master, wav(96000));
await expect(assertCurrentSourceAudioWindow(project, audio.audioConditioning)).rejects.toMatchObject({ code: 'MUSIC_VIDEO_AUDIO_SOURCE_CHANGED' });
});
+
+ it.skipIf(!hasFfmpeg)('pads a window that runs past the end of the recording to its exact length', async () => {
+ // A tone of 1.5 s, sliced from 1 s for a 25-frame (1 s) window: the last
+ // 0.5 s lies past the recording, as on a song's final shot.
+ const tone = wav(72000);
+ for (let i = 0; i < 72000; i += 1) { tone.writeInt16LE(8000, 44 + i * 4); tone.writeInt16LE(8000, 46 + i * 4); }
+ await writeFile(mocks.master, tone);
+ const { runFfmpegProcess } = await vi.importActual('../../lib/ffmpeg.js');
+ mocks.run.mockImplementation((opts) => runFfmpegProcess({ ...opts, bin: 'ffmpeg' }));
+ const audio = await prepareSourceAudioWindow({ project: {}, scene: { startSec: 1, endSec: 2 }, numFrames: 25, fps: 24 });
+ const clip = await readFile(audio.audioFilePath);
+ expect(audio.audioConditioning).toMatchObject({ sampleCount: 48000, startSample: 48000, endSample: 96000 });
+ // ffmpeg may write metadata chunks first, so find the PCM after 'data'.
+ const data = clip.indexOf('data') + 8;
+ expect(clip.readInt16LE(data)).toBe(8000);
+ expect(clip.readInt16LE(data + 47999 * 4)).toBe(0);
+ });
});