MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
IIR.cpp
Go to the documentation of this file.
1#include "IIR.hpp"
2
4
5IIR::IIR(const std::shared_ptr<Node>& input, const std::string& zindex_shifts)
6 : Filter(input, zindex_shifts)
7{
8}
9
10IIR::IIR(const std::shared_ptr<Node>& input, const std::vector<double>& a_coef, const std::vector<double>& b_coef)
11 : Filter(input, a_coef, b_coef)
12{
13}
14
15IIR::IIR(const std::vector<double>& a_coef, const std::vector<double>& b_coef)
16 : Filter(a_coef, b_coef)
17{
18}
19
20double IIR::process_sample(double input)
21{
22 if (is_bypass_enabled()) {
23 return input;
24 }
25
26 double processed_input = input;
27 if (m_input_node) {
28 atomic_inc_modulator_count(m_input_node->m_modulator_count, 1);
29 uint32_t state = m_input_node->m_state.load();
30 if (state & Utils::NodeState::PROCESSED) {
31 processed_input += m_input_node->get_last_output();
32 } else {
33 processed_input += m_input_node->process_sample(input);
35 }
36 }
37
39 build_input_history(processed_input);
40 } else {
41 update_inputs(processed_input);
42 }
43
44 double output = 0.;
45 const size_t num_feedforward = std::min(m_coef_b.size(), m_input_history.size());
46 for (size_t i = 0; i < num_feedforward; ++i) {
47 output += m_coef_b[i] * m_input_history[i];
48 }
49
50 const size_t num_feedback = std::min(m_coef_a.size(), m_output_history.size());
51 for (size_t i = 1; i < num_feedback; ++i) {
52 output -= m_coef_a[i] * m_output_history[i];
53 }
54
55 // output /= m_coef_a[0];
56
57 update_outputs(output);
58
60 notify_tick(output);
61
62 if (m_input_node) {
63 atomic_dec_modulator_count(m_input_node->m_modulator_count, 1);
65 }
66
67 return output * get_gain();
68}
69
80
91
92}
std::vector< double > m_saved_output_history
Definition Filter.hpp:637
void build_input_history(double current_sample)
Builds input history from external context or internal accumulation.
Definition Filter.cpp:68
std::vector< double > m_coef_b
Feedforward (numerator) coefficients.
Definition Filter.hpp:618
std::vector< double > m_output_history
Buffer storing previous output samples.
Definition Filter.hpp:593
virtual void update_outputs(double current_sample)
Updates the output history buffer with a new sample.
Definition Filter.cpp:92
bool is_bypass_enabled() const
Checks if bypass is currently enabled.
Definition Filter.hpp:300
std::shared_ptr< Node > m_input_node
The most recent sample value generated by this oscillator.
Definition Filter.hpp:569
std::vector< double > m_saved_input_history
Definition Filter.hpp:636
double get_gain() const
Gets the current gain value.
Definition Filter.hpp:284
std::vector< double > m_input_history
Buffer storing previous input samples.
Definition Filter.hpp:585
virtual void update_inputs(double current_sample)
Updates the input history buffer with a new sample.
Definition Filter.cpp:84
void notify_tick(double value) override
Notifies all registered callbacks with the current filter context.
Definition Filter.cpp:250
std::vector< double > m_coef_a
Feedback (denominator) coefficients.
Definition Filter.hpp:610
Base class for computational signal transformers implementing difference equations.
Definition Filter.hpp:138
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
Definition IIR.cpp:70
IIR(const std::shared_ptr< Node > &input, const std::string &zindex_shifts)
Creates an IIR filter with specified order.
Definition IIR.cpp:5
double process_sample(double input=0.) override
Processes a single sample through the IIR filter.
Definition IIR.cpp:20
void restore_state() override
Restores the node's state from the last save Recursively cascades through all connected modulator nod...
Definition IIR.cpp:81
bool m_fire_events_during_snapshot
Internal flag controlling whether notify_tick fires during state snapshots Default: false (events don...
Definition Node.hpp:448
void atomic_add_flag(std::atomic< Utils::NodeState > &state, Utils::NodeState flag)
Atomically adds a flag to a node state.
Definition NodeUtils.cpp:96
void try_reset_processed_state(std::shared_ptr< Node > node)
Attempts to reset the processed state of a node.
void atomic_inc_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically increments the modulator count by a specified amount.
void atomic_dec_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically decrements the modulator count by a specified amount.
@ PROCESSED
Node has been processed this cycle.
Definition Utils.hpp:34