MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
FIR.cpp
Go to the documentation of this file.
1#include "FIR.hpp"
2
4
5FIR::FIR(const std::shared_ptr<Node>& input, const std::vector<double>& coeffs)
6 : Filter(input, std::vector<double> { 1.0F }, coeffs)
7{
8}
9
10FIR::FIR(const std::shared_ptr<Node>& input, const std::string& zindex_shifts)
11 : Filter(input, zindex_shifts)
12{
13}
14
15FIR::FIR(const std::vector<double>& coeffs)
16 : FIR(nullptr, coeffs)
17{
18}
19
20double FIR::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.0;
45 const size_t num_taps = std::min(m_coef_b.size(), m_input_history.size());
46
47 for (size_t i = 0; i < num_taps; ++i) {
48 output += m_coef_b[i] * m_input_history[i];
49 }
50
51 update_outputs(output);
52
54 notify_tick(output);
55
56 if (m_input_node) {
57 atomic_dec_modulator_count(m_input_node->m_modulator_count, 1);
59 }
60 return output * get_gain();
61}
62
64{
66
67 if (m_input_node)
68 m_input_node->save_state();
69
70 m_state_saved = true;
71}
72
74{
76
77 if (m_input_node)
78 m_input_node->restore_state();
79
80 m_state_saved = false;
81}
82
83}
double process_sample(double input=0.) override
Processes a single sample through the FIR filter.
Definition FIR.cpp:20
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
Definition FIR.cpp:63
void restore_state() override
Restores the node's state from the last save Recursively cascades through all connected modulator nod...
Definition FIR.cpp:73
FIR(const std::shared_ptr< Node > &input, const std::string &zindex_shifts)
Creates an FIR filter with specified order.
Definition FIR.cpp:10
Finite Impulse Response filter implementation.
Definition FIR.hpp:34
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
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
Base class for computational signal transformers implementing difference equations.
Definition Filter.hpp:138
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