MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
AudioWriteProcessor.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Buffers {
7
8class AudioBuffer;
9
10/**
11 * @class AudioWriteProcessor
12 * @brief Unconditionally writes externally-supplied data into an AudioBuffer each cycle.
13 *
14 * Holds a snapshot of double-precision samples. On every processing cycle the
15 * snapshot is copied into the attached buffer verbatim — no mixing, no gating,
16 * no freshness check. If no snapshot has been set the buffer is zeroed.
17 *
18 * Timing and synchronisation with the supplier are the caller's responsibility.
19 * The processor only guarantees that whatever was last set via set_data() will
20 * appear in the buffer on the next cycle.
21 *
22 * Thread safety: set_data() and processing_function() may run concurrently.
23 * A lock-free pending/active double-buffer swap ensures the audio thread never
24 * blocks on the supplier thread.
25 *
26 * DataVariant overload converts to double via Kakshya::extract_from_variant<double>
27 * using a thread-local staging vector, covering all active variant types without
28 * allocation on the hot path after the first call.
29 */
30class MAYAFLUX_API AudioWriteProcessor : public BufferProcessor {
31public:
33 ~AudioWriteProcessor() override = default;
34
35 /**
36 * @brief Supply the next frame of double-precision samples.
37 * @param data Samples to write into the buffer on the next cycle.
38 * If size differs from the attached buffer's frame count, the
39 * write is truncated or zero-padded to fit.
40 */
41 void set_data(std::vector<double> data);
42
43 /**
44 * @brief Supply data from any DataVariant — converted to double via EigenAccess.
45 * @param variant Source variant.
46 * Arithmetic/complex types: converted via EigenAccess::to_vector()
47 * (complex uses magnitude).
48 * GLM vector/matrix types: converted via EigenAccess::to_matrix(),
49 * then flattened column-major: [x0,y0,z0, x1,y1,z1, ...].
50 */
51 void set_data(const Kakshya::DataVariant& variant);
52
53 /**
54 * @brief Supply data as a raw float span — upconverted to double.
55 * @param data Span of float samples.
56 */
57 void set_data(std::span<const float> data);
58
59 /**
60 * @brief Supply data as a raw double span — copied directly.
61 * @param data Span of double samples.
62 */
63 void set_data(std::span<const double> data);
64
65 /**
66 * @brief Clear any pending snapshot. Buffer will be zeroed on the next cycle.
67 */
68 void clear();
69
70 /**
71 * @brief Returns true if a snapshot has been set and not yet consumed.
72 */
73 [[nodiscard]] bool has_pending() const noexcept;
74
75 void processing_function(const std::shared_ptr<Buffer>& buffer) override;
76 void on_attach(const std::shared_ptr<Buffer>& buffer) override;
77
78 [[nodiscard]] bool is_compatible_with(const std::shared_ptr<Buffer>& buffer) const override;
79
80private:
81 // Pending slot: supplier writes here.
82 std::vector<double> m_pending;
83 // Active slot: audio thread reads from here.
84 std::vector<double> m_active;
85 // Set when m_pending contains unseen data.
86 std::atomic_flag m_dirty;
87
88 void commit_pending();
89 void write_to_buffer(AudioBuffer& buf) const;
90};
91
92} // namespace MayaFlux::Buffers
Concrete audio implementation of the Buffer interface for double-precision audio data.
Unconditionally writes externally-supplied data into an AudioBuffer each cycle.
Central computational transformation interface for continuous buffer processing.
Backend-agnostic interface for sequential data storage and transformation.
Definition Buffer.hpp:37
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