MayaFlux 0.5.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)
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)
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 Updates the context object for callbacks
139 * @param value The current generated sample
140 *
141 * This method creates a specialized context object containing
142 * the current sample value and all oscillator parameters, providing
143 * callbacks with rich information about the oscillator's state.
144 */
145 virtual void update_context(double value) override;
146
147 /**
148 * @brief Sets the generator's frequency
149 * @param frequency New frequency value in Hz
150 *
151 * This method updates the generator's frequency setting,
152 * which controls the rate of oscillation or pattern repetition.
153 */
154 virtual void set_frequency(float frequency);
155
156 /**
157 * @brief Gets the last created context object
158 * @return Reference to the last GeneratorContext object
159 */
160 NodeContext& get_last_context() override;
161
162 /**
163 * @brief Registers a typed callback receiving GeneratorContext directly
164 * @param callback Receives frequency, amplitude, and phase without casting
165 */
166 void on_tick(const TypedHook<GeneratorContext>& callback);
167
168 /**
169 * @brief Registers a conditional typed callback receiving GeneratorContext directly
170 * @param condition Predicate that determines when callback should be triggered
171 * @param callback Receives frequency, amplitude, and phase without casting when condition is met
172
173 */
174 void on_tick_if(const NodeCondition& condition, const TypedHook<GeneratorContext>& callback);
175
176protected:
177 /**
178 * @brief Base amplitude of the generator
179 */
180 double m_amplitude { 1.0 };
181
182 /**
183 * @brief Base frequency of the generator
184 */
185 float m_frequency { 440.0F };
186
187 /**
188 * @brief Current phase of the generator
189 */
190 double m_phase {};
191
192 GeneratorContext m_context { 0., m_frequency, m_amplitude, m_phase };
193 GeneratorContextGpu m_context_gpu { 0., m_frequency, m_amplitude, m_phase, get_gpu_data_buffer() };
194
195 void notify_tick(double value) override;
196};
197
198/**
199 * @brief Sets the generator's amplitude
200 * @param node Generator node to modify
201 * @param value New amplitude value
202 *
203 * This operator allows setting the generator's amplitude using
204 * a more intuitive syntax, such as:
205 *
206 * ```cpp
207 * auto generator = std::make_shared<Sine>(440, 1, 0);
208 * generator * 0.5; // Halves the amplitude
209 * ```
210 */
211MAYAFLUX_API void operator*(const std::shared_ptr<Node>& node, double value);
212
213}
double frequency
float value
uint32_t phase
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:45
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