MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Counter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Generator.hpp"
4
6
7/**
8 * @class Counter
9 * @brief Integer step accumulator with modulo wrap and optional trigger reset
10 *
11 * Advances an internal integer counter by a fixed step on every process_sample()
12 * call, wrapping at a configurable modulo boundary. Output is the counter value
13 * normalized to [0, 1] by default, making it directly composable with any
14 * downstream node expecting a unit-range signal.
15 *
16 * When modulo is zero the counter accumulates without bound and the raw integer
17 * value is emitted as a double, suitable for use as a direct index.
18 *
19 * An optional reset trigger node resets the counter to zero on a rising edge
20 * (transition from zero to nonzero). The trigger is edge-sensitive: a sustained
21 * nonzero value does not repeatedly reset.
22 *
23 * Tick rate is determined entirely by the processing token the node is routed
24 * into (AUDIO_RATE or VISUAL_RATE), consistent with all other generators.
25 */
26class MAYAFLUX_API Counter : public Generator, public std::enable_shared_from_this<Counter> {
27public:
28 /**
29 * @brief Construct with modulo and step
30 * @param modulo Wrap boundary (0 = unbounded)
31 * @param step Increment per tick (negative for countdown)
32 */
33 Counter(uint32_t modulo = 16, int32_t step = 1);
34
35 /**
36 * @brief Construct with reset trigger node
37 * @param reset_trigger Node whose rising edge resets the counter
38 * @param modulo Wrap boundary (0 = unbounded)
39 * @param step Increment per tick (negative for countdown)
40 */
41 Counter(const std::shared_ptr<Node>& reset_trigger, uint32_t modulo = 16, int32_t step = 1);
42
43 ~Counter() override = default;
44
45 double process_sample(double input = 0.0) override;
46
47 std::vector<double> process_batch(unsigned int num_samples) override;
48
49 inline void printGraph() override { }
50 inline void printCurrent() override { }
51
52 /**
53 * @brief Sets the wrap boundary
54 * @param modulo New modulo value (0 = unbounded)
55 */
56 void set_modulo(uint32_t modulo);
57
58 /**
59 * @brief Sets the step increment
60 * @param step Signed step value applied each tick
61 */
62 void set_step(int32_t step);
63
64 /**
65 * @brief Connects a reset trigger node
66 * @param trigger Node whose rising edge resets the counter to zero
67 */
68 void set_reset_trigger(const std::shared_ptr<Node>& trigger);
69
70 /**
71 * @brief Disconnects the reset trigger node
72 */
73 void clear_reset_trigger();
74
75 /**
76 * @brief Resets counter to zero immediately
77 */
78 void reset();
79
80 /** @brief Returns current raw counter value */
81 [[nodiscard]] uint32_t get_count() const { return m_count; }
82
83 /** @brief Returns current modulo */
84 [[nodiscard]] uint32_t get_modulo() const { return m_modulo; }
85
86 /** @brief Returns current step */
87 [[nodiscard]] int32_t get_step() const { return m_step; }
88
89 /**
90 * @brief Registers a callback fired on every increment
91 * @param callback Receives GeneratorContext directly; phase field carries the raw count
92 */
93 void on_increment(const TypedHook<GeneratorContext>& callback);
94
95 /**
96 * @brief Registers a callback fired when the counter wraps to zero
97 * @param callback Receives GeneratorContext at the wrap boundary
98 */
99 void on_wrap(const TypedHook<GeneratorContext>& callback);
100
101 /**
102 * @brief Registers a callback fired when the counter reaches a specific raw value
103 * @param target Raw counter value to match
104 * @param callback Receives GeneratorContext when count == target
105 */
106 void on_count(uint32_t target, const TypedHook<GeneratorContext>& callback);
107
108 bool remove_hook(const NodeHook& callback) override;
109 void remove_all_hooks() override;
110
111 void set_frequency(float) override { }
112 [[nodiscard]] float get_frequency() const { return 0.F; }
113
114 void save_state() override;
115 void restore_state() override;
116
117protected:
118 void notify_tick(double value) override;
119 void update_context(double value) override;
120 NodeContext& get_last_context() override;
121
122private:
123 uint32_t m_count { 0 };
124 uint32_t m_modulo { 16 };
125 int32_t m_step { 1 };
126
127 std::shared_ptr<Node> m_reset_trigger;
128 double m_last_trigger_value { 0.0 };
129
130 uint32_t m_saved_count { 0 };
131 double m_saved_last_trigger_value { 0.0 };
132 double m_saved_last_output { 0.0 };
133
134 bool m_wrapped { false };
135
136 std::vector<TypedHook<GeneratorContext>> m_increment_callbacks;
137 std::vector<TypedHook<GeneratorContext>> m_wrap_callbacks;
138 std::vector<std::pair<uint32_t, TypedHook<GeneratorContext>>> m_count_callbacks;
139
140 GeneratorContext m_context { 0.0, 0.F, 1.0, 0.0 };
141};
142
143} // namespace MayaFlux::Nodes::Generator
void set_frequency(float) override
Sets the generator's frequency.
Definition Counter.hpp:111
std::vector< TypedHook< GeneratorContext > > m_increment_callbacks
Definition Counter.hpp:136
uint32_t get_modulo() const
Returns current modulo.
Definition Counter.hpp:84
int32_t get_step() const
Returns current step.
Definition Counter.hpp:87
std::vector< TypedHook< GeneratorContext > > m_wrap_callbacks
Definition Counter.hpp:137
void printGraph() override
Prints a visual representation of the generated pattern.
Definition Counter.hpp:49
void printCurrent() override
Prints the current state and parameters of the generator.
Definition Counter.hpp:50
std::vector< std::pair< uint32_t, TypedHook< GeneratorContext > > > m_count_callbacks
Definition Counter.hpp:138
std::shared_ptr< Node > m_reset_trigger
Definition Counter.hpp:127
uint32_t get_count() const
Returns current raw counter value.
Definition Counter.hpp:81
Integer step accumulator with modulo wrap and optional trigger reset.
Definition Counter.hpp:26
Specialized context for generator node callbacks.
Definition Generator.hpp:24
Base class for all signal and pattern generators in Maya Flux.
Base context class for node callbacks.
Definition Node.hpp:30
TypedHook<> NodeHook
Alias for TypedHook<NodeContext>.
Definition NodeUtils.hpp:38
std::function< void(ContextT &)> TypedHook
Callback function type for node processing events, parameterised on context type.
Definition NodeUtils.hpp:28