Implement first-order feedback audio filter
This commit is contained in:
parent
9cbb9127b9
commit
9a800b8f7a
2 changed files with 50 additions and 1 deletions
|
|
@ -5,9 +5,12 @@
|
|||
namespace psemek::audio
|
||||
{
|
||||
|
||||
// Turns x[n] into a0*x[n] + a1*x[n-1]
|
||||
// Turns x[n] into y[n] = a0*x[n] + a1*x[n-1]
|
||||
stream_ptr feedforward_filter(stream_ptr stream, float a0, float a1);
|
||||
|
||||
// Turns x[n] into y[n] = a0*x[n] + b1*y[n-1]
|
||||
stream_ptr feedback_filter(stream_ptr stream, float a0, float b1);
|
||||
|
||||
stream_ptr low_pass_filter(stream_ptr stream);
|
||||
stream_ptr high_pass_filter(stream_ptr stream);
|
||||
|
||||
|
|
|
|||
|
|
@ -48,6 +48,47 @@ namespace psemek::audio
|
|||
float prev_[2]{0.f};
|
||||
};
|
||||
|
||||
struct feedback_filter_impl
|
||||
: stream
|
||||
{
|
||||
feedback_filter_impl(stream_ptr stream, float a0, float b1)
|
||||
: stream_(std::move(stream))
|
||||
, a0_(a0)
|
||||
, b1_(b1)
|
||||
{}
|
||||
|
||||
std::optional<std::size_t> length() const override
|
||||
{
|
||||
return stream_->length();
|
||||
}
|
||||
|
||||
std::size_t read(util::span<float> samples) override
|
||||
{
|
||||
std::size_t count = stream_->read(samples);
|
||||
|
||||
for (std::size_t i = 0; i < count; i += 2)
|
||||
{
|
||||
samples[i + 0] = prev_[0] * b1_ + samples[i + 0] * a0_;
|
||||
samples[i + 1] = prev_[1] * b1_ + samples[i + 1] * a0_;
|
||||
prev_[0] = samples[i + 0];
|
||||
prev_[1] = samples[i + 1];
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
std::size_t played() const override
|
||||
{
|
||||
return stream_->played();
|
||||
}
|
||||
|
||||
private:
|
||||
stream_ptr stream_;
|
||||
float a0_;
|
||||
float b1_;
|
||||
float prev_[2]{0.f};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
stream_ptr feedforward_filter(stream_ptr stream, float a0, float a1)
|
||||
|
|
@ -55,6 +96,11 @@ namespace psemek::audio
|
|||
return std::make_shared<feedforward_filter_impl>(std::move(stream), a0, a1);
|
||||
}
|
||||
|
||||
stream_ptr feedback_filter(stream_ptr stream, float a0, float b1)
|
||||
{
|
||||
return std::make_shared<feedback_filter_impl>(std::move(stream), a0, b1);
|
||||
}
|
||||
|
||||
stream_ptr low_pass_filter(stream_ptr stream)
|
||||
{
|
||||
return feedforward_filter(std::move(stream), 1.f, 1.f);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue