Page 1 of 1

Play wav snippets in a loop

Posted: Mon Jan 04, 2021 10:35 pm
by _johnny_
Hi guys,

how can one play wave snippets in a loop?

I want to build a music box and couldn't find anything in the net.

This is the pipeline I have which works fine for one time play:

Code: Select all

    audio_pipeline_register(pipeline_play, fatfs_reader_el, "file_reader");
    audio_pipeline_register(pipeline_play, wav_decoder_el, "wav_decoder");
    audio_pipeline_register(pipeline_play, sonic_el, "sonic");
    audio_pipeline_register(pipeline_play, i2s_writer_el, "i2s_writer");

    const char *link_play[4] = {"file_reader", "wav_decoder", "sonic", "i2s_writer"};
    audio_pipeline_link(pipeline_play, &link_play[0], 4);
I added a callback on the fatfs_reader, which triggers if there is no data left:

Code: Select all

audio_element_set_event_callback(wav_decoder_el, audio_element_event_handler, NULL);

esp_err_t audio_element_event_handler(audio_element_handle_t self, audio_event_iface_msg_t *event, void *ctx)
{
    ESP_LOGI(TAG, "Audio event %d from %s element", event->cmd, audio_element_get_tag(self));
    if (event->cmd == AEL_MSG_CMD_REPORT_STATUS)
    {
        switch ((int)event->data)
        {
        case AEL_STATUS_STATE_RUNNING:
            ESP_LOGI(TAG, "AEL_STATUS_STATE_RUNNING");
            break;
        case AEL_STATUS_STATE_STOPPED:
            ESP_LOGI(TAG, "AEL_STATUS_STATE_STOPPED");
            break;
        case AEL_STATUS_STATE_FINISHED:
            ESP_LOGI(TAG, "AEL_STATUS_STATE_FINISHED");
            //create_fatfs_stream(SAMPLE_RATE, BITS, CHANNEL, AUDIO_STREAM_READER);
            //            audio_pipeline_reset_items_state(pipeline_play);
            //            fatfs_reader_el = create_fatfs_stream(SAMPLE_RATE, BITS, CHANNEL, AUDIO_STREAM_READER);

//            audio_element_info_t writer_info = {0};
//            audio_element_getinfo(fatfs_reader_el, &writer_info);
//            ESP_LOGI(TAG, "%d Total bytes", (int)writer_info.total_bytes);

/* what has to be done here to make it play from the beginning? */

            audio_element_set_byte_pos(fatfs_reader_el,0);
            event->data = AEL_STATUS_STATE_RUNNING;

            break;
        default:
            ESP_LOGI(TAG, "Some other event = %d", (int)event->data);
        }
    }
    return ESP_OK;
}
Any other hints? recommendations?