MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
InputAudioBuffer.cpp
Go to the documentation of this file.
2
4
5namespace MayaFlux::Buffers {
6
7InputAudioBuffer::InputAudioBuffer(uint32_t channel_id, uint32_t num_samples)
8 : AudioBuffer(channel_id, num_samples)
9{
10}
11
12void InputAudioBuffer::write_to(const std::shared_ptr<AudioBuffer>& buffer)
13{
14 if (!buffer) {
16 "InputAudioBuffer: Attempted to write to null buffer");
17 return;
18 }
19
20 const auto& src_data = get_data();
21 auto& dst_data = buffer->get_data();
22
23 if (dst_data.size() != src_data.size()) {
24 dst_data.resize(src_data.size());
25 }
26
27 std::ranges::copy(src_data, dst_data.begin());
28}
29
30void InputAudioBuffer::register_listener(const std::shared_ptr<AudioBuffer>& buffer)
31{
32 if (!buffer) {
34 "InputAudioBuffer: Attempted to register null listener");
35 return;
36 }
37
38 if (auto processor = std::dynamic_pointer_cast<InputAccessProcessor>(get_default_processor())) {
39 processor->add_listener(buffer);
40 }
41}
42
43void InputAudioBuffer::unregister_listener(const std::shared_ptr<AudioBuffer>& buffer)
44{
45 if (auto processor = std::dynamic_pointer_cast<InputAccessProcessor>(get_default_processor())) {
46 processor->remove_listener(buffer);
47 }
48}
49
51{
52 if (auto processor = std::dynamic_pointer_cast<InputAccessProcessor>(get_default_processor())) {
53 processor->clear_listeners();
54 }
55}
56
57void InputAccessProcessor::processing_function(const std::shared_ptr<Buffer>& buffer)
58{
59 auto input_buffer = std::dynamic_pointer_cast<InputAudioBuffer>(buffer);
60 if (!input_buffer)
61 return;
62
63 for (auto& listener : m_listeners) {
64 if (!listener)
65 continue;
66
67 input_buffer->write_to(listener);
68 }
69}
70
71void InputAccessProcessor::on_attach(const std::shared_ptr<Buffer>& buffer)
72{
73 auto input_buffer = std::dynamic_pointer_cast<InputAudioBuffer>(buffer);
74 if (!input_buffer) {
75 throw std::runtime_error("InputAccessProcessor can only be attached to InputAudioBuffer");
76 }
77}
78
79bool InputAccessProcessor::is_compatible_with(const std::shared_ptr<Buffer>& buffer) const
80{
81 return std::dynamic_pointer_cast<InputAudioBuffer>(buffer) != nullptr;
82}
83
84void InputAccessProcessor::add_listener(const std::shared_ptr<AudioBuffer>& buffer)
85{
86 if (!buffer)
87 return;
88
89 auto it = std::ranges::find(m_listeners, buffer);
90 if (it == m_listeners.end()) {
91 m_listeners.push_back(buffer);
92 }
93}
94
95void InputAccessProcessor::remove_listener(const std::shared_ptr<AudioBuffer>& buffer)
96{
97 auto it = std::ranges::find(m_listeners, buffer);
98 if (it != m_listeners.end()) {
99 m_listeners.erase(it);
100 }
101}
102
103}
#define MF_RT_ERROR(comp, ctx,...)
virtual std::vector< double > & get_data()
Gets mutable access to the buffer's underlying audio data.
virtual std::shared_ptr< BufferProcessor > get_default_processor() const override
Gets the current default audio transformation processor.
Concrete audio implementation of the Buffer interface for double-precision audio data.
void remove_listener(const std::shared_ptr< AudioBuffer > &buffer)
Removes a listener buffer.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
Main processing function - dispatches data to listeners.
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when processor is attached to a buffer.
void add_listener(const std::shared_ptr< AudioBuffer > &buffer)
Adds a listener buffer.
std::vector< std::shared_ptr< AudioBuffer > > m_listeners
bool is_compatible_with(const std::shared_ptr< Buffer > &buffer) const override
Checks compatibility with buffer type.
void write_to(const std::shared_ptr< AudioBuffer > &buffer)
Writes buffer data to a specific listener buffer.
void register_listener(const std::shared_ptr< AudioBuffer > &buffer)
Registers a buffer as a listener of this input.
void clear_listeners()
Clears all registered listeners.
InputAudioBuffer(uint32_t channel_id, uint32_t num_samples)
Constructor - only BufferManager can create.
void unregister_listener(const std::shared_ptr< AudioBuffer > &buffer)
Unregisters a listener buffer.
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ Buffers
Buffers, Managers, processors and processing chains.