2022-03-16 02:10:18 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <SFML/Config.hpp>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
|
|
|
#include <map>
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <SFML/System/Time.hpp>
|
|
|
|
|
2022-03-31 03:50:15 +02:00
|
|
|
#include "json.hpp"
|
2022-03-16 02:10:18 +01:00
|
|
|
#include "special_numeric_types.hpp"
|
|
|
|
|
|
|
|
namespace better {
|
|
|
|
struct SecondsAtBeat {
|
|
|
|
Fraction seconds;
|
|
|
|
Fraction beats;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BPMAtBeat {
|
|
|
|
public:
|
2022-04-03 15:59:05 +02:00
|
|
|
BPMAtBeat(Decimal bpm, Fraction beats);
|
2022-03-31 03:50:15 +02:00
|
|
|
Decimal get_bpm() const;
|
2022-04-03 15:59:05 +02:00
|
|
|
Fraction get_beats() const;
|
2022-03-16 02:10:18 +01:00
|
|
|
private:
|
2022-03-31 03:50:15 +02:00
|
|
|
Decimal bpm;
|
2022-04-03 15:59:05 +02:00
|
|
|
Fraction beats;
|
2022-03-16 02:10:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class BPMEvent : public BPMAtBeat {
|
|
|
|
public:
|
2022-03-31 03:50:15 +02:00
|
|
|
BPMEvent(Fraction beats, Fraction seconds, Decimal bpm);
|
2022-03-16 02:10:18 +01:00
|
|
|
Fraction get_seconds() const;
|
|
|
|
private:
|
|
|
|
Fraction seconds;
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto order_by_beats = [](const BPMAtBeat& a, const BPMAtBeat& b) {
|
|
|
|
return a.get_beats() < b.get_beats();
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto order_by_seconds = [](const BPMEvent& a, const BPMEvent& b) {
|
|
|
|
return a.get_seconds() < b.get_seconds();
|
|
|
|
};
|
|
|
|
|
|
|
|
class Timing {
|
|
|
|
public:
|
2022-04-01 02:30:32 +02:00
|
|
|
Timing();
|
2022-03-16 02:10:18 +01:00
|
|
|
Timing(const std::vector<BPMAtBeat>& events, const SecondsAtBeat& offset);
|
|
|
|
|
|
|
|
Fraction fractional_seconds_at(Fraction beats) const;
|
2022-03-17 02:50:30 +01:00
|
|
|
Fraction fractional_seconds_between(Fraction beat_a, Fraction beat_b) const;
|
2022-03-16 02:10:18 +01:00
|
|
|
sf::Time time_at(Fraction beats) const;
|
2022-03-17 02:50:30 +01:00
|
|
|
sf::Time time_between(Fraction beat_a, Fraction beat_b) const;
|
2022-03-16 02:10:18 +01:00
|
|
|
|
2022-03-17 02:50:30 +01:00
|
|
|
Fraction beats_at(sf::Time time) const;
|
2022-03-31 03:50:15 +02:00
|
|
|
|
2022-04-02 04:10:09 +02:00
|
|
|
nlohmann::ordered_json dump_to_memon_1_0_0() const;
|
|
|
|
|
2022-04-03 15:59:05 +02:00
|
|
|
static Timing load_from_memon_1_0_0(const nlohmann::json& json);
|
|
|
|
static Timing load_from_memon_legacy(const nlohmann::json& metadata);
|
2022-03-17 02:50:30 +01:00
|
|
|
|
2022-03-16 02:10:18 +01:00
|
|
|
private:
|
|
|
|
std::set<BPMEvent, decltype(order_by_beats)> events_by_beats{order_by_beats};
|
|
|
|
std::set<BPMEvent, decltype(order_by_seconds)> events_by_seconds{order_by_seconds};
|
|
|
|
};
|
2022-03-17 02:50:30 +01:00
|
|
|
|
|
|
|
const auto frac_to_time = [](const Fraction& f) {
|
|
|
|
auto microseconds = f * 1000000;
|
|
|
|
return sf::microseconds(microseconds.convert_to<sf::Int64>());
|
|
|
|
};
|
2022-03-16 02:10:18 +01:00
|
|
|
}
|