13Counter::Counter(
const std::shared_ptr<Node>& reset_trigger, uint32_t modulo, int32_t step)
16 , m_reset_trigger(reset_trigger)
35 auto erase_typed = [&](
auto& vec) {
36 auto before = vec.size();
37 std::erase_if(vec, [&](
const auto& cb) {
38 return cb.target_type() == callback.target_type();
40 return vec.size() < before;
45 return p.second.target_type() == callback.target_type();
65 double trigger_val = 0.0;
81 auto prev =
static_cast<int32_t
>(
m_count);
82 int32_t next = (prev +
m_step);
83 int32_t wrapped = ((next %
static_cast<int32_t
>(
m_modulo)) +
static_cast<int32_t
>(
m_modulo)) %
static_cast<int32_t
>(
m_modulo);
84 m_wrapped = (wrapped < prev && m_step > 0) || (wrapped > prev &&
m_step < 0);
85 m_count =
static_cast<uint32_t
>(wrapped);
112 std::vector<double> out(num_samples);
113 for (
unsigned int i = 0; i < num_samples; ++i) {
std::vector< TypedHook< GeneratorContext > > m_increment_callbacks
bool remove_hook(const NodeHook &callback) override
Removes a previously registered callback.
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
void on_count(uint32_t target, const TypedHook< GeneratorContext > &callback)
Registers a callback fired when the counter reaches a specific raw value.
void reset()
Resets counter to zero immediately.
std::vector< double > process_batch(unsigned int num_samples) override
Processes multiple samples at once.
void set_reset_trigger(const std::shared_ptr< Node > &trigger)
Connects a reset trigger node.
std::vector< TypedHook< GeneratorContext > > m_wrap_callbacks
void restore_state() override
Restores the node's state from the last save Recursively cascades through all connected modulator nod...
void update_context(double value) override
Updates the context object for callbacks.
void on_wrap(const TypedHook< GeneratorContext > &callback)
Registers a callback fired when the counter wraps to zero.
void set_step(int32_t step)
Sets the step increment.
void remove_all_hooks() override
Removes all registered callbacks.
double m_last_trigger_value
void notify_tick(double value) override
Notifies all registered callbacks with the current context.
double m_saved_last_output
Counter(uint32_t modulo=16, int32_t step=1)
Construct with modulo and step.
std::vector< std::pair< uint32_t, TypedHook< GeneratorContext > > > m_count_callbacks
void on_increment(const TypedHook< GeneratorContext > &callback)
Registers a callback fired on every increment.
std::shared_ptr< Node > m_reset_trigger
double m_saved_last_trigger_value
void set_modulo(uint32_t modulo)
Sets the wrap boundary.
void clear_reset_trigger()
Disconnects the reset trigger node.
GeneratorContext m_context
NodeContext & get_last_context() override
Retrieves the last created context object.
double process_sample(double input=0.0) override
Processes a single data sample.
Specialized context for generator node callbacks.
float m_frequency
Base frequency of the generator.
double m_amplitude
Base amplitude of the generator.
Base context class for node callbacks.
std::vector< NodeHook > m_callbacks
Collection of standard callback functions.
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...
std::vector< std::pair< NodeHook, NodeCondition > > m_conditional_callbacks
Collection of conditional callback functions with their predicates.
TypedHook<> NodeHook
Alias for TypedHook<NodeContext>.
@ PROCESSED
Node has been processed this cycle.
void atomic_add_flag(std::atomic< NodeState > &state, 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< void(ContextT &)> TypedHook
Callback function type for node processing events, parameterised on context type.
bool safe_remove_callback(std::vector< TypedHook< ContextT > > &callbacks, const TypedHook< ContextT > &callback)
Removes all callbacks whose target_type() matches that of the supplied callback.
void atomic_dec_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically decrements the modulator count by a specified amount.