{"record":{"id":"65c691ef630b48f4","repo":"moeru-ai/airi","slug":"audiocontext-not-initialized","errorCode":null,"errorMessage":"AudioContext not initialized","messagePattern":"AudioContext not initialized","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/audio/src/audio-context/index.ts","lineNumber":148,"sourceCode":"    return context\n  }\n  catch (err) {\n    error = errorMessageFromValue(err)\n    isReady = false\n    workletLoaded = false\n    notifyListeners()\n    console.error('Failed to initialize AudioContext:', err)\n    throw err\n  }\n  finally {\n    isInitializing = false\n    notifyListeners()\n  }\n}\n\nexport function createAudioSource(mediaStream: MediaStream): MediaStreamAudioSourceNode {\n  if (!context || !isReady) {\n    throw new Error('AudioContext not initialized')\n  }\n\n  const source = context.createMediaStreamSource(mediaStream)\n  activeSources.add(source)\n  return source\n}\n\nexport function createAudioAnalyser(options?: Partial<{\n  fftSize: number\n  smoothingTimeConstant: number\n  minDecibels: number\n  maxDecibels: number\n}>): AnalyserNode {\n  if (!context || !isReady) {\n    throw new Error('AudioContext not initialized')\n  }\n\n  const analyser = context.createAnalyser()","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/moeru-ai/airi/blob/677329427f32468c74b17f3ec47eeca4e05bec65/packages/audio/src/audio-context/index.ts#L130-L166","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Await initializeAudioContext(48000) once during app startup before creating any sources, analysers, or gain nodes","Use one fixed requested sample rate across the app — a different rate forces re-init during which factories throw","If initialization failed earlier, surface and fix that error first; factories keep throwing until a successful init","In tests, mock the audio-context module or initialize it explicitly before calling helpers"],"exampleFix":"// before\nconst source = createAudioSource(stream) // Error: AudioContext not initialized\n\n// after\nawait initializeAudioContext(48000)\nconst source = createAudioSource(stream)","handlingStrategy":"validation","validationCode":"// Guarantee readiness once, before any factory call\nexport async function getReadyAudio() {\n  return await initializeAudioContext(48000)\n}\n// usage:\nawait getReadyAudio()\nconst source = createAudioSource(stream)","typeGuard":null,"tryCatchPattern":"try {\n  const source = createAudioSource(stream)\n}\ncatch (err) {\n  if (err instanceof Error && err.message === 'AudioContext not initialized') {\n    await initializeAudioContext(48000)\n    retryOnce() // context is now ready\n  }\n}","preventionTips":["Initialize the audio module once at app bootstrap and share the promise","Use one fixed sample rate everywhere to avoid re-init windows","Never swallow initializeAudioContext errors — every later factory call depends on its success"],"tags":["web-audio","audio-context","initialization","media-stream"],"backgroundTag":"audiocontext-not-initialized","analyzedSha":"677329427f32468c74b17f3ec47eeca4e05bec65","analyzedAt":"2026-08-18T17:29:58.153Z","contentChangedAt":"2026-08-18T17:29:58.153Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}