MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Polynomial.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
8
9/**
10 * @enum PolynomialMode
11 * @brief Defines how the polynomial function processes input values
12 */
13enum class PolynomialMode : uint8_t {
14 DIRECT, ///< Evaluates f(x) where x is the current phase/input
15 RECURSIVE, ///< Evaluates using current and previous outputs: y[n] = f(y[n-1], y[n-2], ...)
16 FEEDFORWARD ///< Evaluates using current and previous inputs: y[n] = f(x[n], x[n-1], ...)
17};
18
19class MAYAFLUX_API PolynomialContext : public NodeContext {
20public:
21 /**
22 * @brief Constructs a PolynomialContext
23 * @param value Current output value
24 * @param mode Current polynomial mode
25 * @param buffer_size Size of the input/output buffers
26 * @param input_buffer Current input buffer contents
27 * @param output_buffer Current output buffer contents
28 * @param coefficients Current polynomial coefficients
29 */
30 PolynomialContext(double value,
31 PolynomialMode mode,
32 size_t buffer_size,
33 std::span<double> input_buffer,
34 std::span<double> output_buffer,
35 const std::vector<double>& coefficients)
36 : NodeContext(value)
37 , m_mode(mode)
38 , m_buffer_size(buffer_size)
39 , m_input_buffer(input_buffer)
40 , m_output_buffer(output_buffer)
41 , m_coefficients(coefficients)
42 {
43 }
44
45 /**
46 * @brief Gets the current polynomial mode
47 * @return Current polynomial mode
48 */
49 [[nodiscard]] PolynomialMode get_mode() const { return m_mode; }
50
51 /**
52 * @brief Gets the buffer size
53 * @return Current buffer size
54 */
55 [[nodiscard]] size_t get_buffer_size() const { return m_buffer_size; }
56
57 /**
58 * @brief Gets the input buffer
59 * @return Reference to the input buffer
60 */
61 [[nodiscard]] std::span<double> get_input_buffer() const { return m_input_buffer; }
62
63 /**
64 * @brief Gets the output buffer
65 * @return Reference to the output buffer
66 */
67 [[nodiscard]] std::span<double> get_output_buffer() const { return m_output_buffer; }
68
69 /**
70 * @brief Gets the polynomial coefficients
71 * @return Reference to the coefficient vector
72 */
73 [[nodiscard]] const std::vector<double>& get_coefficients() const { return m_coefficients; }
74
75private:
76 PolynomialMode m_mode; ///< Current processing mode
77 size_t m_buffer_size; ///< Size of the buffers
78 std::span<double> m_input_buffer; ///< Copy of input buffer
79 std::span<double> m_output_buffer; ///< Copy of output buffer
80 const std::vector<double>& m_coefficients; ///< Copy of polynomial coefficients
81
82 friend class Polynomial;
83};
84
85class MAYAFLUX_API PolynomialContextGpu
86 : public PolynomialContext,
87 public GpuVectorData {
88public:
90 double value,
91 PolynomialMode mode,
92 size_t buffer_size,
93 std::span<double> input_buffer,
94 std::span<double> output_buffer,
95 const std::vector<double>& coefficients,
96 std::span<const float> gpu_data)
97 : PolynomialContext(value, mode, buffer_size, input_buffer, output_buffer, coefficients)
98 , GpuVectorData(gpu_data)
99 {
100 }
101
102 friend class Polynomial;
103
104 std::vector<float> gpu_float_buffer;
105};
106
107/**
108 * @class Polynomial
109 * @brief Generator that produces values based on polynomial functions
110 *
111 * The Polynomial generator creates a signal based on mathematical functions
112 * that can operate in different modes:
113 * - Direct mode: evaluates f(x) where x is the current phase or input
114 * - Recursive mode: evaluates using current and previous outputs
115 * - Feedforward mode: evaluates using current and previous inputs
116 *
117 * This flexible approach allows implementing various types of polynomial
118 * equations, difference equations, and recurrence relations.
119 */
120class MAYAFLUX_API Polynomial : public Generator {
121public:
122 /**
123 * @brief Function type for direct polynomial evaluation
124 *
125 * Takes a single input value and returns the computed output.
126 */
127 using DirectFunction = std::function<double(double)>;
128
129 /**
130 * @brief Function type for recursive/feedforward polynomial evaluation
131 *
132 * Takes a reference to a buffer of values and returns the computed output.
133 * For recursive mode, the buffer contains previous outputs.
134 * For feedforward mode, the buffer contains current and previous inputs.
135 */
136 using BufferFunction = std::function<double(std::span<double>)>;
137
138 /**
139 * @class PolynomialContext
140 * @brief Context object for polynomial node callbacks
141 *
142 * Extends the base NodeContext with polynomial-specific information
143 * such as mode, buffer contents, and coefficients.
144 */
145 ;
146
147 /**
148 * @brief Constructs a Polynomial generator in direct mode with coefficient-based definition
149 * @param coefficients Vector of polynomial coefficients (highest power first)
150 *
151 * Creates a polynomial generator that evaluates a standard polynomial function
152 * defined by the provided coefficients. The coefficients are ordered from
153 * highest power to lowest (e.g., [2, 3, 1] represents 2x² + 3x + 1).
154 */
155 explicit Polynomial(const std::vector<double>& coefficients);
156
157 /**
158 * @brief Constructs a Polynomial generator in direct mode with a custom function
159 * @param function Custom function that maps input to output value
160 *
161 * Creates a polynomial generator that evaluates a custom mathematical function
162 * provided by the user.
163 */
164 explicit Polynomial(DirectFunction function);
165
166 /**
167 * @brief Constructs a Polynomial generator in recursive or feedforward mode
168 * @param function Function that processes a buffer of values
169 * @param mode Processing mode (RECURSIVE or FEEDFORWARD)
170 * @param buffer_size Number of previous values to maintain
171 *
172 * Creates a polynomial generator that evaluates using current and previous values.
173 * In recursive mode, the buffer contains previous outputs.
174 * In feedforward mode, the buffer contains current and previous inputs.
175 */
176 Polynomial(BufferFunction function, PolynomialMode mode, size_t buffer_size);
177
178 ~Polynomial() override = default;
179
180 /**
181 * @brief Processes a single sample
182 * @param input Input value
183 * @return The next sample value from the polynomial function
184 *
185 * Computes the next output value based on the polynomial function and current mode.
186 */
187 double process_sample(double input = 0.) override;
188
189 /**
190 * @brief Processes multiple samples at once
191 * @param num_samples Number of samples to generate
192 * @return Vector of generated samples
193 *
194 * This method is more efficient than calling process_sample() repeatedly
195 * when generating multiple samples at once.
196 */
197 std::vector<double> process_batch(unsigned int num_samples) override;
198
199 /**
200 * @brief Resets the generator to its initial state
201 *
202 * Clears the internal buffers and resets to initial conditions.
203 */
204 void reset();
205
206 /**
207 * @brief Sets the polynomial coefficients (for direct mode)
208 * @param coefficients Vector of polynomial coefficients (highest power first)
209 *
210 * Updates the polynomial function to use the specified coefficients.
211 * This allows dynamically changing the polynomial during operation.
212 */
213 void set_coefficients(const std::vector<double>& coefficients);
214
215 /**
216 * @brief Sets a custom direct function
217 * @param function Custom function that maps input to output value
218 *
219 * Updates the generator to use the specified custom function.
220 */
221 void set_direct_function(DirectFunction function);
222
223 /**
224 * @brief Sets a custom buffer function
225 * @param function Function that processes a buffer of values
226 * @param mode Processing mode (RECURSIVE or FEEDFORWARD)
227 * @param buffer_size Number of previous values to maintain
228 *
229 * Updates the generator to use the specified buffer function and mode.
230 */
231 void set_buffer_function(BufferFunction function, PolynomialMode mode, size_t buffer_size);
232
233 /**
234 * @brief Sets initial conditions for recursive mode
235 * @param initial_values Vector of initial output values
236 *
237 * Sets the initial values for the output buffer in recursive mode.
238 * The values are ordered from newest to oldest.
239 */
240 void set_initial_conditions(const std::vector<double>& initial_values);
241
242 /**
243 * @brief Creates a polynomial function from coefficients
244 * @param coefficients Vector of polynomial coefficients (highest power first)
245 * @return Function that evaluates the polynomial
246 *
247 * Generates a function that evaluates the polynomial defined by the
248 * specified coefficients.
249 * The cofficients are ordered from highest power to lowest.
250 */
251 DirectFunction create_polynomial_function(const std::vector<double>& coefficients);
252
253 /**
254 * @brief Sets the input node to generate polynomial values from
255 * @param input_node Node providing the input values
256 *
257 * Configures the node to receive input from another node
258 */
259 inline void set_input_node(const std::shared_ptr<Node>& input_node) { m_input_node = input_node; }
260
261 /**
262 * @brief Gets the current polynomial mode
263 * @return Current polynomial mode
264 */
265 [[nodiscard]] PolynomialMode get_mode() const { return m_mode; }
266
267 /**
268 * @brief Gets the buffer size
269 * @return Current buffer size
270 */
271 [[nodiscard]] size_t get_buffer_size() const { return m_buffer_size; }
272
273 /**
274 * @brief Gets the polynomial coefficients
275 * @return Reference to the coefficient vector
276 */
277 [[nodiscard]] const std::vector<double>& get_coefficients() const { return m_coefficients; }
278
279 /**
280 * @brief Gets the input buffer
281 * @return Reference to the input buffer
282 */
283 [[nodiscard]] std::span<double> get_input_buffer() { return m_history.linearized_view(); }
284
285 /**
286 * @brief Gets the output buffer
287 * @return Reference to the output buffer
288 */
289 [[nodiscard]] std::span<double> get_output_buffer() { return m_history.linearized_view(); }
290
291 /**
292 * @brief Sets the scaling factor for the output values
293 */
294 inline void set_amplitude(double amplitude) override { m_scale_factor = amplitude; }
295
296 /**
297 * @brief Gets the current amplitude scaling factor
298 * @return Current amplitude scaling factor
299 *
300 * This method retrieves the scaling factor applied to the output values,
301 * which controls the overall amplitude of the generated signal.
302 */
303 [[nodiscard]] inline double get_amplitude() const override { return m_scale_factor; }
304
305 void save_state() override;
306 void restore_state() override;
307
308 /**
309 * @brief Uses an external buffer context for processing
310 * @param buffer_view Span representing the external buffer
311 *
312 * Configures the generator to use an external buffer context
313 * for its internal processing, allowing integration with
314 * external data sources.
315 */
316 void set_buffer_context(std::span<double> buffer_view)
317 {
318 m_external_buffer_context = buffer_view;
319 m_use_external_context = true;
320 }
321
322 /**
323 * @brief Clear external buffer context, resume internal accumulation
324 */
326 {
327 m_use_external_context = false;
328 m_external_buffer_context = {};
329 }
330
331 [[nodiscard]] inline bool using_external_context() const
332 {
333 return m_use_external_context;
334 }
335
336 /**
337 * @brief Retrieves the last created context object
338 * @return Reference to the last PolynomialContext object
339 *
340 * This method provides access to the most recent PolynomialContext object
341 * created by the polynomial generator. This context contains information
342 * about the generator's state at the time of the last output generation.
343 */
344 NodeContext& get_last_context() override;
345
346 void set_gpu_compatible(bool compatible) override
347 {
348 Node::set_gpu_compatible(compatible);
349 if (compatible) {
350 m_node_capability |= NodeCapability::VECTOR;
351 } else {
352 m_node_capability &= ~NodeCapability::VECTOR;
353 }
354 }
355
356 /**
357 * @brief Retrieves the current modulators connected to this node
358 * @return Vector of pairs containing the modulator role and the corresponding node
359 */
360 [[nodiscard]] std::vector<std::pair<ModulatorRole, std::shared_ptr<Node>>> get_modulators() const override;
361
362protected:
363 /**
364 * @brief Updates the context object with the current node state
365 * @param value The current sample value
366 */
367 void update_context(double value) override;
368
369private:
370 /**
371 * @brief Converts coefficient vector to a polynomial function
372 * @param coefficients Vector of polynomial coefficients (highest power first)
373 * @return Function that evaluates the polynomial
374 *
375 * Creates a function that evaluates the polynomial defined by the
376 * specified coefficients.
377 */
378 // DirectFunction create_polynomial_function(const std::vector<double>& coefficients);
379
380 PolynomialMode m_mode; ///< Current processing mode
381 DirectFunction m_direct_function; ///< Function for direct mode
382 BufferFunction m_buffer_function; ///< Function for recursive/feedforward mode
383 std::vector<double> m_coefficients; ///< Polynomial coefficients (if using coefficient-based definition)
384 std::span<double> m_external_buffer_context; ///< View into external buffer
385
386 Memory::HistoryBuffer<double> m_history; ///< Ring buffer for input/output history
387 std::vector<double> m_linear_view; ///< Linearized view of history for easy access
388
389 std::vector<double> m_saved_history_state; ///< Saved state of the history buffer
390
391 size_t m_buffer_size {}; ///< Maximum size of the buffers
392 double m_scale_factor; ///< Scaling factor for output
393 std::shared_ptr<Node> m_input_node; ///< Input node for processing
394 size_t m_current_buffer_position {}; // Where we are in the external buffer
395
396 double m_saved_last_output {};
397 bool m_use_external_context {}; // Whether to use it
398
401
402 std::span<double> external_context_view(double input);
403};
404
405} // namespace MayaFlux::Nodes::Generator
Core::GlobalInputConfig input
Definition Config.cpp:36
History buffer for difference equations and recursive relations.
Base class for all signal and pattern generators in Maya Flux.
PolynomialContextGpu(double value, PolynomialMode mode, size_t buffer_size, std::span< double > input_buffer, std::span< double > output_buffer, const std::vector< double > &coefficients, std::span< const float > gpu_data)
std::span< double > get_output_buffer() const
Gets the output buffer.
const std::vector< double > & m_coefficients
Copy of polynomial coefficients.
const std::vector< double > & get_coefficients() const
Gets the polynomial coefficients.
std::span< double > m_output_buffer
Copy of output buffer.
PolynomialMode m_mode
Current processing mode.
PolynomialContext(double value, PolynomialMode mode, size_t buffer_size, std::span< double > input_buffer, std::span< double > output_buffer, const std::vector< double > &coefficients)
Constructs a PolynomialContext.
std::span< double > m_input_buffer
Copy of input buffer.
std::span< double > get_input_buffer() const
Gets the input buffer.
size_t get_buffer_size() const
Gets the buffer size.
PolynomialMode get_mode() const
Gets the current polynomial mode.
std::function< double(std::span< double >)> BufferFunction
Function type for recursive/feedforward polynomial evaluation.
void set_gpu_compatible(bool compatible) override
Sets whether the node is compatible with GPU processing.
PolynomialMode m_mode
Converts coefficient vector to a polynomial function.
std::vector< double > m_saved_history_state
Saved state of the history buffer.
void set_amplitude(double amplitude) override
Sets the scaling factor for the output values.
Memory::HistoryBuffer< double > m_history
Ring buffer for input/output history.
PolynomialMode get_mode() const
Gets the current polynomial mode.
void set_input_node(const std::shared_ptr< Node > &input_node)
Sets the input node to generate polynomial values from.
void set_buffer_context(std::span< double > buffer_view)
Uses an external buffer context for processing.
const std::vector< double > & get_coefficients() const
Gets the polynomial coefficients.
double get_amplitude() const override
Gets the current amplitude scaling factor.
double m_scale_factor
Scaling factor for output.
std::span< double > m_external_buffer_context
View into external buffer.
size_t get_buffer_size() const
Gets the buffer size.
void clear_buffer_context()
Clear external buffer context, resume internal accumulation.
std::vector< double > m_linear_view
Linearized view of history for easy access.
BufferFunction m_buffer_function
Function for recursive/feedforward mode.
std::shared_ptr< Node > m_input_node
Input node for processing.
std::span< double > get_output_buffer()
Gets the output buffer.
std::function< double(double)> DirectFunction
Function type for direct polynomial evaluation.
DirectFunction m_direct_function
Function for direct mode.
std::span< double > get_input_buffer()
Gets the input buffer.
std::vector< double > m_coefficients
Polynomial coefficients (if using coefficient-based definition)
Generator that produces values based on polynomial functions.
GPU-uploadable 1D array data interface.
Base context class for node callbacks.
Definition Node.hpp:53
@ DIRECT
Stateless evaluation of current input only (combinational logic)
PolynomialMode
Defines how the polynomial function processes input values.
@ RECURSIVE
Evaluates using current and previous outputs: y[n] = f(y[n-1], y[n-2], ...)
@ FEEDFORWARD
Evaluates using current and previous inputs: y[n] = f(x[n], x[n-1], ...)
NodeCapability
Bitmask flags declaring what data shapes a node's context can produce.
Definition NodeSpec.hpp:104