Why Audio Worklet?
Audio Worklets run on a separate thread from the main UI, ensuring:- Consistent, low-latency audio processing
- No audio glitches from UI blocking
- Efficient real-time conversion and buffering
AudioProcessor Class
Create anaudio-processor.js file in your public/ directory:
audio-processor.js
Understanding the Conversion
Extract audio input
The Structure:
process() method receives audio data as inputs:inputs[0] is the first audio input (the microphone), input[0] is the first channel (we use mono audio), and data arrives as Float32Array with values between -1.0 and 1.0.Convert Float32 to Int16
Transform the floating-point samples to 16-bit integers:Conversion formula:
- Float32 range:
-1.0to1.0 - Int16 range:
-32768to32767 - Multiply each sample by
32767to scale to Int16
This conversion maintains audio fidelity while matching AssemblyAI’s required PCM format.
Send to main thread
Post the converted audio data back to the main thread:The
ArrayBuffer is transferred efficiently to the main thread where buffering logic handles it.Buffering Audio Chunks
The main thread receives converted audio and buffers it into 100ms chunks before sending to the WebSocket.Receiving Processed Audio
In your main JavaScript file:public/index.js
Buffer Merging
Combine new audio with the existing queue:public/index.js
- Create new array large enough for both buffers
- Copy existing buffer to start
- Append new buffer to end
Calculating Buffer Duration
audioBufferQueue.length: Number of samplesaudioContext.sampleRate: 16000 samples per second/ sampleRate: Converts samples to seconds* 1000: Converts seconds to milliseconds
Creating 100ms Chunks
- Check if buffer has at least 100ms of audio (line 1)
- Calculate samples needed:
16000 × 0.1 = 1600 samples(line 2) - Extract first 100ms as
Uint8Arrayfor WebSocket (line 3) - Remove sent samples from queue (line 4)
- Call callback to send chunk (line 6)
Why 100ms Chunks?
Sending audio in 100ms intervals provides:- Low latency: Quick transcription responses
- Efficient bandwidth: Not too small (overhead) or large (delay)
- Stable processing: Consistent chunk size for the streaming API
Data Flow Summary
Error Handling
The processor includes basic error handling:Next Steps
With audio properly formatted:- Learn how to connect to AssemblyAI’s WebSocket
- Send your audio chunks for real-time transcription