pschatzmann / arduino-liblame

A simple mp3 encoder (not only) for Arduino using LAME
21 stars 3 forks source link

Unable to 'mp3.begin' after importing 'MP3DecoderHelix' (ESP32-S3-DEV-KIT-N8R8-M) #10

Closed jWos1 closed 9 months ago

jWos1 commented 9 months ago

Hi, I'm trying use MP3EncoderLAME along with MP3DecoderHelix from the arduino-audio-tools repo. The example for mp3 encoding from the README file works perfectly fine. However, when I try to import the MP3DecoderHelix as in the streams-url_mp3_helix-i2s example (which works fine as well) from arduino-audio-tools repo, the program seems to break during mp3.begin call and reboots the board. Also, I had to get rid of using namespace liblame; for it to even compile.

Below is the mp3 encoding code with my modifications (I have marked my changes with a comment // CUSTOM):

#include "AudioTools.h" // CUSTOM
#include "AudioCodecs/CodecMP3Helix.h" // CUSTOM
#include "MP3EncoderLAME.h"
#include <stdlib.h>  // for rand

// using namespace liblame; // CUSTOM

void dataCallback(uint8_t *mp3_data, size_t len) {
    Serial.print("mp3 generated with ");
    Serial.print(len);
    Serial.println(" bytes");
}

liblame::MP3EncoderLAME mp3(dataCallback); // CUSTOM
liblame::AudioInfo info; // CUSTOM
int16_t buffer[512];

void setup() {
    Serial.begin(115200);

    info.channels = 1;
    info.sample_rate = 44100;
    mp3.begin(info);

    Serial.println("writing...");

}

void loop() {
    Serial.println("writing 512 samples of random data");
    for (int j=0;j<512;j++){
        buffer[j] = (rand() % 100) - 50;         
    }
    mp3.write(buffer, 512*sizeof(int16_t));
}

Here are the logs that I get:

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x40376d64
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbe4
load:0x403cc700,len:0x2a68
entry 0x403c98d4
[   150][I][esp32-hal-psram.c:96] psramInit(): PSRAM enabled
[Info] C:\Users\Jan\Documents\Arduino\libraries\libLAME\src\liblame\lame.c : 2559 - lame_init - success
[Info] C:\Users\Jan\Documents\Arduino\libraries\libLAME\src\liblame\lame.c : 657 - step 1
[Info] C:\Users\Jan\Documents\Arduino\libraries\libLAME\src\liblame\lame.c : 662 - step 2
[Info] C:\Users\Jan\Documents\Arduino\libraries\libLAME\src\liblame\lame.c : 757 - lame_init_params - samplerate_out: 0
Guru Meditation Error: Core  0 panic'ed (Unhandled debug exception). 
Debug exception reason: Stack canary watchpoint triggered (IDLE) 
Core  0 register dump:
PC      : 0x40056f60  PS      : 0x00060136  A0      : 0x8002e1d7  A1      : 0x3fcf4440  
A2      : 0x3fcf4450  A3      : 0x3ff1ea24  A4      : 0x00000025  A5      : 0x3fcf4450  
A6      : 0x33323130  A7      : 0x37363534  A8      : 0x00000000  A9      : 0x3fcf4680  
A10     : 0x42019bc4  A11     : 0x42041f7c  A12     : 0x00060120  A13     : 0x80000000  
A14     : 0x02c93b30  A15     : 0x00ffffff  SAR     : 0x0000001d  EXCCAUSE: 0x00000001  
EXCVADDR: 0x00000000  LBEG    : 0x40056f5c  LEND    : 0x40056f72  LCOUNT  : 0x00000001  

Backtrace: 0x40056f5d:0x3fcf4440 |<-CORRUPTED

ELF file SHA256: c5a4db905105da7b

Rebooting...

Are these two components (mp3 encoder and decoder) supposed to be working fine together in a single sketch? Any ideas what am I doing wrong?

pschatzmann commented 9 months ago

You will need to analyse the stack-trace. Most likely it a failing memory allocation because you are running out of memory... Liblame uses a lot of memory: in fact even when you start wifi, this will not work any more. https://github.com/dankeboy36/esp-exception-decoder