ESP32 A2DP Source

farukisiker
Posts: 2
Joined: Mon Sep 29, 2025 6:55 am

ESP32 A2DP Source

Postby farukisiker » Tue Sep 30, 2025 11:14 am

Hello all,
I would like to generate and transmit sine wave over Bluetooth A2DP protocol to a headset.
I generate sine wave successfully. I can send it to headset but there is noise inside. Especially, when I try to change frequency.
My sampling rate is 44.1khz.

Code: Select all

static void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
{
    bt_app_work_dispatch(bt_app_av_sm_hdlr, event, param, sizeof(esp_a2d_cb_param_t), NULL);
}

/* generate some random noise to simulate source audio */
static int32_t bt_app_a2d_data_cb(uint8_t *data, int32_t len)
{
    if (data == NULL || len < 0) {
        return 0;
    }

    static int16_t sample = 0;
    static const float amplitude = 10000.0; // -32,768 to 32,767
    static const float deltaTime = 1.0 / 44100.0;
    static uint32_t change_freq_counter = 0;

    /* **************************************** */
    for (int i = 0; i < len; i+=4) { // 2 bytes per sample, 2 channels
        // Generate a 440 Hz sine wave
        float angle = M_TWOPI * frequency * time;
        sample = amplitude * sin(angle);
        data[i] = sample & 0xFF;           // Low byte
        data[i + 1] = (sample >> 8) & 0xFF; // High byte
        data[i + 2] = data[i];              // Low byte for right channel
        data[i + 3] = data[i + 1];          // High byte for right channel
        time += deltaTime;
        if (time >= 1.0) {
            if (change_freq_counter++ > 10000) { // change frequency every 1000 samples
                frequency += 20.0; // increase frequency by 20 Hz
                if (frequency > 1000.0) {
                    frequency = 200.0; // reset to 200 Hz if it exceeds 1000 Hz
                }
                change_freq_counter = 0;
            }
            {

            }
        }
    }
    /* **************************************** */

    return len; // return the number of samples
}
1- Do you have any suggestions for clear transmission?
2- It sends only 512 byte for each callback. Can I increase it?
3- I tried to generate sine wave in other task and just copied inside the callback. It didnt help.

Best regards

Who is online

Users browsing this forum: Google [Bot], Qwantbot, RandomInternetGuy, Semrush [Bot] and 3 guests