Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
curve.h
1// SPDX-FileCopyrightText: © 2020, 2023-2025 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>
10
11#include <nlohmann/json.hpp>
12
13namespace zrythm::dsp
14{
15
21class CurveOptions final
22{
23 Q_GADGET
24
25public:
31 enum class Algorithm
32 {
41
53
65
70
85 };
86 Q_ENUM (Algorithm)
87
88
89 static constexpr double SUPERELLIPSE_CURVINESS_BOUND = 0.82;
90 static constexpr double EXPONENT_CURVINESS_BOUND = 0.95;
91 static constexpr double VITAL_CURVINESS_BOUND = 1.00;
92
93public:
94 // Rule of 0
95 CurveOptions () = default;
96 CurveOptions (double curviness, Algorithm algo);
97
104 [[gnu::hot]] double get_normalized_y (double x, bool start_higher) const;
105
106 friend bool operator== (const CurveOptions &a, const CurveOptions &b);
107
108 NLOHMANN_DEFINE_TYPE_INTRUSIVE (CurveOptions, algo_, curviness_)
109
110public:
113 double curviness_{ 0.0 };
114
117
118 BOOST_DESCRIBE_CLASS (CurveOptions, (), (curviness_, algo_), (), ())
119};
120
124class CurveOptionsQmlAdapter : public QObject
125{
126 Q_OBJECT
127 Q_PROPERTY (
128 double curviness READ curviness WRITE setCurviness NOTIFY curvinessChanged)
129 Q_PROPERTY (
130 zrythm::dsp::CurveOptions::Algorithm algorithm READ algorithm WRITE
131 setAlgorithm NOTIFY algorithmChanged)
132 QML_NAMED_ELEMENT (CurveOptions)
133 QML_UNCREATABLE ("")
134
135public:
136 CurveOptionsQmlAdapter (CurveOptions &options, QObject * parent = nullptr)
137 : QObject (parent), options_ (options)
138 {
139 }
140
141 // ========================================================================
142 // QML Interface
143 // ========================================================================
144
145 double curviness () const { return options_.curviness_; }
146 void setCurviness (double curviness)
147 {
148 if (qFuzzyCompare (options_.curviness_, curviness))
149 return;
150
151 curviness = std::clamp (curviness, -1.0, 1.0);
152 options_.curviness_ = curviness;
153 Q_EMIT curvinessChanged (curviness);
154 }
155 Q_SIGNAL void curvinessChanged (double curviness);
156
157 zrythm::dsp::CurveOptions::Algorithm algorithm () const
158 {
159 return options_.algo_;
160 }
161 void setAlgorithm (zrythm::dsp::CurveOptions::Algorithm algorithm)
162 {
163 if (options_.algo_ == algorithm)
164 return;
165
166 options_.algo_ = algorithm;
167 Q_EMIT algorithmChanged (algorithm);
168 }
169 Q_SIGNAL void
170 algorithmChanged (zrythm::dsp::CurveOptions::Algorithm algorithm);
171
172 Q_INVOKABLE double normalizedY (double x, bool startHigher) const
173 {
174 return options_.get_normalized_y (x, startHigher);
175 }
176
177 // ========================================================================
178
179private:
180 CurveOptions &options_;
181};
182
183} // namespace zrythm::dsp
184
185DEFINE_ENUM_FORMATTER (
187 CurveOptions_Algorithm,
188 QT_TR_NOOP_UTF8 ("Exponent"),
189 QT_TR_NOOP_UTF8 ("Superellipse"),
190 "Vital",
191 QT_TR_NOOP_UTF8 ("Pulse"),
192 QT_TR_NOOP_UTF8 ("Logarithmic"));
Curve options.
Definition curve.h:22
Algorithm algo_
Curve algorithm to use.
Definition curve.h:116
static constexpr double SUPERELLIPSE_CURVINESS_BOUND
Bounds for each algorithm.
Definition curve.h:89
Algorithm
The algorithm to use for curves.
Definition curve.h:32
@ Vital
(e^(nx) - 1) / (e^n - 1) -10 <= n <= 10 where positive tilts down and negative tilts up (when startin...
Definition curve.h:64
@ Logarithmic
a = log (n) b = 1 / (log (1 + (1 / n)))
Definition curve.h:84
@ 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:52
@ 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:40
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:113