3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Keep one gain node per audio bus and refuse a bad frame count input (#2336)
Nguyen Xuan An20f2ccfdf046
diff --git a/changes/2336.md b/changes/2336.mdnew file mode 100644index 00000000..0ce9fd07--- /dev/null+++ b/changes/2336.md@@ -0,0 +1 @@+- Reuse the audio bus gain node when createBus names an existing bus, and make videoFrameCount refuse a bad fps or duration (#2336, #2337).diff --git a/src/audio.js b/src/audio.jsindex b2aa74ae..0a54385f 100644--- a/src/audio.js+++ b/src/audio.js@@ -260,7 +260,13 @@ export function webAudioBackend(options = {}) {buses.clear();},unlock,+ // A second call for a name keeps the same gain node, so voices already on the bus stay on the bus that setBusVolume moves.createBus(name, volume) {+ const existing = buses.get(name);+ if (existing) {+ setParameter(existing.gain, volume, requireContext().currentTime);+ return;+ }const gain = requireContext().createGain();gain.gain.value = volume;gain.connect(master);diff --git a/src/film-timeline.js b/src/film-timeline.jsindex 6bcbff0f..0898ff97 100644--- a/src/film-timeline.js+++ b/src/film-timeline.js@@ -100,6 +100,8 @@ export function normalizeFilmTimeline(timeline, recipe, { checkCamera } = {}) {/** Frames needed for a duration. A sum such as 0.1 + 0.2 lands a hair above its exact value and must not gain a whole frame. */export function videoFrameCount(fps, durationSeconds) {+ if (!Number.isFinite(fps) || fps <= 0) throw new RangeError(`fps must be a number above 0, got ${String(fps)}.`);+ if (!Number.isFinite(durationSeconds) || durationSeconds < 0) throw new RangeError(`duration must be a number of seconds from 0, got ${String(durationSeconds)}.`);return Math.ceil(fps * durationSeconds - 1e-9);}diff --git a/tests/audio-bus-and-frame-count.test.js b/tests/audio-bus-and-frame-count.test.jsnew file mode 100644index 00000000..df9a3e23--- /dev/null+++ b/tests/audio-bus-and-frame-count.test.js@@ -0,0 +1,54 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import { createAudio, webAudioBackend } from '../src/audio.js';++function createParameter(value = 0) {+ return { value, setTargetAtTime(target) { this.value = target; } };+}++function createNode(kind) {+ const node = { kind, connections: [] };+ node.connect = target => { node.connections.push(target); return target; };+ node.disconnect = () => { node.connections.length = 0; };+ return node;+}++function createContext() {+ const nodes = [];+ const track = node => { nodes.push(node); return node; };+ const context = { state: 'running', currentTime: 0, nodes, destination: createNode('destination') };+ context.listener = {};+ for (const name of ['positionX', 'positionY', 'positionZ', 'forwardX', 'forwardY', 'forwardZ', 'upX', 'upY', 'upZ']) context.listener[name] = createParameter();+ context.createGain = () => track(Object.assign(createNode('gain'), { gain: createParameter(1) }));+ context.createDynamicsCompressor = () => track(Object.assign(createNode('limiter'), {+ threshold: createParameter(), knee: createParameter(), ratio: createParameter(), attack: createParameter(), release: createParameter(),+ }));+ context.createBufferSource = () => track(Object.assign(createNode('source'), {+ playbackRate: createParameter(1), start() {}, stop() {},+ }));+ return context;+}++test('creating a bus that already exists moves the bus its voices play through', () => {+ const context = createContext();+ const audio = createAudio({ backend: webAudioBackend({ createContext: () => context, gestureTarget: null }) });+ audio.init();+ audio.defineSound('theme', { buffer: { duration: 1 }, bus: 'music', loop: true });+ audio.play('theme');+ const gainsBefore = context.nodes.filter(node => node.kind === 'gain').length;+ audio.createBus('music', 0.5);+ assert.equal(context.nodes.filter(node => node.kind === 'gain').length, gainsBefore, 'no second gain node for the same bus');+ audio.setBusVolume('music', 0.2);+ const voiceGain = context.nodes.find(node => node.kind === 'source').connections[0];+ const busGain = voiceGain.connections[0];+ assert.equal(busGain.gain.value, 0.2);+});++test('videoFrameCount refuses a duration or rate that would give no frame count', async () => {+ const { videoFrameCount } = await import('../src/film-timeline.js');+ assert.equal(videoFrameCount(30, 1), 30);+ assert.throws(() => videoFrameCount(30, Number.NaN), /duration must be a number of seconds from 0/);+ assert.throws(() => videoFrameCount(30, -1), /duration must be a number of seconds from 0/);+ assert.throws(() => videoFrameCount(0, 1), /fps must be a number above 0/);+ assert.throws(() => videoFrameCount(Number.NaN, 1), /fps must be a number above 0/);+});