Add distortion effect
This commit is contained in:
parent
87e865d025
commit
6f6add1cab
2 changed files with 82 additions and 0 deletions
17
libs/audio/include/psemek/audio/effect/distortion.hpp
Normal file
17
libs/audio/include/psemek/audio/effect/distortion.hpp
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <psemek/audio/stream.hpp>
|
||||||
|
|
||||||
|
namespace psemek::audio
|
||||||
|
{
|
||||||
|
|
||||||
|
struct distortion_control
|
||||||
|
: stream
|
||||||
|
{
|
||||||
|
virtual float strength() const = 0;
|
||||||
|
virtual float strength(float value) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
stream_ptr distortion(stream_ptr stream, float strength = 100.f);
|
||||||
|
|
||||||
|
}
|
||||||
65
libs/audio/source/effect/distortion.cpp
Normal file
65
libs/audio/source/effect/distortion.cpp
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
#include <psemek/audio/effect/distortion.hpp>
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
namespace psemek::audio
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
struct distortion_impl
|
||||||
|
: distortion_control
|
||||||
|
{
|
||||||
|
distortion_impl(stream_ptr stream, float strength)
|
||||||
|
: stream_(std::move(stream))
|
||||||
|
, strength_(strength)
|
||||||
|
{}
|
||||||
|
|
||||||
|
float strength() const override
|
||||||
|
{
|
||||||
|
return strength_.load();
|
||||||
|
}
|
||||||
|
|
||||||
|
float strength(float value) override
|
||||||
|
{
|
||||||
|
return strength_.exchange(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::size_t> length() const override
|
||||||
|
{
|
||||||
|
return stream_->length();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t read(float * data, std::size_t sample_count) override
|
||||||
|
{
|
||||||
|
auto result = stream_->read(data, sample_count);
|
||||||
|
float strength = strength_.load();
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < result; ++i)
|
||||||
|
{
|
||||||
|
data[i] = std::tanh(strength * data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t played() const override
|
||||||
|
{
|
||||||
|
return stream_->played();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
stream_ptr stream_;
|
||||||
|
std::atomic<float> strength_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
stream_ptr distortion(stream_ptr stream, float strength)
|
||||||
|
{
|
||||||
|
return std::make_shared<distortion_impl>(std::move(stream), strength);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue