-
-
Notifications
You must be signed in to change notification settings - Fork 237
Vector, Allocators & PSRAM
The best way to allocate an Array of data in the AudioTools is by using a Vector. E.g. Vector<int16_t> vector{100000};
is allocating a vector of 100'000 2 byte signed integers.
The ESP32 has a few hundred kilobytes of internal RAM, residing on the same die as the rest of the chip components. It can be insufficient for audio, so it has the ability to use up to 4 MB of external PSRAM (Psuedostatic RAM) memory.
To use this you need to activate PSRAM in the Arduino Tools menu. There is one small complication: it is not available yet when global variables are defined! The best way around this is to make sure that the allocation is done in the Arduino setup.
An Allocator defines how memory is allocated: You can define the Allocator in the constructor of the Vector and if nothing is defined, we automatically use the DefaultAllocator which is using PSRAM if possible and if this fails it resorts back to regular RAM.
The following allocator objects have been predefined:
- DefaultAllocator (use PSRAM and RAM)
- DefaultAllocatorPSRAM (only use PSRAM)
- DefaultAllocatorRAM (only use RAM)
and you can create your own by implementing an subclass of Allocator.
Here are a couple of examples:
#include "AudioTools.h"
Vector<int16_t> vectorPSRAM{0}; // same as vectorPSRAM{0, DefaultAllocator};
Vector<int16_t> vectorRAM{10000}; // Allocation in RAM because PSRAM fails.
Vector<int16_t> vectorRAM1{10000, DefaultAllocatorRAM}; // allocation in RAM
Vector<int16_t> vectorRAM2{0, DefaultAllocatorRAM}; // allocation in RAM
void setup() {
vectorPSRAM.resize(10000); // allocate data in PSRAM
vectorRAM2.resize(10000); // allocate data in RAM
}
Since I am using the Vector class to allocate memory in all my other audio classes, PSRAM is automatically used, if activated and the resize() method is a flixible way to adjust the size to dynamic requirements.