16 , m_threshold(threshold)
17 , m_low_threshold(threshold * 0.9)
18 , m_high_threshold(threshold)
20 , m_edge_detected(false)
21 , m_hysteresis_state(false)
22 , m_temporal_time(0.0)
34 , m_threshold(threshold)
35 , m_low_threshold(threshold * 0.9)
36 , m_high_threshold(threshold)
38 , m_edge_detected(false)
39 , m_hysteresis_state(false)
40 , m_temporal_time(0.0)
48 , m_direct_function(
std::move(function))
52 , m_low_threshold(0.45)
53 , m_high_threshold(0.5)
55 , m_edge_detected(false)
56 , m_hysteresis_state(false)
57 , m_temporal_time(0.0)
64 , m_multi_input_function(
std::move(function))
66 , m_input_count(input_count)
68 , m_low_threshold(0.45)
69 , m_high_threshold(0.5)
71 , m_edge_detected(false)
72 , m_hysteresis_state(false)
73 , m_temporal_time(0.0)
81 , m_sequential_function(
std::move(function))
82 , m_history_size(history_size)
85 , m_low_threshold(0.45)
86 , m_high_threshold(0.5)
88 , m_edge_detected(false)
89 , m_hysteresis_state(false)
90 , m_temporal_time(0.0)
98 , m_temporal_function(
std::move(function))
102 , m_low_threshold(0.45)
103 , m_high_threshold(0.5)
105 , m_edge_detected(false)
106 , m_hysteresis_state(false)
107 , m_temporal_time(0.0)
138 result = current_bool;
152 if (current_bool != previous_bool_input) {
170 result = current_bool && previous_bool;
174 result = current_bool || previous_bool;
178 result = current_bool != previous_bool;
182 result = !current_bool;
186 result = !(current_bool && previous_bool);
190 result = !(current_bool || previous_bool);
198 result = current_bool;
226 auto current = result ? 1.0 : 0.0;
242 std::vector<double> output(num_samples);
244 for (
unsigned int i = 0; i < num_samples; ++i) {
267 for (
const auto& input : inputs) {
276 for (
size_t i = 0; i < inputs.size() && i <
m_input_buffer.size(); ++i) {
353 if (create_default_direct_function) {
359 return current && previous;
367 return current || previous;
375 return current != previous;
389 return !(current && previous);
397 return !(current || previous);
475 for (
const auto& value : initial_values) {
489 return std::make_unique<LogicContextGpu>(
501 return std::make_unique<LogicContext>(
518 bool should_call =
false;
520 switch (cb.event_type) {
530 should_call = !value;
534 should_call = state_changed;
538 should_call = state_changed && value;
542 should_call = state_changed && !value;
546 should_call = cb.condition && cb.condition.value()(*m_last_context);
590 return cb.callback.target_type() == callback.target_type();
604 return cb.event_type == LogicEventType::CONDITIONAL && cb.condition && cb.condition->target_type() == callback.target_type();
618 return cb.event_type == type;
SequentialFunction m_sequential_function
Function for sequential mode.
void set_direct_function(DirectFunction function)
Sets a custom combinational logic function.
void set_initial_conditions(const std::vector< bool > &initial_values)
Preloads the state history buffer.
void set_sequential_function(SequentialFunction function, size_t history_size)
Sets a custom state-based evaluation function.
TemporalFunction m_temporal_function
Function for temporal mode.
DirectFunction m_direct_function
Function for direct mode.
bool m_saved_hysteresis_state
double m_saved_last_output
void reset()
Resets internal state to initial conditions.
size_t m_history_size
Maximum size of the history buffer.
void on_change_to(const NodeHook &callback, bool target_state)
Registers a callback for when output changes to a specific state.
EdgeType m_edge_type
Type of edge to detect.
Logic(double threshold=0.5)
Constructs a Logic node with threshold quantization.
double process_sample(double input=0.) override
Processes a single input sample through the logic function.
double m_threshold
Threshold for boolean conversion.
void remove_hooks_of_type(LogicEventType type)
std::deque< bool > m_history
Buffer of input values for feedforward mode.
void on_tick_if(const NodeHook &callback, const NodeCondition &condition) override
Registers a conditional callback for generated samples.
std::function< bool(double)> DirectFunction
Function type for stateless boolean evaluation.
double m_temporal_time
Time tracking for temporal mode.
void set_temporal_function(TemporalFunction function)
Sets a custom time-dependent evaluation function.
double m_high_threshold
High threshold for hysteresis.
std::function< bool(const std::deque< bool > &)> SequentialFunction
Function type for state-based evaluation.
double m_input
Current input value for multi-input mode.
std::vector< double > m_input_buffer
void set_edge_detection(EdgeType type, double threshold=0.5)
Configures digital transition detection.
std::shared_ptr< Node > m_input_node
Input node for processing.
double m_saved_temporal_time
LogicOperator m_operator
Current logic operator.
std::function< bool(const std::vector< double > &)> MultiInputFunction
Function type for parallel input evaluation.
void set_operator(LogicOperator op, bool create_default_direct_function=false)
Sets the boolean operation to perform.
void add_callback(const NodeHook &callback, LogicEventType type, const std::optional< NodeCondition > &condition=std::nullopt)
Adds a callback to the list of all callbacks.
double m_low_threshold
Low threshold for hysteresis.
MultiInputFunction m_multi_input_function
Function for recursive/feedforward mode.
std::deque< bool > m_saved_history
bool m_edge_detected
Whether an edge was detected in the last processing.
bool remove_conditional_hook(const NodeCondition &callback) override
Removes a previously registered conditional callback.
std::vector< LogicCallback > m_all_callbacks
Collection of all callback functions.
void set_hysteresis(double low_threshold, double high_threshold, bool create_default_direct_function=false)
Configures noise-resistant binary quantization with memory.
void set_multi_input_function(MultiInputFunction function, size_t input_count)
Sets a custom parallel input evaluation function.
std::vector< double > process_batch(unsigned int num_samples) override
Processes multiple samples in batch mode.
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
bool m_hysteresis_state
State for hysteresis operator.
void on_tick(const NodeHook &callback) override
Registers a callback for every generated sample.
bool m_saved_edge_detected
void on_change(const NodeHook &callback)
Registers a callback for any state change (true↔false)
std::unique_ptr< NodeContext > create_context(double value) override
Creates a context object for callbacks.
double process_multi_input(const std::vector< double > &inputs)
Processes multiple parallel inputs.
LogicMode m_mode
Current processing mode.
std::function< bool(double, double)> TemporalFunction
Function type for time-dependent evaluation.
void while_false(const NodeHook &callback)
Registers a callback that executes continuously while output is false.
void restore_state() override
Restores the node's state from the last save Recursively cascades through all connected modulator nod...
void notify_tick(double value) override
Notifies all registered callbacks about a new sample.
size_t m_input_count
Expected number of inputs for multi-input mode.
bool remove_hook(const NodeHook &callback) override
Removes a previously registered callback.
void add_input(double input, size_t index)
void set_threshold(double threshold, bool create_default_direct_function=false)
Sets the decision boundary for binary quantization.
void while_true(const NodeHook &callback)
Registers a callback that executes continuously while output is true.
double m_last_output
The most recent sample value generated by this oscillator.
bool m_fire_events_during_snapshot
Internal flag controlling whether notify_tick fires during state snapshots Default: false (events don...
bool m_gpu_compatible
Flag indicating if the node supports GPU processing This flag is set by derived classes to indicate w...
std::unique_ptr< NodeContext > m_last_context
The last context object created for callbacks.
std::span< const float > get_gpu_data_buffer() const
Provides access to the GPU data buffer.
uint32_t get_sample_rate()
Gets the sample rate from the default engine.
EdgeType
Digital transition patterns to detect.
@ FALLING
High-to-low transition (1→0)
@ BOTH
Any state transition.
@ RISING
Low-to-high transition (0→1)
LogicMode
Defines the computational model for digital signal evaluation.
@ SEQUENTIAL
State-based evaluation using history of inputs (sequential logic)
@ DIRECT
Stateless evaluation of current input only (combinational logic)
@ TEMPORAL
Time-dependent evaluation with timing constraints.
@ MULTI_INPUT
Parallel evaluation of multiple input signals.
LogicEventType
Events that can trigger callbacks.
LogicOperator
Digital operators for boolean computation.
@ NOT
Logical NOT - inverts the input.
@ NOR
Logical NOR - inverted OR operation.
@ OR
Logical OR - true when any input is true.
@ NAND
Logical NAND - inverted AND operation.
@ THRESHOLD
Binary quantization - true when input exceeds threshold.
@ AND
Logical AND - true only when all inputs are true.
@ EDGE
Transition detector - identifies state changes.
@ HYSTERESIS
Threshold with memory - prevents rapid oscillation at boundary.
@ CUSTOM
User-defined boolean function.
@ XOR
Logical XOR - true when odd number of inputs are true.
std::function< void(NodeContext &)> NodeHook
Callback function type for node processing events.
void atomic_add_flag(std::atomic< Utils::NodeState > &state, Utils::NodeState flag)
Atomically adds a flag to a node state.
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.
std::function< bool(NodeContext &)> NodeCondition
Predicate function type for conditional callbacks.
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.