2022-03-16 02:10:18 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <SFML/Config.hpp>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
|
|
|
#include <map>
|
2022-04-06 15:47:04 +02:00
|
|
|
#include <set>
|
2022-03-16 02:10:18 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
|
2022-04-14 01:26:31 +02:00
|
|
|
#include <json.hpp>
|
2022-03-16 02:10:18 +01:00
|
|
|
#include <SFML/System/Time.hpp>
|
|
|
|
|
|
|
|
#include "special_numeric_types.hpp"
|
|
|
|
|
|
|
|
namespace better {
|
|
|
|
struct SecondsAtBeat {
|
2022-04-07 00:14:01 +02:00
|
|
|
Decimal seconds;
|
2022-03-16 02:10:18 +01:00
|
|
|
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-09 00:54:06 +02:00
|
|
|
double get_bpm_as_double() 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-09 00:54:06 +02:00
|
|
|
double bpm_as_double;
|
2022-04-03 15:59:05 +02:00
|
|
|
Fraction beats;
|
2022-03-16 02:10:18 +01:00
|
|
|
};
|
|
|
|
|
2022-04-07 00:14:01 +02:00
|
|
|
class BPMEvent {
|
2022-03-16 02:10:18 +01:00
|
|
|
public:
|
2022-04-07 00:14:01 +02:00
|
|
|
BPMEvent(Fraction beats, double seconds, Decimal bpm);
|
|
|
|
Decimal get_bpm() const;
|
2022-04-09 00:54:06 +02:00
|
|
|
double get_bpm_as_double() const;
|
2022-04-07 00:14:01 +02:00
|
|
|
Fraction get_beats() const;
|
2022-04-09 00:54:06 +02:00
|
|
|
double get_seconds() const;
|
2022-04-14 00:13:38 +02:00
|
|
|
|
|
|
|
bool operator==(const BPMEvent&) const = default;
|
2022-04-16 02:26:37 +02:00
|
|
|
friend std::ostream& operator<<(std::ostream& out, const BPMEvent& b);
|
2022-03-16 02:10:18 +01:00
|
|
|
private:
|
2022-04-07 00:14:01 +02:00
|
|
|
Decimal bpm;
|
2022-04-09 00:54:06 +02:00
|
|
|
double bpm_as_double;
|
2022-04-07 00:14:01 +02:00
|
|
|
Fraction beats;
|
|
|
|
double seconds;
|
2022-03-16 02:10:18 +01:00
|
|
|
};
|
|
|
|
|
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-03-16 02:10:18 +01:00
|
|
|
};
|
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();
|
|
|
|
}
|
2022-03-16 02:10:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class Timing {
|
|
|
|
public:
|
2022-04-01 02:30:32 +02:00
|
|
|
Timing();
|
2022-04-07 00:14:01 +02:00
|
|
|
Timing(const std::vector<BPMAtBeat>& events, const Decimal& offset);
|
2022-03-16 02:10:18 +01:00
|
|
|
|
2022-04-07 00:14:01 +02:00
|
|
|
double seconds_at(Fraction beats) const;
|
|
|
|
double 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-04-07 00:14:01 +02:00
|
|
|
Fraction beats_at(double seconds) 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-04-07 00:14:01 +02:00
|
|
|
|
2022-04-13 02:29:33 +02:00
|
|
|
bool operator==(const Timing&) const = default;
|
|
|
|
|
2022-04-16 02:26:37 +02:00
|
|
|
friend std::ostream& operator<<(std::ostream& out, const Timing& t);
|
|
|
|
friend fmt::formatter<better::Timing>;
|
2022-03-16 02:10:18 +01:00
|
|
|
private:
|
2022-04-09 00:54:06 +02:00
|
|
|
Decimal offset;
|
|
|
|
double offset_as_double;
|
2022-04-04 22:03:10 +02:00
|
|
|
std::set<BPMEvent, OrderByBeats> events_by_beats;
|
|
|
|
std::set<BPMEvent, OrderBySeconds> events_by_seconds;
|
2022-03-16 02:10:18 +01:00
|
|
|
};
|
2022-04-14 01:26:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2022-04-16 02:26:37 +02:00
|
|
|
struct fmt::formatter<better::BPMEvent>: formatter<string_view> {
|
2022-04-14 01:26:31 +02:00
|
|
|
// parse is inherited from formatter<string_view>.
|
|
|
|
template <typename FormatContext>
|
2022-04-16 02:26:37 +02:00
|
|
|
auto format(const better::BPMEvent& b, FormatContext& ctx) {
|
2022-04-14 01:26:31 +02:00
|
|
|
return format_to(
|
|
|
|
ctx.out(),
|
2022-04-16 02:26:37 +02:00
|
|
|
"BPMEvent(beats: {}, bpm: {})",
|
|
|
|
b.get_beats(),
|
|
|
|
b.get_bpm()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct fmt::formatter<better::Timing>: formatter<string_view> {
|
|
|
|
// parse is inherited from formatter<string_view>.
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const better::Timing& t, FormatContext& ctx) {
|
|
|
|
return format_to(
|
|
|
|
ctx.out(),
|
|
|
|
"Timing(offset: {}, events: [{}])",
|
|
|
|
t.offset,
|
|
|
|
fmt::join(t.events_by_beats, ", ")
|
2022-04-14 01:26:31 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|