VAAirTrafficNoiseAudioRenderer.cpp 64.4 KB
Newer Older
1 2 3 4 5 6
/*
 *  --------------------------------------------------------------------------------------------
 *
 *    VVV        VVV A           Virtual Acoustics (VA) | http://www.virtualacoustics.org
 *     VVV      VVV AAA          Licensed under the Apache License, Version 2.0
 *      VVV    VVV   AAA
7
 *       VVV  VVV     AAA        Copyright 2015-2018
8 9 10 11 12 13
 *        VVVVVV       AAA       Institute of Technical Acoustics (ITA)
 *         VVVV         AAA      RWTH Aachen University
 *
 *  --------------------------------------------------------------------------------------------
 */

Jonas Stienen's avatar
Jonas Stienen committed
14 15
#include "VAAirTrafficNoiseAudioRenderer.h"

16
#ifdef VACORE_WITH_RENDERER_BINAURAL_AIR_TRAFFIC_NOISE
Jonas Stienen's avatar
Jonas Stienen committed
17 18 19 20

#include "../../../Filtering/VATemporalVariations.h"

// VA includes
21
#include <VA.h>
Jonas Stienen's avatar
Jonas Stienen committed
22 23
#include <VAObjectPool.h>
#include <VAReferenceableObject.h>
24 25
#include "../../../directivities/VADirectivityDAFFEnergetic.h"
#include "../../../directivities/VADirectivityDAFFHRIR.h"
Jonas Stienen's avatar
Jonas Stienen committed
26 27 28 29 30 31 32 33 34 35 36 37 38
#include "../../../Filtering/VAAtmosphere.h"
#include "../../../Motion/VAMotionModelBase.h"
#include "../../../Motion/VASharedMotionModel.h"
#include "../../../Motion/VASampleAndHoldMotionModel.h"
#include "../../../Scene/VAScene.h"
#include "../../../Utils/VAUtils.h"
#include "../../../VAAudiostreamTracker.h"
#include "../../../VACoreImpl.h"
#include "../../../VACoreConfig.h"
#include "../../../VALog.h"

// ITA includes
#include <DAFF.h>
39 40 41
#include <ITAUPConvolution.h>
#include <ITAUPFilter.h>
#include <ITAUPFilterPool.h>
Jonas Stienen's avatar
Jonas Stienen committed
42
#include <ITAAtomicPrimitives.h>
43
#include <ITAThirdOctaveFilterbank.h>
Jonas Stienen's avatar
Jonas Stienen committed
44 45 46
#include <ITAClock.h>
#include <ITACriticalSection.h>
//#include <ITAConfigUtils.h>
47
#include <ITADataSourceRealization.h>
Jonas Stienen's avatar
Jonas Stienen committed
48
#include <ITASampleBuffer.h>
Jonas Stienen's avatar
Jonas Stienen committed
49
#include <ITAStopWatch.h>
Jonas Stienen's avatar
Jonas Stienen committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <ITASampleFrame.h>
#include <ITAStreamInfo.h>
#include <ITAFastMath.h>

// Vista includes
#include <VistaInterProcComm/Concurrency/VistaThreadEvent.h>

// STL includes
#include <assert.h>
#include <fstream>
#include <iomanip>

// 3rdParty includes
#include <tbb/concurrent_queue.h>

65
class CVABATNSoundPath : public CVAPoolObject
Jonas Stienen's avatar
Jonas Stienen committed
66 67 68 69 70 71 72 73
{
public:
	virtual ~CVABATNSoundPath();

	//! Retarded metrics of sound path
	class Metrics
	{
	public:
74
		double dRetardedDistance;		  //!< Metrical distance to retarded sound position
75 76
		VAQuat qAngleRetSourceToListener; //!< Immediate angle of incidence to retarded source position in listener reference frame in YPR convention
		VAQuat qAngleListenerToRetSource; //!< Retarded angle of incidence to listener in source reference frame in YPR convention
Jonas Stienen's avatar
Jonas Stienen committed
77 78 79
	};

	//! State of directivity
80
	class CSoundSourceDirectivityState
Jonas Stienen's avatar
Jonas Stienen committed
81 82
	{
	public:
83 84 85 86 87
		inline CSoundSourceDirectivityState()
			: pData( NULL )
			, iRecord( -1 )
			, bDirectivityEnabled( true ) {};

Jonas Stienen's avatar
Jonas Stienen committed
88 89 90 91
		IVADirectivity* pData;	//!< Directivity data, may be NULL
		int iRecord;			//!< Directivity index
		bool bDirectivityEnabled;

92
		inline bool operator==( const CSoundSourceDirectivityState& rhs ) const
Jonas Stienen's avatar
Jonas Stienen committed
93 94 95 96 97 98
		{
			bool bBothEnabled = ( bDirectivityEnabled == rhs.bDirectivityEnabled );
			bool bSameRecordIndex = ( iRecord == rhs.iRecord );
			bool bSameData = ( pData == rhs.pData );

			return ( bBothEnabled && bSameRecordIndex && bSameData );
99
		};
Jonas Stienen's avatar
Jonas Stienen committed
100 101
	};

102
	class CSoundReceiverDirectivityState
Jonas Stienen's avatar
Jonas Stienen committed
103 104
	{
	public:
105 106 107 108
		inline CSoundReceiverDirectivityState()
			: pData( NULL )
			, iRecord( -1 )
			, fDistance( 1.0f ) {};
Jonas Stienen's avatar
Jonas Stienen committed
109

110
		IVADirectivity* pData;	 //!< HRIR data, may be NULL
Jonas Stienen's avatar
Jonas Stienen committed
111 112 113
		int iRecord;			 //!< HRIR index
		float fDistance;		 //!< HRIR dataset distance

114
		inline bool operator!=( const CSoundReceiverDirectivityState& rhs ) const
Jonas Stienen's avatar
Jonas Stienen committed
115 116
		{
			if( pData != rhs.pData ) return true;
117
			if( fDistance != rhs.fDistance ) return true;
Jonas Stienen's avatar
Jonas Stienen committed
118 119
			if( iRecord != rhs.iRecord ) return true;
			return false;
120
		};
Jonas Stienen's avatar
Jonas Stienen committed
121 122
	};

123 124
	CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSource* pSoundSource;		//!< Verknpfte Quelle
	CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSoundReceiver* pSoundReceiver;	//!< Verknpfter Hrer
Jonas Stienen's avatar
Jonas Stienen committed
125 126 127

	struct PropPath
	{
128
		CVASourceReceiverMetrics oRelations; //!< Informationen ber die Relation von Quelle und Hrer (Position & Orientierung)
Jonas Stienen's avatar
Jonas Stienen committed
129

130 131 132 133 134 135
		double dPropagationTime; //!< Time that the sound waves took to be propagated to the receiver
		double dGeometricalSpreadingLoss; //!< Spherical / geometrical spreading loss (usually 1-by-R distance law)

		CITAVariableDelayLine* pVariableDelayLine;	//!< DSP-Element zur Bercksichtigung der Mediumsausbreitung in instationren Schallfeldern (inkl. Doppler-Effekt, falls aktiviert)


136 137 138 139
		CSoundSourceDirectivityState oDirectivityStateCur;	//!< Aktueller Status der Richtcharakteristik
		CSoundSourceDirectivityState oDirectivityStateNew;	//!< Neuer Status der Richtcharakteristik
		CITAThirdOctaveGainMagnitudeSpectrum oSoundSourceDirectivityMagnitudes; //!< Magnitudes for directivity

Jonas Stienen's avatar
Jonas Stienen committed
140
		CVAAtmosphericTemporalVariantions oATVGenerator;	//!< Generator for temporal variation magnitudes of the time signal by medium shifts during propagation
141
		CITAThirdOctaveGainMagnitudeSpectrum oTemporalVariationMagnitudes;		//!< Magnitudes for temporal variation of the time signal by medium shifts during propagation
Jonas Stienen's avatar
Jonas Stienen committed
142

143
		CITAThirdOctaveGainMagnitudeSpectrum oAirAttenuationMagnitudes; //!< Magnitudes for air attenuation (damping)
Jonas Stienen's avatar
Jonas Stienen committed
144

145 146
		CITAThirdOctaveGainMagnitudeSpectrum oGroundReflectionMagnitudes; //!< Ground reflection parameters (only for reflection paths)

147
		CITAThirdOctaveFilterbank* pThirdOctaveFilterBank; //!< DSP-Element zur Filterung von Third-Octave Datenstzen der Richtcharakteristik
148
		CITAThirdOctaveGainMagnitudeSpectrum oThirdOctaveFilterMagnitudes; //!< DSP magnitudes (third octave band resolution)
Jonas Stienen's avatar
Jonas Stienen committed
149

150 151
		CSoundReceiverDirectivityState oSoundReceiverDirectivityStateCur;	//!< Aktueller Status der HRIR
		CSoundReceiverDirectivityState oSoundReceiverDirectivityStateNew;	//!< Neuer Status der HRIR
Jonas Stienen's avatar
Jonas Stienen committed
152

153 154
		ITAUPConvolution* pFIRConvolverChL;	//!< DSP-Element zur Faltung mit der Kopfbezogenen bertragungsfunktion im Zeitbereich (HRIR) - Links
		ITAUPConvolution* pFIRConvolverChR;	//!< DSP-Element zur Faltung mit der Kopfbezogenen bertragungsfunktion im Zeitbereich (HRIR) - Rechts
155 156 157

		inline PropPath()
			: dPropagationTime( 0.0f )
158
			, dGeometricalSpreadingLoss( 1.0f )
159 160 161 162 163 164 165
			, pVariableDelayLine( NULL )
			, pThirdOctaveFilterBank( NULL )
			, pFIRConvolverChL( NULL )
			, pFIRConvolverChR( NULL )
		{
			oGroundReflectionMagnitudes.SetIdentity();
		};
Jonas Stienen's avatar
Jonas Stienen committed
166 167 168 169 170 171 172 173 174
	};

	PropPath oDirSoundPath; //!< Direct sound propagation path
	PropPath oRefSoundPath; //!< Propagation path including ground reflection

	double dGroundReflectionPlanePosition; //!< Height of ground, defaults to 0

	ITAAtomicBool bDelete; //!< Schallpfad zur Lschung markiert?

175 176 177 178
	inline void PreRequest()
	{
		pSoundSource = nullptr;
		pSoundReceiver = nullptr;
Jonas Stienen's avatar
Jonas Stienen committed
179 180 181 182 183 184 185 186 187 188 189 190

		// Reset DSP elements
		oDirSoundPath.pThirdOctaveFilterBank->Clear();
		oRefSoundPath.pThirdOctaveFilterBank->Clear();
		oDirSoundPath.pVariableDelayLine->Clear();
		oRefSoundPath.pVariableDelayLine->Clear();
		oDirSoundPath.pFIRConvolverChL->clear();
		oRefSoundPath.pFIRConvolverChL->clear();
		oDirSoundPath.pFIRConvolverChR->clear();
		oRefSoundPath.pFIRConvolverChR->clear();
	};

191

Jonas Stienen's avatar
Jonas Stienen committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	//! Bestimmt die relativen Gren des Pfades
	/**
		* Diese berechneten Gren dienen als Grundlage zur Bestimmung der ausgewhlten
		* Datenstze und Einstellungen der DSP-Elemente. Ein weiteres Update der einzelnen
		* DSP-Elemente fhrt z.B. zum Filteraustausch, wenn die Statusnderung Auswirkungen hat
		* (tatschlich ein neuer Datensatz geholt werden muss).
		*
		* Diese Methode ist besonders leichtgewichtig, da sie im StreamProcess genutzt wird.
		*
		* // spter -> \return Gibt false zurck, falls die retardierten Werte noch nicht zur Verfgung stehen.
		*/
	void UpdateMetrics();

	//! Aktualisiert die einzelnen Komponenten des Schallpfades
	void UpdateEntities( int iEffectiveAuralisationMode );

	//! Aktualisiert die Richtcharakteristik auf dem Pfad
209
	void UpdateSoundSourceDirectivity();
Jonas Stienen's avatar
Jonas Stienen committed
210 211 212 213 214

	//! Aktualisiert die Mediumsausbreitung auf dem Pfad
	void UpdateMediumPropagation( double dSpeedOfSound );

	//! Updates the temporal variations model of medium shift fluctuation
215
	void UpdateTemporalVariation();
Jonas Stienen's avatar
Jonas Stienen committed
216 217

	//! Updates the air attenuation (damping)
218
	void UpdateAirAttenuation( const double dTemperatur = 20.0f, const double  dPressure = 101.325e3, const double dHumidity = 60.0f );
Jonas Stienen's avatar
Jonas Stienen committed
219 220

	//! Aktualisiert den Lautstrkeabfall nach dem 1/r-Gesetzt (Inverse Distance Decrease/Law)
221
	void CalculateInverseDistanceDecrease();
Jonas Stienen's avatar
Jonas Stienen committed
222 223

	//! Aktualisiert die HRIR auf dem Pfad
224
	void UpdateSoundReceiverDirectivity();
Jonas Stienen's avatar
Jonas Stienen committed
225 226 227 228 229 230 231 232 233 234

	//! Aktiviert/Deaktiviert Doppler-Verschiebung
	void SetDopplerShiftsEnabled( bool bEnabled );

private:

	//! Standard-Konstruktor deaktivieren
	CVABATNSoundPath();

	//! Konstruktor
235
	CVABATNSoundPath( double dSamplerate, int iBlocklength, int iHRIRFilterLength, int iDirFilterLength );
Jonas Stienen's avatar
Jonas Stienen committed
236 237 238 239 240 241 242 243

	ITASampleFrame m_sfHRIRTemp;		 //!< Intern verwendeter Zwischenspeicher fr HRIR Datentze
	int m_iDefaultVDLSwitchingAlgorithm; //!< Umsetzung der Verzgerungsnderung


	friend class CVABATNSoundPathFactory;
};

244 245 246
// Helper for external simulation
void ExternalSimulationUpdate( CVABATNSoundPath::PropPath&, const CVAStruct& );

Jonas Stienen's avatar
Jonas Stienen committed
247 248
class CVABATNSoundPathFactory : public IVAPoolObjectFactory
{
249
public:
Jonas Stienen's avatar
Jonas Stienen committed
250

251 252 253 254 255 256
	inline CVABATNSoundPathFactory( double dSamplerate, int iBlocklength, int iHRIRFilterLength, int iDirFilterLength )
		: m_dSamplerate( dSamplerate )
		, m_iBlocklength( iBlocklength )
		, m_iHRIRFilterLength( iHRIRFilterLength )
		, m_iDirFilterLength( iDirFilterLength )
	{};
Jonas Stienen's avatar
Jonas Stienen committed
257

258
	inline CVAPoolObject* CreatePoolObject()
Jonas Stienen's avatar
Jonas Stienen committed
259 260
	{
		return new CVABATNSoundPath( m_dSamplerate, m_iBlocklength, m_iHRIRFilterLength, m_iDirFilterLength );
261
	};
Jonas Stienen's avatar
Jonas Stienen committed
262 263 264 265 266 267 268 269

private:
	double m_dSamplerate;		//!< Abtastrate
	int m_iBlocklength;			//!< Blocklnge
	int m_iHRIRFilterLength;	//!< Filterlnge der HRIR
	int m_iDirFilterLength;		//!< Filterlnge der Richtcharakteristik
};

270
class CVABATNSoundReceiverPoolFactory : public IVAPoolObjectFactory
Jonas Stienen's avatar
Jonas Stienen committed
271 272
{
public:
273 274
	inline CVABATNSoundReceiverPoolFactory( CVACoreImpl* pCore, const CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSoundReceiver::Config& oConf )
		: m_pCore( pCore ), m_oListenerConf( oConf )
Jonas Stienen's avatar
Jonas Stienen committed
275 276 277
	{
	};

278
	inline CVAPoolObject* CreatePoolObject()
Jonas Stienen's avatar
Jonas Stienen committed
279
	{
280 281
		CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSoundReceiver * pListener;
		pListener = new CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSoundReceiver( m_pCore, m_oListenerConf );
Jonas Stienen's avatar
Jonas Stienen committed
282 283 284 285 286
		return pListener;
	};

private:
	CVACoreImpl* m_pCore;
287
	const CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSoundReceiver::Config& m_oListenerConf;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
288 289 290

	//! Not for use, avoid C4512
	inline CVABATNSoundReceiverPoolFactory operator=( const CVABATNSoundReceiverPoolFactory & ) { VA_EXCEPT_NOT_IMPLEMENTED; };
Jonas Stienen's avatar
Jonas Stienen committed
291 292 293 294 295
};

class CVABATNSourcePoolFactory : public IVAPoolObjectFactory
{
public:
296
	inline CVABATNSourcePoolFactory( const CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSource::Config& oConf )
Jonas Stienen's avatar
Jonas Stienen committed
297 298 299 300
		: m_oSourceConf( oConf )
	{
	};

301
	inline CVAPoolObject* CreatePoolObject()
Jonas Stienen's avatar
Jonas Stienen committed
302 303 304 305 306 307 308 309
	{
		CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSource * pSource;
		pSource = new CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSource( m_oSourceConf );
		return pSource;
	};

private:
	const CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSource::Config& m_oSourceConf;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
310 311 312

	//! Not for use, avoid C4512
	inline CVABATNSourcePoolFactory operator=( const CVABATNSourcePoolFactory & ) { VA_EXCEPT_NOT_IMPLEMENTED; };
Jonas Stienen's avatar
Jonas Stienen committed
313 314 315 316 317
};

// Renderer

CVABinauralAirTrafficNoiseAudioRenderer::CVABinauralAirTrafficNoiseAudioRenderer( const CVAAudioRendererInitParams& oParams )
318 319 320 321 322
	: ITADatasourceRealization( 2, oParams.pCore->GetCoreConfig()->oAudioDriverConfig.dSampleRate, oParams.pCore->GetCoreConfig()->oAudioDriverConfig.iBuffersize )
	, m_pCore( oParams.pCore )
	, m_pCurSceneState( nullptr )
	, m_iHRIRFilterLength( -1 )
	, m_oParams( oParams )
Jonas Stienen's avatar
Jonas Stienen committed
323 324 325
{
	Init( *oParams.pConfig );

326
	IVAPoolObjectFactory* pListenerFactory = new CVABATNSoundReceiverPoolFactory( m_pCore, m_oDefaultListenerConf );
Jonas Stienen's avatar
Jonas Stienen committed
327 328 329 330 331
	m_pListenerPool = IVAObjectPool::Create( 16, 2, pListenerFactory, true );

	IVAPoolObjectFactory* pSourceFactory = new CVABATNSourcePoolFactory( m_oDefaultSourceConf );
	m_pSourcePool = IVAObjectPool::Create( 16, 2, pSourceFactory, true );

332
	m_pSoundPathFactory = new CVABATNSoundPathFactory( GetSampleRate(), GetBlocklength(), m_iHRIRFilterLength, 128 );
Jonas Stienen's avatar
Jonas Stienen committed
333 334 335

	m_pSoundPathPool = IVAObjectPool::Create( 64, 8, m_pSoundPathFactory, true );

336
	m_pUpdateMessagePool = IVAObjectPool::Create( 2, 1, new CVAPoolObjectDefaultFactory<CVABATNUpdateMessage>, true );
Jonas Stienen's avatar
Jonas Stienen committed
337 338 339 340 341 342

	ctxAudio.m_sbTempBufD.Init( GetBlocklength(), true );
	ctxAudio.m_sbTempBufR.Init( GetBlocklength(), true );
	ctxAudio.m_iResetFlag = 0; // Normal operation mode
	ctxAudio.m_iStatus = 0; // Stopped

343
	m_iCurGlobalAuralizationMode = IVAInterface::VA_AURAMODE_DEFAULT;
Jonas Stienen's avatar
Jonas Stienen committed
344 345
}

346
CVABinauralAirTrafficNoiseAudioRenderer::~CVABinauralAirTrafficNoiseAudioRenderer()
Jonas Stienen's avatar
Jonas Stienen committed
347 348 349 350 351 352 353 354 355 356 357
{
	delete m_pSoundPathPool;
	delete m_pUpdateMessagePool;
}

void CVABinauralAirTrafficNoiseAudioRenderer::Init( const CVAStruct& oArgs )
{
	CVAConfigInterpreter conf( oArgs );

	conf.OptNumber( "GroundPlanePosition", m_dGroundPlanePosition, 0.0f );

358 359 360 361 362 363 364 365
	conf.OptBool( "PropagationDelayExternalSimulation", m_bPropagationDelayExternalSimulation, false );
	conf.OptBool( "GroundReflectionExternalSimulation", m_bGroundReflectionExternalSimulation, false );
	conf.OptBool( "DirectivityExternalSimulation", m_bDirectivityExternalSimulation, false );
	conf.OptBool( "AirAbsorptionExternalSimulation", m_bAirAbsorptionExternalSimulation, false );
	conf.OptBool( "SpreadingLossExternalSimulation", m_bSpreadingLossExternalSimulation, false );
	conf.OptBool( "TemporalVariationsExternalSimulation", m_bTemporalVariationsExternalSimulation, false );


Jonas Stienen's avatar
Jonas Stienen committed
366
	std::string sVLDInterpolationAlgorithm;
367
	conf.OptString( "SwitchingAlgorithm", sVLDInterpolationAlgorithm, "linear" );
Jonas Stienen's avatar
Jonas Stienen committed
368 369 370
	sVLDInterpolationAlgorithm = toLowercase( sVLDInterpolationAlgorithm );

	if( sVLDInterpolationAlgorithm == "switch" )
371
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::SWITCH;
Jonas Stienen's avatar
Jonas Stienen committed
372
	else if( sVLDInterpolationAlgorithm == "crossfade" )
373
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::CROSSFADE;
Jonas Stienen's avatar
Jonas Stienen committed
374
	else if( sVLDInterpolationAlgorithm == "linear" )
375
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::LINEAR_INTERPOLATION;
Jonas Stienen's avatar
Jonas Stienen committed
376
	else if( sVLDInterpolationAlgorithm == "cubicspline" )
377
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::CUBIC_SPLINE_INTERPOLATION;
Jonas Stienen's avatar
Jonas Stienen committed
378
	else if( sVLDInterpolationAlgorithm == "windowedsinc" )
379
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::WINDOWED_SINC_INTERPOLATION;
Jonas Stienen's avatar
Jonas Stienen committed
380 381 382
	else
		ITA_EXCEPT1( INVALID_PARAMETER, "Unrecognized interpolation algorithm '" + sVLDInterpolationAlgorithm + "' in BinauralFreefieldAudioRendererConfig" );

383
	conf.OptInteger( "FIRFilterLength", m_iHRIRFilterLength, 256 );
Jonas Stienen's avatar
Jonas Stienen committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

	// Motion model Listener
	conf.OptInteger( "MotionModelNumHistoryKeys", m_oDefaultListenerConf.iMotionModelNumHistoryKeys, 1000 );

	if( m_oDefaultListenerConf.iMotionModelNumHistoryKeys < 1 )
		VA_EXCEPT2( INVALID_PARAMETER, "Basic motion model history needs to be greater than zero" );

	conf.OptNumber( "MotionModelWindowSize", m_oDefaultListenerConf.dMotionModelWindowSize, 0.1f );
	conf.OptNumber( "MotionModelWindowDelay", m_oDefaultListenerConf.dMotionModelWindowDelay, 0.1f );

	if( ( m_oDefaultListenerConf.dMotionModelWindowSize <= 0 ) || ( m_oDefaultListenerConf.dMotionModelWindowDelay < 0 ) )
		VA_EXCEPT2( INVALID_PARAMETER, "Basic motion model window parameters parse error (zero or negative?)" );

	// Motion model Source
	conf.OptInteger( "MotionModelNumHistoryKeys", m_oDefaultSourceConf.iMotionModelNumHistoryKeys, 1000 );

	if( m_oDefaultSourceConf.iMotionModelNumHistoryKeys < 1 )
		VA_EXCEPT2( INVALID_PARAMETER, "Basic motion model history needs to be greater than zero" );

	conf.OptNumber( "MotionModelWindowSize", m_oDefaultSourceConf.dMotionModelWindowSize, 1.0f );
	conf.OptNumber( "MotionModelWindowDelay", m_oDefaultSourceConf.dMotionModelWindowDelay, 0.5f );

	return;
}

409
void CVABinauralAirTrafficNoiseAudioRenderer::Reset()
Jonas Stienen's avatar
Jonas Stienen committed
410 411 412
{
	ctxAudio.m_iResetFlag = 1; // Request reset

413
	if( ctxAudio.m_iStatus == 0 || m_oParams.bOfflineRendering )
Jonas Stienen's avatar
Jonas Stienen committed
414 415 416 417 418 419 420 421 422 423 424
	{
		// if no streaming active, reset manually
		//SyncInternalData();
		ResetInternalData();
	}

	// Wait for last streaming block before internal reset
	while( ctxAudio.m_iResetFlag != 2 )
	{
		VASleep( 10 ); // Wait for acknowledge
	}
425

Jonas Stienen's avatar
Jonas Stienen committed
426 427 428 429 430
	// Iterate over sound pathes and free items
	std::list< CVABATNSoundPath* >::iterator it = m_lSoundPaths.begin();
	while( it != m_lSoundPaths.end() )
	{
		CVABATNSoundPath* pPath = *it;
431

Jonas Stienen's avatar
Jonas Stienen committed
432 433 434 435 436 437 438 439
		int iNumRefs = pPath->GetNumReferences();
		assert( iNumRefs == 1 );
		pPath->RemoveReference();
		++it;
	}
	m_lSoundPaths.clear();

	// Iterate over listener and free items
440
	std::map< int, CVABATNSoundReceiver* >::const_iterator lcit = m_mListeners.begin();
Jonas Stienen's avatar
Jonas Stienen committed
441 442
	while( lcit != m_mListeners.end() )
	{
443
		CVABATNSoundReceiver* pListener( lcit->second );
Jonas Stienen's avatar
Jonas Stienen committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
		pListener->pData->RemoveReference();
		assert( pListener->GetNumReferences() == 1 );
		pListener->RemoveReference();
		lcit++;
	}
	m_mListeners.clear();

	// Iterate over sources and free items
	std::map< int, CVABATNSource* >::const_iterator scit = m_mSources.begin();
	while( scit != m_mSources.end() )
	{
		CVABATNSource* pSource( scit->second );
		pSource->pData->RemoveReference();
		assert( pSource->GetNumReferences() == 1 );
		pSource->RemoveReference();
		scit++;
	}
	m_mSources.clear();

	// Scene frei geben
	if( m_pCurSceneState )
	{
		m_pCurSceneState->RemoveReference();
		m_pCurSceneState = nullptr;
	}

	ctxAudio.m_iResetFlag = 0; // Enter normal mode
}

void CVABinauralAirTrafficNoiseAudioRenderer::UpdateScene( CVASceneState* pNewSceneState )
474
{
Jonas Stienen's avatar
Jonas Stienen committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	assert( pNewSceneState );

	m_pNewSceneState = pNewSceneState;
	if( m_pNewSceneState == m_pCurSceneState )
		return;

	// Neue Szene referenzieren (gegen Freigabe sperren)
	m_pNewSceneState->AddReference();

	// Unterschiede ermitteln: Neue Szene vs. alte Szene
	CVASceneStateDiff oDiff;
	pNewSceneState->Diff( m_pCurSceneState, &oDiff );

	// Leere Update-Nachricht zusammenstellen
	m_pUpdateMessage = dynamic_cast< CVABATNUpdateMessage* >( m_pUpdateMessagePool->RequestObject() );

	// Quellen, Hrer und Pfade verwalten
	ManageSoundPaths( m_pCurSceneState, pNewSceneState, &oDiff );
493

Jonas Stienen's avatar
Jonas Stienen committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	// Bewegungsinformationen der Quellen und Hrer aktualisieren
	UpdateTrajectories();

	// Entitten der Schallpfade aktualisieren
	UpdateSoundPaths();

	// Update-Nachricht an den Audiokontext schicken
	ctxAudio.m_qpUpdateMessages.push( m_pUpdateMessage );

	// Alte Szene freigeben (dereferenzieren)
	if( m_pCurSceneState ) m_pCurSceneState->RemoveReference();
	m_pCurSceneState = m_pNewSceneState;
	m_pNewSceneState = nullptr;
}

ITADatasource* CVABinauralAirTrafficNoiseAudioRenderer::GetOutputDatasource()
{
	return this;
}

514
void CVABinauralAirTrafficNoiseAudioRenderer::ManageSoundPaths( const CVASceneState* pCurScene, const CVASceneState* pNewScene, const CVASceneStateDiff* pDiff )
Jonas Stienen's avatar
Jonas Stienen committed
515 516 517 518 519 520
{
	// Iterate over current paths and mark deleted (will be removed within internal sync of audio context thread)
	std::list< CVABATNSoundPath* >::iterator itp = m_lSoundPaths.begin();
	while( itp != m_lSoundPaths.end() )
	{
		CVABATNSoundPath* pPath( *itp );
521 522
		int iSourceID = pPath->pSoundSource->pData->iID;
		int iListenerID = pPath->pSoundReceiver->pData->iID;
Jonas Stienen's avatar
Jonas Stienen committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
		bool bDeletetionRequired = false;

		// Source deleted?
		std::vector< int >::const_iterator cits = pDiff->viDelSoundSourceIDs.begin();
		while( cits != pDiff->viDelSoundSourceIDs.end() )
		{
			const int& iIDDeletedSource( *cits++ );

			// Remove only required, if not skipped by this renderer
			bool bSkippedByThisRenderer = false;
			const CVASoundSourceDesc* pSoundSourceDesc = m_pCore->GetSceneManager()->GetSoundSourceDesc( iIDDeletedSource );
			if( !pSoundSourceDesc->sExplicitRendererID.empty() && pSoundSourceDesc->sExplicitRendererID != m_oParams.sID )
				bSkippedByThisRenderer = true;

			if( iSourceID == iIDDeletedSource && !bSkippedByThisRenderer )
			{
				bDeletetionRequired = true; // Source removed, deletion required
				break;
			}
		}

		if( bDeletetionRequired == false )
		{
			// Receiver deleted?
547 548
			std::vector< int >::const_iterator citr = pDiff->viDelReceiverIDs.begin();
			while( citr != pDiff->viDelReceiverIDs.end() )
Jonas Stienen's avatar
Jonas Stienen committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
			{
				const int& iIDListenerDeleted( *citr++ );
				if( iListenerID == iIDListenerDeleted )
				{
					bDeletetionRequired = true; // Listener removed, deletion required
					break;
				}
			}
		}

		if( bDeletetionRequired )
		{
			DeleteSoundPath( pPath );
			itp = m_lSoundPaths.erase( itp ); // Increment via erase on path list
		}
		else
		{
			++itp; // no deletion detected, continue
		}
	}

	// Deleted sources
	std::vector< int >::const_iterator cits = pDiff->viDelSoundSourceIDs.begin();
	while( cits != pDiff->viDelSoundSourceIDs.end() )
	{
		const int& iID( *cits++ );
575

Jonas Stienen's avatar
Jonas Stienen committed
576 577 578 579 580 581 582
		// Remove only required, if not skipped by this renderer
		const CVASoundSourceDesc* pSoundSourceDesc = m_pCore->GetSceneManager()->GetSoundSourceDesc( iID );
		if( pSoundSourceDesc->sExplicitRendererID.empty() || pSoundSourceDesc->sExplicitRendererID == m_oParams.sID )
			DeleteSource( iID );
	}

	// Deleted receivers
583 584
	std::vector< int >::const_iterator citr = pDiff->viDelReceiverIDs.begin();
	while( citr != pDiff->viDelReceiverIDs.end() )
Jonas Stienen's avatar
Jonas Stienen committed
585 586 587 588 589 590 591 592 593 594 595 596
	{
		const int& iID( *citr++ );
		DeleteListener( iID );
	}

	// New sources
	cits = pDiff->viNewSoundSourceIDs.begin();
	while( cits != pDiff->viNewSoundSourceIDs.end() )
	{
		const int& iID( *cits++ );
		const CVASoundSourceDesc* pSoundSourceDesc = m_pCore->GetSceneManager()->GetSoundSourceDesc( iID );
		if( pSoundSourceDesc->sExplicitRendererID.empty() || pSoundSourceDesc->sExplicitRendererID == m_oParams.sID )
597
			CVABATNSource* pSource = CreateSoundSource( iID, pNewScene->GetSoundSourceState( iID ) );
Jonas Stienen's avatar
Jonas Stienen committed
598 599 600
	}

	// New receivers
601 602
	citr = pDiff->viNewReceiverIDs.begin();
	while( citr != pDiff->viNewReceiverIDs.end() )
Jonas Stienen's avatar
Jonas Stienen committed
603 604
	{
		const int& iID( *citr++ );
605
		CVABATNSoundReceiver* pListener = CreateSoundReceiver( iID, pNewScene->GetReceiverState( iID ) );
Jonas Stienen's avatar
Jonas Stienen committed
606 607 608
	}

	// New paths: (1) new receivers, current sources
609 610
	citr = pDiff->viNewReceiverIDs.begin();
	while( citr != pDiff->viNewReceiverIDs.end() )
Jonas Stienen's avatar
Jonas Stienen committed
611 612
	{
		int iListenerID = ( *citr++ );
613
		CVABATNSoundReceiver* pListener = m_mListeners[ iListenerID ];
Jonas Stienen's avatar
Jonas Stienen committed
614

615
		for( size_t i = 0; i < pDiff->viComSoundSourceIDs.size(); i++ )
Jonas Stienen's avatar
Jonas Stienen committed
616
		{
617 618
			int iSourceID = pDiff->viComSoundSourceIDs[ i ];
			CVABATNSource* pSource = m_mSources[ iSourceID ];
Jonas Stienen's avatar
Jonas Stienen committed
619 620 621 622 623 624 625 626 627 628 629 630 631

			bool bForThisRenderer = false;
			const CVASoundSourceDesc* pSoundSourceDesc = m_pCore->GetSceneManager()->GetSoundSourceDesc( iSourceID );
			if( pSoundSourceDesc->sExplicitRendererID.empty() || pSoundSourceDesc->sExplicitRendererID == m_oParams.sID )
				bForThisRenderer = true;

			if( !pSource->bDeleted && bForThisRenderer ) // only, if not marked for deletion and not skipped by renderer
				CVABATNSoundPath* pPath = CreateSoundPath( pSource, pListener );
		}
	}

	// New paths: (2) new sources, current receivers
	cits = pDiff->viNewSoundSourceIDs.begin();
632
	while( cits != pDiff->viNewSoundSourceIDs.end() )
Jonas Stienen's avatar
Jonas Stienen committed
633 634
	{
		const int& iSourceID( *cits++ );
635
		CVABATNSource* pSource = m_mSources[ iSourceID ];
Jonas Stienen's avatar
Jonas Stienen committed
636 637 638 639 640

		bool bForThisRenderer = false;
		const CVASoundSourceDesc* pSoundSourceDesc = m_pCore->GetSceneManager()->GetSoundSourceDesc( iSourceID );
		if( pSoundSourceDesc->sExplicitRendererID.empty() || pSoundSourceDesc->sExplicitRendererID == m_oParams.sID )
			bForThisRenderer = true;
641

642
		for( size_t i = 0; i < pDiff->viComReceiverIDs.size(); i++ )
Jonas Stienen's avatar
Jonas Stienen committed
643
		{
644
			int iListenerID = pDiff->viComReceiverIDs[ i ];
645
			CVABATNSoundReceiver* pListener = m_mListeners[ iListenerID ];
Jonas Stienen's avatar
Jonas Stienen committed
646 647 648 649 650 651 652 653 654 655 656

			if( !pListener->bDeleted && bForThisRenderer )
				CVABATNSoundPath* pPath = CreateSoundPath( pSource, pListener );
		}
	}

	// New paths: (3) new sources, new receivers
	cits = pDiff->viNewSoundSourceIDs.begin();
	while( cits != pDiff->viNewSoundSourceIDs.end() )
	{
		const int& iSourceID( *cits++ );
657
		CVABATNSource* pSource = m_mSources[ iSourceID ];
Jonas Stienen's avatar
Jonas Stienen committed
658 659 660 661 662

		const CVASoundSourceDesc* pSoundSourceDesc = m_pCore->GetSceneManager()->GetSoundSourceDesc( iSourceID );
		if( !pSoundSourceDesc->sExplicitRendererID.empty() && pSoundSourceDesc->sExplicitRendererID != m_oParams.sID )
			continue;

663 664
		citr = pDiff->viNewReceiverIDs.begin();
		while( citr != pDiff->viNewReceiverIDs.end() )
Jonas Stienen's avatar
Jonas Stienen committed
665 666
		{
			const int& iListenerID( *citr++ );
667
			CVABATNSoundReceiver* pListener = m_mListeners[ iListenerID ];
Jonas Stienen's avatar
Jonas Stienen committed
668 669 670
			CVABATNSoundPath* pPath = CreateSoundPath( pSource, pListener );
		}
	}
671

Jonas Stienen's avatar
Jonas Stienen committed
672 673 674 675 676 677 678 679 680 681
	return;
}

void CVABinauralAirTrafficNoiseAudioRenderer::ProcessStream( const ITAStreamInfo* pStreamInfo )
{
	// If streaming is active, set to 1
	ctxAudio.m_iStatus = 1;

	// Schallpfade abgleichen
	SyncInternalData();
682

Jonas Stienen's avatar
Jonas Stienen committed
683 684 685 686 687 688 689 690
	float* pfOutputChL = GetWritePointer( 0 );
	float* pfOutputChR = GetWritePointer( 1 );

	fm_zero( pfOutputChL, GetBlocklength() );
	fm_zero( pfOutputChR, GetBlocklength() );

	const CVAAudiostreamState* pStreamState = dynamic_cast< const CVAAudiostreamState* >( pStreamInfo );
	double dListenerTime = pStreamState->dSysTime;
691

Jonas Stienen's avatar
Jonas Stienen committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
	// Check for reset request
	if( ctxAudio.m_iResetFlag == 1 )
	{
		ResetInternalData();

		return;
	}
	else if( ctxAudio.m_iResetFlag == 2 )
	{
		// Reset active, skip until finished
		return;
	}

	SampleTrajectoriesInternal( dListenerTime );

707
	std::map< int, CVABATNSoundReceiver* >::iterator lit = m_mListeners.begin();
Jonas Stienen's avatar
Jonas Stienen committed
708 709 710 711 712 713 714 715 716 717 718
	while( lit != m_mListeners.end() )
	{
		lit->second->psfOutput->zero();
		lit++;
	}

	// Update sound pathes
	std::list< CVABATNSoundPath* >::iterator spit = ctxAudio.m_lSoundPaths.begin();
	while( spit != ctxAudio.m_lSoundPaths.end() )
	{
		CVABATNSoundPath* pPath( *spit );
719
		CVAReceiverState* pListenerState = ( m_pCurSceneState ? m_pCurSceneState->GetReceiverState( pPath->pSoundReceiver->pData->iID ) : NULL );
720
		CVASoundSourceState* pSourceState = ( m_pCurSceneState ? m_pCurSceneState->GetSoundSourceState( pPath->pSoundSource->pData->iID ) : NULL );
Jonas Stienen's avatar
Jonas Stienen committed
721 722 723 724 725 726 727 728

		if( pListenerState == nullptr || pSourceState == nullptr )
		{
			// Skip if no data is present
			spit++;
			continue;
		}

729
		if( pPath->pSoundReceiver->bValidTrajectoryPresent == false || pPath->pSoundSource->bValidTrajectoryPresent == false )
Jonas Stienen's avatar
Jonas Stienen committed
730 731 732 733 734 735 736 737 738 739
		{
			// Skip if no valid trajectory data is present
			spit++;
			continue;
		}

		// --= Parameter update =--

		pPath->UpdateMetrics();

740 741 742
		bool bAASourceStatusEnabled = ( pSourceState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_MEDIUM_ABSORPTION ) > 0;
		bool bAAListenerStatusEnabled = ( pListenerState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_MEDIUM_ABSORPTION ) > 0;
		bool bAAGlobalStatusEnabled = ( m_iCurGlobalAuralizationMode & IVAInterface::VA_AURAMODE_MEDIUM_ABSORPTION ) > 0;
Jonas Stienen's avatar
Jonas Stienen committed
743
		bool bAAEnabled = bAASourceStatusEnabled && bAAListenerStatusEnabled && bAAGlobalStatusEnabled;
744

745 746 747 748 749
		// @todo get homogeneous medium information for air attenuation
		if( !m_bAirAbsorptionExternalSimulation )
			pPath->UpdateAirAttenuation();

		// Temporal variations
750 751 752
		bool bTVSourceStatusEnabled = ( pSourceState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_TEMP_VAR ) > 0;
		bool bTVListenerStatusEnabled = ( pListenerState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_TEMP_VAR ) > 0;
		bool bTVGlobalStatusEnabled = ( m_iCurGlobalAuralizationMode & IVAInterface::VA_AURAMODE_TEMP_VAR ) > 0;
Jonas Stienen's avatar
Jonas Stienen committed
753
		bool bTVEnabled = bTVSourceStatusEnabled && bTVListenerStatusEnabled && bTVGlobalStatusEnabled;
754 755 756

		if( !m_bTemporalVariationsExternalSimulation )
			pPath->UpdateTemporalVariation();
757 758 759 760

		bool bDPEnabledGlobal = ( m_iCurGlobalAuralizationMode & IVAInterface::VA_AURAMODE_DOPPLER ) > 0;
		bool bDPEnabledListener = ( pListenerState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_DOPPLER ) > 0;
		bool bDPEnabledSource = ( pSourceState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_DOPPLER ) > 0;
761
		bool bDPEnabledCurrent = ( pPath->oDirSoundPath.pVariableDelayLine->GetAlgorithm() != CITAVariableDelayLine::SWITCH ); // switch = disabled
762
		bool bDPStatusChanged = ( bDPEnabledCurrent != ( bDPEnabledGlobal && bDPEnabledListener && bDPEnabledSource ) );
Jonas Stienen's avatar
Jonas Stienen committed
763 764
		if( bDPStatusChanged )
		{
765 766
			pPath->oDirSoundPath.pVariableDelayLine->SetAlgorithm( !bDPEnabledCurrent ? m_iDefaultVDLSwitchingAlgorithm : CITAVariableDelayLine::SWITCH );
			pPath->oRefSoundPath.pVariableDelayLine->SetAlgorithm( !bDPEnabledCurrent ? m_iDefaultVDLSwitchingAlgorithm : CITAVariableDelayLine::SWITCH );
Jonas Stienen's avatar
Jonas Stienen committed
767 768
		}

769
		if( !m_bPropagationDelayExternalSimulation )
770
			pPath->UpdateMediumPropagation( m_pCore->oHomogeneousMedium.dSoundSpeed );
Jonas Stienen's avatar
Jonas Stienen committed
771

772 773 774
		// DSP: set VDL delay time
		pPath->oDirSoundPath.pVariableDelayLine->SetDelayTime( pPath->oDirSoundPath.dPropagationTime );
		pPath->oRefSoundPath.pVariableDelayLine->SetDelayTime( pPath->oRefSoundPath.dPropagationTime );
Jonas Stienen's avatar
Jonas Stienen committed
775 776

		// Spherical spreading loss
777 778 779 780
		bool bSSLEnabledSource = ( pSourceState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_SPREADING_LOSS ) > 0;
		bool bSSLEnabledListener = ( pListenerState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_SPREADING_LOSS ) > 0;
		bool bSSLEnabledGlobal = ( m_iCurGlobalAuralizationMode & IVAInterface::VA_AURAMODE_SPREADING_LOSS ) > 0;
		bool bSSLEnabled = ( bSSLEnabledSource && bSSLEnabledListener && bSSLEnabledGlobal );
781 782 783 784 785 786 787 788 789

		if( !m_bSpreadingLossExternalSimulation )
			pPath->CalculateInverseDistanceDecrease();

		if( !bSSLEnabled ) // Override in case spreading loss is disabled
		{
			pPath->oDirSoundPath.dGeometricalSpreadingLoss = 1.0f;
			pPath->oDirSoundPath.dGeometricalSpreadingLoss = 1.0f;
		}
Jonas Stienen's avatar
Jonas Stienen committed
790

791
		bool bDIREnabledSoure = ( pSourceState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_SOURCE_DIRECTIVITY ) > 0;
792 793
		bool bDIREnabledListener = ( pListenerState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_SOURCE_DIRECTIVITY ) > 0;
		bool bDIREnabledGlobal = ( m_iCurGlobalAuralizationMode & IVAInterface::VA_AURAMODE_SOURCE_DIRECTIVITY ) > 0;
794
		bool bDIREnabled = ( bDIREnabledSoure && bDIREnabledListener && bDIREnabledGlobal );
795 796 797

		if( !m_bDirectivityExternalSimulation )
			pPath->UpdateSoundSourceDirectivity();
Jonas Stienen's avatar
Jonas Stienen committed
798

799
		pPath->UpdateSoundReceiverDirectivity();
Jonas Stienen's avatar
Jonas Stienen committed
800 801

		// Sound source gain / direct sound audibility via AuraMode flags
802 803 804
		bool bDSSourceStatusEnabled = ( pSourceState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_DIRECT_SOUND ) > 0;
		bool bDSListenerStatusEnabled = ( pListenerState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_DIRECT_SOUND ) > 0;
		bool bDSGlobalStatusEnabled = ( m_iCurGlobalAuralizationMode & IVAInterface::VA_AURAMODE_DIRECT_SOUND ) > 0;
Jonas Stienen's avatar
Jonas Stienen committed
805 806
		bool bDSEnabled = bDSSourceStatusEnabled && bDSListenerStatusEnabled && bDSGlobalStatusEnabled;

807
		// Reflection (early reflections) via AuraMode flags
808 809 810
		bool bERSourceStatusEnabled = ( pSourceState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_EARLY_REFLECTIONS ) > 0;
		bool bERListenerStatusEnabled = ( pListenerState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_EARLY_REFLECTIONS ) > 0;
		bool bERGlobalStatusEnabled = ( m_iCurGlobalAuralizationMode & IVAInterface::VA_AURAMODE_EARLY_REFLECTIONS ) > 0;
Jonas Stienen's avatar
Jonas Stienen committed
811 812
		bool bEREnabled = bERSourceStatusEnabled && bERListenerStatusEnabled && bERGlobalStatusEnabled;

813 814
		float fDirectSoundPathOverallGain = float( pPath->oDirSoundPath.dGeometricalSpreadingLoss * pSourceState->GetVolume( m_pCore->GetCoreConfig()->dDefaultAmplitudeCalibration ) );
		float fReflectedSoundPathOverallGain = float( pPath->oRefSoundPath.dGeometricalSpreadingLoss * pSourceState->GetVolume( m_pCore->GetCoreConfig()->dDefaultAmplitudeCalibration ) );
815
		if( pPath->pSoundSource->pData->bMuted )
Jonas Stienen's avatar
Jonas Stienen committed
816
		{
817 818
			fDirectSoundPathOverallGain = 0.0f;
			fReflectedSoundPathOverallGain = 0.0f;
Jonas Stienen's avatar
Jonas Stienen committed
819 820 821 822
		}
		else
		{
			if( bDSEnabled == false )
823
				fDirectSoundPathOverallGain = 0.0f;
Jonas Stienen's avatar
Jonas Stienen committed
824
			if( bEREnabled == false )
825
				fReflectedSoundPathOverallGain = 0.0f;
Jonas Stienen's avatar
Jonas Stienen committed
826 827
		}

828 829 830 831 832
		// DSP: set overall gain
		pPath->oDirSoundPath.pFIRConvolverChL->SetGain( fDirectSoundPathOverallGain );
		pPath->oRefSoundPath.pFIRConvolverChL->SetGain( fReflectedSoundPathOverallGain );
		pPath->oDirSoundPath.pFIRConvolverChR->SetGain( fDirectSoundPathOverallGain );
		pPath->oRefSoundPath.pFIRConvolverChR->SetGain( fReflectedSoundPathOverallGain );
Jonas Stienen's avatar
Jonas Stienen committed
833 834

		// Assemble and set third octave gains
835 836 837
		pPath->oDirSoundPath.oThirdOctaveFilterMagnitudes.SetIdentity();
		pPath->oRefSoundPath.oThirdOctaveFilterMagnitudes.SetIdentity();
		if( bAAEnabled )
838
		{
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
			pPath->oDirSoundPath.oThirdOctaveFilterMagnitudes.Multiply( pPath->oDirSoundPath.oAirAttenuationMagnitudes );
			pPath->oRefSoundPath.oThirdOctaveFilterMagnitudes.Multiply( pPath->oRefSoundPath.oAirAttenuationMagnitudes );
		}
		if( bDIREnabled )
		{
			pPath->oDirSoundPath.oThirdOctaveFilterMagnitudes.Multiply( pPath->oDirSoundPath.oSoundSourceDirectivityMagnitudes );
			pPath->oRefSoundPath.oThirdOctaveFilterMagnitudes.Multiply( pPath->oRefSoundPath.oSoundSourceDirectivityMagnitudes );
		}
		if( bTVEnabled )
		{
			pPath->oDirSoundPath.oThirdOctaveFilterMagnitudes.Multiply( pPath->oDirSoundPath.oTemporalVariationMagnitudes );
			pPath->oRefSoundPath.oThirdOctaveFilterMagnitudes.Multiply( pPath->oRefSoundPath.oTemporalVariationMagnitudes );
		}

		if( bEREnabled )
		{
			pPath->oRefSoundPath.oThirdOctaveFilterMagnitudes.Multiply( pPath->oRefSoundPath.oGroundReflectionMagnitudes );
856
		}
Jonas Stienen's avatar
Jonas Stienen committed
857

858 859 860 861 862
		// DSP: set overall magnitudes
		pPath->oDirSoundPath.pThirdOctaveFilterBank->SetMagnitudes( pPath->oDirSoundPath.oThirdOctaveFilterMagnitudes );
		pPath->oRefSoundPath.pThirdOctaveFilterBank->SetMagnitudes( pPath->oRefSoundPath.oThirdOctaveFilterMagnitudes );


Jonas Stienen's avatar
Jonas Stienen committed
863 864 865

		// Digital signal processing

866
		CVASoundSourceDesc* pSourceData = pPath->pSoundSource->pData;
Jonas Stienen's avatar
Jonas Stienen committed
867 868 869 870 871 872 873
		const ITASampleBuffer* psbInput = pSourceData->pSignalSourceInputBuf;

		pPath->oDirSoundPath.pVariableDelayLine->Process( psbInput, &( ctxAudio.m_sbTempBufD ) );
		pPath->oRefSoundPath.pVariableDelayLine->Process( psbInput, &( ctxAudio.m_sbTempBufR ) );

		pPath->oDirSoundPath.pThirdOctaveFilterBank->Process( ctxAudio.m_sbTempBufD.data(), ctxAudio.m_sbTempBufD.data() ); // inplace
		pPath->oRefSoundPath.pThirdOctaveFilterBank->Process( ctxAudio.m_sbTempBufR.data(), ctxAudio.m_sbTempBufR.data() ); // inplace
874

Jonas Stienen's avatar
Jonas Stienen committed
875

876
		pPath->oDirSoundPath.pFIRConvolverChL->Process( ctxAudio.m_sbTempBufD.data(), ( *pPath->pSoundReceiver->psfOutput )[ 0 ].data(), ITAUPConvolution::OUTPUT_MIX );
877 878
		pPath->oRefSoundPath.pFIRConvolverChL->Process( ctxAudio.m_sbTempBufR.data(), ( *pPath->pSoundReceiver->psfOutput )[ 0 ].data(), ITAUPConvolution::OUTPUT_MIX );

879 880
		pPath->oDirSoundPath.pFIRConvolverChR->Process( ctxAudio.m_sbTempBufD.data(), ( *pPath->pSoundReceiver->psfOutput )[ 1 ].data(), ITAUPConvolution::OUTPUT_MIX );
		pPath->oRefSoundPath.pFIRConvolverChR->Process( ctxAudio.m_sbTempBufR.data(), ( *pPath->pSoundReceiver->psfOutput )[ 1 ].data(), ITAUPConvolution::OUTPUT_MIX );
881

Jonas Stienen's avatar
Jonas Stienen committed
882 883 884 885 886 887
		spit++;
	}

	// TODO: Select active listener
	if( !m_mListeners.empty() )
	{
888 889 890
		CVABATNSoundReceiver* pActiveListener = m_mListeners.begin()->second;
		fm_copy( pfOutputChL, ( *pActiveListener->psfOutput )[ 0 ].data(), m_uiBlocklength );
		fm_copy( pfOutputChR, ( *pActiveListener->psfOutput )[ 1 ].data(), m_uiBlocklength );
Jonas Stienen's avatar
Jonas Stienen committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
	}

	IncrementWritePointer();

	return;
}

void CVABinauralAirTrafficNoiseAudioRenderer::UpdateTrajectories()
{
	// Neue Quellendaten bernehmen
	for( std::map< int, CVABATNSource* >::iterator it = m_mSources.begin(); it != m_mSources.end(); ++it )
	{
		int iSourceID = it->first;
		CVABATNSource* pSource = it->second;

		CVASoundSourceState* pSourceCur = ( m_pCurSceneState ? m_pCurSceneState->GetSoundSourceState( iSourceID ) : nullptr );
		CVASoundSourceState* pSourceNew = ( m_pNewSceneState ? m_pNewSceneState->GetSoundSourceState( iSourceID ) : nullptr );
908

Jonas Stienen's avatar
Jonas Stienen committed
909 910 911 912 913
		const CVAMotionState* pMotionCur = ( pSourceCur ? pSourceCur->GetMotionState() : nullptr );
		const CVAMotionState* pMotionNew = ( pSourceNew ? pSourceNew->GetMotionState() : nullptr );

		if( pMotionNew && ( pMotionNew != pMotionCur ) )
		{
914
			VA_TRACE( "BinauralAirTrafficNoiseAudioRenderer", "Source " << iSourceID << " new motion state" );
Jonas Stienen's avatar
Jonas Stienen committed
915 916 917 918 919
			pSource->pMotionModel->InputMotionKey( pMotionNew );
		}
	}

	// Neue Hrerdaten bernehmen
920
	for( std::map< int, CVABATNSoundReceiver* >::iterator it = m_mListeners.begin(); it != m_mListeners.end(); ++it )
Jonas Stienen's avatar
Jonas Stienen committed
921 922
	{
		int iListenerID = it->first;
923
		CVABATNSoundReceiver* pListener = it->second;
Jonas Stienen's avatar
Jonas Stienen committed
924

925 926
		CVAReceiverState* pListenerCur = ( m_pCurSceneState ? m_pCurSceneState->GetReceiverState( iListenerID ) : nullptr );
		CVAReceiverState* pListenerNew = ( m_pNewSceneState ? m_pNewSceneState->GetReceiverState( iListenerID ) : nullptr );
Jonas Stienen's avatar
Jonas Stienen committed
927 928 929 930 931 932

		const CVAMotionState* pMotionCur = ( pListenerCur ? pListenerCur->GetMotionState() : nullptr );
		const CVAMotionState* pMotionNew = ( pListenerNew ? pListenerNew->GetMotionState() : nullptr );

		if( pMotionNew && ( pMotionNew != pMotionCur ) )
		{
933
			VA_TRACE( "BinauralAirTrafficNoiseAudioRenderer", "Listener " << iListenerID << " new position " );// << *pMotionNew);
Jonas Stienen's avatar
Jonas Stienen committed
934 935 936 937 938 939 940 941
			pListener->pMotionModel->InputMotionKey( pMotionNew );
		}
	}
}

void CVABinauralAirTrafficNoiseAudioRenderer::SampleTrajectoriesInternal( double dTime )
{
	bool bEstimationPossible = true;
942
	for( std::list< CVABATNSource* >::iterator it = ctxAudio.m_lSources.begin(); it != ctxAudio.m_lSources.end(); ++it )
Jonas Stienen's avatar
Jonas Stienen committed
943 944 945 946 947 948 949 950 951 952
	{
		CVABATNSource* pSource = *it;

		pSource->pMotionModel->HandleMotionKeys();
		bEstimationPossible &= pSource->pMotionModel->EstimatePosition( dTime, pSource->vPredPos );
		bEstimationPossible &= pSource->pMotionModel->EstimateOrientation( dTime, pSource->vPredView, pSource->vPredUp );
		pSource->bValidTrajectoryPresent = bEstimationPossible;
	}

	bEstimationPossible = true;
953
	for( std::list< CVABATNSoundReceiver* >::iterator it = ctxAudio.m_lListener.begin(); it != ctxAudio.m_lListener.end(); ++it )
Jonas Stienen's avatar
Jonas Stienen committed
954
	{
955
		CVABATNSoundReceiver* pListener = *it;
Jonas Stienen's avatar
Jonas Stienen committed
956 957 958 959 960 961 962 963

		pListener->pMotionModel->HandleMotionKeys();
		bEstimationPossible &= pListener->pMotionModel->EstimatePosition( dTime, pListener->vPredPos );
		bEstimationPossible &= pListener->pMotionModel->EstimateOrientation( dTime, pListener->vPredView, pListener->vPredUp );
		pListener->bValidTrajectoryPresent = bEstimationPossible;
	}
}

964
CVABATNSoundPath* CVABinauralAirTrafficNoiseAudioRenderer::CreateSoundPath( CVABATNSource* pSource, CVABATNSoundReceiver* pListener )
Jonas Stienen's avatar
Jonas Stienen committed
965 966 967 968 969 970
{
	int iSourceID = pSource->pData->iID;
	int iListenerID = pListener->pData->iID;

	assert( !pSource->bDeleted && !pListener->bDeleted );

971
	VA_VERBOSE( "BinauralAirTrafficNoiseAudioRenderer", "Creating sound path from source " << iSourceID << " -> listener " << iListenerID );
Jonas Stienen's avatar
Jonas Stienen committed
972

973 974 975 976
	CVABATNSoundPath* pPath = dynamic_cast< CVABATNSoundPath* >( m_pSoundPathPool->RequestObject() );

	pPath->pSoundSource = pSource;
	pPath->pSoundReceiver = pListener;
Jonas Stienen's avatar
Jonas Stienen committed
977 978 979

	pPath->bDelete = false;

980
	CVASoundSourceState* pSourceNew = ( m_pNewSceneState ? m_pNewSceneState->GetSoundSourceState( iSourceID ) : nullptr );
Jonas Stienen's avatar
Jonas Stienen committed
981 982
	if( pSourceNew != nullptr )
	{
983 984
		pPath->oDirSoundPath.oDirectivityStateNew.pData = ( IVADirectivity* ) pSourceNew->GetDirectivityData();
		pPath->oRefSoundPath.oDirectivityStateNew.pData = ( IVADirectivity* ) pSourceNew->GetDirectivityData();
Jonas Stienen's avatar
Jonas Stienen committed
985 986
	}

987
	CVAReceiverState* pListenerNew = ( m_pNewSceneState ? m_pNewSceneState->GetReceiverState( iListenerID ) : nullptr );
Jonas Stienen's avatar
Jonas Stienen committed
988 989
	if( pListenerNew != nullptr )
	{
990 991
		pPath->oDirSoundPath.oSoundReceiverDirectivityStateNew.pData = ( IVADirectivity* ) pListenerNew->GetDirectivity();
		pPath->oRefSoundPath.oSoundReceiverDirectivityStateNew.pData = ( IVADirectivity* ) pListenerNew->GetDirectivity();
Jonas Stienen's avatar
Jonas Stienen committed
992
	}
993

Jonas Stienen's avatar
Jonas Stienen committed
994 995 996 997 998 999 1000 1001
	m_lSoundPaths.push_back( pPath );
	m_pUpdateMessage->vNewPaths.push_back( pPath );

	return pPath;
}

void CVABinauralAirTrafficNoiseAudioRenderer::DeleteSoundPath( CVABATNSoundPath* pPath )
{
1002
	VA_VERBOSE( "BinauralAirTrafficNoiseAudioRenderer", "Marking sound path from source " << pPath->pSoundSource->pData->iID << " -> listener " << pPath->pSoundReceiver->pData->iID << " for deletion" );
Jonas Stienen's avatar
Jonas Stienen committed
1003 1004 1005 1006 1007 1008

	pPath->bDelete = true;
	pPath->RemoveReference();
	m_pUpdateMessage->vDelPaths.push_back( pPath );
}

1009
CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSoundReceiver* CVABinauralAirTrafficNoiseAudioRenderer::CreateSoundReceiver( int iID, const CVAReceiverState* pListenerState )
Jonas Stienen's avatar
Jonas Stienen committed
1010
{
1011
	VA_VERBOSE( "BinauralAirTrafficNoiseAudioRenderer", "Creating listener with ID " << iID );
Jonas Stienen's avatar
Jonas Stienen committed
1012

1013
	CVABATNSoundReceiver* pListener = dynamic_cast< CVABATNSoundReceiver* >( m_pListenerPool->RequestObject() );
Jonas Stienen's avatar
Jonas Stienen committed
1014 1015 1016 1017 1018

	pListener->pData = m_pCore->GetSceneManager()->GetListenerDesc( iID );
	pListener->pData->AddReference();

	// Move to prerequest of pool?
1019
	pListener->psfOutput = new ITASampleFrame( 2, m_pCore->GetCoreConfig()->oAudioDriverConfig.iBuffersize, true );
Jonas Stienen's avatar
Jonas Stienen committed
1020 1021 1022 1023 1024 1025 1026 1027
	assert( pListener->pData );
	pListener->bDeleted = false;

	// Motion model
	CVABasicMotionModel* pMotionInstance = dynamic_cast< CVABasicMotionModel* >( pListener->pMotionModel->GetInstance() );
	pMotionInstance->SetName( std::string( "bfrend_mm_listener_" + pListener->pData->sName ) );
	pMotionInstance->Reset();

1028
	m_mListeners.insert( std::pair< int, CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSoundReceiver* >( iID, pListener ) );
Jonas Stienen's avatar
Jonas Stienen committed
1029 1030 1031 1032 1033 1034 1035 1036

	m_pUpdateMessage->vNewListeners.push_back( pListener );

	return pListener;
}

void CVABinauralAirTrafficNoiseAudioRenderer::DeleteListener( int iListenerID )
{
1037
	VA_VERBOSE( "BinauralAirTrafficNoiseAudioRenderer", "Marking listener with ID " << iListenerID << " for removal" );
1038 1039
	std::map< int, CVABATNSoundReceiver* >::iterator it = m_mListeners.find( iListenerID );
	CVABATNSoundReceiver* pListener = it->second;
Jonas Stienen's avatar
Jonas Stienen committed
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
	m_mListeners.erase( it );
	pListener->bDeleted = true;
	pListener->pData->RemoveReference();
	pListener->RemoveReference();

	m_pUpdateMessage->vDelListeners.push_back( pListener );

	return;
}

1050
CVABinauralAirTrafficNoiseAudioRenderer::CVABATNSource* CVABinauralAirTrafficNoiseAudioRenderer::CreateSoundSource( int iID, const CVASoundSourceState* pSourceState )
Jonas Stienen's avatar
Jonas Stienen committed
1051
{
1052
	VA_VERBOSE( "BinauralAirTrafficNoiseAudioRenderer", "Creating source with ID " << iID );
Jonas Stienen's avatar
Jonas Stienen committed
1053
	CVABATNSource* pSource = dynamic_cast< CVABATNSource* >( m_pSourcePool->RequestObject() );
1054 1055

	pSource->pData = m_pCore->GetSceneManager()->GetSoundSourceDesc( iID );
Jonas Stienen's avatar
Jonas Stienen committed
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	pSource->pData->AddReference();

	pSource->bDeleted = false;

	CVABasicMotionModel* pMotionInstance = dynamic_cast< CVABasicMotionModel* >( pSource->pMotionModel->GetInstance() );
	pMotionInstance->SetName( std::string( "bfrend_mm_source_" + pSource->pData->sName ) );
	pMotionInstance->Reset();

	m_mSources.insert( std::pair< int, CVABATNSource* >( iID, pSource ) );

	m_pUpdateMessage->vNewSources.push_back( pSource );

	return pSource;
}

void CVABinauralAirTrafficNoiseAudioRenderer::DeleteSource( int iSourceID )
{
1073
	VA_VERBOSE( "BinauralAirTrafficNoiseAudioRenderer", "Marking source with ID " << iSourceID << " for removal" );
Jonas Stienen's avatar
Jonas Stienen committed
1074 1075 1076 1077 1078 1079 1080 1081
	std::map< int, CVABATNSource* >::iterator it = m_mSources.find( iSourceID );
	CVABATNSource* pSource = it->second;
	m_mSources.erase( it );
	pSource->bDeleted = true;
	pSource->pData->RemoveReference();
	pSource->RemoveReference();

	m_pUpdateMessage->vDelSources.push_back( pSource );
1082

Jonas Stienen's avatar
Jonas Stienen committed
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	return;
}

void CVABinauralAirTrafficNoiseAudioRenderer::SyncInternalData()
{
	CVABATNUpdateMessage* pUpdate;
	while( ctxAudio.m_qpUpdateMessages.try_pop( pUpdate ) )
	{
		std::list< CVABATNSoundPath* >::const_iterator citp = pUpdate->vDelPaths.begin();
		while( citp != pUpdate->vDelPaths.end() )
		{
			CVABATNSoundPath* pPath( *citp++ );
			ctxAudio.m_lSoundPaths.remove( pPath );
			pPath->RemoveReference();
		}

		citp = pUpdate->vNewPaths.begin();
		while( citp != pUpdate->vNewPaths.end() )
		{
			CVABATNSoundPath* pPath( *citp++ );
			pPath->AddReference();
			ctxAudio.m_lSoundPaths.push_back( pPath );
		}

		std::list< CVABATNSource* >::const_iterator cits = pUpdate->vDelSources.begin();
		while( cits != pUpdate->vDelSources.end() )
		{
			CVABATNSource* pSource( *cits++ );
			ctxAudio.m_lSources.remove( pSource );
			pSource->pData->RemoveReference();
			pSource->RemoveReference();
		}

		cits = pUpdate->vNewSources.begin();
		while( cits != pUpdate->vNewSources.end() )
		{
			CVABATNSource* pSource( *cits++ );
			pSource->AddReference();
			pSource->pData->AddReference();
			ctxAudio.m_lSources.push_back( pSource );
		}

1125
		std::list< CVABATNSoundReceiver* >::const_iterator citl = pUpdate->vDelListeners.begin();
Jonas Stienen's avatar
Jonas Stienen committed
1126 1127
		while( citl != pUpdate->vDelListeners.end() )
		{