#define NOMINMAX #include #include "VABinauralCluster.h" // Utils #include "../BinauralTimeOfArrivalEstimator/VABinauralTOAEstimator.h" VABinauralCluster::VABinauralCluster() { } VABinauralCluster::VABinauralCluster(const VABinauralCluster& cluster) : maxError(cluster.maxError), _numSources(cluster._numSources), _clusterSourcePos(cluster._clusterSourcePos) { } VABinauralCluster::~VABinauralCluster() { VABinauralSoundSource* source; std::map::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 VABinauralCluster::init(int sourceID, VABinauralSoundSource* source, VABinauralListener* listener) { _listener = listener; _listenerPos = listener->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); _sources.insert(std::pair(sourceID, source)); // -- source->AddReference(); ++_numSources; } ITASampleFrame* VABinauralCluster::getOutput() { // reset output Buffer _output->zero(); // set VDL Values std::map::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 VABinauralCluster::getDistError(VABinauralSoundSource* source) { VAVec3 sourceToListenerPos = source->predPos - _listenerPos; double dotp = _clusterSourceToListenerPos.Dot(sourceToListenerPos); return (dotp * dotp) / (_clusterSourcePos.Dot(_clusterSourcePos) * sourceToListenerPos.Dot(sourceToListenerPos)); } void VABinauralCluster::addSource(int sourceID, VABinauralSoundSource* source) { double err = getDistError(source); _clusterSourcePos = (source->predPos + _clusterSourcePos * _numSources) / (_numSources + 1); _clusterSourceToListenerPos = _clusterSourcePos - _listenerPos; maxError = std::max(err, maxError); _sources.insert(std::pair(sourceID, source)); // -- source->AddReference(); ++_numSources; } void VABinauralCluster::addSource(int sourceID, VABinauralSoundSource* source, double error) { _clusterSourcePos = (source->predPos + _clusterSourcePos * _numSources) / (_numSources + 1); _clusterSourceToListenerPos = _clusterSourcePos - _listenerPos; maxError = std::max(error, maxError); _sources.insert(std::pair(sourceID, source)); // -- source->AddReference(); ++_numSources; }