Support non-owning audio::recorder
This commit is contained in:
parent
ce5ccfde70
commit
5b0834f097
2 changed files with 27 additions and 10 deletions
|
|
@ -21,5 +21,6 @@ namespace psemek::audio
|
|||
|
||||
std::shared_ptr<recorder> make_recorder(stream_ptr stream);
|
||||
std::shared_ptr<recorder> make_recorder(std::vector<float> samples);
|
||||
std::shared_ptr<recorder> make_recorder(util::span<float const> samples);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,13 +16,17 @@ namespace psemek::audio
|
|||
{}
|
||||
|
||||
recorder_impl(std::vector<float> samples)
|
||||
: buffer_(std::move(samples))
|
||||
, length_(buffer_.size())
|
||||
: storage_(std::move(samples))
|
||||
, samples_(storage_)
|
||||
{}
|
||||
|
||||
recorder_impl(util::span<float const> samples)
|
||||
: samples_(samples)
|
||||
{}
|
||||
|
||||
std::optional<std::size_t> length() const override
|
||||
{
|
||||
return stream_ ? stream_->length() : buffer_.size();
|
||||
return stream_ ? stream_->length() : storage_.size();
|
||||
}
|
||||
|
||||
std::size_t request(std::size_t samples) override
|
||||
|
|
@ -30,23 +34,26 @@ namespace psemek::audio
|
|||
if (!stream_)
|
||||
return 0;
|
||||
|
||||
if (buffer_.size() < length_ + samples)
|
||||
buffer_.resize(std::max<std::size_t>(samples, buffer_.size() * 2));
|
||||
if (storage_.size() < samples_.size() + samples)
|
||||
{
|
||||
storage_.resize(std::max<std::size_t>(samples, storage_.size() * 2));
|
||||
samples_ = {storage_.data(), storage_.data() + samples_.size()};
|
||||
}
|
||||
|
||||
auto result = stream_->read(buffer_.data() + length_, samples);
|
||||
length_ += result;
|
||||
auto result = stream_->read(storage_.data() + samples_.size(), samples);
|
||||
samples_ = {storage_.data(), storage_.data() + samples_.size() + result};
|
||||
return result;
|
||||
}
|
||||
|
||||
util::span<float const> buffer() const override
|
||||
{
|
||||
return {buffer_.data(), buffer_.data() + length_};
|
||||
return samples_;
|
||||
}
|
||||
|
||||
private:
|
||||
stream_ptr stream_;
|
||||
std::vector<float> buffer_;
|
||||
std::size_t length_{0};
|
||||
std::vector<float> storage_;
|
||||
util::span<float const> samples_;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -58,7 +65,16 @@ namespace psemek::audio
|
|||
|
||||
std::shared_ptr<recorder> make_recorder(std::vector<float> samples)
|
||||
{
|
||||
if ((samples.size() % 2) != 0)
|
||||
throw std::runtime_error("bad sample count");
|
||||
return std::make_shared<recorder_impl>(std::move(samples));
|
||||
}
|
||||
|
||||
std::shared_ptr<recorder> make_recorder(util::span<float const> samples)
|
||||
{
|
||||
if ((samples.size() % 2) != 0)
|
||||
throw std::runtime_error("bad sample count");
|
||||
return std::make_shared<recorder_impl>(samples);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue