MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
PolynomialProcessor.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Buffers {
7
8/**
9 * @class PolynomialProcessor
10 * @brief Buffer processor that applies polynomial transformations to audio data
11 *
12 * This processor connects a Polynomial node to an AudioBuffer, allowing
13 * polynomial functions to be applied to buffer data. It supports all three
14 * polynomial modes (direct, recursive, and feedforward) and provides
15 * configuration options for how the polynomial is applied.
16 */
17class MAYAFLUX_API PolynomialProcessor : public BufferProcessor {
18public:
19 /**
20 * @brief Processing mode for the polynomial processor
21 */
22 enum class ProcessMode : uint8_t {
23 SAMPLE_BY_SAMPLE, ///< Process each sample individually
24 BATCH, ///< Process the entire buffer at once
25 WINDOWED, ///< Process using a sliding window
26 BUFFER_CONTEXT ///< Process each sample with access to buffer history
27 };
28
29 // TODO:: Temporary requirment on windows
31
32 /**
33 * @brief Creates a new processor that applies polynomial transformations
34 * @param mode Processing mode (sample-by-sample, batch, or windowed)
35 * @param window_size Size of the sliding window (for WINDOWED mode)
36 * @param args Arguments to pass to the Polynomial constructor
37 */
38 template <typename... Args>
39 requires std::constructible_from<Nodes::Generator::Polynomial, Args...>
40 PolynomialProcessor(ProcessMode mode = ProcessMode::SAMPLE_BY_SAMPLE, size_t window_size = 64, Args&&... args)
41 : m_polynomial(std::make_shared<Nodes::Generator::Polynomial>(std::forward<Args>(args)...))
42 , m_process_mode(mode)
43 , m_window_size(window_size)
44 , m_use_internal(true)
45 {
46 }
47
48 /**
49 * @brief Creates a new processor that applies polynomial transformations
50 * @param polynomial Polynomial node to use for processing
51 * @param mode Processing mode (sample-by-sample, batch, or windowed)
52 * @param window_size Size of the sliding window (for WINDOWED mode)
53 *
54 * NOTE: Using external Polynomial node implies side effects of any progessing chain the node
55 * is connected to. This could mean that the buffer data is not used as input when used node's cached value.
56 */
57 PolynomialProcessor(const std::shared_ptr<Nodes::Generator::Polynomial>& polynomial, ProcessMode mode = ProcessMode::SAMPLE_BY_SAMPLE, size_t window_size = 64);
58
59 /**
60 * @brief Processes an audio buffer using the polynomial function
61 * @param buffer Buffer to process
62 *
63 * Applies the polynomial transformation to the buffer data according
64 * to the configured processing mode and parameters.
65 */
66 void processing_function(std::shared_ptr<Buffer> buffer) override;
67
68 /**
69 * @brief Called when the processor is attached to a buffer
70 * @param buffer Buffer being attached to
71 */
72 void on_attach(std::shared_ptr<Buffer> buffer) override;
73
74 /**
75 * @brief Called when the processor is detached from a buffer
76 * @param buffer Buffer being detached from
77 */
78 inline void on_detach(std::shared_ptr<Buffer>) override { }
79
80 /**
81 * @brief Sets the processing mode
82 * @param mode New processing mode
83 */
84 inline void set_process_mode(ProcessMode mode) { m_process_mode = mode; }
85
86 /**
87 * @brief Gets the current processing mode
88 * @return Current processing mode
89 */
90 [[nodiscard]] inline ProcessMode get_process_mode() const { return m_process_mode; }
91
92 /**
93 * @brief Sets the window size for windowed processing
94 * @param size New window size
95 */
96 inline void set_window_size(size_t size) { m_window_size = size; }
97
98 /**
99 * @brief Gets the current window size
100 * @return Current window size
101 */
102 [[nodiscard]] inline size_t get_window_size() const { return m_window_size; }
103
104 /**
105 * @brief Gets the polynomial node used for processing
106 * @return Polynomial node used for processing
107 */
108 [[nodiscard]] inline std::shared_ptr<Nodes::Generator::Polynomial> get_polynomial() const { return m_polynomial; }
109
110 /**
111 * @brief Checks if the processor is using the internal polynomial node
112 * @return True if using internal polynomial node, false otherwise
113 *
114 * This is useful when the polynomial node is connected to other nodes
115 * and we want to ensure that the processor uses its own internal
116 * polynomial node instead of the one provided in the constructor.
117 */
118 [[nodiscard]] inline bool is_using_internal() const { return m_use_internal; }
119
120 /**
121 * @brief Forces the processor to use the internal polynomial node
122 *
123 * This is useful when the polynomial node is connected to other nodes
124 * and we want to ensure that the processor uses its own internal
125 * polynomial node instead of the one provided in the constructor.
126 */
127 template <typename... Args>
128 requires std::constructible_from<Nodes::Generator::Polynomial, Args...>
129 void force_use_internal(Args&&... args)
130 {
131 m_pending_polynomial = std::make_shared<Nodes::Generator::Polynomial>(std::forward<Args>(args)...);
132 }
133
134 /**
135 * @brief Updates the polynomial node used for processing
136 * @param polynomial New polynomial node to use
137 *
138 * NOTE: Using external Polynomial node implies side effects of any progessing chain the node
139 * is connected to. This could mean that the buffer data is not used as input when used node's cached value.
140 */
141 inline void update_polynomial_node(const std::shared_ptr<Nodes::Generator::Polynomial>& polynomial)
142 {
143 m_pending_polynomial = polynomial;
144 }
145
146private:
147 std::shared_ptr<Nodes::Generator::Polynomial> m_polynomial; ///< Polynomial node for processing
148 ProcessMode m_process_mode {}; ///< Current processing mode
149 size_t m_window_size {}; ///< Window size for windowed processing
150
151 bool m_use_internal {}; ///< Whether to use the buffer's internal previous state
152 std::shared_ptr<Nodes::Generator::Polynomial> m_pending_polynomial; ///< Internal polynomial node
153
154 /**
155 * @brief Processes a span of data using the polynomial function
156 * @param data Span of data to process
157 */
158 void process_span(std::span<double> data);
159};
160
161} // namespace MayaFlux::Buffers
Central computational transformation interface for continuous buffer processing.
void update_polynomial_node(const std::shared_ptr< Nodes::Generator::Polynomial > &polynomial)
Updates the polynomial node used for processing.
ProcessMode get_process_mode() const
Gets the current processing mode.
void on_detach(std::shared_ptr< Buffer >) override
Called when the processor is detached from a buffer.
void set_process_mode(ProcessMode mode)
Sets the processing mode.
PolynomialProcessor(ProcessMode mode=ProcessMode::SAMPLE_BY_SAMPLE, size_t window_size=64, Args &&... args)
Creates a new processor that applies polynomial transformations.
bool is_using_internal() const
Checks if the processor is using the internal polynomial node.
ProcessMode
Processing mode for the polynomial processor.
std::shared_ptr< Nodes::Generator::Polynomial > get_polynomial() const
Gets the polynomial node used for processing.
void force_use_internal(Args &&... args)
Forces the processor to use the internal polynomial node.
std::shared_ptr< Nodes::Generator::Polynomial > m_pending_polynomial
Internal polynomial node.
void set_window_size(size_t size)
Sets the window size for windowed processing.
size_t get_window_size() const
Gets the current window size.
std::shared_ptr< Nodes::Generator::Polynomial > m_polynomial
Polynomial node for processing.
Buffer processor that applies polynomial transformations to audio data.
Generator that produces values based on polynomial functions.