VABinauralOutdoorNoiseAudioRenderer.cpp 20.5 KB
Newer Older
1 2
#include "VABinauralOutdoorNoiseAudioRenderer.h"

3 4 5 6 7
#include <iostream>
#include <fstream>
#include <ctime>
#include <chrono>
#include <time.h>
henryjandrew's avatar
wip  
henryjandrew committed
8 9 10 11 12 13 14
#include <vector>
#include <string>
#include <sstream>



#include <ITAThirdOctaveFilterbank.h>
15

16
// VA Includes
17 18
#include <VA.h>
#include <VAObjectPool.h>
19
#include "../../../core/core.h"
20 21 22 23
#include "../../../Utils/VAUtils.h"

// VA Core Includes
#include "../../../Scene/VAScene.h"
24 25

// ITA includes
26
#include <ITAException.h>
27
#include <ITAFastMath.h>
28 29 30 31 32 33 34 35
#include <ITAStreamInfo.h>
#include <ITAVariableDelayLine.h>

// Utils
#include "../Clustering/VAConfig.h"
#include "../Clustering/Receiver/VABinauralClusteringDirectionReceiverPoolFactory.h"
#include "VABinauralOutdoorWaveFront.h"
#include "VABinauralOutdoorWaveFrontPoolFactory.h"
henryjandrew's avatar
wip  
henryjandrew committed
36
#include "VABinauralOutdoorSource.h"
37 38 39 40 41 42 43 44 45 46 47

CVABinauralOutdoorNoiseRenderer::CVABinauralOutdoorNoiseRenderer( const CVAAudioRendererInitParams& oParams )
	: ITADatasourceRealization( 2, oParams.pCore->GetCoreConfig()->oAudioDriverConfig.dSampleRate, oParams.pCore->GetCoreConfig()->oAudioDriverConfig.iBuffersize )
	, m_oParams( oParams )
	, m_pCore( oParams.pCore )
	, m_pNewSceneState( NULL )
	, m_pCurSceneState( NULL )
	, m_bIndicateReset( false )
	, m_bResetAck( false )
{
	Init( *m_oParams.pConfig );
48

49 50
	IVAPoolObjectFactory* pReceiverFactory = new CVABinauralClusteringDirectionReceiverPoolFactory( m_pCore, m_oDefaultReceiverConf );
	m_pReceiverPool = IVAObjectPool::Create( 16, 2, pReceiverFactory, true );
51

52 53
	IVAPoolObjectFactory* pWaveFrontFactory = new CVABinauralOutdoorWaveFrontPoolFactory( m_oDefaultWaveFrontConf );
	m_pWaveFrontPool = IVAObjectPool::Create( 16, 2, pWaveFrontFactory, true );
54

55
	m_iCurGlobalAuralizationMode = IVAInterface::VA_AURAMODE_DEFAULT;
56 57
}

58
CVABinauralOutdoorNoiseRenderer::~CVABinauralOutdoorNoiseRenderer()
59 60 61
{
}

62
void CVABinauralOutdoorNoiseRenderer::Init( const CVAStruct& oArgs )
63
{
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	CVAConfigInterpreter conf( oArgs );

	// set interpolation algorithm
	std::string vldInterpolationAlgorithm;
	conf.OptString( "SwitchingAlgorithm", vldInterpolationAlgorithm, "linear" );
	vldInterpolationAlgorithm = toLowercase( vldInterpolationAlgorithm );

	if( vldInterpolationAlgorithm == "switch" )
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::SWITCH;
	else if( vldInterpolationAlgorithm == "crossfade" )
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::CROSSFADE;
	else if( vldInterpolationAlgorithm == "linear" )
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::LINEAR_INTERPOLATION;
	else if( vldInterpolationAlgorithm == "cubicspline" )
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::CUBIC_SPLINE_INTERPOLATION;
	else if( vldInterpolationAlgorithm == "windowedsinc" )
		m_iDefaultVDLSwitchingAlgorithm = CITAVariableDelayLine::WINDOWED_SINC_INTERPOLATION;
	else
		ITA_EXCEPT1( INVALID_PARAMETER, "Unrecognized interpolation algorithm '" + vldInterpolationAlgorithm + "' in BinauralFreefieldAudioRendererConfig" );
83

84 85
	// HRTF filter length
	conf.OptInteger( "HRIRFilterLength", m_iHRIRFilterLength, 256 );
86

87 88
	// additional static delay
	conf.OptNumber( "AdditionalStaticDelaySeconds", m_dAdditionalStaticDelaySeconds, 0.0f );
89 90


91 92 93 94
	// default listener config, @todo rest
	m_oDefaultReceiverConf.motionModelLogEstimated = false;
	m_oDefaultReceiverConf.motionModelLogInput = false;
	conf.OptInteger( "MotionModelNumHistoryKeys", m_oDefaultReceiverConf.motionModelNumHistoryKeys, 1000 );
95

96 97
	if( m_oDefaultReceiverConf.motionModelNumHistoryKeys < 1 )
		VA_EXCEPT2( INVALID_PARAMETER, "Basic motion model history needs to be greater than zero" );
98

99 100
	conf.OptNumber( "MotionModelWindowSize", m_oDefaultReceiverConf.motionModelWindowSize, 0.1f );
	conf.OptNumber( "MotionModelWindowDelay", m_oDefaultReceiverConf.motionModelWindowDelay, 0.1f );
101

102 103
	if( ( m_oDefaultReceiverConf.motionModelWindowSize <= 0 ) || ( m_oDefaultReceiverConf.motionModelWindowDelay < 0 ) )
		VA_EXCEPT2( INVALID_PARAMETER, "Basic motion model window parameters parse error (zero or negative?)" );
104

105 106
	// default source config
	conf.OptInteger( "MotionModelNumHistoryKeys", m_oDefaultWaveFrontConf.motionModelNumHistoryKeys, 1000 );
107

108 109
	if( m_oDefaultWaveFrontConf.motionModelNumHistoryKeys < 1 )
		VA_EXCEPT2( INVALID_PARAMETER, "Basic motion model history needs to be greater than zero" );
110

111 112
	conf.OptNumber( "MotionModelWindowSize", m_oDefaultWaveFrontConf.motionModelWindowSize, 0.1f );
	conf.OptNumber( "MotionModelWindowDelay", m_oDefaultWaveFrontConf.motionModelWindowDelay, 0.1f );
113

114 115
	if( ( m_oDefaultWaveFrontConf.motionModelWindowSize <= 0 ) || ( m_oDefaultWaveFrontConf.motionModelWindowDelay < 0 ) )
		VA_EXCEPT2( INVALID_PARAMETER, "Basic motion model window parameters parse error (zero or negative?)" );
116

117 118
	m_oDefaultWaveFrontConf.blockLength = GetBlocklength();
	m_oDefaultWaveFrontConf.sampleRate = GetSampleRate();
119

120 121 122 123 124 125
	VAConfig::blockLength = GetBlocklength();
	VAConfig::hrirLength = m_iHRIRFilterLength;
	VAConfig::amplitudeCalibration = m_pCore->GetCoreConfig()->dDefaultAmplitudeCalibration;

	// initialize after config
	m_pClusterEngine = new CVABinauralClusteringEngine();
126 127
}

128
void CVABinauralOutdoorNoiseRenderer::Reset()
129
{
130
	VA_ERROR( "BinauralClusteringRenderer", "Reset hast not yet been implemented" );
131 132
}

133
ITADatasource* CVABinauralOutdoorNoiseRenderer::GetOutputDatasource()
134
{
135 136
	return this;
}
137

henryjandrew's avatar
wip  
henryjandrew committed
138
void CVABinauralOutdoorNoiseRenderer::SetParameters(const CVAStruct& oInArgs)
139
{
henryjandrew's avatar
wip  
henryjandrew committed
140 141 142 143 144 145 146 147 148 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

	auto it = oInArgs.Begin();
	while (it != oInArgs.End()) {
		//const CVAStructValue& path_temp(&it); //tempory struct to hold the path data for the current iterator
		const CVAStructValue& oPath(&it); 
		CVAStruct path = oPath;
		int path_ID = path["path"];
		//int path_ID = (oPath.GetData())["path"];
		//assert(oPath.IsData());
		oPath.GetData();

		//asume that wave front ID's go from 0 to ... with no gaps, and that wavefronts are not deleted or replaced
		int pool_size = m_pWaveFrontPool->GetNumUsed(); //total number of wavefronts currently in use
		if (path_ID > pool_size) { 
			CVABinauralOutdoorWaveFront* wave_front = dynamic_cast<CVABinauralOutdoorWaveFront*>(m_pWaveFrontPool->RequestObject());

			m_mCurrentWaveFronts.insert(std::pair< int, CVABinauralOutdoorWaveFront* >(path_ID, wave_front));
			//probably should add reference here
		}
		else {
			m_mCurrentWaveFronts[path_ID]->setParameters( oPath ); //cals a function which sets all the relevant internal parameters for the path
		}

		//int path_ID = oPath.;
		//int m_mPaths.find(m_mPaths.begin(), m_mPaths.end(), path_ID);

		//m_mPaths.find //given path number, find the internal representation of that path to store new data in
		it++;
	}

	/*
	// Update
	if (oInArgs.HasKey("receiver") == false || oInArgs.HasKey("source") == false)
	{
		VA_INFO("BinauralOutdoorNoiseRenderer", "Parameter setter was called without source or receiver id, did not update any generic path filter");
		return;
	}
	if (oInArgs.HasKey("paths") == false )
	{
		VA_INFO("BinauralOutdoorNoiseRenderer", "Parameter setter was called without a path list");
		return;
	}

	int iReceiverID = oInArgs["receiver"];
	int iSourceID = oInArgs["source"];

	const CVAStruct& oPaths(oInArgs["paths"]);

	auto it = oPaths.Begin();
	while (it != oPaths.End())
	{
		const CVAStructValue& oPath(it.Ge);
		it++;
		m_mPaths.find
	}

	bool bVerbose = false;
	if (oInArgs.HasKey("verbose"))
		bVerbose = true;

		*/
	// add source to clustering
	//m_pClusterEngine->AddWaveFront( sourceID, pSource );

	// adjust VDL delay for given path / cursor

	//assumes that every paths frequency parameters are updated at every call, will need to be changed in future
	mNumberofPaths = oInArgs["n0_of_paths"];
	mMags.resize(mNumberofPaths);
	for (int n = 0; n <= mNumberofPaths; n++) { //loop over all paths
		std::string path_name = "prop_path_" + std::to_string(n);
		if (oInArgs.HasKey( path_name ) == true) {
			CVAStruct test = oInArgs[path_name]; //struct with all info relating to the current path in

			CVAStructValue frequency_mags = test["values"];
			int num_values = frequency_mags.GetDataSize() / sizeof(float);
			if (num_values != 31)
				VA_EXCEPT1("CVABinauralOutdoorNoiseRenderer: Expected 31 frequency magnitudes.");
			void *values = frequency_mags.GetData();

			const float* pfMags = (const float*)values;

			for (int i = 0; i < 31; i++) {
				(mMags[n])[i] = pfMags[i]; //set the magnitudes for the internal variable
			}
			
			//VA_INFO("BinauralOutdoorNoiseRenderer", "Frequency magnitudes from path " + path_name );
		}
	}
229 230 231 232 233 234 235 236 237 238
	VA_INFO( "BinauralOutdoorNoiseRenderer", "Received input arguments: " + oInArgs.ToString() );
}

CVAStruct CVABinauralOutdoorNoiseRenderer::GetParameters( const CVAStruct& ) const
{
	CVAStruct oOutArgs;
	oOutArgs[ "info" ] = "Nothing meaningful to return, yet";
	return oOutArgs;
}

239 240 241 242
void CVABinauralOutdoorNoiseRenderer::UpdateGlobalAuralizationMode( int iGlobalAuralizationMode )
{
	if( m_iCurGlobalAuralizationMode == iGlobalAuralizationMode )
		return;
243

244
	m_iCurGlobalAuralizationMode = iGlobalAuralizationMode;
245

246
	return;
247 248
}

249 250
void
CVABinauralOutdoorNoiseRenderer::ProcessStream( const ITAStreamInfo* streamInfo )
251 252
{

253
#ifdef BINAURAL_CLUSTERING_RENDERER_WITH_BENCHMARKING
254

255 256 257 258
	auto start = std::chrono::high_resolution_clock::now();
	LARGE_INTEGER frequency;        // ticks per second
	LARGE_INTEGER t1, t2;           // ticks
	double elapsedTime;
259

260 261
	// get ticks per second
	QueryPerformanceFrequency(&frequency);
262

263 264
	// start timer
	QueryPerformanceCounter(&t1);
265

266
#endif // BINAURAL_CLUSTERING_RENDERER_WITH_BENCHMARKING
267

268 269 270 271
	/////
	ITASampleFrame* output;
	float* outputChL = GetWritePointer( 0 );
	float* outputChR = GetWritePointer( 1 );
272

273 274
	fm_zero( outputChL, GetBlocklength() );
	fm_zero( outputChR, GetBlocklength() );
275

276 277 278
	const CVAAudiostreamState* streamState = dynamic_cast< const CVAAudiostreamState* >( streamInfo );
	double time = streamState->dSysTime;
	//updateTrajectories(time); // @todo jst: why is this outcommented? Trajectories must be updated here for Doppler shifts ...
279

280 281 282 283
	// -- create output for every listener
	for( auto const& clusteringIt : m_pClusterEngine->m_mClusteringInstances )
	{
		CVABinauralClustering* clustering = clusteringIt.second;
284

285
		output = clustering->GetOutput();
286

287 288 289
		fm_copy( outputChL, ( *output )[ 0 ].data(), m_uiBlocklength );
		fm_copy( outputChR, ( *output )[ 1 ].data(), m_uiBlocklength );
	}
290

291
#ifdef BINAURAL_CLUSTERING_RENDERER_WITH_BENCHMARKING
292

293 294
	// stop timer
	QueryPerformanceCounter(&t2);
295

296 297
	// compute and print the elapsed time in millisec
	elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
298

299 300 301
	std::ofstream outfile("X:/Sciebo/master-thesis/data/benchmarks/Stream_128.txt", std::fstream::app);
	outfile << elapsedTime << std::endl;
	outfile.close();
302

303
#endif // BINAURAL_CLUSTERING_RENDERER_WITH_BENCHMARKING
304

305
	IncrementWritePointer();
306

307
	return;
308 309
}

310
void CVABinauralOutdoorNoiseRenderer::UpdateScene( CVASceneState* pNewSceneState )
311
{
312
	assert( pNewSceneState );
313

314 315 316
	m_pNewSceneState = pNewSceneState;
	if( m_pNewSceneState == m_pCurSceneState )
		return;
317

318 319
	// get reference to new scene
	m_pNewSceneState->AddReference();
320

321 322 323
	// get difference between old scene state and current scene state
	CVASceneStateDiff diff;
	m_pNewSceneState->Diff( m_pCurSceneState, &diff );
324

325
#ifdef BINAURAL_CLUSTERING_RENDERER_WITH_BENCHMARKING
326

327 328 329
	LARGE_INTEGER frequency;        // ticks per second
	LARGE_INTEGER t1, t2;           // ticks
	double elapsedTime;
330

331 332
	// get ticks per second
	QueryPerformanceFrequency(&frequency);
333

334 335
	// start timer
	QueryPerformanceCounter(&t1);
336

337
#endif // BINAURAL_CLUSTERING_RENDERER_WITH_BENCHMARKING
338

339 340
	// Update receivers
	UpdateSoundReceivers( &diff );
341

342 343
	// Update wave fronts
	UpdateSoundSources( &diff );
344

345
	m_pClusterEngine->Update();
346

347
#ifdef BINAURAL_CLUSTERING_RENDERER_WITH_BENCHMARKING
348

349 350
	// stop timer
	QueryPerformanceCounter(&t2);
351

352 353
	// compute and print the elapsed time in millisec
	elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
354

355 356 357
	std::ofstream outfile("X:/Sciebo/master-thesis/data/benchmarks/UpdateScene_128.txt", std::fstream::app);
	outfile << elapsedTime << std::endl;
	outfile.close();
358

359
#endif // BINAURAL_CLUSTERING_RENDERER_WITH_BENCHMARKING
360

361 362 363
	// Alte Szene freigeben (dereferenzieren)
	if( m_pCurSceneState )
		m_pCurSceneState->RemoveReference();
364

365 366
	m_pCurSceneState = m_pNewSceneState;
	m_pNewSceneState = nullptr;
367 368 369

}

370
void CVABinauralOutdoorNoiseRenderer::UpdateSoundSources( CVASceneStateDiff* diff )
371
{
372
	typedef std::vector< int >::const_iterator icit_t;
373

374 375
	// Delete sources
	for( icit_t it = diff->viDelSoundSourceIDs.begin(); it != diff->viDelSoundSourceIDs.end(); )
376
	{
377 378
		const int& iID( *it++ );
		DeleteSoundSource( iID );
379 380
	}

381 382
	// Add sources
	for( icit_t it = diff->viNewSoundSourceIDs.begin(); it != diff->viNewSoundSourceIDs.end(); )
383
	{
384 385
		const int& iID( *it++ );
		CreateSoundSource( iID, m_pNewSceneState->GetSoundSourceState( iID ) );
386 387
	}

388
	// Update wave front trajectories
henryjandrew's avatar
wip  
henryjandrew committed
389
	std::map< int, CVABinauralOutdoorSource* >::iterator it;
390

henryjandrew's avatar
wip  
henryjandrew committed
391
	for( it = m_mSources.begin(); it != m_mSources.end(); ++it )
392
	{
henryjandrew's avatar
wip  
henryjandrew committed
393 394
		int iSourceID = it->first;
		CVABinauralOutdoorSource* source = it->second;
395

henryjandrew's avatar
wip  
henryjandrew committed
396 397
		CVASoundSourceState* sourceCur = ( m_pCurSceneState ? m_pCurSceneState->GetSoundSourceState(iSourceID) : nullptr );
		CVASoundSourceState* sourceNew = ( m_pNewSceneState ? m_pNewSceneState->GetSoundSourceState(iSourceID) : nullptr );
398

399
		if( sourceNew && ( sourceNew != sourceCur ) )
400
		{
401
			source->pState = sourceNew;
402 403
		}

404 405
		const CVAMotionState* motionCur = ( sourceCur ? sourceCur->GetMotionState() : nullptr );
		const CVAMotionState* motionNew = ( sourceNew ? sourceNew->GetMotionState() : nullptr );
406

407 408 409 410
		if( motionNew && ( motionNew != motionCur ) )
		{
			source->pMotionModel->InputMotionKey( motionNew );
			// Dirty Hack
411

412
			VAVec3 pos = motionNew->GetPosition();
413

414 415 416 417
			if( pos != VAVec3( 0, 0, 0 ) )
			{
				VAVec3 view = motionNew->GetView();
				VAVec3 up = motionNew->GetUp();
418

henryjandrew's avatar
wip  
henryjandrew committed
419
				/*
420 421 422 423
				source->v3PredictedPos = pos;
				source->predView = view;
				source->predUp = up;
				source->hasValidTrajectory = true;
henryjandrew's avatar
wip  
henryjandrew committed
424 425 426

				todo : in future impl trigger propagation simulation
				*/
427 428 429
			}
		}
	}
430 431
}

432
void CVABinauralOutdoorNoiseRenderer::UpdateSoundReceivers( CVASceneStateDiff* diff )
433
{
434
	typedef std::vector< int >::const_iterator icit_t;
435

436 437
	// Delete listeners
	for( icit_t it = diff->viDelReceiverIDs.begin(); it != diff->viDelReceiverIDs.end(); )
438
	{
439 440
		const int& iID( *it++ );
		DeleteSoundReceiver( iID );
441 442
	}

443 444
	// Add listeners
	for( icit_t it = diff->viNewReceiverIDs.begin(); it != diff->viNewReceiverIDs.end(); )
445
	{
446 447
		const int& iID( *it++ );
		CreateSoundReceiver( iID, m_pNewSceneState->GetReceiverState( iID ) );
448 449
	}

450 451 452 453 454
	// Update receiver trajectories and directivity
	for( std::map< int, CVABinauralClusteringDirectionReceiver* >::iterator it = m_mBinauralReceivers.begin(); it != m_mBinauralReceivers.end(); ++it )
	{
		int iListenerID = it->first;
		CVABinauralClusteringDirectionReceiver* listener = it->second;
455

456 457
		CVAReceiverState* listenerCur = ( m_pCurSceneState ? m_pCurSceneState->GetReceiverState( iListenerID ) : nullptr );
		CVAReceiverState* listenerNew = ( m_pNewSceneState ? m_pNewSceneState->GetReceiverState( iListenerID ) : nullptr );
458

459 460
		const CVAMotionState* motionCur = ( listenerCur ? listenerCur->GetMotionState() : nullptr );
		const CVAMotionState* motionNew = ( listenerNew ? listenerNew->GetMotionState() : nullptr );
461

462 463 464 465
		if( motionNew && ( motionNew != motionCur ) )
		{
			listener->pMotionModel->InputMotionKey( motionNew );
			// Dirty Hack
466

467
			VAVec3 pos = motionNew->GetPosition();
468

469 470 471 472
			if( pos != VAVec3( 0, 0, 0 ) )
			{
				VAVec3 view = motionNew->GetView();
				VAVec3 up = motionNew->GetUp();
473

474 475 476 477 478
				listener->v3PredictedPosition = pos;
				listener->predView = view;
				listener->predUp = up;
				listener->hasValidTrajectory = true;
			}
479 480
		}

481 482 483
		if( listenerNew != nullptr )
		{
			listener->pDirectivity = ( IVADirectivity* ) listenerNew->GetDirectivity();
484 485 486 487
		}
	}
}

488
void CVABinauralOutdoorNoiseRenderer::CreateSoundSource( const int sourceID, const CVASoundSourceState* sourceState )
489
{
henryjandrew's avatar
wip  
henryjandrew committed
490 491
	//CVABinauralOutdoorWaveFront* source = dynamic_cast< CVABinauralOutdoorWaveFront* >( m_pWaveFrontPool->RequestObject() ); // Reference = 1
	auto pSource = new CVABinauralOutdoorSource();
492

493
	// set state
henryjandrew's avatar
wip  
henryjandrew committed
494
	pSource->pState = ( CVASoundSourceState* ) sourceState;
495
	// set internal data
henryjandrew's avatar
wip  
henryjandrew committed
496 497
	pSource->pData = m_pCore->GetSceneManager()->GetSoundSourceDesc( sourceID );
	pSource->pData->AddReference();
498

henryjandrew's avatar
wip  
henryjandrew committed
499 500 501
	CVABasicMotionModel::Config sourceMotionConf;
	sourceMotionConf.SetDefaults();
	pSource->pMotionModel = new CVASharedMotionModel(new CVABasicMotionModel(sourceMotionConf), true);
502
	// set motion model
henryjandrew's avatar
wip  
henryjandrew committed
503 504
	CVABasicMotionModel* motionInstance = dynamic_cast< CVABasicMotionModel* >( pSource->pMotionModel->GetInstance() ); // Problem HERE***
	motionInstance->SetName( std::string( "bfrend_mm_source_" + pSource->pData->sName ) );
505
	motionInstance->Reset();
506

507
	// add local reference
henryjandrew's avatar
wip  
henryjandrew committed
508
	m_mSources.insert( std::pair< int, CVABinauralOutdoorSource* >( sourceID, pSource ) );
509 510 511

}

512
void CVABinauralOutdoorNoiseRenderer::DeleteSoundSource( int sourceID )
513
{
514
	// remove local source reference
henryjandrew's avatar
wip  
henryjandrew committed
515 516 517
	std::map< int, CVABinauralOutdoorSource* >::iterator it = m_mSources.find( sourceID );
	auto source = it->second;
	m_mSources.erase( it );
518

henryjandrew's avatar
wip  
henryjandrew committed
519
	// todo henry: how to remove path from  this source?
520
	// remove listener reference from clustering
henryjandrew's avatar
wip  
henryjandrew committed
521 522
	//m_pClusterEngine->RemoveWaveFront( sourceID );
	//source->RemoveReference();
523 524
}

525
void CVABinauralOutdoorNoiseRenderer::CreateSoundReceiver( int listenerID, const CVAReceiverState* recevierState )
526
{
527
	CVABinauralClusteringDirectionReceiver* listener = dynamic_cast< CVABinauralClusteringDirectionReceiver* >( m_pReceiverPool->RequestObject() ); // Reference = 1
528

529 530 531
	// set internal data
	listener->pData = m_pCore->GetSceneManager()->GetListenerDesc( listenerID );
	listener->pData->AddReference();
532

533
	// set motion model
henryjandrew's avatar
wip  
henryjandrew committed
534
	CVABasicMotionModel* motionInstance = dynamic_cast< CVABasicMotionModel* >( listener->pMotionModel->GetInstance() ); //foo possible error Get Instance
535 536
	motionInstance->SetName( std::string( "bfrend_mm_listener_" + listener->pData->sName ) );
	motionInstance->Reset();
537

538 539
	//set directivity
	listener->pDirectivity = ( IVADirectivity* ) recevierState->GetDirectivity();
540

541 542
	// add local reference
	m_mBinauralReceivers.insert( std::pair< int, CVABinauralClusteringDirectionReceiver* >( listenerID, listener ) );
543

544 545 546 547
	// add listener to clustering
	CVABinauralClusteringEngine::Config config = { /*numClusters=*/ 12 }; // @todo use configuranble number of clusters -> config
	m_pClusterEngine->AddReceiver( listenerID, listener, config );
}
548

549 550 551 552 553 554
void CVABinauralOutdoorNoiseRenderer::DeleteSoundReceiver( int listenerID )
{
	// remove local listener reference
	std::map< int, CVABinauralClusteringDirectionReceiver* >::iterator it = m_mBinauralReceivers.find( listenerID );
	CVABinauralClusteringDirectionReceiver* listener = it->second;
	m_mBinauralReceivers.erase( it );
555

556 557 558
	//listener->bDeleted = true;
	//listener->pData->RemoveReference();
	listener->RemoveReference();
559

560 561
	// remove listener reference from clustering
	m_pClusterEngine->RemoveReceiver( listenerID );
562 563 564

}

565
void CVABinauralOutdoorNoiseRenderer::UpdateMotionStates()
566
{
567
	// Neue Quellendaten bernehmen
henryjandrew's avatar
wip  
henryjandrew committed
568
	for( std::map< int, CVABinauralOutdoorSource* >::iterator it = m_mSources.begin(); it != m_mSources.end(); ++it )
569
	{
570
		int sourceID = it->first;
henryjandrew's avatar
wip  
henryjandrew committed
571
		auto source = it->second;
572

573 574
		CVASoundSourceState* sourceCur = ( m_pCurSceneState ? m_pCurSceneState->GetSoundSourceState( sourceID ) : nullptr );
		CVASoundSourceState* sourceNew = ( m_pNewSceneState ? m_pNewSceneState->GetSoundSourceState( sourceID ) : nullptr );
575

576 577
		const CVAMotionState* motionCur = ( sourceCur ? sourceCur->GetMotionState() : nullptr );
		const CVAMotionState* motionNew = ( sourceNew ? sourceNew->GetMotionState() : nullptr );
578

579
		if( motionNew && ( motionNew != motionCur ) )
580
		{
581 582
			//VA_TRACE("BinauralFreeFieldAudioRenderer", "Source " << iSourceID << " new motion state");
			source->pMotionModel->InputMotionKey( motionNew );
583 584 585 586
		}
	}
}

587
void CVABinauralOutdoorNoiseRenderer::UpdateTrajectories( double time )
588
{
henryjandrew's avatar
wip  
henryjandrew committed
589
	for( auto const& sourceIt : m_mSources )
590
	{
591
		bool isValid = true;
henryjandrew's avatar
wip  
henryjandrew committed
592
		auto source = sourceIt.second;
593

594
		source->pMotionModel->HandleMotionKeys();
henryjandrew's avatar
wip  
henryjandrew committed
595
		/* todo trigger simulation in C++ ...
596 597 598
		isValid &= source->pMotionModel->EstimatePosition( time, source->v3PredictedPos );
		isValid &= source->pMotionModel->EstimateOrientation( time, source->predView, source->predUp );
		source->hasValidTrajectory = isValid;
henryjandrew's avatar
wip  
henryjandrew committed
599
		*/
600 601
	}

602
	for( auto const& listenerIt : m_mBinauralReceivers )
603
	{
604 605
		bool isValid = true;
		CVABinauralClusteringDirectionReceiver* listener = listenerIt.second;
606

607 608 609 610
		listener->pMotionModel->HandleMotionKeys();
		isValid &= listener->pMotionModel->EstimatePosition( time, listener->v3PredictedPosition );
		isValid &= listener->pMotionModel->EstimateOrientation( time, listener->predView, listener->predUp );
		listener->hasValidTrajectory = isValid;
611 612
	}
}