F.E.I.S/src/better_timing.hpp

82 lines
2.0 KiB
C++
Raw Normal View History

#pragma once
#include <SFML/Config.hpp>
#include <algorithm>
#include <iterator>
#include <map>
2022-04-06 15:47:04 +02:00
#include <set>
#include <sstream>
#include <vector>
#include <SFML/System/Time.hpp>
#include "json.hpp"
#include "special_numeric_types.hpp"
namespace better {
struct SecondsAtBeat {
Decimal seconds;
Fraction beats;
};
class BPMAtBeat {
public:
2022-04-03 15:59:05 +02:00
BPMAtBeat(Decimal bpm, Fraction beats);
Decimal get_bpm() const;
2022-04-03 15:59:05 +02:00
Fraction get_beats() const;
private:
Decimal bpm;
2022-04-03 15:59:05 +02:00
Fraction beats;
};
class BPMEvent {
public:
BPMEvent(Fraction beats, double seconds, Decimal bpm);
Decimal get_bpm() const;
Fraction get_beats() const;
Fraction get_seconds() const;
private:
Decimal bpm;
Fraction beats;
double seconds;
};
2022-04-04 22:03:10 +02:00
struct OrderByBeats {
2022-04-06 15:47:04 +02:00
template<class T>
bool operator()(const T& a, const T& b) const {
return a.get_beats() < b.get_beats();
}
};
2022-04-04 22:03:10 +02:00
struct OrderBySeconds {
2022-04-06 15:47:04 +02:00
template<class T>
bool operator()(const T& a, const T& b) const {
return a.get_seconds() < b.get_seconds();
}
};
class Timing {
public:
2022-04-01 02:30:32 +02:00
Timing();
Timing(const std::vector<BPMAtBeat>& events, const Decimal& offset);
double seconds_at(Fraction beats) const;
double seconds_between(Fraction beat_a, Fraction beat_b) const;
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-17 02:50:30 +01:00
Fraction beats_at(sf::Time time) const;
Fraction beats_at(double seconds) const;
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);
Decimal offset;
2022-03-17 02:50:30 +01:00
private:
2022-04-04 22:03:10 +02:00
std::set<BPMEvent, OrderByBeats> events_by_beats;
std::set<BPMEvent, OrderBySeconds> events_by_seconds;
};
}