Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
clip_playback_service.h
1// SPDX-FileCopyrightText: © 2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "structure/arrangement/arranger_object_all.h"
7#include "structure/scenes/clip_slot.h"
8#include "structure/tracks/track_collection.h"
9
10#include <QtQmlIntegration/qqmlintegration.h>
11
12namespace zrythm::structure::scenes
13{
14
22class ClipPlaybackService : public QObject
23{
24 Q_OBJECT
25 QML_ELEMENT
26 QML_UNCREATABLE ("")
27
28public:
29 explicit ClipPlaybackService (
30 arrangement::ArrangerObjectRegistry &object_registry,
31 const tracks::TrackCollection &track_collection,
32 QObject * parent = nullptr);
33
34 // ========================================================================
35 // Clip Operations
36 // ========================================================================
37
45 Q_INVOKABLE void launchClip (
46 tracks::Track * track,
47 ClipSlot * clipSlot,
48 structure::tracks::ClipQuantizeOption quantize =
49 structure::tracks::ClipQuantizeOption::NextBar);
50
58 Q_INVOKABLE void stopClip (
59 tracks::Track * track,
60 ClipSlot * clipSlot,
61 structure::tracks::ClipQuantizeOption quantize =
62 structure::tracks::ClipQuantizeOption::NextBar);
63
70 Q_INVOKABLE void launchScene (
71 Scene * scene,
72 structure::tracks::ClipQuantizeOption quantize =
73 structure::tracks::ClipQuantizeOption::NextBar);
74
81 Q_INVOKABLE void stopScene (
82 Scene * scene,
83 structure::tracks::ClipQuantizeOption quantize =
84 structure::tracks::ClipQuantizeOption::NextBar);
85
91 Q_INVOKABLE void stopAllClips (
92 structure::tracks::ClipQuantizeOption quantize =
93 structure::tracks::ClipQuantizeOption::NextBar);
94
95 // ========================================================================
96 // State Queries
97 // ========================================================================
98
105 Q_INVOKABLE bool isClipPlaying (ClipSlot * clipSlot) const;
106
114 Q_INVOKABLE double getClipPlaybackPosition (ClipSlot * clipSlot) const;
115
121 Q_INVOKABLE QList<ClipSlot *> getPlayingClips () const;
122
123 // ========================================================================
124 // Track Playback Mode Management
125 // ========================================================================
126
133
139 Q_INVOKABLE void setTrackToTimelineMode (tracks::Track * track);
140
150
151Q_SIGNALS:
155 void clipLaunched (tracks::Track * track, ClipSlot * clipSlot);
156
160 void clipStopped (tracks::Track * track, ClipSlot * clipSlot);
161
165 void sceneLaunched (Scene * scene);
166
170 void sceneStopped (Scene * scene);
171
172private:
173 // ========================================================================
174 // Internal Implementation
175 // ========================================================================
176
180 void scheduleClipLaunch (
181 tracks::Track * track,
182 ClipSlot * clipSlot,
183 structure::tracks::ClipQuantizeOption quantize);
184
188 void scheduleClipStop (
189 tracks::Track * track,
190 ClipSlot * clipSlot,
191 structure::tracks::ClipQuantizeOption quantize);
192
196 template <arrangement::RegionObject RegionT>
197 void generateClipEvents (
198 tracks::Track * track,
199 const RegionT &region,
200 structure::tracks::ClipQuantizeOption quantize)
201 {
202 if (track == nullptr)
203 return;
204
205 auto * processor = track->get_track_processor ();
206 if (processor == nullptr)
207 return;
208
209 // Generate events using the clip launcher event provider
210 if constexpr (std::is_same_v<RegionT, arrangement::MidiRegion>)
211 {
212 processor->clip_playback_data_provider ().generate_midi_events (
213 region, quantize);
214 }
215 else if constexpr (std::is_same_v<RegionT, arrangement::AudioRegion>)
216 {
217 processor->clip_playback_data_provider ().generate_audio_events (
218 region, quantize);
219 }
220 }
221
225 void updateClipSlotState (ClipSlot * clipSlot, ClipSlot::ClipState newState);
226
230 void startPlaybackStateMonitor (
231 tracks::Track * track,
232 ClipSlot * clipSlot,
233 ClipSlot::ClipState targetState);
234
238 void checkPlaybackState ();
239
240private:
241 arrangement::ArrangerObjectRegistry &object_registry_;
242 const tracks::TrackCollection &track_collection_;
243
247 std::unordered_map<tracks::Track::Uuid, ClipSlot *> playing_clips_;
248
252 std::unordered_map<
253 tracks::Track::Uuid,
254 std::tuple<utils::QObjectUniquePtr<QTimer>, ClipSlot *, ClipSlot::ClipState>>
255 playback_state_monitors_;
256};
257
258}
Q_INVOKABLE void stopAllClips(structure::tracks::ClipQuantizeOption quantize=structure::tracks::ClipQuantizeOption::NextBar)
Stop all playing clips across all tracks.
void setTrackToClipLauncherMode(tracks::Track *track)
Set a track to use clip launcher mode.
Q_INVOKABLE void launchScene(Scene *scene, structure::tracks::ClipQuantizeOption quantize=structure::tracks::ClipQuantizeOption::NextBar)
Launch all clips in a scene.
Q_INVOKABLE QList< ClipSlot * > getPlayingClips() const
Get all currently playing clips.
Q_INVOKABLE void launchClip(tracks::Track *track, ClipSlot *clipSlot, structure::tracks::ClipQuantizeOption quantize=structure::tracks::ClipQuantizeOption::NextBar)
Launch a clip with quantization.
void sceneStopped(Scene *scene)
Emitted when a scene is stopped.
Q_INVOKABLE bool isClipPlaying(ClipSlot *clipSlot) const
Check if a clip is currently playing.
void clipStopped(tracks::Track *track, ClipSlot *clipSlot)
Emitted when a clip is stopped.
Q_INVOKABLE void stopScene(Scene *scene, structure::tracks::ClipQuantizeOption quantize=structure::tracks::ClipQuantizeOption::NextBar)
Stop all clips in a scene.
void cancelPendingClipOperations(tracks::Track *track)
Cancel any pending clip operations for a track.
Q_INVOKABLE double getClipPlaybackPosition(ClipSlot *clipSlot) const
Get the current playback position of a clip, in.
Q_INVOKABLE void stopClip(tracks::Track *track, ClipSlot *clipSlot, structure::tracks::ClipQuantizeOption quantize=structure::tracks::ClipQuantizeOption::NextBar)
Stop a clip with quantization.
void sceneLaunched(Scene *scene)
Emitted when a scene is launched.
Q_INVOKABLE void setTrackToTimelineMode(tracks::Track *track)
Set a track to use timeline mode.
void clipLaunched(tracks::Track *track, ClipSlot *clipSlot)
Emitted when a clip is launched.
A collection of tracks that provides a QAbstractListModel interface.
Represents a track in the project.
Definition track.h:54