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

Parameters

cfg – configurations - see dac_audio_config_t struct

Returns

  • ESP_OK Success

  • ESP_FAIL Encounter error

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_ERR_NO_MEM Out of memory

esp_err_t dac_audio_deinit(void)

deinitialize dac

Returns

  • ESP_OK Success

  • ESP_FAIL Encounter error

esp_err_t dac_audio_start(void)

Start dac to play.

Returns

  • ESP_OK Success

  • ESP_FAIL Encounter error

esp_err_t dac_audio_stop(void)

Stop play.

Returns

  • ESP_OK Success

  • ESP_FAIL Encounter error

esp_err_t dac_audio_set_param(int rate, int bits, int ch)

Configuration dac parameter.

Parameters
  • rate – sample rate (ex: 8000, 44100…)

  • bits – bit width

  • ch – channel number

Returns

  • ESP_OK Success

  • ESP_FAIL Encounter error

  • ESP_ERR_INVALID_ARG Parameter error

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

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

Returns

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

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.

Parameters
  • inbuf – Pointer source data to write

  • len – length of data in bytes

  • bytes_written[out] 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.

Returns

  • ESP_OK Success

  • ESP_FAIL Write encounter error

  • ESP_ERR_INVALID_ARG Parameter error

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