lmms
Cross-platform music production software
Loading...
Searching...
No Matches
AutomatableModel.h
Go to the documentation of this file.
1/*
2 * AutomatableModel.h - declaration of class AutomatableModel
3 *
4 * Copyright (c) 2007-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
5 *
6 * This file is part of LMMS - https://lmms.io
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program (see COPYING); if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA.
22 *
23 */
24
25#ifndef LMMS_AUTOMATABLE_MODEL_H
26#define LMMS_AUTOMATABLE_MODEL_H
27
28#include <cmath>
29#include <QMap>
30#include <QMutex>
31
32#include "JournallingObject.h"
33#include "Model.h"
34#include "TimePos.h"
35#include "ValueBuffer.h"
36#include "ModelVisitor.h"
37
38
39namespace lmms
40{
41
42// simple way to map a property of a view to a model
43#define mapPropertyFromModelPtr(type,getfunc,setfunc,modelname) \
44 public: \
45 type getfunc() const \
46 { \
47 return (type) modelname->value(); \
48 } \
49 public slots: \
50 void setfunc( const type val ) \
51 { \
52 modelname->setValue( val ); \
53 }
54
55#define mapPropertyFromModel(type,getfunc,setfunc,modelname) \
56 public: \
57 type getfunc() const \
58 { \
59 return (type) modelname.value(); \
60 } \
61 public slots: \
62 void setfunc( const type val ) \
63 { \
64 modelname.setValue( (float) val ); \
65 }
66
67// use this to make subclasses visitable
68#define MODEL_IS_VISITABLE \
69 void accept(ModelVisitor& v) override { v.visit(*this); } \
70 void accept(ConstModelVisitor& v) const override { v.visit(*this); }
71
72
73
74class ControllerConnection;
75
76class LMMS_EXPORT AutomatableModel : public Model, public JournallingObject
77{
78 Q_OBJECT
79public:
80 enum class ScaleType
81 {
82 Linear,
83 Logarithmic,
84 Decibel
85 };
86
87
88 ~AutomatableModel() override;
89
90 // Implement those by using the MODEL_IS_VISITABLE macro
91 virtual void accept(ModelVisitor& v) = 0;
92 virtual void accept(ConstModelVisitor& v) const = 0;
93
94public:
103 template<class Target>
104 Target* dynamicCast(bool doThrow = false)
105 {
107 if (doThrow && !vis.result) { Q_ASSERT(false); }
108 return vis.result;
109 }
110
112 template<class Target>
113 const Target* dynamicCast(bool doThrow = false) const
114 {
116 if (doThrow && !vis.result) { Q_ASSERT(false); }
117 return vis.result;
118 }
119
120 bool isAutomated() const;
122 {
123 return isAutomated() || m_controllerConnection != nullptr;
124 }
125
130
131
132 void setControllerConnection( ControllerConnection* c );
133
134
135 template<class T>
136 static T castValue( const float v )
137 {
138 if constexpr (std::is_same_v<T, int>)
139 {
140 return static_cast<int>(std::round(v));
141 }
142 else if constexpr (std::is_same_v<T, bool>)
143 {
144 return std::round(v) != 0;
145 }
146 else
147 {
148 return static_cast<T>(v);
149 }
150 }
151
152 template<class T>
153 inline T value( int frameOffset = 0 ) const
154 {
155 // TODO
156 // The `m_value` should only be updated whenever the Controller value changes,
157 // instead of the Model calling `controller->currentValue()` every time.
158 // This becomes even worse in the case of linked Models, where it has to
159 // loop through the list of all links.
160
162 {
164 {
165 return castValue<T>(controllerValue(frameOffset));
166 }
167 for (auto next = m_nextLink; next != this; next = next->m_nextLink)
168 {
169 if (next->controllerConnection() && next->useControllerValue())
170 {
171 return castValue<T>(fittedValue(next->controllerValue(frameOffset)));
172 }
173 }
174 }
175 return castValue<T>( m_value );
176 }
177
178 float controllerValue( int frameOffset ) const;
179
182 ValueBuffer * valueBuffer();
183
184 template<class T>
185 T initValue() const
186 {
187 return castValue<T>( m_initValue );
188 }
189
190 bool isAtInitValue() const
191 {
192 return m_value == m_initValue;
193 }
194
195 template<class T>
196 T minValue() const
197 {
198 return castValue<T>( m_minValue );
199 }
200
201 template<class T>
202 T maxValue() const
203 {
204 return castValue<T>( m_maxValue );
205 }
206
207 template<class T>
208 T step() const
209 {
210 return castValue<T>( m_step );
211 }
212
214 float scaledValue( float value ) const;
216 float inverseScaledValue( float value ) const;
218 float normValue(float value) const;
220 float inverseNormValue(float value) const;
221
222 void setInitValue( const float value );
223
224 void setValue(const float value, const bool isAutomated = false);
225
226 void incValue( int steps )
227 {
228 setValue( m_value + steps * m_step );
229 }
230
231 float range() const
232 {
233 return m_range;
234 }
235
236 void setRange( const float min, const float max, const float step = 1 );
238 m_scaleType = sc;
239 }
240 void setScaleLogarithmic( bool setToTrue = true )
241 {
243 }
245 {
247 }
248
249 void setStep( const float step );
250
251 float centerValue() const
252 {
253 return m_centerValue;
254 }
255
256 void setCenterValue( const float centerVal )
257 {
258 m_centerValue = centerVal;
259 }
260
262 void linkToModel(AutomatableModel* model);
264 size_t countLinks() const;
265
275 virtual void saveSettings(QDomDocument& doc, QDomElement& element, const QString& name);
276
281 virtual void loadSettings(const QDomElement& element, const QString& name);
282
283 QString nodeName() const override
284 {
285 return "automatablemodel";
286 }
287
288 virtual QString displayValue( const float val ) const = 0;
289
290 bool isLinked() const
291 {
292 return m_nextLink != this;
293 }
294
295 // a way to track changed values in the model and avoid using signals/slots - useful for speed-critical code.
296 // note that this method should only be called once per period since it resets the state of the variable - so if your model
297 // has to be accessed by more than one object, then this function shouldn't be used.
299 {
300 if( m_valueChanged || valueBuffer() )
301 {
302 m_valueChanged = false;
303 return true;
304 }
305 return false;
306 }
307
308 float globalAutomationValueAt( const TimePos& time );
309
310 void setStrictStepSize( const bool b )
311 {
313 }
314
316 {
318 }
319
320 static void resetPeriodCounter()
321 {
322 s_periodCounter = 0;
323 }
324
326 {
328 }
329
330public slots:
331 virtual void reset();
332 void unlink();
333 void unlinkControllerConnection();
334 void setUseControllerValue(bool b = true);
335
336
337protected:
339 const float val = 0,
340 const float min = 0,
341 const float max = 0,
342 const float step = 0,
343 Model* parent = nullptr,
344 const QString& displayName = QString(),
345 bool defaultConstructed = false );
350 float fittedValue( float value ) const;
351
352
353private:
354 // dynamicCast implementation
355 template<class Target>
357 {
358 Target* result = nullptr;
359 void visit(Target& tar) { result = &tar; }
360 };
361
362 // dynamicCast implementation
363 template<class Target>
365 {
366 const Target* result = nullptr;
367 void visit(const Target& tar) { result = &tar; }
368 };
369
370 static bool mustQuoteName(const QString &name);
371
372 void saveSettings( QDomDocument& doc, QDomElement& element ) override
373 {
374 saveSettings( doc, element, "value" );
375 }
376
377 void loadSettings( const QDomElement& element ) override
378 {
379 loadSettings( element, "value" );
380 }
381
382 void setValueInternal(const float value);
383
387 AutomatableModel* getLastLinkedModel() const;
388
390 bool isLinkedToModel(AutomatableModel* model) const;
391
394 template<class T> T logToLinearScale(T value) const;
395
399 template<class T> void roundAt(T& value, const T& where) const;
400
401
403 float m_value;
407 float m_step;
408 float m_range;
410
413
414 // used to determine if step size should be applied strictly (ie. always)
415 // or only when value set from gui (default)
417
421
422
425
426
429 static long s_periodCounter;
430
432
433 // prevent several threads from attempting to write the same vb at the same time
435
437
438signals:
439 void initValueChanged( float val );
441
442} ;
443
444
445
446
447template <typename T> class LMMS_EXPORT TypedAutomatableModel : public AutomatableModel
448{
449public:
451 T value( int frameOffset = 0 ) const
452 {
453 return AutomatableModel::value<T>( frameOffset );
454 }
455
456 T initValue() const
457 {
459 }
460
461 T minValue() const
462 {
464 }
465
466 T maxValue() const
467 {
469 }
470};
471
472
473// some typed AutomatableModel-definitions
474
475class LMMS_EXPORT FloatModel : public TypedAutomatableModel<float>
476{
477 Q_OBJECT
479public:
480 FloatModel( float val = 0, float min = 0, float max = 0, float step = 0,
481 Model * parent = nullptr,
482 const QString& displayName = QString(),
483 bool defaultConstructed = false ) :
484 TypedAutomatableModel( val, min, max, step, parent, displayName, defaultConstructed )
485 {
486 }
487 float getRoundedValue() const;
488 int getDigitCount() const;
489 QString displayValue( const float val ) const override;
490} ;
491
492
493class LMMS_EXPORT IntModel : public TypedAutomatableModel<int>
494{
495 Q_OBJECT
497public:
498 IntModel( int val = 0, int min = 0, int max = 0,
499 Model* parent = nullptr,
500 const QString& displayName = QString(),
501 bool defaultConstructed = false ) :
502 TypedAutomatableModel( val, min, max, 1, parent, displayName, defaultConstructed )
503 {
504 }
505 QString displayValue( const float val ) const override;
506} ;
507
508
509class LMMS_EXPORT BoolModel : public TypedAutomatableModel<bool>
510{
511 Q_OBJECT
513public:
514 BoolModel( const bool val = false,
515 Model* parent = nullptr,
516 const QString& displayName = QString(),
517 bool defaultConstructed = false ) :
518 TypedAutomatableModel( val, false, true, 1, parent, displayName, defaultConstructed )
519 {
520 }
521 QString displayValue( const float val ) const override;
522} ;
523
524using AutomatedValueMap = QMap<AutomatableModel*, float>;
525
526} // namespace lmms
527
528#endif // LMMS_AUTOMATABLE_MODEL_H
#define MODEL_IS_VISITABLE
Definition AutomatableModel.h:68
Definition AutomatableModel.h:77
const Target * dynamicCast(bool doThrow=false) const
const overload, see overloaded function
Definition AutomatableModel.h:113
bool isValueChanged()
Definition AutomatableModel.h:298
virtual void loadSettings(const QDomElement &element, const QString &name)
Loads settings of AutomatableModel from a DOM element.
Definition AutomatableModel.cpp:178
static void resetPeriodCounter()
Definition AutomatableModel.h:320
ValueBuffer m_valueBuffer
Definition AutomatableModel.h:427
T initValue() const
Definition AutomatableModel.h:185
bool isAutomatedOrControlled() const
Definition AutomatableModel.h:121
bool isAutomated() const
Definition AutomatableModel.cpp:90
bool useControllerValue() const
Definition AutomatableModel.h:325
void initValueChanged(float val)
float controllerValue(int frameOffset) const
Definition AutomatableModel.cpp:514
float m_maxValue
Definition AutomatableModel.h:406
void saveSettings(QDomDocument &doc, QDomElement &element) override
Definition AutomatableModel.h:372
static T castValue(const float v)
Definition AutomatableModel.h:136
float m_centerValue
Definition AutomatableModel.h:409
Target * dynamicCast(bool doThrow=false)
Return this class casted to Target.
Definition AutomatableModel.h:104
static long s_periodCounter
Definition AutomatableModel.h:429
AutomatableModel(const float val=0, const float min=0, const float max=0, const float step=0, Model *parent=nullptr, const QString &displayName=QString(), bool defaultConstructed=false)
Definition AutomatableModel.cpp:45
float m_initValue
Definition AutomatableModel.h:404
void destroyed(lmms::jo_id_t id)
void setScaleLogarithmic(bool setToTrue=true)
Definition AutomatableModel.h:240
static void incrementPeriodCounter()
Definition AutomatableModel.h:315
float range() const
Definition AutomatableModel.h:231
float m_step
Definition AutomatableModel.h:407
QString nodeName() const override
Definition AutomatableModel.h:283
void setCenterValue(const float centerVal)
Definition AutomatableModel.h:256
float centerValue() const
Definition AutomatableModel.h:251
T maxValue() const
Definition AutomatableModel.h:202
T value(int frameOffset=0) const
Definition AutomatableModel.h:153
ValueBuffer * valueBuffer()
Function that returns sample-exact data as a ValueBuffer.
Definition AutomatableModel.cpp:540
bool m_useControllerValue
Definition AutomatableModel.h:436
float m_minValue
Definition AutomatableModel.h:405
bool isAtInitValue() const
Definition AutomatableModel.h:190
float m_oldValue
used by valueBuffer for interpolation
Definition AutomatableModel.h:412
float fittedValue(float value) const
Definition AutomatableModel.cpp:414
void setScaleType(ScaleType sc)
Definition AutomatableModel.h:237
QMutex m_valueBufferMutex
Definition AutomatableModel.h:434
bool m_hasStrictStepSize
Definition AutomatableModel.h:416
long m_lastUpdatedPeriod
Definition AutomatableModel.h:428
AutomatableModel * m_nextLink
Definition AutomatableModel.h:420
virtual void accept(ModelVisitor &v)=0
void setValue(const float value, const bool isAutomated=false)
Definition AutomatableModel.cpp:294
ControllerConnection * m_controllerConnection
NULL if not appended to controller, otherwise connection info.
Definition AutomatableModel.h:424
bool isScaleLogarithmic() const
Definition AutomatableModel.h:244
ControllerConnection * controllerConnection() const
Definition AutomatableModel.h:126
void incValue(int steps)
Definition AutomatableModel.h:226
void loadSettings(const QDomElement &element) override
Definition AutomatableModel.h:377
ScaleType m_scaleType
scale type, linear by default
Definition AutomatableModel.h:402
virtual void accept(ConstModelVisitor &v) const =0
virtual void saveSettings(QDomDocument &doc, QDomElement &element, const QString &name)
Saves settings of AutomatableModel into a DOM element.
Definition AutomatableModel.cpp:103
float m_value
Definition AutomatableModel.h:403
T step() const
Definition AutomatableModel.h:208
T minValue() const
Definition AutomatableModel.h:196
bool isLinked() const
Definition AutomatableModel.h:290
ScaleType
Definition AutomatableModel.h:81
@ Logarithmic
Definition AutomatableModel.h:83
@ Linear
Definition AutomatableModel.h:82
virtual QString displayValue(const float val) const =0
bool m_valueChanged
Definition AutomatableModel.h:411
void setStrictStepSize(const bool b)
Definition AutomatableModel.h:310
float m_range
Definition AutomatableModel.h:408
bool m_hasSampleExactData
Definition AutomatableModel.h:431
BoolModel(const bool val=false, Model *parent=nullptr, const QString &displayName=QString(), bool defaultConstructed=false)
Definition AutomatableModel.h:514
Definition ModelVisitor.h:54
Definition ControllerConnection.h:53
FloatModel(float val=0, float min=0, float max=0, float step=0, Model *parent=nullptr, const QString &displayName=QString(), bool defaultConstructed=false)
Definition AutomatableModel.h:480
IntModel(int val=0, int min=0, int max=0, Model *parent=nullptr, const QString &displayName=QString(), bool defaultConstructed=false)
Definition AutomatableModel.h:498
JournallingObject()
Definition JournallingObject.cpp:35
Definition Model.h:37
virtual QString displayName() const
Definition Model.cpp:47
Model(Model *parent, QString displayName=QString(), bool defaultConstructed=false)
Definition Model.cpp:30
Definition ModelVisitor.h:40
Definition TimePos.h:68
Definition AutomatableModel.h:448
AutomatableModel(const float val=0, const float min=0, const float max=0, const float step=0, Model *parent=nullptr, const QString &displayName=QString(), bool defaultConstructed=false)
Definition AutomatableModel.cpp:45
T initValue() const
Definition AutomatableModel.h:456
T minValue() const
Definition AutomatableModel.h:461
T maxValue() const
Definition AutomatableModel.h:466
T value(int frameOffset=0) const
Definition AutomatableModel.h:451
Definition ValueBuffer.h:38
Definition RemotePluginBase.cpp:34
float logToLinearScale(float min, float max, float value)
Scales value from linear to logarithmic.
Definition lmms_math.h:241
QMap< AutomatableModel *, float > AutomatedValueMap
Definition AutomatableModel.h:524
std::uint32_t jo_id_t
Definition LmmsTypes.h:50
Definition AutomatableModel.h:365
void visit(const Target &tar)
Definition AutomatableModel.h:367
const Target * result
Definition AutomatableModel.h:366
Definition AutomatableModel.h:357
Target * result
Definition AutomatableModel.h:358
void visit(Target &tar)
Definition AutomatableModel.h:359