URY playd
C++ minimalist audio player
audio_source.cpp
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 #include <cstdint>
11 #include <string>
12 
13 #include "audio_source.hpp"
14 #include "sample_formats.hpp"
15 
16 AudioSource::AudioSource(const std::string &path) : path(path)
17 {
18 }
19 
21 {
22  auto sf = static_cast<uint8_t>(this->OutputSampleFormat());
23  return SAMPLE_FORMAT_BPS[sf] * this->ChannelCount();
24 }
25 
26 const std::string &AudioSource::Path() const
27 {
28  return this->path;
29 }
30 
31 std::uint64_t AudioSource::SamplesFromMicros(std::uint64_t micros) const
32 {
33  // The sample rate is expressed in terms of samples per second, so we
34  // need to convert the position to seconds then multiply by the rate.
35  // We do things in a slightly peculiar order to minimise rounding.
36 
37  return (micros * this->SampleRate()) / 1000000;
38 }
39 
40 std::uint64_t AudioSource::MicrosFromSamples(std::uint64_t samples) const
41 {
42  // This is basically SamplesFromMicros but backwards.
43 
44  return (samples * 1000000) / this->SampleRate();
45 }
Declaration of the AudioSource class.
std::string path
The file-path of this AudioSource&#39;s audio file.
virtual const std::string & Path() const
Gets the file-path of this audio source&#39;s audio file.
virtual std::uint8_t ChannelCount() const =0
Returns the channel count.
virtual std::uint64_t SamplesFromMicros(std::uint64_t micros) const
Converts a position in microseconds to an elapsed sample count.
virtual SampleFormat OutputSampleFormat() const =0
Returns the output sample format.
std::uint64_t MicrosFromSamples(std::uint64_t samples) const
Converts an elapsed sample count to a position in microseconds.
const std::array< std::size_t, SAMPLE_FORMAT_COUNT > SAMPLE_FORMAT_BPS
Map from SampleFormats to bytes-per-mono-sample.
AudioSource(const std::string &path)
Constructs an AudioSource.
virtual size_t BytesPerSample() const
Returns the number of bytes for each sample this decoder outputs.
virtual std::uint32_t SampleRate() const =0
Returns the sample rate.
The SampleFormat enumeration and related declarations.