Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
position_proxy.h
1// SPDX-FileCopyrightText: © 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "dsp/position.h"
7#include "dsp/tempo_map_qml_adapter.h"
8#include "utils/icloneable.h"
9#include "utils/math.h"
10
11#include <QObject>
12#include <QtQmlIntegration>
13
14#include "realtime_property.h"
15
16namespace zrythm::engine::session
17{
18class Transport;
19}
20
21using namespace zrythm;
22
23Q_DECLARE_OPAQUE_POINTER (zrythm::dsp::Transport *)
24Q_DECLARE_OPAQUE_POINTER (const zrythm::dsp::Transport *)
25
26
32class PositionProxy
33 : public QObject,
34 public IRealtimeProperty,
35 public zrythm::dsp::Position
36{
37 Q_OBJECT
38 QML_ELEMENT
39
40 Q_PROPERTY (double ticks READ getTicks WRITE setTicks NOTIFY ticksChanged)
41 Q_PROPERTY (
42 signed_frame_t frames READ getFrames WRITE setFrames NOTIFY framesChanged)
43 Q_PROPERTY (
44 double ticksPerSixteenthNote READ getTicksPerSixteenthNote CONSTANT)
45
46public:
47 PositionProxy (
48 QObject * parent = nullptr,
49 const Position * pos = nullptr,
50 bool realtime_updateable = false);
51 ~PositionProxy () override;
52 Q_DISABLE_COPY_MOVE (PositionProxy)
53
54 signed_frame_t getFrames () const { return frames_; }
55 void setFrames (signed_frame_t frames);
56 Q_SIGNAL void framesChanged ();
57
58 double getTicks () const { return ticks_; }
59 void setTicks (double ticks);
60 Q_SIGNAL void ticksChanged ();
61
62 static double getTicksPerSixteenthNote ()
63 {
64 return TICKS_PER_SIXTEENTH_NOTE_DBL;
65 }
66 Position get_position () const
67 {
68 return *dynamic_cast<const Position *> (this);
69 }
70
71 Q_INVOKABLE void addTicks (double ticks) { setTicks (getTicks () + ticks); }
72
73 Q_INVOKABLE QString getStringDisplay (
74 const zrythm::dsp::Transport * transport,
75 const zrythm::dsp::TempoMapWrapper * tempo_map) const;
76
77public:
78 // RT-safe wrappers
79
80 void
81 set_frames_rtsafe (signed_frame_t frames, dsp::TicksPerFrame ticks_per_frame)
82 {
83 from_frames (frames, ticks_per_frame);
84 has_update_.store (true, std::memory_order_release);
85 }
86 void set_ticks_rtsafe (double ticks, dsp::FramesPerTick frames_per_tick)
87 {
88 from_ticks (ticks, frames_per_tick);
89 has_update_.store (true, std::memory_order_release);
90 }
91
92 void update_from_ticks_rtsafe (dsp::FramesPerTick frames_per_tick)
93 {
94 update_frames_from_ticks (frames_per_tick);
95 has_update_.store (true, std::memory_order_release);
96 }
97
98 void update_from_frames_rtsafe (dsp::TicksPerFrame ticks_per_frame)
99 {
100 update_ticks_from_frames (ticks_per_frame);
101 has_update_.store (true, std::memory_order_release);
102 }
103
104 void set_position_rtsafe (const Position &pos)
105 {
106 if (pos.frames_ == frames_ && utils::math::floats_equal (pos.ticks_, ticks_))
107 return;
108
109 frames_ = pos.frames_;
110 ticks_ = pos.ticks_;
111 has_update_.store (true, std::memory_order_release);
112 }
113
114 void
115 add_frames_rtsafe (signed_frame_t frames, dsp::TicksPerFrame ticks_per_frame)
116 {
117 if (frames == 0)
118 return;
119
120 add_frames (frames, ticks_per_frame);
121 has_update_.store (true, std::memory_order_release);
123
124 bool processUpdates () override;
125
126 friend void init_from (
127 PositionProxy &obj,
128 const PositionProxy &other,
129 utils::ObjectCloneType clone_type);
130
131 friend auto operator<=> (const PositionProxy &lhs, const PositionProxy &rhs)
132 {
133 return static_cast<const zrythm::dsp::Position &> (lhs)
134 <=> static_cast<const zrythm::dsp::Position &> (rhs);
135 }
136
137 friend bool operator== (const PositionProxy &lhs, const PositionProxy &rhs)
138 {
139 return (lhs <=> rhs) == 0;
140 }
141
142private:
143 std::atomic<bool> has_update_{ false };
144 bool realtime_updateable_;
145};
146
147inline auto
148format_as (const PositionProxy &p)
149{
150 return p.get_position ();
151}
Interface for real-time property updates.
QML-friendly position representation with real-time safety.
Represents the position of an object.
Definition position.h:72
void update_ticks_from_frames(TicksPerFrame ticks_per_frame)
Updates ticks.
void update_frames_from_ticks(FramesPerTick frames_per_tick)
Updates frames.
signed_frame_t frames_
Position in frames (samples).
Definition position.h:491
void add_frames(signed_frame_t frames, TicksPerFrame ticks_per_frame)
Adds the frames to the position and updates the rest of the fields, and makes sure the frames are sti...
Definition position.h:196
void from_ticks(double ticks, dsp::FramesPerTick frames_per_tick)
Sets position to the given total tick count.
Definition position.h:219
double ticks_
Precise total number of ticks.
Definition position.h:484
The Transport class represents the transport controls and state for an audio engine.
Definition transport.h:45
int_fast64_t signed_frame_t
Signed type for frame index.
Definition types.h:78
constexpr bool floats_equal(T a, T b)
Checks if 2 floating point numbers are equal.
Definition math.h:76