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)
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 }
98};
99
100/**
101 * @class Random
102 * @brief Node wrapper for Kinesis::Stochastic - signal-rate stochastic generation
103 *
104 * Provides continuous stochastic signal generation integrated with the node graph system.
105 * This is a thin adapter that connects the core Kinesis::Stochastic infrastructure to
106 * the processing graph, adding amplitude scaling, callbacks, and GPU context support.
107 *
108 * For direct mathematical usage outside the node system, use Kinesis::Stochastic::Stochastic directly.
109 */
110class MAYAFLUX_API Random : public Generator {
111public:
112 /**
113 * @brief Constructor for the stochastic generator
114 * @param type Distribution type to use (default: uniform distribution)
115 *
116 * Creates a stochastic generator with the specified probability distribution.
117 * The generator is initialized with entropy from the system's
118 * random device for non-deterministic behavior across program executions.
119 */
120 Random(Kinesis::Stochastic::Algorithm type = Kinesis::Stochastic::Algorithm::UNIFORM);
121
122 ~Random() override = default;
123
124 /**
125 * @brief Changes the probability distribution type
126 * @param type New distribution type to use
127 */
128 void set_type(Kinesis::Stochastic::Algorithm type);
129
130 /**
131 * @brief Configures distribution parameters
132 * @param key Parameter name
133 * @param value Parameter value
134 *
135 * Allows dynamic adjustment of distribution-specific parameters,
136 * such as mean and standard deviation for a normal distribution,
137 * or lambda for an exponential distribution.
138 */
139 inline void configure(const std::string& key, std::any value) { m_generator.configure(key, std::move(value)); }
140
141 /**
142 * @brief Generates a single stochastic value
143 * @param input Input value (can be used for distribution modulation)
144 * @return Generated stochastic value
145 *
146 * This method generates a single value according to the current
147 * distribution settings. The input parameter can be used to modulate
148 * or influence the distribution in advanced applications.
149 */
150 double process_sample(double input = 0.) override;
151
152 /**
153 * @brief Generates multiple stochastic values at once
154 * @param num_samples Number of values to generate
155 * @return Vector of generated values
156 *
157 * This method efficiently generates multiple values in a single operation,
158 * useful for batch processing or filling buffers.
159 */
160 std::vector<double> process_batch(unsigned int num_samples) override;
161
162 /**
163 * @brief Sets the variance parameter for normal distribution
164 * @param spread New variance value
165 *
166 * For normal (Gaussian) distribution, this controls the statistical
167 * variance of the distribution. Higher values create greater entropy
168 * with more values in the extremes, while lower values concentrate
169 * values toward the center of the distribution.
170 */
171 void set_normal_spread(double spread);
172
173 /**
174 * @brief Sets the output value range
175 * @param start Lower bound of the range
176 * @param end Upper bound of the range
177 *
178 * Defines the minimum and maximum values that the generator can produce.
179 * The actual output values will be scaled to fit within this specified range.
180 */
181 void set_range(double start, double end);
182
183 void save_state() override { }
184 void restore_state() override { }
185
186protected:
187 /**
188 * @brief Updates the context object with the current node state
189 * @param value The current sample value
190 *
191 * This method is responsible for updating the NodeContext object
192 * with the latest state information from the node. It is called
193 * internally whenever a new output value is produced, ensuring that
194 * the context reflects the current state of the node for use in callbacks.
195 */
196 void update_context(double value) override;
197
198 NodeContext& get_last_context() override;
199
200private:
201 /**
202 * @brief Core stochastic generator instance
203 */
205 /**
206 * @brief Current probability distribution algorithm
207 */
209
210 /**
211 * @brief Lower bound of the current output range
212 */
213 double m_current_start { -1.0 };
214
215 /**
216 * @brief Upper bound of the current output range
217 */
218 double m_current_end { 1.0 };
219
220 /**
221 * @brief Variance parameter for normal distribution
222 *
223 * Controls the statistical spread in normal distribution.
224 * Higher values increase entropy and distribution width.
225 */
226 double m_normal_spread { 1.0 };
227
230};
231
232} // namespace MayaFlux::Nodes::Generator
Core::GlobalInputConfig input
Definition Config.cpp:36
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.
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:53
Algorithm
Stochastic and procedural generation algorithms.