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 <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);
98
105 [[gnu::hot]] double get_normalized_y (double x, bool start_higher) const;
106
107 friend bool operator== (const CurveOptions &a, const CurveOptions &b);
108
109private:
110 static constexpr auto kCurvinessKey = "curviness"sv;
111 static constexpr auto kAlgorithmKey = "algorithm"sv;
112 friend void to_json (nlohmann::json &j, const CurveOptions &opts);
113 friend void from_json (const nlohmann::json &j, CurveOptions &opts);
114
115public:
118 double curviness_{ 0.0 };
119
122
123 BOOST_DESCRIBE_CLASS (CurveOptions, (), (curviness_, algo_), (), ())
124};
125
129class CurveOptionsQmlAdapter : public QObject
130{
131 Q_OBJECT
132 Q_PROPERTY (
133 double curviness READ curviness WRITE setCurviness NOTIFY curvinessChanged)
134 Q_PROPERTY (
135 zrythm::dsp::CurveOptions::Algorithm algorithm READ algorithm WRITE
136 setAlgorithm NOTIFY algorithmChanged)
137 QML_NAMED_ELEMENT (CurveOptions)
138 QML_UNCREATABLE ("")
139
140public:
141 CurveOptionsQmlAdapter (CurveOptions &options, QObject * parent = nullptr)
142 : QObject (parent), options_ (options)
143 {
144 }
145
146 // ========================================================================
147 // QML Interface
148 // ========================================================================
149
150 double curviness () const { return options_.curviness_; }
151 void setCurviness (double curviness);
152 Q_SIGNAL void curvinessChanged (double curviness);
153
154 zrythm::dsp::CurveOptions::Algorithm algorithm () const
155 {
156 return options_.algo_;
157 }
158 void setAlgorithm (zrythm::dsp::CurveOptions::Algorithm algorithm);
159 Q_SIGNAL void
160 algorithmChanged (zrythm::dsp::CurveOptions::Algorithm algorithm);
161
162 Q_INVOKABLE double normalizedY (double x, bool startHigher) const
163 {
164 return options_.get_normalized_y (x, startHigher);
165 }
166
167 // ========================================================================
168
169private:
170 CurveOptions &options_;
171};
172
173} // namespace zrythm::dsp
174
175// These may be used in the UI eventually, but it's probably better to have a
176// method in CurveOptionsQmlAdapter that returns them (TODO)
177#if 0
178DEFINE_ENUM_FORMATTER (
180 CurveOptions_Algorithm,
181 QT_TR_NOOP_UTF8 ("Exponent"),
182 QT_TR_NOOP_UTF8 ("Superellipse"),
183 "Vital",
184 QT_TR_NOOP_UTF8 ("Pulse"),
185 QT_TR_NOOP_UTF8 ("Logarithmic"));
186#endif
Curve options.
Definition curve.h:23
Algorithm algo_
Curve algorithm to use.
Definition curve.h:121
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 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:118