MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
IntervalScan.cpp
Go to the documentation of this file.
1#include "IntervalScan.hpp"
2
4
6
7namespace {
8
9 uint32_t clamped_sub(uint32_t a, uint32_t b) noexcept
10 {
11 return a > b ? a - b : 0U;
12 }
13
14}
15
17{
18 if (!std::isfinite(config.enter) || !std::isfinite(config.leave) || !std::isfinite(config.gate_threshold)) {
19 return "Interval scan thresholds must be finite";
20 }
21 if (config.hop == 0) {
22 return "Interval scan hop must be greater than 0";
23 }
24 if (config.max_run != 0 && config.max_run <= static_cast<uint64_t>(config.hop) + 2ULL * config.padding) {
25 return "Interval scan run cap must exceed one hop plus twice the padding";
26 }
27 return {};
28}
29
32{
33 if (const std::string reason = diagnose_interval_scan(config); !reason.empty()) {
34 error<std::invalid_argument>(Journal::Component::Kinesis, Journal::Context::Runtime,
35 std::source_location::current(), "{}", reason);
36 }
37 m_run_limit = config.max_run == 0
38 ? std::numeric_limits<uint32_t>::max()
39 : config.max_run - config.hop - 2 * config.padding;
40}
41
43{
44 m_seen = 0;
45 m_triggered = false;
46 m_start = 0;
47 m_has_start = false;
48 m_pend_valid = false;
49 m_pend_start = 0;
50 m_pend_end = 0;
51 m_best_end = 0;
52 m_best_dur = 0;
54}
55
57{
58 m_prev_end = 0;
59 m_next_start = 0;
60 m_temp_end = 0;
61 m_has_best = false;
62}
63
65{
66 m_total = m_config.length != 0
67 ? m_config.length
68 : static_cast<uint32_t>(m_config.first_sample + m_seen * m_config.hop);
69}
70
71void IntervalScanner::close_run(uint32_t start, uint32_t end, std::vector<Interval>& out)
72{
73 if (m_config.emit_on_close) {
74 out.emplace_back(clamped_sub(start, m_config.padding), std::min(m_total, end + m_config.padding));
75 return;
76 }
77 uint32_t padded_start { 0 };
78 if (m_pend_valid) {
79 const int32_t gap = static_cast<int32_t>(start) - static_cast<int32_t>(m_pend_end);
80 if (gap < static_cast<int32_t>(2U * m_config.padding)) {
81 const int32_t half_gap = gap >= 0 ? gap / 2 : -((-gap + 1) / 2);
82 const auto padded_end = static_cast<uint32_t>(static_cast<int32_t>(m_pend_end) + half_gap);
83 padded_start = half_gap >= 0
84 ? clamped_sub(start, static_cast<uint32_t>(half_gap))
85 : start + static_cast<uint32_t>(-half_gap);
86 out.emplace_back(m_pend_start, padded_end);
87 } else {
88 const uint32_t padded_end = std::min(m_total, m_pend_end + m_config.padding);
89 padded_start = clamped_sub(start, m_config.padding);
90 out.emplace_back(m_pend_start, padded_end);
91 }
92 } else {
93 padded_start = clamped_sub(start, m_config.padding);
94 }
95 m_pend_valid = true;
96 m_pend_start = padded_start;
97 m_pend_end = end;
98}
99
100std::vector<IntervalScanner::Interval> IntervalScanner::push(std::span<const double> scores, std::span<const double> gate)
101{
102 if (!gate.empty() && gate.size() != scores.size()) {
103 error<std::invalid_argument>(Journal::Component::Kinesis, Journal::Context::Runtime,
104 std::source_location::current(), "Interval scan gate has {} scores for {} scores", gate.size(), scores.size());
105 }
106
107 const auto first = static_cast<uint32_t>(m_seen);
108 m_seen += scores.size();
109 update_total();
110
111 std::vector<Interval> out;
112 const bool gated = !gate.empty();
113
114 for (size_t i = 0; i < scores.size(); ++i) {
115 const double score = static_cast<float>(scores[i]);
116 const uint32_t cur = m_config.first_sample + (first + static_cast<uint32_t>(i)) * m_config.hop;
117 const bool above = score >= m_config.enter;
118
119 if (gated && static_cast<double>(static_cast<float>(gate[i])) < m_config.gate_threshold) {
120 if (m_triggered) {
121 const uint32_t end = m_temp_end != 0 ? m_temp_end : cur;
122 if (end > m_start && end - m_start > m_config.min_run) {
123 close_run(m_start, end, out);
124 }
125 }
126 m_has_start = false;
127 m_triggered = false;
129 continue;
130 }
131
132 if (above && m_temp_end != 0) {
133 const uint32_t silence = cur - m_temp_end;
134 if (silence > m_config.gap_at_limit) {
135 if (!m_has_best || silence > m_best_dur) {
137 m_best_dur = silence;
138 m_has_best = true;
139 }
140 }
141 m_temp_end = 0;
142 if (m_next_start < m_prev_end) {
143 m_next_start = cur;
144 }
145 }
146
147 if (above && !m_triggered) {
148 m_triggered = true;
149 m_start = cur;
150 m_has_start = true;
151 continue;
152 }
153
154 if (m_triggered && cur > m_start && cur - m_start > m_run_limit) {
155 if (m_config.use_longest_gap && m_has_best) {
156 const uint32_t cut = m_best_end;
157 const uint32_t dur = m_best_dur;
158 close_run(m_start, cut, out);
159 m_has_start = false;
160 const uint32_t resume = cut + dur;
161 if (resume < cut + cur) {
162 m_start = resume;
163 m_has_start = true;
164 } else {
165 m_triggered = false;
166 }
168 } else if (m_prev_end != 0) {
169 const uint32_t cut = m_prev_end;
170 close_run(m_start, cut, out);
171 m_has_start = false;
172 if (m_next_start < cut) {
173 m_triggered = false;
174 } else {
176 m_has_start = true;
177 }
179 } else {
180 close_run(m_start, cur, out);
181 m_has_start = false;
183 m_triggered = false;
184 continue;
185 }
186 }
187
188 if (score < m_config.leave && m_triggered) {
189 if (m_temp_end == 0) {
190 m_temp_end = cur;
191 }
192 const uint32_t quiet = cur - m_temp_end;
193
194 if (!m_config.use_longest_gap && quiet > m_config.gap_at_limit) {
196 }
197 if (quiet < m_config.min_gap) {
198 continue;
199 }
200
201 const uint32_t end = m_temp_end;
202 if (end > m_start && end - m_start > m_config.min_run) {
203 close_run(m_start, end, out);
204 }
205 m_has_start = false;
206 m_triggered = false;
208 }
209 }
210
211 return out;
212}
213
214std::vector<IntervalScanner::Interval> IntervalScanner::flush()
215{
216 update_total();
217 std::vector<Interval> out;
218
219 if (m_has_start && m_total > m_start && m_total - m_start > m_config.min_run) {
221 }
222 if (m_pend_valid) {
223 out.emplace_back(m_pend_start, std::min(m_total, m_pend_end + m_config.padding));
224 }
225
226 reset();
227 return out;
228}
229
230} // namespace MayaFlux::Kinesis::Discrete
Runs of a score sequence that stay above a threshold, found in order as a stream, for MayaFlux::Kines...
const IntervalScanConfig & config() const noexcept
IntervalScanner(const IntervalScanConfig &config)
Construct a scanner.
std::vector< Interval > push(std::span< const double > scores, std::span< const double > gate={})
Continue the stream with more scores.
std::vector< Interval > flush()
End the stream: close the open run and release the pending interval.
void reset() noexcept
Start a new stream without releasing anything.
void close_run(uint32_t start, uint32_t end, std::vector< Interval > &out)
@ Runtime
General runtime operations (default fallback).
@ Kinesis
General mathematical and physics algorithns.
std::string diagnose_interval_scan(const IntervalScanConfig &config)
Why a config cannot be scanned.