Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
pull_rate_detector.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <array>
7#include <chrono>
8#include <cstddef>
9
10namespace pulp::audio {
11
30 public:
31 using Clock = std::chrono::steady_clock;
32 static constexpr std::size_t kWindow = 8;
33 static constexpr double kEnter = 2.0;
34 static constexpr double kLeave = 1.25;
35
38 bool observe(std::size_t frames, double sample_rate, Clock::time_point now) noexcept {
39 calls_[next_ % kWindow] = {now, frames};
40 ++next_;
41 if (next_ < kWindow || !(sample_rate > 0.0))
42 return fast_;
43 // The oldest call in the window starts the wall-clock span; the audio
44 // of the newer calls is what was delivered within that span.
45 const auto& oldest = calls_[next_ % kWindow];
46 std::size_t newer_frames = 0;
47 for (std::size_t i = 1; i < kWindow; ++i)
48 newer_frames += calls_[(next_ + i) % kWindow].frames;
49 const double audio_s = static_cast<double>(newer_frames) / sample_rate;
50 const double wall_s = std::chrono::duration<double>(now - oldest.time).count();
51 speed_ = wall_s > 0.0 ? audio_s / wall_s : kEnter * 1000.0;
52 fast_ = fast_ ? speed_ >= kLeave : speed_ > kEnter;
53 return fast_;
54 }
55
56 bool fast() const noexcept {
57 return fast_;
58 }
59
61 double speed() const noexcept {
62 return speed_;
63 }
64
66 void reset() noexcept {
67 next_ = 0;
68 fast_ = false;
69 speed_ = 0.0;
70 }
71
72 private:
73 struct Call {
74 Clock::time_point time{};
75 std::size_t frames = 0;
76 };
77 std::array<Call, kWindow> calls_{};
78 std::size_t next_ = 0;
79 bool fast_ = false;
80 double speed_ = 0.0;
81};
82
83} // namespace pulp::audio
Detects a host that calls the audio callback faster than real time.
Definition pull_rate_detector.hpp:29
static constexpr std::size_t kWindow
Definition pull_rate_detector.hpp:32
static constexpr double kLeave
Definition pull_rate_detector.hpp:34
bool observe(std::size_t frames, double sample_rate, Clock::time_point now) noexcept
Records one callback of frames at sample_rate that started at now and returns whether the host is cur...
Definition pull_rate_detector.hpp:38
bool fast() const noexcept
Definition pull_rate_detector.hpp:56
void reset() noexcept
Forget every observation (a new stream: prepare, transport restart).
Definition pull_rate_detector.hpp:66
std::chrono::steady_clock Clock
Definition pull_rate_detector.hpp:31
double speed() const noexcept
Audio seconds delivered per wall-clock second over the last full window; 0 until kWindow calls were o...
Definition pull_rate_detector.hpp:61
static constexpr double kEnter
Definition pull_rate_detector.hpp:33
Definition analyzer_provider.hpp:12