ITAUPTrigger.h 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/*
 * ----------------------------------------------------------------
 *
 *		ITA core libs
 *		(c) Copyright Institute of Technical Acoustics (ITA)
 *		RWTH Aachen University, Germany, 2015-2017
 *
 * ----------------------------------------------------------------
 *				    ____  __________  _______
 *				   //  / //__   ___/ //  _   |
 *				  //  /    //  /    //  /_|  |
 *				 //  /    //  /    //  ___   |
 *				//__/    //__/    //__/   |__|
 *
 * ----------------------------------------------------------------
 *
 */

#ifndef INCLUDE_WATCHER_DSMBC_TRIGGER
#define INCLUDE_WATCHER_DSMBC_TRIGGER

#include <ITAConvolutionDefinitions.h>

#include <ITAUncopyable.h>

// Lightweight atomic trigger for synchronized filter exchanges
class ITAUPTrigger : public ITAUncopyable
{
public:
	inline ITAUPTrigger() : m_iState( 0 )
	{
	};

	// Returns the current state
	inline int getState() const
	{
		return m_iState;
	};

	// Pull the trigger. Update/alter the state.
	void trigger()
	{
		++m_iState;
	};

private:
	volatile int m_iState;
};

// Simple watcher class, realization update checks on a trigger
class ITAUPTriggerWatch
{
public:
	inline ITAUPTriggerWatch()
		: m_pTrigger( 0 )
		, bFirst( true )
		, m_iPrevTriggerState( 0 )
	{
	};

	inline const ITAUPTrigger* getWatchedTrigger() const
	{
		return m_pTrigger;
	};

	inline void setWatchedTrigger( const ITAUPTrigger* pNewTrigger )
	{
		m_pTrigger = pNewTrigger;
		bFirst = true;
	};

	// Logic: We always fire, if there is no trigger associated. This is needed in the convolver.
	inline bool fire()
	{
		if( !m_pTrigger )
			return true;

		int iNewTriggerState = m_pTrigger->getState();
		if( ( iNewTriggerState != m_iPrevTriggerState ) || bFirst )
		{
			m_iPrevTriggerState = iNewTriggerState;
			bFirst = false;
			return true;
		}

		return false;
	};

private:
	const ITAUPTrigger* m_pTrigger;
	bool bFirst;
	int m_iPrevTriggerState;
};

#endif // INCLUDE_WATCHER_DSMBC_TRIGGER