Support retrieving length of a stream

This commit is contained in:
Nikita Lisitsa 2022-10-06 13:05:42 +03:00
parent bc1bbcb306
commit 62af785c08
9 changed files with 54 additions and 0 deletions

View file

@ -2,13 +2,18 @@
#include <cstddef>
#include <memory>
#include <optional>
namespace psemek::audio
{
struct stream
{
// The length of the stream in samples, or nullopt if the stream is infinite
virtual std::optional<std::size_t> length() const = 0;
// Return value less than sample count means end of stream
// Must be called from mixing thread
virtual std::size_t read(float * data, std::size_t sample_count) = 0;
virtual ~stream() {}

View file

@ -14,6 +14,11 @@ namespace psemek::audio
: func_(std::move(func))
{}
std::optional<std::size_t> length() const override
{
return std::nullopt;
}
std::size_t read(float * data, std::size_t sample_count) override
{
auto end = data + sample_count;

View file

@ -27,6 +27,11 @@ namespace psemek::audio
: common_(std::move(common))
{}
std::optional<std::size_t> length() const override
{
return common_->stream->length();
}
std::size_t read(float * data, std::size_t sample_count) override
{
if (counter_ == common_->counter)

View file

@ -18,6 +18,11 @@ namespace psemek::audio
, start_samples_(2 * std::round(start * frequency))
{}
std::optional<std::size_t> length() const override
{
return stream_->length();
}
std::size_t read(float * data, std::size_t sample_count) override
{
auto const result = stream_->read(data, sample_count);

View file

@ -18,6 +18,12 @@ namespace psemek::audio
, start_samples_(2 * std::round(start * frequency))
{}
std::optional<std::size_t> length() const override
{
// TODO: compute fade_out length using the number of samples already played
return stream_->length();
}
std::size_t read(float * data, std::size_t sample_count) override
{
if (current_ >= length_)

View file

@ -21,6 +21,11 @@ namespace psemek::audio
float smoothness() const override { return base_.smoothness(); }
float smoothness(float value) override { return base_.smoothness(value); }
std::optional<std::size_t> length() const override
{
return stream_->length();
}
std::size_t read(float * data, std::size_t sample_count) override
{
auto result = stream_->read(data, sample_count);

View file

@ -18,6 +18,11 @@ namespace psemek::audio
std::size_t read(float * data, std::size_t sample_count) override;
std::optional<std::size_t> length() const override
{
return std::nullopt;
}
private:
std::vector<channel_ptr> channels_;
std::vector<channel_ptr> alive_channels_;

View file

@ -16,6 +16,19 @@ namespace psemek::audio
, right_(std::move(right))
{}
std::optional<std::size_t> length() const override
{
auto left = left_->length();
auto right = right_->length();
if (left && right)
return std::min(*left, *right);
else if (left)
return left;
else
return right;
}
std::size_t read(float * data, std::size_t sample_count) override
{
if (buffer_.size() < sample_count)

View file

@ -14,6 +14,11 @@ namespace psemek::audio
std::fill(data, data + sample_count, 0.f);
return sample_count;
}
std::optional<std::size_t> length() const override
{
return std::nullopt;
}
};
}