Implement audio::concat effect
This commit is contained in:
parent
098c3e84b1
commit
3c22074f8d
2 changed files with 82 additions and 0 deletions
12
libs/audio/include/psemek/audio/effect/concat.hpp
Normal file
12
libs/audio/include/psemek/audio/effect/concat.hpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/audio/stream.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace psemek::audio
|
||||
{
|
||||
|
||||
stream_ptr concat(std::vector<stream_ptr> streams);
|
||||
|
||||
}
|
||||
70
libs/audio/source/effect/concat.cpp
Normal file
70
libs/audio/source/effect/concat.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#include <psemek/audio/effect/concat.hpp>
|
||||
|
||||
#include <atomic>
|
||||
|
||||
namespace psemek::audio
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct concat_impl
|
||||
: stream
|
||||
{
|
||||
concat_impl(std::vector<stream_ptr> streams)
|
||||
: streams_(std::move(streams))
|
||||
{
|
||||
length_ = 0;
|
||||
for (auto const & stream : streams_)
|
||||
{
|
||||
if (auto length = stream->length())
|
||||
*length_ += *length;
|
||||
else
|
||||
{
|
||||
length_ = std::nullopt;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::size_t> length() const override
|
||||
{
|
||||
return length_;
|
||||
}
|
||||
|
||||
std::size_t read(float * data, std::size_t sample_count) override
|
||||
{
|
||||
std::size_t count = 0;
|
||||
|
||||
for (; index_ != streams_.size(); ++index_)
|
||||
{
|
||||
count += streams_[index_]->read(data + count, sample_count - count);
|
||||
if (count == sample_count)
|
||||
break;
|
||||
}
|
||||
|
||||
played_.fetch_add(count);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
std::size_t played() const override
|
||||
{
|
||||
return played_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<stream_ptr> streams_;
|
||||
std::optional<std::size_t> length_;
|
||||
std::atomic<std::size_t> played_{0};
|
||||
std::size_t index_{0};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
stream_ptr concat(std::vector<stream_ptr> streams)
|
||||
{
|
||||
return std::make_shared<concat_impl>(std::move(streams));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue