MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Generator.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
7/**
8 * @class GeneratorContext
9 * @brief Specialized context for generator node callbacks
10 *
11 * GeneratorContext extends the base NodeContext to provide detailed information
12 * about a generator's current state to callbacks. It includes fundamental
13 * oscillator parameters such as frequency, amplitude, and phase that define
14 * the generator's behavior at the moment a sample is produced.
15 *
16 * This rich context enables callbacks to perform sophisticated analysis and
17 * monitoring of generator behavior, such as:
18 * - Tracking parameter changes over time
19 * - Implementing frequency-dependent processing
20 * - Creating visualizations of generator state
21 * - Synchronizing multiple generators based on phase relationships
22 * - Implementing adaptive processing based on generator characteristics
23 */
24class MAYAFLUX_API GeneratorContext : public NodeContext {
25public:
26 /**
27 * @brief Constructs a GeneratorContext with the current generator state
28 * @param value Current output sample value
29 * @param frequency Current oscillation frequency in Hz
30 * @param amplitude Current scaling factor for output values
31 * @param phase Current phase position in radians
32 *
33 * Creates a context object that provides a complete snapshot of the
34 * generator's current state, including its most recent output value
35 * and all parameters that define its oscillation behavior.
36 */
37 GeneratorContext(double value, float frequency, double amplitude, double phase)
38 : NodeContext(value)
40 , amplitude(amplitude)
41 , phase(phase)
42 {
43 }
44
45 /**
46 * @brief Current frequency of the generator
47 *
48 * Represents the oscillation rate in Hertz (cycles per second).
49 * This parameter determines the pitch or periodicity of the
50 * generated signal.
51 */
52 float frequency;
53
54 /**
55 * @brief Current amplitude of the generator
56 */
57 double amplitude;
58
59 /**
60 * @brief Current phase of the generator
61 */
62 double phase;
63};
64
65class MAYAFLUX_API GeneratorContextGpu : public GeneratorContext, public GpuVectorData {
66public:
68 double value,
69 float frequency,
70 double amplitude,
71 double phase,
72 std::span<const float> gpu_data)
73 : GeneratorContext(value, frequency, amplitude, phase)
74 , GpuVectorData(gpu_data)
75 {
76 }
77};
78
79/**
80 * @class Generator
81 * @brief Base class for all signal and pattern generators in Maya Flux
82 *
83 * Generators are specialized nodes that create numerical sequences from mathematical principles,
84 * rather than processing existing signals. They form the foundation of the
85 * computational graph by providing the initial patterns that other nodes
86 * can then transform, filter, or combine.
87 *
88 * Unlike transformation nodes that modify input signals, generators typically:
89 * - Create sequences based on mathematical formulas or algorithms
90 * - Maintain internal state to track progression, phase, or other parameters
91 * - Can operate autonomously without any input (though they may accept modulation inputs)
92 * - Serve as the origin points in computational processing networks
93 *
94 * Common types of generators include:
95 * - Oscillators (sine, square, sawtooth, triangle waves)
96 * - Stochastic generators (various probability distributions)
97 * - Sample-based generators (playing back recorded sequences)
98 * - Envelope generators (creating amplitude contours)
99 *
100 * Generators integrate with the node graph system, allowing them to be:
101 * - Connected to other nodes using operators like '>>' (chain)
102 * - Combined with other nodes using operators like '+' (mix)
103 * - Registered with a RootNode for processing
104 * - Used as modulation sources for other generators or transformations
105 *
106 * The Generator class extends the base Node interface with additional
107 * methods for visualization and analysis of the generated patterns.
108 */
109class MAYAFLUX_API Generator : public Node {
110public:
111 /**
112 * @brief Virtual destructor for proper cleanup
113 */
114 virtual ~Generator() = default;
115
116 /**
117 * @brief Sets the generator's amplitude
118 * @param amplitude New amplitude value
119 *
120 * This method updates the generator's amplitude setting,
121 * which controls the overall scaling of the generated values.
122 */
123 virtual void set_amplitude(double amplitude);
124
125 /**
126 * @brief Gets the current base amplitude
127 * @return Current amplitude
128 */
129 [[nodiscard]] virtual double get_amplitude() const;
130
131 /**
132 * @brief Gets the current frequency
133 * @return Current frequency in Hz
134 */
135 [[nodiscard]] virtual float get_frequency() const { return m_frequency; }
136
137 /**
138 * @brief Allows RootNode to process the Generator without using the processed sample
139 * @param bMock_process True to mock process, false to process normally
140 *
141 * NOTE: This has no effect on the behaviour of process_sample (or process_batch).
142 * This is ONLY used by the RootNode when processing the node graph.
143 * If the output of the Generator needs to be ignored elsewhere, simply discard the return value.
144 *
145 * Calling process manually can be cumbersome. Using a coroutine just to call process
146 * is overkill. This method allows the RootNode to process the Generator without
147 * using the processed sample, which is useful for mocking processing.
148 */
149 virtual void enable_mock_process(bool mock_process);
150
151 /**
152 * @brief Checks if the generator should mock process
153 * @return True if the generator should mock process, false otherwise
154 */
155 [[nodiscard]] virtual bool should_mock_process() const;
156
157 /**
158 * @brief Updates the context object for callbacks
159 * @param value The current generated sample
160 *
161 * This method creates a specialized context object containing
162 * the current sample value and all oscillator parameters, providing
163 * callbacks with rich information about the oscillator's state.
164 */
165 virtual void update_context(double value) override;
166
167 /**
168 * @brief Sets the generator's frequency
169 * @param frequency New frequency value in Hz
170 *
171 * This method updates the generator's frequency setting,
172 * which controls the rate of oscillation or pattern repetition.
173 */
174 virtual void set_frequency(float frequency);
175
176 /**
177 * @brief Gets the last created context object
178 * @return Reference to the last GeneratorContext object
179 */
180 NodeContext& get_last_context() override;
181
182 /**
183 * @brief Registers a typed callback receiving GeneratorContext directly
184 * @param callback Receives frequency, amplitude, and phase without casting
185 */
186 void on_tick(const TypedHook<GeneratorContext>& callback);
187
188 /**
189 * @brief Registers a conditional typed callback receiving GeneratorContext directly
190 * @param condition Predicate that determines when callback should be triggered
191 * @param callback Receives frequency, amplitude, and phase without casting when condition is met
192
193 */
194 void on_tick_if(const NodeCondition& condition, const TypedHook<GeneratorContext>& callback);
195
196protected:
197 /**
198 * @brief Base amplitude of the generator
199 */
200 double m_amplitude { 1.0 };
201
202 /**
203 * @brief Base frequency of the generator
204 */
205 float m_frequency { 440.0F };
206
207 /**
208 * @brief Current phase of the generator
209 */
210 double m_phase {};
211
212 GeneratorContext m_context { 0., m_frequency, m_amplitude, m_phase };
213 GeneratorContextGpu m_context_gpu { 0., m_frequency, m_amplitude, m_phase, get_gpu_data_buffer() };
214
215 void notify_tick(double value) override;
216};
217
218/**
219 * @brief Sets the generator's amplitude
220 * @param node Generator node to modify
221 * @param value New amplitude value
222 *
223 * This operator allows setting the generator's amplitude using
224 * a more intuitive syntax, such as:
225 *
226 * ```cpp
227 * auto generator = std::make_shared<Sine>(440, 1, 0);
228 * generator * 0.5; // Halves the amplitude
229 * ```
230 */
231MAYAFLUX_API void operator*(const std::shared_ptr<Node>& node, double value);
232
233}
double frequency
GeneratorContextGpu(double value, float frequency, double amplitude, double phase, std::span< const float > gpu_data)
Definition Generator.hpp:67
double amplitude
Current amplitude of the generator.
Definition Generator.hpp:57
double phase
Current phase of the generator.
Definition Generator.hpp:62
GeneratorContext(double value, float frequency, double amplitude, double phase)
Constructs a GeneratorContext with the current generator state.
Definition Generator.hpp:37
float frequency
Current frequency of the generator.
Definition Generator.hpp:52
Specialized context for generator node callbacks.
Definition Generator.hpp:24
virtual float get_frequency() const
Gets the current frequency.
virtual ~Generator()=default
Virtual destructor for proper cleanup.
Base class for all signal and pattern generators in Maya Flux.
GPU-uploadable 1D array data interface.
Base context class for node callbacks.
Definition Node.hpp:53
Base interface for all computational processing nodes.
Definition Node.hpp:126
void operator*(const std::shared_ptr< Node > &node, double value)
Sets the generator's amplitude.
Definition Generator.cpp:59
std::function< void(ContextT &)> TypedHook
Callback function type for node processing events, parameterised on context type.
Definition NodeUtils.hpp:28
std::function< bool(NodeContext &)> NodeCondition
Predicate function type for conditional callbacks.
Definition NodeUtils.hpp:54