BinauralOutdoorNoiseRendererTest.cpp 4.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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
#define _USE_MATH_DEFINES

#include <Windows.h>
#include <vector>
#include <math.h>
#include <stdlib.h> 
#include <iostream>
#include <cassert>

#include <VA.h>
#include <VACore.h>

#include <VistaBase/VistaTimeUtils.h>


IVAInterface* va = NULL;
CVAStruct time;
CVAStruct trigger;
int nSources = 100;

std::vector<std::string> ssIDs;
std::vector< int > viSourceIDs( nSources );

std::vector<std::string> GetFileNamesInDirectory( std::string directory )
{
	std::vector<std::string> files;
	WIN32_FIND_DATA fileData;
	HANDLE hFind;

	if( !( ( hFind = FindFirstFile( directory.c_str(), &fileData ) ) == INVALID_HANDLE_VALUE ) )
		while( FindNextFile( hFind, &fileData ) )
			files.push_back( fileData.cFileName );

	FindClose( hFind );
	return files;
}

void SetupScene()
{
	for( int i = 0; i < nSources; ++i )
	{
		float azimuth = ( ( rand() % 3600 ) / 1800.0 ) * M_PI;
		float elevation = ( ( rand() % 900 ) / 1800.0 ) * M_PI;

		float x = sin( elevation ) * cos( azimuth ) * 5;
		float y = cos( elevation ) * 5;
		float z = sin( azimuth ) * sin( elevation ) * 5;

		std::string source_name = "UrbanSource_" + std::to_string( i );
		viSourceIDs[ i ] = va->CreateSoundSource( source_name );
		va->SetSoundSourceSignalSource( viSourceIDs[ i ], ssIDs[ rand() % ssIDs.size() ] );
		va->SetSoundSourcePosition( viSourceIDs[ i ], VAVec3( x, 1.7 + y, z ) );
	}
}

void Scene2()
{
	for( int i = 0; i < nSources; ++i )
	{
		float z = static_cast < float > ( rand() ) / ( static_cast < float > ( RAND_MAX / 24 ) );

		std::string source_name = "UrbanSource_" + std::to_string( i );
		viSourceIDs[ i ] = va->CreateSoundSource( source_name );

		va->SetSoundSourceSignalSource( viSourceIDs[ i ], ssIDs[ rand() % ssIDs.size() ] );

		if( i % 2 == 0 )
		{
			va->SetSoundSourcePosition( viSourceIDs[ i ], VAVec3( -5, 1.7, -z ) );
		}
		else
		{
			va->SetSoundSourcePosition( viSourceIDs[ i ], VAVec3( 5, 1.7, -z ) );
		}
	}
}

void Test()
{
	std::string source_name = "UrbanSource";
	viSourceIDs[ 0 ] = va->CreateSoundSource( source_name );
	va->SetSoundSourceSignalSource( viSourceIDs[ 0 ], ssIDs[ rand() % ssIDs.size() ] );
	va->SetSoundSourcePosition( viSourceIDs[ 0 ], VAVec3( -5, 1.7, 0 ) );
}

int main()
{
	int numsteps = 6000;

	double clock = 0;
	double timestep = 64 / 44100.;

	try
	{
		va = VACore::CreateCoreInstance( "BinauralOutdoorNoiseRendererTest.VACore.ini" );
		va->Initialize();

		// Just name a folder with a lot ofs WAV clips
		va->AddSearchPath( "../data" );

henryjandrew's avatar
wip  
henryjandrew committed
101 102 103
		//std::string sWAVCLipsFolder = "C:/Users/jonas/sciebo/ITA/Lehre/Masterarbeiten/2018 Lucas Msch/2018 MA Lucas Msch Auralization/input_files"; // no tailing '/'
		std::string sWAVCLipsFolder = "C:/Users/andrew/input_files"; // no tailing '/'

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
		va->AddSearchPath( sWAVCLipsFolder );
		std::vector<std::string> vFileNames = GetFileNamesInDirectory( sWAVCLipsFolder + "/*.wav" );

		int receiverID = va->CreateSoundReceiver( "UrbanReceiver" );
		int directivityID = va->CreateDirectivityFromFile( "$(DefaultHRIR)", "HRTF" );
		va->SetSoundReceiverDirectivity( receiverID, directivityID );
		va->SetSoundReceiverPosition( receiverID, VAVec3( 0, 1.7, 0 ) );

		for( auto const& value : vFileNames )
		{
			std::string ssID = va->CreateSignalSourceBufferFromFile( value );
			va->SetSignalSourceBufferPlaybackAction( ssID, 2 );
			va->SetSignalSourceBufferLooping( ssID, true );
			va->SetSignalSourceBufferPlaybackPosition( ssID, static_cast < float > ( rand() ) / ( static_cast < float > ( RAND_MAX / 2 ) ) );
			ssIDs.push_back( ssID );
		}

		assert( ssIDs.size() > 0 );

		// @henry: try different scenes, they will export (and overwrite on each launch) the auralization result
		//         to the folder VACore\tests\recordings\MyVirtualAcousticsProject\renderer\BinauralOutdoorNoise
		//SetupScene();
		Scene2();
		//Test();

		va->SetCoreClock( 0 );

		for( int i = 0; i < numsteps; i++ )
		{

			va->SetSoundReceiverPosition( receiverID, VAVec3( 0, 1.7, -clock * 3 ) );
			clock += timestep;
			time[ "time" ] = clock;
			va->CallModule( "manualclock", time );

			trigger[ "trigger" ] = true;
			va->CallModule( "virtualaudiodevice", trigger );

			std::cout << clock << std::endl;
		}

		va->Finalize();
	}
	catch( CVAException& e )
	{
		std::cerr << "Error: " << e << std::endl;
		int iErrorCode = e.GetErrorCode();
		delete va;
		VistaTimeUtils::Sleep( 60000 );
		return iErrorCode;
	}

	VistaTimeUtils::Sleep( 1000 );

	return 0;
}