#include "tools/beatutils.h"
#include <numeric>
#include <iostream>

auto beat_utils::calculateBeatRateInfo(const std::vector<kku::microsec>& approximate_deltas) -> BeatInfo
{
    if (approximate_deltas.empty())
        return {};

    const long double sum = std::accumulate(approximate_deltas.begin(), approximate_deltas.end(), 0);
    const long double amount = approximate_deltas.size();

    long double average = sum / amount;

    const int bpm = static_cast<int>(60000000. / average);


    return BeatInfo
    {
        bpm,
        static_cast<kku::microsec>(1. / static_cast<long double>(bpm) * 60000000.)
    };
}
