URY playd
C++ minimalist audio player
audio_sink.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_SINK_HPP
11 #define PLAYD_AUDIO_SINK_HPP
12 
13 #include <array>
14 #include <cstdint>
15 #include <memory>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 #include "SDL.h"
21 
22 #include "audio.hpp"
23 #include "audio_source.hpp"
24 #include "ringbuffer.hpp"
25 #include "sample_formats.hpp"
26 
28 class AudioSink
29 {
30 public:
32  using TransferIterator = AudioSource::DecodeVector::iterator;
33 
35  virtual ~AudioSink() = default;
36 
42  virtual void Start() = 0;
43 
49  virtual void Stop() = 0;
50 
56  virtual Audio::State State();
57 
64  virtual std::uint64_t Position() = 0;
65 
73  virtual void SetPosition(std::uint64_t samples) = 0;
74 
81  virtual void SourceOut() = 0;
82 
97  virtual void Transfer(TransferIterator &start,
98  const TransferIterator &end) = 0;
99 };
100 
108 class SdlAudioSink : public AudioSink
109 {
110 public:
116  SdlAudioSink(const AudioSource &source, int device_id);
117 
119  ~SdlAudioSink() override;
120 
121  void Start() override;
122  void Stop() override;
123  Audio::State State() override;
124  std::uint64_t Position() override;
125  void SetPosition(std::uint64_t samples) override;
126  void SourceOut() override;
127  void Transfer(TransferIterator &start,
128  const TransferIterator &end) override;
129 
137  void Callback(std::uint8_t *out, int nbytes);
138 
144  static std::vector<std::pair<int, std::string>> GetDevicesInfo();
145 
151  static bool IsOutputDevice(int id);
152 
154  static void InitLibrary();
155 
157  static void CleanupLibrary();
158 
159 private:
161  SDL_AudioDeviceID device;
162 
165  static const size_t RINGBUF_POWER;
166 
168  static const std::array<SDL_AudioFormat, SAMPLE_FORMAT_COUNT> FORMATS;
169 
172 
175 
177  std::uint64_t position_sample_count;
178 
181 
184 };
185 
186 #endif // PLAYD_AUDIO_SINK_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
An object responsible for decoding an audio file.
virtual std::uint64_t Position()=0
Gets the current played position in the song, in samples.
virtual void SetPosition(std::uint64_t samples)=0
Sets the current played position, given a position in samples.
An output stream for audio, using SDL.
Definition: audio_sink.hpp:108
Declaration of the Audio class.
AudioSource::DecodeVector::iterator TransferIterator
Type of iterators used in the Transfer() method.
Definition: audio_sink.hpp:32
RingBuffer ring_buf
The ring buffer used to transfer samples to the playing callback.
Definition: audio_sink.hpp:174
virtual void Transfer(TransferIterator &start, const TransferIterator &end)=0
Transfers a range of sample bytes into the AudioSink.
SDL_AudioDeviceID device
The SDL device to which we are outputting sound.
Definition: audio_sink.hpp:161
std::uint64_t position_sample_count
The current position, in samples.
Definition: audio_sink.hpp:177
virtual Audio::State State()
Gets this AudioSink&#39;s current state (playing/stopped/at end).
Definition: audio_sink.cpp:31
virtual void SourceOut()=0
Tells this AudioSink that the source has run out.
The RingBuffer class template.
bool source_out
Whether the source has run out of things to feed the sink.
Definition: audio_sink.hpp:180
static const std::array< SDL_AudioFormat, SAMPLE_FORMAT_COUNT > FORMATS
Mapping from SampleFormats to their equivalent SDL_AudioFormats.
Definition: audio_sink.hpp:168
virtual ~AudioSink()=default
Virtual, empty destructor for AudioSink.
virtual void Start()=0
Starts the audio stream.
Audio::State state
The decoder&#39;s current state.
Definition: audio_sink.hpp:183
size_t bytes_per_sample
Number of bytes in one sample.
Definition: audio_sink.hpp:171
The SampleFormat enumeration and related declarations.
A ring buffer.
Definition: ringbuffer.hpp:24
static const size_t RINGBUF_POWER
n, where 2^n is the capacity of the Audio ring buffer.
Definition: audio_sink.hpp:165
virtual void Stop()=0
Stops the audio stream.