11#include "dsp/tick_types.h"
12#include "utils/units.h"
14#include <nlohmann/json_fwd.hpp>
43 friend class TempoMapWrapper;
60 friend void to_json (nlohmann::json &j,
const TempoEvent &e);
61 friend void from_json (
const nlohmann::json &j,
TempoEvent &e);
71 constexpr auto quarters_per_bar ()
const
76 constexpr auto ticks_per_bar ()
const
81 constexpr auto ticks_per_beat ()
const
87 is_different_time_signature (
const TimeSignatureEvent &other)
const
92 friend void to_json (nlohmann::json &j,
const TimeSignatureEvent &e);
93 friend void from_json (
const nlohmann::json &j, TimeSignatureEvent &e);
114 : sample_rate_ (sampleRate)
116 rebuild_time_signature_cache ();
122 sample_rate_ = sampleRate;
170 units::sample_t tick_to_samples_rounded (TimelineTick tick)
const
181 const auto seconds = samples / sample_rate_;
206 std::span<const TempoEvent>
tempo_events () const noexcept {
return events_; }
217 return time_sig_events_;
227 MusicalPosition samples_to_musical_position (units::sample_t samples)
const;
239 static consteval auto get_ppq () {
return from_nttp (PPQ); }
245 units::bpm_t
base_bpm ()
const {
return base_bpm_; }
251 rebuild_cumulative_times ();
257 return base_time_sig_;
263 throw_if_invalid_time_signature (numerator, denominator);
264 base_time_sig_.numerator = numerator;
265 base_time_sig_.denominator = denominator;
266 rebuild_time_signature_cache ();
278 std::span<const TimeSignatureEvent>
281 return effective_time_sig_events_;
286 void rebuild_cumulative_times ();
290 void rebuild_time_signature_cache ();
295 static void throw_if_invalid_time_signature (
int numerator,
int denominator);
298 units::precise_second_t compute_segment_time (
299 const TempoEvent &start,
300 const TempoEvent &end,
301 units::tick_t segmentTicks)
const;
304 void clear_tempo_events ()
307 cumulative_seconds_.clear ();
311 void clear_time_signature_events ()
313 time_sig_events_.clear ();
314 rebuild_time_signature_cache ();
317 static constexpr std::string_view kTempoChangesKey =
"tempoChanges";
318 static constexpr std::string_view kTimeSignaturesKey =
"timeSignatures";
319 static constexpr std::string_view kBaseBpmKey =
"baseBpm";
320 static constexpr std::string_view kBaseTimeSignatureKey =
"baseTimeSignature";
322 friend void from_json (
const nlohmann::json &j,
FixedPpqTempoMap &tempo_map);
325 units::precise_sample_rate_t sample_rate_;
326 static constexpr auto ticks_per_sixteenth_ =
332 units::bpm_t base_bpm_{ units::bpm (120.0) };
335 std::vector<TempoEvent> events_;
336 std::vector<TimeSignatureEvent> time_sig_events_;
337 std::vector<units::precise_second_t>
342 std::vector<TimeSignatureEvent> effective_time_sig_events_;
Manages tempo and time signature events for a DAW timeline.
auto tick_to_seconds(TimelineTick tick) const -> units::precise_second_t
Convert timeline ticks to seconds.
void add_tempo_event(units::tick_t tick, units::bpm_t bpm, CurveType curve)
Add a tempo event.
void remove_tempo_event(units::tick_t tick)
Remove a tempo event at the specified tick.
TimeSignatureEvent time_signature_at_tick(units::tick_t tick) const
Get the time signature event active at the given tick.
void add_time_signature_event(units::tick_t tick, int numerator, int denominator)
Add a time signature event.
CurveType
Tempo curve type (constant or linear ramp).
@ Linear
Linear tempo ramp.
@ Constant
Constant tempo.
FixedPpqTempoMap(units::precise_sample_rate_t sampleRate)
Construct a new FixedPpqTempoMap object.
std::span< const TimeSignatureEvent > time_signature_events() const noexcept
Read-only view of all inserted time signature events, sorted ascending by tick.
std::span< const TempoEvent > tempo_events() const noexcept
Read-only view of all inserted tempo events, sorted ascending by tick.
const TimeSignatureEvent & base_time_signature() const
Base time signature at tick 0.
TimelineTickI musical_position_to_tick(const MusicalPosition &pos) const
Convert musical position to ticks.
auto get_sample_rate() const
Get current sample rate.
std::span< const TimeSignatureEvent > effective_time_signature_events() const noexcept
Read-only view of the time signature events with the base signature prepended at tick 0 when no inser...
void set_base_bpm(units::bpm_t bpm)
Set the base tempo at tick 0 and recompute the lead-segment timing.
MusicalPosition tick_to_musical_position(units::tick_t tick) const
Convert ticks to musical position (bar:beat:sixteenth:tick).
TimelineTick samples_to_tick(units::precise_sample_t samples) const
Convert samples to timeline ticks.
void remove_time_signature_event(units::tick_t tick)
Remove a time signature event at the specified tick.
void set_base_time_signature(int numerator, int denominator)
Set the base time signature at tick 0.
units::bpm_t tempo_at_tick(units::tick_t tick) const
Get the tempo (BPM) active at the given tick.
units::precise_sample_t tick_to_samples(TimelineTick tick) const
Convert timeline ticks to samples.
TimelineTick seconds_to_tick(units::precise_second_t seconds) const
Convert seconds to timeline ticks.
static consteval auto get_ppq()
Get pulses per quarter note.
units::bpm_t base_bpm() const
Base tempo at tick 0.
void set_sample_rate(units::precise_sample_rate_t sampleRate)
Set the sample rate.
Musical position representation.
int tick
Ticks in sixteenth (0-indexed).
int bar
Bar number (1-indexed).
int sixteenth
Sixteenth in beat (1-indexed).
int beat
Beat in bar (1-indexed).
CurveType curve
Curve type from this event to the next.
units::tick_t tick
Position in ticks.
Time signature event definition.
units::tick_t tick
Position in ticks.
int numerator
Beats per bar.
int denominator
Beat unit (2,4,8,16).