Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
project.h
1// SPDX-FileCopyrightText: © 2018-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "dsp/audio_pool.h"
7#include "dsp/engine.h"
8#include "dsp/metronome.h"
9#include "dsp/port_connections_manager.h"
10#include "dsp/snap_grid.h"
11#include "dsp/tempo_map_qml_adapter.h"
12#include "dsp/transport.h"
13#include "plugins/plugin.h"
14#include "plugins/plugin_factory.h"
15#include "structure/arrangement/arranger_object_factory.h"
16#include "structure/arrangement/tempo_object_manager.h"
17#include "structure/scenes/clip_launcher.h"
18#include "structure/scenes/clip_playback_service.h"
19#include "structure/tracks/track_factory.h"
20#include "structure/tracks/tracklist.h"
21#include "utils/app_settings.h"
22
23namespace zrythm::structure::project
24{
25
26#define PORT_CONNECTIONS_MGR (PROJECT->port_connections_manager_.get ())
27#define AUDIO_POOL (PROJECT->audio_pool_.get ())
28#define TRANSPORT (PROJECT->transport_)
29
30#define TRACKLIST (PROJECT->tracklist_)
31#define P_CHORD_TRACK (TRACKLIST->singletonTracks ()->chordTrack ())
32#define P_MARKER_TRACK (TRACKLIST->singletonTracks ()->markerTrack ())
33#define P_MASTER_TRACK (TRACKLIST->singletonTracks ()->masterTrack ())
34#define P_MODULATOR_TRACK (TRACKLIST->singletonTracks ()->modulatorTrack ())
35#define SNAP_GRID_TIMELINE (PROJECT->snapGridTimeline ())
36#define SNAP_GRID_EDITOR (PROJECT->snapGridEditor ())
37#define MONITOR_FADER (PROJECT->controlRoom ()->monitor_fader_)
38#define ROUTER (&PROJECT->engine ()->graph_dispatcher ())
39#define AUDIO_ENGINE (PROJECT->engine ())
40
44class Project final : public QObject
45{
46 Q_OBJECT
47 QML_ELEMENT
48 Q_PROPERTY (
49 QString title READ getTitle WRITE setTitle NOTIFY titleChanged FINAL)
50 Q_PROPERTY (
51 QString directory READ directory WRITE setDirectory NOTIFY directoryChanged
52 FINAL)
53 Q_PROPERTY (
54 zrythm::structure::tracks::Tracklist * tracklist READ tracklist CONSTANT
55 FINAL)
56 Q_PROPERTY (
57 zrythm::structure::scenes::ClipLauncher * clipLauncher READ clipLauncher
58 CONSTANT FINAL)
59 Q_PROPERTY (
60 zrythm::structure::scenes::ClipPlaybackService * clipPlaybackService READ
61 clipPlaybackService CONSTANT FINAL)
62 Q_PROPERTY (zrythm::dsp::AudioEngine * engine READ engine CONSTANT FINAL)
63 Q_PROPERTY (zrythm::dsp::Metronome * metronome READ metronome CONSTANT)
64 Q_PROPERTY (
65 zrythm::dsp::Transport * transport READ getTransport CONSTANT FINAL)
66 Q_PROPERTY (
67 zrythm::dsp::TempoMapWrapper * tempoMap READ getTempoMap CONSTANT FINAL)
68 Q_PROPERTY (
70 tempoObjectManager CONSTANT FINAL)
71 Q_PROPERTY (
72 zrythm::dsp::SnapGrid * snapGridTimeline READ snapGridTimeline CONSTANT FINAL)
73 Q_PROPERTY (
74 zrythm::dsp::SnapGrid * snapGridEditor READ snapGridEditor CONSTANT FINAL)
75 QML_UNCREATABLE ("")
76
77 friend struct PluginBuilderForDeserialization;
78
79public:
80 using TrackUuid = structure::tracks::TrackUuid;
81 using PluginPtrVariant = plugins::PluginPtrVariant;
82 using PluginRegistry = plugins::PluginRegistry;
83
84public:
85 Project (
86 utils::AppSettings &app_settings,
87 std::shared_ptr<juce::AudioDeviceManager> device_manager,
88 std::shared_ptr<juce::AudioPluginFormatManager> plugin_format_manager,
89 plugins::PluginHostWindowFactory plugin_host_window_provider,
90 dsp::Fader &monitor_fader,
91 utils::Utf8String zrythm_version,
92 QObject * parent = nullptr);
93 ~Project () override;
94 Z_DISABLE_COPY_MOVE (Project)
95
96 // =========================================================
97 // QML interface
98 // =========================================================
99
100 QString getTitle () const;
101 void setTitle (const QString &title);
102 QString directory () const;
103 void setDirectory (const QString &directory);
104 structure::tracks::Tracklist * tracklist () const;
105 structure::scenes::ClipLauncher * clipLauncher () const;
106 structure::scenes::ClipPlaybackService * clipPlaybackService () const;
107 dsp::Metronome * metronome () const;
108 dsp::Transport * getTransport () const;
109 dsp::AudioEngine * engine () const;
110 dsp::TempoMapWrapper * getTempoMap () const;
111 dsp::SnapGrid * snapGridTimeline () const;
112 dsp::SnapGrid * snapGridEditor () const;
113 structure::arrangement::TempoObjectManager * tempoObjectManager () const;
114
115 Q_SIGNAL void titleChanged (const QString &title);
116 Q_SIGNAL void directoryChanged (const QString &directory);
117 Q_SIGNAL void aboutToBeDeleted ();
118
119 // =========================================================
120
121 // static Project * get_active_instance ();
122
123 dsp::Fader &monitor_fader ();
124
125 fs::path get_directory (bool for_backup) const;
126
127 // TODO: delete this getter, no one else should use factory directly
128 auto * arrangerObjectFactory () const
129 {
130 return arranger_object_factory_.get ();
131 }
132
133 friend void init_from (
134 Project &obj,
135 const Project &other,
136 utils::ObjectCloneType clone_type);
137
147 Q_INVOKABLE Project * clone (bool for_backup) const;
155 std::optional<fs::path> get_newer_backup ();
156
162 Q_INVOKABLE void activate ();
163
169 void add_default_tracks ();
170
172 get_final_track_dependencies () const;
173
174 auto &get_file_audio_source_registry () const
175 {
176 return *file_audio_source_registry_;
177 }
178 auto &get_track_registry () const { return *track_registry_; }
179 auto &get_plugin_registry () const { return *plugin_registry_; }
180 auto &get_port_registry () const { return *port_registry_; }
181 auto &get_param_registry () const { return *param_registry_; }
182 auto &get_arranger_object_registry () const
183 {
184 return *arranger_object_registry_;
185 }
186
194 std::optional<dsp::PortPtrVariant>
195 find_port_by_id (const dsp::Port::Uuid &id) const
196 {
197 return get_port_registry ().find_by_id (id);
198 }
199
201 find_param_by_id (const dsp::ProcessorParameter::Uuid &id) const
202 {
203 const auto opt_var = get_param_registry ().find_by_id (id);
204 if (opt_var.has_value ())
205 {
206 return std::get<dsp::ProcessorParameter *> (opt_var.value ());
207 }
208 return nullptr;
209 }
210
211 std::optional<plugins::PluginPtrVariant>
212 find_plugin_by_id (plugins::Plugin::Uuid id) const
213 {
214 return get_plugin_registry ().find_by_id (id);
215 }
216
217 std::optional<zrythm::structure::tracks::TrackPtrVariant>
218 find_track_by_id (structure::tracks::Track::Uuid id) const
219 {
220 return get_track_registry ().find_by_id (id);
221 }
222
223 std::optional<zrythm::structure::arrangement::ArrangerObjectPtrVariant>
224 find_arranger_object_by_id (
225 structure::arrangement::ArrangerObject::Uuid id) const
226 {
227 return get_arranger_object_registry ().find_by_id (id);
228 }
229
230 const auto &get_tempo_map () const { return tempo_map_; }
231
232private:
233 static constexpr auto kTempoMapKey = "tempoMap"sv;
234 static constexpr auto kFileAudioSourceRegistryKey =
235 "fileAudioSourceRegistry"sv;
236 static constexpr auto kPortRegistryKey = "portRegistry"sv;
237 static constexpr auto kParameterRegistryKey = "paramRegistry"sv;
238 static constexpr auto kPluginRegistryKey = "pluginRegistry"sv;
239 static constexpr auto kArrangerObjectRegistryKey = "arrangerObjectRegistry"sv;
240 static constexpr auto kTrackRegistryKey = "trackRegistry"sv;
241 static constexpr auto kTitleKey = "title"sv;
242 static constexpr auto kDatetimeKey = "datetime"sv;
243 static constexpr auto kVersionKey = "version"sv;
244 static constexpr auto kSnapGridTimelineKey = "snapGridTimeline"sv;
245 static constexpr auto kSnapGridEditorKey = "snapGridEditor"sv;
246 static constexpr auto kTransportKey = "transport"sv;
247 static constexpr auto kAudioPoolKey = "audioPool"sv;
248 static constexpr auto kTracklistKey = "tracklist"sv;
249 static constexpr auto kRegionLinkGroupManagerKey = "regionLinkGroupManager"sv;
250 static constexpr auto kPortConnectionsManagerKey = "portConnectionsManager"sv;
251 static constexpr auto kTempoObjectManagerKey = "tempoObjectManager"sv;
252 static constexpr auto DOCUMENT_TYPE = "ZrythmProject"sv;
253 static constexpr auto FORMAT_MAJOR_VER = 2;
254 static constexpr auto FORMAT_MINOR_VER = 1;
255 friend void to_json (nlohmann::json &j, const Project &project);
256 friend void from_json (const nlohmann::json &j, Project &project);
257
258private:
259 utils::AppSettings &app_settings_;
260 dsp::TempoMap tempo_map_;
262
263 plugins::PluginHostWindowFactory plugin_host_window_provider_;
264
265 dsp::FileAudioSourceRegistry * file_audio_source_registry_{};
266 dsp::PortRegistry * port_registry_{};
267 dsp::ProcessorParameterRegistry * param_registry_{};
268 PluginRegistry * plugin_registry_{};
269 structure::arrangement::ArrangerObjectRegistry * arranger_object_registry_{};
270 structure::tracks::TrackRegistry * track_registry_{};
271
272public:
275
280 fs::path dir_;
281
287 std::optional<fs::path> backup_dir_;
288
289 /* !!! IMPORTANT: order matters (for destruction) !!! */
290
293
301
302 std::shared_ptr<juce::AudioDeviceManager> device_manager_;
304 std::shared_ptr<juce::AudioPluginFormatManager> plugin_format_manager_;
305
312
320
325
330
332 std::unique_ptr<dsp::AudioPool> pool_;
333
335 // structure::arrangement::RegionLinkGroupManager region_link_group_manager_;
343
347 boost::unordered_flat_map<
348 structure::tracks::TrackUuid,
349 structure::tracks::TrackPtrVariant>
351
354 clip_playback_service_;
355
356 std::unique_ptr<structure::arrangement::ArrangerObjectFactory>
357 arranger_object_factory_;
359 std::unique_ptr<structure::tracks::TrackFactory> track_factory_;
360
362 tempo_object_manager_;
363
364 dsp::Fader &monitor_fader_;
365
373
375 int format_major_ = 0;
376 int format_minor_ = 0;
377};
378
379}
The audio engine.
Definition engine.h:22
The DspGraphDispatcher class manages the processing graph for the audio engine.
A Fader is a processor that is used for volume controls and pan.
Definition fader.h:21
Metronome processor.
Definition metronome.h:20
Wrapper over a Uuid registry that provides (slow) lookup by unique ID.
Definition parameter.h:523
Processor parameter that accepts automation and modulation sources and integrates with QML and the DS...
Definition parameter.h:225
Snap/grid information.
Definition snap_grid.h:22
The Transport class represents the transport controls and state for an audio engine.
Definition transport.h:45
Manages tempo and time signature objects for a project.
dsp::DspGraphDispatcher graph_dispatcher_
Graph dispatcher.
Definition project.h:358
utils::QObjectUniquePtr< dsp::SnapGrid > snap_grid_editor_
Snap/Grid info for the editor.
Definition project.h:300
boost::unordered_flat_map< structure::tracks::TrackUuid, structure::tracks::TrackPtrVariant > tracks_rt_
Realtime cache of tracks.
Definition project.h:336
utils::Utf8String version_
Zrythm version, for serialization.
Definition project.h:278
std::unique_ptr< dsp::AudioPool > pool_
Audio file pool.
Definition project.h:318
int format_major_
Used when deserializing projects.
Definition project.h:361
Q_INVOKABLE Project * clone(bool for_backup) const
Deep-clones the given project.
std::optional< fs::path > get_newer_backup()
Returns the filepath of a backup (directory), if any, if it has a newer timestamp than main project f...
utils::QObjectUniquePtr< dsp::Transport > transport_
Timeline metadata like BPM, time signature, etc.
Definition project.h:310
std::optional< fs::path > backup_dir_
Backup dir to save the project during the current save call.
Definition project.h:273
std::optional< dsp::PortPtrVariant > find_port_by_id(const dsp::Port::Uuid &id) const
Finds the Port corresponding to the identifier.
Definition project.h:181
utils::QObjectUniquePtr< dsp::SnapGrid > snap_grid_timeline_
Snap/Grid info for the timeline.
Definition project.h:303
void add_default_tracks()
Adds the default undeletable tracks to the project.
bool loading_from_backup_
Whether the current is currently being loaded from a backup file.
Definition project.h:286
utils::Utf8String title_
Project title.
Definition project.h:260
Q_INVOKABLE void activate()
Connects things up, exposes ports to the backend, calculates the graph and begins processing.
utils::Utf8String datetime_str_
Datetime string to add to the project file.
Definition project.h:263
utils::QObjectUniquePtr< dsp::PortConnectionsManager > port_connections_manager_
Must be free'd after engine.
Definition project.h:297
utils::QObjectUniquePtr< dsp::AudioEngine > audio_engine_
The audio backend.
Definition project.h:315
fs::path dir_
Path to save the project in.
Definition project.h:266
utils::QObjectUniquePtr< structure::tracks::Tracklist > tracklist_
Manager for region link groups.
Definition project.h:328
Service for managing clip playback operations.
A higher level wrapper over a track collection that serves as the project's only tracklist.
Definition tracklist.h:23
A unique pointer for QObject objects that also works with QObject-based ownership.
Definition qt.h:38
Lightweight UTF-8 string wrapper with safe conversions.
Definition utf8_string.h:38