Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
curve.h
1// SPDX-FileCopyrightText: © 2020, 2023-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "utils/format.h"
7#include "utils/logger.h"
8
9#include <QtQmlIntegration/qqmlintegration.h>
10
11#include <nlohmann/json_fwd.hpp>
12
13using namespace std::string_view_literals;
14
15namespace zrythm::dsp
16{
17
23class CurveOptions final
24{
25 Q_GADGET
26
27public:
33 enum class Algorithm
34 {
43
55
67
72
87 };
88 Q_ENUM (Algorithm)
89
90
91 static constexpr double SUPERELLIPSE_CURVINESS_BOUND = 0.82;
92 static constexpr double EXPONENT_CURVINESS_BOUND = 0.95;
93 static constexpr double VITAL_CURVINESS_BOUND = 1.00;
94
95public:
96 // Rule of 0
97 CurveOptions () = default;
98 CurveOptions (double curviness, Algorithm algo);
99
106 [[gnu::hot]] double get_normalized_y (double x, bool start_higher) const;
107
108 friend bool operator== (const CurveOptions &a, const CurveOptions &b);
109
110private:
111 static constexpr auto kCurvinessKey = "curviness"sv;
112 static constexpr auto kAlgorithmKey = "algorithm"sv;
113 friend void to_json (nlohmann::json &j, const CurveOptions &opts);
114 friend void from_json (const nlohmann::json &j, CurveOptions &opts);
115
116public:
119 double curviness_{ 0.0 };
120
123
124 BOOST_DESCRIBE_CLASS (CurveOptions, (), (curviness_, algo_), (), ())
125};
126
130class CurveOptionsQmlAdapter : public QObject
131{
132 Q_OBJECT
133 Q_PROPERTY (
134 double curviness READ curviness WRITE setCurviness NOTIFY curvinessChanged)
135 Q_PROPERTY (
136 zrythm::dsp::CurveOptions::Algorithm algorithm READ algorithm WRITE
137 setAlgorithm NOTIFY algorithmChanged)
138 QML_NAMED_ELEMENT (CurveOptions)
139 QML_UNCREATABLE ("")
140
141public:
142 CurveOptionsQmlAdapter (CurveOptions &options, QObject * parent = nullptr)
143 : QObject (parent), options_ (options)
144 {
145 }
146
147 // ========================================================================
148 // QML Interface
149 // ========================================================================
150
151 double curviness () const { return options_.curviness_; }
152 void setCurviness (double curviness)
153 {
154 if (qFuzzyCompare (options_.curviness_, curviness))
155 return;
156
157 curviness = std::clamp (curviness, -1.0, 1.0);
158 options_.curviness_ = curviness;
159 Q_EMIT curvinessChanged (curviness);
160 }
161 Q_SIGNAL void curvinessChanged (double curviness);
162
163 zrythm::dsp::CurveOptions::Algorithm algorithm () const
164 {
165 return options_.algo_;
166 }
167 void setAlgorithm (zrythm::dsp::CurveOptions::Algorithm algorithm)
168 {
169 if (options_.algo_ == algorithm)
170 return;
171
172 options_.algo_ = algorithm;
173 Q_EMIT algorithmChanged (algorithm);
174 }
175 Q_SIGNAL void
176 algorithmChanged (zrythm::dsp::CurveOptions::Algorithm algorithm);
177
178 Q_INVOKABLE double normalizedY (double x, bool startHigher) const
179 {
180 return options_.get_normalized_y (x, startHigher);
181 }
182
183 // ========================================================================
184
185private:
186 CurveOptions &options_;
187};
188
189} // namespace zrythm::dsp
190
191DEFINE_ENUM_FORMATTER (
193 CurveOptions_Algorithm,
194 QT_TR_NOOP_UTF8 ("Exponent"),
195 QT_TR_NOOP_UTF8 ("Superellipse"),
196 "Vital",
197 QT_TR_NOOP_UTF8 ("Pulse"),
198 QT_TR_NOOP_UTF8 ("Logarithmic"));
Curve options.
Definition curve.h:24
Algorithm algo_
Curve algorithm to use.
Definition curve.h:122
static constexpr double SUPERELLIPSE_CURVINESS_BOUND
Bounds for each algorithm.
Definition curve.h:91
Algorithm
The algorithm to use for curves.
Definition curve.h:34
@ Vital
(e^(nx) - 1) / (e^n - 1) -10 <= n <= 10 where positive tilts down and negative tilts up (when startin...
Definition curve.h:66
@ Logarithmic
a = log (n) b = 1 / (log (1 + (1 / n)))
Definition curve.h:86
@ SuperEllipse
y = 1 - (1 - x^n)^(1/n) 0 < n <= 1, where the whole thing is tilting up and 0 is full tilt and 1 is s...
Definition curve.h:54
@ Exponent
y = x^n 0 < n <= 1, where the whole thing is tilting up and 0 is full tilt and 1 is straight line (wh...
Definition curve.h:42
double get_normalized_y(double x, bool start_higher) const
Returns the Y value on a curve.
double curviness_
Curviness between -1 and 1, where < 0 tils downwards, > 0 tilts upwards and 0 is a straight line.
Definition curve.h:119