MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Impulse.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Generator.hpp"
4
6
7/**
8 * @class Impulse
9 * @brief Impulse generator node
10 *
11 * The Impulse class generates a single spike followed by zeros, which is a fundamental
12 * signal used in digital signal processing. It produces a value of 1.0 (or specified amplitude)
13 * at specified intervals, and 0.0 elsewhere.
14 *
15 * Key features:
16 * - Configurable frequency, amplitude, and DC offset
17 * - Support for frequency modulation (changing impulse rate)
18 * - Support for amplitude modulation (changing impulse height)
19 * - Automatic registration with the node graph manager (optional)
20 * - Precise timing control for impulse generation
21 *
22 * Impulse generators are used extensively in audio and signal processing for:
23 * - Triggering events at specific intervals
24 * - Testing system responses (impulse response)
25 * - Creating click trains and metronome-like signals
26 * - Serving as the basis for more complex event-based generators
27 * - Synchronization signals for other generators
28 *
29 * The implementation uses a phase accumulation approach similar to other oscillators
30 * but only outputs a non-zero value at the beginning of each cycle.
31 */
32class MAYAFLUX_API Impulse : public Generator, public std::enable_shared_from_this<Impulse> {
33public:
34 /**
35 * @brief Basic constructor with fixed parameters
36 * @param frequency Impulse repetition rate in Hz (default: 1Hz, one impulse per second)
37 * @param amplitude Impulse amplitude (default: 1.0)
38 * @param offset DC offset added to the output (default: 0.0)
39 *
40 * Creates an impulse generator with fixed frequency and amplitude.
41 */
42 Impulse(float frequency = 1, double amplitude = 1, float offset = 0);
43
44 /**
45 * @brief Constructor with frequency modulation
46 * @param frequency_modulator Node that modulates the frequency
47 * @param frequency Base frequency in Hz (default: 1Hz)
48 * @param amplitude Impulse amplitude (default: 1.0)
49 * @param offset DC offset added to the output (default: 0.0)
50 *
51 * Creates an impulse generator with frequency modulation, where the actual frequency
52 * is the base frequency plus the output of the modulator node.
53 */
54 Impulse(const std::shared_ptr<Node>& frequency_modulator, float frequency = 1, double amplitude = 1, float offset = 0);
55
56 /**
57 * @brief Constructor with amplitude modulation
58 * @param frequency Impulse repetition rate in Hz
59 * @param amplitude_modulator Node that modulates the amplitude
60 * @param amplitude Base amplitude (default: 1.0)
61 * @param offset DC offset added to the output (default: 0.0)
62 *
63 * Creates an impulse generator with amplitude modulation, where the actual amplitude
64 * is the base amplitude multiplied by the output of the modulator node.
65 */
66 Impulse(float frequency, const std::shared_ptr<Node>& amplitude_modulator, double amplitude = 1, float offset = 0);
67
68 /**
69 * @brief Constructor with both frequency and amplitude modulation
70 * @param frequency_modulator Node that modulates the frequency
71 * @param amplitude_modulator Node that modulates the amplitude
72 * @param frequency Base frequency in Hz (default: 1Hz)
73 * @param amplitude Base amplitude (default: 1.0)
74 * @param offset DC offset added to the output (default: 0.0)
75 *
76 * Creates an impulse generator with both frequency and amplitude modulation,
77 * enabling complex timing and amplitude control.
78 */
79 Impulse(const std::shared_ptr<Node>& frequency_modulator, const std::shared_ptr<Node>& amplitude_modulator,
80 float frequency = 1, double amplitude = 1, float offset = 0);
81
82 /**
83 * @brief Virtual destructor
84 */
85 ~Impulse() override = default;
86
87 /**
88 * @brief Processes a single input sample and generates an impulse sample
89 * @param input Input sample (used for modulation when modulators are connected)
90 * @return Generated impulse sample (1.0 at the start of each cycle, 0.0 elsewhere)
91 *
92 * This method advances the generator's phase and computes the next
93 * sample of the impulse train, applying any modulation from connected nodes.
94 */
95 double process_sample(double input = 0.) override;
96
97 /**
98 * @brief Processes multiple samples at once
99 * @param num_samples Number of samples to generate
100 * @return Vector of generated impulse samples
101 *
102 * This method is more efficient than calling process_sample() repeatedly
103 * when generating multiple samples at once.
104 */
105 std::vector<double> process_batch(unsigned int num_samples) override;
106
107 /**
108 * @brief Sets the generator's frequency
109 * @param frequency New frequency in Hz
110 *
111 * Updates the generator's frequency, which controls how often impulses occur.
112 */
113 void set_frequency(float frequency) override;
114
115 /**
116 * @brief Gets the current base frequency
117 * @return Current frequency in Hz
118 */
119 inline float get_frequency() const { return m_frequency; }
120
121 /**
122 * @brief Sets all basic parameters at once
123 * @param frequency New frequency in Hz
124 * @param amplitude New amplitude
125 * @param offset New DC offset
126 *
127 * This is more efficient than setting parameters individually
128 * when multiple parameters need to be changed.
129 */
130 inline void set_params(float frequency, double amplitude, float offset)
131 {
132 m_amplitude = amplitude;
133 m_offset = offset;
134 set_frequency(frequency);
135 }
136
137 /**
138 * @brief Sets a node to modulate the generator's frequency
139 * @param modulator Node that will modulate the frequency
140 *
141 * The modulator's output is added to the base frequency,
142 * enabling dynamic control of impulse timing.
143 */
144 void set_frequency_modulator(const std::shared_ptr<Node>& modulator);
145
146 /**
147 * @brief Sets a node to modulate the generator's amplitude
148 * @param modulator Node that will modulate the amplitude
149 *
150 * The modulator's output is multiplied with the base amplitude,
151 * enabling dynamic control of impulse height.
152 */
153 void set_amplitude_modulator(const std::shared_ptr<Node>& modulator);
154
155 /**
156 * @brief Removes all modulation connections
157 *
158 * After calling this method, the generator will use only its
159 * base frequency and amplitude without any modulation.
160 */
161 void clear_modulators();
162
163 /**
164 * @brief Resets the generator's phase and parameters
165 * @param frequency New frequency in Hz (default: 1Hz)
166 * @param amplitude New amplitude (default: 1.0)
167 * @param offset New DC offset (default: 0)
168 *
169 * This method resets the generator's internal state and parameters,
170 * effectively restarting it from the beginning of its cycle.
171 */
172 void reset(float frequency = 1, float amplitude = 1.0F, float offset = 0);
173
174 /**
175 * @brief Registers a callback for every impulse
176 * @param callback Function to call when an impulse occurs
177 *
178 * This method allows external components to monitor or react to
179 * every impulse produced by the generator. The callback
180 * receives a GeneratorContext containing the generated value and
181 * generator parameters like frequency, amplitude, and phase.
182 */
184
185 /**
186 * @brief Removes a previously registered callback
187 * @param callback The callback function to remove
188 * @return True if the callback was found and removed, false otherwise
189 *
190 * Unregisters a callback previously added with on_tick(), stopping
191 * it from receiving further notifications about generated samples.
192 */
193 bool remove_hook(const TypedHook<GeneratorContext>& callback);
194
195 void save_state() override;
196 void restore_state() override;
197
198 /**
199 * @brief Retrieves the current modulators connected to this node
200 * @return Vector of pairs containing the modulator role and the corresponding node
201 */
202 [[nodiscard]] std::vector<std::pair<ModulatorRole, std::shared_ptr<Node>>> get_modulators() const override;
203
204protected:
205 /**
206 * @brief Notifies all registered callbacks about a new sample
207 * @param value The newly generated sample
208 *
209 * This method is called internally whenever a new sample is generated,
210 * creating the appropriate context and invoking all registered callbacks
211 * that should receive notification about this sample.
212 */
213 void notify_tick(double value) override;
214
215private:
216 /**
217 * @brief Phase increment per sample
218 *
219 * This value determines how much the phase advances with each sample,
220 * controlling the generator's frequency.
221 */
222 double m_phase_inc {};
223
224 /**
225 * @brief DC offset added to the output
226 */
227 float m_offset;
228
229 /**
230 * @brief Node that modulates the frequency
231 */
232 std::shared_ptr<Node> m_frequency_modulator;
233
234 /**
235 * @brief Node that modulates the amplitude
236 */
237 std::shared_ptr<Node> m_amplitude_modulator;
238
239 /**
240 * @brief Updates the phase increment based on a new frequency
241 * @param frequency New frequency in Hz
242 *
243 * This method calculates the phase increment needed to produce
244 * impulses at the specified frequency at the current sample rate.
245 */
246 void update_phase_increment(double frequency);
247
248 /**
249 * @brief Collection of impulse-specific callback functions
250 */
251 HookList m_impulse_callbacks { *this };
252
254
255 double m_saved_phase {};
256 float m_saved_frequency {};
257 float m_saved_offset {};
258 double m_saved_phase_inc {};
259 double m_saved_last_output {};
260};
261}
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
Base class for all signal and pattern generators in Maya Flux.
std::shared_ptr< Node > m_amplitude_modulator
Node that modulates the amplitude.
Definition Impulse.hpp:237
~Impulse() override=default
Virtual destructor.
void set_params(float frequency, double amplitude, float offset)
Sets all basic parameters at once.
Definition Impulse.hpp:130
float get_frequency() const
Gets the current base frequency.
Definition Impulse.hpp:119
float m_offset
DC offset added to the output.
Definition Impulse.hpp:227
std::shared_ptr< Node > m_frequency_modulator
Node that modulates the frequency.
Definition Impulse.hpp:232
Impulse generator node.
Definition Impulse.hpp:32
Stores, fires and clears the hooks of one node channel.
Definition NodeUtils.hpp:93
uint64_t HookId
Identifies one registration inside a HookList.
Definition NodeUtils.hpp:60
std::function< void(ContextT &)> TypedHook
Callback function type for node processing events, parameterised on context type.
Definition NodeUtils.hpp:28