Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zavorka/c629b2ef6bdc8acfaa3d173c5f394ad0 to your computer and use it in GitHub Desktop.
Save zavorka/c629b2ef6bdc8acfaa3d173c5f394ad0 to your computer and use it in GitHub Desktop.
libbeatnik: Calculate score from thresholded input
diff --git a/include/re/beatnik/tracker/tracker.hpp b/include/re/beatnik/tracker/tracker.hpp
index 8c29389..ec83a0b 100644
--- a/include/re/beatnik/tracker/tracker.hpp
+++ b/include/re/beatnik/tracker/tracker.hpp
@@ -34,6 +34,13 @@ public:
bool
update_score(T odf_value) noexcept
{
+
+ threshold_sum += odf_value;
+ threshold_buffer.push_back(odf_value);
+ auto in = threshold_buffer[threshold_size / 2];
+ auto out = std::fdim(in, threshold_sum / (threshold_size + 1));
+ threshold_sum -= threshold_buffer.front();
+
if (!is_valid_period(period_guess)) {
return false;
}
@@ -44,7 +51,7 @@ public:
cumulative_score.rend()
);
- auto new_score = (1 - alpha) * odf_value + alpha * last_score.value;
+ auto new_score = (1 - alpha) * out + alpha * last_score.value;
cumulative_score.push_back(new_score);
// a distance from the last beat
backlink.push_back(static_cast<int_t>(last_score.index));
@@ -108,14 +115,17 @@ private:
&& period < max_period;
}
- static constexpr float alpha = 0.9f;
+ static constexpr float alpha = 0.8f;
static constexpr int_t N = min_period * beats_count;
+ static constexpr int_t threshold_size = 14;
int_t period_guess;
int_t counter;
ring_array <T, N> cumulative_score;
ring_array <int_t, N> backlink;
skewed_window <T, min_period> window;
+ T threshold_sum;
+ ring_array<T, threshold_size> threshold_buffer;
};
}
diff --git a/include/re/beatnik/tracker/skewed_window.hpp b/include/re/beatnik/tracker/skewed_window.hpp
index e1e1ef2..dc312dd 100644
--- a/include/re/beatnik/tracker/skewed_window.hpp
+++ b/include/re/beatnik/tracker/skewed_window.hpp
@@ -73,7 +73,7 @@ private:
return std::exp(-.5f * std::pow(TIGHTNESS * std::log(2 - x), 2.f));
};
- static constexpr T TIGHTNESS = 5;
+ static constexpr T TIGHTNESS = 3;
std::array<std::array<T, row_size>, period_range> cache;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment