Zrythm v2.0.0-alpha.1
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 <QObject>
7#include <QtQmlIntegration/qqmlintegration.h>
8
9#include <boost/describe/class.hpp>
10#include <nlohmann/json_fwd.hpp>
11
12using namespace std::string_view_literals;
13
14namespace zrythm::dsp
15{
16
22class CurveOptions final
23{
24 Q_GADGET
25
26public:
32 enum class Algorithm
33 {
42
54
66
71
86 };
87 Q_ENUM (Algorithm)
88
89
90 static constexpr double SUPERELLIPSE_CURVINESS_BOUND = 0.82;
91 static constexpr double EXPONENT_CURVINESS_BOUND = 0.95;
92 static constexpr double VITAL_CURVINESS_BOUND = 1.00;
93
94public:
95 // Rule of 0
96 CurveOptions () = default;
97 CurveOptions (double curviness, Algorithm algo) noexcept;
98
105 [[gnu::hot]] double
106 get_normalized_y (double x, bool start_higher) const noexcept;
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 Q_SIGNAL void curvinessChanged (double curviness);
154
155 zrythm::dsp::CurveOptions::Algorithm algorithm () const
156 {
157 return options_.algo_;
158 }
159 void setAlgorithm (zrythm::dsp::CurveOptions::Algorithm algorithm);
160 Q_SIGNAL void
161 algorithmChanged (zrythm::dsp::CurveOptions::Algorithm algorithm);
162
163 Q_INVOKABLE double normalizedY (double x, bool startHigher) const
164 {
165 return options_.get_normalized_y (x, startHigher);
166 }
167
168 // ========================================================================
169
170private:
171 CurveOptions &options_;
172};
173
188float
189evaluate_curve (
190 float value_a,
191 float value_b,
193 float curviness,
194 double ratio) noexcept [[clang::nonblocking]];
195
196} // namespace zrythm::dsp
197
198// These may be used in the UI eventually, but it's probably better to have a
199// method in CurveOptionsQmlAdapter that returns them (TODO)
200#if 0
201DEFINE_ENUM_FORMATTER (
203 CurveOptions_Algorithm,
204 QT_TR_NOOP_UTF8 ("Exponent"),
205 QT_TR_NOOP_UTF8 ("Superellipse"),
206 "Vital",
207 QT_TR_NOOP_UTF8 ("Pulse"),
208 QT_TR_NOOP_UTF8 ("Logarithmic"));
209#endif
Curve options.
Definition curve.h:23
Algorithm algo_
Curve algorithm to use.
Definition curve.h:122
double get_normalized_y(double x, bool start_higher) const noexcept
Returns the Y value on a curve.
static constexpr double SUPERELLIPSE_CURVINESS_BOUND
Bounds for each algorithm.
Definition curve.h:90
Algorithm
The algorithm to use for curves.
Definition curve.h:33
@ Vital
(e^(nx) - 1) / (e^n - 1) -10 <= n <= 10 where positive tilts down and negative tilts up (when startin...
Definition curve.h:65
@ Logarithmic
a = log (n) b = 1 / (log (1 + (1 / n)))
Definition curve.h:85
@ 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:53
@ 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:41
double curviness_
Curviness between -1 and 1, where < 0 tils downwards, > 0 tilts upwards and 0 is a straight line.
Definition curve.h:119