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/processor_base.h"
9#include "dsp/tempo_map_qml_adapter.h"
10#include "structure/arrangement/arranger_object_owner.h"
11#include "structure/arrangement/automation_region.h"
12#include "structure/arrangement/timeline_data_provider.h"
13#include "structure/tracks/playback_cache_activity_tracker.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 Q_PROPERTY (
40 playbackCacheActivityTracker READ playbackCacheActivityTracker CONSTANT)
41 QML_UNCREATABLE ("")
42
43public:
44 using ArrangerObjectRegistry = arrangement::ArrangerObjectRegistry;
45 using AutomationRegion = arrangement::AutomationRegion;
46 using AutomationPoint = arrangement::AutomationPoint;
47
49 static constexpr int AUTOMATION_RECORDING_TOUCH_REL_MS = 800;
50
51 enum class AutomationMode : uint8_t
52 {
53 Read,
54 Record,
55 Off,
56 };
57 Q_ENUM (AutomationMode)
58
59 enum class AutomationRecordMode : uint8_t
60 {
61 Touch,
62 Latch,
63 };
64 Q_ENUM (AutomationRecordMode)
65
66public:
69 const dsp::TempoMapWrapper &tempo_map,
70 dsp::FileAudioSourceRegistry &file_audio_source_registry,
71 ArrangerObjectRegistry &obj_registry,
72 dsp::ProcessorParameterUuidReference param_id,
73 QObject * parent = nullptr);
74
75public:
76 // ========================================================================
77 // QML Interface
78 // ========================================================================
79
80 dsp::ProcessorParameter * parameter () const
81 {
82 return param_id_.get_object_as<dsp::ProcessorParameter> ();
83 }
84
85 AutomationMode getAutomationMode () const { return automation_mode_.load (); }
86 void setAutomationMode (AutomationMode automation_mode);
87 Q_SIGNAL void automationModeChanged (AutomationMode automation_mode);
88
89 AutomationRecordMode getRecordMode () const { return record_mode_; }
90 void setRecordMode (AutomationRecordMode record_mode);
91 Q_INVOKABLE void swapRecordMode ();
92 Q_SIGNAL void recordModeChanged (AutomationRecordMode record_mode);
93
100 Q_INVOKABLE void
102
106 Q_SIGNAL void
108
109 // cache activity tracking
110 [[nodiscard]] PlaybackCacheActivityTracker *
111 playbackCacheActivityTracker () const
112 {
113 return playback_cache_activity_tracker_.get ();
114 }
115
116 // ========================================================================
121 [[gnu::hot]] bool should_read_automation () const;
122
137 [[gnu::hot]] bool should_be_recording (bool record_aps) const;
138
148 AutomationPoint * get_automation_point_around (
149 units::precise_tick_t position_ticks,
150 units::precise_tick_t delta_ticks,
151 bool search_only_backwards = false);
152
162 AutomationPoint * get_automation_point_before (
163 units::sample_t timeline_position,
164 bool search_only_regions_enclosing_position) const;
165
175 auto get_region_before (
176 units::sample_t pos_samples,
177 bool search_only_regions_enclosing_position) const -> AutomationRegion *;
178
187 std::optional<float> get_normalized_value (
188 units::sample_t timeline_frames,
189 bool search_only_regions_enclosing_position) const;
190
191 bool contains_automation () const { return !get_children_vector ().empty (); }
192
193 std::string
194 get_field_name_for_serialization (const AutomationRegion *) const override
195 {
196 return "regions";
197 }
198
199public:
200 static constexpr auto kParameterKey = "parameter"sv;
201
202private:
203 static constexpr auto kAutomationModeKey = "automationMode"sv;
204 static constexpr auto kRecordModeKey = "recordMode"sv;
205 friend void to_json (nlohmann::json &j, const AutomationTrack &track);
206 friend void from_json (const nlohmann::json &j, AutomationTrack &track);
207
208 friend void init_from (
209 AutomationTrack &obj,
210 const AutomationTrack &other,
211 utils::ObjectCloneType clone_type);
212
213private:
214 const dsp::TempoMapWrapper &tempo_map_;
215 ArrangerObjectRegistry &object_registry_;
216
218 dsp::ProcessorParameterUuidReference param_id_;
219
221 std::atomic<AutomationMode> automation_mode_{ AutomationMode::Read };
222
224 AutomationRecordMode record_mode_{};
225
229 utils::QObjectUniquePtr<arrangement::AutomationTimelineDataProvider>
230 automation_data_provider_;
231
235 utils::QObjectUniquePtr<utils::PlaybackCacheScheduler>
236 automation_cache_request_debouncer_;
237
238 utils::QObjectUniquePtr<PlaybackCacheActivityTracker>
239 playback_cache_activity_tracker_;
240};
241
245inline void
246generate_automation_tracks_for_processor (
247 std::vector<utils::QObjectUniquePtr<AutomationTrack>> &ret,
248 const dsp::ProcessorBase &processor,
249 const dsp::TempoMapWrapper &tempo_map,
250 dsp::FileAudioSourceRegistry &file_audio_source_registry,
251 arrangement::ArrangerObjectRegistry &object_registry)
252{
253 z_debug ("generating automation tracks for {}...", processor.get_node_name ());
254 for (const auto &param_ref : processor.get_parameters ())
255 {
256 auto * const param =
257 param_ref.template get_object_as<dsp::ProcessorParameter> ();
258 if (!param->automatable ())
259 continue;
260
261 ret.emplace_back (
262 utils::make_qobject_unique<AutomationTrack> (
263 tempo_map, file_audio_source_registry, object_registry, param_ref));
264 }
265}
266}
267
268DEFINE_ENUM_FORMATTER (
269 zrythm::structure::tracks::AutomationTrack::AutomationRecordMode,
270 AutomationRecordMode,
271 QT_TR_NOOP_UTF8 ("Touch"),
272 QT_TR_NOOP_UTF8 ("Latch"));
273
274DEFINE_ENUM_FORMATTER (
275 zrythm::structure::tracks::AutomationTrack::AutomationMode,
276 AutomationMode,
277 "On",
278 "Rec",
279 "Off");
Processor parameter that accepts automation and modulation sources and integrates with QML and the DS...
Definition parameter.h:167
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.
Helper that manages cache activity state for a cache-holding object.