MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
AudioContextProcessor.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Yantra {
8}
9
10namespace MayaFlux::Buffers {
11
12class AudioBuffer;
13
14/**
15 * @class AudioContextProcessor
16 * @brief Runs a Yantra word pair context over a child AudioBuffer without blocking.
17 *
18 * Install as the default processor of a child AudioBuffer under AUDIO_PARALLEL.
19 * Each call stages the child's samples into one of depth contexts, dispatches
20 * it asynchronously and returns. The child receives the result of the call made
21 * depth calls earlier, so the output lags the input by depth calls and nothing
22 * waits on the GPU. A call whose result is not complete yields a silent child,
23 * the late result is discarded, and an earlier block is never replayed.
24 *
25 * Contexts come from a factory so each in-flight job owns its buffers. The
26 * default staging writes the samples to binding 0 as word pairs and reads the
27 * primary output back at the same size. A stage function replaces it when the
28 * shader takes other inputs.
29 *
30 * Usage:
31 * auto proc = std::make_shared<AudioContextProcessor>(make_context, 2);
32 * child->set_default_processor(proc);
33 */
34class MAYAFLUX_API AudioContextProcessor : public BufferProcessor {
35public:
36 using ContextFactory = std::function<std::shared_ptr<Yantra::WordPairExecutionContext>()>;
37
38 /**
39 * @brief Fills a context for one job from the child's samples and the call's sequence number.
40 */
41 using StageFunction = std::function<void(Yantra::WordPairExecutionContext&, std::span<const double>, uint64_t)>;
42
43 /**
44 * @param factory Builds one configured context per in-flight job. Called depth times.
45 * @param depth Calls between a block's input and its output. At least 1.
46 * @param stage Optional staging that replaces the default.
47 */
48 explicit AudioContextProcessor(const ContextFactory& factory, uint32_t depth = 2, StageFunction stage = {});
49
50 ~AudioContextProcessor() override;
51
56
57 void processing_function(const std::shared_ptr<Buffer>& buffer) override;
58 void on_attach(const std::shared_ptr<Buffer>& buffer) override;
59 void on_detach(const std::shared_ptr<Buffer>& buffer) override;
60 [[nodiscard]] bool is_compatible_with(const std::shared_ptr<Buffer>& buffer) const override;
61
62 [[nodiscard]] uint32_t depth() const { return static_cast<uint32_t>(m_slots.size()); }
63
64 /**
65 * @brief Calls whose output was silent although a result was due.
66 *
67 * A run of misses is logged once, when it starts. This is the total.
68 */
69 [[nodiscard]] uint64_t missed() const { return m_missed; }
70
71private:
72 struct Slot {
73 std::shared_ptr<Yantra::WordPairExecutionContext> context;
75 uint64_t sequence {};
76 };
77
79 std::vector<Slot> m_slots;
80 uint64_t m_next {};
81 uint64_t m_missed {};
82 bool m_missing {};
83
84 void submit(Slot& slot, uint64_t sequence, std::span<const double> samples);
85 void drain();
86};
87
88} // namespace MayaFlux::Buffers
Concrete audio implementation of the Buffer interface for double-precision audio data.
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.
AudioContextProcessor & operator=(const AudioContextProcessor &)=delete
uint64_t missed() const
Calls whose output was silent although a result was due.
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.
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.
AudioContextProcessor & operator=(AudioContextProcessor &&)=delete
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is attached to a buffer.
AudioContextProcessor(const AudioContextProcessor &)=delete
AudioContextProcessor(AudioContextProcessor &&)=delete
AudioContextProcessor(const ContextFactory &factory, uint32_t depth=2, StageFunction stage={})
Runs a Yantra word pair context over a child AudioBuffer without blocking.
Central computational transformation interface for continuous buffer processing.
Shader context whose numeric input is staged as a uvec2 per double.
constexpr FenceID INVALID_FENCE
std::shared_ptr< Yantra::WordPairExecutionContext > context