Skip to content

Audio 'on the stack' or 'on the heap' ? #1148

@stef-ladefense

Description

@stef-ladefense

hi schreibfaul1, the first thing I say is thank you, thank you for this library of rare quality.

I'm working on a complex project that uses your library to create a web radio station (as you might expect from the errors I'm reporting). I tested it by working on the heap instead of the stack, and I'm getting excellent stability.

  1. Audio audio;
    The memory space required to store the audio object is directly reserved on the stack. If the Audio library is large, allocating the audio object on the stack can consume a significant portion of this limited space, risking a stack overflow if too many objects or functions are stacked.

  2. Audio *audio = new Audio();
    The memory space for the Audio object is reserved on the heap. The audio variable itself is a pointer (a memory address) that is allocated on the stack, but the actual Audio object it refers to is located on the heap. This approach is advantageous for large objects, such as those from a voluminous library, because it does not impact the limited stack space, instead using the heap.

Both methods work. I get stack-based crashes when using a web frontend (using ESPAsyncWebServer), which disappear when using the heap.

The only problem is that m3u8 links don't work with the heap-based function. Their management is too recent to worry about, but you might get some insight on this.

I would like to have your opinion on this use, and if you have a little time to test, here is a piece of code where you will be able to switch from one to the other on a standard link and an m3u8 link.

// comment for standard use
//#define alternate

#ifdef alternate
Audio *audio = new Audio();
#else
Audio audio;
#endif

// callbacks
void my_audio_info(Audio::msg_t m) {
    Serial.printf("%s: %s\n", m.s, m.msg);
}

void setup() {
    Audio::audio_info_callback = my_audio_info;    // optional
    Serial.begin(115200);
    WiFi.begin(ssid.c_str(), password.c_str());
    while (WiFi.status() != WL_CONNECTED) delay(1500);

#ifdef alternate
    audio->setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
    audio->setVolume(21);    // default 0...21
    audio->connecttohost("http://stream.antennethueringen.de/live/aac-64/stream.antennethueringen.de/");
    //audio.connecttohost("https://live.m6radio.quortex.io/webpHJPXnXrN7B6J7Q8mcqmxP/webradio/rtl/202/audio-64000/index.m3u8");
#else
    audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
    audio.setVolume(21);    // default 0...21
    audio.connecttohost("http://stream.antennethueringen.de/live/aac-64/stream.antennethueringen.de/");
    //audio.connecttohost("https://live.m6radio.quortex.io/webpHJPXnXrN7B6J7Q8mcqmxP/webradio/rtl/202/audio-64000/index.m3u8");
#endif
}

void loop() {

#ifdef alternate
    audio->loop();
#else
    audio.loop();
#endif

    vTaskDelay(1);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions