MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
AudioOutputAccessProcessor.cpp
Go to the documentation of this file.
2
4
7
9
10namespace MayaFlux::Kakshya {
11
13 : m_buffer_size(buffer_size)
14 , m_backend_service(
15 Registry::BackendRegistry::instance()
16 .get_service<Registry::Service::AudioBackendService>())
17{
18 if (!m_backend_service) {
20 "AudioOutputAccessProcessor: AudioBackendService not yet registered"
21 "process() calls will be no-ops until the service is available");
22 }
23}
24
26 const std::shared_ptr<SignalSourceContainer>& container)
27{
28 auto ac = std::dynamic_pointer_cast<AudioOutputContainer>(container);
29 if (!ac) {
30 error<std::invalid_argument>(
33 std::source_location::current(),
34 "AudioOutputAccessProcessor requires an AudioOutputContainer");
35 }
36
37 m_channel_count = ac->get_structure().get_channel_count();
38 m_organization = ac->get_structure().organization;
39
40 ac->mark_ready_for_processing(true);
41
43 "AudioOutputAccessProcessor attached: {} channels, {} frames/cycle, {}",
45 m_organization == OrganizationStrategy::PLANAR ? "planar" : "interleaved");
46}
47
49 const std::shared_ptr<SignalSourceContainer>& /*container*/)
50{
52 m_buffer_size = 0;
53}
54
56 const std::shared_ptr<SignalSourceContainer>& container)
57{
58 if (m_is_processing.exchange(true, std::memory_order_acq_rel))
59 return;
60
61 auto ac = std::dynamic_pointer_cast<AudioOutputContainer>(container);
62 if (!ac) {
64 "AudioOutputAccessProcessor requires an AudioOutputContainer");
65 m_is_processing.store(false, std::memory_order_release);
66 return;
67 }
68
69 if (!m_backend_service) {
71 "AudioBackendService unavailable");
72 m_is_processing.store(false, std::memory_order_release);
73 return;
74 }
75
76 const auto snap = m_backend_service->get_output_snapshot();
77 if (snap.empty()) {
79 "AudioOutputAccessProcessor: snapshot empty, no cycle completed yet");
80 m_is_processing.store(false, std::memory_order_release);
81 return;
82 }
83
84 ac->update_processing_state(ProcessingState::PROCESSING);
85
86 thread_local std::vector<std::vector<double>> tl_channels;
87
88 tl_channels.resize(m_channel_count);
89 for (auto& ch : tl_channels)
90 ch.resize(m_buffer_size);
91
93 for (uint32_t ch = 0; ch < m_channel_count; ++ch) {
94 for (uint32_t f = 0; f < m_buffer_size; ++f) {
95 tl_channels[ch][f] = snap[f * m_channel_count + ch];
96 }
97 }
98 } else {
99 tl_channels[0].assign(snap.begin(), snap.end());
100 }
101
102 {
103
104 std::unique_lock lock(ac->m_data_mutex);
105 auto& pd = ac->get_processed_data();
107 pd.resize(m_channel_count);
108 for (uint32_t ch = 0; ch < m_channel_count; ++ch)
109 pd[ch] = DataVariant(tl_channels[ch]);
110 } else {
111 pd.resize(1);
112 pd[0] = DataVariant(tl_channels[0]);
113 }
114 }
115
116 const uint64_t write_head = ac->get_num_frames();
117
118 {
119 std::unique_lock lock(ac->m_data_mutex);
121 if (ac->m_data.size() < m_channel_count)
122 ac->m_data.resize(m_channel_count, DataVariant(std::vector<double> {}));
123 for (uint32_t ch = 0; ch < m_channel_count; ++ch) {
124 auto& vec = std::get<std::vector<double>>(ac->m_data[ch]);
125 vec.insert(vec.end(), tl_channels[ch].begin(), tl_channels[ch].end());
126 }
127 } else {
128 if (ac->m_data.empty())
129 ac->m_data.resize(1, DataVariant(std::vector<double> {}));
130 auto& vec = std::get<std::vector<double>>(ac->m_data[0]);
131 vec.insert(vec.end(), tl_channels[0].begin(), tl_channels[0].end());
132 }
133 ac->m_num_frames += m_buffer_size;
134 ac->setup_dimensions();
135 ac->invalidate_span_cache();
136 ac->m_double_extraction_dirty.store(true, std::memory_order_release);
137 }
138
139 ac->update_processing_state(ProcessingState::PROCESSED);
140 m_is_processing.store(false, std::memory_order_release);
141}
142
143} // namespace MayaFlux::Kakshya
#define MF_INFO(comp, ctx,...)
#define MF_RT_WARN(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
AudioOutputAccessProcessor(uint32_t buffer_size)
Construct with the fixed output block size.
void on_attach(const std::shared_ptr< SignalSourceContainer > &container) override
Validate container type, cache structure, mark ready for processing.
Registry::Service::AudioBackendService * m_backend_service
void process(const std::shared_ptr< SignalSourceContainer > &container) override
Pull engine snapshot, write m_processed_data, append to m_data.
void on_detach(const std::shared_ptr< SignalSourceContainer > &container) override
Clear cached state.
@ Configuration
Configuration and parameter updates.
@ ContainerProcessing
Container operations (Kakshya - file/stream/region processing)
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
@ PROCESSING
Container is actively being processed.
@ PROCESSED
Container has completed processing and results are available.
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.
Definition NDData.hpp:76
@ PLANAR
Separate DataVariant per logical unit (LLL...RRR for stereo)
std::function< std::span< const double >()> get_output_snapshot
Returns a span over the last committed interleaved output buffer.