Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
sample_slot_bank.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
10
11namespace pulp::audio {
12
21
23 bool valid = false;
24 std::uint32_t slot_index = 0;
25 std::uint64_t generation = 0;
26 std::uint32_t num_channels = 0;
27 std::uint64_t num_frames = 0;
28 double sample_rate = 0.0;
29};
30
31// Fixed-capacity sample publication bank for RT readers.
32//
33// Publication/lifecycle mutation is single-owner: callers must externally
34// serialize reserve, write, complete, publish, release, reset,
35// acknowledge_audio_generation, and publish_from_buffer calls. Audio threads
36// only read PublishedSampleView snapshots and generation-aware channel pointers.
38public:
39 SampleSlotBank() = default;
40
41 // Offline/quiescent only. Allocates fixed slot storage; live replacement
42 // should publish into free slots and use generation acknowledgements.
43 bool prepare(std::uint32_t slot_count,
44 std::uint32_t max_channels,
45 std::uint64_t max_frames_per_slot);
46 // Offline/quiescent only; not safe while audio readers or publishers are live.
47 void reset() noexcept;
48 // Offline/quiescent only; releases storage and clears the capacity contract.
49 void release() noexcept;
50
51 std::uint32_t slot_count() const noexcept { return slot_count_; }
52 std::uint32_t max_channels() const noexcept { return max_channels_; }
53 std::uint64_t max_frames_per_slot() const noexcept { return max_frames_per_slot_; }
54
55 // Single-publisher control/background-thread publication path. Audio
56 // readers should use PublishedSampleView plus generation-aware channel_data().
57 int reserve_slot(std::uint32_t num_channels,
58 std::uint64_t num_frames,
59 double sample_rate) noexcept;
60
61 bool write_slot(std::uint32_t slot_index,
63 std::uint64_t frames) noexcept;
64 bool write_slot(std::uint32_t slot_index,
65 BufferView<float> source,
66 std::uint64_t frames) noexcept;
67
68 // Single-publisher control/background-thread helper for the normal
69 // publication path.
70 // Acknowledges the oldest audio-held generation first, then reserves,
71 // writes, completes, and publishes one free slot. Keep low-level calls
72 // for tests and specialized materializers that need explicit state checks.
74 std::uint64_t frames,
75 double sample_rate,
76 std::uint64_t audio_safe_generation = 0) noexcept;
77 bool publish_from_buffer(BufferView<float> source,
78 std::uint64_t frames,
79 double sample_rate,
80 std::uint64_t audio_safe_generation = 0) noexcept;
81
82 bool complete_slot(std::uint32_t slot_index) noexcept;
83 bool publish_slot(std::uint32_t slot_index) noexcept;
84 bool release_slot(std::uint32_t slot_index) noexcept;
85
86 PublishedSampleView read_published_view() const noexcept { return published_.read(); }
87 // Publisher-side acknowledgement of the oldest audio-held generation. Do
88 // not call directly from process(); feed the audio watermark to the
89 // serialized publication owner.
90 bool acknowledge_audio_generation(std::uint64_t generation) noexcept;
91
92 SampleSlotState slot_state(std::uint32_t slot_index) const noexcept;
93 std::uint32_t slot_num_channels(std::uint32_t slot_index) const noexcept;
94 std::uint64_t slot_num_frames(std::uint32_t slot_index) const noexcept;
95 double slot_sample_rate(std::uint32_t slot_index) const noexcept;
96 bool slot_view_valid(const PublishedSampleView& view) const noexcept;
98 std::uint32_t channel) const noexcept;
99 static std::uint64_t oldest_active_generation(
100 const PublishedSampleView& current,
101 const PublishedSampleView* active_views,
102 std::size_t active_count) noexcept;
103 // Controlled diagnostics/tests for manually managed slots. Published audio
104 // readers should use the generation-aware overload above.
105 const float* slot_channel_data_for_test(std::uint32_t slot_index,
106 std::uint32_t channel) const noexcept;
107 [[deprecated("Use channel_data(PublishedSampleView, channel) for audio reads; raw slot access is test/manual-only")]]
108 const float* channel_data(std::uint32_t slot_index, std::uint32_t channel) const noexcept;
109
110private:
111 struct SlotMetadata {
112 std::uint32_t num_channels = 0;
113 std::uint64_t num_frames = 0;
114 double sample_rate = 0.0;
115 };
116
117 template<typename SampleType>
118 bool write_slot_impl(std::uint32_t slot_index,
120 std::uint64_t frames) noexcept;
121 template<typename SampleType>
122 bool publish_from_buffer_impl(BufferView<SampleType> source,
123 std::uint64_t frames,
124 double sample_rate,
125 std::uint64_t audio_safe_generation) noexcept;
126
127 bool valid_slot(std::uint32_t slot_index) const noexcept;
128 float* mutable_channel_data(std::uint32_t slot_index, std::uint32_t channel) noexcept;
129 const float* slot_channel_data_unchecked(std::uint32_t slot_index,
130 std::uint32_t channel) const noexcept;
131 void clear_metadata(std::uint32_t slot_index) noexcept;
132
133 std::vector<float> storage_;
134 std::vector<std::atomic<std::uint8_t>> states_;
135 std::vector<std::atomic_bool> write_committed_;
136 std::vector<SlotMetadata> metadata_;
137 std::vector<std::atomic<std::uint64_t>> retire_after_generation_;
139 PublishedSampleView current_;
140 std::uint32_t slot_count_ = 0;
141 std::uint32_t max_channels_ = 0;
142 std::uint64_t max_frames_per_slot_ = 0;
143 std::uint64_t publish_generation_ = 0;
144
145 void clear_unprepared() noexcept;
146};
147
148} // namespace pulp::audio
Non-owning view over multi-channel audio sample data.
Definition buffer.hpp:30
const float * channel_data(std::uint32_t slot_index, std::uint32_t channel) const noexcept
bool write_slot(std::uint32_t slot_index, BufferView< float > source, std::uint64_t frames) noexcept
SampleSlotState slot_state(std::uint32_t slot_index) const noexcept
std::uint32_t slot_count() const noexcept
Definition sample_slot_bank.hpp:51
bool acknowledge_audio_generation(std::uint64_t generation) noexcept
std::uint64_t max_frames_per_slot() const noexcept
Definition sample_slot_bank.hpp:53
bool complete_slot(std::uint32_t slot_index) noexcept
const float * channel_data(const PublishedSampleView &view, std::uint32_t channel) const noexcept
int reserve_slot(std::uint32_t num_channels, std::uint64_t num_frames, double sample_rate) noexcept
const float * slot_channel_data_for_test(std::uint32_t slot_index, std::uint32_t channel) const noexcept
bool release_slot(std::uint32_t slot_index) noexcept
double slot_sample_rate(std::uint32_t slot_index) const noexcept
bool publish_slot(std::uint32_t slot_index) noexcept
bool publish_from_buffer(BufferView< const float > source, std::uint64_t frames, double sample_rate, std::uint64_t audio_safe_generation=0) noexcept
bool slot_view_valid(const PublishedSampleView &view) const noexcept
static std::uint64_t oldest_active_generation(const PublishedSampleView &current, const PublishedSampleView *active_views, std::size_t active_count) noexcept
PublishedSampleView read_published_view() const noexcept
Definition sample_slot_bank.hpp:86
bool write_slot(std::uint32_t slot_index, BufferView< const float > source, std::uint64_t frames) noexcept
std::uint32_t slot_num_channels(std::uint32_t slot_index) const noexcept
std::uint32_t max_channels() const noexcept
Definition sample_slot_bank.hpp:52
bool prepare(std::uint32_t slot_count, std::uint32_t max_channels, std::uint64_t max_frames_per_slot)
std::uint64_t slot_num_frames(std::uint32_t slot_index) const noexcept
Lock-free reader/writer for reading multi-field structs as consistent snapshots.
Definition seqlock.hpp:42
Definition analyzer_provider.hpp:12
SampleSlotState
Definition sample_slot_bank.hpp:13
@ Reserved
Definition sample_slot_bank.hpp:15
@ Published
Definition sample_slot_bank.hpp:18
@ Writing
Definition sample_slot_bank.hpp:16
@ Retired
Definition sample_slot_bank.hpp:19
@ Completed
Definition realtime_sample_recorder.hpp:19
@ Free
Definition instrument_voice_allocator.hpp:18
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
STL namespace.
Lock-free sequence-lock for coherent multi-field snapshots.
Definition sample_slot_bank.hpp:22
double sample_rate
Definition sample_slot_bank.hpp:28
bool valid
Definition sample_slot_bank.hpp:23
std::uint32_t slot_index
Definition sample_slot_bank.hpp:24
std::uint64_t generation
Definition sample_slot_bank.hpp:25
std::uint64_t num_frames
Definition sample_slot_bank.hpp:27
std::uint32_t num_channels
Definition sample_slot_bank.hpp:26