URY playd
C++ minimalist audio player
player.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_PLAYER_HPP
11 #define PLAYD_PLAYER_HPP
12 
13 #include <cstdint>
14 #include <functional>
15 #include <map>
16 #include <memory>
17 #include <string>
18 #include <utility>
19 #include <vector>
20 
21 #include "audio/audio_sink.hpp"
22 #include "audio/audio_source.hpp"
23 #include "audio/audio.hpp"
24 #include "response.hpp"
25 
31 class Player
32 {
33 public:
35  using SinkFn =
36  std::function<std::unique_ptr<AudioSink>(const AudioSource &, int)>;
37 
39  using SourceFn =
40  std::function<std::unique_ptr<AudioSource>(const std::string &)>;
41 
50  std::map<std::string, SourceFn> sources);
51 
53  Player(const Player &) = delete;
54 
56  Player &operator=(const Player &) = delete;
57 
64  void SetIo(ResponseSink &io);
65 
71  bool Update();
72 
73  //
74  // Commands
75  //
76 
85  Response SetPlaying(const std::string &tag, bool playing);
86 
96  Response Dump(size_t id, const std::string &tag) const;
97 
104  Response Eject(const std::string &tag);
105 
112  Response End(const std::string &tag);
113 
121  Response Load(const std::string &tag, const std::string &path);
122 
130  Response Pos(const std::string &tag, const std::string &pos_str);
131 
138  Response Quit(const std::string &tag);
139 
140 private:
141  int device_id;
143  std::map<std::string, SourceFn> sources;
144  std::unique_ptr<Audio> file;
145  bool dead;
146  const ResponseSink *io;
147  std::uint64_t last_pos;
148 
160  static std::uint64_t PosParse(const std::string &pos_str);
161 
172  void PosRaw(const std::string &tag, std::uint64_t pos);
173 
184  void DumpState(size_t id, const std::string &tag) const;
185 
194  void Respond(int id, Response rs) const;
195 
209  bool CanAnnounceTime(std::uint64_t micros);
210 
211  //
212  // Audio subsystem
213  //
214 
220  std::unique_ptr<Audio> LoadRaw(const std::string &path) const;
221 
229  std::unique_ptr<AudioSource> LoadSource(const std::string &path) const;
230 };
231 
232 #endif // PLAYD_PLAYER_HPP
Declaration of the AudioSource class.
An object responsible for decoding an audio file.
bool Update()
Instructs the Player to perform a cycle of work.
Definition: player.cpp:41
static std::uint64_t PosParse(const std::string &pos_str)
Parses pos_str as a seek timestamp.
Definition: player.cpp:223
A response.
Definition: response.hpp:23
std::function< std::unique_ptr< AudioSink >(const AudioSource &, int)> SinkFn
Type for functions that construct sinks.
Definition: player.hpp:36
Response Load(const std::string &tag, const std::string &path)
Loads a file.
Definition: player.cpp:122
void PosRaw(const std::string &tag, std::uint64_t pos)
Performs an actual seek.
Definition: player.cpp:252
Declaration of the Audio class.
void Respond(int id, Response rs) const
Outputs a response, if there is a ResponseSink attached.
Definition: player.cpp:290
Response Quit(const std::string &tag)
Quits playd.
Definition: player.cpp:210
const ResponseSink * io
The sink for responses.
Definition: player.hpp:146
std::function< std::unique_ptr< AudioSource >(const std::string &)> SourceFn
Type for functions that construct sources.
Definition: player.hpp:40
bool CanAnnounceTime(std::uint64_t micros)
Determines whether we can broadcast a POS response.
Definition: player.cpp:295
Abstract class for anything that can be sent a response.
Definition: response.hpp:117
bool dead
Whether the Player is closing.
Definition: player.hpp:145
Response Eject(const std::string &tag)
Ejects the current loaded song, if any.
Definition: player.cpp:85
int device_id
The sink&#39;s device ID.
Definition: player.hpp:141
std::unique_ptr< AudioSource > LoadSource(const std::string &path) const
Loads a file, creating an AudioSource.
Definition: player.cpp:315
SinkFn sink
The sink create function.
Definition: player.hpp:142
std::map< std::string, SourceFn > sources
The file formats map.
Definition: player.hpp:143
Player & operator=(const Player &)=delete
Deleted copy-assignment constructor.
Player(int device_id, SinkFn sink, std::map< std::string, SourceFn > sources)
Constructs a Player.
Definition: player.cpp:25
Declaration of the AudioSink class.
Response Pos(const std::string &tag, const std::string &pos_str)
Seeks to a given position in the current file.
Definition: player.cpp:155
std::unique_ptr< Audio > LoadRaw(const std::string &path) const
Loads a file, creating an Audio for it.
Definition: player.cpp:306
void DumpState(size_t id, const std::string &tag) const
Emits a response for the current audio state to the sink.
Definition: player.cpp:265
std::uint64_t last_pos
The last-sent position.
Definition: player.hpp:147
Response Dump(size_t id, const std::string &tag) const
Dumps the current player state to the given ID.
Definition: player.cpp:65
Declaration of classes pertaining to responses to the client.
Response SetPlaying(const std::string &tag, bool playing)
Tells the audio file to start or stop playing.
Definition: player.cpp:188
std::unique_ptr< Audio > file
The loaded audio file.
Definition: player.hpp:144
Response End(const std::string &tag)
Ends a file, stopping and rewinding.
Definition: player.cpp:104
void SetIo(ResponseSink &io)
Sets the ResponseSink to which this Player shall send responses.
Definition: player.cpp:36
A Player contains a loaded audio file and a command API for manipulating it.
Definition: player.hpp:31