Commit ffeae456 authored by Lucas Moesch's avatar Lucas Moesch

Added TOAEstimator and implemented ITD in clusters.

parent 2ab5d6f6
#define NOMINMAX
#include <math.h>
#include "VABinauralCluster.h" #include "VABinauralCluster.h"
// Utils
#include "../BinauralTimeOfArrivalEstimator/VABinauralTOAEstimator.h"
VABinauralCluster::VABinauralCluster() VABinauralCluster::VABinauralCluster()
{ {
} }
...@@ -15,25 +21,87 @@ VABinauralCluster::VABinauralCluster(const VABinauralCluster& cluster) : ...@@ -15,25 +21,87 @@ VABinauralCluster::VABinauralCluster(const VABinauralCluster& cluster) :
VABinauralCluster::~VABinauralCluster() VABinauralCluster::~VABinauralCluster()
{ {
VABinauralSoundSource* source;
std::map<int, VABinauralSoundSource*>::const_iterator it;
// clear all references from this cluster
for (it = _sources.begin(); it != _sources.end();)
{
source = it->second;
it = _sources.erase(it);
source->RemoveReference();
}
} }
void void
VABinauralCluster::init(int sourceID, VABinauralSoundSource* source) VABinauralCluster::init(int sourceID, VABinauralSoundSource* source, VABinauralListener* listener)
{ {
_listener = listener;
_listenerPos = listener->predPos;
_clusterSourcePos = _clusterSourcePos + source->predPos; _clusterSourcePos = _clusterSourcePos + source->predPos;
_clusterSourceToListenerPos = _clusterSourcePos - _listenerPos;
_output = new ITASampleFrame(2, listener->output->GetLength(), true);
_tmpChL.Init(listener->output->GetLength(), true);
_tmpChR.Init(listener->output->GetLength(), true);
maxError = getDistError(source); maxError = getDistError(source);
_sources.insert(std::pair<int, VABinauralSoundSource*>(sourceID, source)); _sources.insert(std::pair<int, VABinauralSoundSource*>(sourceID, source));
// --
source->AddReference();
++_numSources; ++_numSources;
} }
ITASampleFrame*
VABinauralCluster::getOutput()
{
// reset output Buffer
_output->zero();
// set VDL Values
std::map<int, VABinauralSoundSource*>::const_iterator it;
for (it = _sources.begin(); it != _sources.end(); ++it)
{
VABinauralSoundSource* source = it->second;
const ITASampleBuffer* input = source->data->pSignalSourceInputBuf;
VAVec3 sourceToListenerPos = source->predPos - _listenerPos;
sourceToListenerPos.Norm();
double phi = atan2(sourceToListenerPos.y, sourceToListenerPos.x);
double theta = acos(sourceToListenerPos.z);
// add general distance VDL
double toaChL = _listener->toaEstimator->getTOALeft(phi, theta);
double toaChR = _listener->toaEstimator->getTOARight(phi, theta);
source->vdlChL->SetDelayTime(toaChL);
source->vdlChR->SetDelayTime(toaChR);
source->vdlChL->Process(input, &(_tmpChL));
source->vdlChR->Process(input, &(_tmpChR));
(*_output)[0] += _tmpChL;
(*_output)[1] += _tmpChR;
}
// convolve
return _output;
}
double double
VABinauralCluster::getDistError(VABinauralSoundSource* source) VABinauralCluster::getDistError(VABinauralSoundSource* source)
{ {
double dotp = _clusterSourcePos.Dot(source->predPos); VAVec3 sourceToListenerPos = source->predPos - _listenerPos;
double dotp = _clusterSourceToListenerPos.Dot(sourceToListenerPos);
return ((dotp * dotp) / (_clusterSourcePos.Dot(_clusterSourcePos)) * (source->predPos.Dot(source->predPos))); return (dotp * dotp) / (_clusterSourcePos.Dot(_clusterSourcePos) * sourceToListenerPos.Dot(sourceToListenerPos));
} }
void void
...@@ -42,19 +110,30 @@ VABinauralCluster::addSource(int sourceID, VABinauralSoundSource* source) ...@@ -42,19 +110,30 @@ VABinauralCluster::addSource(int sourceID, VABinauralSoundSource* source)
double err = getDistError(source); double err = getDistError(source);
_clusterSourcePos = (source->predPos + _clusterSourcePos * _numSources) / (_numSources + 1); _clusterSourcePos = (source->predPos + _clusterSourcePos * _numSources) / (_numSources + 1);
_clusterSourceToListenerPos = _clusterSourcePos - _listenerPos;
maxError = std::max(err, maxError); maxError = std::max(err, maxError);
_sources.insert(std::pair<int, VABinauralSoundSource*>(sourceID, source)); _sources.insert(std::pair<int, VABinauralSoundSource*>(sourceID, source));
// --
source->AddReference();
++_numSources; ++_numSources;
} }
void void
VABinauralCluster::addSource(VABinauralSoundSource* source, double error) VABinauralCluster::addSource(int sourceID, VABinauralSoundSource* source, double error)
{ {
_clusterSourcePos = (source->predPos + _clusterSourcePos * _numSources) / (_numSources + 1); _clusterSourcePos = (source->predPos + _clusterSourcePos * _numSources) / (_numSources + 1);
_clusterSourceToListenerPos = _clusterSourcePos - _listenerPos;
maxError = std::max(error, maxError); maxError = std::max(error, maxError);
_sources.insert(std::pair<int, VABinauralSoundSource*>(sourceID, source));
// --
source->AddReference();
++_numSources; ++_numSources;
} }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
// Utils // Utils
#include "../BinauralSoundSource/VABinauralSoundSource.h" #include "../BinauralSoundSource/VABinauralSoundSource.h"
#include "../BinauralListener/VABinauralListener.h"
class VABinauralCluster : public CVAPoolObject class VABinauralCluster : public CVAPoolObject
...@@ -20,7 +21,10 @@ public: ...@@ -20,7 +21,10 @@ public:
~VABinauralCluster(); ~VABinauralCluster();
void void
init(int sourceID, VABinauralSoundSource* source); init(int sourceID, VABinauralSoundSource* source, VABinauralListener* listener);
ITASampleFrame*
getOutput();
double double
getDistError(VABinauralSoundSource* source); getDistError(VABinauralSoundSource* source);
...@@ -29,7 +33,7 @@ public: ...@@ -29,7 +33,7 @@ public:
addSource(int sourceID, VABinauralSoundSource* source); addSource(int sourceID, VABinauralSoundSource* source);
void void
addSource(VABinauralSoundSource* source, double error); addSource(int sourceID, VABinauralSoundSource* source, double error);
void void
removeSource(); removeSource();
...@@ -39,9 +43,20 @@ public: ...@@ -39,9 +43,20 @@ public:
private: private:
int _numSources; int _numSources;
std::map<int, VABinauralSoundSource* > _sources;
ITASampleBuffer _tmpChL;
ITASampleBuffer _tmpChR;
ITASampleFrame* _output;
VABinauralListener* _listener;
VAVec3 _listenerPos;
VAVec3 _clusterSourcePos; VAVec3 _clusterSourcePos;
VAVec3 _clusterSourceToListenerPos;
std::map<int, VABinauralSoundSource* > _sources;
double double
cummulativeMovingAverage(int n, double average, double value); cummulativeMovingAverage(int n, double average, double value);
......
...@@ -24,6 +24,14 @@ VABinauralClusterEngine::update() ...@@ -24,6 +24,14 @@ VABinauralClusterEngine::update()
} }
} }
VABinauralClustering*
VABinauralClusterEngine::getClustering(int listenerID)
{
std::map< int, VABinauralClustering* >::iterator it = _clusterings.find(listenerID);
return it->second;
}
void void
VABinauralClusterEngine::addSource( int sourceID, VABinauralSoundSource* source ) VABinauralClusterEngine::addSource( int sourceID, VABinauralSoundSource* source )
{ {
...@@ -50,9 +58,11 @@ void ...@@ -50,9 +58,11 @@ void
VABinauralClusterEngine::removeListener(int listenerID) VABinauralClusterEngine::removeListener(int listenerID)
{ {
std::map< int, VABinauralClustering* >::iterator it = _clusterings.find(listenerID); std::map< int, VABinauralClustering* >::iterator it = _clusterings.find(listenerID);
VABinauralClustering* listener = it->second; VABinauralClustering* clustering = it->second;
_clusterings.erase(it); _clusterings.erase(it);
clustering->RemoveReference();
/* /*
TODO: foreach cluster in CL delete cluster TODO: foreach cluster in CL delete cluster
*/ */
......
...@@ -28,6 +28,9 @@ public: ...@@ -28,6 +28,9 @@ public:
void void
update(); update();
VABinauralClustering*
getClustering(int listenerID);
void void
addSource( int sourceID, VABinauralSoundSource* source ); addSource( int sourceID, VABinauralSoundSource* source );
......
...@@ -12,13 +12,13 @@ ...@@ -12,13 +12,13 @@
#include "./VABinauralClustering.h" #include "./VABinauralClustering.h"
#include "./VABinauralClusterPoolFactory.h" #include "./VABinauralClusterPoolFactory.h"
VABinauralClustering::~VABinauralClustering() VABinauralClustering::VABinauralClustering()
{ {
IVAPoolObjectFactory* clusterFactory = new VABinauralClusterPoolFactory(); IVAPoolObjectFactory* clusterFactory = new VABinauralClusterPoolFactory();
_clusterPool = IVAObjectPool::Create(16, 2, clusterFactory, true); _clusterPool = IVAObjectPool::Create(16, 2, clusterFactory, true);
}; };
VABinauralClustering::VABinauralClustering(){}; VABinauralClustering::~VABinauralClustering(){};
void void
VABinauralClustering::addSource(int sourceID, VABinauralSoundSource* source) VABinauralClustering::addSource(int sourceID, VABinauralSoundSource* source)
...@@ -33,12 +33,31 @@ VABinauralClustering::init(int listenerID, VABinauralListener* listener, int num ...@@ -33,12 +33,31 @@ VABinauralClustering::init(int listenerID, VABinauralListener* listener, int num
_listener = listener; _listener = listener;
_numClusters = numClusters; _numClusters = numClusters;
_threshold = cos(180. / numClusters) * cos(180. / numClusters); _threshold = cos(180. / numClusters) * cos(180. / numClusters);
_output = new ITASampleFrame(2, listener->output->GetLength(), true);
_curState.reset(new VABinauralClusteringState(_numClusters, _listener));
} }
VABinauralClustering::binauralOutput_t ITASampleFrame*
VABinauralClustering::getOutput() VABinauralClustering::getOutput()
{ {
_curState.reset(_nextState.release()); // --
_output->zero();
// swap out clustering state
_curState.reset(_nextState.release()); //TODO: only if next State is not null!
std::map< int, VABinauralCluster*>::const_iterator it;
for (it = _curState->clusters.begin(); it != _curState->clusters.end(); ++it)
{
ITASampleFrame* clusterOutput = it->second->getOutput();
(*_output)[0] += (*clusterOutput)[0];
(*_output)[1] += (*clusterOutput)[1];
}
return _output;
} }
void void
...@@ -56,6 +75,7 @@ VABinauralClustering::update() ...@@ -56,6 +75,7 @@ VABinauralClustering::update()
} }
// TODO: refinement // TODO: refinement
for (it = _unassignedSources.begin(); it != _unassignedSources.end(); ++it) for (it = _unassignedSources.begin(); it != _unassignedSources.end(); ++it)
{ {
_assignedSources.insert(std::pair< int, VABinauralSoundSource* >(it->first, it->second)); _assignedSources.insert(std::pair< int, VABinauralSoundSource* >(it->first, it->second));
......
...@@ -12,15 +12,13 @@ ...@@ -12,15 +12,13 @@
#include "VABinauralClusteringState.h" #include "VABinauralClusteringState.h"
#include "../BinauralListener/VABinauralListener.h" #include "../BinauralListener/VABinauralListener.h"
#include "../BinauralSoundSource/VABinauralSoundSource.h" #include "../BinauralSoundSource/VABinauralSoundSource.h"
#include "../BinauralTimeOfArrivalEstimator/VABinauralTOAEstimator.h"
class VABinauralClustering : public CVAPoolObject class VABinauralClustering : public CVAPoolObject
{ {
public: // friend VABinauralClusteringState;
struct binauralOutput_t {
ITASampleBuffer outputChL;
ITASampleBuffer outputChR;
};
public:
~VABinauralClustering(); ~VABinauralClustering();
VABinauralClustering(); VABinauralClustering();
...@@ -28,7 +26,7 @@ public: ...@@ -28,7 +26,7 @@ public:
void void
init(int listenerID, VABinauralListener* listener, int numClusters); init(int listenerID, VABinauralListener* listener, int numClusters);
binauralOutput_t ITASampleFrame*
getOutput(); getOutput();
void void
...@@ -43,6 +41,8 @@ private: ...@@ -43,6 +41,8 @@ private:
int _numClusters; int _numClusters;
double _threshold; double _threshold;
ITASampleFrame* _output;
std::unique_ptr<VABinauralClusteringState> _curState; std::unique_ptr<VABinauralClusteringState> _curState;
std::unique_ptr<VABinauralClusteringState> _nextState; std::unique_ptr<VABinauralClusteringState> _nextState;
......
...@@ -10,7 +10,9 @@ ...@@ -10,7 +10,9 @@
#include "./VABinauralClusterPoolFactory.h" #include "./VABinauralClusterPoolFactory.h"
VABinauralClusteringState::VABinauralClusteringState(int numClusters) VABinauralClusteringState::VABinauralClusteringState(int numClusters, VABinauralListener* listener) :
numClusters(numClusters),
listener(listener)
{ {
for (int i = numClusters - 1; i >= 0; --i) for (int i = numClusters - 1; i >= 0; --i)
{ {
...@@ -25,6 +27,7 @@ VABinauralClusteringState::~VABinauralClusteringState() ...@@ -25,6 +27,7 @@ VABinauralClusteringState::~VABinauralClusteringState()
VABinauralClusteringState::VABinauralClusteringState(const VABinauralClusteringState& state) : VABinauralClusteringState::VABinauralClusteringState(const VABinauralClusteringState& state) :
numClusters(state.numClusters), numClusters(state.numClusters),
listener(state.listener),
sourceClusterReference(state.sourceClusterReference) sourceClusterReference(state.sourceClusterReference)
{ {
std::map< int, VABinauralCluster* >::const_iterator it; std::map< int, VABinauralCluster* >::const_iterator it;
...@@ -68,7 +71,7 @@ VABinauralClusteringState::addSource(int sourceID, VABinauralSoundSource* source ...@@ -68,7 +71,7 @@ VABinauralClusteringState::addSource(int sourceID, VABinauralSoundSource* source
nearestCluster = p.second; nearestCluster = p.second;
} }
nearestCluster->addSource(source, minerr); nearestCluster->addSource(sourceID, source, minerr);
sourceClusterReference.insert(std::pair< int, int >(sourceID, nearestClusterID)); sourceClusterReference.insert(std::pair< int, int >(sourceID, nearestClusterID));
} }
...@@ -78,7 +81,7 @@ VABinauralClusteringState::createCluster(int sourceID, VABinauralSoundSource* s ...@@ -78,7 +81,7 @@ VABinauralClusteringState::createCluster(int sourceID, VABinauralSoundSource* s
int clusterID = freeClusterIDs.back(); int clusterID = freeClusterIDs.back();
VABinauralCluster* cluster = dynamic_cast< VABinauralCluster* >(_clusterPool->RequestObject()); // Reference = 1 VABinauralCluster* cluster = dynamic_cast< VABinauralCluster* >(_clusterPool->RequestObject()); // Reference = 1
cluster->init(sourceID, source); cluster->init(sourceID, source, listener);
clusters.insert(std::pair< int, VABinauralCluster* >(clusterID, cluster)); clusters.insert(std::pair< int, VABinauralCluster* >(clusterID, cluster));
freeClusterIDs.pop(); freeClusterIDs.pop();
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
// Utils // Utils
#include "VABinauralCluster.h" #include "VABinauralCluster.h"
#include "../BinauralSoundSource/VABinauralSoundSource.h" #include "../BinauralSoundSource/VABinauralSoundSource.h"
#include "../BinauralListener/VABinauralListener.h"
class VABinauralClusteringState class VABinauralClusteringState
{ {
...@@ -20,7 +21,9 @@ public: ...@@ -20,7 +21,9 @@ public:
std::map< int, int> sourceClusterReference; std::map< int, int> sourceClusterReference;
std::map< int, VABinauralCluster* > clusters; std::map< int, VABinauralCluster* > clusters;
VABinauralClusteringState(int numClusters); VABinauralListener* listener;
VABinauralClusteringState(int numClusters, VABinauralListener* listener);
VABinauralClusteringState(const VABinauralClusteringState& state); VABinauralClusteringState(const VABinauralClusteringState& state);
......
...@@ -22,13 +22,16 @@ VABinauralListener::PreRequest() ...@@ -22,13 +22,16 @@ VABinauralListener::PreRequest()
listenerMotionConf.dWindowSize = _conf.motionModelWindowSize; listenerMotionConf.dWindowSize = _conf.motionModelWindowSize;
listenerMotionConf.iNumHistoryKeys = _conf.motionModelNumHistoryKeys; listenerMotionConf.iNumHistoryKeys = _conf.motionModelNumHistoryKeys;
sfOutput = new ITASampleFrame( output = new ITASampleFrame(
2, 2,
_core->GetCoreConfig()->oAudioDriverConfig.iBuffersize, _core->GetCoreConfig()->oAudioDriverConfig.iBuffersize,
true true
); );
motionModel = new CVASharedMotionModel(new CVABasicMotionModel(listenerMotionConf), true); motionModel = new CVASharedMotionModel(new CVABasicMotionModel(listenerMotionConf), true);
// TODO: load HRTF from some kind of config
toaEstimator = new VABinauralTOAEstimator();
data = nullptr; data = nullptr;
} }
......
...@@ -13,6 +13,9 @@ ...@@ -13,6 +13,9 @@
// ITA includes // ITA includes
#include <ITASampleFrame.h> #include <ITASampleFrame.h>
// Utils
#include "../BinauralTimeOfArrivalEstimator/VABinauralTOAEstimator.h"
class VABinauralListener : public CVAPoolObject class VABinauralListener : public CVAPoolObject
{ {
public: public:
...@@ -33,7 +36,9 @@ public: ...@@ -33,7 +36,9 @@ public:
VAVec3 prefView; VAVec3 prefView;
VAVec3 predUp; VAVec3 predUp;
ITASampleFrame* sfOutput; ITASampleFrame* output;
VABinauralTOAEstimator* toaEstimator;
VABinauralListener(const CVACoreImpl* core, const config_t& conf); VABinauralListener(const CVACoreImpl* core, const config_t& conf);
......