An output stream for audio, using SDL. More...
#include <audio_sink.hpp>
Public Member Functions | |
SdlAudioSink (const AudioSource &source, int device_id) | |
Constructs an SdlAudioSink. More... | |
~SdlAudioSink () override | |
Destructs an SdlAudioSink. | |
void | Start () override |
Starts the audio stream. More... | |
void | Stop () override |
Stops the audio stream. More... | |
Audio::State | State () override |
Gets this AudioSink's current state (playing/stopped/at end). More... | |
std::uint64_t | Position () override |
Gets the current played position in the song, in samples. More... | |
void | SetPosition (std::uint64_t samples) override |
Sets the current played position, given a position in samples. More... | |
void | SourceOut () override |
Tells this AudioSink that the source has run out. More... | |
void | Transfer (TransferIterator &start, const TransferIterator &end) override |
Transfers a range of sample bytes into the AudioSink. More... | |
void | Callback (std::uint8_t *out, int nbytes) |
The callback proper. More... | |
Public Member Functions inherited from AudioSink | |
virtual | ~AudioSink ()=default |
Virtual, empty destructor for AudioSink. | |
Static Public Member Functions | |
static std::vector< std::pair< int, std::string > > | GetDevicesInfo () |
Gets the number and name of each output device entry in the AudioSystem. More... | |
static bool | IsOutputDevice (int id) |
Can a sound device output sound? More... | |
static void | InitLibrary () |
Initialises the AudioSink's libraries, if not initialised already. | |
static void | CleanupLibrary () |
Cleans up the AudioSink's libraries, if not cleaned up already. | |
Private Attributes | |
SDL_AudioDeviceID | device |
The SDL device to which we are outputting sound. | |
size_t | bytes_per_sample |
Number of bytes in one sample. | |
RingBuffer | ring_buf |
The ring buffer used to transfer samples to the playing callback. | |
std::uint64_t | position_sample_count |
The current position, in samples. | |
bool | source_out |
Whether the source has run out of things to feed the sink. | |
Audio::State | state |
The decoder's current state. | |
Static Private Attributes | |
static const size_t | RINGBUF_POWER = 16 |
n, where 2^n is the capacity of the Audio ring buffer. More... | |
static const std::array< SDL_AudioFormat, SAMPLE_FORMAT_COUNT > | FORMATS |
Mapping from SampleFormats to their equivalent SDL_AudioFormats. More... | |
Additional Inherited Members | |
Public Types inherited from AudioSink | |
using | TransferIterator = AudioSource::DecodeVector::iterator |
Type of iterators used in the Transfer() method. | |
An output stream for audio, using SDL.
An SdlAudioSink consists of an SDL output device and a buffer that stores decoded samples from the Audio object. While active, the SdlAudioSink periodically transfers samples from its buffer to SDL2 in a separate thread.
Definition at line 108 of file audio_sink.hpp.
SdlAudioSink::SdlAudioSink | ( | const AudioSource & | source, |
int | device_id | ||
) |
Constructs an SdlAudioSink.
source | The source from which this sink will receive audio. |
device_id | The device ID to which this sink will output. |
Definition at line 61 of file audio_sink.cpp.
References AudioSource::ChannelCount(), FORMATS, AudioSource::OutputSampleFormat(), and AudioSource::SampleRate().
void SdlAudioSink::Callback | ( | std::uint8_t * | out, |
int | nbytes | ||
) |
The callback proper.
This is executed in a separate thread by SDL once a stream is playing with the callback registered to it.
out | The output buffer to which our samples should be written. |
nbytes | The number of bytes SDL wants to read from out. |
Definition at line 195 of file audio_sink.cpp.
References Audio::AT_END, bytes_per_sample, Audio::PLAYING, position_sample_count, RingBuffer::Read(), RingBuffer::ReadCapacity(), ring_buf, source_out, and state.
|
static |
Gets the number and name of each output device entry in the AudioSystem.
Definition at line 241 of file audio_sink.cpp.
Referenced by ExitWithUsage().
|
static |
Can a sound device output sound?
id | Device ID. |
Definition at line 255 of file audio_sink.cpp.
Referenced by GetDeviceID().
|
overridevirtual |
Gets the current played position in the song, in samples.
As this may be executing whilst the playing callback is running, do not expect it to be highly accurate.
Implements AudioSink.
Definition at line 143 of file audio_sink.cpp.
References position_sample_count.
|
overridevirtual |
Sets the current played position, given a position in samples.
This flushes out the AudioSink ready to receive sample data from the new position.
samples | The new position, as a count of elapsed samples. |
Implements AudioSink.
Definition at line 148 of file audio_sink.cpp.
References Audio::AT_END, RingBuffer::Flush(), position_sample_count, ring_buf, source_out, state, Stop(), and Audio::STOPPED.
|
overridevirtual |
Tells this AudioSink that the source has run out.
When this occurs, the next time the ringbuf goes empty, the sink has also run out and should stop.
Implements AudioSink.
Definition at line 135 of file audio_sink.cpp.
References Audio::AT_END, source_out, and state.
|
overridevirtual |
Starts the audio stream.
Implements AudioSink.
Definition at line 114 of file audio_sink.cpp.
References device, Audio::PLAYING, state, and Audio::STOPPED.
|
overridevirtual |
Gets this AudioSink's current state (playing/stopped/at end).
Reimplemented from AudioSink.
Definition at line 130 of file audio_sink.cpp.
References state.
|
overridevirtual |
Stops the audio stream.
Implements AudioSink.
Definition at line 122 of file audio_sink.cpp.
References device, state, and Audio::STOPPED.
Referenced by SetPosition().
|
overridevirtual |
Transfers a range of sample bytes into the AudioSink.
The range may be empty, but must be valid.
start | An iterator denoting the start of the range. This iterator will be advanced by the number of bytes accepted. |
end | An iterator denoting the end of the range. |
Implements AudioSink.
Definition at line 165 of file audio_sink.cpp.
References bytes_per_sample, ring_buf, RingBuffer::Write(), and RingBuffer::WriteCapacity().
|
staticprivate |
Mapping from SampleFormats to their equivalent SDL_AudioFormats.
Definition at line 168 of file audio_sink.hpp.
Referenced by SdlAudioSink(), and AudioSink::State().
|
staticprivate |
n, where 2^n is the capacity of the Audio ring buffer.
Definition at line 165 of file audio_sink.hpp.
Referenced by AudioSink::State().