complete_test.cpp 7.26 KB
Newer Older
1
2
#ifdef WITH_RAVEN

Pascal Palenda's avatar
Pascal Palenda committed
3
4
5
6
7
8
9
#include <iostream>
#include <string>
#include <memory>
#include <ostream>
#include <valarray>

// simulation scheduler includes
Pascal Palenda's avatar
Pascal Palenda committed
10
11
12
13
#include <ITA/SimulationScheduler/RoomAcoustics/master_simulation_controller.h>
#include <ITA/SimulationScheduler/RoomAcoustics/raven/worker_thread.h>
#include <ITA/SimulationScheduler/Utils/utils.h>
#include <ITA/SimulationScheduler/AudibilityFilter/audibility_filter_interface.h>
Pascal Palenda's avatar
Pascal Palenda committed
14
15
16
17
18
19
20

// ITA includes
#include <ITAFFT.h>
#include <ITAHDFTSpectra.h>
#include <ITAAudiofileReader.h>
#include <ITAAudiofileWriter.h>
#include "ITAHDFTSpectrum.h"
Pascal Palenda's avatar
Pascal Palenda committed
21
#include <ITATimer.h>
Pascal Palenda's avatar
Pascal Palenda committed
22
23
24
25
26
27
28
29

// GTest
#include "gtest/gtest.h"
#include "mocks.hpp"


#include <VistaTools/VistaIniFileParser.h>

30
31
32
33
34
using namespace ITA::SimulationScheduler;
using namespace ITA::SimulationScheduler::AudibilityFilter;
using namespace ITA::SimulationScheduler::Utils;
using namespace ITA::SimulationScheduler::RoomAcoustics;
using namespace ITA::SimulationScheduler::RoomAcoustics::Raven;
Pascal Palenda's avatar
Pascal Palenda committed
35

Pascal Palenda's avatar
Pascal Palenda committed
36
struct CompleteTest : public testing::Test, public ITATimerEventHandler
Pascal Palenda's avatar
Pascal Palenda committed
37
38
39
40
41
42
43
44
45
{
	std::unique_ptr<CMasterSimulationController> masterController;

	std::unique_ptr<MockResultHandler> resultHandler;

	ITABase::CMultichannelFiniteImpulseResponse dsReference, erReference, ddReference;

	CompleteTest ( )
	{
Pascal Palenda's avatar
Pascal Palenda committed
46
		auto oConfig = CMasterSimulationController::MasterSimulationControllerConfig ( );
Pascal Palenda's avatar
Pascal Palenda committed
47

Pascal Palenda's avatar
Pascal Palenda committed
48
		auto schedulerConfig = std::make_shared<CScheduler::LocalSchedulerConfig> ( );
Pascal Palenda's avatar
Pascal Palenda committed
49

Pascal Palenda's avatar
Pascal Palenda committed
50
		auto workerConfig = std::make_shared<CWorkerThread::WorkerThreadConfig> ( );
51
		workerConfig->sRavenProjectFilePath = "D:/ScieBo/MasterThesis/Raven/RavenInput/Cube/referenceNEW.rpf";
Pascal Palenda's avatar
Pascal Palenda committed
52

Pascal Palenda's avatar
Pascal Palenda committed
53
		schedulerConfig->vpWorkerConfigs.push_back ( workerConfig );
Pascal Palenda's avatar
Pascal Palenda committed
54

55
		CAudibilityFilterFactory::RegisterObject ( "MockFilter",
Pascal Palenda's avatar
Pascal Palenda committed
56
57
												   MockAudibilityFilter::createFilter,
												   std::make_shared<MockAudibilityFilter::MockFilterConfig> );
Pascal Palenda's avatar
Pascal Palenda committed
58

Pascal Palenda's avatar
Pascal Palenda committed
59
		auto filterConfig = std::make_shared<MockAudibilityFilter::MockFilterConfig>();
Pascal Palenda's avatar
Pascal Palenda committed
60
		filterConfig->behaviour = MockAudibilityFilter::Behaviour::everythingAudible;
Pascal Palenda's avatar
Pascal Palenda committed
61
		filterConfig->sFilterName = "Filter";
Pascal Palenda's avatar
Pascal Palenda committed
62

Pascal Palenda's avatar
Pascal Palenda committed
63
64
65
		CFilterNetwork::FilterNetworkConfig networkConfig;
		networkConfig.vpFilterConfigs.push_back ( filterConfig );
		networkConfig.sStartFilter = filterConfig->sFilterName;
Pascal Palenda's avatar
Pascal Palenda committed
66

Pascal Palenda's avatar
Pascal Palenda committed
67
68
69
70
71
72
		schedulerConfig->oFilterNetworkConfig = networkConfig;

		oConfig.oDSSchedulerConfig = schedulerConfig;
		oConfig.oERSchedulerConfig = schedulerConfig;
		oConfig.oDDSchedulerConfig = schedulerConfig;
		
73
		VistaIniFileParser::WriteProplistToFile ( "CompleteTest_Master_Test_Config.ini", oConfig.Store(), true );
Pascal Palenda's avatar
Pascal Palenda committed
74

Pascal Palenda's avatar
Pascal Palenda committed
75
76
		ofstream myfile;
		myfile.open ( "CompleteTest_Master_Test_Config.json" );
77
		myfile << Utils::JSONConfigUtils::WriteVistaPropertyListToJSON ( oConfig.Store ( ) ).write_formatted ( );
Pascal Palenda's avatar
Pascal Palenda committed
78
79
		myfile.close ( );
		
Pascal Palenda's avatar
Pascal Palenda committed
80
81
82
83
		masterController = std::make_unique<CMasterSimulationController> ( oConfig );

		resultHandler = std::make_unique<MockResultHandler> ( );

84
		masterController->AttachResultHandler ( resultHandler.get ( ) );
Pascal Palenda's avatar
Pascal Palenda committed
85
86
87
88
89

		dsReference.LoadFromFile ( "D:/ScieBo/MasterThesis/Raven/RavenInput/Cube/DS_reference.wav" );
		erReference.LoadFromFile ( "D:/ScieBo/MasterThesis/Raven/RavenInput/Cube/ER_reference.wav" );
		ddReference.LoadFromFile ( "D:/ScieBo/MasterThesis/Raven/RavenInput/Cube/DD_reference.wav" );
	}
Pascal Palenda's avatar
Pascal Palenda committed
90
91
92
93
94
95
96
97
98

	void handleTimerEvent(const ITATimer& tSource) override
	{
		auto update = std::make_unique<CUpdateScene> ( 1 );
		auto source = std::make_unique<C3DObject> (
			VistaVector3D ( 1, 1.5, 1 ), VistaQuaternion ( 0, 0, 0, 1 ), C3DObject::Type::source, 1 );
		auto receiver = std::make_unique<C3DObject> (
			VistaVector3D ( -0.5, 1.5, -0.5 ), VistaQuaternion ( 0, 0, 0, 1 ), C3DObject::Type::receiver, 1 );

99
		update->SetSourceReceiverPair ( std::move ( source ),
Pascal Palenda's avatar
Pascal Palenda committed
100
101
										std::move ( receiver ) );

102
		masterController->PushUpdate ( std::move ( update ) );
Pascal Palenda's avatar
Pascal Palenda committed
103
	}
Pascal Palenda's avatar
Pascal Palenda committed
104
105
};

106
TEST_F ( CompleteTest, DISABLED_correctResult )
Pascal Palenda's avatar
Pascal Palenda committed
107
108
109
110
111
112
113
{
	auto update = std::make_unique<CUpdateScene> ( 1 );
	auto source = std::make_unique<C3DObject> (
		VistaVector3D ( 1, 1.5, 1 ), VistaQuaternion ( 0, 0, 0, 1 ), C3DObject::Type::source, 1 );
	auto receiver = std::make_unique<C3DObject> (
		VistaVector3D ( -0.5, 1.5, -0.5 ), VistaQuaternion ( 0, 0, 0, 1 ), C3DObject::Type::receiver, 1 );

114
	update->SetSourceReceiverPair ( std::move ( source ),
Pascal Palenda's avatar
Pascal Palenda committed
115
116
									std::move ( receiver ) );

117
	masterController->PushUpdate ( std::move ( update ) );
Pascal Palenda's avatar
Pascal Palenda committed
118
119
120
121
122

	// We need to wait a bit for the workers to start working
	VistaTimeUtils::Sleep ( 500 );

	// wait till we have all results
Pascal Palenda's avatar
Pascal Palenda committed
123
	while ( resultHandler->resultVector.size ( ) != 3 )
Pascal Palenda's avatar
Pascal Palenda committed
124
125
		VistaTimeUtils::Sleep ( 250 );

126
	for ( const auto res : resultHandler->resultVector )
Pascal Palenda's avatar
Pascal Palenda committed
127
	{
128
129
		const auto result = dynamic_cast< const CRIRSimulationResult* >( res );

Pascal Palenda's avatar
Pascal Palenda committed
130
131
132
133
134
135
136
137
138
		ITABase::CMultichannelFiniteImpulseResponse reference;

		if ( result->eResultType == FieldOfDuty::directSound )
			reference = dsReference;
		if ( result->eResultType == FieldOfDuty::earlyReflections )
			reference = erReference;
		if ( result->eResultType == FieldOfDuty::diffuseDecay )
			reference = ddReference;

139
		const auto ir = DataTypeUtils::Convert ( result->sfResult );
Pascal Palenda's avatar
Pascal Palenda committed
140

141
		auto stdDev = CalculationUtils::CalculateStdDevOfAbsSpectralDifference ( reference, {ir} );
Pascal Palenda's avatar
Pascal Palenda committed
142
143
144
145
146
147
148
149

		for ( const auto& deviation : stdDev )
		{
			if ( result->eResultType == FieldOfDuty::diffuseDecay )
				EXPECT_GE ( 6.0, deviation ); // accepted >= deviation
			else
				EXPECT_GE ( 0.001, deviation ); // accepted >= deviation

150
		}
Pascal Palenda's avatar
Pascal Palenda committed
151

152
		auto correlationVector = CalculationUtils::NormalizedCrossCorrelation ( reference, ir );
Pascal Palenda's avatar
Pascal Palenda committed
153
154
155
156

		for ( const auto& correlation : correlationVector )
		{
			if ( result->eResultType == FieldOfDuty::diffuseDecay )
Pascal Palenda's avatar
Pascal Palenda committed
157
				EXPECT_LE ( 0.1, abs ( correlation ).max ( ) ); // threshold <= maxCorr
Pascal Palenda's avatar
Pascal Palenda committed
158
159
160
161
162
			else
				EXPECT_LE ( 0.9, abs ( correlation ).max ( ) ); // threshold <= maxCorr
		}
	}

Pascal Palenda's avatar
Pascal Palenda committed
163
	if ( HasFailure ( ) || true )
Pascal Palenda's avatar
Pascal Palenda committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
	{
		const auto test_info = testing::UnitTest::GetInstance ( )->current_test_info ( );

		auto path = std::string ( test_info->test_case_name ( ) ) + "/" + std::string ( test_info->name ( ) );

		if ( !doesDirectoryExist ( "results" ) )
			makeDirectory ( "results" );

		size_t index = 0;
		while ( true )
		{
			/* Locate the substring to replace. */
			index = path.find ( "/", index );
			if ( index == std::string::npos ) break;

			/* Make the replacement. */
			path.replace ( index, 1, "_" );

			/* Advance index forward so the next iteration doesn't pick it up as well. */
			index++;
		}

Pascal Palenda's avatar
Pascal Palenda committed
186
		for ( const auto& res : resultHandler->resultVector )
Pascal Palenda's avatar
Pascal Palenda committed
187
		{
188
			const auto rirRes = dynamic_cast< CRIRSimulationResult* >( res );
189
			rirRes->sfResult.Store ( "results/" + path + "-FOD_" + to_string ( AsInteger ( rirRes->eResultType ) ) + ".wav" );
Pascal Palenda's avatar
Pascal Palenda committed
190
191
		}
	}
Pascal Palenda's avatar
Pascal Palenda committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
}

TEST_F( CompleteTest, realTimeTest)
{
	using namespace std::placeholders;
	// processStream update rate ~300Hz
	const auto updateRate = 300.0;
	ITATimer oTimer ( 1 / updateRate );
		
	oTimer.attach ( this );

	if ( !oTimer.isActive ( ) )
		oTimer.start ( );

	VistaTimeUtils::Sleep ( 1000 );

	if ( oTimer.isActive ( ) )
		oTimer.stop ( );

	auto prevSize = resultHandler->ddResultVector.size ( );

213
	while ( masterController->IsBusy() )
Pascal Palenda's avatar
Pascal Palenda committed
214
215
216
217
218
219
220
221
222
	{
		VistaTimeUtils::Sleep ( 500 );
	}
	
	EXPECT_TRUE ( resultHandler->resultVector.size ( ) > 3 );
	EXPECT_TRUE ( !resultHandler->dsResultVector.empty ( ) );
	EXPECT_TRUE ( !resultHandler->erResultVector.empty ( ) );
	EXPECT_TRUE ( !resultHandler->ddResultVector.empty ( ) );
	
223
224
225
}

#endif