Microphone Object Structure
The microphone functionality is encapsulated in a factory function that returns an object with three methods:public/index.js
Requesting Microphone Permission
Configuring Audio Context
AssemblyAI’s streaming API requires audio at 16kHz sample rate in 16-bit PCM format.Create AudioContext with 16kHz sample rate
Configure the Configuration explained: The
AudioContext when starting recording:public/index.js
sampleRate: 16000 sets audio sampling at 16kHz (required by AssemblyAI), and latencyHint: 'balanced' balances between latency and audio quality.Create media stream source
Connect the microphone stream to the audio processing pipeline:This creates an
public/index.js
AudioNode from the microphone stream that can be processed by the Web Audio API.Complete Start Recording Function
Here’s the full implementation that combines all the pieces:public/index.js
- Request stream if not already available (line 2)
- Create 16kHz AudioContext (lines 4-7)
- Set up audio processing pipeline (lines 9-14)
- Buffer audio and send in 100ms chunks (lines 16-29)
Stopping Recording
Clean up resources when recording stops:public/index.js
- Stop all media tracks to release the microphone
- Close the AudioContext to free resources
- Clear the audio buffer queue
Usage Example
Here’s how to use the microphone object:Next Steps
Now that you can capture microphone audio:- Learn about the Audio Worklet implementation that converts audio format
- Set up WebSocket integration to send audio data