AutomatableModel.h 11 KB

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