9Filter::Filter(
const std::shared_ptr<Node>&
input,
const std::vector<double>& a_coef,
const std::vector<double>& b_coef)
11 , m_context(0.0, m_input_history, m_output_history, m_coef_a, m_coef_b)
12 , m_context_gpu(0.0, m_input_history, m_output_history, m_coef_a, m_coef_b, get_gpu_data_buffer())
18Filter::Filter(
const std::vector<double>& a_coef,
const std::vector<double>& b_coef)
19 :
Filter(nullptr, a_coef, b_coef)
43 for (
size_t i = 0; i < lookback; ++i) {
69 if (new_coefs.empty()) {
71 "Denominator coefficients cannot be empty. Received size: {}", new_coefs.size());
75 "First denominator coefficient (a[0]) magnitude below {}. Received a[0]: {}",
k_min_leading_coef, new_coefs[0]);
88 if (new_coefs.empty()) {
90 "Numerator coefficients cannot be empty. Received size: {}", new_coefs.size());
99 std::vector<double> samples = source->process_batch(length);
106 std::vector<double> samples =
m_input_node->process_batch(length);
115 if (
index >= buffer.size()) {
116 buffer.resize(
index + 1, 0.0);
123 "Leading denominator coefficient {} below floor; restoring 1.0",
m_coef_a[0]);
178 double max_coef = 0.0;
180 max_coef = std::max(max_coef, std::abs(coef));
183 if (max_coef > 0.0) {
194 double omega = 2.0 * M_PI *
frequency / sample_rate;
195 std::complex<double> z = std::exp(std::complex<double>(0, omega));
197 std::complex<double> numerator = 0.0;
198 for (
size_t i = 0; i <
m_coef_b.size(); ++i) {
199 numerator +=
m_coef_b[i] * std::pow(z, -
static_cast<int>(i));
202 std::complex<double> denominator = 0.0;
203 for (
size_t i = 0; i <
m_coef_a.size(); ++i) {
204 denominator +=
m_coef_a[i] * std::pow(z, -
static_cast<int>(i));
207 return numerator / denominator;
222 std::vector<double>
output(num_samples);
223 for (
unsigned int i = 0; i < num_samples; ++i) {
236 for (
size_t i = 0; i < src.size(); ++i)
254 if (condition(ctx)) {
285std::vector<std::pair<ModulatorRole, std::shared_ptr<Node>>>
#define MF_WARN(comp, ctx,...)
Digital filter coefficient design for MayaFlux::Kinesis.
Core::GlobalInputConfig input
vk::PhysicalDeviceType type
std::shared_ptr< Core::VKImage > output
std::vector< float > gpu_float_buffer
const std::vector< double > & input_history
Current input history buffer.
Specialized context for filter node callbacks.
void add_coef_internal(uint64_t index, double value, std::vector< double > &buffer)
Modifies a specific coefficient in a coefficient buffer.
Filter(const std::shared_ptr< Node > &input, const std::vector< double > &a_coef, const std::vector< double > &b_coef)
Constructor using explicit coefficient vectors.
void add_coef(int index, double value, coefficients type=coefficients::ALL)
Modifies a specific coefficient.
void on_tick(const TypedHook< FilterContext > &callback)
Registers a callback to be called on each tick with the filter context.
std::vector< double > process_batch(unsigned int num_samples) override
Calculates the phase response at a specific frequency.
virtual void reset()
Resets the filter's internal state.
void build_input_history(double current_sample)
Builds input history from external context or internal accumulation.
std::vector< double > m_coef_b
Feedforward (numerator) coefficients.
std::vector< double > m_output_history
Buffer storing previous output samples.
double max_pole_magnitude() const
Largest pole magnitude of the current denominator.
virtual void update_outputs(double current_sample)
Updates the output history buffer with a new sample.
std::vector< std::pair< ModulatorRole, std::shared_ptr< Node > > > get_modulators() const override
Retrieves the current modulators connected to this node.
std::shared_ptr< Node > m_input_node
The most recent sample value generated by this oscillator.
void update_coef_from_input(int length, coefficients type=coefficients::ALL)
Updates coefficients from the filter's own input.
std::complex< double > get_frequency_response(double frequency, double sample_rate) const
Calculates the complex frequency response at a specific frequency.
void set_coefs(const std::vector< double > &new_coefs, coefficients type=coefficients::ALL)
Updates filter coefficients.
bool m_use_external_input_context
void normalize_coefficients(coefficients type=coefficients::ALL)
Normalizes filter coefficients.
void setACoefficients(const std::vector< double > &new_coefs)
Updates the feedback (denominator) coefficients.
std::vector< double > m_input_history
Buffer storing previous input samples.
void setBCoefficients(const std::vector< double > &new_coefs)
Updates the feedforward (numerator) coefficients.
virtual void update_inputs(double current_sample)
Updates the input history buffer with a new sample.
void update_coefs_from_node(int length, const std::shared_ptr< Node > &source, coefficients type=coefficients::ALL)
Updates coefficients from another node's output.
void notify_tick(double value) override
Notifies all registered callbacks with the current filter context.
FilterContextGpu m_context_gpu
std::span< double > m_external_input_context
External input context for input history.
void on_tick_if(const NodeCondition &condition, const TypedHook< FilterContext > &callback)
Registers a conditional callback to be called on each tick if the condition is met.
double get_phase_response(double frequency, double sample_rate) const
Calculates the phase response at a specific frequency.
void update_context(double value) override
Updates filter-specific context object.
std::vector< double > m_coef_a
Feedback (denominator) coefficients.
double get_magnitude_response(double frequency, double sample_rate) const
Calculates the magnitude response at a specific frequency.
double process_sample(double input=0.) override=0
Processes a single sample through the filter.
NodeContext & get_last_context() override
Gets the last created context object.
Base class for computational signal transformers implementing difference equations.
std::span< const float > m_gpu_data
double value
Current sample value.
Base context class for node callbacks.
std::vector< NodeHook > m_callbacks
Collection of standard callback functions.
std::vector< std::pair< NodeHook, NodeCondition > > m_conditional_callbacks
Collection of conditional callback functions with their predicates.
bool m_gpu_compatible
Flag indicating if the node supports GPU processing This flag is set by derived classes to indicate w...
@ Configuration
Configuration and parameter updates.
@ NodeProcessing
Node graph processing (Nodes::NodeGraphManager)
@ Nodes
DSP Generator and Filter Nodes, graph pipeline, node management.
double max_pole_magnitude(std::span< const double > a)
Largest pole magnitude of a denominator polynomial.
constexpr double k_min_leading_coef
Minimum permissible magnitude for the leading denominator coefficient.
std::function< void(ContextT &)> TypedHook
Callback function type for node processing events, parameterised on context type.
std::function< bool(NodeContext &)> NodeCondition
Predicate function type for conditional callbacks.