Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
probability_gate.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <pulp/signal/detail/modular_sequencing_common.hpp>
4#include <pulp/signal/rng.hpp>
6
7#include <algorithm>
8#include <cmath>
9#include <cstdint>
10
11namespace pulp::signal {
12
38template <typename SampleType = float>
39class ProbGateT {
40public:
43 static constexpr double kDefaultProbability = 0.5;
44
46 static constexpr std::uint32_t kProbSeed = 0x1234567u;
47
48 ProbGateT() { rng_.set_seed(kProbSeed); }
49
50 void prepare(double) {}
51
52 void set_probability(double p) {
53 if (std::isfinite(p)) probability_ = std::clamp(p, 0.0, 1.0);
54 }
55 double probability() const { return probability_; }
56
57 void set_seed(std::uint32_t seed) { rng_.set_seed(seed); }
58
60 void apply_reset_edge() { detector_.reset(); }
61
63 void reset() {
64 detector_.reset();
65 rng_.reset();
66 draws_ = 0;
67 }
68
69 static constexpr int latency_samples() { return 0; }
70
73 std::uint32_t draw_count() const { return draws_; }
74
76 bool process_edge(bool trigger_edge) {
77 if (!trigger_edge) return false;
78 ++draws_;
79 return rng_.next_unit<double>() < probability_;
80 }
81
84 bool process(SampleType trigger) { return process_edge(detector_.process(trigger)); }
85
86private:
87 double probability_ = kDefaultProbability;
88 std::uint32_t draws_ = 0;
90 HystereticTriggerDetectT<SampleType> detector_{};
91};
92
95
96} // namespace pulp::signal
Passes each incoming trigger with a stated probability, deterministically.
Definition probability_gate.hpp:39
void prepare(double)
Definition probability_gate.hpp:50
double probability() const
Definition probability_gate.hpp:55
static constexpr int latency_samples()
Definition probability_gate.hpp:69
ProbGateT()
Definition probability_gate.hpp:48
void apply_reset_edge()
Verb 2: clears the edge-detector latch only. The RNG stream is untouched.
Definition probability_gate.hpp:60
void reset()
Verb 1: clears the latch and rewinds the stream to the seed.
Definition probability_gate.hpp:63
bool process(SampleType trigger)
Detects the edge from a trigger signal and decides it, so this block can sit directly on a clock outp...
Definition probability_gate.hpp:84
static constexpr double kDefaultProbability
Definition probability_gate.hpp:43
std::uint32_t draw_count() const
Number of draws consumed since the last reset() — the stream position, exposed so a test can assert i...
Definition probability_gate.hpp:73
void set_seed(std::uint32_t seed)
Definition probability_gate.hpp:57
void set_probability(double p)
Definition probability_gate.hpp:52
bool process_edge(bool trigger_edge)
Decides one already-detected trigger edge.
Definition probability_gate.hpp:76
static constexpr std::uint32_t kProbSeed
Definition probability_gate.hpp:46
Xorshift32 (Marsaglia, "Xorshift RNGs", J.
Definition rng.hpp:33
Definition additive_bank.hpp:218
ProbGateT< double > ProbGate64
Definition probability_gate.hpp:94
ProbGateT< float > ProbGate
Definition probability_gate.hpp:93
Deterministic randomness for modulation: a counter-free PRNG, a Gaussian draw, a stateless purpose-ke...