NCTC_CTCFilterTest.cpp 4.64 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1 2
#include <ITANCTC.h>

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
3 4
#include <ITAFileSystemUtils.h>
#include <ITAStopWatch.h>
Jonas Stienen's avatar
Jonas Stienen committed
5
#include <ITAStringUtils.h>
6
#include <ITAFFTUtils.h>
Jonas Stienen's avatar
Jonas Stienen committed
7 8 9 10 11

#include <DAFF.h>

#include <iostream>

12
int main( int, char** )
Jonas Stienen's avatar
Jonas Stienen committed
13 14 15 16 17 18 19 20 21 22 23
{
	// HRIR
	//std::string sHRIRFileName = correctPath( "../../../../VAData/HRIR/ITA-Kunstkopf_HRIR_AP11_Pressure_Equalized_3x3_256.daff" );
	std::string sHRIRFileName = correctPath( "D:/Users/roecher/Arbeitsumgebung/VAData/HRIR/ITA-Kunstkopf_HRIR_Mess01_D180_1x1_256.daff" );
	// Configuration

	ITANCTC::Config oNCTCConfig;
	oNCTCConfig.iCTCFilterLength = 10000;//2048;
	oNCTCConfig.dSampleRate = 44.1e3;
	oNCTCConfig.fSpeedOfSound = 344.0f;
	//oNCTCConfig.iOptimization = ITANCTC::Config::OPTIMIZATION_NONE;
24
	//oNCTCConfig.iOptimization = ITANCTC::Config::OPTIMIZATION_AIXCAVE_FULLY_CLOSED;
Jonas Stienen's avatar
Jonas Stienen committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

	// 4 loudspeaker configuration (square with LS in corners facing center point)
	/*float fHeight = 3.2f; // meter
	float fDistance = 8.0f; // meter
	ITANCTC::Pose oLSPose;
	oLSPose.vPos.SetValues( std::sqrt( fDistance ), fHeight, -std::sqrt( fDistance ) );
	//oLSPose.qOrient.SetToNeutralQuaternion();
	oNCTCConfig.voLoudspeaker.push_back( ITANCTC::Config::Loudspeaker( oLSPose ) );
	oLSPose.vPos.SetValues( -std::sqrt( fDistance ), fHeight, -std::sqrt( fDistance ) );
	oNCTCConfig.voLoudspeaker.push_back( ITANCTC::Config::Loudspeaker( oLSPose ) );
	oLSPose.vPos.SetValues( -std::sqrt( fDistance ), fHeight, std::sqrt( fDistance ) );
	oNCTCConfig.voLoudspeaker.push_back( ITANCTC::Config::Loudspeaker( oLSPose ) );
	oLSPose.vPos.SetValues( std::sqrt( fDistance ), fHeight, std::sqrt( fDistance ) );
	oNCTCConfig.voLoudspeaker.push_back( ITANCTC::Config::Loudspeaker( oLSPose ) );
	oLSPose.vPos.SetValues( 0.0f, fHeight*2, 0.0f );
	oNCTCConfig.voLoudspeaker.push_back( ITANCTC::Config::Loudspeaker( oLSPose ) );
	oLSPose.vPos.SetValues( 0.0f, -fHeight, 0.0f );
	oNCTCConfig.voLoudspeaker.push_back( ITANCTC::Config::Loudspeaker( oLSPose ) );*/


	// aixCAVE speaker position
	float fHeight = 1.65f; // meter
	ITANCTC::Pose oLSPose;
	oLSPose.vPos.SetValues( -2.555,3.402, 0.0 );
	//oLSPose.qOrient.SetToNeutralQuaternion();
	oNCTCConfig.voLoudspeaker.push_back( ITANCTC::Config::Loudspeaker( oLSPose ) );
	oLSPose.vPos.SetValues( 0.0, 3.403, -2.55 );
	oNCTCConfig.voLoudspeaker.push_back( ITANCTC::Config::Loudspeaker( oLSPose ) );
	oLSPose.vPos.SetValues( 2.560, 3.402, 0.0 );
	oNCTCConfig.voLoudspeaker.push_back( ITANCTC::Config::Loudspeaker( oLSPose ) );
	oLSPose.vPos.SetValues( 0, 3.401, 2.3 );
	oNCTCConfig.voLoudspeaker.push_back( ITANCTC::Config::Loudspeaker( oLSPose ) );
	



	// Use number of LS as N
	oNCTCConfig.N = int( oNCTCConfig.voLoudspeaker.size() );

	// DAFF HRIR
	DAFFReader* r = DAFFReader::create();
	int ec = r->openFile( sHRIRFileName );
	if( ec != 0 )
	{
		std::cerr << "DAFF Error: " << DAFFUtils::StrError( ec ) << std::endl;
		return 255;
	}
	DAFFContentIR* pHRIR = dynamic_cast< DAFFContentIR* >( r->getContent() );


	// Head pose
	ITANCTC::Pose oHead;
	oHead.vPos.SetValues( 0.0f, fHeight, 0.0f );
	oHead.vView.SetValues( 0.0f, 0.0f, -1.0f );
	oHead.vUp.SetValues( 0.0f, 1.0f, 0.0f );
	//oHead.qOrient = VistaQuaternion( VistaAxisAndAngle( VistaVector3D( 0, 1, 0 ), Vista::DegToRad( -45 ) ) );
	//oHead.qOrient.SetToNeutralQuaternion();

	// Validate with view/up
//	VistaVector3D v3View = oHead.qOrient.GetViewDir();
//	VistaVector3D v3Up   = oHead.qOrient.GetUpDir();

	// --- NCTC ---

	// Instanciate
	ITANCTC ctc( oNCTCConfig );

	// Settings
	ctc.SetBeta( 0.001f );

	// Update and calculate filter
	ctc.SetHRIR( pHRIR );
	ctc.UpdateHeadPose( oHead );

	std::vector< ITAHDFTSpectra* > vpCTCFilter;
	for( int n=0; n<ctc.GetN(); n++ )
		vpCTCFilter.push_back( new ITAHDFTSpectra( oNCTCConfig.dSampleRate, 2, oNCTCConfig.iCTCFilterLength+1, true ) );
	
	try
	{
		ctc.CalculateFilter( vpCTCFilter );
	}
	catch( ITAException& e )
	{
		std::cout << e << std::endl;
	}

	for( int i=0; i<ctc.GetN(); i++ )
113
		ITAFFTUtils::Export( vpCTCFilter[i], "CTCFilter_" + IntToString( i ) );
Jonas Stienen's avatar
Jonas Stienen committed
114 115 116 117 118 119 120

	ITAStopWatch sw;
	std::cout << "Starting loop ";
	for( int i=0; i<1000; i++ )
	{
		sw.start();
		ctc.CalculateFilter( vpCTCFilter );
121
		sw.stop();
Jonas Stienen's avatar
Jonas Stienen committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		if( i % 100 == 0 ) std::cout << ".";
	}
	std::cout << std::endl;

	std::cout << "Mean (deviation) processing time for CTC filter: " << sw.mean() * 1.0e3 << " ms";
	std::cout << " ( " << sw.std_deviation() * 1.0e6 << " us )" << std::endl;

	// Export HRTFs
	std::vector<ITAHDFTSpectra*> vpHRTF;

	ctc.GetHRTF(vpHRTF);


	// Export resulting filter set to WAV file
	for( int n=0; n<ctc.GetN(); n++ )
	{
138 139
		ITAFFTUtils::Export( vpCTCFilter[ n ], "CTCFilter_LS" + IntToString( int( n + 1 ) ) );
		ITAFFTUtils::Export( vpHRTF[ n ], "HRTFs_LS" + IntToString( int( n + 1 ) ) );
Jonas Stienen's avatar
Jonas Stienen committed
140 141 142 143 144
	}
	delete pHRIR;
	
	return 0;
}