MrBuddyCasino / ESP32_MP3_Decoder

A internet radio and bluetooth speaker project for the Espressif ESP32
787 stars 185 forks source link

return MAD_ERROR_LOSTSYNC when decode mp3 file, lost synchronization #48

Open NOA-Vernon opened 6 years ago

NOA-Vernon commented 6 years ago

Hi, I want to read the mp3 file from sd card, decode it and output throuhg built-in DAC, but it occured an error(MAD_ERROR_LOSTSYNC ). following is my code that calling funcion: static void play_mp3_file(void vparameter) { web_radio_t radio_conf = vparameter;

char mp3_data[1024];//[4096];

player_t *player_config = radio_conf->player_config;

int ret;
int i;

ESP_LOGI(TAG,"play_mp3_file task\r\n"); FILE* f = fopen("/sdcard/ghsy.mp3","r"); if (f == NULL) { ESP_LOGI(TAG,"Failed to open file for reading.\r\n"); vTaskDelete(NULL); }

player_config->media_stream->content_type = AUDIO_MPEG;
player_config->media_stream->eof = false;

player_config->command = CMD_START; player_config->buffer_pref = BUF_PREF_FAST;//BUF_PREF_SAFE;//

audio_player_start(player_config); 
while(1)
{
    ret=fread(mp3_data,1,sizeof(mp3_data),f);

    if(ret <= 0)
    {
        ESP_LOGI(TAG,"play mp3 read file error.\r\n");
        break;
    }

ESP_LOGI(TAG,"play_mp3_file read data succ\r\n"); while( audio_stream_consumer(mp3_data, sizeof(mp3_data), player_config) != 0) { vTaskDelay(500/ portTICK_PERIOD_MS); ESP_LOGI(TAG,"play_mp3_file vTaskDelay ing\r\n"); } vTaskDelay(3000/ portTICK_PERIOD_MS); } fclose(f); printf("delete play_mp3_file_task.\r\n"); 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); ESP_LOGI(TAG,"web_radio_start\r\n");
xTaskCreate(&play_mp3_file, "play_mp3_file", 4096+4096, config, 5, NULL);

}

and the debug log is:

ets Jun 8 2016 00:22:57

rst:0x1 (POWERON_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:5648 ho 0 tail 12 room 4 load:0x40078000,len:0 load:0x40078000,len:13912 entry 0x40078fd8 I (30) boot: ESP-IDF v3.1-dev-463-g77eae33 2nd stage bootloader I (30) boot: compile time 16:47:34 I (30) boot: Enabling RNG early entropy source... I (36) boot: SPI Speed : 40MHz I (40) boot: SPI Mode : DIO I (44) boot: SPI Flash Size : 4MB I (48) boot: Partition Table: I (52) boot: ## Label Usage Type ST Offset Length I (59) boot: 0 nvs WiFi data 01 02 00009000 00006000 I (67) boot: 1 phy_init RF data 01 01 0000f000 00001000 I (74) boot: 2 factory factory app 00 00 00010000 00200000 I (82) boot: End of partition table I (86) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x48468 (296040) map I (197) esp_image: segment 1: paddr=0x00058490 vaddr=0x3ffb0000 size=0x041a0 ( 16800) load I (204) esp_image: segment 2: paddr=0x0005c638 vaddr=0x40080000 size=0x00400 ( 1024) load I (205) esp_image: segment 3: paddr=0x0005ca40 vaddr=0x40080400 size=0x035d0 ( 13776) load I (219) esp_image: segment 4: paddr=0x00060018 vaddr=0x400d0018 size=0x67cb8 (425144) map I (369) esp_image: segment 5: paddr=0x000c7cd8 vaddr=0x400839d0 size=0x07e94 ( 32404) load I (383) esp_image: segment 6: paddr=0x000cfb74 vaddr=0x400c0000 size=0x00000 ( 0) load I (391) boot: Loaded app from partition at offset 0x10000 I (391) boot: Disabling RNG early entropy source... I (393) cpu_start: Pro cpu up. I (396) cpu_start: Starting app cpu, entry point is 0x400812a8 I (0) cpu_start: App cpu up. I (407) heap_init: Initializing. RAM available for dynamic allocation: I (413) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM I (419) heap_init: At 3FFBE3A8 len 00021C58 (135 KiB): DRAM I (426) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM I (432) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM I (438) heap_init: At 4008B864 len 0001479C (81 KiB): IRAM I (445) cpu_start: Pro cpu start user code I (127) cpu_start: Starting scheduler on PRO CPU. I (0) cpu_start: Starting scheduler on APP CPU. I (129) main: starting app_main() I (129) main: RAM left: 253568 I (159) main: hardware initialized I (159) main: Initializing SD card I (159) main: Using SPI peripheral I (169) gpio: GPIO[13]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0  Name: SD16G Type: SDHC/SDXC Speed: default speed Size: 14832MB CSD: ver=1, sector_size=512, capacity=30375936 read_bl_len=9 SCR: sd_spec=2, bus_width=5 I (289) playlist: Tracks: SomaFM / Illinois Street Lounge - http://ice1.somafm.com/illstreet-128-mp3 SomaFM / Secret Agent - http://ice1.somafm.com/secretagent-128-mp3 SomaFM / Left Coast 70s - http://ice1.somafm.com/seventies-128-mp3 SomaFM / Boot Liquor - http://ice1.somafm.com/bootliquor-128-mp3 I (309) renderer: init I2S mode 2, port 0, 16 bit, 44100 Hz I (319) I2S: queue free spaces: 1 I (319) I2S: DMA Malloc info, datalen=blocksize=256, dma_buf_count=32 I (329) I2S: PLL_D2: Req RATE: 44100, real rate: 2777.000, BITS: 16, CLKM: 30, BCK: 60, MCLK: 30.234, SCLK: 88864.000000, diva: 64, divb: 14 I (339) web_radio: web_radio_start

 I (349) main: RAM left 205480 I (349) web_radio: play_mp3_file task

 I (359) web_radio: play_mp3_file read data succ

 I (3359) web_radio: play_mp3_file read data succ

 I (6359) web_radio: play_mp3_file read data succ

 I (9359) web_radio: play_mp3_file read data succ

 I (12359) web_radio: play_mp3_file read data succ

 I (15359) web_radio: play_mp3_file read data succ

 I (18359) web_radio: play_mp3_file read data succ

 I (21359) web_radio: play_mp3_file read data succ

 I (24359) web_radio: play_mp3_file read data succ

 I (27359) web_radio: play_mp3_file read data succ

 I (30359) web_radio: play_mp3_file read data succ

 I (33359) web_radio: play_mp3_file read data succ

 I (36359) web_radio: play_mp3_file read data succ

 I (39359) web_radio: play_mp3_file read data succ

 I (42359) web_radio: play_mp3_file read data succ

 I (45359) web_radio: play_mp3_file read data succ

 I (48359) web_radio: play_mp3_file read data succ

 I (51359) web_radio: play_mp3_file read data succ

 I (54359) web_radio: play_mp3_file read data succ

 I (57359) web_radio: play_mp3_file read data succ

 I (60359) web_radio: play_mp3_file read data succ

 I (63359) web_radio: play_mp3_file read data succ

 I (66359) web_radio: play_mp3_file read data succ

 I (69359) web_radio: play_mp3_file read data succ

 I (72359) web_radio: play_mp3_file read data succ

 I (75359) web_radio: play_mp3_file read data succ

 I (78359) web_radio: play_mp3_file read data succ

 I (81359) web_radio: play_mp3_file read data succ

 I (84359) web_radio: play_mp3_file read data succ

 I (84359) audio_player: RAM left 209848 I (84359) audio_player: created decoder task: mp3_decoder_task I (84359) mad_decoder: decoder start E (84369) mad_decoder: dec err 0x0101 (lost synchronization) E (84369) mad_decoder: dec err 0x0101 (lost synchronization) E (84379) mad_decoder: dec err 0x0101 (lost synchronization) E (84389) mad_decoder: dec err 0x0102 (reserved header layer value) E (84389) mad_decoder: dec err 0x0101 (lost synchronization) E (84399) mad_decoder: dec err 0x0101 (lost synchronization) E (84409) mad_decoder: dec err 0x0101 (lost synchronization) E (84409) mad_decoder: dec err 0x0101 (lost synchronization) E (84419) mad_decoder: dec err 0x0101 (lost synchronization) E (84429) mad_decoder: dec err 0x0101 (lost synchronization) E (84429) mad_decoder: dec err 0x0101 (lost synchronization) E (84439) mad_decoder: dec err 0x0101 (lost synchronization) E (84439) mad_decoder: dec err 0x0101 (lost synchronization) E (84449) mad_decoder: dec err 0x0101 (lost synchronization) E (84459) mad_decoder: Buffer underflow, need 2881 bytes. E (84459) mad_decoder: Buffer underflow, need 2881 bytes. E (84469) mad_decoder: Buffer underflow, need 2881 bytes. E (84469) mad_decoder: Buffer underflow, need 2881 bytes. E (84479) mad_decoder: Buffer underflow, need 2881 bytes. E (84489) mad_decoder: Buffer underflow, need 2881 bytes. E (84489) mad_decoder: Buffer underflow, need 2881 bytes. E (84499) mad_decoder: Buffer underflow, need 2881 bytes. E (84499) mad_decoder: Buffer underflow, need 2881 bytes. E (84509) mad_decoder: Buffer underflow, need 2881 bytes. E (84519) mad_decoder: Buffer underflow, need 2881 bytes. E (84519) mad_decoder: Buffer underflow, need 2881 bytes. E (84529) mad_decoder: Buffer underflow, need 2881 bytes. E (84539) mad_decoder: Buffer underflow, need 2881 bytes. E (84539) mad_decoder: Buffer underflow, need 2881 bytes. E (84549) mad_decoder: Buffer underflow, need 2881 bytes. E (84549) mad_decoder: Buffer underflow, need 2881 bytes. E (84559) mad_decoder: Buffer underflow, need 2881 bytes. E (84569) mad_decoder: Buffer underflow, need 2881 bytes. E (84569) mad_decoder: Buffer underflow, need 2881 bytes. E (84579) mad_decoder: Buffer underflow, need 2881 bytes. E (84579) mad_decoder: Buffer underflow, need 2881 bytes. E (84589) mad_decoder: Buffer underflow, need 2881 bytes. E (84599) mad_decoder: Buffer underflow, need 2881 bytes. E (84599) mad_decoder: Buffer underflow, need 2881 bytes. E (84609) mad_decoder: Buffer underflow, need 2881 bytes. E (84609) mad_decoder: Buffer underflow, need 2881 bytes. E (84619) mad_decoder: Buffer underflow, need 2881 bytes. E (84629) mad_decoder: Buffer underflow, need 2881 bytes. E (84629) mad_decoder: Buffer underflow, need 2881 bytes. E (84639) mad_decoder: Buffer underflow, need 2881 bytes. E (84639) mad_decoder: Buffer underflow, need 2881 bytes. E (84649) mad_decoder: Buffer underflow, need 2881 bytes. E (84659) mad_decoder: Buffer underflow, need 2881 bytes. E (84659) mad_decoder: Buffer underflow, need 2881 bytes. E (84669) mad_decoder: Buffer underflow, need 2881 bytes. E (84679) mad_decoder: Buffer underflow, need 2881 bytes. E (84679) mad_decoder: Buffer underflow, need 2881 bytes. E (84689) mad_decoder: Buffer underflow, need 2881 bytes. E (84689) mad_decoder: Buffer underflow, need 2881 bytes. E (84699) mad_decoder: Buffer underflow, need 2881 bytes. E (84709) mad_decoder: Buffer underflow, need 2881 bytes. E (84709) mad_decoder: Buffer underflow, need 2881 bytes. E (84719) mad_decoder: Buffer underflow, need 2881 bytes. E (84719) mad_decoder: Buffer underflow, need 2881 bytes. E (84729) mad_decoder: Buffer underflow, need 2881 bytes. E (84739) mad_decoder: Buffer underflow, need 2881 bytes. E (84739) mad_decoder: Buffer underflow, need 2881 bytes. E (84749) mad_decoder: Buffer underflow, need 2881 bytes. E (84749) mad_decoder: Buffer underflow, need 2881 bytes. E (84759) mad_decoder: Buffer underflow, need 2881 bytes. E (84769) mad_decoder: Buffer underflow, need 2881 bytes. E (84769) mad_decoder: Buffer underflow, need 2881 bytes. E (84779) mad_decoder: Buffer underflow, need 2881 bytes. E (84779) mad_decoder: Buffer underflow, need 2881 bytes. E (84789) mad_decoder: Buffer underflow, need 2881 bytes. E (84799) mad_decoder: Buffer underflow, need 2881 bytes. E (84799) mad_decoder: Buffer underflow, need 2881 bytes. E (84809) mad_decoder: Buffer underflow, need 2881 bytes. E (84809) mad_decoder: Buffer underflow, need 2881 bytes. E (84819) mad_decoder: Buffer underflow, need 2881 bytes. E (84829) mad_decoder: Buffer underflow, need 2881 bytes. E (84829) mad_decoder: Buffer underflow, need 2881 bytes. E (84839) mad_decoder: Buffer underflow, need 2881 bytes. E (84849) mad_decoder: Buffer underflow, need 2881 bytes. E (84849) mad_decoder: Buffer underflow, need 2881 bytes. E (84859) mad_decoder: Buffer underflow, need 2881 bytes. E (84859) mad_decoder: Buffer underflow, need 2881 bytes. E (84869) mad_decoder: Buffer underflow, need 2881 bytes.

Is there any hint or solution for solving this problem? thank you in advance.

SinanAkkoyun commented 4 years ago

Did you figure it out? I am also working on it...

NOA-Vernon commented 4 years ago

I just increased the mp3 decode buffer for getting more data from remote server before playing, but this is a workaround only.