21 #include "../../errors.hpp" 22 #include "../../messages.h" 23 #include "../sample_formats.hpp" 24 #include "../audio_source.hpp" 27 SndfileAudioSource::SndfileAudioSource(
const std::string &path)
30 this->info.format = 0;
32 this->file = sf_open(path.c_str(), SFM_READ, &this->info);
33 if (this->file ==
nullptr) {
34 throw FileError(
"sndfile: can't open " + path +
": " +
35 sf_strerror(
nullptr));
41 assert(0 < this->info.channels);
43 this->buffer.insert(this->buffer.begin(), 4096 * this->info.channels, 0);
46 SndfileAudioSource::~SndfileAudioSource()
48 if (this->file !=
nullptr) sf_close(this->file);
51 std::uint8_t SndfileAudioSource::ChannelCount()
const 53 assert(0 < this->info.channels);
54 return static_cast<std::uint8_t
>(this->info.channels);
57 std::uint32_t SndfileAudioSource::SampleRate()
const 59 assert(0 < this->info.samplerate);
63 assert(this->info.samplerate <= INT32_MAX);
64 return static_cast<std::uint32_t
>(this->info.samplerate);
67 std::uint64_t SndfileAudioSource::Seek(std::uint64_t in_samples)
70 auto clen =
static_cast<unsigned long>(this->info.frames);
71 if (clen < in_samples) {
72 Debug() <<
"sndfile: seek at" << in_samples <<
"past EOF at" 77 auto out_samples = sf_seek(this->file, in_samples, SEEK_SET);
78 if (out_samples == -1) {
79 Debug() <<
"sndfile: seek failed" << std::endl;
86 SndfileAudioSource::DecodeResult SndfileAudioSource::Decode()
88 auto read = sf_read_int(this->file, &*this->buffer.begin(),
93 return std::make_pair(DecodeState::END_OF_FILE, DecodeVector());
106 uint8_t *begin =
reinterpret_cast<uint8_t *
>(&*this->buffer.begin());
109 uint8_t *end = begin + (read * 4);
111 return std::make_pair(DecodeState::DECODING, DecodeVector(begin, end));
114 SampleFormat SndfileAudioSource::OutputSampleFormat()
const 119 static_assert(
sizeof(
int) == 4,
120 "sndfile outputs int, which we need to be 4 bytes");
An object responsible for decoding an audio file.
An Error signifying that playd can't read a file.
const std::string MSG_SEEK_FAIL
Message shown when an attempt to seek fails.
An Error signifying that playd can't seek in a file.
Class for telling the human what playd is doing.
Declaration of the SndfileAudioSource class.