How callback works with wavPlayer?

I am trying to understand how this program works:

It is always on a loop running sampler.Prepare(). But you can control the wav output with the encoder

The encoder part is on the callBack function, but when is the callBack function executed if the program only runs sampler.Prepare()

I have looked how sampler.Prepare() is declared, but I don’t see it uses a callBack function

When this calls the callBack function?

Audio callback is called when data from the DMA transaction is received from codec. It interrupts main() function to process new buffer of audio. In this case it sends results from sampler.Stream() to codec output:

Sampler itself is not controlling anything, you call it constantly to receive new data and less frequently to prepare new data. You can think of this as 2 separate threads of execution, except that audio callback always has higher priority than main loop.

1 Like

When does that happen? Where is the “callback interruption” declared?

I am asking this because I am trying to make the user able to change the audio that is playing. I see some examples use the callback function, and others use the main() loop

When codec’s buffer is half full - timing is controlled by MCU peripherals.

Well mostly here, but that probably won’t help much.

Really, if you have such questions, it’s better to read a bit about interrupts on microcontrollers, STM32H7 reference manual, etc. Or maybe my explanation above about timing and priorities between DMA and MCU would be good enough.

Well the general rule is that audio callback should react on audio input from codec , everything else goes to main loop to make it lower priority. So audio gets processing in realtime, hardware events and user input goes after that as time permits.

1 Like