123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- /*
- * AutomatableModel.h - declaration of class AutomatableModel
- *
- * Copyright (c) 2007-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
- *
- * This file is part of LMMS - https://lmms.io
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public
- * License along with this program (see COPYING); if not, write to the
- * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301 USA.
- *
- */
- #ifndef AUTOMATABLE_MODEL_H
- #define AUTOMATABLE_MODEL_H
- #include <QtCore/QMap>
- #include <QtCore/QMutex>
- #include "JournallingObject.h"
- #include "Model.h"
- #include "MidiTime.h"
- #include "ValueBuffer.h"
- #include "MemoryManager.h"
- // simple way to map a property of a view to a model
- #define mapPropertyFromModelPtr(type,getfunc,setfunc,modelname) \
- public: \
- type getfunc() const \
- { \
- return (type) modelname->value(); \
- } \
- public slots: \
- void setfunc( const type val ) \
- { \
- modelname->setValue( val ); \
- }
- #define mapPropertyFromModel(type,getfunc,setfunc,modelname) \
- public: \
- type getfunc() const \
- { \
- return (type) modelname.value(); \
- } \
- public slots: \
- void setfunc( const type val ) \
- { \
- modelname.setValue( (float) val ); \
- }
- class ControllerConnection;
- class LMMS_EXPORT AutomatableModel : public Model, public JournallingObject
- {
- Q_OBJECT
- MM_OPERATORS
- public:
- typedef QVector<AutomatableModel *> AutoModelVector;
- enum ScaleType
- {
- Linear,
- Logarithmic,
- Decibel
- };
- virtual ~AutomatableModel();
- bool isAutomated() const;
- bool isAutomatedOrControlled() const
- {
- return isAutomated() || m_controllerConnection != NULL;
- }
- ControllerConnection* controllerConnection() const
- {
- return m_controllerConnection;
- }
- void setControllerConnection( ControllerConnection* c );
- template<class T>
- static T castValue( const float v )
- {
- return (T)( v );
- }
- template<bool>
- static bool castValue( const float v )
- {
- return ( qRound( v ) != 0 );
- }
- template<class T>
- inline T value( int frameOffset = 0 ) const
- {
- if( unlikely( hasLinkedModels() || m_controllerConnection != NULL ) )
- {
- return castValue<T>( controllerValue( frameOffset ) );
- }
- return castValue<T>( m_value );
- }
- float controllerValue( int frameOffset ) const;
- //! @brief Function that returns sample-exact data as a ValueBuffer
- //! @return pointer to model's valueBuffer when s.ex.data exists, NULL otherwise
- ValueBuffer * valueBuffer();
- template<class T>
- T initValue() const
- {
- return castValue<T>( m_initValue );
- }
- bool isAtInitValue() const
- {
- return m_value == m_initValue;
- }
- template<class T>
- T minValue() const
- {
- return castValue<T>( m_minValue );
- }
- template<class T>
- T maxValue() const
- {
- return castValue<T>( m_maxValue );
- }
- template<class T>
- T step() const
- {
- return castValue<T>( m_step );
- }
- //! @brief Returns value scaled with the scale type and min/max values of this model
- float scaledValue( float value ) const;
- //! @brief Returns value applied with the inverse of this model's scale type
- float inverseScaledValue( float value ) const;
- void setInitValue( const float value );
- void setAutomatedValue( const float value );
- void setValue( const float value );
- void incValue( int steps )
- {
- setValue( m_value + steps * m_step );
- }
- float range() const
- {
- return m_range;
- }
- void setRange( const float min, const float max, const float step = 1 );
- void setScaleType( ScaleType sc ) {
- m_scaleType = sc;
- }
- void setScaleLogarithmic( bool setToTrue = true )
- {
- setScaleType( setToTrue ? Logarithmic : Linear );
- }
- bool isScaleLogarithmic() const
- {
- return m_scaleType == Logarithmic;
- }
- void setStep( const float step );
- float centerValue() const
- {
- return m_centerValue;
- }
- void setCenterValue( const float centerVal )
- {
- m_centerValue = centerVal;
- }
- static void linkModels( AutomatableModel* m1, AutomatableModel* m2 );
- static void unlinkModels( AutomatableModel* m1, AutomatableModel* m2 );
- void unlinkAllModels();
- /**
- * @brief Saves settings (value, automation links and controller connections) of AutomatableModel into
- * specified DOM element using <name> as attribute/node name
- * @param doc TODO
- * @param element Where this option shall be saved.
- * Depending on the model, this can be done in an attribute or in a subnode.
- * @param name Name to store this model as.
- */
- virtual void saveSettings( QDomDocument& doc, QDomElement& element, const QString& name );
- /*! \brief Loads settings (value, automation links and controller connections) of AutomatableModel from
- specified DOM element using <name> as attribute/node name */
- virtual void loadSettings( const QDomElement& element, const QString& name );
- virtual QString nodeName() const
- {
- return "automatablemodel";
- }
- virtual QString displayValue( const float val ) const = 0;
- bool hasLinkedModels() const
- {
- return !m_linkedModels.empty();
- }
- // a way to track changed values in the model and avoid using signals/slots - useful for speed-critical code.
- // note that this method should only be called once per period since it resets the state of the variable - so if your model
- // has to be accessed by more than one object, then this function shouldn't be used.
- bool isValueChanged()
- {
- if( m_valueChanged || valueBuffer() )
- {
- m_valueChanged = false;
- return true;
- }
- return false;
- }
- float globalAutomationValueAt( const MidiTime& time );
- void setStrictStepSize( const bool b )
- {
- m_hasStrictStepSize = b;
- }
- static void incrementPeriodCounter()
- {
- ++s_periodCounter;
- }
- static void resetPeriodCounter()
- {
- s_periodCounter = 0;
- }
- public slots:
- virtual void reset();
- void unlinkControllerConnection();
- protected:
- AutomatableModel(
- const float val = 0,
- const float min = 0,
- const float max = 0,
- const float step = 0,
- Model* parent = NULL,
- const QString& displayName = QString(),
- bool defaultConstructed = false );
- //! returns a value which is in range between min() and
- //! max() and aligned according to the step size (step size 0.05 -> value
- //! 0.12345 becomes 0.10 etc.). You should always call it at the end after
- //! doing your own calculations.
- float fittedValue( float value ) const;
- private:
- virtual void saveSettings( QDomDocument& doc, QDomElement& element )
- {
- saveSettings( doc, element, "value" );
- }
- virtual void loadSettings( const QDomElement& element )
- {
- loadSettings( element, "value" );
- }
- void linkModel( AutomatableModel* model );
- void unlinkModel( AutomatableModel* model );
- //! @brief Scales @value from linear to logarithmic.
- //! Value should be within [0,1]
- template<class T> T logToLinearScale( T value ) const;
- //! rounds @a value to @a where if it is close to it
- //! @param value will be modified to rounded value
- template<class T> void roundAt( T &value, const T &where ) const;
- ScaleType m_scaleType; //! scale type, linear by default
- float m_value;
- float m_initValue;
- float m_minValue;
- float m_maxValue;
- float m_step;
- float m_range;
- float m_centerValue;
- bool m_valueChanged;
- // currently unused?
- float m_oldValue;
- int m_setValueDepth;
- // used to determine if step size should be applied strictly (ie. always)
- // or only when value set from gui (default)
- bool m_hasStrictStepSize;
- AutoModelVector m_linkedModels;
- //! NULL if not appended to controller, otherwise connection info
- ControllerConnection* m_controllerConnection;
- ValueBuffer m_valueBuffer;
- long m_lastUpdatedPeriod;
- static long s_periodCounter;
- bool m_hasSampleExactData;
- // prevent several threads from attempting to write the same vb at the same time
- QMutex m_valueBufferMutex;
- signals:
- void initValueChanged( float val );
- void destroyed( jo_id_t id );
- } ;
- template <typename T> class LMMS_EXPORT TypedAutomatableModel : public AutomatableModel
- {
- public:
- using AutomatableModel::AutomatableModel;
- T value( int frameOffset = 0 ) const
- {
- return AutomatableModel::value<T>( frameOffset );
- }
- T initValue() const
- {
- return AutomatableModel::initValue<T>();
- }
- T minValue() const
- {
- return AutomatableModel::minValue<T>();
- }
- T maxValue() const
- {
- return AutomatableModel::maxValue<T>();
- }
- };
- // some typed AutomatableModel-definitions
- class LMMS_EXPORT FloatModel : public TypedAutomatableModel<float>
- {
- Q_OBJECT
- public:
- FloatModel( float val = 0, float min = 0, float max = 0, float step = 0,
- Model * parent = NULL,
- const QString& displayName = QString(),
- bool defaultConstructed = false ) :
- TypedAutomatableModel( val, min, max, step, parent, displayName, defaultConstructed )
- {
- }
- float getRoundedValue() const;
- int getDigitCount() const;
- QString displayValue( const float val ) const override;
- } ;
- class LMMS_EXPORT IntModel : public TypedAutomatableModel<int>
- {
- Q_OBJECT
- public:
- IntModel( int val = 0, int min = 0, int max = 0,
- Model* parent = NULL,
- const QString& displayName = QString(),
- bool defaultConstructed = false ) :
- TypedAutomatableModel( val, min, max, 1, parent, displayName, defaultConstructed )
- {
- }
- QString displayValue( const float val ) const override;
- } ;
- class LMMS_EXPORT BoolModel : public TypedAutomatableModel<bool>
- {
- Q_OBJECT
- public:
- BoolModel( const bool val = false,
- Model* parent = NULL,
- const QString& displayName = QString(),
- bool defaultConstructed = false ) :
- TypedAutomatableModel( val, false, true, 1, parent, displayName, defaultConstructed )
- {
- }
- QString displayValue( const float val ) const override;
- } ;
- typedef QMap<AutomatableModel*, float> AutomatedValueMap;
- #endif
|