Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
tempo_track.h
1// SPDX-FileCopyrightText: © 2019-2020, 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "structure/tracks/automatable_track.h"
7#include "utils/types.h"
8
9#define P_TEMPO_TRACK (TRACKLIST->getTempoTrack ())
10
11namespace zrythm::structure::tracks
12{
13constexpr float TEMPO_TRACK_MAX_BPM = 420.f;
14constexpr float TEMPO_TRACK_MIN_BPM = 40.f;
15constexpr float TEMPO_TRACK_DEFAULT_BPM = 140.f;
16constexpr int TEMPO_TRACK_DEFAULT_BEATS_PER_BAR = 4;
17constexpr int TEMPO_TRACK_MIN_BEATS_PER_BAR = 1;
18constexpr int TEMPO_TRACK_MAX_BEATS_PER_BAR = 16;
19constexpr auto TEMPO_TRACK_DEFAULT_BEAT_UNIT = BeatUnit::Four;
20constexpr auto TEMPO_TRACK_MIN_BEAT_UNIT = BeatUnit::Two;
21constexpr auto TEMPO_TRACK_MAX_BEAT_UNIT = BeatUnit::Sixteen;
22
30class TempoTrack final
31 : public QObject,
32 public AutomatableTrack,
33 public utils::InitializableObject<TempoTrack>
34{
35 Q_OBJECT
36 QML_ELEMENT
37 DEFINE_TRACK_QML_PROPERTIES (TempoTrack)
38 DEFINE_AUTOMATABLE_TRACK_QML_PROPERTIES (TempoTrack)
39 Q_PROPERTY (double bpm READ getBpm WRITE setBpm NOTIFY bpmChanged FINAL)
40
41 friend class InitializableObject;
42
43 DECLARE_FINAL_TRACK_CONSTRUCTORS (TempoTrack)
44
45public:
46 // ==================================================================
47 // QML Interface
48 // ==================================================================
49
50 double getBpm () const;
51 void setBpm (double bpm);
52 Q_SIGNAL void bpmChanged (double bpm);
53 Q_INVOKABLE int getBeatUnit () const;
54 Q_INVOKABLE int getBeatsPerBar () const;
55
56 // ==================================================================
57
63 void clear_objects () override;
64
65 void
66 init_loaded (PluginRegistry &plugin_registry, PortRegistry &port_registry)
67 override;
68
72 bpm_t get_bpm_at_pos (Position pos);
73
74 friend void init_from (
75 TempoTrack &obj,
76 const TempoTrack &other,
77 utils::ObjectCloneType clone_type);
78
83
84 std::string get_current_bpm_as_str () const;
85 void set_bpm_from_str (const std::string &str);
86
98 void set_bpm (bpm_t bpm, bpm_t start_bpm, bool temporary, bool fire_events);
99
100 static constexpr int beat_unit_enum_to_int (BeatUnit ebeat_unit)
101 {
102 switch (ebeat_unit)
103 {
104 case BeatUnit::Two:
105 return 2;
106 case BeatUnit::Four:
107 return 4;
108 case BeatUnit::Eight:
109 return 8;
110 case BeatUnit::Sixteen:
111 return 16;
112 default:
113 z_return_val_if_reached (0);
114 }
115 }
116
117 void set_beat_unit_from_enum (BeatUnit ebeat_unit);
118
119 BeatUnit get_beat_unit_enum () const
120 {
121 return beat_unit_to_enum (get_beat_unit ());
122 }
123
124 static BeatUnit beat_unit_to_enum (int beat_unit);
125
126 void set_beat_unit (int beat_unit);
127
131 void set_beats_per_bar (int beats_per_bar);
132
133 int get_beats_per_bar () const;
134
135 int get_beat_unit () const;
136
138 const PortUuid &port_uuid,
139 const dsp::PortIdentifier &id,
140 float value) override;
141
142 void
143 append_ports (std::vector<Port *> &ports, bool include_plugins) const final;
144
145 ControlPort &get_bpm_port () const
146 {
147 return *std::get<ControlPort *> (bpm_port_->get_object ());
148 }
149 ControlPort &get_beats_per_bar_port () const
150 {
151 return *std::get<ControlPort *> (beats_per_bar_port_->get_object ());
152 }
153 ControlPort &get_beat_unit_port () const
154 {
155 return *std::get<ControlPort *> (beat_unit_port_->get_object ());
156 }
157
158private:
159 static constexpr auto kBpmPortKey = "bpmPort"sv;
160 static constexpr auto kBeatsPerBarPortKey = "beatsPerBarPort"sv;
161 static constexpr auto kBeatUnitPortKey = "beatUnitPort"sv;
162 friend void to_json (nlohmann::json &j, const TempoTrack &track)
163 {
164 to_json (j, static_cast<const Track &> (track));
165 to_json (j, static_cast<const AutomatableTrack &> (track));
166 j[kBpmPortKey] = track.bpm_port_;
167 j[kBeatsPerBarPortKey] = track.beats_per_bar_port_;
168 j[kBeatUnitPortKey] = track.beat_unit_port_;
169 }
170 friend void from_json (const nlohmann::json &j, TempoTrack &track)
171 {
172 from_json (j, static_cast<Track &> (track));
173 from_json (j, static_cast<AutomatableTrack &> (track));
174 j.at (kBpmPortKey).get_to (track.bpm_port_);
175 j.at (kBeatsPerBarPortKey).get_to (track.beats_per_bar_port_);
176 j.at (kBeatUnitPortKey).get_to (track.beat_unit_port_);
177 }
178
179 bool initialize ();
180
181private:
182 PortRegistry &port_registry_;
183
185 std::optional<PortUuidReference> bpm_port_;
186
188 std::optional<PortUuidReference> beats_per_bar_port_;
189
191 std::optional<PortUuidReference> beat_unit_port_;
192};
193}
Control port specifics.
Struct used to identify Ports in the project.
Represents a track that controls the tempo of the audio.
Definition tempo_track.h:34
void clear_objects() override
Removes all objects from the tempo track.
void on_control_change_event(const PortUuid &port_uuid, const dsp::PortIdentifier &id, float value) override
Will be called when a control port's value changes.
void set_beats_per_bar(int beats_per_bar)
Updates beat unit and anything depending on it.
bpm_t get_current_bpm() const
Returns the current BPM.
void init_loaded(PluginRegistry &plugin_registry, PortRegistry &port_registry) override
Adds additional metadata to track members after deserialization.
void set_bpm(bpm_t bpm, bpm_t start_bpm, bool temporary, bool fire_events)
Sets the BPM.
void append_ports(std::vector< Port * > &ports, bool include_plugins) const final
Appends all channel ports and optionally plugin ports to the array.
bpm_t get_bpm_at_pos(Position pos)
Returns the BPM at the given pos.
A factory class for creating initializable objects using static polymorphism.
BeatUnit
Beat unit.
Definition types.h:198
float bpm_t
The BPM type.
Definition types.h:73