espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.38k stars 7.22k forks source link

DAC audio plays back at incorrect speed in ESP-IDF 4.4 (IDFGH-7226) #8820

Open phoddie opened 2 years ago

phoddie commented 2 years ago

Environment

Problem Description

Audio playback using i2s_ API to DAC plays back at about 2 half-steps higher than expected. For example, a B-flat tone plays back correctly under ESP-IDF 4.3. Under ESP-IDF 4.4, the same audio plays back as a C natural (two half steps higher).

Code to reproduce this issue

Summary of implementation:

    i2s_config_t i2s_config = {
        .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN,
        .sample_rate = 11025,
        .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
        .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
        .communication_format = I2S_COMM_FORMAT_STAND_MSB,
        .intr_alloc_flags = 0,
        .dma_buf_count = 2,
        .dma_buf_len = 1024,
        .use_apll = false
    };
    i2s_driver_install(0, &i2s_config, 0, NULL);
    i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);

    while (moreAudioToPlay)
        i2s_write(0, buffer, 4096, &bytes_written, portMAX_DELAY);

Other information

This issue was first noticed by @meganetaaan. His report includes a video that shows the behavior under ESP-IDF 4.3 and 4.4 so you can easily compare.

L-KAYA commented 2 years ago

I2S_CHANNEL_FMT_ONLY_RIGHT means using only one channel, but I2S_DAC_CHANNEL_BOTH_EN enabled two channel, they need to be matched. Please try to use I2S_CHANNEL_FMT_ALL_RIGHT with I2S_DAC_CHANNEL_BOTH_EN or I2S_CHANNEL_FMT_ONLY_RIGHT with I2S_DAC_CHANNEL_RIGHT_EN.

phoddie commented 2 years ago

@L-KAYA – I made a mistake above. Apologies. The code is using I2S_CHANNEL_FMT_ONLY_RIGHT with I2S_DAC_CHANNEL_RIGHT_EN on M5Stack Fire. I also tried with I2S_CHANNEL_FMT_ALL_RIGHT with I2S_DAC_CHANNEL_BOTH_EN but the result is the same.

We are currently pinned to commit c29343. I also tried the latest from the v4.4 tag. That does not play audio at all. just a pop.