MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
FeedbackBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Buffers {
7
8/**
9 * @class FeedbackBuffer
10 * @brief Specialized buffer implementing computational feedback systems
11 *
12 * FeedbackBuffer extends AudioBuffer to create a buffer that maintains memory
13 * of its previous state, enabling the creation of recursive computational systems.
14 * This implementation transcends traditional audio effects, providing a foundation
15 * for complex dynamical systems, emergent behaviors, and self-modifying algorithms.
16 *
17 * Key features:
18 * - Implements a discrete-time recursive system with controllable feedback coefficient
19 * - Enables creation of complex dynamical systems with memory
20 * - Supports emergence of non-linear behaviors through controlled recursion
21 * - Provides a foundation for generative algorithms that evolve over time
22 *
23 * Feedback is a fundamental concept in computational systems that enables complex
24 * behaviors to emerge from simple rules. This implementation provides a clean,
25 * controlled way to introduce recursive elements without the risks of uncontrolled
26 * recursion or stack overflow that can occur in node-based feedback.
27 */
28class MAYAFLUX_API FeedbackBuffer : public AudioBuffer {
29public:
30 /**
31 * @brief Creates a new feedback buffer
32 * @param channel_id Channel identifier for this buffer
33 * @param num_samples Buffer size in samples
34 * @param feedback Feedback coefficient (0.0-1.0)
35 * @param feed_samples Number of samples to feed back
36 *
37 * Initializes a buffer that implements a discrete-time recursive system.
38 * The feedback parameter controls the coefficient of recursion, determining
39 * how strongly the system's past states influence its future evolution.
40 */
41 FeedbackBuffer(uint32_t channel_id = 0, uint32_t num_samples = 512, float feedback = 0.5F, uint32_t feed_samples = 512);
42
43 /**
44 * @brief Sets the feedback coefficient
45 * @param amount Feedback coefficient (0.0-1.0)
46 *
47 * Controls the strength of recursion in the system:
48 * - 0.0: No recursion (behaves like a standard buffer)
49 * - 0.5: Balanced influence between new input and previous state
50 * - 1.0: Maximum recursion (can lead to saturation or chaotic behavior)
51 */
52 inline void set_feedback(float amount) { m_feedback_amount = amount; }
53
54 /**
55 * @brief Gets the current feedback coefficient
56 * @return Current feedback coefficient (0.0-1.0)
57 */
58 inline float get_feedback() const { return m_feedback_amount; }
59
60 /**
61 * @brief Gets mutable access to the previous state vector
62 * @return Reference to the vector containing previous state data
63 *
64 * This provides direct access to the system's previous state, which can be
65 * useful for advanced algorithms or analysis. Use with caution as modifying
66 * this directly can affect the system's evolution.
67 */
68 inline std::vector<double>& get_previous_buffer() { return m_previous_buffer; }
69
70 /**
71 * @brief Gets read-only access to the previous state vector
72 * @return Const reference to the vector containing previous state data
73 */
74 inline const std::vector<double>& get_previous_buffer() const { return m_previous_buffer; }
75
76 /**
77 * @brief Processes this buffer using its default processor
78 *
79 * For a FeedbackBuffer, this involves applying the recursive algorithm
80 * that combines current input with the previous state according to
81 * the feedback coefficient.
82 */
83 void process_default() override;
84
85 inline void set_feed_samples(uint32_t samples) { m_feed_samples = samples; }
86
87 [[nodiscard]] inline uint32_t get_feed_samples() const { return m_feed_samples; }
88
89protected:
90 /**
91 * @brief Creates the default processor for this buffer type
92 * @return Shared pointer to a FeedbackProcessor
93 *
94 * FeedbackBuffers use a FeedbackProcessor as their default processor,
95 * which implements the recursive algorithm.
96 */
97 std::shared_ptr<BufferProcessor> create_default_processor() override;
98
99private:
100 /**
101 * @brief Feedback coefficient (0.0-1.0)
102 *
103 * Controls the strength of recursion in the system.
104 */
106
107 /**
108 * @brief Storage for the previous system state
109 *
110 * This vector maintains a copy of the system's state from the previous
111 * processing cycle, enabling the implementation of recursive algorithms.
112 */
113 std::vector<double> m_previous_buffer;
114
115 uint32_t m_feed_samples { 512 }; /// Number of samples to feed back
116};
117
118/**
119 * @class FeedbackProcessor
120 * @brief Processor that implements recursive computational algorithms
121 *
122 * FeedbackProcessor is a specialized buffer processor that implements discrete-time
123 * recursive algorithms by combining a system's current state with its previous state.
124 * It can be applied to any AudioBuffer, not just FeedbackBuffer, allowing recursive
125 * properties to be added to existing computational pipelines.
126 *
127 * Unlike stateless processors, FeedbackProcessor maintains memory between processing
128 * cycles, storing the previous system state for use in the next cycle. This
129 * memory-based behavior enables the emergence of complex temporal patterns and
130 * evolutionary behaviors.
131 *
132 * Applications:
133 * - Generative algorithms with memory and evolution
134 * - Simulation of complex dynamical systems
135 * - Creation of emergent, self-modifying behaviors
136 * - Implementation of recursive mathematical functions
137 * - Cross-domain feedback systems (audio influencing visual, data influencing audio, etc.)
138 */
139class MAYAFLUX_API FeedbackProcessor : public BufferProcessor {
140
141public:
142 /**
143 * @brief Creates a new feedback processor
144 * @param feedback Feedback coefficient (0.0-1.0)
145 *
146 * Initializes a processor that implements a recursive algorithm
147 * combining a system's current state with its previous state
148 * according to the specified feedback coefficient.
149 */
150 FeedbackProcessor(float feedback = 0.5F, uint32_t feed_samples = 512);
151
152 /**
153 * @brief Processes a buffer by applying the recursive algorithm
154 * @param buffer Buffer to process
155 *
156 * This method:
157 * 1. Combines the current state with the stored previous state
158 * 2. Stores the resulting output as the new previous state
159 *
160 * The combination is weighted by the feedback coefficient, with higher
161 * values resulting in stronger influence from the previous state.
162 */
163 void processing_function(std::shared_ptr<Buffer> buffer) override;
164
165 /**
166 * @brief Called when this processor is attached to a buffer
167 * @param buffer Buffer this processor is being attached to
168 *
169 * Initializes the previous state storage to match the size of the
170 * attached buffer. If the buffer is a FeedbackBuffer, the processor
171 * will use its internal previous state.
172 */
173 void on_attach(std::shared_ptr<Buffer> buffer) override;
174
175 /**
176 * @brief Called when this processor is detached from a buffer
177 * @param buffer Buffer this processor is being detached from
178 *
179 * Cleans up any buffer-specific state.
180 */
181 void on_detach(std::shared_ptr<Buffer> buffer) override;
182
183 /**
184 * @brief Sets the feedback coefficient
185 * @param amount Feedback coefficient (0.0-1.0)
186 */
187 inline void set_feedback(float amount) { m_feedback_amount = amount; }
188
189 /**
190 * @brief Gets the current feedback coefficient
191 * @return Current feedback coefficient (0.0-1.0)
192 */
193 [[nodiscard]] inline float get_feedback() const { return m_feedback_amount; }
194
195 /**
196 * @brief Gets the number of samples to feed back
197 * @return Number of samples to feed back
198 */
199 [[nodiscard]] inline uint32_t get_feed_samples() const { return m_feed_samples; }
200
201 /**
202 * @brief Sets the number of samples to feed back
203 * @param samples Number of samples to feed back
204 */
205 inline void set_feed_samples(uint32_t samples) { m_feed_samples = samples; }
206
207private:
208 /**
209 * @brief Feedback coefficient (0.0-1.0)
210 */
212
213 /**
214 * @brief Number of samples to feed back
215 */
217
218 /**
219 * @brief Storage for the previous system state
220 *
221 * This vector maintains a copy of the system's state from the previous
222 * processing cycle, enabling the implementation of recursive algorithms.
223 */
224 std::vector<double> m_previous_buffer;
225
226 /**
227 * @brief Flag indicating whether to use the buffer's internal previous state
228 *
229 * If the attached buffer is a FeedbackBuffer, the processor will use its
230 * internal previous state instead of maintaining its own.
231 */
233
234 size_t m_buffer_index {}; /// Current index in the previous buffer for feedback
235};
236}
Concrete audio implementation of the Buffer interface for double-precision audio data.
Central computational transformation interface for continuous buffer processing.
const std::vector< double > & get_previous_buffer() const
Gets read-only access to the previous state vector.
std::vector< double > m_previous_buffer
Storage for the previous system state.
void set_feedback(float amount)
Sets the feedback coefficient.
float m_feedback_amount
Feedback coefficient (0.0-1.0)
void set_feed_samples(uint32_t samples)
float get_feedback() const
Gets the current feedback coefficient.
std::vector< double > & get_previous_buffer()
Gets mutable access to the previous state vector.
Specialized buffer implementing computational feedback systems.
uint32_t get_feed_samples() const
Gets the number of samples to feed back.
bool m_using_internal_buffer
Flag indicating whether to use the buffer's internal previous state.
uint32_t m_feed_samples
Number of samples to feed back.
std::vector< double > m_previous_buffer
Storage for the previous system state.
void set_feedback(float amount)
Sets the feedback coefficient.
void set_feed_samples(uint32_t samples)
Sets the number of samples to feed back.
float m_feedback_amount
Feedback coefficient (0.0-1.0)
float get_feedback() const
Gets the current feedback coefficient.
Processor that implements recursive computational algorithms.