MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
BlockFeedbackProcessor.cpp
Go to the documentation of this file.
2
4
5namespace MayaFlux::Buffers {
6
7BlockFeedbackProcessor::BlockFeedbackProcessor(Transform transform, size_t lag_blocks, std::vector<double> coefficients)
8 : m_transform(std::move(transform))
9 , m_lag_blocks(std::max<size_t>(lag_blocks, 1))
10 , m_coefficients(std::move(coefficients))
11{
12}
13
14void BlockFeedbackProcessor::on_attach(const std::shared_ptr<Buffer>& buffer)
15{
16 auto [found, inserted] = m_states.try_emplace(buffer.get());
17 if (inserted) {
18 found->second.coefs = m_coefficients;
19 }
20}
21
22void BlockFeedbackProcessor::on_detach(const std::shared_ptr<Buffer>& buffer)
23{
24 m_states.erase(buffer.get());
25}
26
27bool BlockFeedbackProcessor::is_compatible_with(const std::shared_ptr<Buffer>& buffer) const
28{
29 return std::dynamic_pointer_cast<AudioBuffer>(buffer) != nullptr;
30}
31
32double BlockFeedbackProcessor::feature(const std::shared_ptr<Buffer>& buffer) const
33{
34 const auto found = m_states.find(buffer.get());
35 return found == m_states.end() ? 0.0 : found->second.feature;
36}
37
38std::span<double> BlockFeedbackProcessor::coefficients(const std::shared_ptr<Buffer>& buffer)
39{
40 const auto found = m_states.find(buffer.get());
41 if (found == m_states.end()) {
42 return {};
43 }
44 return found->second.coefs;
45}
46
47std::shared_ptr<BlockFeedbackProcessor> BlockFeedbackProcessor::clone() const
48{
49 auto copy = std::make_shared<BlockFeedbackProcessor>(m_transform, m_lag_blocks, m_coefficients);
50 copy->m_retain_input = m_retain_input;
51 copy->m_observer = m_observer;
52 return copy;
53}
54
55void BlockFeedbackProcessor::processing_function(const std::shared_ptr<Buffer>& buffer)
56{
57 auto audio = std::dynamic_pointer_cast<AudioBuffer>(buffer);
58 if (!audio || !m_transform) {
59 return;
60 }
61
62 auto& data = audio->get_data();
63 const size_t samples = data.size();
64 if (samples == 0) {
65 return;
66 }
67
68 auto [found, inserted] = m_states.try_emplace(buffer.get());
69 State& state = found->second;
70 if (inserted) {
71 state.coefs = m_coefficients;
72 }
73
74 if (state.blocks.size() != m_lag_blocks || state.blocks.front().size() != samples) {
75 state.blocks.assign(m_lag_blocks, std::vector<double>(samples, 0.0));
76 state.kept.assign(samples, 0.0);
77 state.next = 0;
78 }
79
80 const std::vector<double>& previous = state.blocks.at(state.next);
81
82 if (m_retain_input) {
83 std::ranges::copy(data, state.kept.begin());
84 }
85
86 state.feature = m_transform(std::span<double>(data), std::span<const double>(previous), std::span<double>(state.coefs));
87
88 state.blocks.at(state.next) = m_retain_input ? state.kept : data;
89 state.next = (state.next + 1) % m_lag_blocks;
90
91 if (m_observer) {
92 m_observer(state.feature);
93 }
94}
95
96} // namespace MayaFlux::Buffers
std::shared_ptr< NetworkGeometryBuffer > buffer
glm::ivec3 max
std::unordered_map< const Buffer *, State > m_states
bool is_compatible_with(const std::shared_ptr< Buffer > &buffer) const override
Checks if this processor can handle the specified buffer type.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
The core processing function that must be implemented by derived classes.
BlockFeedbackProcessor(Transform transform, size_t lag_blocks=1, std::vector< double > coefficients={})
std::span< double > coefficients(const std::shared_ptr< Buffer > &buffer)
The coefficient array of one attached buffer, empty if it is not attached.
std::function< double(std::span< double >, std::span< const double >, std::span< double >)> Transform
The per-block function.
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is attached to a buffer.
void on_detach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is detached from a buffer.
std::shared_ptr< BlockFeedbackProcessor > clone() const
Same configuration, no attached state.
double feature(const std::shared_ptr< Buffer > &buffer) const
The feature of the last block of one attached buffer, zero if none.