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

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

16
#ifdef VACORE_WITH_RENDERER_BINAURAL_FREE_FIELD
Jonas Stienen's avatar
Jonas Stienen committed
17
18

// VA includes
19
20
21
#include <VA.h>

// VA core includes
22
23
#include "../../../directivities/VADirectivityDAFFEnergetic.h"
#include "../../../directivities/VADirectivityDAFFHRIR.h"
24
#include "../../../directivities/VADirectivityDAFFHATOHRIR.h"
Jonas Stienen's avatar
Jonas Stienen committed
25
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"
#include <VAObjectPool.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
43
44
#include <ITAAtomicPrimitives.h>
#include <ITAClock.h>
#include <ITACriticalSection.h>
45
#include <ITADataSourceRealization.h>
46
47
#include <ITAThirdOctaveFilterbank.h>
#include <ITAVariableDelayLine.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
#include <ITASampleFrame.h>
#include <ITAStreamInfo.h>
#include <ITAFastMath.h>
53
#include <ITAConstants.h>
54
#include <ITANumericUtils.h>
Jonas Stienen's avatar
Jonas Stienen committed
55
56
57

// Vista includes
#include <VistaInterProcComm/Concurrency/VistaThreadEvent.h>
58
#include <VistaBase/VistaQuaternion.h>
Jonas Stienen's avatar
Jonas Stienen committed
59
60

// STL includes
61
#include <algorithm>
62
#include <cassert>
Jonas Stienen's avatar
Jonas Stienen committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <fstream>
#include <iomanip>

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

class CVABFFSoundPath : public CVAPoolObject
{
public:
	virtual ~CVABFFSoundPath();

	//! Retarded metrics of sound path
	class Metrics
	{
	public:
		double dRetardedDistance;				 //!< Metrical distance to retarded sound position
80
81
		VAQuat qOrientationRetSourceToReceiver; //!< Immediate angle of incidence to retarded source position in listener reference frame in YPR convention
		VAQuat qOrientationReceiverToRetSource; //!< Retarded angle of incidence to listener in source reference frame in YPR convention
Jonas Stienen's avatar
Jonas Stienen committed
82
83
84
85
86
87
	};

	//! State of directivity
	class CDirectivityState
	{
	public:
88
		inline CDirectivityState()
89
90
			: pData( NULL )
			, iRecord( -1 )
91
92
			, bDirectivityEnabled( true )
		{};
93

Jonas Stienen's avatar
Jonas Stienen committed
94
95
96
97
		IVADirectivity* pData;	//!< Directivity data, may be NULL
		int iRecord;			//!< Directivity index
		bool bDirectivityEnabled;

98
		inline bool operator==( const CDirectivityState& rhs ) const
Jonas Stienen's avatar
Jonas Stienen committed
99
100
101
102
103
104
		{
			bool bBothEnabled = ( bDirectivityEnabled == rhs.bDirectivityEnabled );
			bool bSameRecordIndex = ( iRecord == rhs.iRecord );
			bool bSameData = ( pData == rhs.pData );

			return ( bBothEnabled && bSameRecordIndex && bSameData );
105
		};
Jonas Stienen's avatar
Jonas Stienen committed
106
107
108
109
110
	};

	class CHRIRState
	{
	public:
111
		inline CHRIRState()
112
113
			: pData( NULL )
			, iRecord( -1 )
114
115
116
			, fDistance( 1.0f )
			, dHATODeg( 0.0f )
		{};
Jonas Stienen's avatar
Jonas Stienen committed
117

118
		IVADirectivity* pData;	 //!< HRIR data, may be NULL
Jonas Stienen's avatar
Jonas Stienen committed
119
120
		int iRecord;			 //!< HRIR index
		float fDistance;		 //!< HRIR dataset distance
121
		double dHATODeg; //!< HRIR head-above-torso orientation
Jonas Stienen's avatar
Jonas Stienen committed
122

123
		inline bool operator!=( const CHRIRState& rhs ) const
Jonas Stienen's avatar
Jonas Stienen committed
124
125
		{
			if( pData != rhs.pData ) return true;
126
			if( fDistance != rhs.fDistance ) return true;
Jonas Stienen's avatar
Jonas Stienen committed
127
			if( iRecord != rhs.iRecord ) return true;
128
			if( dHATODeg != rhs.dHATODeg ) return true;
Jonas Stienen's avatar
Jonas Stienen committed
129
			return false;
130
		};
Jonas Stienen's avatar
Jonas Stienen committed
131
132
133
134
135
	};

	CVABinauralFreeFieldAudioRenderer::CVABFFSource* pSource;
	CVABinauralFreeFieldAudioRenderer::CVABFFListener* pListener;

136
	CVASourceReceiverMetrics oRelations; //!< Informatioen on source and receiver relations (distances & angles)
Jonas Stienen's avatar
Jonas Stienen committed
137
138
139
140
141
142
143
144

	CDirectivityState oDirectivityStateCur;
	CDirectivityState oDirectivityStateNew;

	CHRIRState oHRIRStateCur;
	CHRIRState oHRIRStateNew;

	ITAAtomicBool bDelete;
145

146
147
148
	CITAThirdOctaveFilterbank* pThirdOctaveFilterBank;
	CITAVariableDelayLine* pVariableDelayLineChL;
	CITAVariableDelayLine* pVariableDelayLineChR;
149
150
	ITAUPConvolution* pFIRConvolverChL;
	ITAUPConvolution* pFIRConvolverChR;
Jonas Stienen's avatar
Jonas Stienen committed
151

152
	inline void PreRequest()
153
	{
Jonas Stienen's avatar
Jonas Stienen committed
154
155
156
157
158
		pSource = nullptr;
		pListener = nullptr;

		// Reset DSP elements
		pThirdOctaveFilterBank->Clear();
159
160
		pVariableDelayLineChL->Clear();
		pVariableDelayLineChR->Clear();
Jonas Stienen's avatar
Jonas Stienen committed
161
162
163
164
165
		pFIRConvolverChL->clear();
		pFIRConvolverChR->clear();
	};

	void UpdateDir( bool bDIRAuraModeEnabled );
166
	void UpdateMediumPropagation( double dSpeedOfSound, double dAdditionalDelaySeconds = 0.0f );
167
	double CalculateInverseDistanceDecrease( const double dMinimumDistance ) const;
168
	void UpdateHRIR( const CVAReceiverState::CVAAnthropometricParameter&, const double dHATODeg );
169

Jonas Stienen's avatar
Jonas Stienen committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
	//! 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();
	// bool UpdateMetrics( double dTimestamp, double dSpeedOfSound ); // For retarded information

private:

	//! Standard-Konstruktor deaktivieren
	CVABFFSoundPath();

	//! Konstruktor
	CVABFFSoundPath( double dSamplerate, int iBlocklength, int iHRIRFilterLength, int iDirFilterLength );

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

	friend class CVABFFSoundPathFactory;
};

198
199
class CVABFFSoundPathFactory : public IVAPoolObjectFactory
{
200
public:
Jonas Stienen's avatar
Jonas Stienen committed
201
	CVABFFSoundPathFactory( double dSamplerate, int iBlocklength, int iHRIRFilterLength, int iDirFilterLength )
202
203
204
205
		: m_dSamplerate( dSamplerate )
		, m_iBlocklength( iBlocklength )
		, m_iHRIRFilterLength( iHRIRFilterLength )
		, m_iDirFilterLength( iDirFilterLength )
206
	{};
Jonas Stienen's avatar
Jonas Stienen committed
207
208
209
210

	CVAPoolObject* CreatePoolObject()
	{
		return new CVABFFSoundPath( m_dSamplerate, m_iBlocklength, m_iHRIRFilterLength, m_iDirFilterLength );
211
	};
Jonas Stienen's avatar
Jonas Stienen committed
212
213
214
215
216
217
218
219
220
221
222

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

class CVABFFListenerPoolFactory : public IVAPoolObjectFactory
{
public:
223
224
	CVABFFListenerPoolFactory( CVACoreImpl* pCore, const CVABinauralFreeFieldAudioRenderer::CVABFFListener::Config& oConf )
		: m_pCore( pCore ), m_oListenerConf( oConf )
Jonas Stienen's avatar
Jonas Stienen committed
225
226
227
228
229
230
231
232
233
234
235
236
237
	{
	};

	CVAPoolObject* CreatePoolObject()
	{
		CVABinauralFreeFieldAudioRenderer::CVABFFListener * pListener;
		pListener = new CVABinauralFreeFieldAudioRenderer::CVABFFListener( m_pCore, m_oListenerConf );
		return pListener;
	};

private:
	CVACoreImpl* m_pCore;
	const CVABinauralFreeFieldAudioRenderer::CVABFFListener::Config& m_oListenerConf;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
238
239
240

	//! Not for use, avoid C4512
	inline CVABFFListenerPoolFactory operator=( const CVABFFListenerPoolFactory & ) { VA_EXCEPT_NOT_IMPLEMENTED; };
Jonas Stienen's avatar
Jonas Stienen committed
241
242
243
244
245
};

class CVABFFSourcePoolFactory : public IVAPoolObjectFactory
{
public:
246
	CVABFFSourcePoolFactory( const CVABinauralFreeFieldAudioRenderer::CVABFFSource::Config& oConf )
Jonas Stienen's avatar
Jonas Stienen committed
247
248
249
250
251
252
253
254
255
256
257
258
259
		: m_oSourceConf( oConf )
	{
	};

	CVAPoolObject* CreatePoolObject()
	{
		CVABinauralFreeFieldAudioRenderer::CVABFFSource * pSource;
		pSource = new CVABinauralFreeFieldAudioRenderer::CVABFFSource( m_oSourceConf );
		return pSource;
	};

private:
	const CVABinauralFreeFieldAudioRenderer::CVABFFSource::Config& m_oSourceConf;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
260
261
262

	//! Not for use, avoid C4512
	inline CVABFFSourcePoolFactory operator=( const CVABFFSourcePoolFactory & ) { VA_EXCEPT_NOT_IMPLEMENTED; };
Jonas Stienen's avatar
Jonas Stienen committed
263
264
265
266
267
};

// Renderer

CVABinauralFreeFieldAudioRenderer::CVABinauralFreeFieldAudioRenderer( const CVAAudioRendererInitParams& oParams )
268
269
270
271
272
273
274
275
276
	: ITADatasourceRealization( 2, oParams.pCore->GetCoreConfig()->oAudioDriverConfig.dSampleRate, oParams.pCore->GetCoreConfig()->oAudioDriverConfig.iBuffersize )
	, CVAObject( oParams.sClass + ":" + oParams.sID )
	, m_pCore( oParams.pCore )
	, m_pCurSceneState( nullptr )
	, m_bDumpListeners( false )
	, m_dDumpListenersGain( 1.0 )
	, m_iHRIRFilterLength( -1 )
	, m_dAdditionalStaticDelaySeconds( 0.0f )
	, m_oParams( oParams )
Jonas Stienen's avatar
Jonas Stienen committed
277
278
279
280
281
282
283
284
285
286
{
	// read config
	Init( *oParams.pConfig );

	IVAPoolObjectFactory* pListenerFactory = new CVABFFListenerPoolFactory( m_pCore, m_oDefaultListenerConf );
	m_pListenerPool = IVAObjectPool::Create( 16, 2, pListenerFactory, true );

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

287
	m_pSoundPathFactory = new CVABFFSoundPathFactory( GetSampleRate(), GetBlocklength(), m_iHRIRFilterLength, 128 );
Jonas Stienen's avatar
Jonas Stienen committed
288
289
290

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

291
	m_pUpdateMessagePool = IVAObjectPool::Create( 2, 1, new CVAPoolObjectDefaultFactory<CVABFFUpdateMessage>, true );
Jonas Stienen's avatar
Jonas Stienen committed
292

293
294
	ctxAudio.m_sbTempL.Init( GetBlocklength(), true );
	ctxAudio.m_sbTempR.Init( GetBlocklength(), true );
Jonas Stienen's avatar
Jonas Stienen committed
295
296
297
	ctxAudio.m_iResetFlag = 0; // Normal operation mode
	ctxAudio.m_iStatus = 0; // Stopped

298
	m_iCurGlobalAuralizationMode = IVAInterface::VA_AURAMODE_DEFAULT;
Jonas Stienen's avatar
Jonas Stienen committed
299
300
301
302
303

	// Register the renderer as a module
	oParams.pCore->RegisterModule( this );
}

304
CVABinauralFreeFieldAudioRenderer::~CVABinauralFreeFieldAudioRenderer()
Jonas Stienen's avatar
Jonas Stienen committed
305
306
307
308
309
310
311
312
{
	delete m_pSoundPathPool;
	delete m_pUpdateMessagePool;
}

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

Jonas Stienen's avatar
Jonas Stienen committed
314
	std::string sVLDInterpolationAlgorithm;
315
	conf.OptString( "SwitchingAlgorithm", sVLDInterpolationAlgorithm, "linear" );
Jonas Stienen's avatar
Jonas Stienen committed
316
317
318
	sVLDInterpolationAlgorithm = toLowercase( sVLDInterpolationAlgorithm );

	if( sVLDInterpolationAlgorithm == "switch" )
319
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::SWITCH;
Jonas Stienen's avatar
Jonas Stienen committed
320
	else if( sVLDInterpolationAlgorithm == "crossfade" )
321
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::CROSSFADE;
Jonas Stienen's avatar
Jonas Stienen committed
322
	else if( sVLDInterpolationAlgorithm == "linear" )
323
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::LINEAR_INTERPOLATION;
Jonas Stienen's avatar
Jonas Stienen committed
324
	else if( sVLDInterpolationAlgorithm == "cubicspline" )
325
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::CUBIC_SPLINE_INTERPOLATION;
Jonas Stienen's avatar
Jonas Stienen committed
326
	else if( sVLDInterpolationAlgorithm == "windowedsinc" )
327
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::WINDOWED_SINC_INTERPOLATION;
Jonas Stienen's avatar
Jonas Stienen committed
328
329
330
331
332
	else
		ITA_EXCEPT1( INVALID_PARAMETER, "Unrecognized interpolation algorithm '" + sVLDInterpolationAlgorithm + "' in BinauralFreefieldAudioRendererConfig" );

	conf.OptInteger( "HRIRFilterLength", m_iHRIRFilterLength, 256 );

333
334
	conf.OptNumber( "AdditionalStaticDelaySeconds", m_dAdditionalStaticDelaySeconds, 0.0f );

Jonas Stienen's avatar
Jonas Stienen committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
	// 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?)" );

	conf.OptBool( "MotionModelLogInputListener", m_oDefaultListenerConf.bMotionModelLogInputEnabled, false );
	conf.OptBool( "MotionModelLogEstimatedOutputListener", m_oDefaultListenerConf.bMotionModelLogEstimatedEnabled, false );

	// 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, 0.1f );
	conf.OptNumber( "MotionModelWindowDelay", m_oDefaultSourceConf.dMotionModelWindowDelay, 0.1f );

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

	conf.OptBool( "MotionModelLogInputSources", m_oDefaultSourceConf.bMotionModelLogInputEnabled, false );
	conf.OptBool( "MotionModelLogEstimatedOutputSources", m_oDefaultSourceConf.bMotionModelLogEstimatedEnabled, false );

	return;
}

368
void CVABinauralFreeFieldAudioRenderer::Reset()
Jonas Stienen's avatar
Jonas Stienen committed
369
{
370
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Received reset call, indicating reset now" );
Jonas Stienen's avatar
Jonas Stienen committed
371
372
	ctxAudio.m_iResetFlag = 1; // Request reset

373
	if( ctxAudio.m_iStatus == 0 || m_oParams.bOfflineRendering )
Jonas Stienen's avatar
Jonas Stienen committed
374
	{
375
		VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Was not streaming, will reset manually" );
Jonas Stienen's avatar
Jonas Stienen committed
376
377
378
379
		// if no streaming active, reset manually
		//SyncInternalData();
		ResetInternalData();
	}
380
381
	else
	{
382
		VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Still streaming, will now wait for reset acknownledge" );
383
	}
Jonas Stienen's avatar
Jonas Stienen committed
384
385
386
387
388
389

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

391
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Operation reset has green light, clearing items" );
392

Jonas Stienen's avatar
Jonas Stienen committed
393
394
395
396
397
	// Iterate over sound pathes and free items
	std::list< CVABFFSoundPath* >::iterator it = m_lSoundPaths.begin();
	while( it != m_lSoundPaths.end() )
	{
		CVABFFSoundPath* pPath = *it;
398

Jonas Stienen's avatar
Jonas Stienen committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
		int iNumRefs = pPath->GetNumReferences();
		assert( iNumRefs == 1 );
		pPath->RemoveReference();

		++it;
	}
	m_lSoundPaths.clear();

	// Iterate over listener and free items
	std::map< int, CVABFFListener* >::const_iterator lcit = m_mListeners.begin();
	while( lcit != m_mListeners.end() )
	{
		CVABFFListener* pListener( lcit->second );
		pListener->pData->RemoveReference();
		assert( pListener->GetNumReferences() == 1 );
		pListener->RemoveReference();
		lcit++;
	}
	m_mListeners.clear();

	// Iterate over sources and free items
	std::map< int, CVABFFSource* >::const_iterator scit = m_mSources.begin();
	while( scit != m_mSources.end() )
	{
		CVABFFSource* 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
439

440
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Reset successful" );
Jonas Stienen's avatar
Jonas Stienen committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
}

void CVABinauralFreeFieldAudioRenderer::UpdateScene( CVASceneState* pNewSceneState )
{
	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< CVABFFUpdateMessage* >( m_pUpdateMessagePool->RequestObject() );

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

Jonas Stienen's avatar
Jonas Stienen committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
	// 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* CVABinauralFreeFieldAudioRenderer::GetOutputDatasource()
{
	return this;
}

void CVABinauralFreeFieldAudioRenderer::ManageSoundPaths( const CVASceneState* pCurScene,
485
486
	const CVASceneState* pNewScene,
	const CVASceneStateDiff* pDiff )
Jonas Stienen's avatar
Jonas Stienen committed
487
488
489
490
491
{
	// Warning: take care for explicit sources and listeners for this renderer!

	// Iterate over current paths and mark deleted (will be removed within internal sync of audio context thread)
	std::list< CVABFFSoundPath* >::iterator itp = m_lSoundPaths.begin();
492
	while( itp != m_lSoundPaths.end() )
Jonas Stienen's avatar
Jonas Stienen committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
	{
		CVABFFSoundPath* pPath( *itp );
		int iSourceID = pPath->pSource->pData->iID;
		int iListenerID = pPath->pListener->pData->iID;
		bool bDeletetionRequired = false;

		// Source deleted?
		std::vector< int >::const_iterator cits = pDiff->viDelSoundSourceIDs.begin();
		while( cits != pDiff->viDelSoundSourceIDs.end() )
		{
			const int& iIDDeletedSource( *cits++ );
			if( iSourceID == iIDDeletedSource )
			{
				bDeletetionRequired = true; // Source removed, deletion required
				break;
			}
		}

		if( bDeletetionRequired == false )
		{
			// Receiver deleted?
514
515
			std::vector< int >::const_iterator citr = pDiff->viDelReceiverIDs.begin();
			while( citr != pDiff->viDelReceiverIDs.end() )
Jonas Stienen's avatar
Jonas Stienen committed
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
			{
				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++ );
		DeleteSource( iID );
	}

	// Deleted receivers
546
547
	std::vector< int >::const_iterator citr = pDiff->viDelReceiverIDs.begin();
	while( citr != pDiff->viDelReceiverIDs.end() )
Jonas Stienen's avatar
Jonas Stienen committed
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
	{
		const int& iID( *citr++ );
		DeleteListener( iID );
	}

	// New sources
	cits = pDiff->viNewSoundSourceIDs.begin();
	while( cits != pDiff->viNewSoundSourceIDs.end() )
	{
		const int& iID( *cits++ );

		// Only add, if no other renderer has been connected explicitly with this source
		const CVASoundSourceDesc* pSoundSourceDesc = m_pCore->GetSceneManager()->GetSoundSourceDesc( iID );
		if( pSoundSourceDesc->sExplicitRendererID.empty() || pSoundSourceDesc->sExplicitRendererID == m_oParams.sID )
			CVABFFSource* pSource = CreateSource( iID, pNewScene->GetSoundSourceState( iID ) );
	}

	// New receivers
566
567
	citr = pDiff->viNewReceiverIDs.begin();
	while( citr != pDiff->viNewReceiverIDs.end() )
Jonas Stienen's avatar
Jonas Stienen committed
568
569
	{
		const int& iID( *citr++ );
570
		CVABFFListener* pListener = CreateListener( iID, pNewScene->GetReceiverState( iID ) );
Jonas Stienen's avatar
Jonas Stienen committed
571
572
573
	}

	// New paths: (1) new receivers, current sources
574
575
	citr = pDiff->viNewReceiverIDs.begin();
	while( citr != pDiff->viNewReceiverIDs.end() )
Jonas Stienen's avatar
Jonas Stienen committed
576
577
	{
		int iListenerID = ( *citr++ );
578
		CVABFFListener* pListener = m_mListeners[ iListenerID ];
Jonas Stienen's avatar
Jonas Stienen committed
579

580
		for( size_t i = 0; i < pDiff->viComSoundSourceIDs.size(); i++ )
Jonas Stienen's avatar
Jonas Stienen committed
581
582
583
		{
			// Only add, if no other renderer has been connected explicitly with this source
			// and only, if not marked for deletion
584
			int iSourceID = pDiff->viComSoundSourceIDs[ i ];
Jonas Stienen's avatar
Jonas Stienen committed
585
586
587
588
589
590
591
			std::map< int, CVABFFSource* >::iterator it = m_mSources.find( iSourceID );
			if( it == m_mSources.end() )
				continue; // This source is skipped by the renderer
			CVABFFSource* pSource = it->second;

			const CVASoundSourceDesc* pSoundSourceDesc = m_pCore->GetSceneManager()->GetSoundSourceDesc( iSourceID );
			if( !pSource->bDeleted &&
592
				( pSoundSourceDesc->sExplicitRendererID.empty() || pSoundSourceDesc->sExplicitRendererID == m_oParams.sID ) )
Jonas Stienen's avatar
Jonas Stienen committed
593
594
595
596
597
598
				CVABFFSoundPath* pPath = CreateSoundPath( pSource, pListener );
		}
	}

	// New paths: (2) new sources, current receivers
	cits = pDiff->viNewSoundSourceIDs.begin();
599
	while( cits != pDiff->viNewSoundSourceIDs.end() )
Jonas Stienen's avatar
Jonas Stienen committed
600
601
602
603
604
	{
		const int& iSourceID( *cits++ );
		std::map< int, CVABFFSource* >::iterator it = m_mSources.find( iSourceID );
		if( it == m_mSources.end() )
			continue; // Explicit source is not connected to this renderer
605

Jonas Stienen's avatar
Jonas Stienen committed
606
		CVABFFSource* pSource = it->second;
607
		for( size_t i = 0; i < pDiff->viComReceiverIDs.size(); i++ )
Jonas Stienen's avatar
Jonas Stienen committed
608
		{
609
			int iListenerID = pDiff->viComReceiverIDs[ i ];
610
			CVABFFListener* pListener = m_mListeners[ iListenerID ];
Jonas Stienen's avatar
Jonas Stienen committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
			if( !pListener->bDeleted )
				CVABFFSoundPath* pPath = CreateSoundPath( pSource, pListener );
		}
	}

	// New paths: (3) new sources, new receivers
	cits = pDiff->viNewSoundSourceIDs.begin();
	while( cits != pDiff->viNewSoundSourceIDs.end() )
	{
		const int& iSourceID( *cits++ );
		std::map< int, CVABFFSource* >::iterator it = m_mSources.find( iSourceID );

		if( it == m_mSources.end() )
			continue;

		CVABFFSource* pSource = it->second;
		assert( pSource );

629
630
		citr = pDiff->viNewReceiverIDs.begin();
		while( citr != pDiff->viNewReceiverIDs.end() )
Jonas Stienen's avatar
Jonas Stienen committed
631
632
		{
			const int& iListenerID( *citr++ );
633
			CVABFFListener* pListener = m_mListeners[ iListenerID ];
Jonas Stienen's avatar
Jonas Stienen committed
634
635
636
			CVABFFSoundPath* pPath = CreateSoundPath( pSource, pListener );
		}
	}
637

Jonas Stienen's avatar
Jonas Stienen committed
638
639
640
641
642
	return;
}

void CVABinauralFreeFieldAudioRenderer::ProcessStream( const ITAStreamInfo* pStreamInfo )
{
643
644
645
646
647
	if( ctxAudio.m_iStatus == 0 )
	{
		// If streaming is active, set to 1
		ctxAudio.m_iStatus = 1;
	}
Jonas Stienen's avatar
Jonas Stienen committed
648
649
650

	// Schallpfade abgleichen
	SyncInternalData();
651

Jonas Stienen's avatar
Jonas Stienen committed
652
653
654
655
656
657
658
659
	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;
660

Jonas Stienen's avatar
Jonas Stienen committed
661
662
663
	// Check for reset request
	if( ctxAudio.m_iResetFlag == 1 )
	{
664
		VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Process stream detecting reset request, will reset internally now" );
Jonas Stienen's avatar
Jonas Stienen committed
665
666
667
668
669
670
		ResetInternalData();

		return;
	}
	else if( ctxAudio.m_iResetFlag == 2 )
	{
671
		VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Process stream detecting ongoing reset, will stop processing here" );
672

Jonas Stienen's avatar
Jonas Stienen committed
673
674
675
676
677
		return;
	}

	SampleTrajectoriesInternal( dListenerTime );

678
679
	std::list< CVABFFListener* >::iterator lit = ctxAudio.m_lListeners.begin();
	while( lit != ctxAudio.m_lListeners.end() )
Jonas Stienen's avatar
Jonas Stienen committed
680
	{
681
682
		CVABFFListener* pListener( *( lit++ ) );
		pListener->psfOutput->zero();
Jonas Stienen's avatar
Jonas Stienen committed
683
684
685
686
687
688
689
	}

	// Update sound pathes
	std::list< CVABFFSoundPath* >::iterator spit = ctxAudio.m_lSoundPaths.begin();
	while( spit != ctxAudio.m_lSoundPaths.end() )
	{
		CVABFFSoundPath* pPath( *spit );
690
		CVAReceiverState* pListenerState = ( m_pCurSceneState ? m_pCurSceneState->GetReceiverState( pPath->pListener->pData->iID ) : NULL );
691
		CVASoundSourceState* pSourceState = ( m_pCurSceneState ? m_pCurSceneState->GetSoundSourceState( pPath->pSource->pData->iID ) : NULL );
Jonas Stienen's avatar
Jonas Stienen committed
692
693
694
695
696
697
698
699

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

700
		if( !pPath->pListener->bValidTrajectoryPresent || !pPath->pSource->bValidTrajectoryPresent )
Jonas Stienen's avatar
Jonas Stienen committed
701
702
703
704
705
706
707
708
709
710
711
		{
			// Skip if no valid trajectory data is present
			spit++;
			continue;
		}

		// --= Parameter update =--

		pPath->UpdateMetrics();

		// VDL Doppler shift settings
712
713
714
715
716
		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;
		bool bDPEnabledCurrent = ( pPath->pVariableDelayLineChL->GetAlgorithm() != CITAVariableDelayLine::SWITCH ); // switch = disabled
		bool bDPStatusChanged = ( bDPEnabledCurrent != ( bDPEnabledGlobal && bDPEnabledListener && bDPEnabledSource ) );
Jonas Stienen's avatar
Jonas Stienen committed
717
		if( bDPStatusChanged )
718
		{
719
720
			pPath->pVariableDelayLineChL->SetAlgorithm( !bDPEnabledCurrent ? m_iDefaultVDLSwitchingAlgorithm : CITAVariableDelayLine::SWITCH );
			pPath->pVariableDelayLineChR->SetAlgorithm( !bDPEnabledCurrent ? m_iDefaultVDLSwitchingAlgorithm : CITAVariableDelayLine::SWITCH );
721
		}
722
		pPath->UpdateMediumPropagation( m_pCore->oHomogeneousMedium.dSoundSpeed, m_dAdditionalStaticDelaySeconds );
Jonas Stienen's avatar
Jonas Stienen committed
723
724

		// Spherical spreading loss
725
726
727
728
		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 );
729
730
731
		double dDistanceDecrease = pPath->CalculateInverseDistanceDecrease( m_pCore->GetCoreConfig()->dDefaultMinimumDistance );
		if( bSSLEnabled == false )
			dDistanceDecrease = m_pCore->GetCoreConfig()->dDefaultDistance;
Jonas Stienen's avatar
Jonas Stienen committed
732

733
734
735
736
		bool bDIREnabledSource = ( pSourceState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_SOURCE_DIRECTIVITY ) > 0;
		bool bDIREnabledListener = ( pListenerState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_SOURCE_DIRECTIVITY ) > 0;
		bool bDIREnabledGlobal = ( m_iCurGlobalAuralizationMode & IVAInterface::VA_AURAMODE_SOURCE_DIRECTIVITY ) > 0;
		bool bDIREnabled = ( bDIREnabledSource && bDIREnabledListener && bDIREnabledGlobal );
Jonas Stienen's avatar
Jonas Stienen committed
737
738
		pPath->UpdateDir( bDIREnabled );

739
		// HATO
740
741
		const VistaQuaternion qHATO( pListenerState->GetMotionState()->GetHeadAboveTorsoOrientation().comp );
		const double dHATODeg = rad2grad( qHATO.GetAngles().b );
742
		pPath->UpdateHRIR( pListenerState->GetAnthropometricData(), dHATODeg );
Jonas Stienen's avatar
Jonas Stienen committed
743
744

		// Sound source gain / direct sound audibility via AuraMode flags
745
746
747
		bool bDSSourceStatusEnabled = ( pSourceState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_DIRECT_SOUND );
		bool bDSListenerStatusEnabled = ( pListenerState->GetAuralizationMode() & IVAInterface::VA_AURAMODE_DIRECT_SOUND );
		bool bDSGlobalStatusEnabled = ( m_iCurGlobalAuralizationMode & IVAInterface::VA_AURAMODE_DIRECT_SOUND );
Jonas Stienen's avatar
Jonas Stienen committed
748
749
		bool bDSEnabled = bDSSourceStatusEnabled && bDSListenerStatusEnabled && bDSGlobalStatusEnabled;

750
		float fSoundSourceGain = float( dDistanceDecrease * pSourceState->GetVolume( m_oParams.pCore->GetCoreConfig()->dDefaultAmplitudeCalibration ) );
Jonas Stienen's avatar
Jonas Stienen committed
751
752
		if( pPath->pSource->pData->bMuted || ( bDSEnabled == false ) )
			fSoundSourceGain = 0.0f;
753
754
		pPath->pFIRConvolverChL->SetGain( fSoundSourceGain );
		pPath->pFIRConvolverChR->SetGain( fSoundSourceGain );
Jonas Stienen's avatar
Jonas Stienen committed
755
756
757
758
759
760

		// --= DSP =--

		CVASoundSourceDesc* pSourceData = pPath->pSource->pData;
		const ITASampleBuffer* psbInput = pSourceData->pSignalSourceInputBuf;
		assert( psbInput ); // Knallt es hier, dann ist die Eingabequelle noch nicht gesetzt
761
		assert( pSourceData->iID >= 0 ); // Knallt es hier, dann wurde dem SoundPath unterm hintern die Quelle entzogen! -> Problem mit Referenzierung und Reset?
Jonas Stienen's avatar
Jonas Stienen committed
762

763
		pPath->pThirdOctaveFilterBank->Process( psbInput->data(), ctxAudio.m_sbTempL.data() );
764
765
		pPath->pVariableDelayLineChR->Process( &( ctxAudio.m_sbTempL ), &( ctxAudio.m_sbTempR ) );
		pPath->pVariableDelayLineChL->Process( &( ctxAudio.m_sbTempL ), &( ctxAudio.m_sbTempL ) ); // inplace
766
767
		pPath->pFIRConvolverChL->Process( ctxAudio.m_sbTempL.data(), ( *pPath->pListener->psfOutput )[ 0 ].data(), ITAUPConvolution::OUTPUT_MIX );
		pPath->pFIRConvolverChR->Process( ctxAudio.m_sbTempR.data(), ( *pPath->pListener->psfOutput )[ 1 ].data(), ITAUPConvolution::OUTPUT_MIX );
Jonas Stienen's avatar
Jonas Stienen committed
768
769
770
771
772

		spit++;
	}

	// TODO: Select active listener
773
	if( !ctxAudio.m_lListeners.empty() )
Jonas Stienen's avatar
Jonas Stienen committed
774
	{
775
776
777
		CVABFFListener* pActiveListener = *( ctxAudio.m_lListeners.begin() );
		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
778

779
780
		// Listener dumping
		if( m_iDumpListenersFlag > 0 )
Jonas Stienen's avatar
Jonas Stienen committed
781
		{
782
783
784
785
786
787
788
			std::map< int, CVABFFListener* >::iterator it = m_mListeners.begin();
			while( it != m_mListeners.end() )
			{
				CVABFFListener* pListener = it++->second;
				pListener->psfOutput->mul_scalar( float( m_dDumpListenersGain ) );
				pListener->pListenerOutputAudioFileWriter->write( pListener->psfOutput );
			}
Jonas Stienen's avatar
Jonas Stienen committed
789

790
791
792
793
			// Ack on dump stop
			if( m_iDumpListenersFlag == 2 )
				m_iDumpListenersFlag = 0;
		}
Jonas Stienen's avatar
Jonas Stienen committed
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
	}

	IncrementWritePointer();

	return;
}

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

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

Jonas Stienen's avatar
Jonas Stienen committed
812
813
814
815
816
		const CVAMotionState* pMotionCur = ( pSourceCur ? pSourceCur->GetMotionState() : nullptr );
		const CVAMotionState* pMotionNew = ( pSourceNew ? pSourceNew->GetMotionState() : nullptr );

		if( pMotionNew && ( pMotionNew != pMotionCur ) )
		{
817
			VA_TRACE( "BinauralFreeFieldAudioRenderer", "Source " << iSourceID << " new motion state" );
Jonas Stienen's avatar
Jonas Stienen committed
818
819
820
821
822
823
824
825
826
827
			pSource->pMotionModel->InputMotionKey( pMotionNew );
		}
	}

	// Neue Hrerdaten bernehmen
	for( std::map< int, CVABFFListener* >::iterator it = m_mListeners.begin(); it != m_mListeners.end(); ++it )
	{
		int iListenerID = it->first;
		CVABFFListener* pListener = it->second;

828
829
		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
830
831
832
833
834
835

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

		if( pMotionNew && ( pMotionNew != pMotionCur ) )
		{
836
			VA_TRACE( "BinauralFreeFieldAudioRenderer", "Listener " << iListenerID << " new position " );// << *pMotionNew);
Jonas Stienen's avatar
Jonas Stienen committed
837
838
839
840
841
842
843
844
			pListener->pMotionModel->InputMotionKey( pMotionNew );
		}
	}
}

void CVABinauralFreeFieldAudioRenderer::SampleTrajectoriesInternal( double dTime )
{
	bool bValid = true;
845
	for( std::list< CVABFFSource* >::iterator it = ctxAudio.m_lSources.begin(); it != ctxAudio.m_lSources.end(); ++it )
Jonas Stienen's avatar
Jonas Stienen committed
846
847
848
849
	{
		CVABFFSource* pSource = *it;

		pSource->pMotionModel->HandleMotionKeys();
850
851
		bValid &= pSource->pMotionModel->EstimatePosition( dTime, pSource->vPredPos );
		bValid &= pSource->pMotionModel->EstimateOrientation( dTime, pSource->vPredView, pSource->vPredUp );
Jonas Stienen's avatar
Jonas Stienen committed
852
853
854
855
		pSource->bValidTrajectoryPresent = bValid;
	}

	bValid = true;
856
	for( std::list< CVABFFListener* >::iterator it = ctxAudio.m_lListeners.begin(); it != ctxAudio.m_lListeners.end(); ++it )
Jonas Stienen's avatar
Jonas Stienen committed
857
858
859
860
861
862
863
864
865
866
	{
		CVABFFListener* pListener = *it;

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

867
CVABFFSoundPath* CVABinauralFreeFieldAudioRenderer::CreateSoundPath( CVABFFSource* pSource, CVABFFListener* pListener )
Jonas Stienen's avatar
Jonas Stienen committed
868
869
870
871
872
873
{
	int iSourceID = pSource->pData->iID;
	int iListenerID = pListener->pData->iID;

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

874
875
876
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Creating sound path from source " << iSourceID << " -> listener " << iListenerID );

	CVABFFSoundPath* pPath = dynamic_cast< CVABFFSoundPath* >( m_pSoundPathPool->RequestObject() );
Jonas Stienen's avatar
Jonas Stienen committed
877
878
879
880
881

	pPath->pSource = pSource;
	pPath->pListener = pListener;

	pPath->bDelete = false;
882
883

	CVASoundSourceState* pSourceNew = ( m_pNewSceneState ? m_pNewSceneState->GetSoundSourceState( iSourceID ) : nullptr );
Jonas Stienen's avatar
Jonas Stienen committed
884
	if( pSourceNew != nullptr )
885
		pPath->oDirectivityStateNew.pData = ( IVADirectivity* ) pSourceNew->GetDirectivityData();
Jonas Stienen's avatar
Jonas Stienen committed
886

887
	CVAReceiverState* pListenerNew = ( m_pNewSceneState ? m_pNewSceneState->GetReceiverState( iListenerID ) : nullptr );
Jonas Stienen's avatar
Jonas Stienen committed
888
	if( pListenerNew != nullptr )
889
		pPath->oHRIRStateNew.pData = ( IVADirectivity* ) pListenerNew->GetDirectivity();
Jonas Stienen's avatar
Jonas Stienen committed
890

891
892
893
894
895
	// Configure DSP elements
	pPath->pVariableDelayLineChL->SetAlgorithm( m_iDefaultVDLSwitchingAlgorithm );
	pPath->pVariableDelayLineChR->SetAlgorithm( m_iDefaultVDLSwitchingAlgorithm );


Jonas Stienen's avatar
Jonas Stienen committed
896
897
898
899
900
901
902
903
	m_lSoundPaths.push_back( pPath );
	m_pUpdateMessage->vNewPaths.push_back( pPath );

	return pPath;
}

void CVABinauralFreeFieldAudioRenderer::DeleteSoundPath( CVABFFSoundPath* pPath )
{
904
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Marking sound path from source " << pPath->pSource->pData->iID << " -> listener " << pPath->pListener->pData->iID << " for deletion" );
Jonas Stienen's avatar
Jonas Stienen committed
905
906
907
908
909
910

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

911
CVABinauralFreeFieldAudioRenderer::CVABFFListener* CVABinauralFreeFieldAudioRenderer::CreateListener( const int iID, const CVAReceiverState* pListenerState )
Jonas Stienen's avatar
Jonas Stienen committed
912
{
913
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Creating listener with ID " << iID );
Jonas Stienen's avatar
Jonas Stienen committed
914

915
	CVABFFListener* pListener = dynamic_cast< CVABFFListener* >( m_pListenerPool->RequestObject() ); // Reference = 1
Jonas Stienen's avatar
Jonas Stienen committed
916
917
918
919
920
921

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

	// Move to prerequest of pool?
	pListener->psfOutput = new ITASampleFrame( 2,
922
923
		m_pCore->GetCoreConfig()->oAudioDriverConfig.iBuffersize,
		true );
Jonas Stienen's avatar
Jonas Stienen committed
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
	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();

	m_mListeners.insert( std::pair< int, CVABinauralFreeFieldAudioRenderer::CVABFFListener* >( iID, pListener ) );

	m_pUpdateMessage->vNewListeners.push_back( pListener );

	return pListener;
}

void CVABinauralFreeFieldAudioRenderer::DeleteListener( int iListenerID )
{
941
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Marking listener with ID " << iListenerID << " for removal" );
Jonas Stienen's avatar
Jonas Stienen committed
942
943
944
945
946
947
948
949
950
951
952
953
954
955
	std::map< int, CVABFFListener* >::iterator it = m_mListeners.find( iListenerID );
	CVABFFListener* pListener = it->second;
	m_mListeners.erase( it );
	pListener->bDeleted = true;
	pListener->pData->RemoveReference();
	pListener->RemoveReference();

	m_pUpdateMessage->vDelListeners.push_back( pListener );

	return;
}

CVABinauralFreeFieldAudioRenderer::CVABFFSource* CVABinauralFreeFieldAudioRenderer::CreateSource( int iID, const CVASoundSourceState* pSourceState )
{
956
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Creating source with ID " << iID );
Jonas Stienen's avatar
Jonas Stienen committed
957
	CVABFFSource* pSource = dynamic_cast< CVABFFSource* >( m_pSourcePool->RequestObject() );
958
959

	pSource->pData = m_pCore->GetSceneManager()->GetSoundSourceDesc( iID );
Jonas Stienen's avatar
Jonas Stienen committed
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
	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, CVABFFSource* >( iID, pSource ) );

	m_pUpdateMessage->vNewSources.push_back( pSource );

	return pSource;
}

void CVABinauralFreeFieldAudioRenderer::DeleteSource( int iSourceID )
{
977
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Marking source with ID " << iSourceID << " for removal" );
Jonas Stienen's avatar
Jonas Stienen committed
978
	std::map< int, CVABFFSource* >::iterator it = m_mSources.find( iSourceID );
979

Jonas Stienen's avatar
Jonas Stienen committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
	if( it == m_mSources.end() ) // Not found in internal list ...
	{
		CVASoundSourceDesc* pDesc = m_pCore->GetSceneManager()->GetSoundSourceDesc( iSourceID );
		if( !pDesc->sExplicitRendererID.empty() || pDesc->sExplicitRendererID == GetObjectName() )
			VA_WARN( "BinauralFreeFieldAudioRenderer", "Attempted to remote an explicit sound source for this renderer which could not be found." );
		return;
	}

	CVABFFSource* pSource = it->second;
	m_mSources.erase( it );
	pSource->bDeleted = true;
	pSource->pData->RemoveReference();
	pSource->RemoveReference();

	m_pUpdateMessage->vDelSources.push_back( pSource );
995

Jonas Stienen's avatar
Jonas Stienen committed
996
997
998
999
1000
	return;
}

void CVABinauralFreeFieldAudioRenderer::SyncInternalData()
{
For faster browsing, not all history is shown. View entire blame