VABinauralClustering.cpp 3.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#define NOMINMAX

#include <limits>
#include <algorithm>

#include <math.h>

// VA
#include <VAObjectPool.h>

11 12 13
// ITA includes
#include <ITAUPFilter.h>

14 15 16 17
// Utils
#include "./VABinauralClustering.h"
#include "./VABinauralClusterPoolFactory.h"

Lucas Moesch's avatar
Lucas Moesch committed
18
VABinauralClustering::VABinauralClustering()
19 20
{
	IVAPoolObjectFactory* clusterFactory = new VABinauralClusterPoolFactory();
21
	clusterPool = IVAObjectPool::Create(16, 2, clusterFactory, true);
22 23
};

24
VABinauralClustering::~VABinauralClustering(){};
25 26 27 28 29 30 31

void
VABinauralClustering::addSource(int sourceID, VABinauralSoundSource* source)
{
	_unassignedSources.insert(std::pair< int, VABinauralSoundSource* >(sourceID, source));
};

Lucas Moesch's avatar
WIP  
Lucas Moesch committed
32 33 34 35 36 37
void 
VABinauralClustering::removeSource(int sourceID)
{
	_delSourceIDs.insert(sourceID);
}

38
void
Lucas Moesch's avatar
Lucas Moesch committed
39
VABinauralClustering::init(int listenerID, VABinauralListener* listener, int numClusters)
40 41 42 43 44
{
	_listenerID = listenerID;
	_listener = listener;
	_numClusters = numClusters;
	_threshold = cos(180. / numClusters) * cos(180. / numClusters);
45 46 47

	_output = new ITASampleFrame(2, listener->output->GetLength(), true);

Lucas Moesch's avatar
Lucas Moesch committed
48
	_curState.reset(new VABinauralClusteringState(_numClusters, _listener, clusterPool));
49 50
}

51
ITASampleFrame*
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
52
VABinauralClustering::getOutput()
53 54 55
{	
	_output->zero();
	// swap out clustering state
56 57 58 59 60 61
	if (_nextState != nullptr){
		_curState.reset(_nextState.release());
	};

	std::map< int, VABinauralCluster*>::const_iterator clusterIt;
	
62
	for (clusterIt = _curState->clusters.begin(); clusterIt != _curState->clusters.end(); ++clusterIt)
63
	{
64
		ITASampleFrame* clusterOutput = clusterIt->second->getOutput();
65 66 67 68 69 70

		(*_output)[0] += (*clusterOutput)[0];
		(*_output)[1] += (*clusterOutput)[1];
	}

	return _output;
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
71 72
}

73 74 75
void
VABinauralClustering::update()
{
76 77
//	if (_nextState == nullptr)
//	{
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
78

Lucas Moesch's avatar
Lucas Moesch committed
79
		VABinauralClusteringState* state = new VABinauralClusteringState(_numClusters, _listener, clusterPool); //new VABinauralClusteringState(*_curState);
Lucas Moesch's avatar
wip  
Lucas Moesch committed
80

Lucas Moesch's avatar
Lucas Moesch committed
81 82
		// remove removed sources 
		std::set< int >::const_iterator it;
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
83

Lucas Moesch's avatar
Lucas Moesch committed
84 85 86 87 88
		for (it = _delSourceIDs.begin(); it != _delSourceIDs.end(); ++it)
		{
			// remove if in unassigned sources
			std::map< int, VABinauralSoundSource* >::iterator delIt = _unassignedSources.find(*it);
			VABinauralSoundSource* source = delIt->second;
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
89

Lucas Moesch's avatar
Lucas Moesch committed
90
			state->removeSource(*it);
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
91

Lucas Moesch's avatar
Lucas Moesch committed
92
			_unassignedSources.erase(delIt);
93

Lucas Moesch's avatar
Lucas Moesch committed
94 95 96
			// remove if in assigned sources
			delIt = _assignedSources.find(*it);
			source = delIt->second;
97

Lucas Moesch's avatar
Lucas Moesch committed
98 99
			_assignedSources.erase(delIt);
		}
100

Lucas Moesch's avatar
Lucas Moesch committed
101 102
		// add unassigned sources
		std::map< int, VABinauralSoundSource* >::iterator sourceIt;
103

Lucas Moesch's avatar
Lucas Moesch committed
104 105
		for (sourceIt = _unassignedSources.begin(); sourceIt != _unassignedSources.end(); ++sourceIt)
		{
106 107 108
			if (sourceIt->second->hasValidTrajectory){
				state->addSource(sourceIt->first, sourceIt->second, _threshold, 0);
			};
Lucas Moesch's avatar
Lucas Moesch committed
109
		}
110

Lucas Moesch's avatar
Lucas Moesch committed
111
		// TODO: refinement
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
112

Lucas Moesch's avatar
Lucas Moesch committed
113
		// update source status
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
114
		/*for (auto const& unassignedSources : _unassignedSources)
Lucas Moesch's avatar
Lucas Moesch committed
115 116
		{
			_assignedSources.insert(std::pair< int, VABinauralSoundSource* >(assignedSources.first, assignedSources.second));
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
117
		}*/
Lucas Moesch's avatar
Lucas Moesch committed
118

Lucas Moesch's avatar
WIP  
Lucas Moesch committed
119
		//_unassignedSources.clear();
Lucas Moesch's avatar
Lucas Moesch committed
120 121

		_nextState.reset(state);
122
//	}
123 124 125 126 127 128 129
}

void
VABinauralClustering::PreRequest(){};

void
VABinauralClustering::PreRelease(){};