Overview
UAC (USB Audio Class) is a standardized protocol for transmitting audio data over USB. The ESP32_USB_STREAM library implements a UAC host driver that supports both microphone input (capture) and speaker output (playback) simultaneously.The library can handle one microphone stream and one speaker stream concurrently, along with one UVC video stream.
Audio Streaming Architecture
The library manages two independent audio streams:Microphone (Input) Streaming
Microphone streaming captures audio data from a USB microphone or audio input device.Configuration
Audio Format Parameters
Number of audio channels:
1- Mono audio2- Stereo audioUAC_CH_ANY- Accept any channel configuration
Bit depth per sample:
8- 8-bit audio16- 16-bit audio (CD quality)24- 24-bit audio (high quality)32- 32-bit audioUAC_BITS_ANY- Accept any bit resolution
Sample rate in Hz:
8000- Phone quality16000- Wideband speech44100- CD quality48000- Professional audio96000- High-resolution audioUAC_FREQUENCY_ANY- Accept any sample rate
Reading Microphone Data
There are two ways to receive microphone data:Method 1: Callback Function (Recommended)
Method 2: Manual Read from Buffer
Microphone Frame Structure
Themic_frame_t structure contains:
| Field | Type | Description |
|---|---|---|
data | void* | Pointer to audio sample data |
data_bytes | uint32_t | Size of audio data in bytes |
bit_resolution | uint16_t | Bits per sample (8, 16, 24, 32) |
samples_frequence | uint32_t | Sample rate in Hz |
Speaker (Output) Streaming
Speaker streaming sends audio data to a USB speaker or audio output device.Configuration
Writing Speaker Data
Concurrent Microphone and Speaker
You can use both microphone and speaker simultaneously:Buffer Management
The library uses ring buffers to handle audio data flow:Buffer Size Guidelines
Small Buffers (Low Latency)
Small Buffers (Low Latency)
Pros:
- Minimal audio delay
- Lower memory usage
- Better for real-time applications
- Higher risk of buffer underrun/overrun
- More CPU interrupts
- Less tolerance for processing delays
Large Buffers (High Latency)
Large Buffers (High Latency)
Pros:
- Stable audio streaming
- Tolerates processing variations
- Fewer buffer errors
- Increased audio latency
- Higher memory usage
- Not suitable for real-time interaction
Volume and Mute Controls
Microphone Controls
Speaker Controls
Volume and mute control availability depends on the USB audio device. Not all devices support these features.
Suspend and Resume
You can independently control each audio stream:Microphone Suspend/Resume
Speaker Suspend/Resume
Dynamic Audio Configuration
You can change audio parameters while suspended:Querying Audio Capabilities
Get Available Audio Formats
Common Audio Formats
Phone Quality
8 kHz, Mono, 8-bit
- Bandwidth: 8 KB/s
- Use: Voice calls
Wideband Speech
16 kHz, Mono, 16-bit
- Bandwidth: 32 KB/s
- Use: VoIP, voice commands
CD Quality
44.1 kHz, Stereo, 16-bit
- Bandwidth: 176 KB/s
- Use: Music playback
Example: Complete UAC Setup
Best Practices
Buffer Management
Buffer Management
- Size buffers appropriately for your latency requirements
- Monitor buffer overflow/underflow conditions
- Use larger buffers if experiencing audio glitches
- Free buffers when streaming stops
Callback Performance
Callback Performance
- Keep mic callbacks extremely fast
- Avoid Serial.print() in production code
- Use queues to pass data to other tasks
- Never call UAC functions from within callbacks
Sample Rate Matching
Sample Rate Matching
- Ensure continuous data flow to speaker at exact sample rate
- Use FreeRTOS timers for precise timing if needed
- Match mic and speaker rates for echo/passthrough
- Consider sample rate conversion for format mismatches
Audio Quality
Audio Quality
- Start with lower sample rates and increase if stable
- Use 16-bit resolution as a good quality/performance balance
- Monitor CPU usage during streaming
- Test with different USB audio devices for compatibility
Troubleshooting
No audio data received
No audio data received
- Check device is properly connected
- Verify UAC configuration matches device capabilities
- Ensure buffer size is sufficient
- Check callback is registered before start()
Audio glitches or dropouts
Audio glitches or dropouts
- Increase buffer size for more tolerance
- Reduce callback processing time
- Lower sample rate or reduce bit depth
- Check for CPU or memory constraints
Volume/mute controls don't work
Volume/mute controls don't work
- Not all devices support these features
- Check device USB descriptor for feature unit
- Try different control values
- Some devices may have hardware-only controls
Related Topics
UVC Streaming
Learn about USB video streaming
Hardware Requirements
ESP32 SoC and USB wiring requirements