Skip to content

Reuse AudioContext across WebAudioAdapter setup calls #802

Description

@kraenhansen

Problem

WebAudioAdapter.setupOutputAnalysis() creates a new AudioContext (and reloads the audio worklet module) every time it's called. If the agent reconnects and republishes a track, TrackSubscribed fires again, calling setupAudioCapturesetupOutputAnalysis, which creates a second AudioContext without closing the first — leaking it.

The same pattern exists in setupInputAnalysis.

Proposed solution

Reuse the AudioContext across calls instead of closing and recreating it:

  • Keep the AudioContext alive for the lifetime of the adapter
  • On re-call, disconnect old nodes and create new ones on the existing context
  • The worklet module is already loaded on the context, so subsequent calls skip the expensive loadRawAudioProcessor() await
  • Only close the context in cleanup()

This would also simplify the class by removing the need for cleanup-on-re-call guards.

Context

Identified during the platform isolation review (#784). The current code works for the common single-call case, but leaks resources in reconnection scenarios.

Files

  • packages/client/src/platform/web/webAudioAdapter.tssetupOutputAnalysis() and setupInputAnalysis()

Metadata

Metadata

Assignees

Labels

@elevenlabs/clientIssues and PRs related to the `@elevenlabs/client` package.refactorCode refactoring or restructuring

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions