UAC Microphone Capture Example
This example demonstrates how to capture audio from a USB microphone using the USB Audio Class (UAC) protocol. It shows configuration of audio format, receiving audio data via callbacks, and controlling microphone settings.What This Example Demonstrates
- Initializing USB audio capture (microphone)
- Configuring audio format (sample rate, bit depth, channels)
- Receiving audio data through callbacks
- Controlling microphone volume and mute
- Suspending and resuming audio capture
Hardware Setup
Required Components:- ESP32-S2 or ESP32-S3 development board
- USB microphone (UAC-compatible)
- USB OTG cable or adapter
- Connect USB microphone to ESP32’s USB port
- Connect ESP32 to computer via serial for monitoring
- Ensure adequate power supply
Complete Code
Code Explanation
1. Microphone Callback Function
static void onMicFrameCallback(mic_frame_t *frame, void *ptr)- Called when audio data is availableframe->bit_resolution- Audio bit depth (8, 16, 24, or 32 bits)frame->samples_frequence- Sample rate in Hz (e.g., 16000, 44100, 48000)frame->data_bytes- Number of bytes in this audio bufferframe->data- Pointer to actual audio samples (not shown in print, but available for processing)
2. UAC Configuration
UAC_CH_ANY- Accept any channel count (mono/stereo)UAC_BITS_ANY- Accept any bit depth (8/16/24/32-bit)UAC_FREQUENCY_ANY- Accept any sample rate6400- Input buffer size in bytes (6.4KB)
UAC_CH_ANY - Speaker channel configuration
6. UAC_BITS_ANY - Speaker bit depth
7. UAC_FREQUENCY_ANY - Speaker sample rate
8. 6400 - Output buffer size in bytes
Why configure both? The library supports simultaneous microphone and speaker operation. Even if only using microphone, both sets of parameters must be provided.
3. Callback Registration
&onMicFrameCallback- Function pointer to your callbackNULL- Optional user data (accessible asptrin callback)
4. Stream Initialization
start()begins USB host operationconnectWait()blocks until microphone connects or timeout- After connection, callbacks begin firing automatically
5. Volume and Mute Control
- Range: 0-100 (percentage)
- Value is cast to
(void *)pointer - Some microphones may not support volume control
0= unmuted (audio flows)1= muted (audio stopped, but still streaming)
6. Suspend and Resume
- Power saving when audio not needed
- Temporarily pause recording
- Switch between different audio sources
- No need to reconfigure when resuming
Expected Serial Output
bit_resolution = 16- 16-bit audio (CD quality)samples_frequence = 16000- 16 kHz sample rate (common for voice)data_bytes = 640- 640 bytes per callback
- 16-bit = 2 bytes per sample
- Mono (1 channel)
- 640 bytes ÷ 2 bytes/sample = 320 samples
- 320 samples ÷ 16000 Hz = 20ms of audio per callback
Audio Format Negotiation
TheUAC_*_ANY constants auto-negotiate with the microphone. You can also specify exact formats:
Request Specific Format
Common Configurations
| Use Case | Channels | Bits | Sample Rate | Buffer Size |
|---|---|---|---|---|
| Voice recording | 1 (mono) | 16 | 16000 Hz | 6400 bytes |
| Music recording | 2 (stereo) | 16 | 44100 Hz | 12800 bytes |
| High quality | 2 (stereo) | 24 | 48000 Hz | 19200 bytes |
| Low bandwidth | 1 (mono) | 8 | 8000 Hz | 3200 bytes |
Processing Audio Data
Save Audio to SD Card (WAV Format)
Calculate Audio Level (VU Meter)
Stream Audio Over WiFi
Voice Activity Detection (VAD)
Performance Considerations
Buffer Size Selection:- Larger buffers = less frequent callbacks, less CPU overhead
- Smaller buffers = lower latency, more real-time responsiveness
- 6400 bytes works well for 16kHz, 16-bit audio
- Increase for higher sample rates
- Keep callback code fast and minimal
- Avoid heavy processing in callback
- Use ring buffers to pass data to main loop for processing
- Minimize serial output (increases latency)
- Audio data in callback is temporary
- Copy data if you need to keep it
- Use DMA or ringbuffers for efficiency
Troubleshooting
No callbacks received:- Check USB connection
- Verify microphone is UAC-compatible
- Increase
connectWait()timeout - Check device power requirements
- Increase buffer size
- Reduce callback processing time
- Check for buffer overruns
- Verify sample rate matches microphone capability
- Not all USB microphones support these controls
- Check microphone’s UAC feature support
- Some microphones have hardware volume controls only
- Remember: bytes = samples × channels × (bits ÷ 8)
- Stereo doubles the data size
- Higher sample rates increase data rate
Related Examples
UAC Speaker
Play audio through USB speakers
Combined Streaming
Use microphone, speaker, and camera together