MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
FeedbackBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
5
7
8namespace MayaFlux::Buffers {
9
10/**
11 * @class FeedbackBuffer
12 * @brief Buffer with temporal memory for recursive processing
13 *
14 * FeedbackBuffer extends AudioBuffer with a HistoryBuffer that maintains
15 * the previous processing state, enabling delay-line feedback and recursive
16 * algorithms. The history buffer provides proper temporal indexing where
17 * [0] = most recent sample and [k] = k samples ago.
18 *
19 * The feedback path is:
20 * output[n] = input[n] + feedback_amount * history[feed_samples]
21 *
22 * For filtering in the feedback path, attach a FilterProcessor to the
23 * buffer's processing chain rather than relying on hardcoded averaging.
24 */
25class MAYAFLUX_API FeedbackBuffer : public AudioBuffer {
26public:
27 /**
28 * @brief Construct feedback buffer
29 * @param channel_id Audio channel assignment
30 * @param num_samples Buffer size in samples
31 * @param feedback Feedback coefficient (0.0 to 1.0)
32 * @param feed_samples Delay length in samples
33 */
34 FeedbackBuffer(uint32_t channel_id, uint32_t num_samples,
35 float feedback = 0.5F, uint32_t feed_samples = 512);
36
37 /**
38 * @brief Get feedback coefficient
39 */
40 [[nodiscard]] inline float get_feedback() const { return m_feedback_amount; }
41
42 /**
43 * @brief Set feedback coefficient
44 * @param amount Feedback coefficient (0.0 to 1.0)
45 *
46 * Propagates to the default processor if one is attached.
47 */
48 void set_feedback(float amount);
49
50 /**
51 * @brief Get mutable access to the history buffer
52 * @return Reference to the HistoryBuffer storing previous state
53 */
54 inline Memory::HistoryBuffer<double>& get_history_buffer() { return m_history; }
55
56 /**
57 * @brief Get read-only access to the history buffer
58 * @return Const reference to the HistoryBuffer storing previous state
59 */
60 [[nodiscard]] inline const Memory::HistoryBuffer<double>& get_history_buffer() const { return m_history; }
61
62 void process_default() override;
63
64 /**
65 * @brief Set delay length in samples
66 * @param samples New delay length
67 *
68 * Reconstructs the history buffer. Previous state is lost.
69 */
70 void set_feed_samples(uint32_t samples);
71
72 /**
73 * @brief Get delay length in samples
74 */
75 [[nodiscard]] inline uint32_t get_feed_samples() const { return m_feed_samples; }
76
77protected:
78 std::shared_ptr<BufferProcessor> create_default_processor() override;
79
80private:
84};
85
86/**
87 * @class FeedbackProcessor
88 * @brief Processor implementing delay-line feedback via HistoryBuffer
89 *
90 * Applies a simple delay-line feedback algorithm:
91 * output[n] = input[n] + feedback_amount * delayed_sample
92 *
93 * When attached to a FeedbackBuffer, uses its internal HistoryBuffer.
94 * When attached to any other AudioBuffer, maintains its own HistoryBuffer.
95 *
96 * For filtering in the feedback loop (lowpass damping, etc.), chain a
97 * FilterProcessor after this processor rather than embedding filter logic.
98 */
99class MAYAFLUX_API FeedbackProcessor : public BufferProcessor {
100public:
101 /**
102 * @brief Construct feedback processor
103 * @param feedback Feedback coefficient (0.0 to 1.0)
104 * @param feed_samples Delay length in samples
105 */
106 FeedbackProcessor(float feedback = 0.5F, uint32_t feed_samples = 512);
107
108 void processing_function(const std::shared_ptr<Buffer>& buffer) override;
109 void on_attach(const std::shared_ptr<Buffer>& buffer) override;
110 void on_detach(const std::shared_ptr<Buffer>& buffer) override;
111
112 /**
113 * @brief Set feedback coefficient
114 */
115 inline void set_feedback(float amount) { m_feedback_amount = amount; }
116
117 /**
118 * @brief Get feedback coefficient
119 */
120 [[nodiscard]] inline float get_feedback() const { return m_feedback_amount; }
121
122 /**
123 * @brief Set delay length in samples
124 */
125 void set_feed_samples(uint32_t samples);
126
127 /**
128 * @brief Get delay length in samples
129 */
130 [[nodiscard]] inline uint32_t get_feed_samples() const { return m_feed_samples; }
131
132private:
135
137 Memory::HistoryBuffer<double>* m_active_history { nullptr };
138};
139
140} // namespace MayaFlux::Buffers
Concrete audio implementation of the Buffer interface for double-precision audio data.
Central computational transformation interface for continuous buffer processing.
uint32_t get_feed_samples() const
Get delay length in samples.
const Memory::HistoryBuffer< double > & get_history_buffer() const
Get read-only access to the history buffer.
Memory::HistoryBuffer< double > m_history
float get_feedback() const
Get feedback coefficient.
Memory::HistoryBuffer< double > & get_history_buffer()
Get mutable access to the history buffer.
Buffer with temporal memory for recursive processing.
uint32_t get_feed_samples() const
Get delay length in samples.
void set_feedback(float amount)
Set feedback coefficient.
Memory::HistoryBuffer< double > m_history
float get_feedback() const
Get feedback coefficient.
Processor implementing delay-line feedback via HistoryBuffer.
History buffer for difference equations and recursive relations.