63 lines
1.2 KiB
C++
63 lines
1.2 KiB
C++
#include <psemek/audio/effect/fade_out.hpp>
|
|
#include <psemek/audio/constants.hpp>
|
|
|
|
#include <cmath>
|
|
|
|
namespace psemek::audio
|
|
{
|
|
|
|
namespace
|
|
{
|
|
|
|
struct fade_out_impl
|
|
: stream
|
|
{
|
|
fade_out_impl(stream_ptr stream, float length, float start)
|
|
: stream_(std::move(stream))
|
|
, length_(2 * std::round(length * frequency))
|
|
, start_samples_(2 * std::round(start * frequency))
|
|
{}
|
|
|
|
std::size_t read(float * data, std::size_t sample_count) override
|
|
{
|
|
if (current_ >= length_)
|
|
return 0;
|
|
|
|
auto result = stream_->read(data, sample_count);
|
|
|
|
if (result <= start_samples_)
|
|
{
|
|
start_samples_ -= result;
|
|
}
|
|
else
|
|
{
|
|
for (std::size_t i = start_samples_; i < result; i += 2)
|
|
{
|
|
float m = static_cast<float>(length_ - std::min(current_, length_)) / length_;
|
|
|
|
data[i + 0] *= m;
|
|
data[i + 1] *= m;
|
|
|
|
current_ += 2;
|
|
}
|
|
start_samples_ = 0;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private:
|
|
stream_ptr stream_;
|
|
std::size_t length_;
|
|
std::size_t current_ = 0;
|
|
std::size_t start_samples_;
|
|
};
|
|
|
|
}
|
|
|
|
stream_ptr fade_out(stream_ptr stream, float length, float start)
|
|
{
|
|
return std::make_shared<fade_out_impl>(std::move(stream), length, start);
|
|
}
|
|
|
|
}
|