MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
FilterProcessor.cpp
Go to the documentation of this file.
1#include "FilterProcessor.hpp"
3
5// #include "MayaFlux/Nodes/Filters/FIR.hpp"
6// #include "MayaFlux/Nodes/Filters/IIR.hpp"
7
8namespace MayaFlux::Buffers {
9
11{
12 if (m_use_internal) {
13 sample = m_filter->process_sample(sample);
14 return;
15 }
16
17 Nodes::atomic_inc_modulator_count(m_filter->m_modulator_count, 1);
18 uint32_t state = m_filter->m_state.load();
19
20 if (state & Utils::NodeState::PROCESSED) {
21 sample = m_filter->get_last_output();
22 } else {
23 sample = m_filter->process_sample(sample);
25 }
26
27 Nodes::atomic_dec_modulator_count(m_filter->m_modulator_count, 1);
29}
30
31void FilterProcessor::processing_function(std::shared_ptr<Buffer> buffer)
32{
33 if (!m_filter || !buffer)
34 return;
35
36 auto audio_buffer = std::dynamic_pointer_cast<AudioBuffer>(buffer);
37 if (!audio_buffer || audio_buffer->get_data().empty())
38 return;
39
40 if (m_pending_filter) {
42 m_pending_filter.reset();
43 m_use_internal = true;
44 }
45
46 auto& data = audio_buffer->get_data();
47
48 const auto& state = m_filter->m_state.load();
49
50 if (state == Utils::NodeState::INACTIVE) {
51 for (size_t i = 0; i < data.size(); ++i) {
52 m_filter->set_input_context(std::span<double>(data.data(), i));
53 data[i] = m_filter->process_sample(data[i]);
54 }
55 } else {
56 m_filter->save_state();
57 for (size_t i = 0; i < data.size(); ++i) {
58 m_filter->set_input_context(std::span<double>(data.data(), i));
59 data[i] = m_filter->process_sample(data[i]);
60 }
61 m_filter->restore_state();
62 }
63
64 m_filter->clear_input_context();
65}
66
67void FilterProcessor::on_attach(std::shared_ptr<Buffer> /*buffer*/)
68{
69 if (m_filter)
70 m_filter->reset();
71}
72
73} // namespace MayaFlux::Buffers
void processing_function(std::shared_ptr< Buffer > buffer) override
The core processing function that must be implemented by derived classes.
std::shared_ptr< Nodes::Filters::Filter > m_pending_filter
void process_single_sample(double &sample)
void on_attach(std::shared_ptr< Buffer > buffer) override
Called when this processor is attached to a buffer.
std::shared_ptr< Nodes::Filters::Filter > m_filter
void atomic_add_flag(std::atomic< Utils::NodeState > &state, Utils::NodeState flag)
Atomically adds a flag to a node state.
Definition NodeUtils.cpp:96
void try_reset_processed_state(std::shared_ptr< Node > node)
Attempts to reset the processed state of a node.
void atomic_inc_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically increments the modulator count by a specified amount.
void atomic_dec_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically decrements the modulator count by a specified amount.
@ INACTIVE
Engine is not processing this node.
Definition Utils.hpp:29
@ PROCESSED
Node has been processed this cycle.
Definition Utils.hpp:34