MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Counter.cpp
Go to the documentation of this file.
1#include "Counter.hpp"
2
4
5Counter::Counter(uint32_t modulo, int32_t step)
6 : m_modulo(modulo)
7 , m_step(step)
8{
9 m_amplitude = 1.0;
10 m_frequency = 0.F;
12}
13
14Counter::Counter(const std::shared_ptr<Node>& reset_trigger, uint32_t modulo, int32_t step)
15 : m_modulo(modulo)
16 , m_step(step)
17 , m_reset_trigger(reset_trigger)
18{
19 m_amplitude = 1.0;
20 m_frequency = 0.F;
22}
23
24void Counter::set_modulo(uint32_t modulo) { m_modulo = modulo; }
25void Counter::set_step(int32_t step) { m_step = step; }
26void Counter::set_reset_trigger(const std::shared_ptr<Node>& trigger) { m_reset_trigger = trigger; }
28void Counter::reset() { m_count = 0; }
29
30void Counter::on_increment(const TypedHook<GeneratorContext>& callback) { m_increment_callbacks.push_back(callback); }
31void Counter::on_wrap(const TypedHook<GeneratorContext>& callback) { m_wrap_callbacks.push_back(callback); }
32void Counter::on_count(uint32_t target, const TypedHook<GeneratorContext>& callback) { m_count_callbacks.emplace_back(target, callback); }
33
34bool Counter::remove_hook(const NodeHook& callback)
35{
36 bool r = safe_remove_callback(m_callbacks, callback);
37 auto erase_typed = [&](auto& vec) {
38 auto before = vec.size();
39 std::erase_if(vec, [&](const auto& cb) {
40 return cb.target_type() == callback.target_type();
41 });
42 return vec.size() < before;
43 };
44 r |= erase_typed(m_increment_callbacks);
45 r |= erase_typed(m_wrap_callbacks);
46 std::erase_if(m_count_callbacks, [&](const auto& p) {
47 return p.second.target_type() == callback.target_type();
48 });
49 return r;
50}
51
53{
54 m_callbacks.clear();
57 m_wrap_callbacks.clear();
58 m_count_callbacks.clear();
59}
60
61double Counter::process_sample(double /*input*/)
62{
63 if (m_reset_trigger) {
64 atomic_inc_modulator_count(m_reset_trigger->m_modulator_count, 1);
65 uint32_t state = m_reset_trigger->m_state.load();
66
67 double trigger_val = 0.0;
68 if (state & NodeState::PROCESSED) {
69 trigger_val = m_reset_trigger->get_last_output();
70 } else {
71 trigger_val = m_reset_trigger->process_sample(0.0);
73 }
74
75 if (trigger_val != 0.0 && m_last_trigger_value == 0.0) {
76 m_count = 0;
77 }
78 m_last_trigger_value = trigger_val;
79 }
80
81 m_wrapped = false;
82 if (m_modulo > 0) {
83 auto prev = static_cast<int32_t>(m_count);
84 int32_t next = (prev + m_step);
85 int32_t wrapped = ((next % static_cast<int32_t>(m_modulo)) + static_cast<int32_t>(m_modulo)) % static_cast<int32_t>(m_modulo);
86 m_wrapped = (wrapped < prev && m_step > 0) || (wrapped > prev && m_step < 0);
87 m_count = static_cast<uint32_t>(wrapped);
88 } else {
89 m_count += static_cast<uint32_t>(m_step);
90 }
91
92 double output = (m_modulo > 1)
93 ? static_cast<double>(m_count) / static_cast<double>(m_modulo - 1)
94 : static_cast<double>(m_count);
95
96 output *= m_amplitude;
97 m_last_output = output;
98
100 && !m_networked_node) {
101 notify_tick(output);
102 }
103
104 if (m_reset_trigger) {
105 atomic_dec_modulator_count(m_reset_trigger->m_modulator_count, 1);
107 }
108
109 return output;
110}
111
112std::vector<double> Counter::process_batch(unsigned int num_samples)
113{
114 std::vector<double> out(num_samples);
115 for (unsigned int i = 0; i < num_samples; ++i) {
116 out[i] = process_sample(0.0);
117 }
118 return out;
119}
120
121void Counter::notify_tick(double value)
122{
123 update_context(value);
124 auto& ctx = get_last_context();
125 auto& gc = dynamic_cast<GeneratorContext&>(ctx);
126
127 for (auto& cb : m_callbacks) {
128 cb(ctx);
129 }
130 for (auto& [cb, cond] : m_conditional_callbacks) {
131 if (cond(ctx)) {
132 cb(ctx);
133 }
134 }
135 for (auto& cb : m_increment_callbacks) {
136 cb(gc);
137 }
138 if (m_wrapped) {
139 for (auto& cb : m_wrap_callbacks) {
140 cb(gc);
141 }
142 }
143 for (auto& [target, cb] : m_count_callbacks) {
144 if (m_count == target) {
145 cb(gc);
146 }
147 }
148}
149
150void Counter::update_context(double value)
151{
152 m_context = GeneratorContext(value, 0.F, m_amplitude, static_cast<double>(m_count));
153}
154
167
180
181std::vector<std::pair<ModulatorRole, std::shared_ptr<Node>>>
183{
184 if (m_reset_trigger)
186 return {};
187}
188
189} // namespace MayaFlux::Nodes::Generatorx::Nodes::Generator
std::vector< TypedHook< GeneratorContext > > m_increment_callbacks
Definition Counter.hpp:138
bool remove_hook(const NodeHook &callback) override
Removes a previously registered callback.
Definition Counter.cpp:34
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
Definition Counter.cpp:155
void on_count(uint32_t target, const TypedHook< GeneratorContext > &callback)
Registers a callback fired when the counter reaches a specific raw value.
Definition Counter.cpp:32
void reset()
Resets counter to zero immediately.
Definition Counter.cpp:28
std::vector< double > process_batch(unsigned int num_samples) override
Processes multiple samples at once.
Definition Counter.cpp:112
void set_reset_trigger(const std::shared_ptr< Node > &trigger)
Connects a reset trigger node.
Definition Counter.cpp:26
std::vector< TypedHook< GeneratorContext > > m_wrap_callbacks
Definition Counter.hpp:139
void restore_state() override
Restores the node's state from the last save Recursively cascades through all connected modulator nod...
Definition Counter.cpp:168
void update_context(double value) override
Updates the context object for callbacks.
Definition Counter.cpp:150
void on_wrap(const TypedHook< GeneratorContext > &callback)
Registers a callback fired when the counter wraps to zero.
Definition Counter.cpp:31
void set_step(int32_t step)
Sets the step increment.
Definition Counter.cpp:25
void remove_all_hooks() override
Removes all registered callbacks.
Definition Counter.cpp:52
void notify_tick(double value) override
Notifies all registered callbacks with the current context.
Definition Counter.cpp:121
Counter(uint32_t modulo=16, int32_t step=1)
Construct with modulo and step.
Definition Counter.cpp:5
std::vector< std::pair< uint32_t, TypedHook< GeneratorContext > > > m_count_callbacks
Definition Counter.hpp:140
void on_increment(const TypedHook< GeneratorContext > &callback)
Registers a callback fired on every increment.
Definition Counter.cpp:30
std::shared_ptr< Node > m_reset_trigger
Definition Counter.hpp:129
std::vector< std::pair< ModulatorRole, std::shared_ptr< Node > > > get_modulators() const override
Retrieves the current modulators connected to this node.
Definition Counter.cpp:182
void set_modulo(uint32_t modulo)
Sets the wrap boundary.
Definition Counter.cpp:24
void clear_reset_trigger()
Disconnects the reset trigger node.
Definition Counter.cpp:27
double process_sample(double input=0.0) override
Processes a single data sample.
Definition Counter.cpp:61
Specialized context for generator node callbacks.
Definition Generator.hpp:24
float m_frequency
Base frequency of the generator.
NodeContext & get_last_context() override
Gets the last created context object.
Definition Generator.cpp:51
double m_amplitude
Base amplitude of the generator.
std::vector< NodeHook > m_callbacks
Collection of standard callback functions.
Definition Node.hpp:434
bool m_state_saved
tracks if the node's state has been saved by a snapshot operation
Definition Node.hpp:457
bool m_networked_node
Flag indicating if the node is part of a NodeNetwork This flag is used to disable event firing when t...
Definition Node.hpp:452
double m_last_output
The most recent sample value generated by this oscillator.
Definition Node.hpp:405
bool m_fire_events_during_snapshot
Internal flag controlling whether notify_tick fires during state snapshots Default: false (events don...
Definition Node.hpp:487
std::vector< std::pair< NodeHook, NodeCondition > > m_conditional_callbacks
Collection of conditional callback functions with their predicates.
Definition Node.hpp:444
TypedHook<> NodeHook
Alias for TypedHook<NodeContext>.
Definition NodeUtils.hpp:38
@ PROCESSED
Node has been processed this cycle.
Definition NodeSpec.hpp:49
void atomic_add_flag(std::atomic< NodeState > &state, NodeState flag)
Atomically adds a flag to a node state.
Definition NodeUtils.cpp:60
void try_reset_processed_state(std::shared_ptr< Node > node)
Attempts to reset the processed state of a node.
Definition NodeUtils.cpp:97
void atomic_inc_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically increments the modulator count by a specified amount.
Definition NodeUtils.cpp:87
std::function< void(ContextT &)> TypedHook
Callback function type for node processing events, parameterised on context type.
Definition NodeUtils.hpp:28
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.
Definition NodeUtils.hpp:92
void atomic_dec_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically decrements the modulator count by a specified amount.
Definition NodeUtils.cpp:92