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

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

6
7
8
// Utils
#include "../BinauralTimeOfArrivalEstimator/VABinauralTOAEstimator.h"

9
10
11
12
13
14
VABinauralCluster::VABinauralCluster()
{
}

VABinauralCluster::VABinauralCluster(const VABinauralCluster& cluster) :
	maxError(cluster.maxError),
Lucas Moesch's avatar
wip    
Lucas Moesch committed
15
	numSources(cluster.numSources),
16
17
18
19
20
21
22
23
	_clusterSourcePos(cluster._clusterSourcePos)
{

}


VABinauralCluster::~VABinauralCluster()
{
24
25
26
27
28
29
30
31
32
33
34
	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();
	}
35
36
37
}

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

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

50
51
	maxError = getDistError(source);

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

54
55
56
	// --
	source->AddReference();

Lucas Moesch's avatar
wip    
Lucas Moesch committed
57
	++numSources;
58
59
}

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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);

79
		//TODO: add general distance VDL
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
		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;
}

98
99
100
double
VABinauralCluster::getDistError(VABinauralSoundSource* source)
{
101
102
	VAVec3 sourceToListenerPos = source->predPos - _listenerPos;
	double dotp = _clusterSourceToListenerPos.Dot(sourceToListenerPos);
103

104
	return (dotp * dotp) / (_clusterSourcePos.Dot(_clusterSourcePos) * sourceToListenerPos.Dot(sourceToListenerPos));
105
106
107
}

void 
Lucas Moesch's avatar
WIP    
Lucas Moesch committed
108
VABinauralCluster::addSource(int sourceID, VABinauralSoundSource* source)
109
110
111
{
	double err = getDistError(source);

Lucas Moesch's avatar
wip    
Lucas Moesch committed
112
	_clusterSourcePos = (source->predPos + _clusterSourcePos * numSources) / (numSources + 1);
113
114
	_clusterSourceToListenerPos = _clusterSourcePos - _listenerPos;

115
116
	maxError = std::max(err, maxError);

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

119
120
121
	// --
	source->AddReference();

Lucas Moesch's avatar
wip    
Lucas Moesch committed
122
	++numSources;
123
124
125
}

void
126
VABinauralCluster::addSource(int sourceID, VABinauralSoundSource* source, double error)
127
{
Lucas Moesch's avatar
wip    
Lucas Moesch committed
128
	_clusterSourcePos = (source->predPos + _clusterSourcePos * numSources) / (numSources + 1);
129
130
	_clusterSourceToListenerPos = _clusterSourcePos - _listenerPos;

131
132
	maxError = std::max(error, maxError);

133
134
135
136
	_sources.insert(std::pair<int, VABinauralSoundSource*>(sourceID, source));
	// --
	source->AddReference();

Lucas Moesch's avatar
wip    
Lucas Moesch committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	++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;

155
156
}