DAC Audio¶
ESP32 has two independent DAC channels and can play audio using I2S directly via DMA. The APIs in this document have been simplified on the basis of ESP-IDF, and the related data has been recoded to support more types of sampling bit width.
API Reference¶
Header File¶
Functions¶
-
esp_err_t
dac_audio_init
(dac_audio_config_t *cfg)¶ initialize i2s build-in dac to play audio with
- Attention
only support ESP32, because i2s of ESP32S2 not have a build-in dac
- Return
ESP_OK Success
ESP_FAIL Encounter error
ESP_ERR_INVALID_ARG Parameter error
ESP_ERR_NO_MEM Out of memory
- Parameters
cfg
: configurations - see dac_audio_config_t struct
-
esp_err_t
dac_audio_deinit
(void)¶ deinitialize dac
- Return
ESP_OK Success
ESP_FAIL Encounter error
-
esp_err_t
dac_audio_start
(void)¶ Start dac to play.
- Return
ESP_OK Success
ESP_FAIL Encounter error
-
esp_err_t
dac_audio_stop
(void)¶ Stop play.
- Return
ESP_OK Success
ESP_FAIL Encounter error
-
esp_err_t
dac_audio_set_param
(int rate, int bits, int ch)¶ Configuration dac parameter.
- Return
ESP_OK Success
ESP_FAIL Encounter error
ESP_ERR_INVALID_ARG Parameter error
- Parameters
rate
: sample rate (ex: 8000, 44100…)bits
: bit widthch
: channel number
-
esp_err_t
dac_audio_set_volume
(int8_t volume)¶ Set volume.
- Attention
Using volume greater than 0 may cause variable overflow and distortion Usually you should enter a volume less than or equal to 0
- Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
- Parameters
volume
: Volume to set (-16 ~ 16), see Macro VOLUME_0DB Set to 0 for original output; Set to less then 0 for attenuation, and -16 is mute; Set to more than 0 for enlarge, and 16 is double output
-
esp_err_t
dac_audio_write
(uint8_t *inbuf, size_t len, size_t *bytes_written, TickType_t ticks_to_wait)¶ Write data to play.
- Return
ESP_OK Success
ESP_FAIL Write encounter error
ESP_ERR_INVALID_ARG Parameter error
- Parameters
inbuf
: Pointer source data to writelen
: length of data in bytes[out] bytes_written
: Number of bytes written, if timeout, the result will be less than the size passed in.ticks_to_wait
: TX buffer wait timeout in RTOS ticks. If this many ticks pass without space becoming available in the DMA transmit buffer, then the function will return (note that if the data is written to the DMA buffer in pieces, the overall operation may still take longer than this timeout.) Pass portMAX_DELAY for no timeout.
Structures¶
-
struct
dac_audio_config_t
¶ Configuration parameters for dac_audio_init function.
Public Members
-
i2s_port_t
i2s_num
¶ I2S_NUM_0, I2S_NUM_1
-
int
sample_rate
¶ I2S sample rate
-
i2s_bits_per_sample_t
bits_per_sample
¶ I2S bits per sample
-
i2s_dac_mode_t
dac_mode
¶ DAC mode configurations - see i2s_dac_mode_t
-
int
dma_buf_count
¶ DMA buffer count, number of buffer
-
int
dma_buf_len
¶ DMA buffer length, length of each buffer
-
uint32_t
max_data_size
¶ one time max write data size
-
i2s_port_t