#pragma once #include #include #include #include #include #include #include #include #include #include "special_numeric_types.hpp" namespace better { struct SecondsAtBeat { Decimal seconds; Fraction beats; }; class BPMAtBeat { public: BPMAtBeat(Decimal bpm, Fraction beats); Decimal get_bpm() const; double get_bpm_as_double() const; Fraction get_beats() const; private: Decimal bpm; double bpm_as_double; Fraction beats; }; class BPMEvent { public: BPMEvent(Fraction beats, double seconds, Decimal bpm); Decimal get_bpm() const; double get_bpm_as_double() const; Fraction get_beats() const; double get_seconds() const; bool operator==(const BPMEvent&) const = default; friend std::ostream& operator<<(std::ostream& out, const BPMEvent& b); private: Decimal bpm; double bpm_as_double; Fraction beats; double seconds; }; struct OrderByBeats { template bool operator()(const T& a, const T& b) const { return a.get_beats() < b.get_beats(); } }; struct OrderBySeconds { template bool operator()(const T& a, const T& b) const { return a.get_seconds() < b.get_seconds(); } }; class Timing { public: Timing(); Timing(const std::vector& 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; sf::Time time_between(Fraction beat_a, Fraction beat_b) const; Fraction beats_at(sf::Time time) const; Fraction beats_at(double seconds) const; nlohmann::ordered_json dump_to_memon_1_0_0() const; static Timing load_from_memon_1_0_0(const nlohmann::json& json); static Timing load_from_memon_legacy(const nlohmann::json& metadata); bool operator==(const Timing&) const = default; friend std::ostream& operator<<(std::ostream& out, const Timing& t); friend fmt::formatter; private: Decimal offset; double offset_as_double; std::set events_by_beats; std::set events_by_seconds; }; } template <> struct fmt::formatter: formatter { // parse is inherited from formatter. template auto format(const better::BPMEvent& b, FormatContext& ctx) { return format_to( ctx.out(), "BPMEvent(beats: {}, bpm: {})", b.get_beats(), b.get_bpm() ); } }; template <> struct fmt::formatter: formatter { // parse is inherited from formatter. template auto format(const better::Timing& t, FormatContext& ctx) { return format_to( ctx.out(), "Timing(offset: {}, events: [{}])", t.offset, fmt::join(t.events_by_beats, ", ") ); } };