ESP32 Webradio

AlexanderK
Posts: 3
Joined: Sun Nov 05, 2017 9:41 pm

Re: ESP32 Webradio

Postby AlexanderK » Mon Nov 06, 2017 8:53 pm

Hi Buddy,
I'm studying your project and I have a problem with playing music in APLL mode with PDM. First I compiled the sources without any change and start radio with APPL + PDM. And the problem is that output play very-very-very slow - actually even I can't understand what is playing. I've tried without APLL - it plays OK (but not a long time).

Below is the output for APLL and non-APLL modes. For APLL It looks good except XTAL 35MHz. Could you please advice what is the problem? Maybe for APLL I need another configuration of HW?

>>> Output for APLL
...
I (66) boot: Detected ESP32
W (68) rtc_clk: Potentially bogus XTAL frequency: 35 MHz, guessing 40 MHz
...

I (3756) renderer: init I2S mode 3, port 0, 16 bit, 44100 Hz
I (3756) renderer: chip revision 1, enabling APLL
I (3766) I2S: queue free spaces: 1
I (3766) I2S: DMA Malloc info, datalen=blocksize=256, dma_buf_count=32
I (3776) I2S: APLL: Req RATE: 44100, real rate: 44099.020, BITS: 16, CLKM: 1, BC K: 8, MCLK: 5.669, SCLK: 1411168.625000, diva: 1, divb: 0
I (3786) gpio: GPIO[0]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldo wn: 0| Intr:1
I (3796) main: RAM left 170588
....
I (4226) audio_player: Buffer fill 50%, 16012 bytes
I (4316) audio_player: RAM left 170968
I (4316) audio_player: created decoder task: mp3_decoder_task
I (4316) mad_decoder: decoder start
E (4316) mad_decoder: dec err 0x0235 (bad main_data_begin pointer)
E (4326) mad_decoder: dec err 0x0235 (bad main_data_begin pointer)
I (4326) audio_player: Buffer fill 91%, 29134 bytes
I (4516) wifi: pm start, type:0

I (8016) audio_player: Buffer fill 94%, 30123 bytes
I (12126) audio_player: Buffer fill 96%, 30985 bytes
...
I (51116) audio_player: Buffer fill 99%, 31832 bytes
I (53846) http_client: ... done reading from socket. Last read return=0 errno=12 8
I (53846) http_client: socket closed
I (53846) web_radio: http_client_get completed
I (63186) mad_decoder: decoder stopped
I (63186) mad_decoder: MAD decoder stack: 540


>>> Output for non-APLL
I (3276) renderer: init I2S mode 3, port 0, 16 bit, 44100 Hz
I (3276) renderer: chip revision 1, enabling APLL
I (3286) I2S: queue free spaces: 1
I (3286) I2S: DMA Malloc info, datalen=blocksize=256, dma_buf_count=32
I (3296) I2S: PLL_D2: Req RATE: 44100, real rate: 200000.000, BITS: 16, CLKM: 5, BCK: 5, MCLK: 5.669, SCLK: 6400000.000000, diva: 64, divb: 42
....
For me it is strange digits i I2S output here. Playing is OK, but not a long time as it plays lower rate than in browser and I suppose output buffer of server finished. Thank for help in advance.

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

Re: ESP32 Webradio

Postby BuddyCasino » Tue Nov 07, 2017 9:05 am

I simply didn't test APLL + PDM, assuming it would work the same as I2S. Can you open an issue on Github please?

AlexanderK
Posts: 3
Joined: Sun Nov 05, 2017 9:41 pm

Re: ESP32 Webradio

Postby AlexanderK » Tue Nov 07, 2017 11:58 am

Yes of course. Done. DAC APLL=1 also doesn't work also. I wrote it also. Thank you!

frankiPL
Posts: 9
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: 263
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: 9
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: 263
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: 9
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
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: 263
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: No registered users and 6 guests