MayaFlux 0.2.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 && !m_networked_node) {
61 notify_tick(output);
62 }
63
64 if (m_input_node) {
65 atomic_dec_modulator_count(m_input_node->m_modulator_count, 1);
67 }
68
69 return output * get_gain();
70}
71
82
93
94}
std::vector< double > m_saved_output_history
Definition Filter.hpp:649
void build_input_history(double current_sample)
Builds input history from external context or internal accumulation.
Definition Filter.cpp:72
std::vector< double > m_coef_b
Feedforward (numerator) coefficients.
Definition Filter.hpp:630
std::vector< double > m_output_history
Buffer storing previous output samples.
Definition Filter.hpp:605
virtual void update_outputs(double current_sample)
Updates the output history buffer with a new sample.
Definition Filter.cpp:96
bool is_bypass_enabled() const
Checks if bypass is currently enabled.
Definition Filter.hpp:307
std::shared_ptr< Node > m_input_node
The most recent sample value generated by this oscillator.
Definition Filter.hpp:581
std::vector< double > m_saved_input_history
Definition Filter.hpp:648
double get_gain() const
Gets the current gain value.
Definition Filter.hpp:291
std::vector< double > m_input_history
Buffer storing previous input samples.
Definition Filter.hpp:597
virtual void update_inputs(double current_sample)
Updates the input history buffer with a new sample.
Definition Filter.cpp:88
void notify_tick(double value) override
Notifies all registered callbacks with the current filter context.
Definition Filter.cpp:254
std::vector< double > m_coef_a
Feedback (denominator) coefficients.
Definition Filter.hpp:622
Base class for computational signal transformers implementing difference equations.
Definition Filter.hpp:145
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
Definition IIR.cpp:72
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:83
bool m_state_saved
tracks if the node's state has been saved by a snapshot operation
Definition Node.hpp:426
bool m_networked_node
Flag indicating if the node is part of a NodeNetwork This flag is used to disable event firing when t...
Definition Node.hpp:421
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