fix(core): a decoded buffer's handover leaves the routed element silent (#7309)

* fix(core): a decoded buffer's handover leaves the routed element silent

* fix(core): a handed-over element sounds again when playback stops
This commit is contained in:
Miguel Ángel
2026-11-06 10:17:47 +00:00
committed by GitHub
parent f3e45b492e
commit cb8e02e826
2 changed files with 42 additions and 3 deletions
@@ -195,6 +195,33 @@ describe("WebAudioTransport", () => {
expect([transport.routesElement(gone), transport.routesElement(kept)]).toEqual([false, true]);
});
it("keeps a routed element silent once a decoded buffer plays in its place", async () => {
const { transport, mock, gen } = setupTransport(100);
const node = { connect: vi.fn(), disconnect: vi.fn() };
mock.ctx.createMediaElementSource = vi.fn(() => node) as never;
await transport.scheduleMediaElementPlayback(mockEl, 0, 0, 0, 1, gen, 1);
node.connect.mockClear();
node.disconnect.mockClear();
transport.releaseRouted(mockEl);
expect(node.disconnect).toHaveBeenCalled();
expect(node.connect).not.toHaveBeenCalled();
});
it("sounds a handed-over element through the output again once playback stops", async () => {
const { transport, mock, gen } = setupTransport(100);
const node = { connect: vi.fn(), disconnect: vi.fn() };
mock.ctx.createMediaElementSource = vi.fn(() => node) as never;
await transport.scheduleMediaElementPlayback(mockEl, 0, 0, 0, 1, gen, 1);
transport.releaseRouted(mockEl);
node.connect.mockClear();
transport.stopAll();
expect(node.connect).toHaveBeenCalledWith(mock.ctx.destination);
});
it("creates one MediaElementAudioSourceNode per element and context", async () => {
const { transport, mock } = setupTransport(100);
+15 -3
View File
@@ -240,7 +240,7 @@ export class WebAudioTransport {
private _pendingDecodeBytes = new Map<string, number>();
private _failedSrcs = new Set<string>();
private _mediaElementSources = new WeakMap<HTMLMediaElement, MediaElementAudioSourceNode>();
private _handedOver = new WeakSet<HTMLMediaElement>();
private _handedOver = new Set<HTMLMediaElement>();
private _activeSources: ScheduledSource[] = [];
private _masterGain: GainNode | null = null;
/** Where the master bus chain lands; the program meter taps here. */
@@ -1052,12 +1052,12 @@ export class WebAudioTransport {
}
}
/** Drops the routed element's source once a decoded buffer plays in its place; the element then sounds natively. */
/** Drops the routed element's source once a decoded buffer plays in its place; it stays silent until a stop. */
releaseRouted(el: HTMLMediaElement): void {
const routed = this._activeSources.find((s) => s.el === el && s.sourceKind === "media-element");
if (!routed) return;
this._activeSources.splice(this._activeSources.indexOf(routed), 1);
this.dispose(routed);
this.dispose(routed, false);
this._handedOver.add(el);
}
@@ -1132,11 +1132,23 @@ export class WebAudioTransport {
stopAll(): void {
for (const source of this._activeSources) this.dispose(source);
this._activeSources = [];
this.reconnectHandedOver();
this._paused = true;
this._playGeneration += 1;
this.rest();
}
private reconnectHandedOver(): void {
for (const el of this._handedOver) {
try {
if (this._ctx) this._mediaElementSources.get(el)?.connect(this._ctx.destination);
} catch (err) {
swallow("webAudioTransport.reconnectHandedOver", err);
}
}
this._handedOver.clear();
}
/** Stops what plays from media gone from the page, without sending it to the destination as a stop does. */
releaseDetached(): void {
this._activeSources = this._activeSources.filter((source) => {