pschatzmann / arduino-liblame

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

Running out of memory(?) #9

Open poolster opened 8 months ago

poolster commented 8 months ago

I have installed the library and running the simple example on an ESP-WROOM-32 based board, however it seems that it quickly runs out of memory at runtime. This is the debugging info:

[Debug] D:\Documenten\Arduino\libraries\arduino-liblame-main\src\liblame\lame.c : 2539 - lame_init ==> calloc(1,304) -> 0x3ffbb114 - available MALLOC_CAP_8BIT: 110580 / MALLOC_CAP_32BIT: 110580 / MALLOC_CAP_SPIRAM: 0 [Debug] D:\Documenten\Arduino\libraries\arduino-liblame-main\src\liblame\lame.c : 2451 - lame_init_old [Debug] D:\Documenten\Arduino\libraries\arduino-liblame-main\src\liblame\util.c : 864 - disable_FPE ==> calloc(1,85840) -> 0x3ffe46c4 - available MALLOC_CAP_8BIT: 34804 / MALLOC_CAP_32BIT: 69620 / MALLOC_CAP_SPIRAM: 0 [Debug] D:\Documenten\Arduino\libraries\arduino-liblame-main\src\liblame\lame.c : 2397 - lame_init_internal_flags ==> calloc(1,2772) -> 0x3ffbb254 - available MALLOC_CAP_8BIT: 34804 / MALLOC_CAP_32BIT: 69620 / MALLOC_CAP_SPIRAM: 0 [Error] D:\Documenten\Arduino\libraries\arduino-liblame-main\src\liblame\lame.c : 2772 - calloc(1,38808) -> 0x0 available MALLOC_CAP_8BIT: 34804 / MALLOC_CAP_32BIT: 69620 / MALLOC_CAP_SPIRAM: 0

I'm not familiar with the LAME encoder, but it can't be that memory hungry for encoding blocks of 512 bytes, or can it?

Only changes I've made to config.h are USE_DEBUG 1, USE_DEBUG_ALLOC 1 and ESP_PSRAM_ENABLE_LIMIT 0

Thanks in advance for your help!

pschatzmann commented 8 months ago

Unfortunately it is. If you are looking for an efficient codec have a look at adpcm.

To resolve your issue activate psram!

poolster commented 8 months ago

Thanks for your speedy comment, Phil. Unfortunately the WROOM has pretty big internal flash but doesn´t have any internal SPIRAM...

I got your AAC-encoder working though on this same platform! And since my audio finally ends up on a VS1053 that'll work just as well for me.

dl9sec commented 4 months ago

Unfortunately I can't get this code compiling even with 4MB of enabled PSRAM (ESP32 Audio Kit V2.2 A402 with ESP32-AIS):

#include "AudioTools.h"
#include "AudioLibs/AudioBoardStream.h"
#include "AudioCodecs/CodecMP3LAME.h"

// WIFI
const char *ssid = "myssid";
const char *password = "mypass";

AudioInfo info(16000,1,16);
MP3EncoderLAME mp3;
AudioEncoderServer server(&mp3, ssid, password);
AudioBoardStream kit(AudioKitEs8388V1);    

// Arduino setup
void setup(){
  Serial.begin(115200);
  // Defining Loglevels for the different libraries
  //AudioLogger::instance().begin(Serial, AudioLogger::Info);
  //LOGLEVEL_FDK = FDKInfo; 
  //LOGLEVEL_AUDIOKIT = AudioKitInfo;

  // start i2s input with default configuration
  Serial.println("starting AudioKit...");
  auto config = kit.defaultConfig(RX_MODE);
  config.input_device = ADC_INPUT_LINE2;
  config.copyFrom(info); 
  config.sd_active = false;
  kit.begin(config);
  Serial.println("AudioKit started");

  // start data sink
  server.begin(kit, info);
  Serial.println("Server started");
}

// Arduino loop  
void loop() {
  // Handle new connections
  server.doLoop();  
}

It ends up in:

c:/users/thors/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\Users\thors\AppData\Local\Temp\arduino_build_38567/AnalogAudioMP3Streamer.ino.elf section `.dram0.bss' will not fit in region `dram0_0_seg'
c:/users/thors/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: DRAM segment data does not fit.
c:/users/thors/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: DRAM segment data does not fit.
c:/users/thors/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: region `dram0_0_seg' overflowed by 3368 bytes
collect2.exe: error: ld returned 1 exit status
pschatzmann commented 4 months ago

For what partition scheme ? I see that you forgot to configure the codec...

dl9sec commented 4 months ago

Partition scheme "Huge APP". Which configuration of the MP3 codec? I couldn't find anything useful to configure from the Doxygen documentation (or the other examples for MP3)... Maybe I am simply too unexperienced with that CPP stuff...

pschatzmann commented 4 months ago

My mistake, I can reproduce your issue, but not I have no idea yet what might cause this.

pschatzmann commented 4 months ago

It seems that the ESP32 core is using much more static memory now. I tried to roll back to a very old 1.0.6 release and there it is working perfectly.

There is also no problem when using the encoder w/o WIFI. The 3.0.0-alpha release seems to work as well.

dl9sec commented 4 months ago

Oh, that's a real pitty :-( It seems that there is a high potential in the ESP-IDF to save ESP core memory: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/performance/ram-usage.html#optimizing-iram-usage But I don't know, if there are similar possibilities in the Arduino environment... Edit: Saw, that is about IRAM usage not DRAM usage...

pschatzmann commented 4 months ago

I found another work around: in liblame: config.h, I changed USE_MEMORY_HACK to 0 and decreased ESP_PSRAM_ENABLE_LIMIT to 1024 So in any case mp3 encoding works only with PSRAM!

I also needed to change the logic on the server a bit because the new Chrome versions are timing out the connection before we can return the mp3 data.

dl9sec commented 4 months ago

That's great! It works like a charm now. Thank you very much for the efforts early in the morning :-D