MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
AudioBuffer.cpp
Go to the documentation of this file.
1#include "AudioBuffer.hpp"
2
4
7
8namespace MayaFlux::Buffers {
9
14
15AudioBuffer::AudioBuffer(uint32_t channel_id, uint32_t num_samples)
16 : m_channel_id(channel_id)
17 , m_num_samples(num_samples)
18 , m_default_processor(nullptr)
19 , m_has_data(true)
20 , m_should_remove(false)
21 , m_process_default(true)
22 , m_processing_chain(std::make_shared<BufferProcessingChain>())
23 , m_is_processing(false)
24{
25 if (num_samples != s_preferred_buffer_size) {
27 "AudioBuffer initialized with a non-default number of samples ({}). This may lead to unexpected behavior.",
28 num_samples);
29 }
30 m_data.resize(num_samples);
31}
32
33void AudioBuffer::setup(uint32_t channel, uint32_t num_samples)
34{
35 m_channel_id = channel;
36 resize(num_samples);
37}
38
39void AudioBuffer::resize(uint32_t num_samples)
40{
41 m_num_samples = num_samples;
42 m_data.resize(num_samples);
43}
44
46{
47 std::ranges::fill(m_data, 0.0);
48}
49
50void AudioBuffer::set_num_samples(uint32_t num_samples)
51{
52 m_num_samples = num_samples;
53 m_data.resize(num_samples);
54}
55
57{
59 m_default_processor->process(shared_from_this());
60 }
61}
62
63void AudioBuffer::set_default_processor(const std::shared_ptr<BufferProcessor>& processor)
64{
65 try {
67 m_default_processor->on_detach(shared_from_this());
68 }
69 if (processor) {
70 processor->on_attach(shared_from_this());
71 }
72 m_default_processor = processor;
73 } catch (const std::exception& e) {
75 std::source_location::current(),
76 "Error setting default processor: {}", e.what());
77 }
78}
79
80std::shared_ptr<Buffer> AudioBuffer::clone_to(uint8_t dest_desc)
81{
82 auto buf = clone_to(static_cast<uint32_t>(dest_desc));
83 return std::dynamic_pointer_cast<Buffer>(buf);
84}
85
86std::shared_ptr<AudioBuffer> AudioBuffer::clone_to(uint32_t channel)
87{
88 auto buffer = std::make_shared<AudioBuffer>(channel, m_num_samples);
89 buffer->get_data() = m_data;
90 buffer->set_default_processor(m_default_processor);
91 buffer->set_processing_chain(get_processing_chain(), true);
92
93 return buffer;
94}
95
96bool AudioBuffer::read_once(const std::shared_ptr<AudioBuffer>& buffer, bool force)
97{
98 if (buffer && buffer->get_num_samples() == m_num_samples) {
99 if (m_is_processing.load() || buffer->is_processing()) {
101 "read_once: Attempting to read from an audio buffer while it is being processed.");
102
103 if (!force) {
105 "read_once: Skipping read due to ongoing processing.");
106 return false;
107 }
109 "read_once: Forcing read despite ongoing processing. This may lead to data corruption.");
110 }
111 m_data = buffer->get_data();
112 m_has_data = true;
113 return true;
114 }
115
117 "read_once: Buffer read failed due to size mismatch or null buffer.");
118
119 return false;
120}
121
122void AudioBuffer::set_processing_chain(const std::shared_ptr<BufferProcessingChain>& chain, bool force)
123{
124 if (m_processing_chain && !force) {
125 m_processing_chain->merge_chain(chain);
126 return;
127 }
128 m_processing_chain = chain;
129}
130
131}
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
bool m_has_data
Whether the audio buffer has data to process this cycle.
virtual void setup(uint32_t channel, uint32_t num_samples)
Initializes the audio buffer with specified channel and capacity.
void clear() override
Resets all audio samples in the buffer to silence.
virtual bool read_once(const std::shared_ptr< AudioBuffer > &buffer, bool force=false)
Reads audio data into the buffer from the audio backend.
uint32_t m_channel_id
Audio channel identifier for this buffer.
std::atomic< bool > m_is_processing
void process_default() override
Applies the default audio transformation to the buffer's data.
uint32_t m_num_samples
Capacity of the buffer in audio samples.
virtual void resize(uint32_t num_samples)
Adjusts the audio buffer's sample capacity.
std::shared_ptr< BufferProcessingChain > m_processing_chain
Audio transformation processing chain for this buffer.
std::shared_ptr< BufferProcessor > m_default_processor
Default audio transformation processor for this buffer.
virtual void set_num_samples(uint32_t num_samples)
Sets the capacity of the audio buffer.
void set_processing_chain(const std::shared_ptr< BufferProcessingChain > &chain, bool force=false) override
Sets the audio transformation chain for this buffer.
void set_default_processor(const std::shared_ptr< BufferProcessor > &processor) override
Sets the default audio transformation processor for this buffer.
std::vector< double > m_data
Vector storing the actual double-precision audio sample data.
std::shared_ptr< BufferProcessingChain > get_processing_chain() override
Gets the audio transformation chain attached to this buffer.
AudioBuffer()
Creates a new uninitialized audio buffer.
bool m_process_default
Whether the audio buffer should be processed using its default processor.
virtual std::shared_ptr< AudioBuffer > clone_to(uint32_t channel)
Creates a clone of this audio buffer for a specific channel.
Concrete audio implementation of the Buffer interface for double-precision audio data.
Advanced pipeline manager for multi-stage buffer transformations with backend optimization.
static uint32_t s_preferred_buffer_size
Global default buffer size.
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ Init
Engine/subsystem initialization.
@ Buffers
Buffers, Managers, processors and processing chains.