MayaFlux 0.2.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, typeid(GeneratorContext).name())
39 , frequency(frequency)
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 type_id = typeid(GeneratorContextGpu).name();
77 }
78};
79
80/**
81 * @class Generator
82 * @brief Base class for all signal and pattern generators in Maya Flux
83 *
84 * Generators are specialized nodes that create numerical sequences from mathematical principles,
85 * rather than processing existing signals. They form the foundation of the
86 * computational graph by providing the initial patterns that other nodes
87 * can then transform, filter, or combine.
88 *
89 * Unlike transformation nodes that modify input signals, generators typically:
90 * - Create sequences based on mathematical formulas or algorithms
91 * - Maintain internal state to track progression, phase, or other parameters
92 * - Can operate autonomously without any input (though they may accept modulation inputs)
93 * - Serve as the origin points in computational processing networks
94 *
95 * Common types of generators include:
96 * - Oscillators (sine, square, sawtooth, triangle waves)
97 * - Stochastic generators (various probability distributions)
98 * - Sample-based generators (playing back recorded sequences)
99 * - Envelope generators (creating amplitude contours)
100 *
101 * Generators integrate with the node graph system, allowing them to be:
102 * - Connected to other nodes using operators like '>>' (chain)
103 * - Combined with other nodes using operators like '+' (mix)
104 * - Registered with a RootNode for processing
105 * - Used as modulation sources for other generators or transformations
106 *
107 * The Generator class extends the base Node interface with additional
108 * methods for visualization and analysis of the generated patterns.
109 */
110class MAYAFLUX_API Generator : public Node {
111public:
112 /**
113 * @brief Virtual destructor for proper cleanup
114 */
115 virtual ~Generator() = default;
116
117 /**
118 * @brief Sets the generator's amplitude
119 * @param amplitude New amplitude value
120 *
121 * This method updates the generator's amplitude setting,
122 * which controls the overall scaling of the generated values.
123 */
124 virtual void set_amplitude(double amplitude);
125
126 /**
127 * @brief Gets the current base amplitude
128 * @return Current amplitude
129 */
130 [[nodiscard]] virtual double get_amplitude() const;
131
132 /**
133 * @brief Allows RootNode to process the Generator without using the processed sample
134 * @param bMock_process True to mock process, false to process normally
135 *
136 * NOTE: This has no effect on the behaviour of process_sample (or process_batch).
137 * This is ONLY used by the RootNode when processing the node graph.
138 * If the output of the Generator needs to be ignored elsewhere, simply discard the return value.
139 *
140 * Calling process manually can be cumbersome. Using a coroutine just to call process
141 * is overkill. This method allows the RootNode to process the Generator without
142 * using the processed sample, which is useful for mocking processing.
143 */
144 virtual void enable_mock_process(bool mock_process);
145
146 /**
147 * @brief Checks if the generator should mock process
148 * @return True if the generator should mock process, false otherwise
149 */
150 [[nodiscard]] virtual bool should_mock_process() const;
151
152 /**
153 * @brief Prints a visual representation of the generated pattern
154 *
155 * This method should output a visual representation of the
156 * generator's output over time, useful for analysis and
157 * understanding the pattern characteristics.
158 */
159 virtual void printGraph() = 0;
160
161 /**
162 * @brief Prints the current state and parameters of the generator
163 *
164 * This method should output the current configuration and state
165 * of the generator, including parameters like frequency, amplitude,
166 * phase, or any other generator-specific settings.
167 */
168 virtual void printCurrent() = 0;
169
170 /**
171 * @brief Updates the context object for callbacks
172 * @param value The current generated sample
173 *
174 * This method creates a specialized context object containing
175 * the current sample value and all oscillator parameters, providing
176 * callbacks with rich information about the oscillator's state.
177 */
178 virtual void update_context(double value) override;
179
180 /**
181 * @brief Sets the generator's frequency
182 * @param frequency New frequency value in Hz
183 *
184 * This method updates the generator's frequency setting,
185 * which controls the rate of oscillation or pattern repetition.
186 */
187 virtual void set_frequency(float frequency);
188
189 /**
190 * @brief Gets the last created context object
191 * @return Reference to the last GeneratorContext object
192 */
193 NodeContext& get_last_context() override;
194
195protected:
196 /**
197 * @brief Base amplitude of the generator
198 */
199 double m_amplitude { 1.0 };
200
201 /**
202 * @brief Base frequency of the generator
203 */
204 float m_frequency { 440.0F };
205
206 /**
207 * @brief Current phase of the generator
208 */
209 double m_phase {};
210
211 GeneratorContext m_context { 0., m_frequency, m_amplitude, m_phase };
212 GeneratorContextGpu m_context_gpu { 0., m_frequency, m_amplitude, m_phase, get_gpu_data_buffer() };
213};
214
215}
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 ~Generator()=default
Virtual destructor for proper cleanup.
virtual void printCurrent()=0
Prints the current state and parameters of the generator.
virtual void printGraph()=0
Prints a visual representation of the generated pattern.
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:30
Base interface for all computational processing nodes.
Definition Node.hpp:109