VABinauralRealTimeRenderer.cpp 10.3 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 19 20
// Utils
#include "./Utils/BinauralListener/VABinauralListenerPoolFactory.h"
#include "./Utils/BinauralSoundSource/VABinauralSoundSourcePoolFactory.h"
21

22 23
VABinauralRealTimeRenderer::VABinauralRealTimeRenderer( const CVAAudioRendererInitParams& params ) :
	ITADatasourceRealization(2, params.pCore->GetCoreConfig()->oAudioDriverConfig.dSampleRate, params.pCore->GetCoreConfig()->oAudioDriverConfig.iBuffersize),
24 25
	_params(params),
	_core(params.pCore),
26 27 28
	_newSceneState(NULL), 
	_curSceneState(NULL), 
	_indicateReset(false), 
29 30
	_resetAck(false),
	_clusterEngine()
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 41 42 43 44
}


VABinauralRealTimeRenderer::~VABinauralRealTimeRenderer()
{
}
45

46 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
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
98 99 100

	_defaultSourceConf.blockLength = GetBlocklength();
	_defaultSourceConf.sampleRate = GetSampleRate();
101 102
}

103 104 105
void
VABinauralRealTimeRenderer::processStream(const ITAStreamInfo* streamInfo)
{
106
	ITASampleFrame* output;
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
107 108 109 110 111 112 113 114 115
	float* outputChL = GetWritePointer(0);
	float* outputChR = GetWritePointer(1);

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

	const CVAAudiostreamState* streamState = dynamic_cast< const CVAAudiostreamState* >(streamInfo);
	double dListenerTime = streamState->dSysTime;

116 117 118 119 120 121 122 123 124 125
	// -- create output for every listener
	std::map< int, VABinauralListener* >::const_iterator it;
	for (it = _listeners.begin(); it != _listeners.end(); ++it)
	{
		VABinauralClustering* clustering = _clusterEngine.getClustering(it->first);
		output = clustering->getOutput();

		fm_copy(outputChL, (*output)[0].data(), m_uiBlocklength);
		fm_copy(outputChR, (*output)[1].data(), m_uiBlocklength);
	}
126

Lucas Moesch's avatar
WIP  
Lucas Moesch committed
127 128 129 130

	IncrementWritePointer();

	return;
131 132 133 134 135 136 137 138 139 140
}

void
VABinauralRealTimeRenderer::updateScene(CVASceneState* newSceneState)
{
	assert(newSceneState);

	_newSceneState = newSceneState;
	if (_newSceneState == _curSceneState)
		return;
141
	// get reference to new scene
142 143
	_newSceneState->AddReference();

144
	// get difference between old scene state and current scene state
145 146
	CVASceneStateDiff diff;
	_newSceneState->Diff(_curSceneState, &diff);
147 148 149 150 151 152 153 154

	// update listeners
	updateListeners(&diff);

	// update sources
	updateSources(&diff);
}

155
/*
156 157 158 159
ITADatasource* VABinauralRealTimeRenderer::GetOutputDatasource()
{
	return this;
}
160
*/
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

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

	// Delete sources
	for (icit_t it = diff->viDelSoundSourceIDs.begin(); it != diff->viDelSoundSourceIDs.end(); ++it)
	{
		const int& iID(*it++);
		deleteSource(iID);
	}

	// Add sources
	for (icit_t it = diff->viNewSoundSourceIDs.begin(); it != diff->viNewSoundSourceIDs.end(); ++it)
	{
		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);
		CVASoundSourceState* sourceNew = (_newSceneState ? _curSceneState->GetSoundSourceState(sourceID) : nullptr);

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

		if (motionNew && (motionNew != motionCur))
		{
			source->motionModel->InputMotionKey(motionNew);
		}
	}

	_clusterEngine.update();
}

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

	// Delete listeners
	for (icit_t it = diff->viDelReceiverIDs.begin(); it != diff->viDelReceiverIDs.end(); ++it)
	{
		const int& iID(*it++);
		deleteListener(iID);
	}

	// Add listeners
	for (icit_t it = diff->viNewReceiverIDs.begin(); it != diff->viNewReceiverIDs.end(); ++it)
	{
		const int& iID(*it++);
		createListener(iID, _newSceneState->GetReceiverState(iID));
	}

	// update listener trajectories
	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);
		}
	}

}

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

	// 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
	_clusterEngine.addSource(sourceID, source);
}

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

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

	// remove listener reference from clustering
	_clusterEngine.removeListener(sourceID);

280
}
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

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();

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

	// add listener to clustering
	VABinauralClusterEngine::clusterConfig_t config = { /*numClusters=*/ 9 };
	_clusterEngine.addListener(listenerID, listener, config);
}

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
	_clusterEngine.removeListener(listenerID);

}