From ab645ccfd9d3222a9136a424c5357f3147a9f2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Thu, 2 Jan 2025 15:43:50 -0500 Subject: [PATCH] analysis: implement a sample-based envelope follower from kferg.dev --- .../Analysis/Envelope.hpp | 45 +++++++++++++++++++ .../score_plugin_analysis.cpp | 1 + 2 files changed, 46 insertions(+) diff --git a/src/plugins/score-plugin-analysis/Analysis/Envelope.hpp b/src/plugins/score-plugin-analysis/Analysis/Envelope.hpp index 74057c0fca..ab983037e5 100644 --- a/src/plugins/score-plugin-analysis/Analysis/Envelope.hpp +++ b/src/plugins/score-plugin-analysis/Analysis/Envelope.hpp @@ -63,4 +63,49 @@ struct Peak : Analysis::GistState inputs.audio, inputs.gain, inputs.gate, outputs.result, frames); } }; + +struct EnvelopeFollower +{ + halp_meta(name, "Envelope Follower (audio)") + halp_meta(c_name, "EnvelopeFollowerAudio") + halp_meta(category, "Analysis/Envelope") + halp_meta(author, "Kevin Ferguson") + halp_meta(manual_url, "https://ossia.io/score-docs/processes/analysis.html#envelope") + halp_meta( + description, + "Sample-level envelope Follower\n" + "(https://kferg.dev/posts/2020/audio-reactive-programming-envelope-followers/)") + halp_meta(uuid, "0a262706-1216-44f4-85ca-52f1f25785bc") + + struct inputs + { + halp::knob_f32<"Millis (up)", halp::range{0., 1000., 50.}> a; + halp::knob_f32<"Millis (down)", halp::range{0., 1000., 15.}> b; + }; + struct outputs + { + }; + void prepare(halp::setup s) { rate = s.rate; } + double rate{48000.}; + double y{}; + + double operator()(double x, struct inputs inputs, outputs) noexcept + { + using namespace std; + const auto abs_x = abs(x); + if(rate > 0) + { + const auto a = exp(log(0.5) / (rate * (inputs.a.value / 1000.0))); + const auto b = exp(log(0.5) / (rate * (inputs.b.value / 1000.0))); + + const auto coeff = (abs_x > y) ? a : b; + y = coeff * y + (1. - coeff) * abs_x; + } + else + { + y = 0.; + } + return y; + } +}; } diff --git a/src/plugins/score-plugin-analysis/score_plugin_analysis.cpp b/src/plugins/score-plugin-analysis/score_plugin_analysis.cpp index 113401431e..6ec8c487a9 100644 --- a/src/plugins/score-plugin-analysis/score_plugin_analysis.cpp +++ b/src/plugins/score-plugin-analysis/score_plugin_analysis.cpp @@ -32,6 +32,7 @@ std::vector score_plugin_analysis::factories( oscr::instantiate_fx(fx, ctx, key); oscr::instantiate_fx(fx, ctx, key); oscr::instantiate_fx(fx, ctx, key); + oscr::instantiate_fx(fx, ctx, key); oscr::instantiate_fx(fx, ctx, key); oscr::instantiate_fx(fx, ctx, key); oscr::instantiate_fx(fx, ctx, key);