ESP32-audioI2S icon indicating copy to clipboard operation
ESP32-audioI2S copied to clipboard

Optimize Program Size: Add Macros to Disable Unused Audio Decoders

Open rvbc1 opened this issue 1 year ago • 4 comments

Hello,

I’ve added the following macros to the compilation process to disable unnecessary audio decoders:

AUDIO_I2S_DISABLE_AAC_DECODER AUDIO_I2S_DISABLE_FLAC_DECODER AUDIO_I2S_DISABLE_MP3_DECODER AUDIO_I2S_DISABLE_OPUS_DECODER AUDIO_I2S_DISABLE_VORBIS_DECODER

In my specific use case, I only need the MP3 decoder, so the additional decoders were consuming unnecessary flash memory. Even with a custom partition table optimized for maximum program size, I was struggling with flash memory limits.

To address this, I implemented simple #ifndef directives where necessary. I believe I have covered most, if not all, required locations, as the program now compiles successfully without any macros enabled. I tested it on a basic program and my project. It also compiles when only one decoder is enabled (with four out of the five macros activated). The solution works well on my project with only the MP3 decoder enabled.

In my tests, disabling all unnecessary decoders reduced the program size by approximately 300KB, which is a significant improvement, representing 22% of the program flash size based on the default ESP32 partition scheme. There might be more opportunities for optimization if any locations were missed.

For those using PlatformIO, you can implement these macros by adding the following lines to your platformio.ini file:

build_flags = 
	-D AUDIO_I2S_DISABLE_AAC_DECODER
	-D AUDIO_I2S_DISABLE_FLAC_DECODER
	-D AUDIO_I2S_DISABLE_MP3_DECODER
	-D AUDIO_I2S_DISABLE_OPUS_DECODER
	-D AUDIO_I2S_DISABLE_VORBIS_DECODER

I hope this improvement proves helpful to others as well ;)

rvbc1 avatar Aug 07 '24 14:08 rvbc1

With an ESP32 with 4MB Flash you have ~3.4MB available. That's not enough?

schreibfaul1 avatar Aug 07 '24 16:08 schreibfaul1

With OTA, I need two partitions for the program (ota1 and ota2), which gives me 1.9MB of flash memory for the program. The remaining 0.2MB is used for NVS, bootloader, etc. In the current version 3.12, my program consumes 99.8% of the available flash memory. On the master branch (after adding the AAC decoder), the program size is around 2.1MB. I also use a custom Arduino library builder (with all IDF debug options disabled, as the standard settings have warnings and errors enabled) because starting with Arduino 3.0.x, the program became too large (Arduino 2.x had a smaller flash footprint).

rvbc1 avatar Aug 07 '24 18:08 rvbc1

I’m using Wi-Fi, Ethernet, Webserver, MQTT, SD card, this library, and an external DAC driver. These components take up a significant amount of flash memory.

rvbc1 avatar Aug 07 '24 18:08 rvbc1

Additional preprocessor instructions can also be a source of errors. If you only need only the MP3 decoder, you can also remove most of the code, not just the other decoders.

schreibfaul1 avatar Aug 12 '24 20:08 schreibfaul1

This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days.

github-actions[bot] avatar Nov 10 '24 02:11 github-actions[bot]

This PR was closed because it has been stalled for 10 days with no activity.

github-actions[bot] avatar Nov 20 '24 02:11 github-actions[bot]