VABinauralRealTimeRenderer.cpp 13.6 KB
Newer Older
1 2 3
#include "VABinauralRealTimeRenderer.h"

// VA Includes
4
#include <VA.h>
5 6
#include <VAObjectPool.h>
#include "../../../VACoreImpl.h"
7 8
#include "../../../Utils/VAUtils.h"

9 10
// VA Core Includes
#include "../../../Scene/VAScene.h"
11

12 13
// ITA includes
#include <ITAException.h>
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
14 15
#include <ITAFastMath.h>
#include <ITAStreamInfo.h>
16
#include <ITAVariableDelayLine.h>
17

18
// Utils
Lucas Moesch's avatar
Lucas Moesch committed
19
#include "./Utils/Config/VAConfig.h"
20 21
#include "./Utils/BinauralListener/VABinauralListenerPoolFactory.h"
#include "./Utils/BinauralSoundSource/VABinauralSoundSourcePoolFactory.h"
22

23 24
VABinauralRealTimeRenderer::VABinauralRealTimeRenderer( const CVAAudioRendererInitParams& params ) :
	ITADatasourceRealization(2, params.pCore->GetCoreConfig()->oAudioDriverConfig.dSampleRate, params.pCore->GetCoreConfig()->oAudioDriverConfig.iBuffersize),
25 26
	_params(params),
	_core(params.pCore),
27 28 29
	_newSceneState(NULL), 
	_curSceneState(NULL), 
	_indicateReset(false), 
30
	_resetAck(false)
31
{
32 33 34 35
	init(*_params.pConfig);

	IVAPoolObjectFactory* listenerFactory = new VABinauralListenerPoolFactory(_core, _defaultListenerConf);
	_listenerPool = IVAObjectPool::Create(16, 2, listenerFactory, true);
36

37 38
	IVAPoolObjectFactory* sourceFactory = new VABinauralSoundSourcePoolFactory(_defaultSourceConf);
	_sourcePool = IVAObjectPool::Create(16, 2, sourceFactory, true);
39 40

	_curGlobalAuralizationMode = IVAInterface::VA_AURAMODE_DEFAULT;
41 42 43 44 45 46
}


VABinauralRealTimeRenderer::~VABinauralRealTimeRenderer()
{
}
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
void
VABinauralRealTimeRenderer::init( const CVAStruct& oArgs )
{
	CVAConfigInterpreter conf(oArgs);

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

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

	// HRTF filter length
	conf.OptInteger("HRIRFilterLength", _hrirFilterLength, 256);

	// additional static delay
	conf.OptNumber("AdditionalStaticDelaySeconds", _additionalStaticDelaySeconds, 0.0f);

	// default listener config
	conf.OptInteger("MotionModelNumHistoryKeys", _defaultListenerConf.motionModelNumHistoryKeys, 1000);

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

	conf.OptNumber("MotionModelWindowSize", _defaultListenerConf.motionModelWindowSize, 0.1f);
	conf.OptNumber("MotionModelWindowDelay", _defaultListenerConf.motionModelWindowDelay, 0.1f);

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

	// default source config
	conf.OptInteger("MotionModelNumHistoryKeys", _defaultSourceConf.motionModelNumHistoryKeys, 1000);

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

	conf.OptNumber("MotionModelWindowSize", _defaultSourceConf.motionModelWindowSize, 0.1f);
	conf.OptNumber("MotionModelWindowDelay", _defaultSourceConf.motionModelWindowDelay, 0.1f);

	if ((_defaultSourceConf.motionModelWindowSize <= 0) || (_defaultSourceConf.motionModelWindowDelay < 0))
		VA_EXCEPT2(INVALID_PARAMETER, "Basic motion model window parameters parse error (zero or negative?)");
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
100 101 102

	_defaultSourceConf.blockLength = GetBlocklength();
	_defaultSourceConf.sampleRate = GetSampleRate();
Lucas Moesch's avatar
Lucas Moesch committed
103 104 105

	VAConfig::blockLength = GetBlocklength();
	VAConfig::hrirLength = _hrirFilterLength;
106 107 108 109
	VAConfig::amplitudeCalibration = _core->GetCoreConfig()->dDefaultAmplitudeCalibration;

	// initialize after config
	_clusterEngine = new VABinauralClusterEngine();
110 111
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
void 
VABinauralRealTimeRenderer::Reset()
{

};

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

CVAStruct
VABinauralRealTimeRenderer::CallObject(const CVAStruct& oArgs){
	CVAStruct oReturn;

	return oReturn;
};

void VABinauralRealTimeRenderer::UpdateGlobalAuralizationMode(int iGlobalAuralizationMode)
{
	if (_curGlobalAuralizationMode == iGlobalAuralizationMode)
		return;

	_curGlobalAuralizationMode = iGlobalAuralizationMode;

	return;
}

140
void
141
VABinauralRealTimeRenderer::ProcessStream(const ITAStreamInfo* streamInfo)
142
{
143
	ITASampleFrame* output;
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
144 145 146 147 148 149 150
	float* outputChL = GetWritePointer(0);
	float* outputChR = GetWritePointer(1);

	fm_zero(outputChL, GetBlocklength());
	fm_zero(outputChR, GetBlocklength());

	const CVAAudiostreamState* streamState = dynamic_cast< const CVAAudiostreamState* >(streamInfo);
Lucas Moesch's avatar
wip  
Lucas Moesch committed
151 152 153
	double time = streamState->dSysTime;

	updateTrajectories(time);
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
154

155
	// -- create output for every listener
156
	for (auto const& clusteringIt : _clusterEngine->_clusterings)
Lucas Moesch's avatar
Lucas Moesch committed
157 158 159 160 161 162 163
	{
		VABinauralClustering* clustering = clusteringIt.second;

		output = clustering->getOutput();

		fm_copy(outputChL, (*output)[0].data(), m_uiBlocklength);
		fm_copy(outputChR, (*output)[1].data(), m_uiBlocklength);
Lucas Moesch's avatar
Lucas Moesch committed
164
	}
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
165 166 167 168

	IncrementWritePointer();

	return;
169 170 171
}

void
172
VABinauralRealTimeRenderer::UpdateScene(CVASceneState* newSceneState)
173 174 175 176 177 178
{
	assert(newSceneState);

	_newSceneState = newSceneState;
	if (_newSceneState == _curSceneState)
		return;
179
	// get reference to new scene
180 181
	_newSceneState->AddReference();

182
	// get difference between old scene state and current scene state
183 184
	CVASceneStateDiff diff;
	_newSceneState->Diff(_curSceneState, &diff);
185 186 187 188 189 190

	// update listeners
	updateListeners(&diff);

	// update sources
	updateSources(&diff);
Lucas Moesch's avatar
Lucas Moesch committed
191

192 193
	_clusterEngine->update();

Lucas Moesch's avatar
Lucas Moesch committed
194 195 196 197 198
	// Alte Szene freigeben (dereferenzieren)
	if (_curSceneState) _curSceneState->RemoveReference();
	
	_curSceneState = _newSceneState;
	_newSceneState = nullptr;
199 200
}

201
/*
202 203 204 205
ITADatasource* VABinauralRealTimeRenderer::GetOutputDatasource()
{
	return this;
}
206
*/
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
207

208 209 210 211 212 213 214

void
VABinauralRealTimeRenderer::updateSources( CVASceneStateDiff* diff )
{
	typedef std::vector< int >::const_iterator icit_t;

	// Delete sources
Lucas Moesch's avatar
Lucas Moesch committed
215
	for (icit_t it = diff->viDelSoundSourceIDs.begin(); it != diff->viDelSoundSourceIDs.end();)
216 217 218 219 220 221
	{
		const int& iID(*it++);
		deleteSource(iID);
	}

	// Add sources
Lucas Moesch's avatar
Lucas Moesch committed
222
	for (icit_t it = diff->viNewSoundSourceIDs.begin(); it != diff->viNewSoundSourceIDs.end();)
223 224 225 226 227 228 229 230 231 232 233 234 235 236
	{
		const int& iID(*it++);
		createSource(iID, _newSceneState->GetSoundSourceState(iID));
	}

	// update source trajectories
	std::map< int, VABinauralSoundSource* >::iterator it;

	for (it = _sources.begin(); it != _sources.end(); ++it)
	{
		int sourceID = it->first;
		VABinauralSoundSource* source = it->second;

		CVASoundSourceState* sourceCur = (_curSceneState ? _curSceneState->GetSoundSourceState(sourceID) : nullptr);
Lucas Moesch's avatar
Lucas Moesch committed
237
		CVASoundSourceState* sourceNew = (_newSceneState ? _newSceneState->GetSoundSourceState(sourceID) : nullptr);
238

239 240 241 242 243
		if (sourceNew && (sourceNew != sourceCur))
		{
			source->state = sourceNew;
		}

244 245 246 247 248 249
		const CVAMotionState* motionCur = (sourceCur ? sourceCur->GetMotionState() : nullptr);
		const CVAMotionState* motionNew = (sourceNew ? sourceNew->GetMotionState() : nullptr);

		if (motionNew && (motionNew != motionCur))
		{
			source->motionModel->InputMotionKey(motionNew);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
			// Dirty Hack
			if (!source->hasValidTrajectory)
			{
				VAVec3 pos = motionNew->GetPosition();
				
				if (pos != VAVec3(0, 0, 0))
				{
					VAVec3 view = motionNew->GetView();
					VAVec3 up = motionNew->GetUp();

					source->predPos = pos;
					source->predView = view;
					source->predUp = up;
					source->hasValidTrajectory = true;
				}
			}
266 267 268 269 270 271 272 273 274 275
		}
	}
}

void
VABinauralRealTimeRenderer::updateListeners( CVASceneStateDiff* diff )
{
	typedef std::vector< int >::const_iterator icit_t;

	// Delete listeners
Lucas Moesch's avatar
Lucas Moesch committed
276
	for (icit_t it = diff->viDelReceiverIDs.begin(); it != diff->viDelReceiverIDs.end();)
277 278 279 280 281 282
	{
		const int& iID(*it++);
		deleteListener(iID);
	}

	// Add listeners
Lucas Moesch's avatar
Lucas Moesch committed
283
	for (icit_t it = diff->viNewReceiverIDs.begin(); it != diff->viNewReceiverIDs.end();)
284 285 286 287 288
	{
		const int& iID(*it++);
		createListener(iID, _newSceneState->GetReceiverState(iID));
	}

Lucas Moesch's avatar
WIP  
Lucas Moesch committed
289
	// update listener trajectories and directivity
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	for (std::map< int, VABinauralListener* >::iterator it = _listeners.begin(); it != _listeners.end(); ++it)
	{
		int iListenerID = it->first;
		VABinauralListener* listener = it->second;

		CVAReceiverState* listenerCur = (_curSceneState ? _curSceneState->GetReceiverState(iListenerID) : nullptr);
		CVAReceiverState* listenerNew = (_newSceneState ? _newSceneState->GetReceiverState(iListenerID) : nullptr);

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

		if (motionNew && (motionNew != motionCur))
		{
			listener->motionModel->InputMotionKey(motionNew);
		}
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
305 306 307 308 309

		if (listenerNew != nullptr)
		{
			listener->directivity = (IVADirectivity*) listenerNew->GetDirectivity();
		}
310 311 312 313 314 315 316 317
	}
}

void
VABinauralRealTimeRenderer::createSource( const int sourceID, const CVASoundSourceState* sourceState )
{
	VABinauralSoundSource* source = dynamic_cast< VABinauralSoundSource* >(_sourcePool->RequestObject()); // Reference = 1

318 319
	// set state
	source->state = (CVASoundSourceState*) sourceState;
320 321 322 323 324 325 326 327 328 329 330 331 332
	// set internal data
	source->data = _core->GetSceneManager()->GetSoundSourceDesc(sourceID);
	source->data->AddReference();

	// set motion model
	CVABasicMotionModel* motionInstance = dynamic_cast< CVABasicMotionModel* >(source->motionModel->GetInstance());
	motionInstance->SetName(std::string("bfrend_mm_source_" + source->data->sName));
	motionInstance->Reset();

	// add local reference
	_sources.insert(std::pair< int, VABinauralSoundSource* >(sourceID, source));

	// add source to clustering
333
	_clusterEngine->addSource(sourceID, source);
334 335 336 337 338 339 340 341 342 343 344
}

void
VABinauralRealTimeRenderer::deleteSource(int sourceID)
{
	// remove local source reference
	std::map< int, VABinauralSoundSource* >::iterator it = _sources.find(sourceID);
	VABinauralSoundSource* source = it->second;
	_sources.erase(it);

	// remove listener reference from clustering
345
	_clusterEngine->removeSource(sourceID);
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
346
	source->RemoveReference();
347
}
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

void
VABinauralRealTimeRenderer::createListener(int listenerID, const CVAReceiverState* recevierState)
{
	VABinauralListener* listener = dynamic_cast< VABinauralListener* >(_listenerPool->RequestObject()); // Reference = 1

	// set internal data
	listener->data = _core->GetSceneManager()->GetListenerDesc(listenerID);
	listener->data->AddReference();

	// set motion model
	CVABasicMotionModel* motionInstance = dynamic_cast< CVABasicMotionModel* >(listener->motionModel->GetInstance());
	motionInstance->SetName(std::string("bfrend_mm_listener_" + listener->data->sName));
	motionInstance->Reset();

Lucas Moesch's avatar
WIP  
Lucas Moesch committed
363 364 365
	//set directivity
	listener->directivity = ( IVADirectivity* ) recevierState->GetDirectivity();

366 367 368 369
	// add local reference
	_listeners.insert(std::pair< int, VABinauralListener* >(listenerID, listener));

	// add listener to clustering
Lucas Moesch's avatar
Lucas Moesch committed
370
	VABinauralClusterEngine::clusterConfig_t config = { /*numClusters=*/ 9};
371
	_clusterEngine->addListener(listenerID, listener, config);
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
}

void
VABinauralRealTimeRenderer::deleteListener(int listenerID)
{
	// remove local listener reference
	std::map< int, VABinauralListener* >::iterator it = _listeners.find(listenerID);
	VABinauralListener* listener = it->second;
	_listeners.erase(it);

	//listener->bDeleted = true;
	//listener->pData->RemoveReference();
	listener->RemoveReference();

	// remove listener reference from clustering
387
	_clusterEngine->removeListener(listenerID);
388

Lucas Moesch's avatar
wip  
Lucas Moesch committed
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 414
}
void VABinauralRealTimeRenderer::updateMotionStates()
{
	// Neue Quellendaten bernehmen
	for (std::map< int, VABinauralSoundSource* >::iterator it = _sources.begin(); it != _sources.end(); ++it)
	{
		int sourceID = it->first;
		VABinauralSoundSource* source = it->second;

		CVASoundSourceState* sourceCur = (_curSceneState ? _curSceneState->GetSoundSourceState(sourceID) : nullptr);
		CVASoundSourceState* sourceNew = (_newSceneState ? _newSceneState->GetSoundSourceState(sourceID) : nullptr);

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

		if (motionNew && (motionNew != motionCur))
		{
			//VA_TRACE("BinauralFreeFieldAudioRenderer", "Source " << iSourceID << " new motion state");
			source->motionModel->InputMotionKey(motionNew);
		}
	}
}

void
VABinauralRealTimeRenderer::updateTrajectories( double time )
{
Lucas Moesch's avatar
Lucas Moesch committed
415 416

	for (auto const& sourceIt: _sources)
Lucas Moesch's avatar
wip  
Lucas Moesch committed
417 418
	{
		bool isValid = true;
Lucas Moesch's avatar
Lucas Moesch committed
419
		VABinauralSoundSource* source = sourceIt.second;
Lucas Moesch's avatar
wip  
Lucas Moesch committed
420 421 422 423 424 425

		source->motionModel->HandleMotionKeys();
		isValid &= source->motionModel->EstimatePosition(time, source->predPos);
		isValid &= source->motionModel->EstimateOrientation(time, source->predView, source->predUp);
		source->hasValidTrajectory = isValid;
	}
Lucas Moesch's avatar
Lucas Moesch committed
426 427 428 429 430 431 432 433 434 435 436 437


	for (auto const& listenerIt: _listeners)
	{
		bool isValid = true;
		VABinauralListener* listener = listenerIt.second;

		listener->motionModel->HandleMotionKeys();
		isValid &= listener->motionModel->EstimatePosition(time, listener->predPos);
		isValid &= listener->motionModel->EstimateOrientation(time, listener->predView, listener->predUp);
		listener->hasValidTrajectory = isValid;
	}
Lucas Moesch's avatar
wip  
Lucas Moesch committed
438
}