Overview
The@proj-airi/audio package provides audio processing utilities including AudioContext management, audio encoding/decoding, and worklet-based audio processing.
Installation
Package Structure
The package provides three main export paths:Audio Context Management
Manage Web Audio API context with automatic initialization and cleanup.initializeAudioContext()
Initialize or get the shared AudioContext.number
default:"48000"
Desired sample rate in Hz. The actual sample rate will be at least 48000Hz for high quality.
AudioContext
The initialized AudioContext instance
Audio Context State
Get current audio context state:State Interface
Subscribe to State Changes
Audio Node Creation
createAudioSource()
Create an audio source from a MediaStream.MediaStream
required
MediaStream to create source from
MediaStreamAudioSourceNode
Created audio source node
createAudioAnalyser()
Create an analyser node for audio visualization.number
FFT size for frequency analysis (power of 2)
number
Smoothing between frames (0-1)
number
Minimum decibel value for FFT scaling
number
Maximum decibel value for FFT scaling
createAudioGainNode()
Create a gain node for volume control.number
default:"1"
Initial gain value (0-1)
Audio Resampling
createResamplingWorkletNode()
Create a worklet node for real-time audio resampling.AudioNode
required
Source audio node to resample
number
default:"48000"
Input sample rate in Hz
number
default:"16000"
Output sample rate in Hz
number
default:"1"
Number of audio channels
number
default:"2"
Resampling quality (0-4, higher is better)
number
default:"4096"
Processing buffer size
Audio Encoding
WAV Encoding
Encode audio data to WAV format.ArrayBufferLike
required
Float32 audio samples
number
required
Sample rate in Hz
number
default:"1"
Number of audio channels
ArrayBuffer
Complete WAV file as ArrayBuffer
string
Base64-encoded WAV file
Resource Management
Node Cleanup
Remove nodes when done:Context Suspension
Suspend and resume audio context:Complete Cleanup
Clean up all resources:Complete Example
Microphone recording with resampling:Best Practices
Initialize early
Initialize early
Initialize AudioContext early in your app, preferably on user interaction due to browser autoplay policies.
Use high sample rates
Use high sample rates
Use 48kHz or higher for input to ensure quality. Resample down as needed for processing.
Clean up resources
Clean up resources
Always remove nodes and clean up the context when done to prevent memory leaks.
Handle errors
Handle errors
Wrap audio operations in try-catch blocks and check the error state.
Suspend when inactive
Suspend when inactive
Suspend the audio context when not actively processing to save CPU.
Browser Compatibility
The package uses Web Audio API features:- AudioContext
- AudioWorklet
- MediaStream
- Chrome/Edge 66+
- Firefox 76+
- Safari 14.1+
Next Steps
Stage UI Package
Integrate audio with UI components
Server SDK
Stream audio to server runtime
