AudioProcessor Class
AnAudioWorkletProcessor that converts Float32 audio data to Int16 format for streaming to AssemblyAI’s real-time transcription service.
Defined in audio-processor.js:3-25
Overview
TheAudioProcessor class extends the browser’s AudioWorkletProcessor API to process audio samples in real-time. It converts 32-bit floating-point audio data to 16-bit integer format, which is the required format for AssemblyAI’s streaming API.
Class Definition
process() Method
Processes incoming audio data and converts it from Float32 to Int16 format.Method Signature
Parameters
Array of inputs, where each input is an array of channels, and each channel contains Float32Array audio samples.
Return Value
Returnsboolean:
true- Keep the processor alive and continue processingfalse- Stop processing (returned when an error occurs)
Audio Conversion
The processor performs Float32 to Int16 conversion using the following algorithm:Conversion Constant
audio-processor.js:1
Conversion Process
- Extract Float32 channel data (values range from -1.0 to 1.0)
- Convert to Int16 by multiplying by
MAX_16BIT_INT(32767) - Transfer the buffer to the main thread via
postMessage()
audio-processor.js:12-17
Message Posting
The processor posts messages to the main thread containing the converted audio data:An ArrayBuffer containing Int16 audio samples ready for transmission to AssemblyAI.
Error Handling
The processor includes error handling for missing inputs or channel data:- Logs the error to the console
- Returns
falseto stop processing
Usage Example
The AudioProcessor is loaded and instantiated in the main application:public/index.js:32-51.
Technical Notes
- The processor runs on a separate audio thread for optimal performance
- Audio samples are processed in chunks determined by the browser’s audio system
- The conversion maintains audio quality while reducing bit depth from 32 to 16 bits
- The 16-bit format is more efficient for network transmission and compatible with AssemblyAI’s API