VABinauralCluster.cpp 5.1 KB
Newer Older
1
2
3
#define NOMINMAX

#include <math.h>
4
5
#include "VABinauralCluster.h"

6
7
8
// ITA includes
#include <ITAConstants.h>

9
// Utils
10
#include "../RelationMetrics/VARelationMetrics.h"
11
12
#include "../BinauralTimeOfArrivalEstimator/VABinauralTOAEstimator.h"

13
14
15
16
17
18
VABinauralCluster::VABinauralCluster()
{
}

VABinauralCluster::VABinauralCluster(const VABinauralCluster& cluster) :
	maxError(cluster.maxError),
Lucas Moesch's avatar
wip    
Lucas Moesch committed
19
	numSources(cluster.numSources),
20
21
22
23
24
25
26
27
	_clusterSourcePos(cluster._clusterSourcePos)
{

}


VABinauralCluster::~VABinauralCluster()
{
28
29
30
31
32
33
34
35
36
37
38
	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();
	}
39
40
41
}

void
42
VABinauralCluster::init(int sourceID, VABinauralSoundSource* source, VABinauralListener* listener)
43
{
44
45
	_listener = listener;
	_listenerPos = listener->predPos;
46
	_clusterSourcePos = _clusterSourcePos + source->predPos;
47
48
49
50
51
52
53
	_clusterSourceToListenerPos = _clusterSourcePos - _listenerPos;

	_output = new ITASampleFrame(2, listener->output->GetLength(), true);
	
	_tmpChL.Init(listener->output->GetLength(), true);
	_tmpChR.Init(listener->output->GetLength(), true);

54
55
	maxError = getDistError(source);

Lucas Moesch's avatar
WIP    
Lucas Moesch committed
56
57
	_sources.insert(std::pair<int, VABinauralSoundSource*>(sourceID, source));

58
59
60
	// --
	source->AddReference();

Lucas Moesch's avatar
wip    
Lucas Moesch committed
61
	++numSources;
62
63
}

Lucas Moesch's avatar
Lucas Moesch committed
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
void
VABinauralCluster::init(VABinauralCluster* cluster)
{
	_listener = cluster->_listener;
	_listenerPos = _listener->predPos;
	_clusterSourcePos = cluster->_clusterSourcePos;
	_clusterSourceToListenerPos = _clusterSourcePos - _listenerPos;

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

	_tmpChL.Init(_listener->output->GetLength(), true);
	_tmpChR.Init(_listener->output->GetLength(), true);

	maxError = cluster->maxError;

	std::map< int, VABinauralSoundSource*>::const_iterator it;
	for (it = cluster->_sources.begin(); it != cluster->_sources.end(); ++it)
	{
		_sources.insert(std::pair<int, VABinauralSoundSource*>(it->first, it->second));
		it->second->AddReference();
	}

	numSources = cluster->numSources;
}

89
90
91
92
93
94
ITASampleFrame*
VABinauralCluster::getOutput()
{
	// reset output Buffer
	_output->zero();

95
96
97
98
99
100
101
	// get cluster HRTF TOA
	VARelationMetrics clusterSourceMetrics;
	clusterSourceMetrics.calc(_listener->predPos, _listener->predView, _listener->predUp, _clusterSourcePos);

	double toaHRTFChL = _listener->toaEstimator->getTOALeft(clusterSourceMetrics.phi, clusterSourceMetrics.theta);
	double toaHRTFChR = _listener->toaEstimator->getTOARight(clusterSourceMetrics.phi, clusterSourceMetrics.theta);

102
103
104
105
106
107
108
	// 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;

109
110
		VARelationMetrics sourceMetrics;
		sourceMetrics.calc(_listener->predPos, _listener->predView, _listener->predUp, source->predPos);
111

112
113
114
115
		double toaDistance = sourceMetrics.dist / 343; // TODO: Medium Propagation....
	
		double toaSourceChL = _listener->toaEstimator->getTOALeft(sourceMetrics.phi, sourceMetrics.theta);
		double toSourceaChR = _listener->toaEstimator->getTOARight(sourceMetrics.phi, sourceMetrics.theta);
116

117
118
		source->vdlChL->SetDelayTime(toaDistance + toaSourceChL - toaHRTFChL);
		source->vdlChR->SetDelayTime(toaDistance + toSourceaChR - toaHRTFChR);
119
120
121
122
123
124
125
126

		source->vdlChL->Process(input, &(_tmpChL));
		source->vdlChR->Process(input, &(_tmpChR));

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

127
	// convolve here!
128
129
130
131

	return _output;
}

132
133
134
double
VABinauralCluster::getDistError(VABinauralSoundSource* source)
{
135
136
	VAVec3 sourceToListenerPos = source->predPos - _listenerPos;
	double dotp = _clusterSourceToListenerPos.Dot(sourceToListenerPos);
137

138
	return (dotp * dotp) / (_clusterSourcePos.Dot(_clusterSourcePos) * sourceToListenerPos.Dot(sourceToListenerPos));
139
140
141
}

void 
Lucas Moesch's avatar
WIP    
Lucas Moesch committed
142
VABinauralCluster::addSource(int sourceID, VABinauralSoundSource* source)
143
144
145
{
	double err = getDistError(source);

Lucas Moesch's avatar
wip    
Lucas Moesch committed
146
	_clusterSourcePos = (source->predPos + _clusterSourcePos * numSources) / (numSources + 1);
147
148
	_clusterSourceToListenerPos = _clusterSourcePos - _listenerPos;

149
150
	maxError = std::max(err, maxError);

Lucas Moesch's avatar
WIP    
Lucas Moesch committed
151
152
	_sources.insert(std::pair<int, VABinauralSoundSource*>(sourceID, source));

153
154
155
	// --
	source->AddReference();

Lucas Moesch's avatar
wip    
Lucas Moesch committed
156
	++numSources;
157
158
159
}

void
160
VABinauralCluster::addSource(int sourceID, VABinauralSoundSource* source, double error)
161
{
Lucas Moesch's avatar
wip    
Lucas Moesch committed
162
	_clusterSourcePos = (source->predPos + _clusterSourcePos * numSources) / (numSources + 1);
163
164
	_clusterSourceToListenerPos = _clusterSourcePos - _listenerPos;

165
166
	maxError = std::max(error, maxError);

167
168
169
170
	_sources.insert(std::pair<int, VABinauralSoundSource*>(sourceID, source));
	// --
	source->AddReference();

Lucas Moesch's avatar
wip    
Lucas Moesch committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
	++numSources;
}

void
VABinauralCluster::removeSource(int sourceID)
{
	std::map<int, VABinauralSoundSource*>::const_iterator it = _sources.find(sourceID);
	VABinauralSoundSource* source = it->second;

	_sources.erase(it);

	source->RemoveReference();

	_clusterSourcePos = (_clusterSourcePos * numSources - source->predPos) / (numSources - 1);

	//TODO: MaxError
	--numSources;

189
190
}