You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The AudioContext class represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode. It is the primary interface for creating and managing Web Audio API nodes. Audio contexts control the creation of nodes, the execution of audio processing, and the decoding of audio data. You must create an AudioContext before doing anything with Web Audio. Note that browsers require a user gesture (click, tap) before an AudioContext can start producing sound.
Constructors
Signature
Description
AudioContext()
Creates a new audio context with default settings.
AudioContext(AudioContextOptions options)
Creates a new audio context with the specified options (sample rate, latency hint, sink ID).
AudioContext(SpawnJSObjectReference _ref)
Deserialization constructor.
Properties (inherited from BaseAudioContext)
Property
Type
Description
CurrentTime
double
An ever-increasing hardware time in seconds used for scheduling. Starts at 0.
Destination
AudioDestinationNode
The final destination of all audio in the context (the audio output device).
Listener
AudioListener
The AudioListener used for 3D audio spatialization.
SampleRate
float
The sample rate in samples per second used by all nodes in this context. Cannot be changed after creation.
State
string
Current state: "suspended", "running", or "closed".
AudioWorklet
AudioWorklet
The AudioWorklet object for creating custom audio processing nodes.
Methods
AudioContext-Specific Methods
Method
Return Type
Description
Close()
Task
Closes the audio context, releasing system audio resources.
Resume()
Task
Resumes a suspended audio context (required after user gesture).
Suspend()
Task
Suspends the audio context, halting audio hardware access and reducing CPU/battery usage.
GetOutputTimestamp()
AudioTimestamp
Returns timestamp information relating to the current audio context.
SetSinkId(string sinkId)
Task
Sets the output audio device by device ID (from MediaDevices.EnumerateDevices()).
SetSinkId(AudioSinkOptions options)
Task
Sets the output sink options. Use type: "none" to process audio without playing it.
CreateMediaStreamSource(MediaStream stream)
MediaStreamAudioSourceNode
Creates a source node from a MediaStream (e.g., microphone input).
Creates a custom waveform for use with an OscillatorNode.
CreateStereoPanner()
StereoPannerNode
Creates a stereo panning node (left/right).
CreateWaveShaper()
WaveShaperNode
Creates a wave shaper node for non-linear distortion.
DecodeAudioData(ArrayBuffer audioData)
Task<AudioBuffer>
Decodes audio file data (MP3, WAV, OGG, etc.) into an AudioBuffer.
Events
Event
Type
Description
OnStateChange
ActionEvent<Event>
Fired when the context state changes ("suspended", "running", "closed").
Example
// Create an audio contextusingvaraudioCtx=newAudioContext();Console.WriteLine($"Sample rate: {audioCtx.SampleRate} Hz");Console.WriteLine($"State: {audioCtx.State}");// Resume after user gesture (browsers require this)awaitaudioCtx.Resume();// Play a simple tone with an oscillatorusingvaroscillator=audioCtx.CreateOscillator();usingvargainNode=audioCtx.CreateGain();oscillator.Type=OscillatorType.Sine;oscillator.Frequency.Value=440;// A4 notegainNode.Gain.Value=0.5f;// 50% volumeoscillator.Connect(gainNode);gainNode.Connect(audioCtx.Destination);oscillator.Start();// Stop after 2 secondsoscillator.Stop((float)(audioCtx.CurrentTime+2.0));// Process microphone input with an analyserusingvarstream=awaitmediaDevices.GetUserMedia(newMediaStreamConstraints{Audio=true});usingvarsource=audioCtx.CreateMediaStreamSource(stream);usingvaranalyser=audioCtx.CreateAnalyser();analyser.FftSize=2048;source.Connect(analyser);analyser.Connect(audioCtx.Destination);// Decode and play an audio fileusingvarresponse=awaitJS.CallAsync<Response>("fetch","audio/music.mp3");usingvarbuffer=awaitresponse.ArrayBuffer();usingvaraudioBuffer=awaitaudioCtx.DecodeAudioData(buffer);usingvarbufferSource=audioCtx.CreateBufferSource();bufferSource.Buffer=audioBuffer;bufferSource.Connect(audioCtx.Destination);bufferSource.Start();// Apply a low-pass filterusingvarfilter=audioCtx.CreateBiquadFilter();filter.Type="lowpass";filter.Frequency.Value=1000;source.Disconnect();source.Connect(filter);filter.Connect(audioCtx.Destination);// Monitor state changes (named method for proper cleanup)audioCtx.OnStateChange+=AudioCtx_OnStateChange;// Suspend to save batteryawaitaudioCtx.Suspend();// Clean up event handler before disposalaudioCtx.OnStateChange-=AudioCtx_OnStateChange;// Close when completely doneawaitaudioCtx.Close();voidAudioCtx_OnStateChange(Eventevt){Console.WriteLine($"Audio context state: {audioCtx.State}");evt.Dispose();}