MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
AudioContextProcessor.cpp
Go to the documentation of this file.
2
7
8namespace MayaFlux::Buffers {
9
11 : m_stage(std::move(stage))
12{
13 if (!factory) {
14 error<std::invalid_argument>(
16 std::source_location::current(),
17 "AudioContextProcessor: null context factory");
18 }
19
21
22 m_slots.resize(std::max<uint32_t>(depth, 1U));
23 for (auto& slot : m_slots) {
24 slot.context = factory();
25 if (!slot.context) {
26 error<std::invalid_argument>(
28 std::source_location::current(),
29 "AudioContextProcessor: factory returned a null context");
30 }
31 }
32}
33
38
39void AudioContextProcessor::on_attach(const std::shared_ptr<Buffer>& buffer)
40{
41 if (!std::dynamic_pointer_cast<AudioBuffer>(buffer)) {
43 "AudioContextProcessor can only attach to an AudioBuffer");
44 }
45}
46
47void AudioContextProcessor::on_detach(const std::shared_ptr<Buffer>& /*buffer*/)
48{
49 drain();
50}
51
52bool AudioContextProcessor::is_compatible_with(const std::shared_ptr<Buffer>& buffer) const
53{
54 return std::dynamic_pointer_cast<AudioBuffer>(buffer) != nullptr;
55}
56
58{
60 for (auto& slot : m_slots) {
61 if (slot.fence == Portal::Graphics::INVALID_FENCE) {
62 continue;
63 }
64 foundry.wait_for_fence(slot.fence);
66 }
67}
68
69void AudioContextProcessor::submit(Slot& slot, uint64_t sequence, std::span<const double> samples)
70{
71 if (m_stage) {
72 m_stage(*slot.context, samples, sequence);
73 } else {
74 slot.context->stage_words(0, samples);
75 slot.context->set_output_size(0, samples.size() * sizeof(uint64_t));
76 }
77
78 slot.sequence = sequence;
79 slot.fence = slot.context->dispatch_async(Yantra::Datum<std::vector<Kakshya::DataVariant>> {});
80
83 "AudioContextProcessor dispatch {} did not run", sequence);
84 }
85}
86
87void AudioContextProcessor::processing_function(const std::shared_ptr<Buffer>& buffer)
88{
89 auto audio = std::dynamic_pointer_cast<AudioBuffer>(buffer);
90 if (!audio) {
92 "AudioContextProcessor received a non-audio buffer");
93 return;
94 }
95
96 auto& samples = audio->get_data();
97 if (samples.empty()) {
98 return;
99 }
100
101 const uint64_t sequence = m_next++;
102 const uint64_t depth = m_slots.size();
103 const bool due = sequence >= depth;
104 const uint64_t expected = due ? sequence - depth : 0;
105 auto& slot = m_slots[sequence % depth];
106
107 std::vector<double> delivered;
108 bool busy = false;
109
110 if (slot.fence != Portal::Graphics::INVALID_FENCE) {
111 if (Portal::Graphics::get_shader_foundry().is_fence_signaled(slot.fence)) {
112 const auto result = slot.context->collect_result();
114 if (due && slot.sequence == expected) {
115 delivered = slot.context->decode_primary(result);
116 }
117 } else {
118 busy = true;
119 }
120 }
121
122 if (!busy) {
123 submit(slot, sequence, samples);
124 }
125
126 if (delivered.empty()) {
127 if (due) {
128 ++m_missed;
129 if (!m_missing) {
130 m_missing = true;
132 "AudioContextProcessor block {} was not complete, output is silent until results arrive in time", expected);
133 }
134 }
135 audio->clear();
136 return;
137 }
138
139 m_missing = false;
140
141 const size_t copied = std::min(samples.size(), delivered.size());
142 std::copy_n(delivered.begin(), copied, samples.begin());
143 std::fill(samples.begin() + static_cast<std::ptrdiff_t>(copied), samples.end(), 0.0);
144}
145
146} // namespace MayaFlux::Buffers
#define MF_ERROR(comp, ctx,...)
#define MF_RT_WARN(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
Runs a shader on doubles carried as two 32-bit words, with no fp64 on the device.
std::function< void(Yantra::WordPairExecutionContext &, std::span< const double >, uint64_t)> StageFunction
Fills a context for one job from the child's samples and the call's sequence number.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
The core processing function that must be implemented by derived classes.
void on_detach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is detached from a buffer.
void submit(Slot &slot, uint64_t sequence, std::span< const double > samples)
std::function< std::shared_ptr< Yantra::WordPairExecutionContext >()> ContextFactory
bool is_compatible_with(const std::shared_ptr< Buffer > &buffer) const override
Checks if this processor can handle the specified buffer type.
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is attached to a buffer.
AudioContextProcessor(const ContextFactory &factory, uint32_t depth=2, StageFunction stage={})
@ AUDIO_PARALLEL
High-performance audio processing with GPU acceleration.
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains).
@ Init
Engine/subsystem initialization.
@ Buffers
Buffers, Managers, processors and processing chains.
constexpr FenceID INVALID_FENCE
MAYAFLUX_API ShaderFoundry & get_shader_foundry()
Get the global shader compiler instance.
std::shared_ptr< Yantra::WordPairExecutionContext > context
Input/Output container for computation pipeline data flow with structure preservation.
Definition DataIO.hpp:63