URY playd
C++ minimalist audio player
PipeAudio Class Reference

A concrete implementation of Audio as a 'pipe'. More...

#include <audio.hpp>

+ Inheritance diagram for PipeAudio:
+ Collaboration diagram for PipeAudio:

Public Member Functions

 PipeAudio (std::unique_ptr< AudioSource > src, std::unique_ptr< AudioSink > sink)
 Constructs a PipeAudio from a source and a sink. More...
 
Audio::State Update () override
 Performs an update cycle on this Audio. More...
 
const std::string & File () const override
 This Audio's current file. More...
 
void SetPlaying (bool playing) override
 Sets whether this Audio should be playing or not. More...
 
Audio::State CurrentState () const override
 The state of this Audio. More...
 
void SetPosition (std::uint64_t position) override
 Attempts to seek to the given position. More...
 
std::uint64_t Position () const override
 This Audio's current position. More...
 
- Public Member Functions inherited from Audio
virtual ~Audio ()=default
 Virtual, empty destructor for Audio.
 

Private Member Functions

void ClearFrame ()
 Clears the current frame and its iterator.
 
bool DecodeIfFrameEmpty ()
 Decodes a new frame, if the current frame is empty. More...
 
bool FrameFinished () const
 Returns whether the current frame has been finished. More...
 
void TransferFrame ()
 Transfers as much of the current frame as possible to the sink.
 

Private Attributes

std::unique_ptr< AudioSourcesrc
 The source of audio data.
 
std::unique_ptr< AudioSinksink
 The sink to which audio data is sent.
 
AudioSource::DecodeVector frame
 The current decoded frame.
 
AudioSource::DecodeVector::iterator frame_iterator
 The current position in the current decoded frame.
 

Additional Inherited Members

- Public Types inherited from Audio
enum  State : uint8_t { State::NONE, State::STOPPED, State::PLAYING, State::AT_END }
 Enumeration of possible states for this Audio. More...
 

Detailed Description

A concrete implementation of Audio as a 'pipe'.

PipeAudio is comprised of a 'source', which decodes frames from a file, and a 'sink', which plays out the decoded frames. Updating consists of shifting frames from the source to the sink.

See also
Audio
AudioSink
AudioSource

Definition at line 147 of file audio.hpp.

Constructor & Destructor Documentation

§ PipeAudio()

PipeAudio::PipeAudio ( std::unique_ptr< AudioSource src,
std::unique_ptr< AudioSink sink 
)

Constructs a PipeAudio from a source and a sink.

Parameters
srcThe source of decoded audio frames.
sinkThe target of decoded audio frames.
See also
AudioSystem::Load

Definition at line 62 of file audio.cpp.

References ClearFrame().

64  : src(std::move(src)), sink(std::move(sink))
65 {
66  this->ClearFrame();
67 }
void ClearFrame()
Clears the current frame and its iterator.
Definition: audio.cpp:112
std::unique_ptr< AudioSource > src
The source of audio data.
Definition: audio.hpp:170
std::unique_ptr< AudioSink > sink
The sink to which audio data is sent.
Definition: audio.hpp:173

Member Function Documentation

§ CurrentState()

Audio::State PipeAudio::CurrentState ( ) const
overridevirtual

The state of this Audio.

Returns
this Audio's current state.

Implements Audio.

Definition at line 85 of file audio.cpp.

References sink.

86 {
87  return this->sink->State();
88 }
std::unique_ptr< AudioSink > sink
The sink to which audio data is sent.
Definition: audio.hpp:173

§ DecodeIfFrameEmpty()

bool PipeAudio::DecodeIfFrameEmpty ( )
private

Decodes a new frame, if the current frame is empty.

Returns
True if more frames are available to decode; false otherwise.

Definition at line 153 of file audio.cpp.

References AudioSource::END_OF_FILE, frame, frame_iterator, FrameFinished(), and src.

Referenced by Update().

154 {
155  // Either the current frame is in progress, or has been emptied.
156  // AdvanceFrameIterator() establishes this assertion by emptying a
157  // frame as soon as it finishes.
158  assert(this->frame.empty() || !this->FrameFinished());
159 
160  // If we still have a frame, don't bother decoding yet.
161  if (!this->FrameFinished()) return true;
162 
163  assert(this->src != nullptr);
164  AudioSource::DecodeResult result = this->src->Decode();
165 
166  this->frame = result.second;
167  this->frame_iterator = this->frame.begin();
168 
169  return result.first != AudioSource::DecodeState::END_OF_FILE;
170 }
bool FrameFinished() const
Returns whether the current frame has been finished.
Definition: audio.cpp:172
AudioSource::DecodeVector::iterator frame_iterator
The current position in the current decoded frame.
Definition: audio.hpp:179
The decoder has run out of things to decode.
AudioSource::DecodeVector frame
The current decoded frame.
Definition: audio.hpp:176
std::pair< DecodeState, DecodeVector > DecodeResult
Type of the result of Decode().
std::unique_ptr< AudioSource > src
The source of audio data.
Definition: audio.hpp:170

§ File()

const std::string & PipeAudio::File ( ) const
overridevirtual

This Audio's current file.

Returns
The filename of this current file.
Exceptions
NoAudioErrorif the current state is NONE.

Implements Audio.

Definition at line 69 of file audio.cpp.

References src.

70 {
71  return this->src->Path();
72 }
std::unique_ptr< AudioSource > src
The source of audio data.
Definition: audio.hpp:170

§ FrameFinished()

bool PipeAudio::FrameFinished ( ) const
private

Returns whether the current frame has been finished.

If this is true, then either the frame is empty, or all of the samples in the frame have been fed to the ringbuffer.

Returns
True if the frame is finished; false otherwise.

Definition at line 172 of file audio.cpp.

References frame, and frame_iterator.

Referenced by DecodeIfFrameEmpty(), TransferFrame(), and Update().

173 {
174  return this->frame.end() <= this->frame_iterator;
175 }
AudioSource::DecodeVector::iterator frame_iterator
The current position in the current decoded frame.
Definition: audio.hpp:179
AudioSource::DecodeVector frame
The current decoded frame.
Definition: audio.hpp:176

§ Position()

std::uint64_t PipeAudio::Position ( ) const
overridevirtual

This Audio's current position.

As this may be executing whilst the playing callback is running, do not expect it to be highly accurate.

Returns
The current position, in microseconds.
Exceptions
NoAudioErrorif the current state is NONE.
See also
Seek

Implements Audio.

Definition at line 90 of file audio.cpp.

References sink, and src.

91 {
92  assert(this->sink != nullptr);
93  assert(this->src != nullptr);
94 
95  return this->src->MicrosFromSamples(this->sink->Position());
96 }
std::unique_ptr< AudioSource > src
The source of audio data.
Definition: audio.hpp:170
std::unique_ptr< AudioSink > sink
The sink to which audio data is sent.
Definition: audio.hpp:173

§ SetPlaying()

void PipeAudio::SetPlaying ( bool  playing)
overridevirtual

Sets whether this Audio should be playing or not.

Parameters
playingTrue for playing; false for stopped.
Exceptions
NoAudioErrorif the current state is NONE.

Implements Audio.

Definition at line 74 of file audio.cpp.

References sink.

75 {
76  assert(this->sink != nullptr);
77 
78  if (playing) {
79  this->sink->Start();
80  } else {
81  this->sink->Stop();
82  }
83 }
std::unique_ptr< AudioSink > sink
The sink to which audio data is sent.
Definition: audio.hpp:173

§ SetPosition()

void PipeAudio::SetPosition ( std::uint64_t  position)
overridevirtual

Attempts to seek to the given position.

Parameters
positionThe position to seek to, in microseconds.
Exceptions
NoAudioErrorif the current state is NONE.
See also
Position

Implements Audio.

Definition at line 98 of file audio.cpp.

References ClearFrame(), sink, and src.

99 {
100  assert(this->sink != nullptr);
101  assert(this->src != nullptr);
102 
103  auto in_samples = this->src->SamplesFromMicros(position);
104  auto out_samples = this->src->Seek(in_samples);
105  this->sink->SetPosition(out_samples);
106 
107  // We might still have decoded samples from the old position in
108  // our frame, so clear them out.
109  this->ClearFrame();
110 }
void ClearFrame()
Clears the current frame and its iterator.
Definition: audio.cpp:112
std::unique_ptr< AudioSource > src
The source of audio data.
Definition: audio.hpp:170
std::unique_ptr< AudioSink > sink
The sink to which audio data is sent.
Definition: audio.hpp:173

§ Update()

Audio::State PipeAudio::Update ( )
overridevirtual

Performs an update cycle on this Audio.

Depending on the Audio implementation, this may do actions such as performing a decoding round, checking for end-of-file, transferring frames, and so on.

Returns
The state of the Audio after updating.
See also
State

Implements Audio.

Definition at line 118 of file audio.cpp.

References DecodeIfFrameEmpty(), FrameFinished(), sink, src, and TransferFrame().

119 {
120  assert(this->sink != nullptr);
121  assert(this->src != nullptr);
122 
123  bool more_available = this->DecodeIfFrameEmpty();
124  if (!more_available) this->sink->SourceOut();
125 
126  if (!this->FrameFinished()) this->TransferFrame();
127 
128  return this->sink->State();
129 }
bool FrameFinished() const
Returns whether the current frame has been finished.
Definition: audio.cpp:172
void TransferFrame()
Transfers as much of the current frame as possible to the sink.
Definition: audio.cpp:131
bool DecodeIfFrameEmpty()
Decodes a new frame, if the current frame is empty.
Definition: audio.cpp:153
std::unique_ptr< AudioSource > src
The source of audio data.
Definition: audio.hpp:170
std::unique_ptr< AudioSink > sink
The sink to which audio data is sent.
Definition: audio.hpp:173

The documentation for this class was generated from the following files: