Module spectrusty::audio::carousel

source ·
Expand description

Tools for assisting audio rendering via audio frameworks that run on separate threads.

Some audio frameworks require sample generators to be run on a separate thread while the sound is being played to provide data to fill the audio buffer just in time when it needs to be refilled.

When emulating a computer we have to synchronize emulation, render video frames, read user input, and at the same time to render audio samples asynchronously exactly when some external thread tells us to. Having a concurrent thread for rendering audio frames makes it somewhat difficult.

This module exists solely for the purpose to ease this task. “The Carousel” consists of an audio producer and an audio consumer. The audio producer lives in the same thread where the emulation is run and where the sound is being produced. The audio consumer is delegated to the audio thread and its role is to relay audio samples to the audio framework.

                                 (new sample data)
                    /----> AudioBuffer ----> AudioBuffer ---->\
+----------------------+                                  +----------------------+
|  AudioFrameProducer  |                                  |  AudioFrameConsumer  | -> 🔊
+----------------------+                                  +----------------------+
                    \<---- AudioBuffer <---- AudioBuffer -----/
                                 (recycled buffers)

The produced audio buffers, ready to be played, are being sent via mpsc::channel from the audio producer to the audio consumer. The consumer fills the audio buffers provided by the audio framework with samples from the received audio buffer frames and sends the used up frame buffers back via another channel to the audio producer to be filled again with new sample data.

The size of each audio buffer is determined only by the emulated frame duration and is unrelated to the audio framework output buffer size.

The number of buffers in circulation determines the audio latency. The larger the latency the more stable the playback is at the cost of the delay of the sound. Knowing the output buffer size the minimum latency should be calculated from the number of samples in the output buffer divided by the number of samples in the single audio frame plus one.

Structs

The audio buffer is a carrier of audio samples generated for every emulated frame.
Relays AudioBuffer samples to the audio framework output buffers.
Allows relaying rendered AudioBuffer to the AudioFrameConsumer.

Traits

Provides various methods to primitive types being used as audio samples.

Functions

Creates an inter-connected pair or AudioFrameProducer and AudioFrameConsumer.

Type Definitions