Add truncate effect
This commit is contained in:
parent
3e9814aee4
commit
9d58904a66
2 changed files with 58 additions and 0 deletions
11
libs/audio/include/psemek/audio/effect/truncate.hpp
Normal file
11
libs/audio/include/psemek/audio/effect/truncate.hpp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/audio/stream.hpp>
|
||||
#include <psemek/audio/duration.hpp>
|
||||
|
||||
namespace psemek::audio
|
||||
{
|
||||
|
||||
stream_ptr truncate(stream_ptr stream, duration length);
|
||||
|
||||
}
|
||||
47
libs/audio/source/effect/truncate.cpp
Normal file
47
libs/audio/source/effect/truncate.cpp
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#include <psemek/audio/effect/truncate.hpp>
|
||||
|
||||
namespace psemek::audio
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct truncate_impl
|
||||
: stream
|
||||
{
|
||||
truncate_impl(stream_ptr stream, duration length)
|
||||
: stream_(std::move(stream))
|
||||
, length_(length)
|
||||
{}
|
||||
|
||||
std::optional<std::size_t> length() const override
|
||||
{
|
||||
return length_.samples();
|
||||
}
|
||||
|
||||
std::size_t read(float * data, std::size_t sample_count) override
|
||||
{
|
||||
auto played = stream_->played();
|
||||
auto result = std::min<std::size_t>(length_.samples() - played, sample_count);
|
||||
result = stream_->read(data, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::size_t played() const override
|
||||
{
|
||||
return stream_->played();
|
||||
}
|
||||
|
||||
private:
|
||||
stream_ptr stream_;
|
||||
duration length_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
stream_ptr truncate(stream_ptr stream, duration length)
|
||||
{
|
||||
return std::make_shared<truncate_impl>(std::move(stream), length);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue