ESP32 Webradio

frankiPL
Posts: 5
Joined: Thu Nov 16, 2017 8:59 am

Re: ESP32 Webradio

Postby frankiPL » Thu Nov 16, 2017 9:18 am

Hi MrBuddyCasino,

I was asking You earlier about reading from broadcast (created issue on github).
Now I would like to run Your project in two modes (to test it for 80 devices)
1. reading stream from local server
2. reading stream from SD card
We have added SD card support and server to write mp3 files from external server to SD card, for each ESP32 player
Regarding second option I can see two possibilities
a) feed mp3 stream from mp3 File to audio_stream_consumer in audio_player.c
b) use http_client_get from http.c and first write "fake" headers into http_parser_execute and then mp3 stream

Could you tell us which one we should use? "B" option should read/set all header values before audio_player_start on the other hand we intend to play only mp3 with constant bitrate, so maybe we should skip the http header parsing and just write mp3 stream to spiramfifo ?

Best Regards
Marek

BuddyCasino
Posts: 231
Joined: Sun Jun 19, 2016 12:00 am

Re: ESP32 Webradio

Postby BuddyCasino » Fri Nov 17, 2017 12:54 pm

The right abstraction would be a separate URL handler for sd cards I guess. So depending on the URL scheme, the handler would choose http/https/scdard/filesystem or whatever. Not sure how well sdcard access can be abstracted, never done it.

frankiPL
Posts: 5
Joined: Thu Nov 16, 2017 8:59 am

Re: ESP32 Webradio

Postby frankiPL » Sat Nov 18, 2017 10:23 am

Hi

We have created new task for reading from SD card and it works well, below there is example code.
This is still work in progress, but if You accept pull requests we would like to add this functionality when it is complete.

Best Regards
Marek

Code: Select all

static void sd_get_task(void *pvParameters)
{
    web_radio_t *radio_conf = pvParameters;

    /* configure callbacks */
    http_parser_settings callbacks = { 0 };
    callbacks.on_body = on_body_cb;
    callbacks.on_header_field = on_header_field_cb;
    callbacks.on_header_value = on_header_value_cb;
    callbacks.on_headers_complete = on_headers_complete_cb;
    callbacks.on_message_complete = on_message_complete_cb;

    // blocks until end of stream
    int result = st_file_get("/sdcard/test_file.mp3",&callbacks,radio_conf->player_config);

    if (result != 0) {
        ESP_LOGE(TAG, "sd_client_get error");
    } else {
        ESP_LOGI(TAG, "sd_client_get completed");
    }

    vTaskDelete(NULL);
}

void web_radio_start(web_radio_t *config)
{
    // start reader task
    //xTaskCreatePinnedToCore(&http_get_task, "http_get_task", 2560, config, 20,NULL, 0);
    xTaskCreatePinnedToCore(&sd_get_task, "sd_get_task", 2560, config, 20,NULL, 0);
}

char header[]="HTTP/1.0 200 OK\nContent-Type: audio/mpeg\n\n";
FILE* st_read_file;

int st_file_get(const char *file_name, http_parser_settings *callbacks, void *user_data) {

    /* Read HTTP response */
    char recv_buf[64];
    bzero(recv_buf, sizeof(recv_buf));
    ssize_t recved;

    /* parse response */
    http_parser parser;
    http_parser_init(&parser, HTTP_RESPONSE);
    parser.data = user_data;

    esp_err_t nparsed = 0;
    ESP_LOGI(TAG,"%s size:%d",header,strlen(header));
    nparsed = http_parser_execute(&parser, callbacks, header, strlen(header));


    st_open_file("","test_file.mp3");
    fseek(st_file,400000,SEEK_SET);

    nparsed=0;
    do {
        recved = fread(recv_buf,sizeof(char),sizeof(recv_buf)-1,st_read_file);
       // ESP_LOGI(TAG,"write mp3 bytes:%d",recved);

        // using http parser causes stack overflow somtimes - disable for now
        nparsed = http_parser_execute(&parser, callbacks, recv_buf, recved);

    } while(recved > 0 && nparsed >= 0);

    st_read_close_file();

    return 0;
}
void st_read_open_file(const char *dir_name,const char *file_name) {
    char path[100];
    create_file_path(path,dir_name,file_name);
    ESP_LOGI(TAG,"st_open_file path:%s",path);
    st_read_file = fopen(path, "r");
    if (st_read_file == NULL) {
        ESP_LOGE(TAG, "Failed to open file %s",path);
        return;
    }
    ESP_LOGI(TAG,"File open OK\n");
}
void st_read_close_file() {
    fclose(st_read_file);
    ESP_LOGI(TAG,"File closed\n");
}

BuddyCasino
Posts: 231
Joined: Sun Jun 19, 2016 12:00 am

Re: ESP32 Webradio

Postby BuddyCasino » Sun Nov 19, 2017 3:55 pm

Nice, looks like you know what you're doing. Can you tell me what hardware are you using? So I can order the parts to test it on my side.

frankiPL
Posts: 5
Joined: Thu Nov 16, 2017 8:59 am

Re: ESP32 Webradio

Postby frankiPL » Sat Nov 25, 2017 12:55 pm

Hi,

Very sorry for the late reply, I had to switch to other tasks.
I have some problem with writing to SD card in SPI mode, so I described my configuration in this thread
https://www.esp32.com/viewtopic.php?f=2&t=3657 unfortunately no response.
We will try access card in SDIO mode.
In about two weeks we will order some prototype boards with ESP32 and other modules like DAC so if you are interested I can write more.

Best Regards
Marek

joknjokn
Posts: 1
Joined: Tue Nov 28, 2017 8:57 am

Re: ESP32 Webradio

Postby joknjokn » Tue Nov 28, 2017 9:31 am

Hello everyone,

I was looking into whether the ESP32 could be used as the "brain" in DIY guitar pedals/synthesizers. It seems relatively powerful.

With your knowledge (I'm no expert), do you think it would be powerful enough for this, with potential good audio quality?

If so, do you have a suggestion for a tiny budget-friendly I2S DAC board with input and output of good/fine quality?

Thank you for any help!

- Jonas
Student, Aalborg University Copenhagen

BuddyCasino
Posts: 231
Joined: Sun Jun 19, 2016 12:00 am

Re: ESP32 Webradio

Postby BuddyCasino » Tue Nov 28, 2017 2:11 pm

It should work very well for this task. You can find code on Github or via Hackaday projects, the ESP32 has two full-duplex I2S ports which you can use. If I were you I'd just go search Aliexpress for I2S boards that suit you, there are plenty on sale for little money.

Who is online

Users browsing this forum: Bing [Bot] and 2 guests