18 , m_threshold(threshold)
19 , m_low_threshold(threshold * 0.9)
20 , m_high_threshold(threshold)
22 , m_context(0.0, m_mode, m_operator, {}, m_threshold, m_edge_detected, m_edge_type, m_input_buffer)
23 , m_context_gpu(0.0, m_mode, m_operator, {}, m_threshold, m_edge_detected, m_edge_type, m_input_buffer, get_gpu_data_buffer())
25 m_direct_function = [
this](
double input) {
26 return input > m_threshold;
35 , m_threshold(threshold)
36 , m_low_threshold(threshold * 0.9)
37 , m_high_threshold(threshold)
39 , m_context(0.0, m_mode, m_operator, {}, m_threshold, m_edge_detected, m_edge_type, m_input_buffer)
40 , m_context_gpu(0.0, m_mode, m_operator, {}, m_threshold, m_edge_detected, m_edge_type, m_input_buffer, get_gpu_data_buffer())
48 , m_direct_function(
std::move(function))
52 , m_low_threshold(0.45)
53 , m_high_threshold(0.5)
55 , m_context(0.0, m_mode, m_operator, {}, m_threshold, m_edge_detected, m_edge_type, m_input_buffer)
56 , m_context_gpu(0.0, m_mode, m_operator, {}, m_threshold, m_edge_detected, m_edge_type, m_input_buffer, get_gpu_data_buffer())
63 , m_multi_input_function(
std::move(function))
65 , m_input_count(input_count)
67 , m_low_threshold(0.45)
68 , m_high_threshold(0.5)
70 , m_context(0.0, m_mode, m_operator, {}, m_threshold, m_edge_detected, m_edge_type, m_input_buffer)
71 , m_context_gpu(0.0, m_mode, m_operator, {}, m_threshold, m_edge_detected, m_edge_type, m_input_buffer, get_gpu_data_buffer())
73 m_input_buffer.resize(input_count, 0.0);
79 , m_sequential_function(
std::move(function))
80 , m_history_size(history_size)
83 , m_low_threshold(0.45)
84 , m_high_threshold(0.5)
86 , m_history_ring(history_size, false)
87 , m_history_linear(history_size, false)
88 , m_context(0.0, m_mode, m_operator, {}, m_threshold, m_edge_detected, m_edge_type, m_input_buffer)
89 , m_context_gpu(0.0, m_mode, m_operator, {}, m_threshold, m_edge_detected, m_edge_type, m_input_buffer, get_gpu_data_buffer())
96 , m_temporal_function(
std::move(function))
100 , m_low_threshold(0.45)
101 , m_high_threshold(0.5)
103 , m_context(0.0, m_mode, m_operator, {}, m_threshold, m_edge_detected, m_edge_type, m_input_buffer)
104 , m_context_gpu(0.0, m_mode, m_operator, {}, m_threshold, m_edge_detected, m_edge_type, m_input_buffer, get_gpu_data_buffer())
135 result = current_bool;
149 if (current_bool != previous_bool_input) {
167 result = current_bool && previous_bool;
171 result = current_bool || previous_bool;
175 result = current_bool != previous_bool;
179 result = !current_bool;
183 result = !(current_bool && previous_bool);
187 result = !(current_bool || previous_bool);
195 result = current_bool;
221 auto current = result ? 1.0 : 0.0;
239 std::vector<double> output(num_samples);
241 for (
unsigned int i = 0; i < num_samples; ++i) {
264 for (
const auto& input : inputs) {
273 for (
size_t i = 0; i < inputs.size() && i <
m_input_buffer.size(); ++i) {
351 if (create_default_direct_function) {
357 return current && previous;
365 return current || previous;
373 return current != previous;
387 return !(current && previous);
395 return !(current || previous);
508 bool should_call =
false;
510 switch (cb.event_type) {
516 should_call = (bool)value;
520 should_call = !value;
524 should_call = state_changed;
528 should_call = state_changed && (bool)value;
532 should_call = state_changed && !value;
536 should_call = cb.condition && cb.condition.value()(ctx);
593 return cb.callback.target_type() == callback.target_type();
607 return cb.event_type == LogicEventType::CONDITIONAL && cb.condition && cb.condition->target_type() == callback.target_type();
621 return cb.event_type == type;
std::span< bool > m_history
History of boolean states.
LogicMode m_mode
Current computational model.
LogicOperator m_operator
Current boolean operator.
EdgeType m_edge_type
Type of transition being monitored.
bool m_edge_detected
Whether a state transition was detected.
double m_threshold
Decision boundary for binary quantization.
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.
std::vector< uint8_t > m_saved_history_ring
DirectFunction m_direct_function
Function for direct mode.
bool m_saved_hysteresis_state
double m_saved_last_output
void update_context(double value) override
Updates the context with the latest sample value.
void reset()
Resets internal state to initial conditions.
std::vector< uint8_t > m_history_linear
Linear view of history for easy access.
std::span< bool > get_history()
Gets the current state history.
size_t m_history_size
Maximum size of the history buffer.
EdgeType m_edge_type
Type of edge to detect.
Logic(double threshold=0.5)
Constructs a Logic node with threshold quantization.
size_t m_saved_history_head
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::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.
NodeContext & get_last_context() override
Retrieves the last created context object.
double m_high_threshold
High threshold for hysteresis.
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.
void history_push(bool val)
std::function< bool(const std::vector< double > &)> MultiInputFunction
Function type for parallel input evaluation.
size_t m_saved_history_count
void on_change_to(bool target_state, const NodeHook &callback)
Registers a callback for when output changes to a specific state.
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.
bool m_edge_detected
Whether an edge was detected in the last processing.
std::function< bool(std::span< bool >)> SequentialFunction
Function type for state-based evaluation.
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...
void on_tick_if(const NodeCondition &condition, const NodeHook &callback) override
Registers a conditional callback for generated samples.
std::vector< uint8_t > m_history_ring
Ring buffer for history storage.
bool m_hysteresis_state
State for hysteresis operator.
void on_tick(const NodeHook &callback) override
Registers a callback for every generated sample.
size_t m_history_count
Number of valid entries in the history buffer.
bool m_saved_edge_detected
void on_change(const NodeHook &callback)
Registers a callback for any state change (true↔false)
size_t m_history_head
Head index for the history ring buffer.
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.
std::span< bool > history_linearized_view()
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.
LogicContextGpu m_context_gpu
double value
Current sample value.
Base context class for node callbacks.
bool m_state_saved
tracks if the node's state has been saved by a snapshot operation
bool m_networked_node
Flag indicating if the node is part of a NodeNetwork This flag is used to disable event firing when t...
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...
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.