home/ atoms/ real-time-audio-callback-model

Real-time audio computes each sample on demand inside a callback that must meet its deadline and never block

In a real-time audio system, audio is not pre-generated and buffered — a callback function is invoked whenever a block of samples must be delivered, and it must return within its deadline (the block duration) or a glitch occurs. The callback cannot block: no dynamic memory allocation, no file I/O, no system calls, no waiting on a lock held by another thread. Code that is safe in ordinary contexts can cause dropouts inside the callback. The defining distinction: an oscillator that pre-generates several seconds of audio then plays it back does NOT run in real time; one that computes each sample on demand inside the callback DOES. This constraint shapes every architectural decision in embedded and low-latency audio programming.

Examples

The Bela C++ course contrasts an oscillator that generates five seconds of audio up front against a live callback oscillator. Forbidden operations (malloc, fopen, sleep) form the practical boundary test for real-time safety.

Assessment

Given a C++ audio sketch, identify which function calls are unsafe inside the audio callback and explain why each can cause a missed deadline. Rewrite a blocking section to be callback-safe.

“does not run in real time: it generates 5 seconds of audio at the beginning then plays it back”