Skip to main content

Class: AudioManager

Defined in: src/core/AudioManager.ts:91

Manages audio playback synchronized with the manim-web Scene timeline.

Usage:

const audio = new AudioManager();
await audio.addSound('/sounds/click.wav', { time: 0.5 });
audio.play(); // start playback
audio.seek(2.0); // jump to 2 s
const wav = await audio.exportWAV();

Constructors​

Constructor​

new AudioManager(): AudioManager

Returns​

AudioManager

Accessors​

context​

Get Signature​

get context(): AudioContext

Defined in: src/core/AudioManager.ts:145

Get the underlying AudioContext (creates one if needed).

Returns​

AudioContext


hasTimelineClock​

Get Signature​

get hasTimelineClock(): boolean

Defined in: src/core/AudioManager.ts:217

Whether a timeline clock exists (see startTimelineClock).

Returns​

boolean


isPlaying​

Get Signature​

get isPlaying(): boolean

Defined in: src/core/AudioManager.ts:485

Whether audio is currently playing.

Returns​

boolean


pendingLoads​

Get Signature​

get pendingLoads(): number

Defined in: src/core/AudioManager.ts:183

Number of audio files being fetched or decoded right now. A frame-by-frame renderer waits for 0 before it moves the clock, so a clip lands at the timeline time its author intended.

Returns​

number


tracks​

Get Signature​

get tracks(): readonly AudioTrack[]

Defined in: src/core/AudioManager.ts:478

Get a readonly copy of the current track list.

Returns​

readonly AudioTrack[]

Methods​

addSound()​

addSound(url, options): Promise<AudioTrack>

Defined in: src/core/AudioManager.ts:272

Add a sound to the timeline.

Parameters​

url​

string

URL of the audio file

options​

AddSoundOptions = {}

Scheduling and playback options

Returns​

Promise<AudioTrack>

The created AudioTrack (buffer may still be loading)


addSoundAtAnimation()​

addSoundAtAnimation(animation, url, options): Promise<AudioTrack>

Defined in: src/core/AudioManager.ts:316

Add a sound that starts when a given animation begins on the timeline.

The animation must already be scheduled (i.e. have a non-null startTime). If the animation has not been scheduled yet, time falls back to 0.

Parameters​

animation​

Animation

Animation whose start time to sync with

url​

string

URL of the audio file

options​

Omit<AddSoundOptions, "time"> & object = {}

Additional options (time is overridden by animation)

Returns​

Promise<AudioTrack>

The created AudioTrack


clearTracks()​

clearTracks(): void

Defined in: src/core/AudioManager.ts:468

Remove all tracks and stop playback. A running timeline clock keeps its origin, so a scene can reset its clips without moving frame 0.

Returns​

void


connectOutput()​

connectOutput(node): void

Defined in: src/core/AudioManager.ts:591

Connect the master output to an external node (for example an AnalyserNode used for audio-reactive visualization). The node keeps receiving the mixed output until the context is closed.

Parameters​

node​

AudioNode

The destination node to feed from the master gain

Returns​

void


createStreamDestination()​

createStreamDestination(): MediaStreamAudioDestinationNode

Defined in: src/core/AudioManager.ts:577

Get an AudioNode destination suitable for muxing with a MediaRecorder. This creates a MediaStreamAudioDestinationNode that can be combined with a video stream for video+audio export.

Returns​

MediaStreamAudioDestinationNode

MediaStreamAudioDestinationNode connected to the master gain


dispose()​

dispose(): void

Defined in: src/core/AudioManager.ts:661

Release all audio resources.

Returns​

void


exportWAV()​

exportWAV(duration?, sampleRate?): Promise<Blob>

Defined in: src/core/AudioManager.ts:565

Export all audio tracks as a WAV Blob.

Parameters​

duration?​

number

Total duration (seconds). Defaults to getDuration().

sampleRate?​

number = 44100

Sample rate. Defaults to 44100.

Returns​

Promise<Blob>

WAV Blob


getCurrentTime()​

getCurrentTime(): number

Defined in: src/core/AudioManager.ts:406

Get the current playback position on the timeline (seconds). With a timeline clock this is the clock time.

Returns​

number


getDuration()​

getDuration(): number

Defined in: src/core/AudioManager.ts:492

Get the total duration of the audio timeline (end of the latest track).

Returns​

number


loadBuffer()​

loadBuffer(url): Promise<AudioBuffer>

Defined in: src/core/AudioManager.ts:158

Load and decode an audio file, with caching.

Parameters​

url​

string

URL of the audio resource

Returns​

Promise<AudioBuffer>

Decoded AudioBuffer


mixdown()​

mixdown(duration?, sampleRate?): Promise<AudioBuffer>

Defined in: src/core/AudioManager.ts:512

Render all audio tracks into a single stereo AudioBuffer (offline).

Parameters​

duration?​

number

Total duration to render (seconds). Defaults to getDuration().

sampleRate?​

number = 44100

Output sample rate. Defaults to 44100.

Returns​

Promise<AudioBuffer>

Mixed-down AudioBuffer


pause()​

pause(): void

Defined in: src/core/AudioManager.ts:352

Pause audio playback, remembering the current position.

Returns​

void


play()​

play(): void

Defined in: src/core/AudioManager.ts:333

Start (or resume) audio playback from the current position.

Returns​

void


removeTrack()​

removeTrack(trackId): void

Defined in: src/core/AudioManager.ts:449

Remove a track by id.

Parameters​

trackId​

number

Returns​

void


seek()​

seek(time): void

Defined in: src/core/AudioManager.ts:372

Seek to an absolute time on the timeline (seconds). If currently playing, audio restarts from the new position.

Parameters​

time​

number

Returns​

void


setGlobalGain()​

setGlobalGain(value): void

Defined in: src/core/AudioManager.ts:420

Set the master output volume.

Parameters​

value​

number

Gain value (0 = silent, 1 = unity)

Returns​

void


setSceneElapsed()​

setSceneElapsed(source): void

Defined in: src/core/AudioManager.ts:212

Tell the manager how much scene time has passed (seconds since the scene's first play() / wait()). Only timelineNow reads it, when it starts the clock, so plain transport use is not affected.

Parameters​

source​

() => number

Returns​

void


setTrackGain()​

setTrackGain(trackId, value): void

Defined in: src/core/AudioManager.ts:432

Set the gain for a specific track.

Parameters​

trackId​

number

Track identifier

value​

number

Gain value (0 = silent, 1 = unity)

Returns​

void


startTimelineClock()​

startTimelineClock(origin): void

Defined in: src/core/AudioManager.ts:200

Start the timeline clock at origin (a performance.now() value). After this call, timelineNow measures from origin, and the transport follows the clock (see timelineNow). A video renderer calls this at frame 0.

Parameters​

origin​

number = ...

Returns​

void

Post​

this.hasTimelineClock

Post​

this.timelineNow() >= 0


stop()​

stop(): void

Defined in: src/core/AudioManager.ts:394

Stop playback and reset to the beginning.

Returns​

void


timelineNow()​

timelineNow(): number

Defined in: src/core/AudioManager.ts:242

The current timeline time in seconds, to place a clip at "now".

When no clock runs yet, the first call starts one at the later of the transport position (getCurrentTime()) and the scene time (see setSceneElapsed): seek(5) then a narration places the clip at 5, and a narration 3 s into a live scene lands at 3. From then on the transport follows the clock:

  • a clip added while the clock runs plays at once;
  • pause() stops the clock and play() resumes it from the same time;
  • seek(t) moves the clock to t and reschedules every clip from t (Scene.play() skips its restart seek(0) while a clock exists);
  • stop() removes the clock and restores plain transport behaviour.

A scene reports the time since its first play() / wait() through setSceneElapsed, so a narration placed later lands at that time without a manual startTimelineClock.

Returns​

number

Post​

result >= 0

Post​

old.hasTimelineClock === false => result >= old.getCurrentTime()


audioBufferToWAV()​

static audioBufferToWAV(buffer): Blob

Defined in: src/core/AudioManager.ts:603

Encode an AudioBuffer as a WAV Blob (PCM 16-bit).

Parameters​

buffer​

AudioBuffer

Returns​

Blob