createMicrophone()
Factory function that creates a microphone manager object with methods for requesting permissions, starting, and stopping audio recording. Defined inpublic/index.js:12-59
Function Signature
Return Value
Returns an object with the following methods:requestPermission()- Request microphone access from the userstartRecording(callback)- Start recording audio and invoke callback with audio datastopRecording()- Stop recording and clean up resources
Internal State
The returned object maintains the following internal state:Methods
requestPermission()
Requests microphone access permission from the user.Method Signature
Return Value
Returns aPromise<void> that resolves when permission is granted.
Implementation
public/index.js:20-22
Error Handling
Throws an error if:- User denies microphone permission
- No microphone device is available
- Browser doesn’t support
getUserMedia
startRecording()
Starts recording audio and processes it through an AudioWorklet.Method Signature
Parameters
Callback function invoked with audio data chunks. Receives a
Uint8Array containing 100ms of audio data at 16kHz sample rate.Callback Signature:Return Value
Returns aPromise<void> that resolves when recording has started.
Implementation Details
The method performs the following steps:-
Request stream if not available:
-
Create AudioContext with 16kHz sample rate:
-
Set up AudioWorklet processing:
-
Process audio messages with buffering:
public/index.js:23-52
Audio Buffering Strategy
- Audio data is buffered until at least 100ms of audio is accumulated
- Each callback receives exactly 100ms of audio (1,600 samples at 16kHz)
- Remaining audio is kept in the buffer for the next chunk
- This ensures consistent chunk sizes for optimal streaming performance
stopRecording()
Stops recording and cleans up all audio resources.Method Signature
Return Value
Returnsvoid (no return value).
Implementation
public/index.js:53-57
Cleanup Operations
- Stops all media stream tracks (releases microphone access)
- Closes the AudioContext (frees audio processing resources)
- Clears the audio buffer queue (frees memory)
Usage Example
Basic Usage
Complete Integration Example
Frompublic/index.js:80-102:
Technical Specifications
Audio Configuration
Sample rate in Hz. AssemblyAI’s API requires 16kHz audio.
Audio processing latency hint. Options:
'interactive', 'balanced', 'playback'.Duration of each audio chunk in milliseconds.
Number of samples per chunk (16000 Hz × 0.1 seconds = 1600 samples).
Browser Compatibility
Requires browser support for:navigator.mediaDevices.getUserMedia()AudioContextAudioWorkletAPIMediaStreamAPI