MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Phasor.cpp
Go to the documentation of this file.
1#include "Phasor.hpp"
2
4
5Phasor::Phasor(float frequency, double amplitude, float offset)
6 : m_offset(offset)
7 , m_frequency_modulator(nullptr)
8 , m_amplitude_modulator(nullptr)
9 , m_phase_wrapped(false)
10 , m_threshold_crossed(false)
11{
12 m_amplitude = amplitude;
15}
16
17Phasor::Phasor(const std::shared_ptr<Node>& frequency_modulator, float frequency, double amplitude, float offset)
18 : m_offset(offset)
19 , m_frequency_modulator(frequency_modulator)
20 , m_amplitude_modulator(nullptr)
21 , m_phase_wrapped(false)
22 , m_threshold_crossed(false)
23{
24 m_amplitude = amplitude;
27}
28
29Phasor::Phasor(float frequency, const std::shared_ptr<Node>& amplitude_modulator, double amplitude, float offset)
30 : m_offset(offset)
31 , m_frequency_modulator(nullptr)
32 , m_amplitude_modulator(amplitude_modulator)
33 , m_phase_wrapped(false)
34 , m_threshold_crossed(false)
35{
36 m_amplitude = amplitude;
39}
40
41Phasor::Phasor(const std::shared_ptr<Node>& frequency_modulator, const std::shared_ptr<Node>& amplitude_modulator,
42 float frequency, double amplitude, float offset)
43 : m_offset(offset)
44 , m_frequency_modulator(frequency_modulator)
45 , m_amplitude_modulator(amplitude_modulator)
46 , m_phase_wrapped(false)
47 , m_threshold_crossed(false)
48{
49 m_amplitude = amplitude;
52}
53
59
64
65void Phasor::set_frequency_modulator(const std::shared_ptr<Node>& modulator)
66{
67 m_frequency_modulator = modulator;
68}
69
70void Phasor::set_amplitude_modulator(const std::shared_ptr<Node>& modulator)
71{
72 m_amplitude_modulator = modulator;
73}
74
76{
77 m_frequency_modulator = nullptr;
78 m_amplitude_modulator = nullptr;
79}
80
82{
83 double output = 0.0;
84 m_phase_wrapped = false;
85
86 double effective_freq = m_frequency;
87
90 uint32_t state = m_frequency_modulator->m_state.load();
91 if (state & NodeState::PROCESSED) {
92 effective_freq += m_frequency_modulator->get_last_output();
93 } else {
94 effective_freq = m_frequency_modulator->process_sample(0.F);
96 }
97
98 update_phase_increment(effective_freq);
99 }
100
101 output = m_phase * m_amplitude;
102
105 uint32_t state = m_amplitude_modulator->m_state.load();
106 if (state & NodeState::PROCESSED) {
107 output *= m_amplitude_modulator->get_last_output();
108 } else {
109 output *= m_amplitude_modulator->process_sample(0.F);
111 }
112 }
113
114 output += m_offset;
115
117 if (m_phase >= 1.0) {
118 m_phase -= 1.0;
119 m_phase_wrapped = true;
120 }
121
122 m_last_output = output;
123
125 && !m_networked_node) {
126 notify_tick(output);
127 }
128
132 }
136 }
137
138 return output;
139}
140
141std::vector<double> Phasor::process_batch(unsigned int num_samples)
142{
143 std::vector<double> output(num_samples, 0.0);
144
145 for (unsigned int i = 0; i < num_samples; ++i) {
146 output[i] = process_sample(0.0); // Use 0.0 as default input
147 }
148
149 return output;
150}
151
152void Phasor::reset(float frequency, float amplitude, float offset, double phase)
153{
155 m_amplitude = amplitude;
157 m_phase = phase;
158
159 while (m_phase >= 1.0)
160 m_phase -= 1.0;
161 while (m_phase < 0.0)
162 m_phase += 1.0;
163
165 m_last_output = 0.0;
166}
167
172
174{
175 return m_threshold_callbacks.add_unique(adapt_hook(callback), callback.target_type(), 0, { threshold, 0.0 });
176}
177
182
187
189{
191
192 auto& ctx = get_last_context();
193
194 m_callbacks.fire(ctx);
195
196 if (m_phase_wrapped) {
198 }
199
201
202 for (const auto& entry : m_threshold_callbacks.snapshot()) {
203 const double threshold = entry.params[0];
205 entry.hook(ctx);
206 m_threshold_crossed = true;
207 } else if (value < threshold) {
208 m_threshold_crossed = false;
209 }
210 }
211}
212
228
244
245std::vector<std::pair<ModulatorRole, std::shared_ptr<Node>>>
247{
248 std::vector<std::pair<ModulatorRole, std::shared_ptr<Node>>> result;
253 return result;
254}
255
256} // namespace MayaFlux::Nodes::Generator
uint64_t offset
Definition Composite.cpp:10
Core::GlobalInputConfig input
Definition Config.cpp:42
PickCallback callback
Definition Picker.cpp:22
double frequency
glm::vec3 value
float threshold
float m_frequency
Base frequency of the generator.
NodeContext & get_last_context() override
Gets the last created context object.
Definition Generator.cpp:37
double m_amplitude
Base amplitude of the generator.
virtual void update_context(double value) override
Updates the context object for callbacks.
Definition Generator.cpp:7
double m_phase
Current phase of the generator.
HookId on_phase_wrap(const TypedHook< GeneratorContext > &callback)
Registers a callback for every phase wrap.
Definition Phasor.cpp:168
std::shared_ptr< Node > m_frequency_modulator
Node that modulates the frequency.
Definition Phasor.hpp:279
void set_amplitude_modulator(const std::shared_ptr< Node > &modulator)
Sets a node to modulate the generator's amplitude.
Definition Phasor.cpp:70
HookId on_threshold(const TypedHook< GeneratorContext > &callback, double threshold, bool rising=true)
Registers a callback for every time the phasor crosses a threshold.
Definition Phasor.cpp:173
bool m_threshold_crossed
Flag indicating whether the threshold has been crossed in the current sample.
Definition Phasor.hpp:313
void restore_state() override
Restores the node's state from the last save Recursively cascades through all connected modulator nod...
Definition Phasor.cpp:229
bool m_phase_wrapped
Flag indicating whether the phase has wrapped in the current sample.
Definition Phasor.hpp:308
void set_frequency(float frequency) override
Sets the generator's frequency.
Definition Phasor.cpp:54
bool remove_hook(const TypedHook< GeneratorContext > &callback)
Removes a previously registered callback.
Definition Phasor.cpp:183
std::vector< std::pair< ModulatorRole, std::shared_ptr< Node > > > get_modulators() const override
Retrieves the current modulators connected to this node.
Definition Phasor.cpp:246
HookList m_threshold_callbacks
Collection of threshold-specific callback functions with their thresholds.
Definition Phasor.hpp:303
std::shared_ptr< Node > m_amplitude_modulator
Node that modulates the amplitude.
Definition Phasor.hpp:284
std::vector< double > process_batch(unsigned int num_samples) override
Processes multiple samples at once.
Definition Phasor.cpp:141
void update_phase_increment(double frequency)
Updates the phase increment based on a new frequency.
Definition Phasor.cpp:60
double m_phase_inc
Phase increment per sample.
Definition Phasor.hpp:269
void notify_tick(double value) override
Notifies all registered callbacks about a new sample.
Definition Phasor.cpp:188
void reset(float frequency=1, float amplitude=1.0, float offset=0, double phase=0.0)
Resets the generator's phase and parameters.
Definition Phasor.cpp:152
HookList m_phase_wrap_callbacks
Collection of phase wrap-specific callback functions.
Definition Phasor.hpp:298
void set_frequency_modulator(const std::shared_ptr< Node > &modulator)
Sets a node to modulate the generator's frequency.
Definition Phasor.cpp:65
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
Definition Phasor.cpp:213
Phasor(float frequency=1, double amplitude=1, float offset=0)
Basic constructor with fixed parameters.
Definition Phasor.cpp:5
double process_sample(double input=0.) override
Processes a single input sample and generates a phasor sample.
Definition Phasor.cpp:81
bool remove_threshold_callback(const TypedHook< GeneratorContext > &callback)
Removes a previously registered phase wrap callback.
Definition Phasor.cpp:178
void clear_modulators()
Removes all modulation connections.
Definition Phasor.cpp:75
float m_offset
DC offset added to the output.
Definition Phasor.hpp:274
Snapshot snapshot() const
The current entries, held stable for the lifetime of the returned object.
Definition NodeUtils.cpp:43
void fire(NodeContext &ctx) const
Calls every entry whose condition, if any, holds.
Definition NodeUtils.cpp:68
bool remove_first_by_key(const std::type_info &key)
Removes the first entry registered with this callable type.
HookId add_unique(NodeHook hook, const std::type_info &key, uint8_t tag=0, std::array< double, 2 > params={})
Appends unless an entry with the same key and params exists, in which case that entry's id is returne...
HookList m_conditional_callbacks
Hooks fired on a tick only while their condition holds.
Definition Node.hpp:485
bool m_state_saved
tracks if the node's state has been saved by a snapshot operation
Definition Node.hpp:498
bool m_networked_node
Flag indicating if the node is part of a NodeNetwork This flag is used to disable event firing when t...
Definition Node.hpp:493
double m_last_output
The most recent sample value generated by this oscillator.
Definition Node.hpp:441
HookList m_callbacks
Hooks fired on every tick.
Definition Node.hpp:478
bool m_fire_events_during_snapshot
Internal flag controlling whether notify_tick fires during state snapshots Default: false (events don...
Definition Node.hpp:528
uint32_t m_timing_rate
Current timing rate for the node, used for timing calculations (can be sample rate or frame rate)
Definition Node.hpp:504
@ PROCESSED
Node has been processed this cycle.
Definition NodeSpec.hpp:49
void try_reset_processed_state(std::shared_ptr< Node > node)
Attempts to reset the processed state of a node.
void atomic_inc_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically increments the modulator count by a specified amount.
uint64_t HookId
Identifies one registration inside a HookList.
Definition NodeUtils.hpp:60
void atomic_dec_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically decrements the modulator count by a specified amount.
std::function< void(ContextT &)> TypedHook
Callback function type for node processing events, parameterised on context type.
Definition NodeUtils.hpp:28
NodeHook adapt_hook(const TypedHook< ContextT > &hook)
Wraps a context-typed hook into a NodeHook.
void atomic_add_flag(std::atomic< NodeState > &state, NodeState flag)
Atomically adds a flag to a node state.