RavenNetTestClient.cpp 9.22 KB
Newer Older
1
2
3
4
5

// RavenNet includes
#include <ITASimulationScheduler/Raven/RavenNetClient.h>

// Raven includes
6
7
8
9
#include <ITASimulationScheduler/Interfaces.h>
#include <ITASimulationScheduler/Raven/Simulator.h>
#include <ITASimulationScheduler/LocalScheduler.h>
#include <ITASimulationScheduler/Utils.h>
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

// ITA includes
#include <ITADebug.h>
#include <ITASampleBuffer.h>
#include <ITASampleFrame.h>
#include <ITAStopWatch.h>
#include <ITAStringUtils.h>
#include <ITAAudiofileWriter.h>

// Vista includes
#include <VistaBase/VistaTimeUtils.h>

// STL includes
#include <iostream>

using namespace std;

static const double dSampleRate = 44.1e3;
static const string sRavenProjectPath = "$(RavenDataBasePath)/Models/Quader/quader.ac"; // Muss auf Serverseite existieren

void normalize_0dB( ITASampleFrame* );

32
void testSimulator( ITASimulationScheduler::ISimulationInterface* pRavenSimulator )
33
34
35
36
37
38
39
{
	cout << " * Loading scene geometry ... ";
	pRavenSimulator->LoadScene( sRavenProjectPath );
	cout << "[done]" << endl;

	int iCounter = 0;

40
    ITASimulationScheduler::RAVEN::CReceiverState oReceiverState;
41
42
43
44
45
	oReceiverState.vPos.init( 0.1f, 1.6f, 0.9f );
	oReceiverState.vView.init( 0, 0, -1 );
	oReceiverState.vUp.init( 0, 1, 0 );
	oReceiverState.sHRIRFilename = "$(RavenDataBasePath)/HRIR/ITA-Kunstkopf_HRIR_Mess01_D180_1x5_128.daff";

46
    ITASimulationScheduler::RAVEN::CSourceState oSourceState;
47
48
49
50
51
	oSourceState.vPos.init( 1, 0.5f, -1.1f );
	oSourceState.vView.init( 0, 0, 1 );
	oSourceState.vUp.init( 0, 1, 0 );
	oSourceState.sDirectivityFilename = "$(RavenDataBasePath)/Directivity/Slenczka_2005_energetic_3d_octave/Bratsche.daff";

52
    ITASimulationScheduler::CSimulationTask oTask1;
53
54
55
56
57
58
59
	oTask1.oScene.AddReceiver( 0 );
	oTask1.oScene.SetReceiverState( 0, oReceiverState );
	oTask1.oScene.AddSource( 0 );
	oTask1.oScene.SetSourceState( 0, oSourceState );
	oTask1.oConfig.SetDefaults();
	oTask1.uiID = ( unsigned int ) iCounter++;
	oTask1.uiReferenceID = 0;
60
    oTask1.iSimulationType = ITASimulationScheduler::CSimulationTask::SIM_DS;
61
62
	oTask1.oConfig.oGeometry.sSceneFileName = sRavenProjectPath;

63
    ITASimulationScheduler::CSimulationTask oTask2( oTask1 );
64
	oTask2.uiID = ( unsigned int ) iCounter++;
65
66
    oTask2.iSimulationType = ITASimulationScheduler::CSimulationTask::SIM_ER_IS;
    ITASimulationScheduler::CSimulationTask oTask3( oTask1 );
67
	oTask3.uiID = ( unsigned int ) iCounter++;
68
    oTask3.iSimulationType = ITASimulationScheduler::CSimulationTask::SIM_DD_RT;
69
70

	ITAStopWatch swInit, swSimDS, swSimIS, swSimRT;
71
    ITASimulationScheduler::CSimulationResult oResult;
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

	// Initial config adaption
	cout << "Sending initial configuration ..." << endl;
	swInit.start();
	pRavenSimulator->Compute( &oTask1, &oResult );
	cout << "[done]" << endl;
	cout << "Initialization took " << timeToString( swInit.stop() ) << endl;

	int i = -1;
	while( i++ < 100 && false )
	{
		oTask1.uiID = iCounter++;
		cout << "Sending DS compute ...";
		swSimDS.start();
		pRavenSimulator->Compute( &oTask1, &oResult );
		cout << " [done]" << endl;
		cout << "Task" << oTask1.uiID << " calc time " << timeToString( swSimDS.stop() ) << endl;
		string sAudible = oResult.vcspResult[ 0 ]->bDirectSoundAudible ? "(audible)" : "(not audible)";
		cout << "Received Direct Sound result " << sAudible << endl;
		oResult.Clear();
	}

	i = -1;
	while( i++ < 40 && false )
	{
		oTask2.uiID = iCounter++;
		swSimIS.start();
		cout << "Sending IS compute ...";
		pRavenSimulator->Compute( &oTask2, &oResult );
		cout << " [done]" << endl;
		cout << "Task" << oTask2.uiID << " calc time " << timeToString( swSimIS.stop() ) << endl;
		normalize_0dB( oResult.vcspResult[ 0 ]->psfResult );
		writeAudiofile( "Task" + IntToString( oTask2.uiID ) + "_RT.wav", oResult.vcspResult[ 0 ]->psfResult, dSampleRate );
		cout << "Received Image Sources result" << endl;
		oResult.Clear();
	}

	i = -1;
	while( i++ < 10 && true )
	{
		oTask3.uiID = iCounter++;
		swSimRT.start();
		cout << "Sending RT compute ...";
		pRavenSimulator->Compute( &oTask3, &oResult );
		cout << " [done]" << endl;
		cout << "Task" << oTask3.uiID << " calc time " << timeToString( swSimRT.stop() ) << endl;
		normalize_0dB( oResult.vcspResult[ 0 ]->psfResult );
		writeAudiofile( "Task" + IntToString( oTask3.uiID ) + "_RT.wav", oResult.vcspResult[ 0 ]->psfResult, dSampleRate );
		cout << "Received Ray Tracing result" << endl;
		oResult.Clear();
	}

	cout << "Initialization:	" << swInit.ToString() << endl;
	cout << "Direct Sound:		" << swSimDS.ToString() << endl;
	cout << "Image Sources:		" << swSimIS.ToString() << endl;
	cout << "Tray Tracing:		" << swSimRT.ToString() << endl;

	return;
}

132
void testScheduler( ITASimulationScheduler::ISimulationSchedulerInterface* pRavenScheduler )
133
134
135
{

	// ResultHandler angeben, um Resultate abzufangen
136
    ITASimulationScheduler::CSimulationResultDumper oDumper( "./" );
137
138
139
140
141
142
143
144
145
146
	pRavenScheduler->AttachSimulationResultHandler( &oDumper );

	// Preload scene
	//pRavenScheduler->LoadScene( sRavenProjectPath );
	//pRavenScheduler->Reset();

	VistaTimeUtils::Sleep( 2000 );

	pRavenScheduler->LoadScene( sRavenProjectPath );

147
    ITASimulationScheduler::RAVEN::CReceiverState oReceiverState;
148
149
150
151
152
	oReceiverState.vPos.init( 0.1f, 1.6f, 0.9f );
	oReceiverState.vView.init( 0, 0, -1 );
	oReceiverState.vUp.init( 0, 1, 0 );
	//oReceiverState.sHRIRFilename = "$(RavenDataBasePath)/HRIR/ITA-Kunstkopf_HRIR_Mess01_D180_1x5_128.daff";

153
    ITASimulationScheduler::RAVEN::CSourceState oSourceState;
154
155
156
157
158
	oSourceState.vPos.init( 1, 0.5f, -0.1f );
	oSourceState.vView.init( 0, 0, 1 );
	oSourceState.vUp.init( 0, 1, 0 );
	//oSourceState.sDirectivityFilename = "$(RavenDataBasePath)/Directivity/Slenczka_2005_energetic_3d_octave/Bratsche.daff";

159
    ITASimulationScheduler::CSimulationTask oTask1;
160
161
162
163
	oTask1.oScene.AddReceiver( 0 );
	oTask1.oScene.SetReceiverState( 0, oReceiverState );
	oTask1.oScene.AddSource( 0 );
	oTask1.oScene.SetSourceState( 0, oSourceState );
164
    oTask1.iSimulationType = ITASimulationScheduler::CSimulationTask::SIM_DS;
165
166
167
168
	oTask1.uiReferenceID = 0;
	oTask1.oConfig.SetDefaults();
	oTask1.oConfig.oGeometry.sSceneFileName = sRavenProjectPath;

169
170
171
172
    ITASimulationScheduler::CSimulationTask oTask2( oTask1 );
    oTask2.iSimulationType = ITASimulationScheduler::CSimulationTask::SIM_ER_IS;
    ITASimulationScheduler::CSimulationTask oTask3( oTask1 );
    oTask3.iSimulationType = ITASimulationScheduler::CSimulationTask::SIM_DD_RT;
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

	int iNumTasksAdded = 0;

	ITAStopWatch sw;
	int uiIDCounter = 0;

	int i = 0;
	while( i++ < 3 && false )
	{
		oTask1.uiID = uiIDCounter++;
		sw.start();
		pRavenScheduler->AddTask( &oTask1 );
		iNumTasksAdded++;
		sw.stop();
		std::cout << "Added DS task " << oTask1.uiID << std::endl;
	}

	i = 0;
	while( i++ < 3 && false )
	{
		oTask2.uiID = uiIDCounter++;
		sw.start();
		pRavenScheduler->AddTask( &oTask2 );
		iNumTasksAdded++;
		sw.stop();
		std::cout << "Added IS task " << oTask2.uiID << std::endl;
	}

	i = 0;
	while( i++ < 3 && true )
	{
		oTask3.uiID = uiIDCounter++;
		sw.start();
		pRavenScheduler->AddTask( &oTask3 );
		iNumTasksAdded++;
		sw.stop();
		std::cout << "Added RT task " << oTask3.uiID << std::endl;
	}

	std::cout << "AddTask: " << sw.ToString() << std::endl;

	while( oDumper.GetNumTasksReceived() != iNumTasksAdded )
	{
		std::cout << "Waiting for results, " << iNumTasksAdded - oDumper.GetNumTasksReceived()
			<< " of " << iNumTasksAdded << " missing" << std::endl;
		VistaTimeUtils::Sleep( 2500 );
	}

	std::cout << "All results received." << std::endl;

	return;
}

int main( int argc, char* argv[] )
{
	if( argc != 2 )
	{
		cerr << "Syntax error, usage: RavenNetTestClient SERVER_ADRESS" << endl;
		return 255;
	}

	string sServerAdress = string( argv[ 1 ] );

236
    ITASimulationScheduler::RAVEN::IRavenNetClient* pClient = ITASimulationScheduler::RAVEN::IRavenNetClient::Create();
237
238
239
240
241
242

	int iPort = 12480;

	cout << "Attempting to connect to " << sServerAdress << " on port " << iPort << endl;

	int err;
243
    if( ( err = pClient->Initialize( sServerAdress, iPort ) ) != ITASimulationScheduler::RAVEN::IRavenNetClient::RAVEN_NO_ERROR )
244
245
246
247
248
249
250
251
	{
		cout << "[ERROR] " << err << endl;
		return 255;
	}

	if( pClient->IsConnected() )
		cout << "Successfully connected to Raven server!" << endl;

252
    pClient->SetExceptionHandlingMode( ITASimulationScheduler::RAVEN::IRavenNetClient::EXC_SERVER_PRINT );
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286


	// --- Raven steuern ---

	if( pClient->HasRavenSimulator() )
	{
		try {
			testSimulator( pClient->GetRavenSimulator() );
		}
		catch( ITAException& err )
		{
			cout << "[error]: " << err << endl;
		}
	}
	else
	{
		std::cout << "Server has no simulator" << std::endl;
	}

	if( pClient->HasRavenSimulationScheduler() )
	{
		try {
			testScheduler( pClient->GetRavenSimulationScheduler() );
		}
		catch( ITAException& err )
		{
			cout << "[error]: " << err << endl;
		}
	}
	else
	{
		std::cout << "Server has no scheduler" << std::endl;
	}

287
    if( pClient->Disconnect() == ITASimulationScheduler::RAVEN::IRavenNetClient::RAVEN_NO_ERROR )
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
		cout << " * Successfully disconnected." << endl;

	delete pClient;

	return 0;
}

void normalize_0dB( ITASampleFrame* p ) {
	float fGlobalPeak = 0.0f;
	for( int i = 0; i < p->length(); i++ ) {
		ITASampleBuffer& p1 = ( *p )[ 0 ];
		ITASampleBuffer& p2 = ( *p )[ 1 ];
		float fSample1 = p1[ i ];
		float fSample2 = p2[ i ];
		fGlobalPeak = std::max( fGlobalPeak, fSample1 );
		fGlobalPeak = std::max( fGlobalPeak, fSample2 );
	}
	if( fGlobalPeak != 0.0f )
		p->mul_scalar( 1 / fGlobalPeak );
}