Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
source_player.hpp
Go to the documentation of this file.
1#pragma once
2
7
9
10#include <cstddef>
11
12namespace pulp::audio {
13
23public:
24 AudioSourcePlayer() = default;
25
29 if (source_ == source) return;
30 if (source_ && prepared_) source_->release_resources();
31 source_ = source;
32 prepared_ = false;
33 last_sample_rate_ = 0.0;
34 last_block_size_ = 0;
35 }
36
37 AudioSource* source() const { return source_; }
38
40 void set_gain(float gain) { gain_ = gain; }
41 float gain() const { return gain_; }
42
46 int num_samples,
47 double sample_rate) {
48 if (num_samples <= 0 || out.num_channels() == 0) return;
49 if (!source_) {
50 fill_silence(out, num_samples);
51 return;
52 }
53 if (!prepared_
54 || sample_rate != last_sample_rate_
55 || num_samples > last_block_size_) {
56 source_->prepare_to_play(num_samples, sample_rate);
57 last_sample_rate_ = sample_rate;
58 last_block_size_ = num_samples;
59 prepared_ = true;
60 }
61 source_->get_next_audio_block(out, /*start_sample=*/0, num_samples);
62 if (gain_ != 1.0f) {
63 for (std::size_t ch = 0; ch < out.num_channels(); ++ch) {
64 float* ptr = out.channel_ptr(ch);
65 for (int i = 0; i < num_samples; ++i) ptr[i] *= gain_;
66 }
67 }
68 }
69
72 void release() {
73 if (source_ && prepared_) source_->release_resources();
74 prepared_ = false;
75 last_sample_rate_ = 0.0;
76 last_block_size_ = 0;
77 }
78
79private:
80 static void fill_silence(BufferView<float> out, int n) {
81 for (std::size_t ch = 0; ch < out.num_channels(); ++ch) {
82 float* ptr = out.channel_ptr(ch);
83 for (int i = 0; i < n; ++i) ptr[i] = 0.0f;
84 }
85 }
86
87 AudioSource* source_ = nullptr;
88 float gain_ = 1.0f;
89 bool prepared_ = false;
90 double last_sample_rate_ = 0.0;
91 int last_block_size_ = 0;
92};
93
94} // namespace pulp::audio
void set_gain(float gain)
Overall gain applied after the source's render. Default 1.0.
Definition source_player.hpp:40
void release()
Manually release the source's resources (mirrors what set_source(nullptr) does for the prior source).
Definition source_player.hpp:72
AudioSource * source() const
Definition source_player.hpp:37
void audio_callback(BufferView< float > out, int num_samples, double sample_rate)
Call from the host's audio callback.
Definition source_player.hpp:45
void set_source(AudioSource *source)
Bind a source (or nullptr to detach).
Definition source_player.hpp:28
float gain() const
Definition source_player.hpp:41
Abstract base — host-callable audio source.
Definition source.hpp:33
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
Definition analyzer_provider.hpp:12
Audio source hierarchy — abstract bases for streaming audio playback graphs.