moeru-ai/airi · error · Error

AudioContext not initialized

Error message

AudioContext not initialized

What it means

Thrown synchronously by createAudioSource when the module-level singleton AudioContext does not exist or isReady is false. isReady only becomes true after initializeAudioContext() resolves successfully, so any node factory called before a completed init — or after a failed/closed one — throws immediately.

Solutions

  1. Await initializeAudioContext(48000) once during app startup before creating any sources, analysers, or gain nodes
  2. Use one fixed requested sample rate across the app — a different rate forces re-init during which factories throw
  3. If initialization failed earlier, surface and fix that error first; factories keep throwing until a successful init
  4. In tests, mock the audio-context module or initialize it explicitly before calling helpers

Example fix

// before
const source = createAudioSource(stream) // Error: AudioContext not initialized

// after
await initializeAudioContext(48000)
const source = createAudioSource(stream)
Defensive patterns

Strategy: validation

Validate before calling

// Guarantee readiness once, before any factory call
export async function getReadyAudio() {
  return await initializeAudioContext(48000)
}
// usage:
await getReadyAudio()
const source = createAudioSource(stream)

Try / catch

try {
  const source = createAudioSource(stream)
}
catch (err) {
  if (err instanceof Error && err.message === 'AudioContext not initialized') {
    await initializeAudioContext(48000)
    retryOnce() // context is now ready
  }
}

Prevention

When it happens

Trigger: Calling createAudioSource(mediaStream) before awaiting initializeAudioContext(), during a re-initialization triggered by requesting a different sample rate, or after a previous initializeAudioContext() rejection whose error was swallowed.

Common situations: Component setup code runs ahead of the async audio bootstrap promise; getUserMedia callback fires before init completes; autoplay policy or a prior error tore the context down; unit tests touching audio helpers without initializing or mocking the module.

Related errors


AI-assisted analysis of moeru-ai/airi@677329427f (2026-08-18). Data as JSON: /api/errors/65c691ef630b48f4. Report an issue: GitHub.

Appendix: source

Thrown at packages/audio/src/audio-context/index.ts:148

    return context
  }
  catch (err) {
    error = errorMessageFromValue(err)
    isReady = false
    workletLoaded = false
    notifyListeners()
    console.error('Failed to initialize AudioContext:', err)
    throw err
  }
  finally {
    isInitializing = false
    notifyListeners()
  }
}

export function createAudioSource(mediaStream: MediaStream): MediaStreamAudioSourceNode {
  if (!context || !isReady) {
    throw new Error('AudioContext not initialized')
  }

  const source = context.createMediaStreamSource(mediaStream)
  activeSources.add(source)
  return source
}

export function createAudioAnalyser(options?: Partial<{
  fftSize: number
  smoothingTimeConstant: number
  minDecibels: number
  maxDecibels: number
}>): AnalyserNode {
  if (!context || !isReady) {
    throw new Error('AudioContext not initialized')
  }

  const analyser = context.createAnalyser()

View on GitHub (pinned to 677329427f)