Zrythm v2.0.0-alpha.1
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
content_time_warp.h
1// SPDX-FileCopyrightText: © 2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <span>
7#include <vector>
8
9#include "dsp/position.h"
10#include "dsp/tempo_map_qml_adapter.h"
11#include "dsp/tick_types.h"
12#include "utils/units.h"
13
14#include <QObject>
15#include <QtQmlIntegration/qqmlintegration.h>
16
17namespace zrythm::dsp
18{
19
39class ContentTimeWarp : public QObject
40{
41 Q_OBJECT
42 QML_ELEMENT
43 QML_UNCREATABLE ("")
44
45public:
46 struct WarpPoint
47 {
48 ContentTick content_ticks;
49 TimelineTick timeline_delta_ticks;
50 };
51
52 ContentTimeWarp (
53 const TempoMapWrapper &tempo_map_wrapper,
54 const TimelinePosition * clip_position,
55 const ContentPosition * clip_length,
56 QObject * parent = nullptr);
57 ~ContentTimeWarp () override = default;
58
59 std::span<const WarpPoint> warpPoints () const { return warp_points_; }
60
75 TimelineTick contentToTimeline (ContentTick content) const;
76
85 Q_INVOKABLE double contentToTimelineTicksRelative (double contentTicks) const;
86
99 Q_INVOKABLE double
100 timelineTicksRelativeToContent (double timelineTicksRelative) const;
101
110 units::sample_t contentToTimelineSamples (ContentTick content) const;
111
120 ContentTick timelineToContent (TimelineTick timeline) const;
121
127 void configure_as_project (units::bpm_t source_bpm = units::bpm (0.0));
128 void configure_as_source (units::bpm_t source_bpm);
129 void configure_as_warped (
130 units::bpm_t source_bpm,
131 std::span<const WarpPoint> user_markers);
132
139 bool is_identity () const;
140
141Q_SIGNALS:
142 void mapChanged ();
143
144private:
145 void rebuild ();
146 void connect_for_mode ();
147 void disconnect_all ();
148
149 // Source-mode exact conversions (bypass warp points for exact results
150 // even beyond the current clip length). Both are pure functions of the
151 // tempo map, clip_position_, and source BPM.
152 //
153 // @pre clip_position_ != nullptr — Source mode requires a known clip
154 // start position (asserted).
155 TimelineTick source_content_to_timeline (ContentTick content) const;
156 ContentTick source_timeline_to_content (TimelineTick timeline) const;
157
158 enum class Mode
159 {
160 Project,
161 Source,
162 Warped
163 };
164
165 const TempoMapWrapper &tempo_map_wrapper_;
166 const TimelinePosition * clip_position_;
167 const ContentPosition * clip_length_;
168 units::bpm_t source_bpm_ = units::bpm (0.0);
169 Mode mode_ = Mode::Project;
170 std::vector<WarpPoint> user_markers_;
171 std::vector<WarpPoint> warp_points_;
172
173 QMetaObject::Connection tempo_conn_;
174 QMetaObject::Connection pos_conn_;
175 QMetaObject::Connection len_conn_;
176};
177
185TimelineTick
186warp_lookup (
187 std::span<const ContentTimeWarp::WarpPoint> warp_points,
188 ContentTick content_ticks);
189
195ContentTick
196reverse_warp_lookup (
197 std::span<const ContentTimeWarp::WarpPoint> warp_points,
198 TimelineTick timeline_delta_ticks);
199
200} // namespace zrythm::dsp
A Position whose ticks are content-space (clip-local) ticks.
Definition position.h:118
TimelineTick contentToTimeline(ContentTick content) const
Convert content ticks to ABSOLUTE timeline ticks.
ContentTick timelineToContent(TimelineTick timeline) const
Convert ABSOLUTE timeline ticks to content ticks.
Q_INVOKABLE double timelineTicksRelativeToContent(double timelineTicksRelative) const
Convert timeline ticks RELATIVE to the clip's start position back to content ticks.
Q_INVOKABLE double contentToTimelineTicksRelative(double contentTicks) const
Convert content ticks to timeline ticks RELATIVE to the clip's start position.
bool is_identity() const
Returns true if the warp mapping is 1:1 (no timestretch needed).
void configure_as_project(units::bpm_t source_bpm=units::bpm(0.0))
Configures Project mode (identity warp — clip follows project tempo).
units::sample_t contentToTimelineSamples(ContentTick content) const
Convert content ticks to ABSOLUTE timeline samples.
A Position whose ticks are absolute timeline ticks.
Definition position.h:97