28 reserve_channel_scratch();
32 reader_ = std::move(
reader);
34 reserve_channel_scratch();
46 reserve_channel_scratch();
55 int num_samples)
override {
57 const auto num_channels =
static_cast<uint32_t
>(out.
num_channels());
61 if (channel_scratch_.size() < num_channels)
62 channel_scratch_.resize(num_channels);
64 zero_fill(out, num_channels, start_sample, num_samples);
67 int frames_filled = 0;
68 while (frames_filled < num_samples) {
69 const int remaining = num_samples - frames_filled;
71 uint64_t available = 0;
72 if (length > read_pos_) available = length - read_pos_;
73 const int chunk = (length == 0)
75 : std::min<int>(remaining,
static_cast<int>(std::min<uint64_t>(
77 static_cast<uint64_t
>(remaining))));
79 if (looping_ && length > 0) {
84 zero_fill(out, num_channels,
85 start_sample + frames_filled, num_samples - frames_filled);
89 float** chunk_ptrs = channel_scratch_.data();
90 for (uint32_t c = 0; c < num_channels; ++c) {
91 chunk_ptrs[c] = out.
channel_ptr(c) + start_sample + frames_filled;
93 const bool ok = reader_->read_frames(chunk_ptrs,
96 static_cast<uint64_t
>(chunk));
98 zero_fill(out, num_channels,
99 start_sample + frames_filled, num_samples - frames_filled);
102 read_pos_ +=
static_cast<uint64_t
>(chunk);
103 frames_filled += chunk;
104 if (looping_ && length > 0 && read_pos_ >= length) {
114 if (length > 0 && new_position > length) new_position = length;
115 read_pos_ = new_position;
121 if (!reader_)
return 0;
122 return reader_->info().num_frames;
126 void set_looping(
bool should_loop)
override { looping_ = should_loop; }
131 void reserve_channel_scratch() {
132 if (!reader_)
return;
133 const auto needed =
static_cast<std::size_t
>(reader_->info().num_channels);
134 if (channel_scratch_.size() < needed) channel_scratch_.resize(needed);
137 static void zero_fill(BufferView<float>& out, uint32_t num_channels,
138 int start,
int count) {
139 for (uint32_t c = 0; c < num_channels; ++c) {
140 float* p = out.channel_ptr(c) + start;
141 for (
int i = 0; i < count; ++i) p[i] = 0.0f;
145 std::shared_ptr<MemoryMappedAudioReader> reader_;
148 std::vector<float*> channel_scratch_;
149 uint64_t read_pos_ = 0;
150 bool looping_ =
false;
Non-owning view over multi-channel audio sample data.
Definition buffer.hpp:30
std::size_t num_channels() const
Definition buffer.hpp:83
SampleType * channel_ptr(std::size_t index)
Raw pointer to one channel's sample data.
Definition buffer.hpp:68
Memory-mapped audio file reader.
Definition mmap_reader.hpp:19
AudioSource with a notion of position.
Definition source.hpp:60
Definition analyzer_provider.hpp:12
@ ok
Definition analyzer_provider.hpp:58
Audio source hierarchy — abstract bases for streaming audio playback graphs.