Skip to main content

uacConfiguration()

Configures the USB Audio Class (UAC) interface for both microphone and speaker functionality.

Syntax

void uacConfiguration(
    uint8_t mic_ch_num,
    uint16_t mic_bit_resolution,
    uint32_t mic_samples_frequency,
    uint32_t mic_buf_size,
    uint8_t spk_ch_num,
    uint16_t spk_bit_resolution,
    uint32_t spk_samples_frequency,
    uint32_t spk_buf_size
)

Parameters

Microphone Parameters

  • mic_ch_num (uint8_t) - Microphone channel numbers. Use UAC_CH_ANY to accept any channel number.
  • mic_bit_resolution (uint16_t) - Microphone bit resolution. Use UAC_BITS_ANY to accept any bit resolution.
  • mic_samples_frequency (uint32_t) - Microphone sample frequency in Hz. Use UAC_FREQUENCY_ANY to accept any frequency.
  • mic_buf_size (uint32_t) - Microphone receive buffer size. Set to 0 if not using microphone.

Speaker Parameters

  • spk_ch_num (uint8_t) - Speaker channel numbers. Use UAC_CH_ANY to accept any channel number.
  • spk_bit_resolution (uint16_t) - Speaker bit resolution. Use UAC_BITS_ANY to accept any bit resolution.
  • spk_samples_frequency (uint32_t) - Speaker sample frequency in Hz. Use UAC_FREQUENCY_ANY to accept any frequency.
  • spk_buf_size (uint32_t) - Speaker buffer size. Set to 0 if not using speaker.

Audio Format Constants

The library provides these constants for flexible audio format configuration:
UAC_CH_ANY          // Accept any channel number (value: 0)
UAC_BITS_ANY        // Accept any bit resolution (value: __UINT16_MAX__)
UAC_FREQUENCY_ANY   // Accept any sample frequency (value: __UINT32_MAX__)

Examples

Microphone Only Configuration

USB_STREAM *usb = new USB_STREAM();

// Configure for microphone only
// Accept any audio format, with 6400 byte buffer
usb->uacConfiguration(
    UAC_CH_ANY,          // Accept any mic channels
    UAC_BITS_ANY,        // Accept any mic bit resolution
    UAC_FREQUENCY_ANY,   // Accept any mic frequency
    6400,                // 6400 byte mic buffer
    UAC_CH_ANY,          // No speaker channels
    UAC_BITS_ANY,        // No speaker bit resolution
    UAC_FREQUENCY_ANY,   // No speaker frequency
    0                    // No speaker buffer (disabled)
);

Speaker Only Configuration

USB_STREAM *usb = new USB_STREAM();

// Configure for speaker only
usb->uacConfiguration(
    UAC_CH_ANY,          // No mic channels
    UAC_BITS_ANY,        // No mic bit resolution
    UAC_FREQUENCY_ANY,   // No mic frequency
    0,                   // No mic buffer (disabled)
    UAC_CH_ANY,          // Accept any speaker channels
    UAC_BITS_ANY,        // Accept any speaker bit resolution
    UAC_FREQUENCY_ANY,   // Accept any speaker frequency
    6400                 // 6400 byte speaker buffer
);

Microphone and Speaker Configuration

USB_STREAM *usb = new USB_STREAM();

// Configure for both microphone and speaker
usb->uacConfiguration(
    UAC_CH_ANY,          // Accept any mic channels
    UAC_BITS_ANY,        // Accept any mic bit resolution
    UAC_FREQUENCY_ANY,   // Accept any mic frequency
    6400,                // 6400 byte mic buffer
    UAC_CH_ANY,          // Accept any speaker channels
    UAC_BITS_ANY,        // Accept any speaker bit resolution
    UAC_FREQUENCY_ANY,   // Accept any speaker frequency
    6400                 // 6400 byte speaker buffer
);

Specific Audio Format Configuration

USB_STREAM *usb = new USB_STREAM();

// Configure with specific audio formats
usb->uacConfiguration(
    2,       // Stereo microphone
    16,      // 16-bit resolution
    48000,   // 48kHz sample rate
    8192,    // 8KB mic buffer
    2,       // Stereo speaker
    16,      // 16-bit resolution
    48000,   // 48kHz sample rate
    8192     // 8KB speaker buffer
);

Notes

  • Call this method before start() to configure the UAC interface.
  • Using UAC_*_ANY constants allows the library to automatically negotiate the best audio format with the connected USB host.
  • The buffer sizes affect latency and memory usage. Larger buffers provide more buffering but use more RAM.
  • Set buffer size to 0 for unused audio direction (microphone or speaker) to disable it.
  • Valid ranges when specifying exact values:
    • Channel numbers: 1-2
    • Bit resolution: 8-24 bits
    • Sample frequency: 1000-48000 Hz

Build docs developers (and LLMs) love