Page 1 of 1

Can't get WAV to play to bluetooth speaker please help

Posted: Sat Feb 16, 2019 12:00 pm
by andrews
Hello

I have been trying without luck for a couple of days to play a WAV file from a plain esp32 (no lyra, no additional components), to a bluetooth speaker.

I can't get it work. My code is below, and under that is a log of the build and execution.

The things I notice most in the log are these lines:

W (7806) SPIFFS_STREAM: No more data, ret:0
W (7896) BT_APPL: ### UNDERFLOW :: ONLY READ 428 BYTES OUT OF 512 ###

Any help would be much appreciated - if anyone can help me to getg a WAV to play from spiffs it would be much appreciated!!!!

thanks

Code: Select all

/* Play music from Bluetooth device

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "audio_common.h"
#include "audio_element.h"
#include "audio_event_iface.h"
#include "audio_pipeline.h"
#include "bluetooth_service.h"
#include "esp_log.h"
#include "esp_peripherals.h"
#include "wav_decoder.h"
#include "nvs_flash.h"
#include "sdkconfig.h"
#include "periph_spiffs.h"
#include "spiffs_stream.h"


#define SAVE_FILE_RATE      22050
#define SAVE_FILE_CHANNEL   1
#define SAVE_FILE_BITS      8

#define PLAYBACK_RATE       44100
#define PLAYBACK_CHANNEL    2
#define PLAYBACK_BITS       16

static const char *TAG = "BLUETOOTH_SOURCE_EXAMPLE";

static audio_element_handle_t create_spiffs_stream(int sample_rates, int bits, int channels, audio_stream_type_t type)
{
    spiffs_stream_cfg_t spiffs_cfg = SPIFFS_STREAM_CFG_DEFAULT();
    spiffs_cfg.type = type;
    audio_element_handle_t spiffs_stream = spiffs_stream_init(&spiffs_cfg);
    mem_assert(spiffs_stream);
    audio_element_info_t writer_info = {0};
    audio_element_getinfo(spiffs_stream, &writer_info);
    writer_info.bits = bits;
    writer_info.channels = channels;
    writer_info.sample_rates = sample_rates;
    audio_element_setinfo(spiffs_stream, &writer_info);
    return spiffs_stream;
}

void app_main(void)
{
    //nvs_flash_erase();
    esp_err_t err = nvs_flash_init();
    if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
        // NVS partition was truncated and needs to be erased
        // Retry nvs_flash_init
        ESP_ERROR_CHECK(nvs_flash_erase());
        err = nvs_flash_init();
    }


    audio_pipeline_handle_t pipeline;
    audio_element_handle_t spiffs_reader_el, filter_upsample_el, wav_decoder, bt_stream_writer;

    // Initialize peripherals management
    esp_periph_config_t periph_cfg = { 0 };
    esp_periph_init(&periph_cfg);

    // Initialize Spiffs peripheral
    periph_spiffs_cfg_t spiffs_cfg = {
        .root = "/spiffs",
        .partition_label = NULL,
        .max_files = 5,
        .format_if_mount_failed = true
    };
    esp_periph_handle_t spiffs_handle = periph_spiffs_init(&spiffs_cfg);

    // Start spiffs peripheral
    esp_periph_start(spiffs_handle);

    // Wait until spiffs was mounted
    while (!periph_spiffs_is_mounted(spiffs_handle)) {
        vTaskDelay(100 / portTICK_PERIOD_MS);
    }



    esp_log_level_set("*", ESP_LOG_INFO);
    esp_log_level_set(TAG, ESP_LOG_DEBUG);

    ESP_LOGI(TAG, "[ 1 ] Create Bluetooth service");
    bluetooth_service_cfg_t bt_cfg = {
        .device_name = "ESP-ADF-SOURCE",
        .mode = BLUETOOTH_A2DP_SOURCE,
        .remote_name = "BT-12",
    };
    bluetooth_service_start(&bt_cfg);

    ESP_LOGI(TAG, "[1.1] Get Bluetooth stream");
    bt_stream_writer = bluetooth_service_create_stream();

    ESP_LOGI(TAG, "[ 2 ] Create SPIFFS stream to read data");
    spiffs_reader_el = create_spiffs_stream(SAVE_FILE_RATE, SAVE_FILE_BITS, SAVE_FILE_CHANNEL, AUDIO_STREAM_READER);


    ESP_LOGI(TAG, "[ 3 ] Create wav decoder to decode wav file");
    wav_decoder_cfg_t wavdec_cfg = DEFAULT_WAV_DECODER_CONFIG();
    wav_decoder = wav_decoder_init(&wavdec_cfg);
    //audio_element_set_read_cb(wav_decoder, wav_music_read_cb, NULL);

    ESP_LOGI(TAG, "[ 4 ] Create audio pipeline for BT Source");
    audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
    pipeline = audio_pipeline_init(&pipeline_cfg);

    ESP_LOGI(TAG, "[4.1] Register all elements to audio pipeline");
    audio_pipeline_register(pipeline, spiffs_reader_el,     "spiffs_reader");
    audio_pipeline_register(pipeline, wav_decoder,        "wav");
    audio_pipeline_register(pipeline, bt_stream_writer,   "bt");

    ESP_LOGI(TAG, "[4.2] Link it together wav_decoder-->bt_stream_writer");
    audio_pipeline_link(pipeline, (const char *[]) {"spiffs_reader", "wav", "bt"}, 3);

    audio_element_set_uri(spiffs_reader_el, "/spiffs/pcm1644sSHORT.wav");

    ESP_LOGI(TAG, "[5.1] Create Bluetooth peripheral");
    esp_periph_handle_t bt_periph = bluetooth_service_create_periph();

    ESP_LOGI(TAG, "[5.2] Start Bluetooth peripheral");
    esp_periph_start(bt_periph);
    ESP_LOGI(TAG, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

    ESP_LOGI(TAG, "[ 6 ] Setup event listener");
    audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
    audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);

    ESP_LOGI(TAG, "[6.1] Listening event from all elements of pipeline");
    audio_pipeline_set_listener(pipeline, evt);

    ESP_LOGI(TAG, "[6.2] Listening event from peripherals");
    audio_event_iface_set_listener(esp_periph_get_event_iface(), evt);

    ESP_LOGI(TAG, "[ 7 ] Start audio_pipeline");
    audio_pipeline_run(pipeline);

    ESP_LOGI(TAG, "[ 8 ] Listen for all pipeline events");
    while (1) {
        audio_event_iface_msg_t msg;
        esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
        if (ret != ESP_OK) {
            ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
            continue;
        }

        ESP_LOGI(TAG, "[ * ] Audio event");
        ESP_LOGI(TAG, "[ * ] Free heap: %u", xPortGetFreeHeapSize());
        ESP_LOGI(TAG, "[ * ] msg.cmd: %d", msg.cmd);

        if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) wav_decoder
            && msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
            audio_element_info_t music_info = {0};
            audio_element_getinfo(wav_decoder, &music_info);

            ESP_LOGI(TAG, "[ * ] Receive music info from wav decoder, sample_rates=%d, bits=%d, ch=%d",
                     music_info.sample_rates, music_info.bits, music_info.channels);

            audio_element_setinfo(bt_stream_writer, &music_info);
            //i2s_stream_set_clk(bt_stream_writer, music_info.sample_rates, music_info.bits, music_info.channels);
            continue;
        }


        /* Stop when the Bluetooth is disconnected or suspended */
        if (msg.source_type == PERIPH_ID_BLUETOOTH
                && msg.source == (void *)bt_periph) {
            if ((msg.cmd == PERIPH_BLUETOOTH_DISCONNECTED) || (msg.cmd == PERIPH_BLUETOOTH_AUDIO_SUSPENDED)) {
                ESP_LOGW(TAG, "[ * ] Bluetooth disconnected or suspended");
                periph_bluetooth_stop(bt_periph);
                break;
            }
        }
    }

    ESP_LOGI(TAG, "[ 9 ] Stop audio_pipeline");
    audio_pipeline_terminate(pipeline);

    /* Terminate the pipeline before removing the listener */
    audio_pipeline_remove_listener(pipeline);

    /* Stop all peripherals before removing the listener */
    esp_periph_stop_all();
    audio_event_iface_remove_listener(esp_periph_get_event_iface(), evt);

    /* Make sure audio_pipeline_remove_listener & audio_event_iface_remove_listener are called before destroying event_iface */
    audio_event_iface_destroy(evt);

    /* Release all resources */
    audio_pipeline_unregister(pipeline, spiffs_reader_el);
    audio_pipeline_unregister(pipeline, bt_stream_writer);
    audio_pipeline_unregister(pipeline, wav_decoder);
    //audio_pipeline_deinit(spiffs_reader_el);
    audio_pipeline_deinit(pipeline);
    audio_element_deinit(bt_stream_writer);
    audio_element_deinit(wav_decoder);
    esp_periph_destroy();
    bluetooth_service_destroy();
}

Here is the make:

Code: Select all

$ make flash
Flashing binaries to serial port /dev/cu.SLAB_USBtoUART (app at offset 0x10000 )...
esptool.py v2.6-beta1
Serial port /dev/cu.SLAB_USBtoUART
Connecting........_
Chip is ESP32D0WDQ6 (revision 0)
Features: WiFi, BT, Dual Core, Coding Scheme None
MAC: 24:0a:c4:81:5c:a0
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 21344 bytes to 12567...
Wrote 21344 bytes (12567 compressed) at 0x00001000 in 1.1 seconds (effective 153.4 kbit/s)...
Hash of data verified.
Compressed 864656 bytes to 507801...
Wrote 864656 bytes (507801 compressed) at 0x00010000 in 44.9 seconds (effective 154.2 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 134...
Wrote 3072 bytes (134 compressed) at 0x00008000 in 0.0 seconds (effective 1522.8 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
And here is the log:

Code: Select all

$ make monitor
MONITOR
--- idf_monitor on /dev/cu.SLAB_USBtoUART 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5996
load:0x40078000,len:9176
load:0x40080000,len:6064
0x40080000: _WindowOverflow4 at /Users/andrewstuartsupercoders/esp/esp-adf/esp-idf/components/freertos/xtensa_vectors.S:1685

entry 0x40080330
0x40080330: _KernelExceptionVector at ??:?

I (29) boot: ESP-IDF v3.1.1-6-g2aa9a2118 2nd stage bootloader
I (29) boot: compile time 16:28:24
I (40) boot: Enabling RNG early entropy source...
I (40) boot: SPI Speed      : 40MHz
I (40) boot: SPI Mode       : DIO
I (43) boot: SPI Flash Size : 4MB
I (47) boot: Partition Table:
I (50) boot: ## Label            Usage          Type ST Offset   Length
I (57) boot:  0 nvs              WiFi data        01 02 00009000 00005000
I (65) boot:  1 otadata          OTA data         01 00 0000e000 00002000
I (72) boot:  2 app0             OTA app          00 10 00010000 00300000
I (80) boot:  3 eeprom           Unknown data     01 99 00310000 00001000
I (87) boot:  4 spiffs           Unknown data     01 82 00311000 000ef000
I (95) boot: End of partition table
I (99) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x2941c (168988) map
I (141) esp_image: segment 1: paddr=0x00039444 vaddr=0x3ffc0000 size=0x031e8 ( 12776) load
I (144) esp_image: segment 2: paddr=0x0003c634 vaddr=0x3ffc31e8 size=0x00000 (     0) load
I (148) esp_image: segment 3: paddr=0x0003c63c vaddr=0x40080000 size=0x00400 (  1024) load
0x40080000: _WindowOverflow4 at /Users/andrewstuartsupercoders/esp/esp-adf/esp-idf/components/freertos/xtensa_vectors.S:1685

I (157) esp_image: segment 4: paddr=0x0003ca44 vaddr=0x40080400 size=0x035cc ( 13772) load
I (169) esp_image: segment 5: paddr=0x00040018 vaddr=0x400d0018 size=0x94d54 (609620) map
0x400d0018: _flash_cache_start at ??:?

I (295) esp_image: segment 6: paddr=0x000d4d74 vaddr=0x400839cc size=0x0e3e0 ( 58336) load
0x400839cc: spi_flash_read_encrypted at /Users/andrewstuartsupercoders/esp/esp-adf/esp-idf/components/spi_flash/flash_ops.c:634

I (308) esp_image: segment 7: paddr=0x000e315c vaddr=0x400c0000 size=0x00000 (     0) load
I (308) esp_image: segment 8: paddr=0x000e3164 vaddr=0x50000000 size=0x00000 (     0) load
I (318) boot: Loaded app from partition at offset 0x10000
I (321) boot: Disabling RNG early entropy source...
I (327) cpu_start: Pro cpu up.
I (330) cpu_start: Starting app cpu, entry point is 0x40081008
0x40081008: call_start_cpu1 at /Users/andrewstuartsupercoders/esp/esp-adf/esp-idf/components/esp32/cpu_start.c:231

I (0) cpu_start: App cpu up.
I (341) heap_init: Initializing. RAM available for dynamic allocation:
I (347) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
I (354) heap_init: At 3FFCAEE0 len 00015120 (84 KiB): DRAM
I (360) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (366) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (372) heap_init: At 40091DAC len 0000E254 (56 KiB): IRAM
I (379) cpu_start: Pro cpu start user code
I (24) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (186) PERIPH_SPIFFS: Partition size: total: 892556, used: 95882
I (186) BLUETOOTH_SOURCE_EXAMPLE: [ 1 ] Create Bluetooth service
I (186) BTDM_INIT: BT controller compile version [7b0770a]

I (196) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (256) phy: phy_version: 4000, b6198fa, Sep  3 2018, 15:11:06, 0, 0
I (656) BLUETOOTH_SERVICE: Starting device discovery...
I (656) BLUETOOTH_SOURCE_EXAMPLE: [1.1] Get Bluetooth stream
I (656) BLUETOOTH_SOURCE_EXAMPLE: [ 2 ] Create SPIFFS stream to read data
I (666) BLUETOOTH_SOURCE_EXAMPLE: [ 3 ] Create wav decoder to decode wav file
I (666) BLUETOOTH_SOURCE_EXAMPLE: [ 4 ] Create audio pipeline for BT Source
I (676) BLUETOOTH_SERVICE: Discovery started.
I (676) BLUETOOTH_SOURCE_EXAMPLE: [4.1] Register all elements to audio pipeline
I (696) BLUETOOTH_SOURCE_EXAMPLE: [4.2] Link it together wav_decoder-->bt_stream_writer
I (696) AUDIO_PIPELINE: audio_pipeline_link:0x3ffe17c0, spiffs_reader, 0x3ffdfed0
I (706) AUDIO_PIPELINE: audio_pipeline_link:0x3ffdf42c, wav, 0x3ffdefc4
I (716) BLUETOOTH_SOURCE_EXAMPLE: [5.1] Create Bluetooth peripheral
I (726) BLUETOOTH_SOURCE_EXAMPLE: [5.2] Start Bluetooth peripheral
I (726) BLUETOOTH_SOURCE_EXAMPLE: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
I (736) BLUETOOTH_SOURCE_EXAMPLE: [ 6 ] Setup event listener
I (746) BLUETOOTH_SOURCE_EXAMPLE: [6.1] Listening event from all elements of pipeline
I (756) BLUETOOTH_SOURCE_EXAMPLE: [6.2] Listening event from peripherals
I (756) BLUETOOTH_SOURCE_EXAMPLE: [ 7 ] Start audio_pipeline
I (766) AUDIO_ELEMENT: [spiffs_reader] audio_element_run
I (776) AUDIO_ELEMENT: [spiffs_reader] Element task created
I (776) AUDIO_ELEMENT: [wav] audio_element_run
I (786) AUDIO_ELEMENT: [wav] Element task created
I (786) AUDIO_ELEMENT: [bt] audio_element_run
I (796) AUDIO_ELEMENT: [bt] Element task created
I (796) AUDIO_PIPELINE: Func:audio_pipeline_run, Line:278, MEM Total:93732 Bytes

I (806) AUDIO_ELEMENT: [spiffs_reader] AEL_MSG_CMD_RESUME,state:1
I (816) AUDIO_ELEMENT: [wav] AEL_MSG_CMD_RESUME,state:1
I (816) SPIFFS_STREAM: File size is 46040 byte, pos:0
I (836) AUDIO_PIPELINE: Pipeline started
I (836) BLUETOOTH_SOURCE_EXAMPLE: [ 8 ] Listen for all pipeline events
I (836) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (846) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 93340
I (846) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 8
I (856) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (866) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 93340
I (866) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 8
I (876) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (876) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 93340
I (886) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 9
I (886) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Receive music info from wav decoder, sample_rates=44100, bits=16, ch=2
I (4626) BLUETOOTH_SERVICE: Scanned device: fc:58:fa:5b:f3:1b
I (4626) BLUETOOTH_SERVICE: --Class of Device: 0x260404
I (4626) BLUETOOTH_SERVICE: --RSSI: -61
I (4636) BLUETOOTH_SERVICE: --Name: BT-12
I (4636) BLUETOOTH_SERVICE: Found a target device, address fc:58:fa:5b:f3:1b, name BT-12
I (4646) BLUETOOTH_SERVICE: Cancel device discovery ...
I (4656) BLUETOOTH_SERVICE: Device discovery stopped.
I (4656) BLUETOOTH_SERVICE: a2dp connecting to peer: BT-12
E (4666) BT_APPL: reset flags
I (4666) BLUETOOTH_SERVICE: bt_a2d_source_cb state 4, evt 0x0
I (7356) BLUETOOTH_SERVICE: authentication success: BT-12
I (7356) BLUETOOTH_SERVICE: fc 58 fa 5b f3 1b
I (7356) BLUETOOTH_SERVICE: ESP_BT_GAP_PIN_REQ_EVT min_16_digit:253
I (7366) BLUETOOTH_SERVICE: Input pin code: 0000 0000 0000 0000
W (7376) BT_BTM: BTM_PINCodeReply() - Wrong State: 0

E (7386) BT_APPL: bta_av_rc_create ACP handle exist for shdl:0
W (7586) BT_APPL: new conn_srvc id:18, app_id:0
I (7586) BLUETOOTH_SERVICE: bt_a2d_source_cb state 4, evt 0x0
I (7586) BLUETOOTH_SERVICE: a2dp connected
I (7586) BLUETOOTH_SERVICE: a2dp media ready checking ...
I (7586) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (7596) BLUETOOTH_SERVICE: bt_a2d_source_cb state 5, evt 0x3
I (7596) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 88736
I (7606) BLUETOOTH_SERVICE: a2dp media ready, starting ...
I (7616) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 1
W (7616) BT_APPL: new conn_srvc id:18, app_id:1
I (7636) BT_LOG: bta_av_link_role_ok hndl:x41 role:0 conn_audio:x1 bits:1 features:x824b

I (7636) BLUETOOTH_SERVICE: bt_a2d_source_cb state 5, evt 0x3
I (7646) BLUETOOTH_SERVICE: a2dp media start successfully.
I (7656) BLUETOOTH_SERVICE: bt_a2d_source_cb state 5, evt 0x1
W (7806) SPIFFS_STREAM: No more data, ret:0
I (7806) AUDIO_ELEMENT: IN-[spiffs_reader] AEL_IO_DONE,0
I (7806) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (7806) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 91100
I (7816) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 8
I (7866) AUDIO_ELEMENT: IN-[wav] AEL_IO_DONE,-2
I (7866) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (7866) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 86976
I (7866) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 8
W (7896) BT_APPL: ### UNDERFLOW :: ONLY READ 428 BYTES OUT OF 512 ###
W (7896) BT_APPL: btc_media_aa_prep_sbc_2_send underflow 1, 428
I (7926) AUDIO_ELEMENT: IN-[bt] AEL_IO_DONE,-2
W (7926) BT_APPL: ### UNDERFLOW :: ONLY READ -2 BYTES OUT OF 84 ###
W (7926) BT_APPL: btc_media_aa_prep_sbc_2_send underflow 12, 426
I (7926) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (7936) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 91100
I (7946) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 4
W (7946) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Bluetooth disconnected or suspended
W (7956) BT_APPL: btc_get_num_aa_frame() - Limiting frames to be sent from 22 to 21
I (7956) BLUETOOTH_SOURCE_EXAMPLE: [ 9 ] Stop audio_pipeline
I (7966) AUDIO_ELEMENT: IN-[bt] AEL_IO_DONE,-2
W (7976) BT_APPL: ### UNDERFLOW :: ONLY READ -2 BYTES OUT OF 86 ###
W (7966) AUDIO_PIPELINE: There are no listener registered
W (7986) BT_APPL: btc_media_aa_prep_sbc_2_send underflow 21, 424
W (7996) BT_APPL: btc_get_num_aa_frame() - Limiting frames to be sent from 37 to 21
/Users/andrewstuartsupercoders/esp/esp-adf/esp-idf/components/freertos/queue.c:1441 (xQueueGenericReceive)- assert failed!
abort() was called at PC 0x4008c855 on core 1
0x4008c855: xQueueGenericReceive at /Users/xxx/esp/esp-adf/esp-idf/components/freertos/queue.c:2037


Backtrace: 0x4008f790:0x3ffe13b0 0x4008f967:0x3ffe13d0 0x4008c855:0x3ffe13f0 0x400e7fcd:0x3ffe1430 0x400e70d2:0x3ffe1460 0x400e82e1:0x3ffe1490 0x4011ddad:0x3ffe14b0 0x4011dfac:0x3ffe14f0 0x4011e13e:0x3ffe1520 0x4011e17f:0x3ffe1550 0x4011e1a7:0x3ffe1570 0x4011e201:0x3ffe1590
0x4008f790: invoke_abort at /Users/xxx/esp/esp-adf/esp-idf/components/esp32/panic.c:649

0x4008f967: abort at /Users/xxx/esp/esp-adf/esp-idf/components/esp32/panic.c:649

0x4008c855: xQueueGenericReceive at /Users/xxx/esp/esp-adf/esp-idf/components/freertos/queue.c:2037

0x400e7fcd: rb_read at /Users/xxx/esp/esp-adf/components/audio_pipeline/ringbuf.c:444

0x400e70d2: audio_element_input at /Users/xxx/esp/esp-adf/components/audio_pipeline/audio_element.c:702

0x400e82e1: bt_a2d_source_data_cb at /Users/xxx/esp/esp-adf/components/audio_service/bluetooth_service.c:671

0x4011ddad: btc_aa_src_data_read at /Users/xxx/esp/esp-adf/esp-idf/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_source.c:200
 (inlined by) btc_media_aa_read_feeding at /Users/xxx/esp/esp-adf/esp-idf/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_source.c:1201

0x4011dfac: btc_media_aa_prep_sbc_2_send at /Users/xxx/esp/esp-adf/esp-idf/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_source.c:1348

0x4011e13e: btc_a2dp_source_prep_2_send at /Users/xxx/esp/esp-adf/esp-idf/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_source.c:1433

0x4011e17f: btc_a2dp_source_send_aa_frame at /Users/xxx/esp/esp-adf/esp-idf/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_source.c:1460

0x4011e1a7: btc_a2dp_source_handle_timer at /Users/xxx/esp/esp-adf/esp-idf/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_source.c:1478

0x4011e201: btc_a2dp_source_task_handler at /Users/xxx/esp/esp-adf/esp-idf/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_source.c:294


Rebooting...

Re: Can't get WAV to play to bluetooth speaker please help

Posted: Wed Feb 27, 2019 8:38 am
by ESP_Angus
Can you give any more details about the WAV file you are using?

From a quick look at a few key log messages, the first part looks about right for a very short WAV file:

Code: Select all

I (816) SPIFFS_STREAM: File size is 46040 byte, pos:0
...
I (886) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Receive music info from wav decoder, sample_rates=44100, bits=16, ch=2
...
I (7606) BLUETOOTH_SERVICE: a2dp media ready, starting ...
I (7616) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 1
W (7616) BT_APPL: new conn_srvc id:18, app_id:1
I (7636) BT_LOG: bta_av_link_role_ok hndl:x41 role:0 conn_audio:x1 bits:1 features:x824b
...
W (7806) SPIFFS_STREAM: No more data, ret:0
I (7806) AUDIO_ELEMENT: IN-[spiffs_reader] AEL_IO_DONE,0
...
W (7896) BT_APPL: ### UNDERFLOW :: ONLY READ 428 BYTES OUT OF 512 ###
...
(more underflow errors)
46040 byte WAV file at 44100kHz 16 bits/sample 2 channels = 260ms of samples.

I see A2DP streaming start at 7636ms timestamp and then "no more data" from the SPIFFS stream at 7806 and first underflow error from the A2DP stream at 7896. 7896 - 7636 = 260ms. Looks like the A2DP stream pushed out the entire WAV file and then no more data was coming from the source stream so the underflow errors started as soon as the transmit buffer (90ms of samples) was empty.

I think probably the abort() after timestamp 7996 is a side effect of the buffer running out and the "end" event not being handled. I'm not familiar enough with ADF to suggest exactly what to do here, but I think it comes down to handling the "end of stream" event by either stopping the A2DP stream or providing another source of samples.

Code: Select all

I (7866) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 86976
Good news is there seems to be plenty of free RAM. This is the lowest "free heap" value I can see.

Re: Can't get WAV to play to bluetooth speaker please help

Posted: Thu Feb 28, 2019 9:48 pm
by andrews
I've tried again with a wav file that I made sure is a clean WAV file. I have attached it to this post - as a .doc file because this forum does not accept .wav files - so you must rename it to .wav.

I have provided the code again below and also the output log file.

Here is how I checked the wav file:

Code: Select all

ubuntu@ip-10-0-67-173:~/wav$ qwavheaderdump sayhelloworldM1644.wav
sayhelloworldCLEANEDUP_M1644.wav (73436 bytes):
	riff: 'RIFF'
	riff length: 73428
	wave: 'WAVE'
	fmt: 'fmt '
	fmt length: 16
	format: 1
	channels: 1
	sample rate: 44100
	bytes/second: 88200
	bytes/sample: 2
	bits/sample: 16
	data: 'data'
	data length: 73392
ubuntu@ip-10-0-67-173:~/wav$
sayhelloworldM1644.doc
RENAME THIS TO .wav - it is not a doc file!
(71.71 KiB) Downloaded 850 times
Here is the output log from the latest run with the .wav file attached:

Code: Select all

ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:6004
load:0x40078000,len:9176
load:0x40080000,len:6064
0x40080000: _WindowOverflow4 at /Users/andrewstuartsupercoders/esp/esp-adf/esp-idf/components/freertos/xtensa_vectors.S:1685

entry 0x40080330
0x40080330: _KernelExceptionVector at ??:?

I (29) boot: ESP-IDF v3.1.1-6-g2aa9a2118-dirty 2nd stage bootloader
I (29) boot: compile time 08:09:59
I (40) boot: Enabling RNG early entropy source...
I (40) boot: SPI Speed      : 40MHz
I (40) boot: SPI Mode       : DIO
I (43) boot: SPI Flash Size : 4MB
I (47) boot: Partition Table:
I (51) boot: ## Label            Usage          Type ST Offset   Length
I (58) boot:  0 nvs              WiFi data        01 02 00009000 00005000
I (65) boot:  1 otadata          OTA data         01 00 0000e000 00002000
I (73) boot:  2 app0             OTA app          00 10 00010000 00200000
I (80) boot:  3 eeprom           Unknown data     01 99 00210000 00001000
I (88) boot:  4 spiffs           Unknown data     01 82 00211000 001ef000
I (95) boot: End of partition table
I (99) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x29444 (169028) map
I (142) esp_image: segment 1: paddr=0x0003946c vaddr=0x3ffc0000 size=0x031e8 ( 12776) load
I (145) esp_image: segment 2: paddr=0x0003c65c vaddr=0x3ffc31e8 size=0x00000 (     0) load
I (148) esp_image: segment 3: paddr=0x0003c664 vaddr=0x40080000 size=0x00400 (  1024) load
0x40080000: _WindowOverflow4 at /Users/andrewstuartsupercoders/esp/esp-adf/esp-idf/components/freertos/xtensa_vectors.S:1685

I (158) esp_image: segment 4: paddr=0x0003ca6c vaddr=0x40080400 size=0x035a4 ( 13732) load
I (169) esp_image: segment 5: paddr=0x00040018 vaddr=0x400d0018 size=0x94fbc (610236) map
0x400d0018: _flash_cache_start at ??:?

I (296) esp_image: segment 6: paddr=0x000d4fdc vaddr=0x400839a4 size=0x0e428 ( 58408) load
0x400839a4: spi_flash_guard_end at /Users/andrewstuartsupercoders/esp/esp-adf/esp-idf/components/spi_flash/flash_ops.c:160
 (inlined by) spi_flash_read at /Users/andrewstuartsupercoders/esp/esp-adf/esp-idf/components/spi_flash/flash_ops.c:610

I (309) esp_image: segment 7: paddr=0x000e340c vaddr=0x400c0000 size=0x00000 (     0) load
I (309) esp_image: segment 8: paddr=0x000e3414 vaddr=0x50000000 size=0x00000 (     0) load
I (319) boot: Loaded app from partition at offset 0x10000
I (321) boot: Disabling RNG early entropy source...
I (327) cpu_start: Pro cpu up.
I (331) cpu_start: Starting app cpu, entry point is 0x40081008
0x40081008: call_start_cpu1 at /Users/andrewstuartsupercoders/esp/esp-adf/esp-idf/components/esp32/cpu_start.c:231

I (0) cpu_start: App cpu up.
I (341) heap_init: Initializing. RAM available for dynamic allocation:
I (348) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
I (354) heap_init: At 3FFCAEE0 len 00015120 (84 KiB): DRAM
I (360) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (367) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (373) heap_init: At 40091DCC len 0000E234 (56 KiB): IRAM
I (379) cpu_start: Pro cpu start user code
I (24) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (285) PERIPH_SPIFFS: Partition size: total: 1856396, used: 74798
I (365) BLUETOOTH_SOURCE_EXAMPLE: [ 1 ] Create Bluetooth service
I (365) BTDM_INIT: BT controller compile version [7b0770a]

I (365) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (425) phy: phy_version: 4000, b6198fa, Sep  3 2018, 15:11:06, 0, 0
I (655) BLUETOOTH_SERVICE: Starting device discovery...
I (665) BLUETOOTH_SOURCE_EXAMPLE: [1.1] Get Bluetooth stream
I (665) BLUETOOTH_SOURCE_EXAMPLE: [ 2 ] Create SPIFFS stream to read data
I (675) BLUETOOTH_SERVICE: Discovery started.
I (675) BLUETOOTH_SOURCE_EXAMPLE: [ 3 ] Create wav decoder to decode wav file
I (685) BLUETOOTH_SOURCE_EXAMPLE: [ 4 ] Create audio pipeline for BT Source
I (695) BLUETOOTH_SERVICE: Scanned device: fc:58:fa:5b:f3:1b
I (695) BLUETOOTH_SERVICE: --Class of Device: 0x260404
I (705) BLUETOOTH_SERVICE: --RSSI: -82
I (705) BLUETOOTH_SERVICE: --Name: BT-12
I (715) BLUETOOTH_SERVICE: Found a target device, address fc:58:fa:5b:f3:1b, name BT-12
I (715) BLUETOOTH_SERVICE: Cancel device discovery ...
I (725) BLUETOOTH_SERVICE: Device discovery stopped.
I (735) BLUETOOTH_SERVICE: a2dp connecting to peer: BT-12
E (735) BT_APPL: reset flags
I (745) BLUETOOTH_SERVICE: bt_a2d_source_cb state 4, evt 0x0
I (745) BLUETOOTH_SOURCE_EXAMPLE: [4.1] Register all elements to audio pipeline
I (755) BLUETOOTH_SOURCE_EXAMPLE: [4.2] Link it together wav_decoder-->bt_stream_writer
I (765) AUDIO_PIPELINE: audio_pipeline_link:0x3ffdee20, spiffs_reader, 0x3ffe08a0
I (775) AUDIO_PIPELINE: audio_pipeline_link:0x3ffdf07c, wav, 0x3ffe1c30
I (775) BLUETOOTH_SOURCE_EXAMPLE: [5.1] Create Bluetooth peripheral
I (785) BLUETOOTH_SOURCE_EXAMPLE: [5.2] Start Bluetooth peripheral
I (795) BLUETOOTH_SOURCE_EXAMPLE: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
I (805) BLUETOOTH_SOURCE_EXAMPLE: [ 6 ] Setup event listener
I (805) BLUETOOTH_SOURCE_EXAMPLE: [6.1] Listening event from all elements of pipeline
I (815) BLUETOOTH_SOURCE_EXAMPLE: [6.2] Listening event from peripherals
I (825) BLUETOOTH_SOURCE_EXAMPLE: [ 7 ] Start audio_pipeline
I (825) AUDIO_ELEMENT: [spiffs_reader] audio_element_run
I (835) AUDIO_ELEMENT: [spiffs_reader] Element task created
I (845) AUDIO_ELEMENT: [wav] audio_element_run
I (845) AUDIO_ELEMENT: [wav] Element task created
I (855) AUDIO_ELEMENT: [bt] audio_element_run
I (855) AUDIO_ELEMENT: [bt] Element task created
I (865) AUDIO_PIPELINE: Func:audio_pipeline_run, Line:278, MEM Total:94952 Bytes

I (875) AUDIO_ELEMENT: [spiffs_reader] AEL_MSG_CMD_RESUME,state:1
I (885) AUDIO_ELEMENT: [wav] AEL_MSG_CMD_RESUME,state:1
I (885) SPIFFS_STREAM: File size is 73436 byte, pos:0
I (895) AUDIO_PIPELINE: Pipeline started
I (905) BLUETOOTH_SOURCE_EXAMPLE: [ 8 ] Listen for all pipeline events
I (905) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (905) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 94556
I (915) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 8
I (925) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (925) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 94556
I (935) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 8
I (935) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (945) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 94556
I (945) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 9
I (955) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Receive music info from wav decoder, sample_rates=44100, bits=16, ch=1
I (3805) BLUETOOTH_SERVICE: authentication success: BT-12
I (3805) BLUETOOTH_SERVICE: fc 58 fa 5b f3 1b
I (3805) BLUETOOTH_SERVICE: ESP_BT_GAP_PIN_REQ_EVT min_16_digit:253
I (3815) BLUETOOTH_SERVICE: Input pin code: 0000 0000 0000 0000
W (3815) BT_BTM: BTM_PINCodeReply() - Wrong State: 0

E (3845) BT_APPL: bta_av_rc_create ACP handle exist for shdl:0
W (4025) BT_APPL: new conn_srvc id:18, app_id:0
I (4025) BLUETOOTH_SERVICE: bt_a2d_source_cb state 4, evt 0x0
I (4025) BLUETOOTH_SERVICE: a2dp connected
I (4035) BLUETOOTH_SERVICE: a2dp media ready checking ...
I (4035) BLUETOOTH_SERVICE: bt_a2d_source_cb state 5, evt 0x3
I (4045) BLUETOOTH_SERVICE: a2dp media ready, starting ...
W (4055) BT_APPL: new conn_srvc id:18, app_id:1
I (4055) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (4065) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 87928
I (4065) BT_LOG: bta_av_link_role_ok hndl:x41 role:0 conn_audio:x1 bits:1 features:x824b

I (4075) BLUETOOTH_SERVICE: bt_a2d_source_cb state 5, evt 0x3
I (4085) BLUETOOTH_SERVICE: a2dp media start successfully.
I (4085) BLUETOOTH_SERVICE: bt_a2d_source_cb state 5, evt 0x1
I (4105) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 1
W (4395) SPIFFS_STREAM: No more data, ret:0
I (4395) AUDIO_ELEMENT: IN-[spiffs_reader] AEL_IO_DONE,0
I (4395) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (4405) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 90960
I (4405) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 8
I (4455) AUDIO_ELEMENT: IN-[wav] AEL_IO_DONE,-2
I (4455) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (4455) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 90960
I (4455) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 8
W (4505) BT_APPL: ### UNDERFLOW :: ONLY READ 176 BYTES OUT OF 512 ###
W (4505) BT_APPL: btc_media_aa_prep_sbc_2_send underflow 9, 176
I (4535) AUDIO_ELEMENT: IN-[bt] AEL_IO_DONE,-2
W (4535) BT_APPL: ### UNDERFLOW :: ONLY READ -2 BYTES OUT OF 336 ###
W (4535) BT_APPL: btc_media_aa_prep_sbc_2_send underflow 19, 174
I (4545) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Audio event
I (4555) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Free heap: 90960
I (4555) BLUETOOTH_SOURCE_EXAMPLE: [ * ] msg.cmd: 4
W (4565) BLUETOOTH_SOURCE_EXAMPLE: [ * ] Bluetooth disconnected or suspended
W (4565) BT_APPL: btc_get_num_aa_frame() - Limiting frames to be sent from 29 to 21
I (4575) AUDIO_ELEMENT: IN-[bt] AEL_IO_DONE,-2
W (4585) BT_APPL: ### UNDERFLOW :: ONLY READ -2 BYTES OUT OF 338 ###
W (4595) BT_APPL: btc_media_aa_prep_sbc_2_send underflow 21, 172
W (4595) BT_APPL: btc_get_num_aa_frame() - Limiting frames to be sent from 40 to 21
I (4605) AUDIO_ELEMENT: IN-[bt] AEL_IO_DONE,-2
W (4615) BT_APPL: ### UNDERFLOW :: ONLY READ -2 BYTES OUT OF 340 ###
W (4615) BT_APPL: btc_media_aa_prep_sbc_2_send underflow 21, 170
E (4625) BT_APPL: bta_av_str_stopped:audio_open_cnt=1, p_data 0x3ffdffe4
W (4635) BT_APPL: btc_get_num_aa_frame() - Limiting frames to be sent from 53 to 21
I (4645) AUDIO_ELEMENT: IN-[bt] AEL_IO_DONE,-2
W (4645) BT_APPL: ### UNDERFLOW :: ONLY READ -2 BYTES OUT OF 342 ###
W (4655) BT_APPL: btc_media_aa_prep_sbc_2_send underflow 21, 168
W (4665) BT_APPL: btc_get_num_aa_frame() - Limiting frames to be sent from 62 to 21
I (4665) AUDIO_ELEMENT: IN-[bt] AEL_IO_DONE,-2
W (4675) BT_APPL: ### UNDERFLOW :: ONLY READ -2 BYTES OUT OF 344 ###
W (4675) BT_APPL: btc_media_aa_prep_sbc_2_send underflow 21, 166
I (4685) BLUETOOTH_SERVICE: bt_a2d_source_cb state 5, evt 0x3
I (4695) BLUETOOTH_SERVICE: a2dp media start failed.
I (4695) BLUETOOTH_SERVICE: bt_a2d_source_cb state 5, evt 0x1
I (4705) BLUETOOTH_SOURCE_EXAMPLE: [ 9 ] Stop audio_pipeline
W (4715) AUDIO_PIPELINE: There are no listener registered
I (4715) AUDIO_PIPELINE: audio_pipeline_unlinked
E (4735) PERIPH_SPIFFS: Unmount SPIFFS error!
E (4735) PERIPH_SPIFFS: Failed to unmount SPIFFS
W (4735) BT_AVCT: avct_lcb_last_ccb
W (4735) BT_AVCT: 0: aloc:1, lcb:0x3ffde320/0x3ffde320, ccb:0x3ffde388/0x3ffde388
W (4745) BT_AVCT: 1: aloc:0, lcb:0x0/0x3ffde320, ccb:0x3ffde3a0/0x3ffde388
W (4755) BT_AVCT: 2: aloc:0, lcb:0x0/0x3ffde320, ccb:0x3ffde3b8/0x3ffde388
E (4765) BT_BTC: btc_sm_dispatch : Invalid handle
E (4775) BT_BTC: btc_sm_dispatch : Invalid handle


Code: Select all

/* Play music from Bluetooth device

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "audio_common.h"
#include "audio_element.h"
#include "audio_event_iface.h"
#include "audio_pipeline.h"
#include "bluetooth_service.h"
#include "esp_log.h"
#include "esp_peripherals.h"
#include "wav_decoder.h"
#include "nvs_flash.h"
#include "sdkconfig.h"
#include "periph_spiffs.h"
#include "spiffs_stream.h"


#define SAVE_FILE_RATE      22050
#define SAVE_FILE_CHANNEL   1
#define SAVE_FILE_BITS      8

#define PLAYBACK_RATE       44100
#define PLAYBACK_CHANNEL    2
#define PLAYBACK_BITS       16

static const char *TAG = "BLUETOOTH_SOURCE_EXAMPLE";

static audio_element_handle_t create_spiffs_stream(int sample_rates, int bits, int channels, audio_stream_type_t type)
{
    spiffs_stream_cfg_t spiffs_cfg = SPIFFS_STREAM_CFG_DEFAULT();
    spiffs_cfg.type = type;
    audio_element_handle_t spiffs_stream = spiffs_stream_init(&spiffs_cfg);
    mem_assert(spiffs_stream);
    audio_element_info_t writer_info = {0};
    audio_element_getinfo(spiffs_stream, &writer_info);
    writer_info.bits = bits;
    writer_info.channels = channels;
    writer_info.sample_rates = sample_rates;
    audio_element_setinfo(spiffs_stream, &writer_info);
    return spiffs_stream;
}

void app_main(void)
{
    //nvs_flash_erase();
    esp_err_t err = nvs_flash_init();
    if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
        // NVS partition was truncated and needs to be erased
        // Retry nvs_flash_init
        ESP_ERROR_CHECK(nvs_flash_erase());
        err = nvs_flash_init();
    }


    audio_pipeline_handle_t pipeline;
    audio_element_handle_t spiffs_reader_el, filter_upsample_el, wav_decoder, bt_stream_writer;

    // Initialize peripherals management
    esp_periph_config_t periph_cfg = { 0 };
    esp_periph_init(&periph_cfg);

    // Initialize Spiffs peripheral
    periph_spiffs_cfg_t spiffs_cfg = {
        .root = "/spiffs",
        .partition_label = NULL,
        .max_files = 5,
        .format_if_mount_failed = true
    };
    esp_periph_handle_t spiffs_handle = periph_spiffs_init(&spiffs_cfg);

    // Start spiffs peripheral
    esp_periph_start(spiffs_handle);

    // Wait until spiffs was mounted
    while (!periph_spiffs_is_mounted(spiffs_handle)) {
        vTaskDelay(100 / portTICK_PERIOD_MS);
    }



    esp_log_level_set("*", ESP_LOG_INFO);
    esp_log_level_set(TAG, ESP_LOG_DEBUG);

    ESP_LOGI(TAG, "[ 1 ] Create Bluetooth service");
    bluetooth_service_cfg_t bt_cfg = {
        .device_name = "ESP-ADF-SOURCE",
        .mode = BLUETOOTH_A2DP_SOURCE,
        .remote_name = "BT-12",
    };
    bluetooth_service_start(&bt_cfg);

    ESP_LOGI(TAG, "[1.1] Get Bluetooth stream");
    bt_stream_writer = bluetooth_service_create_stream();

    ESP_LOGI(TAG, "[ 2 ] Create SPIFFS stream to read data");
    spiffs_reader_el = create_spiffs_stream(SAVE_FILE_RATE, SAVE_FILE_BITS, SAVE_FILE_CHANNEL, AUDIO_STREAM_READER);


    ESP_LOGI(TAG, "[ 3 ] Create wav decoder to decode wav file");
    wav_decoder_cfg_t wavdec_cfg = DEFAULT_WAV_DECODER_CONFIG();
    wav_decoder = wav_decoder_init(&wavdec_cfg);
    //audio_element_set_read_cb(wav_decoder, wav_music_read_cb, NULL);

    ESP_LOGI(TAG, "[ 4 ] Create audio pipeline for BT Source");
    audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
    pipeline = audio_pipeline_init(&pipeline_cfg);

    ESP_LOGI(TAG, "[4.1] Register all elements to audio pipeline");
    audio_pipeline_register(pipeline, spiffs_reader_el,     "spiffs_reader");
    audio_pipeline_register(pipeline, wav_decoder,        "wav");
    audio_pipeline_register(pipeline, bt_stream_writer,   "bt");

    ESP_LOGI(TAG, "[4.2] Link it together wav_decoder-->bt_stream_writer");
    audio_pipeline_link(pipeline, (const char *[]) {"spiffs_reader", "wav", "bt"}, 3);

    audio_element_set_uri(spiffs_reader_el, "/spiffs/sayhelloworldM1644.wav");

    ESP_LOGI(TAG, "[5.1] Create Bluetooth peripheral");
    esp_periph_handle_t bt_periph = bluetooth_service_create_periph();

    ESP_LOGI(TAG, "[5.2] Start Bluetooth peripheral");
    esp_periph_start(bt_periph);
    ESP_LOGI(TAG, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

    ESP_LOGI(TAG, "[ 6 ] Setup event listener");
    audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
    audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);

    ESP_LOGI(TAG, "[6.1] Listening event from all elements of pipeline");
    audio_pipeline_set_listener(pipeline, evt);

    ESP_LOGI(TAG, "[6.2] Listening event from peripherals");
    audio_event_iface_set_listener(esp_periph_get_event_iface(), evt);

    ESP_LOGI(TAG, "[ 7 ] Start audio_pipeline");
    audio_pipeline_run(pipeline);

    ESP_LOGI(TAG, "[ 8 ] Listen for all pipeline events");
    while (1) {
        audio_event_iface_msg_t msg;
        esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
        if (ret != ESP_OK) {
            ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
            continue;
        }

        ESP_LOGI(TAG, "[ * ] Audio event");
        ESP_LOGI(TAG, "[ * ] Free heap: %u", xPortGetFreeHeapSize());
        ESP_LOGI(TAG, "[ * ] msg.cmd: %d", msg.cmd);

        if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) wav_decoder
            && msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
            audio_element_info_t music_info = {0};
            audio_element_getinfo(wav_decoder, &music_info);

            ESP_LOGI(TAG, "[ * ] Receive music info from wav decoder, sample_rates=%d, bits=%d, ch=%d",
                     music_info.sample_rates, music_info.bits, music_info.channels);

            audio_element_setinfo(bt_stream_writer, &music_info);
            //i2s_stream_set_clk(bt_stream_writer, music_info.sample_rates, music_info.bits, music_info.channels);
            continue;
        }


        /* Stop when the Bluetooth is disconnected or suspended */
        if (msg.source_type == PERIPH_ID_BLUETOOTH
                && msg.source == (void *)bt_periph) {
            if ((msg.cmd == PERIPH_BLUETOOTH_DISCONNECTED) || (msg.cmd == PERIPH_BLUETOOTH_AUDIO_SUSPENDED)) {
                ESP_LOGW(TAG, "[ * ] Bluetooth disconnected or suspended");
                periph_bluetooth_stop(bt_periph);
                break;
            }
        }
    }

    ESP_LOGI(TAG, "[ 9 ] Stop audio_pipeline");
    audio_pipeline_terminate(pipeline);

    /* Terminate the pipeline before removing the listener */
    audio_pipeline_remove_listener(pipeline);

    /* Stop all peripherals before removing the listener */
    esp_periph_stop_all();
    audio_event_iface_remove_listener(esp_periph_get_event_iface(), evt);

    /* Make sure audio_pipeline_remove_listener & audio_event_iface_remove_listener are called before destroying event_iface */
    audio_event_iface_destroy(evt);

    /* Release all resources */
    audio_pipeline_unregister(pipeline, spiffs_reader_el);
    audio_pipeline_unregister(pipeline, bt_stream_writer);
    audio_pipeline_unregister(pipeline, wav_decoder);
    //audio_pipeline_deinit(spiffs_reader_el);
    audio_pipeline_deinit(pipeline);
    audio_element_deinit(bt_stream_writer);
    audio_element_deinit(wav_decoder);
    esp_periph_destroy();
    bluetooth_service_destroy();
}

Re: Can't get WAV to play to bluetooth speaker please help

Posted: Mon Mar 04, 2019 11:26 pm
by andrews
Thanks to Angus from Espressif I now have the WAV file playing to bluetooth speaker.

BUT there is still a problem ..... the beginning of the WAV file is not played on the bluetooth speaker.

Here is the working code https://github.com/bootrino/esp32_spiffs_wav_bluetooth

Can someone from Espressif please help to work out why the code at https://github.com/bootrino/esp32_spiffs_wav_bluetooth drops the beginning of the audio?

Many thanks.

Re: Can't get WAV to play to bluetooth speaker please help

Posted: Tue Mar 05, 2019 1:20 am
by ESP_Angus
andrews wrote:
Mon Mar 04, 2019 11:26 pm
Can someone from Espressif please help to work out why the code at https://github.com/bootrino/esp32_spiffs_wav_bluetooth drops the beginning of the audio?
Are you sure the audio is being dropped by the ESP32 and not the speaker? If the speaker has to power on an amplifier (for example) when the stream starts then it may mute the output for a short period, to avoid pops/clicks from startup.