Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
sample_stream_window.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <cstddef>
5#include <cstdint>
6#include <vector>
7
9
10namespace pulp::audio {
11
13 std::uint32_t channels = 0;
14 std::uint32_t page_count = 0;
15 std::uint64_t page_frames = 0;
16};
17
25
27 // Nonzero identity for one logical streamed sample. Reads are always
28 // generation-qualified so old pages cannot satisfy a new sample request.
29 std::uint64_t stream_generation = 0;
30 std::uint64_t start_frame = 0;
31 std::uint64_t valid_frames = 0;
32 bool final_page = false;
33};
34
36 bool valid = false;
37 std::uint32_t page_index = 0;
38 std::uint64_t stream_generation = 0;
39 std::uint64_t start_frame = 0;
40 std::uint64_t valid_frames = 0;
41 std::uint64_t local_offset = 0;
42 bool final_page = false;
43};
44
46 std::uint64_t stream_generation = 0;
47 std::uint64_t start_frame = 0;
48 std::uint64_t frames = 0;
49 bool accumulate = false;
50 bool zero_fill_misses = true;
51};
52
54 std::uint64_t requested_frames = 0;
55 std::uint64_t copied_frames = 0;
56 std::uint64_t missed_frames = 0;
57 std::uint64_t zero_filled_frames = 0;
58 bool complete = false;
59};
60
62 std::uint64_t pages_published = 0;
63 std::uint64_t pages_retired = 0;
64 std::uint64_t ready_read_chunks = 0;
65 std::uint64_t ready_frames_read = 0;
66 std::uint64_t missed_frames = 0;
67 std::uint64_t zero_filled_frames = 0;
68 std::uint32_t ready_pages = 0;
69};
70
71// Prepared planar page storage for storage-backed sample streaming.
72//
73// This is a data-plane primitive only: it owns fixed page storage and page
74// metadata, but it does not own files, decoders, worker threads, prefetch
75// policy, voice state, or loop rendering. Background/importer code fills and
76// publishes pages; audio code performs generation-qualified ready-page lookup
77// and reads already-resident data only.
79public:
80 SampleStreamWindow() = default;
81
82 // Offline/quiescent only. Allocates all page storage and metadata. A failed
83 // prepare releases any previously prepared storage.
84 bool prepare(const SampleStreamWindowConfig& config);
85 // Offline/quiescent only; not safe while readers or publishers are live.
86 void release() noexcept;
87 // Quiescent/control-thread only. Clears pages and counters without freeing
88 // prepared storage.
89 void reset_when_quiescent() noexcept;
90
91 bool prepared() const noexcept { return prepared_; }
92 std::uint32_t channels() const noexcept { return channels_; }
93 std::uint32_t page_count() const noexcept { return page_count_; }
94 std::uint64_t page_frames() const noexcept { return page_frames_; }
95
96 SampleStreamPageState page_state(std::uint32_t page_index) const noexcept;
97
98 // Control/background-thread fill lifecycle. Single publisher ownership is
99 // expected. A Ready page must be retired before it can be filled again, and
100 // retired pages can only be reused once the caller reports an audio-safe
101 // generation at or beyond the page's retire-after generation.
102 bool begin_fill_page(std::uint32_t page_index,
103 std::uint64_t completed_audio_generation = 0) noexcept;
104 float* writable_channel_data(std::uint32_t page_index,
105 std::uint32_t channel) noexcept;
106 bool copy_to_filling_page(std::uint32_t page_index,
107 BufferView<const float> source,
108 std::uint64_t frames) noexcept;
109 bool copy_to_filling_page(std::uint32_t page_index,
110 BufferView<float> source,
111 std::uint64_t frames) noexcept;
112 bool publish_page(std::uint32_t page_index,
113 const SampleStreamPageDescriptor& descriptor) noexcept;
114 bool cancel_fill_page(std::uint32_t page_index) noexcept;
115 // retire_after_audio_generation must be nonzero; use reset_when_quiescent()
116 // for offline teardown instead of retiring a live page with no audio epoch.
117 bool retire_page(std::uint32_t page_index,
118 std::uint64_t retire_after_audio_generation) noexcept;
119
120 // RT-safe after prepare when page lifetimes are externally generation-gated.
121 // Lookup is a bounded linear scan over the prepared page window; keep
122 // page_count small until a higher-level indexed streaming cache exists.
124 std::uint64_t stream_generation,
125 std::uint64_t source_frame) const noexcept;
127 std::uint32_t channel) const noexcept;
129 BufferView<float> destination,
130 const SampleStreamWindowReadRequest& request) noexcept;
132
133private:
134 template<typename SampleType>
135 bool copy_to_filling_page_impl(std::uint32_t page_index,
136 BufferView<SampleType> source,
137 std::uint64_t frames) noexcept;
138
139 bool valid_page(std::uint32_t page_index) const noexcept;
140 bool descriptor_valid(const SampleStreamPageDescriptor& descriptor) const noexcept;
141 bool overlaps_ready_page(std::uint32_t page_index,
142 const SampleStreamPageDescriptor& descriptor) const noexcept;
143 float* mutable_page_channel(std::uint32_t page_index,
144 std::uint32_t channel) noexcept;
145 const float* page_channel(std::uint32_t page_index,
146 std::uint32_t channel) const noexcept;
147 void clear_page(std::uint32_t page_index) noexcept;
148 void clear_unprepared() noexcept;
149 std::uint64_t frames_until_next_ready_page(std::uint64_t stream_generation,
150 std::uint64_t source_frame,
151 std::uint64_t limit) const noexcept;
152
153 struct PageMetadata {
154 SampleStreamPageDescriptor descriptor{};
155 };
156
157 std::vector<float> storage_;
158 std::vector<std::atomic<std::uint8_t>> states_;
159 std::vector<PageMetadata> metadata_;
160 std::vector<std::atomic<std::uint64_t>> retire_after_generation_;
161
162 std::uint32_t channels_ = 0;
163 std::uint32_t page_count_ = 0;
164 std::uint64_t page_frames_ = 0;
165 bool prepared_ = false;
166
167 std::atomic<std::uint64_t> pages_published_{0};
168 std::atomic<std::uint64_t> pages_retired_{0};
169 std::atomic<std::uint64_t> ready_read_chunks_{0};
170 std::atomic<std::uint64_t> ready_frames_read_{0};
171 std::atomic<std::uint64_t> missed_frames_{0};
172 std::atomic<std::uint64_t> zero_filled_frames_{0};
173};
174
175} // namespace pulp::audio
Non-owning view over multi-channel audio sample data.
Definition buffer.hpp:30
void reset_when_quiescent() noexcept
bool retire_page(std::uint32_t page_index, std::uint64_t retire_after_audio_generation) noexcept
bool cancel_fill_page(std::uint32_t page_index) noexcept
SampleStreamPageView ready_page_for_frame(std::uint64_t stream_generation, std::uint64_t source_frame) const noexcept
std::uint32_t page_count() const noexcept
Definition sample_stream_window.hpp:93
SampleStreamPageState page_state(std::uint32_t page_index) const noexcept
bool prepare(const SampleStreamWindowConfig &config)
SampleStreamWindowStats stats() const noexcept
bool begin_fill_page(std::uint32_t page_index, std::uint64_t completed_audio_generation=0) noexcept
SampleStreamWindowReadResult read_frames(BufferView< float > destination, const SampleStreamWindowReadRequest &request) noexcept
const float * ready_channel_data(const SampleStreamPageView &view, std::uint32_t channel) const noexcept
bool prepared() const noexcept
Definition sample_stream_window.hpp:91
std::uint32_t channels() const noexcept
Definition sample_stream_window.hpp:92
bool publish_page(std::uint32_t page_index, const SampleStreamPageDescriptor &descriptor) noexcept
bool copy_to_filling_page(std::uint32_t page_index, BufferView< const float > source, std::uint64_t frames) noexcept
float * writable_channel_data(std::uint32_t page_index, std::uint32_t channel) noexcept
std::uint64_t page_frames() const noexcept
Definition sample_stream_window.hpp:94
Definition analyzer_provider.hpp:12
@ Retired
Definition sample_slot_bank.hpp:19
@ Empty
Definition sample_resource.hpp:21
@ Ready
Definition sample_starvation_envelope.hpp:10
SampleStreamPageState
Definition sample_stream_window.hpp:18
@ Retiring
Definition sample_stream_window.hpp:22
@ Filling
Definition sample_stream_window.hpp:20
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
STL namespace.
Definition sample_stream_window.hpp:26
std::uint64_t start_frame
Definition sample_stream_window.hpp:30
bool final_page
Definition sample_stream_window.hpp:32
std::uint64_t stream_generation
Definition sample_stream_window.hpp:29
std::uint64_t valid_frames
Definition sample_stream_window.hpp:31
Definition sample_stream_window.hpp:35
std::uint64_t local_offset
Definition sample_stream_window.hpp:41
std::uint32_t page_index
Definition sample_stream_window.hpp:37
std::uint64_t stream_generation
Definition sample_stream_window.hpp:38
bool final_page
Definition sample_stream_window.hpp:42
std::uint64_t start_frame
Definition sample_stream_window.hpp:39
bool valid
Definition sample_stream_window.hpp:36
std::uint64_t valid_frames
Definition sample_stream_window.hpp:40
Definition sample_stream_window.hpp:12
std::uint32_t channels
Definition sample_stream_window.hpp:13
std::uint32_t page_count
Definition sample_stream_window.hpp:14
std::uint64_t page_frames
Definition sample_stream_window.hpp:15
Definition sample_stream_window.hpp:45
std::uint64_t start_frame
Definition sample_stream_window.hpp:47
std::uint64_t frames
Definition sample_stream_window.hpp:48
bool accumulate
Definition sample_stream_window.hpp:49
bool zero_fill_misses
Definition sample_stream_window.hpp:50
std::uint64_t stream_generation
Definition sample_stream_window.hpp:46
Definition sample_stream_window.hpp:53
std::uint64_t copied_frames
Definition sample_stream_window.hpp:55
std::uint64_t missed_frames
Definition sample_stream_window.hpp:56
std::uint64_t requested_frames
Definition sample_stream_window.hpp:54
bool complete
Definition sample_stream_window.hpp:58
std::uint64_t zero_filled_frames
Definition sample_stream_window.hpp:57
Definition sample_stream_window.hpp:61
std::uint64_t pages_published
Definition sample_stream_window.hpp:62
std::uint64_t ready_frames_read
Definition sample_stream_window.hpp:65
std::uint64_t missed_frames
Definition sample_stream_window.hpp:66
std::uint64_t pages_retired
Definition sample_stream_window.hpp:63
std::uint64_t zero_filled_frames
Definition sample_stream_window.hpp:67
std::uint64_t ready_read_chunks
Definition sample_stream_window.hpp:64
std::uint32_t ready_pages
Definition sample_stream_window.hpp:68