Constructor
new AudioManager(optsopt)
| Name | Type | Attributes | Description |
|---|---|---|---|
opts | AudioManager. | <optional> | Configuration options. |
| Name | Type | Description |
|---|---|---|
opts | AudioManager. | Current configuration options. |
audioCtx | AudioContext | | Audio context (null until initialized). |
gainSpeech | GainNode | | Speech gain node. |
gainBackground | GainNode | | Background/music gain node. |
gainStream | GainNode | | Streaming gain node. |
analyser | AnalyserNode | | Analyser for metering/lipsync. |
convolver | ConvolverNode | | Reverb convolver. |
speechBufferSource | AudioBufferSourceNode | | Last created speech buffer source. |
backgroundBufferSource | AudioBufferSourceNode | | Last created background buffer source. |
workletLoaded | boolean | Whether the worklet is loaded. |
streamNode | AudioWorkletNode | | Worklet node for streaming. |
Members
(static, constant) AUDIO_DEFAULTS :AudioManager.AudioManagerOptions
Default options of AudioManager.
{
sampleRate: null,
mixer: { speech: 1, background: 0 },
analyser: { fftSize: 256, smoothing: 0.1, minDb: -70, maxDb: -10 },
reverb: { ir: "dry", normalize: false },
workletUrl: null
}
Methods
createBackgroundSource() → {AudioBufferSourceNode}
Create a one-shot BufferSource for background playback.
- Type:
- AudioBufferSourceNode
createSpeechSource() → {AudioBufferSourceNode}
Create a one-shot BufferSource for speech playback.
- Type:
- AudioBufferSourceNode
createStreamNode() → {AudioWorkletNode}
Create a fresh AudioWorkletNode for streaming playback. Caller wires up message handlers on node.port.
If worklet is not loaded (call ensureWorkletLoaded() first).
- Type
- Error
- Type:
- AudioWorkletNode
(async) dispose() → {Promise.<void>}
Dispose all audio resources. Closes context and releases nodes. Safe to call multiple times (idempotent).
- Type:
- Promise.<void>
(async) ensureWorkletLoaded() → {Promise.<void>}
Ensure the AudioWorklet module is loaded once per AudioContext.
If AudioContext is unavailable/closed, URL cannot be resolved, or loading times out.
- Type
- Error
- Type:
- Promise.<void>
getAnalyserNode() → {AnalyserNode}
Returns the analyser node for metering.
- Type:
- AnalyserNode
rebuild(sampleRate) → {void}
Rebuild the entire graph with a new sample rate.
| Name | Type | Description |
|---|---|---|
sampleRate | number | Target sample rate in Hz. |
- Type:
- void
(async) resumeIfNeeded() → {Promise.<void>}
Resume the AudioContext if suspended/interrupted (autoplay policies). No-op if context is absent.
- Type:
- Promise.<void>
setMixerGain(speechopt, backgroundopt, fadeSecsopt) → {void}
Set speech/background gains with optional exponential fade.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
speech | number | | <optional> | null | Target speech gain (linear, typically 0..1). Pass null to keep current. |
background | number | | <optional> | null | Target background gain (linear, typically 0..1). Pass null to keep current. |
fadeSecs | number | <optional> | 0 | Exponential fade duration in seconds (0 = instant). |
- Type:
- void
(async) setReverb(input, optsopt) → {Promise.<void>}
Set the impulse response buffer used by the internal ConvolverNode.
Passing a non-null AudioBuffer enables reverb with that IR. Passing null clears the buffer and effectively bypasses the convolver, which is the cheapest option CPU-wise. behavior (keep the convolver active but with no tail), create a Dirac buffer yourself and pass it here.
| Name | Type | Attributes | Description |
|---|---|---|---|
input | string | | Allowed strings: "off", "dry". | |
opts | AudioManager. | <optional> |
On fetch/decode failures (when
inputis URL/ArrayBuffer).- Type
- Error
- Type:
- Promise.<void>
// Enable reverb:
audio.setReverb(irBuffer);// Bypass (no reverb, lowest CPU cost):
audio.setReverb(null);// Optional: emulate a dry impulse (no tail, convolver stays active):
const sr = audio.audioCtx.sampleRate;
const dry = audio.audioCtx.createBuffer(2, sr, sr);
dry.getChannelData(0)[0] = 1.0;
dry.getChannelData(1)[0] = 1.0;
audio.setReverb(dry);stopBackground() → {void}
Stop current background audio if playing.
- Type:
- void
Type Definitions
AudioAnalyserOptions
Analyser settings for metering/lip-sync.
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
fftSize | number | <optional> | 256 | FFT size (power of two). |
smoothing | number | <optional> | 0.1 | Smoothing factor in [0..1]. |
minDb | number | <optional> | -70 | Minimum decibels. |
maxDb | number | <optional> | -10 | Maximum decibels. |
AudioManagerOptions
Audio engine options for AudioManager.
- Object
| Name | Type | Attributes | Description |
|---|---|---|---|
sampleRate | number | | <optional> | Optional AudioContext sampleRate hint. |
mixer | AudioManager. | <optional> | Mixer gains for speech/background. |
analyser | AudioManager. | <optional> | Analyser configuration. |
reverb | AudioManager. | <optional> | Reverb/convolver configuration. |
workletUrl | string | | <optional> | URL to the AudioWorklet module; if omitted, a resolver is used. |
AudioMixerOptions
Mixer (linear gains). Typical range: 0..1 (but not clamped).
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
speech | number | <optional> | 1 | Gain for speech path. |
background | number | <optional> | 0 | Gain for background/music path. |
AudioReverbOptions
Reverb (convolver) settings. ir accepted values:
- 'dry': use a unit impulse (no tail; convolver remains active)
- 'off': disable/bypass convolver (lowest CPU usage)
- null: same as 'off'
- string: URL to an impulse response file (fetched and decoded)
- ArrayBuffer | AudioBuffer: preloaded IR data
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
ir | string | | <optional> | "dry" | Impulse response source. Allowed strings: "dry", "off". |
normalize | boolean | <optional> | false | Convolver normalization flag. |
PlayBackgroundOptions
Options for AudioManager#playBackgroundFromUrl.
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
loop | boolean | <optional> | true | Loop playback. |
rate | number | <optional> | 1.0 | playbackRate multiplier. |
ReverbSetOptions
Options for AudioManager#setReverb.
- Object
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
normalize | boolean | <optional> | false | Convolver normalization flag. |