67 [[nodiscard]]
bool isValid() const noexcept {
return beats_ !=
nullptr; }
72 [[nodiscard]] std::span<const Beat>
beats() const noexcept
74 return isValid() ? std::span<const Beat>(beats_.get(), count_)
75 : std::span<const Beat>();
78 [[nodiscard]]
double tempoAt(std::int64_t frame)
const noexcept
80 const auto map =
beats();
81 if (map.size() < 2 || frame < map.front().frame || frame >= map.back().frame)
83 auto next = std::upper_bound(map.begin(), map.end(), frame,
84 [](std::int64_t p,
const Beat &b) { return p < b.frame; });
85 const auto &beat = *(next - 1);
86 return beat.reliable ? beat.nextIntervalBpm : 0;
90 return isValid() ? std::max(std::size_t(1), count_) *
sizeof(
Beat) : 0;
99 std::unique_ptr<Beat[]> beats_;
100 std::size_t count_ = 0;
119 detail::OfflineSession job(jobOptions);
126 candidate.options_ = options;
127 const auto frames = features.
features();
129 candidate.spec_.sampleRate, features.
hopFrames(), frames.size(),
131 typename OnsetDetector<T>::OdfFrame frame;
132 frame.value = frames[i].attack;
133 frame.registers = frames[i].registers;
134 frame.referenceSample = featureReference(features, i);
137 options.minimumBpm, options.maximumBpm,
139 candidate.report_.tempoBpm =
static_cast<double>(tracked.tempoBpm);
140 candidate.report_.secondaryTempoBpm =
static_cast<double>(tracked.secondaryTempoBpm);
141 candidate.report_.confidence =
static_cast<double>(tracked.confidence);
142 for (
const auto frame : tracked.beats())
143 if (frame >= 0 && frame < candidate.spec_.frames)
145 candidate.beats_ = job.template allocate<Beat>(std::max(std::size_t(1), candidate.count_));
146 std::size_t index = 0;
147 for (
const auto frame : tracked.beats())
148 if (frame >= 0 && frame < candidate.spec_.frames)
149 candidate.beats_[index++].frame = frame;
150 for (std::size_t i = 0; i + 1 < candidate.count_; ++i)
152 auto &beat = candidate.beats_[i];
153 const auto next = candidate.beats_[i + 1].frame;
154 if (next <= beat.frame)
156 const double interval =
static_cast<double>(next - beat.frame);
157 std::array<double, 3> local{interval, interval, interval};
159 local[0] =
static_cast<double>(beat.frame - candidate.beats_[i - 1].frame);
160 if (i + 2 < candidate.count_)
161 local[2] =
static_cast<double>(candidate.beats_[i + 2].frame - next);
162 std::sort(local.begin(), local.end());
163 const double quarter = local[1];
164 beat.nextIntervalBpm = 60 * candidate.spec_.sampleRate / quarter;
165 const double tolerance = std::min(0.05 * candidate.spec_.sampleRate, 0.125 * interval);
170 beat.reliable = candidate.report_.confidence >= options.minimumConfidence &&
171 std::abs(interval - quarter) <= 2 * tolerance &&
172 anchored(features.
pulses(), beat.frame, tolerance) &&
173 anchored(features.
pulses(), next, tolerance);
174 candidate.report_.reliableIntervals += beat.reliable ? 1 : 0;
176 confirmLocalIntervals(features, candidate, job);
177 candidate.report_.reason =
182 result.memoryBytes = job.bytes();
183 result.analysis = std::move(candidate);
201 auto features = OfflineTransientAnalyzer<T>().analyze(source, {
false,
true,
true},
203 if (!features.succeeded())
205 result =
analyze(features.analysis, options,
207 if (!result.succeeded())
209 if (source.
getSpec() != result.analysis.getSpec())
211 result.memoryBytes += features.memoryBytes;
224 [[nodiscard]]
static std::int64_t featureReference(
225 const typename OfflineTransientAnalyzer<T>::Analysis &features, std::size_t index)
noexcept
230 static void confirmLocalIntervals(
231 const typename OfflineTransientAnalyzer<T>::Analysis &features, Analysis &candidate,
232 detail::OfflineSession &job)
234 if (candidate.count_ < 2 || candidate.report_.confidence >= candidate.options_.minimumConfidence)
236 const auto frames = features.features();
237 const auto window =
static_cast<std::size_t
>(std::ceil(
238 std::max(8., 240 / candidate.options_.minimumBpm) * candidate.spec_.sampleRate /
239 features.hopFrames()));
240 if (frames.size() <= window)
243 for (std::size_t begin = 0;;)
246 candidate.spec_.sampleRate, features.hopFrames(), window,
248 typename OnsetDetector<T>::OdfFrame frame;
249 frame.value = frames[begin + i].attack;
250 frame.registers = frames[begin + i].registers;
251 frame.referenceSample = featureReference(features, begin + i);
253 }, candidate.options_.minimumBpm, candidate.options_.maximumBpm,
255 ++candidate.report_.localWindows;
256 const auto grid = local.beats();
257 if (local.confidence >= candidate.options_.minimumConfidence && grid.size() >= 2)
259 const auto first = std::lower_bound(candidate.beats_.get(),
260 candidate.beats_.get() + candidate.count_, grid.front(),
261 [](
const Beat &beat,
auto p) { return beat.frame < p; });
262 auto index =
static_cast<std::size_t
>(first - candidate.beats_.get());
265 for (; index + 1 < candidate.count_ && candidate.beats_[index].frame <= grid.back(); ++index)
267 auto &beat = candidate.beats_[index];
270 const auto end = candidate.beats_[index + 1].frame;
271 const double interval =
static_cast<double>(end - beat.frame);
272 const double quarter = 60 * candidate.spec_.sampleRate / beat.nextIntervalBpm;
273 const double tolerance = std::min(.05 * candidate.spec_.sampleRate, .125 * interval);
274 if (std::abs(interval - quarter) > 2 * tolerance ||
275 !anchored(features.pulses(), beat.frame, tolerance) ||
276 !anchored(features.pulses(), end, tolerance))
278 auto position = std::lower_bound(grid.begin(), grid.end(), beat.frame);
279 if (position == grid.end() || (position != grid.begin() &&
280 beat.frame - *(position - 1) < *position - beat.frame))
282 if (position + 1 == grid.end() ||
283 std::abs(
static_cast<double>(*position - beat.frame)) > tolerance ||
284 std::abs(
static_cast<double>(*(position + 1) - end)) > tolerance)
286 beat.reliable =
true;
287 ++candidate.report_.reliableIntervals;
288 ++candidate.report_.locallyConfirmedIntervals;
291 if (begin + window == frames.size())
293 begin = std::min(begin + window / 2, frames.size() - window);
296 static void validate(Options options)
298 if (!std::isfinite(options.minimumBpm) || !std::isfinite(options.maximumBpm) ||
299 (options.minimumBpm < 20) || (options.maximumBpm > 480) ||
300 options.minimumBpm > options.maximumBpm || !std::isfinite(options.minimumConfidence) ||
301 (options.minimumConfidence < 0) || (options.minimumConfidence > 1))
304 [[nodiscard]]
static bool
305 anchored(std::span<
const typename OfflineTransientAnalyzer<T>::Event> pulses,
306 std::int64_t frame,
double tolerance)
noexcept
308 const auto next = std::lower_bound(pulses.begin(), pulses.end(), frame,
309 [](
const auto &event,
auto p) { return event.begin < p; });
310 return (next != pulses.end() &&
static_cast<double>(next->begin - frame) <= tolerance) ||
311 (next != pulses.begin() &&
static_cast<double>(frame - (next - 1)->begin) <= tolerance);