Class Sound
Defined in File Sound.h
Class Documentation
-
class Sound
USE sound manager.
The Sound class handles all sounds produced by the components, mixes and plays them. Multi-platform audio provided by miniaudio.h.
How to use this class: 1) Construct using correct number of required output nodes. 2) Start the sound device using start(). 3) Periodically call writeSamples according to sample rate (which can be found using getSampleRate). 4) Enjoy the sound. 5) Stop the device using stop().
The class mixes the output of all the devices, passes them through LPF and outputs to the sound device. Ring buffer is used to store samples in advance to battle crackling when there are not enough samples to play when the dataCallback is called (missed deadline problem). If the buffer is not available at the moment of writing, a cache is used instead, which is flushed to the buffer in the next write where the buffer is available.
Note about terms used: This class uses same terminology as miniaudio.h documentation - frame consists of samples, the count equals the number of audio channels. This means stereo frame consists of a left speaker sample followed by the right speaker sample.
Public Functions
-
explicit Sound(size_t outputCount)
Create sound device and prepare node graph with specified number of sources.
- Parameters
outputCount – Required number of input nodes. Equals the total amount of Components’ audio outputs in the System.
-
~Sound()
-
void start()
Start the sound device (which executes dataCallback periodically to asks for more audio frames).
-
void stop()
Stop the sound device.
-
void writeFrames(const SoundSampleSources &sources)
Write audio frames to their respective buffers (or caches, if the buffer is currently used by the audio callback).
- Parameters
sources – Sound sources to take frames from.
Public Static Functions
-
static inline constexpr int getSampleRate()
Get sample rate of the audio device.
Note
Used mainly to generate appropriate number of audio frames per second (which are then passsed to writeFrames).
- Returns
Sample rate.
-
explicit Sound(size_t outputCount)