MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Stochastic.cpp
Go to the documentation of this file.
1#include "Stochastic.hpp"
2
4
6 : m_random_engine(std::random_device {}())
7 , m_current_start(-1.0F)
8 , m_current_end(1.0F)
9 , m_normal_spread(4.0F)
10 , m_type(type)
11{
12}
13
20
21double Random::random_sample(double start, double end)
22{
23 validate_range(start, end);
24 m_current_start = start;
25 m_current_end = end;
27}
28
29std::vector<double> Random::random_array(double start, double end, unsigned int num_samples)
30{
31 validate_range(start, end);
32 m_current_start = start;
33 m_current_end = end;
34
35 std::vector<double> samples;
36 samples.reserve(num_samples);
37
38 for (unsigned int i = 0; i < num_samples; ++i) {
39 samples.push_back(transform_sample(generate_distributed_sample(), start, end) * m_amplitude);
40 }
41
42 return samples;
43}
44
45std::vector<double> Random::process_batch(unsigned int num_samples)
46{
47 return random_array(m_current_start, m_current_end, num_samples);
48}
49
51{
52 switch (m_type) {
54 std::uniform_real_distribution<double> dist(m_current_start, m_current_end);
55 return dist(m_random_engine);
56 }
58 const double range = m_current_end - m_current_start;
59 std::normal_distribution<double> dist(0.0, range / m_normal_spread);
60 return dist(m_random_engine);
61 }
63 std::exponential_distribution<double> dist(1.0);
64 return dist(m_random_engine);
65 }
67 std::poisson_distribution<int> dist(m_current_end - m_current_start);
68 return static_cast<double>(dist(m_random_engine));
69 }
70 default:
71 throw std::invalid_argument("Invalid distribution type");
72 }
73}
74
75double Random::transform_sample(double sample, double start, double end) const
76{
78 sample = std::max(start, std::min(end, sample));
80 sample /= end;
81 sample = start + sample * (end - start);
82 }
83 return sample;
84}
85
86void Random::validate_range(double start, double end) const
87{
88 if (start > end) {
89 throw std::invalid_argument("Start must be less than or equal to end");
90 }
91}
92
93std::unique_ptr<NodeContext> Random::create_context(double value)
94{
95 if (m_gpu_compatible) {
96 return std::make_unique<StochasticContextGpu>(
97 value,
98 m_type,
104 }
105 return std::make_unique<StochasticContext>(value, m_type, m_amplitude, m_current_start, m_current_end, m_normal_spread);
106}
107
108void Random::notify_tick(double value)
109{
111 for (auto& callback : m_callbacks) {
112 callback(*m_last_context);
113 }
114 for (auto& [callback, condition] : m_conditional_callbacks) {
115 if (condition(*m_last_context)) {
116 callback(*m_last_context);
117 }
118 }
119}
120
122{
123 // When opengl, vulkan or sciplot plugged in
124}
125
127{
128 // When opengl, vulkan or sciplot plugged in
129}
130}
double m_amplitude
Base amplitude of the generator.
Utils::distribution m_type
Current probability distribution algorithm.
void validate_range(double start, double end) const
Validates that the specified range is mathematically valid.
double m_normal_spread
Variance parameter for normal distribution.
std::vector< double > process_batch(unsigned int num_samples) override
Generates multiple stochastic values at once.
std::vector< double > random_array(double start, double end, unsigned int num_samples)
Generates an array of stochastic values within a specified range.
std::mt19937 m_random_engine
Mersenne Twister entropy generator.
double transform_sample(double sample, double start, double end) const
Transforms a raw value to fit within the specified range.
std::unique_ptr< NodeContext > create_context(double value) override
Creates a context object for callbacks.
void printGraph() override
Visualizes the distribution characteristics.
Random(Utils::distribution type=Utils::distribution::UNIFORM)
Constructor for the stochastic generator.
Definition Stochastic.cpp:5
double m_current_start
Lower bound of the current output range.
double generate_distributed_sample()
Generates a raw value according to the current distribution.
double m_current_end
Upper bound of the current output range.
double random_sample(double start, double end)
Generates a stochastic value within a specified range.
void printCurrent() override
Outputs the current configuration parameters.
void notify_tick(double value) override
Notifies all registered callbacks about a new value.
double process_sample(double input=0.) override
Generates a single stochastic value.
std::vector< NodeHook > m_callbacks
Collection of standard callback functions.
Definition Node.hpp:416
double m_last_output
The most recent sample value generated by this oscillator.
Definition Node.hpp:378
std::vector< std::pair< NodeHook, NodeCondition > > m_conditional_callbacks
Collection of conditional callback functions with their predicates.
Definition Node.hpp:426
bool m_gpu_compatible
Flag indicating if the node supports GPU processing This flag is set by derived classes to indicate w...
Definition Node.hpp:387
std::unique_ptr< NodeContext > m_last_context
The last context object created for callbacks.
Definition Node.hpp:396
std::span< const float > get_gpu_data_buffer() const
Provides access to the GPU data buffer.
Definition Node.cpp:78