M5Core2 play wav audio failed by use M5.Speaker class



  • Hardware: M5Core2
    Platform:Arduino

    Q:
    I have prepared a wave format file in the TF card and want to play it through the M5.Speaker class, but the audio cannot be played normally after configuring the Speaker

    // code
    void playSpeak(File* file) {
        auto config = M5.Speaker.config();
        config.pin_data_out = 2;
        config.pin_ws =  0;
        config.sample_rate = 16000;
        config.dma_buf_len = 1024;
        config.dma_buf_count = 2;
        // config.buzzer = true;
        config.i2s_port = I2S_NUM_0;
        M5.Speaker.config(config);
        M5.Speaker.begin();
        // M5.Speaker.tone(661, 1000);
        M5.Speaker.setAllChannelVolume(255);
        M5.Speaker.setVolume(64);
        M5.Speaker.setChannelVolume(0, 255);
        // bool status = M5.Speaker.tone(2000, 1000);
        uint8_t* dataBuf = (uint8_t*)malloc(file->size());
        file->read(dataBuf, file->size());
        Serial.printf("File Content: %d", file->size());
        bool status = M5.Speaker.playWav(dataBuf, file->size(), 1, 1, true);
        if (!status) {
            Serial.println("Failed to play");
        }
        free(dataBuf);
        file->close();
        M5.Speaker.end();
        i2s_driver_uninstall(I2S_NUM_0);
    }
    

    I have opened the source official test code in the Speaker class to print the WAV file information. The port print content is as follows:

    Speech Start
    File Content: 957804[ 14851][D][Speaker_Class.cpp:772] playWav(): [wav] RIFF           : RIFF
    [ 14851][D][Speaker_Class.cpp:773] playWav(): [wav] chunk_size     : 957796
    [ 14855][D][Speaker_Class.cpp:774] playWav(): [wav] WAVEfmt        : WAVEfmt 
    [ 14862][D][Speaker_Class.cpp:775] playWav(): [wav] fmt_chunk_size : 16
    [ 14868][D][Speaker_Class.cpp:776] playWav(): [wav] audiofmt       : 1
    [ 14874][D][Speaker_Class.cpp:777] playWav(): [wav] channel        : 1
    [ 14880][D][Speaker_Class.cpp:778] playWav(): [wav] sample_rate    : 16000
    [ 14887][D][Speaker_Class.cpp:779] playWav(): [wav] byte_per_sec   : 32000
    [ 14894][D][Speaker_Class.cpp:780] playWav(): [wav] block_size     : 2
    [ 14900][D][Speaker_Class.cpp:781] playWav(): [wav] bit_per_sample : 16
    Speech end
    

    To solve this problem, I tried the following:

    1. Test whether the hardware power amplifier is available. I tested the power amplifier through the official tone function to make sound normally.
    2. Read and learn I2S-related knowledge, and understand the basic principles
    3. Check the ESP32 I2S related instructions on the Internet

    But I didn't solve my problem, hope anyone can help me to find out where is the problem. Thank you so much!