11 return std::isfinite(response.quiet_seconds) && response.quiet_seconds > 0.0
12 && std::isfinite(response.loud_seconds) && response.loud_seconds > 0.0
13 && std::isfinite(response.reference_rms) && response.reference_rms > 0.0
14 && std::isfinite(response.smoothing_seconds) && response.smoothing_seconds >= 0.0;
23 "ModalDampingOperator has no ModalNetwork");
27 m_modes.reserve(network->get_node_count());
28 for (
size_t index = 0; index < network->get_node_count(); ++index) {
29 const double decay = network->get_mode_decay_time(index).value_or(
m_default_response.quiet_seconds);
30 const double initial = std::isfinite(decay) && decay > 0.0
36 .decay_seconds = initial });
47 "ModalDampingOperator ignored mode {}, it has {} modes", index,
m_modes.size());
52 const auto field = [&](
const Control& control,
double Response::* member, std::string_view name) {
58 candidate.*member = control.
value();
59 if (!
valid(candidate)) {
61 "ModalDampingOperator ignored invalid value for {}", name);
64 mode.response = candidate;
76 candidate.*member = value;
77 if (!
valid(candidate)) {
83 mode.response.*member = value;
90 const auto field = [
this](
Control& control,
double Response::* member, std::string_view name) {
91 const auto value = control.
pending();
94 "ModalDampingOperator ignored invalid value for {}", name);
118 if (!network || network->get_sample_rate() == 0 || frames <= 0.0F) {
122 const double dt =
static_cast<double>(frames) / network->get_sample_rate();
123 const size_t count = std::min(
m_modes.size(), network->get_node_count());
125 for (
size_t index = 0; index < count; ++index) {
126 const auto buffer = network->get_node_audio_buffer(index);
127 double sum_squares = 0.0;
130 for (
const double sample : *buffer) {
131 if (std::isfinite(sample)) {
132 sum_squares += sample * sample;
137 const double rms = buffer && !buffer->empty()
138 ? std::sqrt(sum_squares /
static_cast<double>(buffer->size()))
142 const auto& response = mode.response;
143 const double amount = std::clamp(rms / response.reference_rms, 0.0, 1.0);
144 const double target = std::lerp(response.quiet_seconds, response.loud_seconds, amount);
147 mode.decay_seconds, target, response.smoothing_seconds, dt);
148 (void)network->set_mode_decay_time(index, mode.decay_seconds);
156 "ModalDampingOperator ignored unknown parameter {}", name);
165 if (name ==
"mode_count") {
166 return static_cast<double>(
m_modes.size());
#define MF_WARN(comp, ctx,...)
static std::optional< double > read_named(const C &controls, std::string_view name)
bool has_value() const noexcept
True once a value was given.
double value() const noexcept
The value as a double.
std::optional< T > pending() noexcept
The value to apply now as type T, or nullopt when nothing is due.
void observe(T actual) noexcept
Shows the value the owner really has, unless a source is feeding it.
bool sourced() const noexcept
True when a node or a network feeds this control.
One named control of a network or operator: empty, a value, or a mapped source.
void process(float frames) override
Process for one batch cycle.
Response m_default_response
std::vector< ModeState > m_modes
ModalDampingControls & m_controls
std::weak_ptr< ModalNetwork > m_network
std::optional< double > query_state(std::string_view name) const override
The control of that name as the operator holds it, or "mode_count".
static bool valid(const Response &response) noexcept
bool apply_field(double Response::*member, double value)
void set_parameter(std::string_view name, double value) override
Sets the control of that name, so "loud_seconds" is the same as set({ .loud_seconds = ....
ModalDampingOperator(const std::shared_ptr< ModalNetwork > &network)
void read_controls(ModalDampingControls &controls) const
Fills the controls whose value the operator holds outside the struct.
void set_mode(size_t index, const ModalDampingControls &controls)
Change one mode's response from the numbers controls holds.
C & declare_controls()
Create the operator's controls struct and keep it alive.
void refresh_controls()
Read node sources into their controls.
bool set_named_control(std::string_view name, double value)
Sets the control called name, the way set() would.
Self::Controls get(this const Self &self)
The controls with the values the operator holds now.
@ Configuration
Configuration and parameter updates.
@ Nodes
DSP Generator and Filter Nodes, graph pipeline, node management.
T damp(T current, T target, T smoothing, T dt) noexcept
Exponential smoothing: move current toward target at rate smoothing over time step dt.
Control smoothing_seconds
Controls of a ModalDampingOperator, each a number or a mapped node.