So far I never needed to use any of the ESP32 multicore functionality because all the examples are very simple and everything is fitting on a single core. However if you write some more complex scenarios which are e.g. updating a screen, process user input w/o impacting the audio, you should consider to use multiple tasks. Then you need to use a save way to communicate data between the tasks.
In this case I strongly recommend to use the arduino-freertos-addons which provides all the functionality you need in an elegant way: Just read the Readme of the project.
Unfortunately a FreeRTOS StreamBuffer was missing and I also wanted to have a platform independent solution, so I have added some additional functionality to my audio-tools:
- SynchronizedBufferRTOS: a FreeRTOS StreamBuffer implementation using the BaseBuffer API
- Mutex: with different implementations per platform.
- LockGuard: a RAII class for managing the Mutex
- SynchronizedBuffer: which allows you to use any BaseBuffer implementation in a save way
If you use an ESP32, it is much more efficient to use a FreeRTOS StreamBuffer. Just write the data with writeArray() on one Task and read it back with readArry() from the other task. It is the same API you would use with any other class which is based on BaseBuffer
#include "AudioLibs/SynchronizedBuffer.h" SynchronizedBufferRTOS<int16_t> buffer(2048, 512);
In my test sketch I was measuring a thruput of 36.57-37.93 Mbytes per second.
Here is an example how to use the SynchronizedBuffer class. Just wrap one of the existing buffer classes. Here is an example of a thread-save Double Buffer:
#include "AudioLibs/SynchronizedBuffer.h" audio_tools::Mutex mutex; NBuffer<int16_t> nbuffer(512,2); SynchronizedBuffer<int16_t> buffer(nbuffer, mutex);
In my test sketch this was giving a thuput of only 1.68 Mbytes per second.
Here is a thread-save RingBuffer
#include "AudioLibs/SynchronizedBuffer.h" audio_tools::Mutex mutex; RingBuffer<int16_t> nbuffer(512*4); SynchronizedBuffer<int16_t> buffer(nbuffer, mutex);
and I was measuring 2.35 Mbytes per second.