MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
StreamReaderNode.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Nodes {
6
7/**
8 * @class StreamReaderNode
9 * @brief Node that reads sample-by-sample from externally provided data
10 *
11 * Exposes set_data() to receive a block of samples from any source.
12 * Each process_sample() advances a read head through that data.
13 * When exhausted, returns the last valid sample (hold).
14 *
15 * The node does not know or care who calls set_data(). A buffer
16 * processor, a pipeline dispatch, a coroutine, manual user code,
17 * anything that has a span of doubles can feed it.
18 *
19 * Ownership: only one writer may feed data at a time. The first caller
20 * to set_data() with a non-null owner claims exclusive write access.
21 * Subsequent callers with a different owner are rejected until
22 * release_owner() is called.
23 */
24class MAYAFLUX_API StreamReaderNode final : public Node {
25public:
27
28 double process_sample(double input = 0.0) override;
29 std::vector<double> process_batch(unsigned int num_samples) override;
30
31 void save_state() override;
32 void restore_state() override;
33
34 NodeContext& get_last_context() override;
35
36 /**
37 * @brief Replace the internal data and reset read head to 0
38 * @param data Samples to read from. Copied internally.
39 * @param owner Feeder claiming write access. Rejected if another feeder owns this node.
40 * @return true if data was accepted, false if a different owner holds the lock
41 */
42 bool set_data(std::span<const double> data, const void* owner = nullptr);
43
44 /**
45 * @brief Release ownership so another feeder can write
46 * @param owner Must match the current owner
47 */
48 void release_owner(const void* owner);
49
50 /**
51 * @brief Number of unread samples remaining
52 */
53 [[nodiscard]] size_t remaining() const noexcept;
54
55 /**
56 * @brief Reset read head to 0 without changing data
57 */
58 void rewind() noexcept;
59
60protected:
61 void update_context(double value) override;
62 void notify_tick(double value) override;
63
64private:
67 : NodeContext(0.0, typeid(StreamReaderNode).name())
68 {
69 }
70 };
71
72 std::vector<double> m_data;
73 size_t m_read_head {};
74 double m_hold_value {};
75 std::atomic<const void*> m_owner { nullptr };
76
77 std::vector<double> m_saved_data;
78 size_t m_saved_read_head {};
79 double m_saved_hold_value {};
80 double m_saved_last_output {};
81
83};
84
85} // namespace MayaFlux::Nodes
Base context class for node callbacks.
Definition Node.hpp:30
Base interface for all computational processing nodes.
Definition Node.hpp:109
Node that reads sample-by-sample from externally provided data.
Contains the node-based computational processing system components.
Definition Chronie.hpp:11