AutomatableModel.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. #ifndef AUTOMATABLE_MODEL_H
  25. #define AUTOMATABLE_MODEL_H
  26. #include <QtCore/QMap>
  27. #include <QtCore/QMutex>
  28. #include "JournallingObject.h"
  29. #include "Model.h"
  30. #include "MidiTime.h"
  31. #include "ValueBuffer.h"
  32. #include "MemoryManager.h"
  33. #include "ModelVisitor.h"
  34. // simple way to map a property of a view to a model
  35. #define mapPropertyFromModelPtr(type,getfunc,setfunc,modelname) \
  36. public: \
  37. type getfunc() const \
  38. { \
  39. return (type) modelname->value(); \
  40. } \
  41. public slots: \
  42. void setfunc( const type val ) \
  43. { \
  44. modelname->setValue( val ); \
  45. }
  46. #define mapPropertyFromModel(type,getfunc,setfunc,modelname) \
  47. public: \
  48. type getfunc() const \
  49. { \
  50. return (type) modelname.value(); \
  51. } \
  52. public slots: \
  53. void setfunc( const type val ) \
  54. { \
  55. modelname.setValue( (float) val ); \
  56. }
  57. // use this to make subclasses visitable
  58. #define MODEL_IS_VISITABLE \
  59. void accept(ModelVisitor& v) override { v.visit(*this); } \
  60. void accept(ConstModelVisitor& v) const override { v.visit(*this); }
  61. class ControllerConnection;
  62. class LMMS_EXPORT AutomatableModel : public Model, public JournallingObject
  63. {
  64. Q_OBJECT
  65. MM_OPERATORS
  66. public:
  67. typedef QVector<AutomatableModel *> AutoModelVector;
  68. enum ScaleType
  69. {
  70. Linear,
  71. Logarithmic,
  72. Decibel
  73. };
  74. virtual ~AutomatableModel();
  75. // Implement those by using the MODEL_IS_VISITABLE macro
  76. virtual void accept(ModelVisitor& v) = 0;
  77. virtual void accept(ConstModelVisitor& v) const = 0;
  78. public:
  79. /**
  80. @brief Return this class casted to Target
  81. @test AutomatableModelTest.cpp
  82. @param doThrow throw an assertion if the cast fails, instead of
  83. returning a nullptr
  84. @return the casted class if Target is the exact or a base class of
  85. *this, nullptr otherwise
  86. */
  87. template<class Target>
  88. Target* dynamicCast(bool doThrow = false)
  89. {
  90. DCastVisitor<Target> vis; accept(vis);
  91. if (doThrow && !vis.result) { Q_ASSERT(false); }
  92. return vis.result;
  93. }
  94. //! const overload, see overloaded function
  95. template<class Target>
  96. const Target* dynamicCast(bool doThrow = false) const
  97. {
  98. ConstDCastVisitor<Target> vis; accept(vis);
  99. if (doThrow && !vis.result) { Q_ASSERT(false); }
  100. return vis.result;
  101. }
  102. bool isAutomated() const;
  103. bool isAutomatedOrControlled() const
  104. {
  105. return isAutomated() || m_controllerConnection != NULL;
  106. }
  107. ControllerConnection* controllerConnection() const
  108. {
  109. return m_controllerConnection;
  110. }
  111. void setControllerConnection( ControllerConnection* c );
  112. template<class T>
  113. static T castValue( const float v )
  114. {
  115. return (T)( v );
  116. }
  117. template<bool>
  118. static bool castValue( const float v )
  119. {
  120. return ( qRound( v ) != 0 );
  121. }
  122. template<class T>
  123. inline T value( int frameOffset = 0 ) const
  124. {
  125. if( unlikely( hasLinkedModels() || m_controllerConnection != NULL ) )
  126. {
  127. return castValue<T>( controllerValue( frameOffset ) );
  128. }
  129. return castValue<T>( m_value );
  130. }
  131. float controllerValue( int frameOffset ) const;
  132. //! @brief Function that returns sample-exact data as a ValueBuffer
  133. //! @return pointer to model's valueBuffer when s.ex.data exists, NULL otherwise
  134. ValueBuffer * valueBuffer();
  135. template<class T>
  136. T initValue() const
  137. {
  138. return castValue<T>( m_initValue );
  139. }
  140. bool isAtInitValue() const
  141. {
  142. return m_value == m_initValue;
  143. }
  144. template<class T>
  145. T minValue() const
  146. {
  147. return castValue<T>( m_minValue );
  148. }
  149. template<class T>
  150. T maxValue() const
  151. {
  152. return castValue<T>( m_maxValue );
  153. }
  154. template<class T>
  155. T step() const
  156. {
  157. return castValue<T>( m_step );
  158. }
  159. //! @brief Returns value scaled with the scale type and min/max values of this model
  160. float scaledValue( float value ) const;
  161. //! @brief Returns value applied with the inverse of this model's scale type
  162. float inverseScaledValue( float value ) const;
  163. void setInitValue( const float value );
  164. void setAutomatedValue( const float value );
  165. void setValue( const float value );
  166. void incValue( int steps )
  167. {
  168. setValue( m_value + steps * m_step );
  169. }
  170. float range() const
  171. {
  172. return m_range;
  173. }
  174. void setRange( const float min, const float max, const float step = 1 );
  175. void setScaleType( ScaleType sc ) {
  176. m_scaleType = sc;
  177. }
  178. void setScaleLogarithmic( bool setToTrue = true )
  179. {
  180. setScaleType( setToTrue ? Logarithmic : Linear );
  181. }
  182. bool isScaleLogarithmic() const
  183. {
  184. return m_scaleType == Logarithmic;
  185. }
  186. void setStep( const float step );
  187. float centerValue() const
  188. {
  189. return m_centerValue;
  190. }
  191. void setCenterValue( const float centerVal )
  192. {
  193. m_centerValue = centerVal;
  194. }
  195. static void linkModels( AutomatableModel* m1, AutomatableModel* m2 );
  196. static void unlinkModels( AutomatableModel* m1, AutomatableModel* m2 );
  197. void unlinkAllModels();
  198. /**
  199. * @brief Saves settings (value, automation links and controller connections) of AutomatableModel into
  200. * specified DOM element using <name> as attribute/node name
  201. * @param doc TODO
  202. * @param element Where this option shall be saved.
  203. * Depending on the model, this can be done in an attribute or in a subnode.
  204. * @param name Name to store this model as.
  205. */
  206. virtual void saveSettings( QDomDocument& doc, QDomElement& element, const QString& name );
  207. /*! \brief Loads settings (value, automation links and controller connections) of AutomatableModel from
  208. specified DOM element using <name> as attribute/node name */
  209. virtual void loadSettings( const QDomElement& element, const QString& name );
  210. QString nodeName() const override
  211. {
  212. return "automatablemodel";
  213. }
  214. virtual QString displayValue( const float val ) const = 0;
  215. bool hasLinkedModels() const
  216. {
  217. return !m_linkedModels.empty();
  218. }
  219. // a way to track changed values in the model and avoid using signals/slots - useful for speed-critical code.
  220. // note that this method should only be called once per period since it resets the state of the variable - so if your model
  221. // has to be accessed by more than one object, then this function shouldn't be used.
  222. bool isValueChanged()
  223. {
  224. if( m_valueChanged || valueBuffer() )
  225. {
  226. m_valueChanged = false;
  227. return true;
  228. }
  229. return false;
  230. }
  231. float globalAutomationValueAt( const MidiTime& time );
  232. void setStrictStepSize( const bool b )
  233. {
  234. m_hasStrictStepSize = b;
  235. }
  236. static void incrementPeriodCounter()
  237. {
  238. ++s_periodCounter;
  239. }
  240. static void resetPeriodCounter()
  241. {
  242. s_periodCounter = 0;
  243. }
  244. public slots:
  245. virtual void reset();
  246. void unlinkControllerConnection();
  247. protected:
  248. AutomatableModel(
  249. const float val = 0,
  250. const float min = 0,
  251. const float max = 0,
  252. const float step = 0,
  253. Model* parent = NULL,
  254. const QString& displayName = QString(),
  255. bool defaultConstructed = false );
  256. //! returns a value which is in range between min() and
  257. //! max() and aligned according to the step size (step size 0.05 -> value
  258. //! 0.12345 becomes 0.10 etc.). You should always call it at the end after
  259. //! doing your own calculations.
  260. float fittedValue( float value ) const;
  261. private:
  262. // dynamicCast implementation
  263. template<class Target>
  264. struct DCastVisitor : public ModelVisitor
  265. {
  266. Target* result = nullptr;
  267. void visit(Target& tar) { result = &tar; }
  268. };
  269. // dynamicCast implementation
  270. template<class Target>
  271. struct ConstDCastVisitor : public ConstModelVisitor
  272. {
  273. const Target* result = nullptr;
  274. void visit(const Target& tar) { result = &tar; }
  275. };
  276. static bool mustQuoteName(const QString &name);
  277. void saveSettings( QDomDocument& doc, QDomElement& element ) override
  278. {
  279. saveSettings( doc, element, "value" );
  280. }
  281. void loadSettings( const QDomElement& element ) override
  282. {
  283. loadSettings( element, "value" );
  284. }
  285. void linkModel( AutomatableModel* model );
  286. void unlinkModel( AutomatableModel* model );
  287. //! @brief Scales @value from linear to logarithmic.
  288. //! Value should be within [0,1]
  289. template<class T> T logToLinearScale( T value ) const;
  290. //! rounds @a value to @a where if it is close to it
  291. //! @param value will be modified to rounded value
  292. template<class T> void roundAt( T &value, const T &where ) const;
  293. ScaleType m_scaleType; //! scale type, linear by default
  294. float m_value;
  295. float m_initValue;
  296. float m_minValue;
  297. float m_maxValue;
  298. float m_step;
  299. float m_range;
  300. float m_centerValue;
  301. bool m_valueChanged;
  302. // currently unused?
  303. float m_oldValue;
  304. int m_setValueDepth;
  305. // used to determine if step size should be applied strictly (ie. always)
  306. // or only when value set from gui (default)
  307. bool m_hasStrictStepSize;
  308. AutoModelVector m_linkedModels;
  309. //! NULL if not appended to controller, otherwise connection info
  310. ControllerConnection* m_controllerConnection;
  311. ValueBuffer m_valueBuffer;
  312. long m_lastUpdatedPeriod;
  313. static long s_periodCounter;
  314. bool m_hasSampleExactData;
  315. // prevent several threads from attempting to write the same vb at the same time
  316. QMutex m_valueBufferMutex;
  317. signals:
  318. void initValueChanged( float val );
  319. void destroyed( jo_id_t id );
  320. } ;
  321. template <typename T> class LMMS_EXPORT TypedAutomatableModel : public AutomatableModel
  322. {
  323. public:
  324. using AutomatableModel::AutomatableModel;
  325. T value( int frameOffset = 0 ) const
  326. {
  327. return AutomatableModel::value<T>( frameOffset );
  328. }
  329. T initValue() const
  330. {
  331. return AutomatableModel::initValue<T>();
  332. }
  333. T minValue() const
  334. {
  335. return AutomatableModel::minValue<T>();
  336. }
  337. T maxValue() const
  338. {
  339. return AutomatableModel::maxValue<T>();
  340. }
  341. };
  342. // some typed AutomatableModel-definitions
  343. class LMMS_EXPORT FloatModel : public TypedAutomatableModel<float>
  344. {
  345. Q_OBJECT
  346. MODEL_IS_VISITABLE
  347. public:
  348. FloatModel( float val = 0, float min = 0, float max = 0, float step = 0,
  349. Model * parent = NULL,
  350. const QString& displayName = QString(),
  351. bool defaultConstructed = false ) :
  352. TypedAutomatableModel( val, min, max, step, parent, displayName, defaultConstructed )
  353. {
  354. }
  355. float getRoundedValue() const;
  356. int getDigitCount() const;
  357. QString displayValue( const float val ) const override;
  358. } ;
  359. class LMMS_EXPORT IntModel : public TypedAutomatableModel<int>
  360. {
  361. Q_OBJECT
  362. MODEL_IS_VISITABLE
  363. public:
  364. IntModel( int val = 0, int min = 0, int max = 0,
  365. Model* parent = NULL,
  366. const QString& displayName = QString(),
  367. bool defaultConstructed = false ) :
  368. TypedAutomatableModel( val, min, max, 1, parent, displayName, defaultConstructed )
  369. {
  370. }
  371. QString displayValue( const float val ) const override;
  372. } ;
  373. class LMMS_EXPORT BoolModel : public TypedAutomatableModel<bool>
  374. {
  375. Q_OBJECT
  376. MODEL_IS_VISITABLE
  377. public:
  378. BoolModel( const bool val = false,
  379. Model* parent = NULL,
  380. const QString& displayName = QString(),
  381. bool defaultConstructed = false ) :
  382. TypedAutomatableModel( val, false, true, 1, parent, displayName, defaultConstructed )
  383. {
  384. }
  385. QString displayValue( const float val ) const override;
  386. } ;
  387. typedef QMap<AutomatableModel*, float> AutomatedValueMap;
  388. #endif