URY playd
C++ minimalist audio player
audio.hpp
Go to the documentation of this file.
1 // This file is part of playd.
2 // playd is licensed under the MIT licence: see LICENSE.txt.
3 
10 #ifndef PLAYD_AUDIO_HPP
11 #define PLAYD_AUDIO_HPP
12 
13 #include <cstdint>
14 #include <memory>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 #include "../response.hpp"
20 #include "audio_source.hpp"
21 
22 class AudioSink;
23 
35 class Audio
36 {
37 public:
42  enum class State : uint8_t {
43  NONE,
44  STOPPED,
45  PLAYING,
46  AT_END,
47  };
48 
50  virtual ~Audio() = default;
51 
52  //
53  // Control interface
54  //
55 
66  virtual State Update() = 0;
67 
73  virtual void SetPlaying(bool playing) = 0;
74 
81  virtual void SetPosition(std::uint64_t position) = 0;
82 
83  //
84  // Property access
85  //
86 
92  virtual const std::string &File() const = 0;
93 
98  virtual Audio::State CurrentState() const = 0;
99 
110  virtual std::uint64_t Position() const = 0;
111 };
112 
122 class NoAudio : public Audio
123 {
124 public:
125  Audio::State Update() override;
126  Audio::State CurrentState() const override;
127 
128  // The following all raise an exception:
129 
130  void SetPlaying(bool playing) override;
131  void SetPosition(std::uint64_t position) override;
132  std::uint64_t Position() const override;
133  const std::string &File() const override;
134 };
135 
147 class PipeAudio : public Audio
148 {
149 public:
156  PipeAudio(std::unique_ptr<AudioSource> src,
157  std::unique_ptr<AudioSink> sink);
158 
159  Audio::State Update() override;
160  const std::string &File() const override;
161 
162  void SetPlaying(bool playing) override;
163  Audio::State CurrentState() const override;
164 
165  void SetPosition(std::uint64_t position) override;
166  std::uint64_t Position() const override;
167 
168 private:
170  std::unique_ptr<AudioSource> src;
171 
173  std::unique_ptr<AudioSink> sink;
174 
177 
179  AudioSource::DecodeVector::iterator frame_iterator;
180 
182  void ClearFrame();
183 
189  bool DecodeIfFrameEmpty();
190 
197  bool FrameFinished() const;
198 
200  void TransferFrame();
201 };
202 
203 #endif // PLAYD_AUDIO_HPP
Declaration of the AudioSource class.
State
Enumeration of possible states for this Audio.
Definition: audio.hpp:42
Abstract class for audio output sinks.
Definition: audio_sink.hpp:28
The Audio is currently playing.
The Audio has ended and can&#39;t play without a seek.
virtual const std::string & File() const =0
This Audio&#39;s current file.
virtual ~Audio()=default
Virtual, empty destructor for Audio.
There is no Audio.
AudioSource::DecodeVector::iterator frame_iterator
The current position in the current decoded frame.
Definition: audio.hpp:179
virtual std::uint64_t Position() const =0
This Audio&#39;s current position.
virtual void SetPosition(std::uint64_t position)=0
Attempts to seek to the given position.
AudioSource::DecodeVector frame
The current decoded frame.
Definition: audio.hpp:176
A dummy Audio implementation representing a lack of file.
Definition: audio.hpp:122
virtual void SetPlaying(bool playing)=0
Sets whether this Audio should be playing or not.
virtual Audio::State CurrentState() const =0
The state of this Audio.
std::vector< std::uint8_t > DecodeVector
Type of decoded sample vectors.
std::unique_ptr< AudioSource > src
The source of audio data.
Definition: audio.hpp:170
A concrete implementation of Audio as a &#39;pipe&#39;.
Definition: audio.hpp:147
virtual State Update()=0
Performs an update cycle on this Audio.
The Audio has been stopped, or not yet played.
std::unique_ptr< AudioSink > sink
The sink to which audio data is sent.
Definition: audio.hpp:173
An audio item.
Definition: audio.hpp:35