#define NOMINMAX #include #include #include // VA #include // ITA includes #include // Utils #include "./VABinauralClustering.h" #include "./VABinauralClusterPoolFactory.h" VABinauralClustering::VABinauralClustering(int blocklength) : _blocklength(blocklength) { IVAPoolObjectFactory* clusterFactory = new VABinauralClusterPoolFactory(); clusterPool = IVAObjectPool::Create(16, 2, clusterFactory, true); }; VABinauralClustering::~VABinauralClustering(){}; void VABinauralClustering::addSource(int sourceID, VABinauralSoundSource* source) { _unassignedSources.insert(std::pair< int, VABinauralSoundSource* >(sourceID, source)); }; void VABinauralClustering::removeSource(int sourceID) { _delSourceIDs.insert(sourceID); } void VABinauralClustering::init(int listenerID, VABinauralListener* listener, int numClusters, int HRIRFilterLength) { _HRIRFilterLength = HRIRFilterLength; _listenerID = listenerID; _listener = listener; _numClusters = numClusters; _threshold = cos(180. / numClusters) * cos(180. / numClusters); _output = new ITASampleFrame(2, listener->output->GetLength(), true); // initialize left channel convolver for each cluster for (int i = 0; i < _numClusters; ++i) { ITAUPConvolution* convChL = new ITAUPConvolution(_blocklength, _HRIRFilterLength); convChL->SetFilterExchangeFadingFunction(ITABase::FadingFunction::COSINE_SQUARE); convChL->SetFilterCrossfadeLength((std::min)(_blocklength, 32)); convChL->SetGain(0.0f, true); ITAUPFilter* HRIRFilterChL = convChL->RequestFilter(); HRIRFilterChL->identity(); convChL->ExchangeFilter(HRIRFilterChL); FIRConvolverChL.insert(std::pair< int, ITAUPConvolution* >(i, convChL)); } // initialize right channel convolver for each cluster for (int i = 0; i < _numClusters; ++i) { ITAUPConvolution* convChR = new ITAUPConvolution(_blocklength, _HRIRFilterLength); convChR->SetFilterExchangeFadingFunction(ITABase::FadingFunction::COSINE_SQUARE); convChR->SetFilterCrossfadeLength((std::min)(_blocklength, 32)); convChR->SetGain(0.0f, true); ITAUPFilter* HRIRFilterChR = convChR->RequestFilter(); HRIRFilterChR->identity(); convChR->ExchangeFilter(HRIRFilterChR); FIRConvolverChR.insert(std::pair< int, ITAUPConvolution* >(i, convChR)); } _curState.reset(new VABinauralClusteringState(_numClusters, _listener, clusterPool, &FIRConvolverChL, &FIRConvolverChR)); } ITASampleFrame* VABinauralClustering::getOutput() { // -- _output->zero(); // swap out clustering state if (_nextState != nullptr) _curState.reset(_nextState.release()); 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 VABinauralClustering::update() { //TODO: DIRTY HACK if (_nextState == nullptr) { VABinauralClusteringState* state = new VABinauralClusteringState(*_curState); // remove removed sources std::set< int >::const_iterator it; 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; state->removeSource(*it); _unassignedSources.erase(delIt); // remove if in assigned sources delIt = _assignedSources.find(*it); source = delIt->second; _assignedSources.erase(delIt); } // add unassigned sources std::map< int, VABinauralSoundSource* >::iterator sourceIt; for (sourceIt = _unassignedSources.begin(); sourceIt != _unassignedSources.end(); ++sourceIt) { state->addSource(sourceIt->first, sourceIt->second, _threshold, 0); } // TODO: refinement // update source status for (auto const& assignedSources : _assignedSources) { _assignedSources.insert(std::pair< int, VABinauralSoundSource* >(assignedSources.first, assignedSources.second)); } _unassignedSources.clear(); _nextState.reset(state); // TODO: update fixed clustertrajectories } } void VABinauralClustering::PreRequest(){}; void VABinauralClustering::PreRelease(){};