VABinauralFreeFieldAudioRenderer.cpp 49 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1
2
3
// $Id:  $
#include "VABinauralFreefieldAudioRenderer.h"

4
#if ( VACORE_WITH_RENDERER_BINAURAL_FREE_FIELD == 1 )
Jonas Stienen's avatar
Jonas Stienen committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

// VA includes
#include "../../../Data/VADirectivity.h"
#include "../../../Data/VAHRIRDataset.h"
#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 <VACore.h>
#include "../../../VACoreImpl.h"
#include "../../../VACoreConfig.h"
#include "../../../VALog.h"
#include <VAObjectPool.h>
#include <VAReferenceableObject.h>

// ITA includes
#include <DAFF.h>
25
26
27
#include <ITAUPConvolution.h>
#include <ITAUPFilter.h>
#include <ITAUPFilterPool.h>
Jonas Stienen's avatar
Jonas Stienen committed
28
29
30
#include <ITAAtomicPrimitives.h>
#include <ITAClock.h>
#include <ITACriticalSection.h>
31
#include <ITADataSourceRealization.h>
32
33
#include <ITAThirdOctaveFilterbank.h>
#include <ITAVariableDelayLine.h>
Jonas Stienen's avatar
Jonas Stienen committed
34
#include <ITASampleBuffer.h>
Jonas Stienen's avatar
Jonas Stienen committed
35
#include <ITAStopWatch.h>
Jonas Stienen's avatar
Jonas Stienen committed
36
37
38
#include <ITASampleFrame.h>
#include <ITAStreamInfo.h>
#include <ITAFastMath.h>
39
#include <ITAConstants.h>
40
#include <ITANumericUtils.h>
Jonas Stienen's avatar
Jonas Stienen committed
41
42
43
44
45

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

// STL includes
46
#include <algorithm>
47
#include <cassert>
Jonas Stienen's avatar
Jonas Stienen committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#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
		VAOrientYPR yprAngleRetSourceToListener; //!< Immediate angle of incidence to retarded source position in listener reference frame in YPR convention
		VAOrientYPR yprAngleListenerToRetSource; //!< Retarded angle of incidence to listener in source reference frame in YPR convention
	};

	//! State of directivity
	class CDirectivityState
	{
	public:
		CDirectivityState()
		: pData( NULL )
		, iRecord( -1 )
		, bDirectivityEnabled( true ) {}
		
		IVADirectivity* pData;	//!< Directivity data, may be NULL
		int iRecord;			//!< Directivity index
		bool bDirectivityEnabled;

		bool operator==( const CDirectivityState& rhs ) const
		{
			bool bBothEnabled = ( bDirectivityEnabled == rhs.bDirectivityEnabled );
			bool bSameRecordIndex = ( iRecord == rhs.iRecord );
			bool bSameData = ( pData == rhs.pData );

			return ( bBothEnabled && bSameRecordIndex && bSameData );
		}
	};

	class CHRIRState
	{
	public:
		CHRIRState() 
		: pData( NULL )
		, iRecord( -1 )
		, fDistance( 1.0f ) {}

		IVAHRIRDataset* pData;	 //!< HRIR data, may be NULL
		int iRecord;			 //!< HRIR index
		float fDistance;		 //!< HRIR dataset distance

		bool operator!=(const CHRIRState& rhs) const
		{
			if( pData != rhs.pData ) return true;
			if( fDistance != rhs.fDistance ) return true;			
			if( iRecord != rhs.iRecord ) return true;
			return false;
		}
	};

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

	CVASourceListenerMetrics oRelations; //!< Informatioen on source and receiver relations (distances & angles)

	CDirectivityState oDirectivityStateCur;
	CDirectivityState oDirectivityStateNew;

	CHRIRState oHRIRStateCur;
	CHRIRState oHRIRStateNew;

	ITAAtomicBool bDelete;
		
126
127
128
	CITAThirdOctaveFilterbank* pThirdOctaveFilterBank;
	CITAVariableDelayLine* pVariableDelayLineChL;
	CITAVariableDelayLine* pVariableDelayLineChR;
129
130
	ITAUPConvolution* pFIRConvolverChL;
	ITAUPConvolution* pFIRConvolverChR;
Jonas Stienen's avatar
Jonas Stienen committed
131
132
133
134
135
136
137
138

	void PreRequest()
	{	
		pSource = nullptr;
		pListener = nullptr;

		// Reset DSP elements
		pThirdOctaveFilterBank->Clear();
139
140
		pVariableDelayLineChL->Clear();
		pVariableDelayLineChR->Clear();
Jonas Stienen's avatar
Jonas Stienen committed
141
142
143
144
145
		pFIRConvolverChL->clear();
		pFIRConvolverChR->clear();
	};

	void UpdateDir( bool bDIRAuraModeEnabled );
146
	void UpdateMediumPropagation( double dSpeedOfSound, double dAdditionalDelaySeconds = 0.0f );
Jonas Stienen's avatar
Jonas Stienen committed
147
	double CalculateInverseDistanceDecrease() const;
148
	void UpdateHRIR(const CVAListenerState::CVAAnthropometricParameter& );
Jonas Stienen's avatar
Jonas Stienen committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
	
	//! 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;
};

class CVABFFSoundPathFactory : public IVAPoolObjectFactory {
public: 
	CVABFFSoundPathFactory( double dSamplerate, int iBlocklength, int iHRIRFilterLength, int iDirFilterLength )
	: m_dSamplerate(dSamplerate)
	, m_iBlocklength(iBlocklength)
	, m_iHRIRFilterLength(iHRIRFilterLength)
	, m_iDirFilterLength(iDirFilterLength)
	{}

	CVAPoolObject* CreatePoolObject()
	{
		return new CVABFFSoundPath( m_dSamplerate, m_iBlocklength, m_iHRIRFilterLength, m_iDirFilterLength );
	}

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:
	CVABFFListenerPoolFactory( CVACoreImpl* pCore, const CVABinauralFreeFieldAudioRenderer::CVABFFListener::Config& oConf  )
	: m_pCore( pCore ), m_oListenerConf( oConf )
	{
	};

	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;
};

class CVABFFSourcePoolFactory : public IVAPoolObjectFactory
{
public:
	CVABFFSourcePoolFactory( const CVABinauralFreeFieldAudioRenderer::CVABFFSource::Config& oConf  )
		: m_oSourceConf( oConf )
	{
	};

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

private:
	const CVABinauralFreeFieldAudioRenderer::CVABFFSource::Config& m_oSourceConf;
};

// Renderer

CVABinauralFreeFieldAudioRenderer::CVABinauralFreeFieldAudioRenderer( const CVAAudioRendererInitParams& oParams )
241
: ITADatasourceRealization( 2, oParams.pCore->GetCoreConfig()->oAudioDriverConfig.dSampleRate, oParams.pCore->GetCoreConfig()->oAudioDriverConfig.iBuffersize )
Jonas Stienen's avatar
Jonas Stienen committed
242
243
244
245
246
247
, CVAObject(oParams.sClass + ":" + oParams.sID)
, m_pCore( oParams.pCore )
, m_pCurSceneState( nullptr )
, m_bDumpListeners( false )
, m_dDumpListenersGain( 1.0 )
, m_iHRIRFilterLength( -1 )
248
, m_dAdditionalStaticDelaySeconds( 0.0f )
Jonas Stienen's avatar
Jonas Stienen committed
249
250
251
252
253
254
255
256
257
258
259
, m_oParams( oParams )
{
	// 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 );

260
	m_pSoundPathFactory = new CVABFFSoundPathFactory( GetSampleRate(), GetBlocklength(), m_iHRIRFilterLength, 128 );
Jonas Stienen's avatar
Jonas Stienen committed
261
262
263
264
265

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

	m_pUpdateMessagePool = IVAObjectPool::Create( 2, 1, new CVAPoolObjectDefaultFactory<CVABFFUpdateMessage>, true);

266
267
	ctxAudio.m_sbTempL.Init( GetBlocklength(), true );
	ctxAudio.m_sbTempR.Init( GetBlocklength(), true );
Jonas Stienen's avatar
Jonas Stienen committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
	ctxAudio.m_iResetFlag = 0; // Normal operation mode
	ctxAudio.m_iStatus = 0; // Stopped

	m_iCurGlobalAuralizationMode = IVACore::VA_AURAMODE_DEFAULT;

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

CVABinauralFreeFieldAudioRenderer::~CVABinauralFreeFieldAudioRenderer() 
{
	delete m_pSoundPathPool;
	delete m_pUpdateMessagePool;
}

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

	conf.OptNumber( "SpeedOfSound", m_dSpeedOfSound, 344.0f );

	std::string sVLDInterpolationAlgorithm;
290
	conf.OptString( "SwitchingAlgorithm", sVLDInterpolationAlgorithm, "linear" );
Jonas Stienen's avatar
Jonas Stienen committed
291
292
293
	sVLDInterpolationAlgorithm = toLowercase( sVLDInterpolationAlgorithm );

	if( sVLDInterpolationAlgorithm == "switch" )
294
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::SWITCH;
Jonas Stienen's avatar
Jonas Stienen committed
295
	else if( sVLDInterpolationAlgorithm == "crossfade" )
296
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::CROSSFADE;
Jonas Stienen's avatar
Jonas Stienen committed
297
	else if( sVLDInterpolationAlgorithm == "linear" )
298
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::LINEAR_INTERPOLATION;
Jonas Stienen's avatar
Jonas Stienen committed
299
	else if( sVLDInterpolationAlgorithm == "cubicspline" )
300
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::CUBIC_SPLINE_INTERPOLATION;
Jonas Stienen's avatar
Jonas Stienen committed
301
	else if( sVLDInterpolationAlgorithm == "windowedsinc" )
302
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::WINDOWED_SINC_INTERPOLATION;
Jonas Stienen's avatar
Jonas Stienen committed
303
304
305
306
307
	else
		ITA_EXCEPT1( INVALID_PARAMETER, "Unrecognized interpolation algorithm '" + sVLDInterpolationAlgorithm + "' in BinauralFreefieldAudioRendererConfig" );

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

308
309
	conf.OptNumber( "AdditionalStaticDelaySeconds", m_dAdditionalStaticDelaySeconds, 0.0f );

Jonas Stienen's avatar
Jonas Stienen committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
	// 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;
}

void CVABinauralFreeFieldAudioRenderer::Reset() 
{
345
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Received reset call, indicating reset now" );
Jonas Stienen's avatar
Jonas Stienen committed
346
347
348
349
	ctxAudio.m_iResetFlag = 1; // Request reset

	if( ctxAudio.m_iStatus == 0 )
	{
350
		VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Was not streaming, will reset manually" );
Jonas Stienen's avatar
Jonas Stienen committed
351
352
353
354
		// if no streaming active, reset manually
		//SyncInternalData();
		ResetInternalData();
	}
355
356
	else
	{
357
		VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Still streaming, will now wait for reset acknownledge" );
358
	}	
Jonas Stienen's avatar
Jonas Stienen committed
359
360
361
362
363
364

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

366
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Operation reset has green light, clearing items" );
Jonas Stienen's avatar
Jonas Stienen committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
409
410
411
412
413
	
	// Iterate over sound pathes and free items
	std::list< CVABFFSoundPath* >::iterator it = m_lSoundPaths.begin();
	while( it != m_lSoundPaths.end() )
	{
		CVABFFSoundPath* pPath = *it;
		
		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
414

415
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Reset successful" );
Jonas Stienen's avatar
Jonas Stienen committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
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
546
547
548
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
}

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 );
	
	// 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,
														  const CVASceneState* pNewScene,
														  const CVASceneStateDiff* pDiff )
{
	// 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();
	while( itp != m_lSoundPaths.end() ) 
	{
		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?
			std::vector< int >::const_iterator citr = pDiff->viDelListenerIDs.begin();
			while( citr != pDiff->viDelListenerIDs.end() )
			{
				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
	std::vector< int >::const_iterator citr = pDiff->viDelListenerIDs.begin();
	while( citr != pDiff->viDelListenerIDs.end() )
	{
		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
	citr = pDiff->viNewListenerIDs.begin();
	while( citr != pDiff->viNewListenerIDs.end() )
	{
		const int& iID( *citr++ );
		CVABFFListener* pListener = CreateListener( iID, pNewScene->GetListenerState( iID ) );
	}

	// New paths: (1) new receivers, current sources
	citr = pDiff->viNewListenerIDs.begin();
	while( citr != pDiff->viNewListenerIDs.end() )
	{
		int iListenerID = ( *citr++ );
		CVABFFListener* pListener = m_mListeners[iListenerID];

		for( size_t i=0; i<pDiff->viComSoundSourceIDs.size(); i++ )
		{
			// Only add, if no other renderer has been connected explicitly with this source
			// and only, if not marked for deletion
			int iSourceID = pDiff->viComSoundSourceIDs[i];
			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 &&
				( pSoundSourceDesc->sExplicitRendererID.empty() || pSoundSourceDesc->sExplicitRendererID == m_oParams.sID ) ) 
				CVABFFSoundPath* pPath = CreateSoundPath( pSource, pListener );
		}
	}

	// New paths: (2) new sources, current 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; // Explicit source is not connected to this renderer
		
		CVABFFSource* pSource = it->second;
		for( size_t i=0; i<pDiff->viComListenerIDs.size(); i++ )
		{
			int iListenerID = pDiff->viComListenerIDs[i];
			CVABFFListener* pListener = m_mListeners[iListenerID];
			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 );

		citr = pDiff->viNewListenerIDs.begin();
		while( citr != pDiff->viNewListenerIDs.end() )
		{
			const int& iListenerID( *citr++ );
			CVABFFListener* pListener = m_mListeners[iListenerID];
			CVABFFSoundPath* pPath = CreateSoundPath( pSource, pListener );
		}
	}
	
	return;
}

void CVABinauralFreeFieldAudioRenderer::ProcessStream( const ITAStreamInfo* pStreamInfo )
{
618
619
620
621
622
	if( ctxAudio.m_iStatus == 0 )
	{
		// If streaming is active, set to 1
		ctxAudio.m_iStatus = 1;
	}
Jonas Stienen's avatar
Jonas Stienen committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638

	// Schallpfade abgleichen
	SyncInternalData();
	
	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;
	
	// Check for reset request
	if( ctxAudio.m_iResetFlag == 1 )
	{
639
		VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Process stream detecting reset request, will reset internally now" );
Jonas Stienen's avatar
Jonas Stienen committed
640
641
642
643
644
645
		ResetInternalData();

		return;
	}
	else if( ctxAudio.m_iResetFlag == 2 )
	{
646
		VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Process stream detecting ongoing reset, will stop processing here" );
647
				
Jonas Stienen's avatar
Jonas Stienen committed
648
649
650
651
652
		return;
	}

	SampleTrajectoriesInternal( dListenerTime );

653
654
	std::list< CVABFFListener* >::iterator lit = ctxAudio.m_lListeners.begin();
	while( lit != ctxAudio.m_lListeners.end() )
Jonas Stienen's avatar
Jonas Stienen committed
655
	{
656
657
		CVABFFListener* pListener( *( lit++ ) );
		pListener->psfOutput->zero();
Jonas Stienen's avatar
Jonas Stienen committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
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 );
		CVAListenerState* pListenerState = ( m_pCurSceneState ? m_pCurSceneState->GetListenerState( pPath->pListener->pData->iID ) : NULL );
        CVASoundSourceState* pSourceState = ( m_pCurSceneState ? m_pCurSceneState->GetSoundSourceState( pPath->pSource->pData->iID ) : NULL );

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

		if( !pPath->pListener->bValidTrajectoryPresent || !pPath->pSource->bValidTrajectoryPresent  )
		{
			// Skip if no valid trajectory data is present
			spit++;
			continue;
		}

		// --= Parameter update =--

		pPath->UpdateMetrics();

		// VDL Doppler shift settings
		bool bDPEnabledGlobal	= ( m_iCurGlobalAuralizationMode & IVACore::VA_AURAMODE_DOPPLER ) > 0;
		bool bDPEnabledListener	= ( pListenerState->GetAuralizationMode() & IVACore::VA_AURAMODE_DOPPLER ) > 0;
		bool bDPEnabledSource	= ( pSourceState->GetAuralizationMode() & IVACore::VA_AURAMODE_DOPPLER ) > 0;
690
		bool bDPEnabledCurrent	= ( pPath->pVariableDelayLineChL->GetAlgorithm() != CITAVariableDelayLine::SWITCH ); // switch = disabled
Jonas Stienen's avatar
Jonas Stienen committed
691
692
		bool bDPStatusChanged	= ( bDPEnabledCurrent != ( bDPEnabledGlobal && bDPEnabledListener && bDPEnabledSource ) );
		if( bDPStatusChanged )
693
		{
694
695
			pPath->pVariableDelayLineChL->SetAlgorithm( !bDPEnabledCurrent ? m_iDefaultVDLSwitchingAlgorithm : CITAVariableDelayLine::SWITCH );
			pPath->pVariableDelayLineChR->SetAlgorithm( !bDPEnabledCurrent ? m_iDefaultVDLSwitchingAlgorithm : CITAVariableDelayLine::SWITCH );
696
		}
697
		pPath->UpdateMediumPropagation( m_dSpeedOfSound, m_dAdditionalStaticDelaySeconds );
Jonas Stienen's avatar
Jonas Stienen committed
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713

		// Spherical spreading loss
		bool bSSLEnabledSource		= ( pSourceState->GetAuralizationMode() & IVACore::VA_AURAMODE_SPREADING_LOSS ) > 0;
		bool bSSLEnabledListener	= ( pListenerState->GetAuralizationMode() & IVACore::VA_AURAMODE_SPREADING_LOSS ) > 0;
		bool bSSLEnabledGlobal		= ( m_iCurGlobalAuralizationMode & IVACore::VA_AURAMODE_SPREADING_LOSS ) > 0;
		bool bSSLEnabled			= ( bSSLEnabledSource && bSSLEnabledListener && bSSLEnabledGlobal );
		double dDistanceDecrease = pPath->CalculateInverseDistanceDecrease();
		if( !bSSLEnabled )
			dDistanceDecrease = 1.0f;

		bool bDIREnabledSource		= ( pSourceState->GetAuralizationMode() & IVACore::VA_AURAMODE_DIRECTIVITY ) > 0;
		bool bDIREnabledListener	= ( pListenerState->GetAuralizationMode() & IVACore::VA_AURAMODE_DIRECTIVITY ) > 0;
		bool bDIREnabledGlobal		= ( m_iCurGlobalAuralizationMode & IVACore::VA_AURAMODE_DIRECTIVITY ) > 0;
		bool bDIREnabled			= ( bDIREnabledSource && bDIREnabledListener && bDIREnabledGlobal );
		pPath->UpdateDir( bDIREnabled );

Dipl.-Ing. Jonas Stienen's avatar
Style    
Dipl.-Ing. Jonas Stienen committed
714
		pPath->UpdateHRIR( pListenerState->GetAnthropometricData() );
Jonas Stienen's avatar
Jonas Stienen committed
715
716
717
718
719
720
721
722
723
724

		// Sound source gain / direct sound audibility via AuraMode flags
		bool bDSSourceStatusEnabled = ( pSourceState->GetAuralizationMode() & IVACore::VA_AURAMODE_DIRECT_SOUND );
		bool bDSListenerStatusEnabled = ( pListenerState->GetAuralizationMode() & IVACore::VA_AURAMODE_DIRECT_SOUND );
		bool bDSGlobalStatusEnabled = ( m_iCurGlobalAuralizationMode & IVACore::VA_AURAMODE_DIRECT_SOUND );
		bool bDSEnabled = bDSSourceStatusEnabled && bDSListenerStatusEnabled && bDSGlobalStatusEnabled;

		float fSoundSourceGain = float( dDistanceDecrease * pSourceState->GetVolume() );
		if( pPath->pSource->pData->bMuted || ( bDSEnabled == false ) )
			fSoundSourceGain = 0.0f;
725
726
		pPath->pFIRConvolverChL->SetGain( fSoundSourceGain );
		pPath->pFIRConvolverChR->SetGain( fSoundSourceGain );
Jonas Stienen's avatar
Jonas Stienen committed
727
728
729
730
731
732
733
734

		// --= DSP =--

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

735
736
737
		pPath->pThirdOctaveFilterBank->Process( psbInput->data(), ctxAudio.m_sbTempL.data() ); 
		pPath->pVariableDelayLineChR->Process( &( ctxAudio.m_sbTempL ), &( ctxAudio.m_sbTempR ) );
		pPath->pVariableDelayLineChL->Process( &( ctxAudio.m_sbTempL ), &( ctxAudio.m_sbTempL ) ); // inplace
738
739
		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
740
741
742
743
744

		spit++;
	}

	// TODO: Select active listener
745
	if( !ctxAudio.m_lListeners.empty() )
Jonas Stienen's avatar
Jonas Stienen committed
746
	{
747
748
749
		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
750

751
752
		// Listener dumping
		if( m_iDumpListenersFlag > 0 )
Jonas Stienen's avatar
Jonas Stienen committed
753
		{
754
755
756
757
758
759
760
			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
761

762
763
764
765
			// Ack on dump stop
			if( m_iDumpListenersFlag == 2 )
				m_iDumpListenersFlag = 0;
		}
Jonas Stienen's avatar
Jonas Stienen committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
	}

	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 );
		
		const CVAMotionState* pMotionCur = ( pSourceCur ? pSourceCur->GetMotionState() : nullptr );
		const CVAMotionState* pMotionNew = ( pSourceNew ? pSourceNew->GetMotionState() : nullptr );

		if( pMotionNew && ( pMotionNew != pMotionCur ) )
		{
789
			VA_TRACE( "VABinauralFreeFieldAudioRenderer", "Source " << iSourceID << " new motion state" );
Jonas Stienen's avatar
Jonas Stienen committed
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
			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;

		CVAListenerState* pListenerCur = ( m_pCurSceneState ? m_pCurSceneState->GetListenerState( iListenerID ) : nullptr );
		CVAListenerState* pListenerNew = ( m_pNewSceneState ? m_pNewSceneState->GetListenerState( iListenerID ) : nullptr );

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

		if( pMotionNew && ( pMotionNew != pMotionCur ) )
		{
808
			VA_TRACE( "VABinauralFreeFieldAudioRenderer", "Listener " << iListenerID << " new position " );// << *pMotionNew);
Jonas Stienen's avatar
Jonas Stienen committed
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
			pListener->pMotionModel->InputMotionKey( pMotionNew );
		}
	}
}

void CVABinauralFreeFieldAudioRenderer::SampleTrajectoriesInternal( double dTime )
{
	bool bValid = true;
	for( std::list< CVABFFSource* >::iterator it=ctxAudio.m_lSources.begin(); it != ctxAudio.m_lSources.end(); ++it )
	{
		CVABFFSource* pSource = *it;

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

	bValid = true;
828
	for( std::list< CVABFFListener* >::iterator it = ctxAudio.m_lListeners.begin(); it != ctxAudio.m_lListeners.end(); ++it )
Jonas Stienen's avatar
Jonas Stienen committed
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
	{
		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;
	}
}

CVABFFSoundPath* CVABinauralFreeFieldAudioRenderer::CreateSoundPath( CVABFFSource* pSource, CVABFFListener* pListener ) 
{
	int iSourceID = pSource->pData->iID;
	int iListenerID = pListener->pData->iID;

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

846
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Creating sound path from source " << iSourceID << " -> listener " << iListenerID);
Jonas Stienen's avatar
Jonas Stienen committed
847
848
849
850
851
852
853

	CVABFFSoundPath* pPath = dynamic_cast<CVABFFSoundPath*>( m_pSoundPathPool->RequestObject() );
	
	pPath->pSource = pSource;
	pPath->pListener = pListener;

	pPath->bDelete = false;
854
	
Jonas Stienen's avatar
Jonas Stienen committed
855
856
857
858
859
860
861
862
	CVASoundSourceState* pSourceNew = (m_pNewSceneState ? m_pNewSceneState->GetSoundSourceState( iSourceID ) : nullptr );
	if( pSourceNew != nullptr )
		pPath->oDirectivityStateNew.pData = (IVADirectivity*) pSourceNew->GetDirectivityData();

	CVAListenerState* pListenerNew = ( m_pNewSceneState ? m_pNewSceneState->GetListenerState( iListenerID ) : nullptr );
	if( pListenerNew != nullptr )
		pPath->oHRIRStateNew.pData = (IVAHRIRDataset*) pListenerNew->GetHRIRDataset();

863
864
865
866
867
	// Configure DSP elements
	pPath->pVariableDelayLineChL->SetAlgorithm( m_iDefaultVDLSwitchingAlgorithm );
	pPath->pVariableDelayLineChR->SetAlgorithm( m_iDefaultVDLSwitchingAlgorithm );


Jonas Stienen's avatar
Jonas Stienen committed
868
869
870
871
872
873
874
875
	m_lSoundPaths.push_back( pPath );
	m_pUpdateMessage->vNewPaths.push_back( pPath );

	return pPath;
}

void CVABinauralFreeFieldAudioRenderer::DeleteSoundPath( CVABFFSoundPath* pPath )
{
876
	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
877
878
879
880
881
882
883
884

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

CVABinauralFreeFieldAudioRenderer::CVABFFListener* CVABinauralFreeFieldAudioRenderer::CreateListener( const int iID, const CVAListenerState* pListenerState )
{
885
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Creating listener with ID " << iID );
Jonas Stienen's avatar
Jonas Stienen committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912

	CVABFFListener* pListener = dynamic_cast<CVABFFListener*>( m_pListenerPool->RequestObject() ); // Reference = 1

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

	// Move to prerequest of pool?
	pListener->psfOutput = new ITASampleFrame( 2,
											   m_pCore->GetCoreConfig()->oAudioDriverConfig.iBuffersize,
											   true ); 
	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 )
{
913
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Marking listener with ID " << iListenerID << " for removal" );
Jonas Stienen's avatar
Jonas Stienen committed
914
915
916
917
918
919
920
921
922
923
924
925
926
927
	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 )
{
928
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Creating source with ID " << iID );
Jonas Stienen's avatar
Jonas Stienen committed
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
	CVABFFSource* pSource = dynamic_cast< CVABFFSource* >( m_pSourcePool->RequestObject() );
	
	pSource->pData = m_pCore->GetSceneManager()->GetSoundSourceDesc(iID);
	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 )
{
949
	VA_VERBOSE( "BinauralFreeFieldAudioRenderer", "Marking source with ID " << iSourceID << " for removal" );
Jonas Stienen's avatar
Jonas Stienen committed
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
	std::map< int, CVABFFSource* >::iterator it = m_mSources.find( iSourceID );
	
	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 );
		
	return;
}

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

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

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