ESP32 BT Using PDM microphone with hfp-hf example

Danila724
Posts: 1
Joined: Thu Dec 30, 2021 9:24 pm

ESP32 BT Using PDM microphone with hfp-hf example

Postby Danila724 » Fri Dec 31, 2021 3:31 pm

Hi,
I'm trying to add support for PDM microphone to hfp_hf example.
First, I've add this code to init i2s interface and pdm microphone:

Code: Select all

int app_i2s_pdm_init(){

    i2s_config_t i2s_config = {
            .mode = I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM,
            .sample_rate = 16000,
            .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
            .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
            .communication_format = I2S_COMM_FORMAT_STAND_I2S,//I2S_COMM_FORMAT_I2S,
            .dma_buf_count = 8,                            // number of buffers, 128 max.
            .dma_buf_len = 256, // Size of each DMA buffer in samples. Max 1024.
            .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1        // Interrupt level 1
    };

    i2s_pin_config_t pins = {
        .bck_io_num = I2S_PIN_NO_CHANGE,    // Not used
        .ws_io_num = GPIO_NUM_26,
        .data_out_num = I2S_PIN_NO_CHANGE,    // Not used
        .data_in_num = GPIO_NUM_37
    };

    i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
    i2s_set_pin(I2S_NUM_0, &pins);

    return 0;
}
Next, I've modified the function (changes are highlighted):

Code: Select all

static uint32_t bt_app_hf_client_outgoing_cb(uint8_t *p_buf, uint32_t sz)
{
    if (!m_rb) {
        return 0;
    }

    size_t item_size = 0;
    size_t bytes_read = 0;

    uint8_t *data = xRingbufferReceiveUpTo(m_rb, &item_size, 0, sz);
    if (item_size == sz) {
        [color=#00BF00]//memcpy(p_buf, data, item_size);
        i2s_read(I2S_NUM_0, p_buf, sz, &bytes_read, portMAX_DELAY);[/color]
        vRingbufferReturnItem(m_rb, data);
        return sz;
    } else if (0 < item_size) {
        vRingbufferReturnItem(m_rb, data);
        return 0;
    } else {
        // data not enough, do not read
        return 0;
    }
}
The idea is to read data from i2s buffer and put it to the buffer, provided by bt_app_hf_client_outgoing_cb.

The problem: I can connect to the smartphone and receive audio data on the smartphone over bt. But sound is corrupted.
It's not completely a garbage (or noise). It's possible to understand that this is a speech, there are pauses between the words.
But the words are not recognizable.

I'm sure that hardware is ok as I checked it with another example (esp32-hsp-hf).

Please give me an advise how to fix it or what to do next.

Thank you.

Best regards,
Danila

66jedrek
Posts: 1
Joined: Sat May 18, 2024 9:30 pm

Re: ESP32 BT Using PDM microphone with hfp-hf example

Postby 66jedrek » Sat May 18, 2024 9:38 pm

Hi @Danila724
Did you find any solution for that issue?
I'm working on very similar code (using newer ESP-IDF with new configuration of PDM and functions) and with analogous implementation like yours. I've found function esp_hf_client_pcm_resample which improved slightly audio, so I can understand whole words, however it's still very distorted and already I'm stuck.
Do you have any suggestions?

Who is online

Users browsing this forum: No registered users and 103 guests