Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
automation_track.h
1// SPDX-FileCopyrightText: © 2018-2022, 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <utility>
7
8#include "dsp/parameter.h"
9#include "dsp/processor_base.h"
10#include "dsp/tempo_map_qml_adapter.h"
11#include "structure/arrangement/arranger_object_owner.h"
12#include "structure/arrangement/automation_region.h"
13#include "structure/arrangement/timeline_data_provider.h"
14#include "utils/playback_cache_scheduler.h"
15#include "utils/units.h"
16
17#include <QtQmlIntegration/qqmlintegration.h>
18
19namespace zrythm::structure::tracks
20{
22 : public QObject,
23 public arrangement::ArrangerObjectOwner<arrangement::AutomationRegion>
24{
25 Q_OBJECT
26 QML_ELEMENT
27 Q_PROPERTY (dsp::ProcessorParameter * parameter READ parameter CONSTANT)
28 Q_PROPERTY (
29 AutomationMode automationMode READ getAutomationMode WRITE setAutomationMode
30 NOTIFY automationModeChanged)
31 Q_PROPERTY (
32 AutomationRecordMode recordMode READ getRecordMode WRITE setRecordMode
33 NOTIFY recordModeChanged)
34 DEFINE_ARRANGER_OBJECT_OWNER_QML_PROPERTIES (
36 regions,
38 QML_UNCREATABLE ("")
39
40public:
41 using ArrangerObjectRegistry = arrangement::ArrangerObjectRegistry;
42 using AutomationRegion = arrangement::AutomationRegion;
43 using AutomationPoint = arrangement::AutomationPoint;
44
46 static constexpr int AUTOMATION_RECORDING_TOUCH_REL_MS = 800;
47
48 enum class AutomationMode : basic_enum_base_type_t
49 {
50 Read,
51 Record,
52 Off,
53 };
54 Q_ENUM (AutomationMode)
55
56 enum class AutomationRecordMode : basic_enum_base_type_t
57 {
58 Touch,
59 Latch,
60 };
61 Q_ENUM (AutomationRecordMode)
62
63public:
66 const dsp::TempoMapWrapper &tempo_map,
67 dsp::FileAudioSourceRegistry &file_audio_source_registry,
68 ArrangerObjectRegistry &obj_registry,
69 dsp::ProcessorParameterUuidReference param_id,
70 QObject * parent = nullptr);
71
72public:
73 // ========================================================================
74 // QML Interface
75 // ========================================================================
76
77 dsp::ProcessorParameter * parameter () const
78 {
79 return param_id_.get_object_as<dsp::ProcessorParameter> ();
80 }
81
82 AutomationMode getAutomationMode () const { return automation_mode_.load (); }
83 void setAutomationMode (AutomationMode automation_mode);
84 Q_SIGNAL void automationModeChanged (AutomationMode automation_mode);
85
86 AutomationRecordMode getRecordMode () const { return record_mode_; }
87 void setRecordMode (AutomationRecordMode record_mode);
88 Q_INVOKABLE void swapRecordMode ()
89 {
90 record_mode_ = static_cast<AutomationRecordMode> (
91 (std::to_underlying (record_mode_) + 1)
92 % magic_enum::enum_count<AutomationRecordMode> ());
93 Q_EMIT recordModeChanged (record_mode_);
94 }
95 Q_SIGNAL void recordModeChanged (AutomationRecordMode record_mode);
96
103 Q_INVOKABLE void
105
109 Q_SIGNAL void
111
112 // ========================================================================
117 [[gnu::hot]] bool should_read_automation () const;
118
133 [[gnu::hot]] bool should_be_recording (bool record_aps) const;
134
144 AutomationPoint * get_automation_point_around (
145 units::precise_tick_t position_ticks,
146 units::precise_tick_t delta_ticks,
147 bool search_only_backwards = false);
148
158 AutomationPoint * get_automation_point_before (
159 units::sample_t timeline_position,
160 bool search_only_regions_enclosing_position) const;
161
171 auto get_region_before (
172 units::sample_t pos_samples,
173 bool search_only_regions_enclosing_position) const -> AutomationRegion *;
174
183 std::optional<float> get_normalized_value (
184 units::sample_t timeline_frames,
185 bool search_only_regions_enclosing_position) const;
186
187 bool contains_automation () const { return !get_children_vector ().empty (); }
188
189 std::string
190 get_field_name_for_serialization (const AutomationRegion *) const override
191 {
192 return "regions";
193 }
194
195public:
196 static constexpr auto kParamIdKey = "paramId"sv;
197
198private:
199 static constexpr auto kAutomationModeKey = "automationMode"sv;
200 static constexpr auto kRecordModeKey = "recordMode"sv;
201 friend void to_json (nlohmann::json &j, const AutomationTrack &track);
202 friend void from_json (const nlohmann::json &j, AutomationTrack &track);
203
204 friend void init_from (
205 AutomationTrack &obj,
206 const AutomationTrack &other,
207 utils::ObjectCloneType clone_type);
208
209private:
210 const dsp::TempoMapWrapper &tempo_map_;
211 ArrangerObjectRegistry &object_registry_;
212
214 dsp::ProcessorParameterUuidReference param_id_;
215
217 std::atomic<AutomationMode> automation_mode_{ AutomationMode::Read };
218
220 AutomationRecordMode record_mode_{};
221
225 arrangement::AutomationTimelineDataProvider automation_data_provider_;
226
230 utils::QObjectUniquePtr<utils::PlaybackCacheScheduler>
231 automation_cache_request_debouncer_;
232};
233
237inline void
238generate_automation_tracks_for_processor (
239 std::vector<utils::QObjectUniquePtr<AutomationTrack>> &ret,
240 const dsp::ProcessorBase &processor,
241 const dsp::TempoMapWrapper &tempo_map,
242 dsp::FileAudioSourceRegistry &file_audio_source_registry,
243 arrangement::ArrangerObjectRegistry &object_registry)
244{
245 z_debug ("generating automation tracks for {}...", processor.get_node_name ());
246 for (const auto &param_ref : processor.get_parameters ())
247 {
248 auto * const param =
249 param_ref.template get_object_as<dsp::ProcessorParameter> ();
250 if (!param->automatable ())
251 continue;
252
253 ret.emplace_back (
254 utils::make_qobject_unique<AutomationTrack> (
255 tempo_map, file_audio_source_registry, object_registry, param_ref));
256 }
257}
258}
259
260DEFINE_ENUM_FORMATTER (
261 zrythm::structure::tracks::AutomationTrack::AutomationRecordMode,
262 AutomationRecordMode,
263 QT_TR_NOOP_UTF8 ("Touch"),
264 QT_TR_NOOP_UTF8 ("Latch"));
265
266DEFINE_ENUM_FORMATTER (
267 zrythm::structure::tracks::AutomationTrack::AutomationMode,
268 AutomationMode,
269 "On",
270 "Rec",
271 "Off");
utils::Utf8String get_node_name() const final
Returns a human friendly name of the node.
Processor parameter that accepts automation and modulation sources and integrates with QML and the DS...
Definition parameter.h:225
An automation point inside an AutomationTrack.
Represents an automation region, which contains a collection of automation points.
Q_SIGNAL void automationObjectsNeedRecache(utils::ExpandableTickRange affectedRange)
Fired when a new cache is required.
bool should_read_automation() const
Returns whether the automation in the automation track should be read.
std::optional< float > get_normalized_value(units::sample_t timeline_frames, bool search_only_regions_enclosing_position) const
Returns the normalized parameter value at the given timeline position.
bool should_be_recording(bool record_aps) const
Returns if the automation track should currently be recording data.
AutomationPoint * get_automation_point_before(units::sample_t timeline_position, bool search_only_regions_enclosing_position) const
Returns the last automation point before the given timeline position.
AutomationTrack(const dsp::TempoMapWrapper &tempo_map, dsp::FileAudioSourceRegistry &file_audio_source_registry, ArrangerObjectRegistry &obj_registry, dsp::ProcessorParameterUuidReference param_id, QObject *parent=nullptr)
Creates an automation track for the given parameter.
AutomationPoint * get_automation_point_around(units::precise_tick_t position_ticks, units::precise_tick_t delta_ticks, bool search_only_backwards=false)
Find an automation point near the given position position_ticks with a tolerance of delta_ticks.
Q_INVOKABLE void regeneratePlaybackCaches(utils::ExpandableTickRange affectedRange)
To be connected to to notify of any changes to the automation content.
auto get_region_before(units::sample_t pos_samples, bool search_only_regions_enclosing_position) const -> AutomationRegion *
Returns the active region at a given position, if any.