Add fade in & fade out effects

This commit is contained in:
Nikita Lisitsa 2022-10-05 22:30:46 +03:00
parent 21b2028122
commit bb31fe79c9
4 changed files with 144 additions and 0 deletions

View file

@ -0,0 +1,10 @@
#pragma once
#include <psemek/audio/stream.hpp>
namespace psemek::audio
{
stream_ptr fade_in(stream_ptr stream, float length, float start = 0.f);
}

View file

@ -0,0 +1,10 @@
#pragma once
#include <psemek/audio/stream.hpp>
namespace psemek::audio
{
stream_ptr fade_out(stream_ptr stream, float length, float start = 0.f);
}

View file

@ -0,0 +1,61 @@
#include <psemek/audio/effect/fade_in.hpp>
#include <psemek/audio/constants.hpp>
#include <cmath>
namespace psemek::audio
{
namespace
{
struct fade_in_impl
: stream
{
fade_in_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
{
auto const result = stream_->read(data, sample_count);
std::fill(data, data + std::min(result, start_samples_), 0.f);
if (result <= start_samples_)
{
start_samples_ -= result;
}
else
{
for (std::size_t i = start_samples_; i < result; i += 2)
{
float m = static_cast<float>(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_in(stream_ptr stream, float length, float start)
{
return std::make_shared<fade_in_impl>(std::move(stream), length, start);
}
}

View file

@ -0,0 +1,63 @@
#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);
}
}