Rename volume attack -> smoothness

This commit is contained in:
Nikita Lisitsa 2022-10-06 12:41:31 +03:00
parent 9b3afc9865
commit bc1bbcb306
6 changed files with 53 additions and 53 deletions

View file

@ -1,30 +0,0 @@
#pragma once
#include <psemek/audio/constants.hpp>
#include <cmath>
namespace psemek::audio
{
inline float attack_to_multiplier(float attack)
{
if (attack == 0.f)
return 1.f;
return - std::expm1(- inv_frequency / attack);
}
inline float multiplier_to_attack(float multiplier)
{
if (multiplier == 1.f)
return 0.f;
return - inv_frequency / std::log1p(- multiplier);
}
inline void attack_update(float & value, float target_value, float multiplier)
{
value += (target_value - value) * multiplier;
}
}

View file

@ -11,10 +11,10 @@ namespace psemek::audio
virtual float gain() const = 0;
virtual float gain(float value) = 0;
virtual float attack() const = 0;
virtual float attack(float value) = 0;
virtual float smoothness() const = 0;
virtual float smoothness(float value) = 0;
};
std::shared_ptr<volume_control> volume(stream_ptr stream, float gain = 1.f, float attack = 0.f);
std::shared_ptr<volume_control> volume(stream_ptr stream, float gain = 1.f, float smoothness = 0.f);
}

View file

@ -9,19 +9,19 @@ namespace psemek::audio
struct volume_base
{
volume_base(float gain, float attack);
volume_base(float gain, float smoothness);
float gain() const { return gain_.load(); }
float gain(float value) { return gain_.exchange(value); }
float attack() const;
float attack(float value);
float smoothness() const;
float smoothness(float value);
void apply(float * data, std::size_t sample_count);
private:
std::atomic<float> gain_;
std::atomic<float> attack_multiplier_;
std::atomic<float> smoothness_multiplier_;
float real_gain_;
float last_sample_src_[2];
float last_sample_tgt_[2];

View file

@ -0,0 +1,30 @@
#pragma once
#include <psemek/audio/constants.hpp>
#include <cmath>
namespace psemek::audio
{
inline float smoothness_to_multiplier(float smoothness)
{
if (smoothness == 0.f)
return 1.f;
return - std::expm1(- inv_frequency / smoothness);
}
inline float multiplier_to_smoothness(float multiplier)
{
if (multiplier == 1.f)
return 0.f;
return - inv_frequency / std::log1p(- multiplier);
}
inline void smooth_update(float & value, float target_value, float smoothness_multiplier)
{
value += (target_value - value) * smoothness_multiplier;
}
}

View file

@ -10,16 +10,16 @@ namespace psemek::audio
struct volume_control_impl
: volume_control
{
volume_control_impl(stream_ptr stream, float gain, float attack)
: base_(gain, attack)
volume_control_impl(stream_ptr stream, float gain, float smoothness)
: base_(gain, smoothness)
, stream_(std::move(stream))
{}
float gain() const override { return base_.gain(); }
float gain(float value) override { return base_.gain(value); }
float attack() const override { return base_.attack(); }
float attack(float value) override { return base_.attack(value); }
float smoothness() const override { return base_.smoothness(); }
float smoothness(float value) override { return base_.smoothness(value); }
std::size_t read(float * data, std::size_t sample_count) override
{
@ -35,9 +35,9 @@ namespace psemek::audio
}
std::shared_ptr<volume_control> volume(stream_ptr stream, float gain, float attack)
std::shared_ptr<volume_control> volume(stream_ptr stream, float gain, float smoothness)
{
return std::make_shared<volume_control_impl>(std::move(stream), gain, attack);
return std::make_shared<volume_control_impl>(std::move(stream), gain, smoothness);
}
}

View file

@ -1,35 +1,35 @@
#include <psemek/audio/effect/volume_base.hpp>
#include <psemek/audio/constants.hpp>
#include <psemek/audio/attack.hpp>
#include <psemek/audio/smooth.hpp>
#include <cmath>
namespace psemek::audio
{
volume_base::volume_base(float gain, float attack)
volume_base::volume_base(float gain, float smoothness)
: gain_{gain}
, attack_multiplier_{attack_to_multiplier(attack)}
, smoothness_multiplier_{smoothness_to_multiplier(smoothness)}
, real_gain_{gain}
, last_sample_src_{0.f, 0.f}
, last_sample_tgt_{0.f, 0.f}
{}
float volume_base::attack() const
float volume_base::smoothness() const
{
return multiplier_to_attack(attack_multiplier_.load());
return multiplier_to_smoothness(smoothness_multiplier_.load());
}
float volume_base::attack(float value)
float volume_base::smoothness(float value)
{
auto old = attack_multiplier_.exchange(attack_to_multiplier(value));
return multiplier_to_attack(old);
auto old = smoothness_multiplier_.exchange(smoothness_to_multiplier(value));
return multiplier_to_smoothness(old);
}
void volume_base::apply(float * data, std::size_t sample_count)
{
float gain = gain_.load();
float attack_multiplier = attack_multiplier_.load();
float smoothness_multiplier = smoothness_multiplier_.load();
auto apply_impl = [this](float & target, float & last_src, float & last_tgt)
{
@ -45,7 +45,7 @@ namespace psemek::audio
apply_impl(*p++, last_sample_src_[0], last_sample_tgt_[0]);
apply_impl(*p++, last_sample_src_[1], last_sample_tgt_[1]);
attack_update(real_gain_, gain, attack_multiplier);
smooth_update(real_gain_, gain, smoothness_multiplier);
}
}