MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Nodes/Generators/Random.hpp
Go to the documentation of this file.
1#pragma once
2#include "Generator.hpp"
3
5
7
8/**
9 * @class RandomContext
10 * @brief Specialized context for stochastic generator callbacks
11 *
12 * RandomContext extends the base NodeContext to provide detailed information
13 * about a stochastic generator's current state to callbacks. It includes the
14 * current distribution type, amplitude scaling, range parameters, and statistical
15 * configuration values.
16 *
17 * This rich context enables callbacks to perform sophisticated analysis and
18 * monitoring of stochastic behavior, such as:
19 * - Tracking statistical properties of generated sequences
20 * - Implementing adaptive responses to emergent patterns
21 * - Visualizing probability distributions in real-time
22 * - Creating cross-domain mappings based on stochastic properties
23 * - Detecting and responding to specific statistical conditions
24 */
25class MAYAFLUX_API RandomContext : public NodeContext {
26public:
27 /**
28 * @brief Constructs a RandomContext with the current generator state
29 * @param value Current output sample value
30 * @param type Current probability distribution algorithm
31 * @param amplitude Current scaling factor for output values
32 * @param range_start Lower bound of the current output range
33 * @param range_end Upper bound of the current output range
34 * @param normal_spread Variance parameter for normal distribution
35 *
36 * Creates a context object that provides a complete snapshot of the
37 * stochastic generator's current state, including its most recent output
38 * value and all parameters that define its statistical behavior.
39 */
40 RandomContext(double value, Kinesis::Stochastic::Algorithm type, double amplitude,
41 double range_start, double range_end, double normal_spread)
42 : NodeContext(value, typeid(RandomContext).name())
43 , distribution_type(type)
44 , amplitude(amplitude)
45 , range_start(range_start)
46 , range_end(range_end)
47 , normal_spread(normal_spread)
48 {
49 }
50
51 /**
52 * @brief Current distribution type
53 *
54 * Identifies which probability algorithm is currently active in the
55 * generator (uniform, normal, exponential, etc.). This determines the
56 * fundamental statistical properties of the generated sequence.
57 */
59
60 /**
61 * @brief Current amplitude scaling factor
62 */
63 double amplitude;
64
65 /**
66 * @brief Current lower bound of the range
67 */
69
70 /**
71 * @brief Current upper bound of the range
72 */
73 double range_end;
74
75 /**
76 * @brief Current variance parameter for normal distribution
77 */
79};
80
81/**
82 * @class RandomContextGpu
83 */
84class MAYAFLUX_API RandomContextGpu : public RandomContext, public GpuVectorData {
85public:
87 double value,
89 double amplitude,
90 double range_start,
91 double range_end,
92 double normal_spread,
93 std::span<const float> gpu_data)
94 : RandomContext(value, type, amplitude, range_start, range_end, normal_spread)
95 , GpuVectorData(gpu_data)
96 {
97 type_id = typeid(RandomContextGpu).name();
98 }
99};
100
101/**
102 * @class Random
103 * @brief Node wrapper for Kinesis::Stochastic - signal-rate stochastic generation
104 *
105 * Provides continuous stochastic signal generation integrated with the node graph system.
106 * This is a thin adapter that connects the core Kinesis::Stochastic infrastructure to
107 * the processing graph, adding amplitude scaling, callbacks, and GPU context support.
108 *
109 * For direct mathematical usage outside the node system, use Kinesis::Stochastic::Stochastic directly.
110 */
111class MAYAFLUX_API Random : public Generator {
112public:
113 /**
114 * @brief Constructor for the stochastic generator
115 * @param type Distribution type to use (default: uniform distribution)
116 *
117 * Creates a stochastic generator with the specified probability distribution.
118 * The generator is initialized with entropy from the system's
119 * random device for non-deterministic behavior across program executions.
120 */
121 Random(Kinesis::Stochastic::Algorithm type = Kinesis::Stochastic::Algorithm::UNIFORM);
122
123 ~Random() override = default;
124
125 /**
126 * @brief Changes the probability distribution type
127 * @param type New distribution type to use
128 */
129 void set_type(Kinesis::Stochastic::Algorithm type);
130
131 /**
132 * @brief Configures distribution parameters
133 * @param key Parameter name
134 * @param value Parameter value
135 *
136 * Allows dynamic adjustment of distribution-specific parameters,
137 * such as mean and standard deviation for a normal distribution,
138 * or lambda for an exponential distribution.
139 */
140 inline void configure(const std::string& key, std::any value) { m_generator.configure(key, std::move(value)); }
141
142 /**
143 * @brief Generates a single stochastic value
144 * @param input Input value (can be used for distribution modulation)
145 * @return Generated stochastic value
146 *
147 * This method generates a single value according to the current
148 * distribution settings. The input parameter can be used to modulate
149 * or influence the distribution in advanced applications.
150 */
151 double process_sample(double input = 0.) override;
152
153 /**
154 * @brief Generates multiple stochastic values at once
155 * @param num_samples Number of values to generate
156 * @return Vector of generated values
157 *
158 * This method efficiently generates multiple values in a single operation,
159 * useful for batch processing or filling buffers.
160 */
161 std::vector<double> process_batch(unsigned int num_samples) override;
162
163 /**
164 * @brief Sets the variance parameter for normal distribution
165 * @param spread New variance value
166 *
167 * For normal (Gaussian) distribution, this controls the statistical
168 * variance of the distribution. Higher values create greater entropy
169 * with more values in the extremes, while lower values concentrate
170 * values toward the center of the distribution.
171 */
172 void set_normal_spread(double spread);
173
174 /**
175 * @brief Sets the output value range
176 * @param start Lower bound of the range
177 * @param end Upper bound of the range
178 *
179 * Defines the minimum and maximum values that the generator can produce.
180 * The actual output values will be scaled to fit within this specified range.
181 */
182 void set_range(double start, double end);
183
184 void printGraph() override { }
185 void printCurrent() override { }
186 void save_state() override { }
187 void restore_state() override { }
188
189protected:
190 /**
191 * @brief Updates the context object with the current node state
192 * @param value The current sample value
193 *
194 * This method is responsible for updating the NodeContext object
195 * with the latest state information from the node. It is called
196 * internally whenever a new output value is produced, ensuring that
197 * the context reflects the current state of the node for use in callbacks.
198 */
199 void update_context(double value) override;
200
201 /**
202 * @brief Notifies all registered callbacks about a new value
203 * @param value The newly generated value
204 *
205 * This method is called internally whenever a new value is generated,
206 * creating the appropriate context and invoking all registered callbacks
207 * that should receive notification about this value.
208 */
209 void notify_tick(double value) override;
210
211 NodeContext& get_last_context() override;
212
213private:
214 /**
215 * @brief Core stochastic generator instance
216 */
218 /**
219 * @brief Current probability distribution algorithm
220 */
222
223 /**
224 * @brief Lower bound of the current output range
225 */
226 double m_current_start { -1.0 };
227
228 /**
229 * @brief Upper bound of the current output range
230 */
231 double m_current_end { 1.0 };
232
233 /**
234 * @brief Variance parameter for normal distribution
235 *
236 * Controls the statistical spread in normal distribution.
237 * Higher values increase entropy and distribution width.
238 */
239 double m_normal_spread { 1.0 };
240
243};
244
245} // namespace MayaFlux::Nodes::Generator
Unified generative infrastructure for stochastic and procedural algorithms.
Base class for all signal and pattern generators in Maya Flux.
RandomContextGpu(double value, Kinesis::Stochastic::Algorithm type, double amplitude, double range_start, double range_end, double normal_spread, std::span< const float > gpu_data)
RandomContext(double value, Kinesis::Stochastic::Algorithm type, double amplitude, double range_start, double range_end, double normal_spread)
Constructs a RandomContext with the current generator state.
double normal_spread
Current variance parameter for normal distribution.
double amplitude
Current amplitude scaling factor.
double range_end
Current upper bound of the range.
double range_start
Current lower bound of the range.
Kinesis::Stochastic::Algorithm distribution_type
Current distribution type.
Specialized context for stochastic generator callbacks.
void printGraph() override
Prints a visual representation of the generated pattern.
void printCurrent() override
Prints the current state and parameters of the generator.
Kinesis::Stochastic::Stochastic m_generator
Core stochastic generator instance.
Kinesis::Stochastic::Algorithm m_type
Current probability distribution algorithm.
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
void restore_state() override
Restores the node's state from the last save Recursively cascades through all connected modulator nod...
void configure(const std::string &key, std::any value)
Configures distribution parameters.
Node wrapper for Kinesis::Stochastic - signal-rate stochastic generation.
GPU-uploadable 1D array data interface.
Base context class for node callbacks.
Definition Node.hpp:30
Algorithm
Stochastic and procedural generation algorithms.