VACoreImpl.cpp 141 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1
/*
2
 *  --------------------------------------------------------------------------------------------
Jonas Stienen's avatar
Jonas Stienen committed
3
 *
4
5
6
7
8
9
10
11
 *    VVV        VVV A           Virtual Acoustics (VA) | http://www.virtualacoustics.org
 *     VVV      VVV AAA          Licensed under the Apache License, Version 2.0
 *      VVV    VVV   AAA
 *       VVV  VVV     AAA        Copyright 2015-2017
 *        VVVVVV       AAA       Institute of Technical Acoustics (ITA)
 *         VVVV         AAA      RWTH Aachen University
 *
 *  --------------------------------------------------------------------------------------------
Jonas Stienen's avatar
Jonas Stienen committed
12
 */
13

Jonas Stienen's avatar
Jonas Stienen committed
14
15
16
17
18
19
#include "VACoreImpl.h"

// VA base
#include <VABaseDefinitions.h>
#include <VAStruct.h>
#include <VACoreVersion.h>
20
#include <VAException.h>
21
#include <VAPoolObject.h>
Jonas Stienen's avatar
Jonas Stienen committed
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

// VA includes
#include "Audiosignals/VAAudioSignalSourceManager.h"
#include "Audiosignals/VAAudiofileSignalSource.h"
#include "Audiosignals/VAAudiofileSignalSource.h"
#include "Audiosignals/VAEngineSignalSource.h"
#include "Audiosignals/VAMachineSignalSource.h"
#include "Audiosignals/VASequencerSignalSource.h"
#include "Data/VAHRIRManager.h"
#include "Rendering/VAAudioRenderer.h"
#include "Rendering/VAAudioRendererRegistry.h"
#include "Reproduction/VAAudioReproductionModule.h"
#include "Reproduction/VAAudioReproductionModuleRegistry.h"
#include "Scene/VAScene.h"
#include "Scene/VASoundSourceDesc.h"
#include "Scene/VAListenerDesc.h"
#include "Utils/VADebug.h"
#include "Utils/VAUtils.h"
#include "VAAudiostreamTracker.h"
#include "VABaseDefinitions.h"
#include "VADirectivityManager.h"
#include "VALog.h"
#include "VACoreEventManager.h"
#include "VACoreFactory.h"
#include "VACoreThread.h"
#include "VASourceListenerMetrics.h"

49
#include "Drivers/Audio/VAAudioDriverBackend.h"
50
#include "Drivers/Audio/VAAudioDriverConfig.h"
51
52
53
54
55
56
57
58

#ifdef VACORE_WITH_AUDIO_BACKEND_ASIO
#include "Drivers/Audio/VAASIOBackend.h"
#endif
#ifdef VACORE_WITH_AUDIO_BACKEND_PORTAUDIO
#include "Drivers/Audio/VAPortaudioBackend.h"
#endif

Jonas Stienen's avatar
Jonas Stienen committed
59
60
61
62
// ITA includes
#include <ITAASCIITable.h>
#include <ITAClock.h>
#include <ITAException.h>
63
#include <ITAFileSystemUtils.h>
Jonas Stienen's avatar
Jonas Stienen committed
64
65
66
67
68
69
#include <ITAFunctors.h>
#include <ITAPeakDetector.h>
#include <ITASoundSample.h>
#include <ITASoundSampler.h>
#include <ITASoundSamplePool.h>
#include <ITAStreamAmplifier.h>
70
#include <ITAStreamPatchBay.h>
Jonas Stienen's avatar
Jonas Stienen committed
71
72
73
74
75
76
77
78
79
80
81
82
#include <ITAStreamProbe.h>
#include <ITAStringUtils.h>

// Vista includes
#include <VistaTools/VistaFileSystemFile.h>
#include <VistaTools/VistaFileSystemDirectory.h>

// 3rdParty includes
#include <DAFF.h>

// STL includes
#include <algorithm>
83
#include <iomanip>
Jonas Stienen's avatar
Jonas Stienen committed
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>

/*

	Informationen
	=-=-=-=-=-=-=

	Thread-safety & locking

	Alle durch die Schnittstelle nach aussen angebotenen Funktionen, werden
	gegen Reentrance grob-granular mittels eines kritischen Bereiches gesperrt.
	Damit sind auch parallele Calls auf unterschiedliche Methoden der Klasse
96
	unterbunden. Sollte dies zu Performance-Problemen fhren, kann ein
Jonas Stienen's avatar
Jonas Stienen committed
97
98
99
100
101
	verfeinerter Locking-Mechanismus entwickelt werden. Zunchst mal aber so.


	TODO: berall sicherstellen, das ITAExceptions zu VAExceptions umgebaut werden!!!

102
	*/
Jonas Stienen's avatar
Jonas Stienen committed
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


// --= Hilfsmakros =--

// berprfung ob Initialisiert
#define VA_CHECK_INITIALIZED \
{ if (m_iState != VA_CORESTATE_READY) VA_EXCEPT2(MODAL_ERROR, "Core not initialized"); }

// Mutex-Lock welches parallelen Methoden-Mehrfracheintritt (reentrance) im Methoden-Scope verhindert
#define VA_NO_REENTRANCE \
ITACriticalSectionLock oReentranceLock(m_csReentrance)

// Manuelles das Lock gegen parallelen Methoden-Mehrfracheintritt (reentrance) holen
#define VA_LOCK_REENTRANCE \
m_csReentrance.enter()

// Manuelle das Lock gegen parallelen Methoden-Mehrfracheintritt (reentrance) freigeben 
#define VA_UNLOCK_REENTRANCE \
m_csReentrance.leave()

/*

	Sichere Ausnahmebehandlung

	Problem: Intern knnen auch andere Typen von Ausnahmen als
128
129
	CVAException geworfen werden. Nach aussen hin drfen
	aber NUR CVAExceptions geworfen werden (Konsistent)
Jonas Stienen's avatar
Jonas Stienen committed
130
131

	Lsung: TRY-/CATCH-Makros fr abgesicherte Bereiche in allen Methoden
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
	der externen Schnittstelle. Zweistufiger Aufbau.
	1. (innen) Fange alle Exception-Typen und konvertiere sie in CVAExceptions
	2. (aussen) Fange die einzig mgl. CVAExceptions und biete
	gemeinsamen Catch-Weg zur Problemlsung (Aufrumen) an

	try {
	try {
	// Code ...
	} catch (ITAException& e) {
	// Konvertiere zu CVAException
	} catch (VistaExceptionBase& e) {
	// Konvertiere zu CVAException
	} catch (...)
	// Generiere unerwartete CVAException
	}
	} catch (CVAException& e) {
	// Gemeinsames Aufrumen
	}
Jonas Stienen's avatar
Jonas Stienen committed
150
151

	Beispiel:
152
153
154
155
156
157

	VA_TRY {
	// Code ..
	} VA_CATCH {
	// Mach was ...
	}
Jonas Stienen's avatar
Jonas Stienen committed
158
159
160

	Hinweis: Initiale/finale Geschweifte Klammer fehlen, da diese hinter die Makros geschrieben werden

161
	*/
Jonas Stienen's avatar
Jonas Stienen committed
162
163
164
165
166
167
168
169

#define VA_TRY \
	try { \
		try

#define VA_CATCH(EXPR) \
		  catch (CVAException& e) { \
			throw e; \
170
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																} catch (ITAException& e) { \
Jonas Stienen's avatar
Jonas Stienen committed
171
			throw convert2VAException(e); \
172
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																} catch (VistaExceptionBase& e) { \
Jonas Stienen's avatar
Jonas Stienen committed
173
			throw convert2VAException(e); \
174
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																} catch (...) { \
Jonas Stienen's avatar
Jonas Stienen committed
175
			throw getDefaultUnexpectedVAException(); \
176
177
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																} \
																																																																																																																																																																																																																																																																} catch (CVAException& EXPR)
Jonas Stienen's avatar
Jonas Stienen committed
178
179
180
181

#define VA_FINALLY \
		  catch (CVAException& e) { \
			throw e; \
182
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																} catch (ITAException& e) { \
Jonas Stienen's avatar
Jonas Stienen committed
183
			throw convert2VAException(e); \
184
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																} catch (VistaExceptionBase& e) { \
Jonas Stienen's avatar
Jonas Stienen committed
185
			throw convert2VAException(e); \
186
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																} catch (...) { \
Jonas Stienen's avatar
Jonas Stienen committed
187
			throw getDefaultUnexpectedVAException(); \
188
189
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																} \
																																																																																																																																																																																																																																																																} catch (...)
Jonas Stienen's avatar
Jonas Stienen committed
190
191
192
193

#define VA_RETHROW \
		  catch (CVAException& e) { \
			throw e; \
194
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																} catch (ITAException& e) { \
Jonas Stienen's avatar
Jonas Stienen committed
195
			throw convert2VAException(e); \
196
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																} catch (VistaExceptionBase& e) { \
Jonas Stienen's avatar
Jonas Stienen committed
197
			throw convert2VAException(e); \
198
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																} catch (...) { \
Jonas Stienen's avatar
Jonas Stienen committed
199
			throw getDefaultUnexpectedVAException(); \
200
201
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																} \
																																																																																																																																																																																																																																																																} catch (...) { throw; }
Jonas Stienen's avatar
Jonas Stienen committed
202
203
204
205


IVACore* VACore::CreateCoreInstance( const CVAStruct& oArgs )
{
206
	VA_TRACE( "Config", oArgs );
Jonas Stienen's avatar
Jonas Stienen committed
207
208
209
210
	return new CVACoreImpl( oArgs );
}

IVACore* VACore::CreateCoreInstance( const std::string& sConfigFile )
211
{
Jonas Stienen's avatar
Jonas Stienen committed
212
213
214
215
216
	CVAStruct oFinalCoreConfigStruct, oCurrentConfig;
	std::list< VistaFileSystemFile > voConfigFiles;
	std::vector< VistaFileSystemDirectory > voIncludePaths;
	voConfigFiles.push_back( VistaFileSystemFile( sConfigFile ) );

217
218
	VA_PRINT( "VA working directory: '" << VistaFileSystemDirectory::GetCurrentWorkingDirectory() << "'" );

Jonas Stienen's avatar
Jonas Stienen committed
219
220
221
222
223
224
225
	while( voConfigFiles.empty() == false )
	{
		VistaFileSystemFile oCurrentConfigFile( voConfigFiles.front() );
		voConfigFiles.pop_front();

		if( oCurrentConfigFile.Exists() == false )
		{
226
			for( size_t n = 0; n < voIncludePaths.size(); n++ )
Jonas Stienen's avatar
Jonas Stienen committed
227
			{
228
				std::string sCombinedFilePath = voIncludePaths[ n ].GetName() + PATH_SEPARATOR + oCurrentConfigFile.GetLocalName();
Jonas Stienen's avatar
Jonas Stienen committed
229
230
231
				oCurrentConfigFile.SetName( sCombinedFilePath );
				if( oCurrentConfigFile.Exists() && oCurrentConfigFile.IsFile() )
				{
232
					VA_INFO( "Config", "Including further configuration file '" + oCurrentConfigFile.GetLocalName() +
233
						"' from include path '" + voIncludePaths[ n ].GetName() + "'" );
Jonas Stienen's avatar
Jonas Stienen committed
234
235
236
237
238
239
240
241
242
					break;
				}
			}

			if( !oCurrentConfigFile.Exists() )
			{
				VA_EXCEPT2( FILE_NOT_FOUND, "Configuration file '" + oCurrentConfigFile.GetLocalName() + "' not found, aborting." );
			}
		}
243

244
		VA_VERBOSE( "Config", std::string( "Reading INI file '" ) + oCurrentConfigFile.GetLocalName() + "'" );
Jonas Stienen's avatar
Jonas Stienen committed
245
		readINIFileAsStruct( oCurrentConfigFile.GetName(), oCurrentConfig );
246

Jonas Stienen's avatar
Jonas Stienen committed
247
248
249
250
251
252
		if( oCurrentConfig.HasKey( "paths" ) )
		{
			const CVAStruct& oPaths( oCurrentConfig[ "paths" ] );
			CVAStruct::const_iterator it = oPaths.Begin();
			while( it != oPaths.End() )
			{
253
				const CVAStructValue& oIncludePath( ( it++ )->second );
Jonas Stienen's avatar
Jonas Stienen committed
254
255
256
257
258
259
260
261
262
263
264
265
				VistaFileSystemDirectory oNewPathDir( oIncludePath );
				if( oNewPathDir.Exists() && oNewPathDir.IsDirectory() )
					voIncludePaths.push_back( oNewPathDir );
			}
		}

		if( oCurrentConfig.HasKey( "files" ) )
		{
			const CVAStruct& oPaths( oCurrentConfig[ "files" ] );
			CVAStruct::const_iterator it = oPaths.Begin();
			while( it != oPaths.End() )
			{
266
267
				const CVAStructValue& oIncludeFile( ( it++ )->second );

Jonas Stienen's avatar
Jonas Stienen committed
268
269
270
271
272
273
274
275
276
277
278
279
280
				voConfigFiles.push_back( VistaFileSystemFile( oIncludeFile ) );
			}
		}

		oCurrentConfig.RemoveKey( "files" );

		// Merge structs (check for uniqueness)
		oFinalCoreConfigStruct.Merge( oCurrentConfig, true );
	}

	return CreateCoreInstance( oFinalCoreConfigStruct );
}

281
#ifdef WIN32
Jonas Stienen's avatar
Jonas Stienen committed
282
283
// Trick um DLL-Pfad zu ermitteln
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
284
#endif
Jonas Stienen's avatar
Jonas Stienen committed
285

286
std::string VACore::GetCoreLibFilePath()
Jonas Stienen's avatar
Jonas Stienen committed
287
{
288
#ifdef WIN32
Jonas Stienen's avatar
Jonas Stienen committed
289
	CHAR pszPath[ MAX_PATH + 1 ] = { 0 };
290
	GetModuleFileNameA( ( HINSTANCE ) &__ImageBase, pszPath, _countof( pszPath ) );
Jonas Stienen's avatar
Jonas Stienen committed
291
	return std::string( pszPath );
292
293
#else
	VA_EXCEPT2( NOT_IMPLEMENTED, "This function is not implemented for your platform. Sorry." );
Jonas Stienen's avatar
Jonas Stienen committed
294
	return "";
295
#endif // WIN32
Jonas Stienen's avatar
Jonas Stienen committed
296
297
}

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
CVACoreImpl::CVACoreImpl( const CVAStruct& oArgs )
	: m_pAudioDriverBackend( nullptr ),
	m_pGlobalSamplePool( nullptr ),
	m_pGlobalSampler( nullptr ),
	m_pSignalSourceMan( nullptr ),
	m_pDirMan( nullptr ),
	m_pHRIRMan( nullptr ),
	m_pSceneMan( nullptr ),
	m_pNewSceneState( nullptr ),
	m_iCurActiveListener( -1 ),
	m_iNewActiveListener( -1 ),
	m_iUpdActiveListener( -1 ),
	m_pCoreEventMan( nullptr ),
	m_pCoreThread( nullptr ),
	m_pInputAmp( nullptr ),
	m_pR2RPatchbay( nullptr ),
	m_pOutputPatchbay( nullptr ),
	m_pInputPeakDetector( nullptr ),
	m_pOutputPeakDetector( nullptr ),
	m_pOutputTracker( nullptr ),
	m_pStreamProbeDeviceInput( nullptr ),
	m_pStreamProbeFinal( nullptr ),
	m_pCurSceneState( nullptr ),
	m_pClock( ITAClock::getDefaultClock() ),
	m_pTicker( NULL ),
	m_lSyncModOwner( -1 ),
	m_lSyncModSpinCount( 0 ),
	m_iState( VA_CORESTATE_CREATED ),

	// TODO: Welche Default-Wert mssen erst in Initialize gesetzt werden?
	m_iGlobalAuralizationMode( IVACore::VA_AURAMODE_ALL ),
	m_dOutputGain( 1 ), m_dInputGain( 1 ),
	m_bOutputMuted( false ), m_bInputMuted( false ),
	m_dStreamClockOffset( 0 ), m_fCoreClockOffset( 0 ),

	// --= Profiling =--
	m_pmCoreThreadLoopTotalDuration( "Core thread loop" )
Jonas Stienen's avatar
Jonas Stienen committed
335
336
337
{
	VA_NO_REENTRANCE;

338
339
340
341
	VA_TRY
	{
		// read configuration
		m_oCoreConfig.Init( oArgs );
Jonas Stienen's avatar
Jonas Stienen committed
342

343
		// register core itself as a module
Jonas Stienen's avatar
Jonas Stienen committed
344
345
		SetObjectName( "VACore" );
		m_oModules.RegisterObject( this );
346
		VA_VERBOSE( "Core", "Registered core module with name '" << GetObjectName() << "'" );
Jonas Stienen's avatar
Jonas Stienen committed
347
348
349
350

		// Der Event-Manager muss immer verfgbar sein,
		// unabhnging davon ob der Core initialisiert wurde oder nicht.
		m_pCoreEventMan = new CVACoreEventManager;
351

Jonas Stienen's avatar
Jonas Stienen committed
352
353
		m_iState = VA_CORESTATE_CREATED;

354
		VA_TRACE( "Core", "CVACoreImpl instance created [" << this << "]" );
Jonas Stienen's avatar
Jonas Stienen committed
355
356
357
358

	} VA_RETHROW;
}

359
360
CVACoreImpl::~CVACoreImpl()
{
Jonas Stienen's avatar
Jonas Stienen committed
361
362
363
	VA_NO_REENTRANCE;

	// Implizit finalisieren, falls dies nicht durch den Benutzer geschah
364
365
366
367
	if( m_iState == VA_CORESTATE_READY )
	{
		VA_TRY
		{
Jonas Stienen's avatar
Jonas Stienen committed
368
			Finalize();
369
		}
370
			VA_FINALLY
371
		{
Jonas Stienen's avatar
Jonas Stienen committed
372
373
374
375
376
377
378
379
			// Fehler beim Finalisieren ignorieren
		};
	}

	// Nachricht senden [blocking], das die Kerninstanz gelscht wird.
	CVACoreEvent ev;
	ev.iEventType = CVACoreEvent::VA_COREEVENT_DESTROY;
	ev.pSender = this;
380
	m_pCoreEventMan->BroadcastEvent( ev );
Jonas Stienen's avatar
Jonas Stienen committed
381
382
383
384
385

	// Module deregistrieren
	m_oModules.Clear();

	// Nachrichten-Manager freigeben
386
387
	VA_TRY
	{
Jonas Stienen's avatar
Jonas Stienen committed
388
		delete m_pCoreEventMan;
389
390
	}
	VA_RETHROW;
Jonas Stienen's avatar
Jonas Stienen committed
391

392
	VA_TRACE( "Core", "CVACoreImpl instance deleted [" << this << "]" );
Jonas Stienen's avatar
Jonas Stienen committed
393
394

	// Profiling ausgeben
395
	VA_VERBOSE( "Core", m_pmCoreThreadLoopTotalDuration.ToString() );
Jonas Stienen's avatar
Jonas Stienen committed
396
397
}

398
399
void CVACoreImpl::SetDebugStream( std::ostream* posDebug )
{
400
401
	VALog_setOutputStream( posDebug );
	VALog_setErrorStream( posDebug );
Jonas Stienen's avatar
Jonas Stienen committed
402
403
}

404
void CVACoreImpl::GetVersionInfo( CVACoreVersionInfo* pVersionInfo ) const
Jonas Stienen's avatar
Jonas Stienen committed
405
406
407
408
409
{
	if( !pVersionInfo )
		return;

	std::stringstream ss;
410
	ss << VACORE_VERSION_MAJOR << "." << VACORE_VERSION_MINOR;
Jonas Stienen's avatar
Jonas Stienen committed
411
	pVersionInfo->sVersion = ss.str();
412
413
414
415
	ss.clear();
#ifdef VACORE_CMAKE_DATE
	ss << VACORE_CMAKE_DATE;
#else
416
	ss << "Unkown date";
417
418
#endif
	pVersionInfo->sDate = ss.str();
Jonas Stienen's avatar
Jonas Stienen committed
419
	pVersionInfo->sFlags = "";
420

Jonas Stienen's avatar
Jonas Stienen committed
421
#ifdef DEBUG
422
	pVersionInfo->sComments = "debug";
Jonas Stienen's avatar
Jonas Stienen committed
423
#else
424
	pVersionInfo->sComments = "release";
Jonas Stienen's avatar
Jonas Stienen committed
425
426
427
#endif
}

428
void CVACoreImpl::AttachCoreEventHandler( IVACoreEventHandler* pCoreEventHandler ) {
Jonas Stienen's avatar
Jonas Stienen committed
429
430
431
	VA_TRY
	{
		// Immer mglich. Unabhngig vom Zustand. Thread-safety wird im Manager geregelt.
432
		m_pCoreEventMan->AttachHandler( pCoreEventHandler );
Jonas Stienen's avatar
Jonas Stienen committed
433
434
435
	} VA_RETHROW;
}

436
437
void CVACoreImpl::DetachCoreEventHandler( IVACoreEventHandler* pCoreEventHandler ) {
	VA_TRY{
Jonas Stienen's avatar
Jonas Stienen committed
438
		// Immer mglich. Unabhngig vom Zustand. Thread-safety wird im Manager geregelt.
439
		m_pCoreEventMan->DetachHandler( pCoreEventHandler );
Jonas Stienen's avatar
Jonas Stienen committed
440
441
442
	} VA_RETHROW;
}

443
444
445
int CVACoreImpl::GetState() const
{
	VA_VERBOSE( "Core", "Core state requested, current state is " << m_iState );
Jonas Stienen's avatar
Jonas Stienen committed
446
447
448
449
450
451
452
453
	return m_iState;
}

void CVACoreImpl::Initialize() {
	VA_NO_REENTRANCE;

	// TODO: Prfen ob im Fehlerfall zurck in den sauberen Grundzustand [WICHTIG!]

454
	VA_VERBOSE( "Core", "Initializing core" );
Jonas Stienen's avatar
Jonas Stienen committed
455

456
457
	VA_TRY
	{
458
459
		if( m_iState == VA_CORESTATE_READY )
		VA_EXCEPT2( MODAL_ERROR, "Core already initialized." );
Jonas Stienen's avatar
Jonas Stienen committed
460

461
462
		if( m_iState == VA_CORESTATE_FAIL )
			VA_EXCEPT2( MODAL_ERROR, "Core corrupted, reinitialization impossible" );
Jonas Stienen's avatar
Jonas Stienen committed
463

464
		m_pCoreThread = new CVACoreThread( this );
Jonas Stienen's avatar
Jonas Stienen committed
465

466
		SetProgress( "Setting up audio hardware", "", 1 );
Jonas Stienen's avatar
Jonas Stienen committed
467
468
		InitializeAudioDriver();

469
		m_pR2RPatchbay = new ITAStreamPatchbay( m_oCoreConfig.oAudioDriverConfig.dSampleRate, m_oCoreConfig.oAudioDriverConfig.iBuffersize );
Jonas Stienen's avatar
Jonas Stienen committed
470
471

		// Create output patch bay with a single output that uses all available physical audio outputs from sound card
472
		m_pOutputPatchbay = new ITAStreamPatchbay( m_oCoreConfig.oAudioDriverConfig.dSampleRate, m_oCoreConfig.oAudioDriverConfig.iBuffersize );
Jonas Stienen's avatar
Jonas Stienen committed
473
474
475
476
477
		int iPhysicalHardwareOutput = m_pOutputPatchbay->AddOutput( m_oCoreConfig.oAudioDriverConfig.iOutputChannels );
		m_pOutputPatchbay->SetOutputGain( iPhysicalHardwareOutput, m_dOutputGain );

		m_iGlobalAuralizationMode = VA_AURAMODE_ALL;

478
		// Set up input stream network
Jonas Stienen's avatar
Jonas Stienen committed
479
		ITADatasource* pInputTail = nullptr;
480
481
		if( m_oCoreConfig.oAudioDriverConfig.iInputChannels > 0 )
		{
Jonas Stienen's avatar
Jonas Stienen committed
482
			pInputTail = m_pAudioDriverBackend->getInputStreamDatasource();
483
484
			if( pInputTail )
			{
485
486
				m_pInputAmp = new ITAStreamAmplifier( pInputTail, ( float ) m_dInputGain );
				m_pInputPeakDetector = new ITAPeakDetector( m_pInputAmp );
Jonas Stienen's avatar
Jonas Stienen committed
487
488
				pInputTail = m_pInputPeakDetector;

489
490
491
492
493
				if( m_oCoreConfig.bRecordDeviceInputEnabled )
				{
					m_pStreamProbeDeviceInput = new ITAStreamProbe( pInputTail, m_oCoreConfig.sRecordDeviceInputFilePath );
					pInputTail = m_pStreamProbeDeviceInput;
				}
Jonas Stienen's avatar
Jonas Stienen committed
494
495
496
			}
		}

497
		SetProgress( "Setting up resource managers", "", 2 );
Jonas Stienen's avatar
Jonas Stienen committed
498

499
500
501
		m_pSignalSourceMan = new CVAAudioSignalSourceManager( this, m_oCoreConfig.oAudioDriverConfig, pInputTail );
		m_pGlobalSamplePool = ITASoundSamplePool::Create( 1, m_oCoreConfig.oAudioDriverConfig.dSampleRate );
		m_pGlobalSampler = ITASoundSampler::Create( 1, m_oCoreConfig.oAudioDriverConfig.dSampleRate, m_oCoreConfig.oAudioDriverConfig.iBuffersize, m_pGlobalSamplePool );
Jonas Stienen's avatar
Jonas Stienen committed
502
		m_pGlobalSampler->AddMonoTrack();
503

Jonas Stienen's avatar
Jonas Stienen committed
504
505
506
		m_pDirMan = new CVADirectivityManager;
		m_pDirMan->Initialize();

507
		m_pHRIRMan = new CVAHRIRManager( m_oCoreConfig.oAudioDriverConfig.dSampleRate );
Jonas Stienen's avatar
Jonas Stienen committed
508
509
		m_pHRIRMan->Initialize();

510
		SetProgress( "Setting up scene management", "", 3 );
Jonas Stienen's avatar
Jonas Stienen committed
511

512
		m_pSceneMan = new CVASceneManager( m_pClock );
Jonas Stienen's avatar
Jonas Stienen committed
513
514
515
		m_pSceneMan->Initialize();
		m_pCurSceneState = m_pSceneMan->GetHeadSceneState();

516
		// @todo move to dynamic listener state
Jonas Stienen's avatar
Jonas Stienen committed
517
518
519
520
521
522
523
524
525
526
		m_oListenerRealWorldCoords.px = 0;
		m_oListenerRealWorldCoords.py = 0;
		m_oListenerRealWorldCoords.pz = 0;
		m_oListenerRealWorldCoords.vx = 0;
		m_oListenerRealWorldCoords.vy = 0;
		m_oListenerRealWorldCoords.vz = -1;
		m_oListenerRealWorldCoords.ux = 0;
		m_oListenerRealWorldCoords.uy = 1;
		m_oListenerRealWorldCoords.uz = 0;

527
		// Register all renderers and initialize
528
		CVAAudioRendererRegistry::GetInstance()->RegisterInternalCoreFactoryMethods();
Jonas Stienen's avatar
Jonas Stienen committed
529
530
		InitializeAudioRenderers();

531
		if( m_voRenderers.empty() )
Jonas Stienen's avatar
Jonas Stienen committed
532
533
			VA_EXCEPT1( "No audio renderers created" );

534
		SetProgress( "Initializing reproduction modules", "", 6 );
535

536
537
		// Register all reproductions and initialize
		CVAAudioReproductionModuleRegistry::GetInstance()->RegisterInternalCoreFactoryMethods();
Jonas Stienen's avatar
Jonas Stienen committed
538
539
		InitializeReproductionModules();

540
		if( m_voReproductionModules.empty() )
Jonas Stienen's avatar
Jonas Stienen committed
541
			VA_EXCEPT1( "No audio reproduction modules created" );
542

Jonas Stienen's avatar
Jonas Stienen committed
543
544
545
546
547
548
549
550
551
552
553
554
		// Patch renderer and reproduction modules
		PatchRendererToReproductionModules();

		// Patch audio reproduction to output
		// TODO:
		//SetProgress( "Patching audio modules", "", 7 );
		PatchReproductionModulesToOutput();

		/* Wire the global sampler (1:N)
		int iGlobalSamplerInput = m_pOutputPatchbay->AddInput( m_pGlobalSampler );
		int iNumPhysicalOutputChannels = m_pOutputPatchbay->GetOutputNumChannels( iPhysicalHardwareOutput );
		for( int i=0; i<iNumPhysicalOutputChannels; i++ )
555
		m_pOutputPatchbay->ConnectChannels( iGlobalSamplerInput, 0, iPhysicalHardwareOutput, i );
Jonas Stienen's avatar
Jonas Stienen committed
556
557
558
559
		*/

		// Create output peak detector that uses patch bay output stream
		m_pOutputPeakDetector = new ITAPeakDetector( m_pOutputPatchbay->GetOutputDatasource( iPhysicalHardwareOutput ) );
560

Jonas Stienen's avatar
Jonas Stienen committed
561
562
		// Setup output dump (if set)
		ITADatasource* pOutputTail = m_pOutputPeakDetector;
563
		if( m_oCoreConfig.bRecordDeviceOutputEnabled )
Jonas Stienen's avatar
Jonas Stienen committed
564
		{
565
			m_pStreamProbeFinal = new ITAStreamProbe( pOutputTail, m_oCoreConfig.sRecordFinalOutputFilePath );
Jonas Stienen's avatar
Jonas Stienen committed
566
567
568
569
570
571
572
573
574
575
576
577
			pOutputTail = m_pStreamProbeFinal;
		}

		// Attach the stream tracker
		m_pOutputTracker = new CVAAudiostreamTracker( pOutputTail, m_pClock, &m_fCoreClockOffset, &m_lSyncModOwner, m_pSignalSourceMan );
		pOutputTail = m_pOutputTracker;

		// Give output stream datasource to audio driver
		m_pAudioDriverBackend->setOutputStreamDatasource( pOutputTail );

		// Core-Clock auf 0 initialisieren
		double dNow = m_pClock->getTime();
578
		m_fCoreClockOffset = ( float ) dNow;
Jonas Stienen's avatar
Jonas Stienen committed
579
580
581
		m_dStreamClockOffset = -1;

		// Timer erzeugen und konfigurieren (wird fr Peak-Events benutzt)
582
		m_pTicker = new VistaTicker();
583
		m_pTicker->AddTrigger( new VistaTicker::TriggerContext( m_oCoreConfig.iTriggerUpdateMilliseconds, true ) );
584
		m_pTicker->SetAfterPulseFunctor( this );
Jonas Stienen's avatar
Jonas Stienen committed
585
586
587
588
589
590

		// Audio-Streaming starten
		SetProgress( "Starting audio streaming", "", 7 );
		m_pAudioDriverBackend->startStreaming();

		// Timer fr Peak-Events starten
591
		m_pTicker->StartTicker();
Jonas Stienen's avatar
Jonas Stienen committed
592
593
594
595
596
597
598

		// Initialisierung erfolgreich!
		m_iState = VA_CORESTATE_READY;

		SetProgress( "Initialization finished", "", 8 );
		FinishProgress();

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
599
	}
600
		VA_FINALLY
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
601
	{
Jonas Stienen's avatar
Jonas Stienen committed
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
		// Aufrumen und Exception weiterwerfen
		Tidyup();
		throw;
	}
}

void CVACoreImpl::Reset()
{
	VA_CHECK_INITIALIZED;

	// Wait for core thread
	while( !m_pCoreThread->TryBreak() )
		VASleep( 20 );

	VA_NO_REENTRANCE;

	if( IsSceneLocked() )
	{
620
		VA_WARN( "Core", "Encountered locked scene during reset. Please unlock before resetting, skipping." );
Jonas Stienen's avatar
Jonas Stienen committed
621
622
623
624
625
		m_pCoreThread->Continue();
	}

	VA_TRY
	{
626
		VA_INFO( "Core", "Resetting core" );
Jonas Stienen's avatar
Jonas Stienen committed
627
628

		// Reset audio renderers
629
630
		std::vector< CVAAudioRendererDesc >::iterator it = m_voRenderers.begin();
		while( it != m_voRenderers.end() )
Jonas Stienen's avatar
Jonas Stienen committed
631
632
633
634
		{
			CVAAudioRendererDesc& oRend( *it++ );
			oRend.pInstance->Reset();
		}
635

Jonas Stienen's avatar
Jonas Stienen committed
636
637
638
		if( m_pCurSceneState )
		{
			// Referenz entfernen welche in CoreThreadLoop hinzugefgt wurde
639
			m_pCurSceneState->RemoveReference();
Jonas Stienen's avatar
Jonas Stienen committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
			m_pCurSceneState = nullptr;
		}

		// Alle Szenenobjekte lschen
		m_pSceneMan->Reset();
		m_pSignalSourceMan->Reset();
		m_pDirMan->Reset();
		m_pHRIRMan->Reset();

		// TODO: Check if the pool and sampler must really be recreated
		/*
		delete m_pGlobalSamplePool;
		m_pGlobalSamplePool = nullptr;
		m_pGlobalSamplePool = ITASoundSamplePool::Create(1, m_oCoreConfig.oAudioDriverConfig.dSamplerate);

		// This causes a crash in patch bay
		delete m_pGlobalSampler;
		m_pGlobalSampler = nullptr;
		m_pGlobalSampler = ITASoundSampler::Create(1, m_oCoreConfig.oAudioDriverConfig.dSamplerate, m_oCoreConfig.oAudioDriverConfig.iBuffersize, m_pGlobalSamplePool);
		m_pGlobalSampler->AddMonoTrack();
		*/
		//m_pGlobalSampler->RemoveAllPlaybacks();

		// Werte neusetzen
		m_pCurSceneState = m_pSceneMan->GetHeadSceneState();
		m_iCurActiveListener = -1;
		m_iNewActiveListener = -1;

		m_pCoreThread;

		// Core-Thread fortsetzen
		m_pCoreThread->Continue();

		// Ereignis generieren, wenn Operation erfolgreich
		CVACoreEvent ev;
		ev.iEventType = CVACoreEvent::VA_COREEVENT_RESET;
		ev.pSender = this;
677
		m_pCoreEventMan->BroadcastEvent( ev );
Jonas Stienen's avatar
Jonas Stienen committed
678
679

	}
680
		VA_FINALLY
Jonas Stienen's avatar
Jonas Stienen committed
681
682
683
684
685
686
687
688
	{
		Tidyup();
		throw;
	}
}

void CVACoreImpl::Tidyup() {
	/*
689
	 *  Hinweis: Diese Hilfsmethode wird nur innerhalb des Reentrance-Locks
Jonas Stienen's avatar
Jonas Stienen committed
690
691
	 *           aufgerufen - daher keine weiter Absicherung ntig.
	 */
692

Jonas Stienen's avatar
Jonas Stienen committed
693
694
	VA_TRY
	{
695
696
697
698
699
		if( m_pTicker )
		{
			m_pTicker->StopTicker();
			m_pTicker->SetAfterPulseFunctor( NULL );
		}
Jonas Stienen's avatar
Jonas Stienen committed
700
701

		FinalizeAudioDriver();
702
703
		FinalizeRenderingModules();
		FinalizeReproductionModules();
Jonas Stienen's avatar
Jonas Stienen committed
704

705
706
		delete m_pTicker;
		m_pTicker = nullptr;
Jonas Stienen's avatar
Jonas Stienen committed
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727

		delete m_pCoreThread;
		m_pCoreThread = nullptr;

		delete m_pInputAmp;
		m_pInputAmp = nullptr;

		delete m_pInputPeakDetector;
		m_pInputPeakDetector = nullptr;

		delete m_pR2RPatchbay;
		m_pR2RPatchbay = nullptr;

		delete m_pOutputPatchbay;
		m_pOutputPatchbay = nullptr;

		delete m_pOutputPeakDetector;
		m_pOutputPeakDetector = nullptr;

		delete m_pOutputTracker;
		m_pOutputTracker = nullptr;
728

Jonas Stienen's avatar
Jonas Stienen committed
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
		delete m_pStreamProbeDeviceInput;
		m_pStreamProbeDeviceInput = nullptr;

		delete m_pStreamProbeFinal;
		m_pStreamProbeFinal = nullptr;

		delete m_pSignalSourceMan;
		m_pSignalSourceMan = nullptr;

		delete m_pGlobalSampler;
		m_pGlobalSampler = nullptr;

		delete m_pGlobalSamplePool;
		m_pGlobalSamplePool = nullptr;

744
745
		if( m_pDirMan )
			m_pDirMan->Finalize();
Jonas Stienen's avatar
Jonas Stienen committed
746
747
748
		delete m_pDirMan;
		m_pDirMan = nullptr;

749
750
		if( m_pHRIRMan )
			m_pHRIRMan->Finalize();
Jonas Stienen's avatar
Jonas Stienen committed
751
752
753
		delete m_pHRIRMan;
		m_pHRIRMan = nullptr;

754
755
		if( m_pSceneMan )
			m_pSceneMan->Finalize();
Jonas Stienen's avatar
Jonas Stienen committed
756
757
758
759
760
761
		delete m_pSceneMan;
		m_pSceneMan = nullptr;

		m_iState = VA_CORESTATE_CREATED;

	}
762
		VA_FINALLY
Jonas Stienen's avatar
Jonas Stienen committed
763
764
765
766
767
	{
		m_iState = VA_CORESTATE_FAIL;
	}
}

768
769
void CVACoreImpl::Finalize()
{
Jonas Stienen's avatar
Jonas Stienen committed
770
771
772
	VA_NO_REENTRANCE;

	//VA_TRACE("Core", __FUNCTION__ << " entry");
773
	VA_INFO( "Core", "Finalizing core" );
Jonas Stienen's avatar
Jonas Stienen committed
774

775
776
	VA_TRY
	{
Jonas Stienen's avatar
Jonas Stienen committed
777
		// Mehrfaches Finialisieren fhrt nicht zu Fehlern
778
779
780
781
		if( m_iState == VA_CORESTATE_CREATED ) return;

		if( m_iState == VA_CORESTATE_FAIL )
			VA_EXCEPT2( MODAL_ERROR, "Core corrupted, finalization impossible" );
Jonas Stienen's avatar
Jonas Stienen committed
782
783
784
785
786
787
788
789

		// Core-Thread anhalten (wenn frei ist)
		while( !m_pCoreThread->TryBreak() )
			VASleep( 10 );
		//m_pCoreThread->Break(); << deadlock

		// Alle Filterketten lschen und warten bis Zustand sicher bernommen
		// Wichtig: Dies muss vor dem Beenden des Streamings geschehen
790

Jonas Stienen's avatar
Jonas Stienen committed
791
		// Reset audio renderers
792
		for( std::vector<CVAAudioRendererDesc>::iterator it = m_voRenderers.begin(); it != m_voRenderers.end(); ++it )
Jonas Stienen's avatar
Jonas Stienen committed
793
794
795
			it->pInstance->Reset();

		// Peak-Nachrichten stoppen
796
		m_pTicker->StopTicker();
Jonas Stienen's avatar
Jonas Stienen committed
797
798
799
800

		// Audio-Streaming beenden
		m_pAudioDriverBackend->stopStreaming();

801
		InitProgress( "Stopping auralization threads", "", 2 );
Jonas Stienen's avatar
Jonas Stienen committed
802

803
804
805
806
		// Stop and delete ticker
		m_pTicker->SetAfterPulseFunctor( NULL );
		delete m_pTicker;
		m_pTicker = NULL;
Jonas Stienen's avatar
Jonas Stienen committed
807
808
809
810
811

		// Hauptthread beenden und freigeben
		delete m_pCoreThread;
		m_pCoreThread = nullptr;

812
		SetProgress( "Releasing audio hardware", "", 1 );
Jonas Stienen's avatar
Jonas Stienen committed
813
		FinalizeAudioDriver();
814
815
		FinalizeRenderingModules();
		FinalizeReproductionModules();
Jonas Stienen's avatar
Jonas Stienen committed
816

817
		SetProgress( "Cleaning up resources", "", 2 );
Jonas Stienen's avatar
Jonas Stienen committed
818
819
820
821
822
823
		delete m_pInputAmp;
		m_pInputAmp = nullptr;

		delete m_pInputPeakDetector;
		m_pInputPeakDetector = nullptr;

824
		m_voReproductionModules.clear();
Jonas Stienen's avatar
Jonas Stienen committed
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869

		delete m_pR2RPatchbay;
		m_pR2RPatchbay = nullptr;

		delete m_pOutputPatchbay;
		m_pOutputPatchbay = nullptr;

		delete m_pOutputPeakDetector;
		m_pOutputPeakDetector = nullptr;

		delete m_pOutputTracker;
		m_pOutputTracker = nullptr;

		delete m_pStreamProbeDeviceInput;
		m_pStreamProbeDeviceInput = nullptr;

		delete m_pStreamProbeFinal;
		m_pStreamProbeFinal = nullptr;

		delete m_pSignalSourceMan;
		m_pSignalSourceMan = nullptr;

		delete m_pGlobalSampler;
		m_pGlobalSampler = nullptr;

		delete m_pGlobalSamplePool;
		m_pGlobalSamplePool = nullptr;

		m_pDirMan->Finalize();
		delete m_pDirMan;
		m_pDirMan = nullptr;

		m_pHRIRMan->Finalize();
		delete m_pHRIRMan;
		m_pHRIRMan = nullptr;

		m_pSceneMan->Finalize();
		delete m_pSceneMan;
		m_pSceneMan = nullptr;

		// Finalisierung erfolgreich. Nun wieder im Grundzustand!
		m_iState = VA_CORESTATE_CREATED;

		FinishProgress();

870
	} VA_FINALLY{
Jonas Stienen's avatar
Jonas Stienen committed
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
		// Nochmals versuchen aufzurumen
		Tidyup();

		// Allgemein: Fehler beim Finalisieren? => Core im Sack
		m_iState = VA_CORESTATE_FAIL;

		// VAExceptions unverndert nach aussen leiten
		throw;
	}
}

/* +----------------------------------------------------------+ *
 * |                                                          | *
 * |   Module interface                                       | *
 * |                                                          | *
 * +----------------------------------------------------------+ */

888
void CVACoreImpl::RegisterModule( CVAObject* pModule )
Jonas Stienen's avatar
Jonas Stienen committed
889
{
890
	m_oModules.RegisterObject( pModule );
Jonas Stienen's avatar
Jonas Stienen committed
891
892
893
}


894
895
void CVACoreImpl::EnumerateModules( std::vector<CVAModuleInfo>& viModuleInfos )const
{
Jonas Stienen's avatar
Jonas Stienen committed
896
897
898
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

899
#if ( VACORE_MODULE_INTERFACE_ENABLED == 1 )
900
901
	VA_TRY
	{
Jonas Stienen's avatar
Jonas Stienen committed
902
		std::vector<CVAObjectInfo> v;
903
		m_oModules.GetObjectInfos( v );
Jonas Stienen's avatar
Jonas Stienen committed
904

905
		VA_PRINT( "Available modules (" << v.size() << ")" );
Jonas Stienen's avatar
Jonas Stienen committed
906
907
908

		viModuleInfos.clear();
		viModuleInfos.resize( v.size() );
909
910
911
912
913
		for( size_t i = 0; i < v.size(); i++ )
		{
			VA_PRINT( "'" << v[ i ].sName << "'\t\t\t" << v[ i ].sDesc );
			viModuleInfos[ i ].sName = v[ i ].sName;
			viModuleInfos[ i ].sDesc = v[ i ].sDesc;
Jonas Stienen's avatar
Jonas Stienen committed
914
		}
915
916
917
	}
	VA_RETHROW;

918
#else // VACORE_MODULE_INTERFACE_ENABLED
919

920
921
	VA_EXCEPT1( "This VACore version does not provide modules" );

922
#endif // VACORE_MODULE_INTERFACE_ENABLED
923

Jonas Stienen's avatar
Jonas Stienen committed
924
925
}

926
927
void CVACoreImpl::CallModule( const std::string& sModuleName, const CVAStruct& oArgs, CVAStruct& oReturn )
{
Jonas Stienen's avatar
Jonas Stienen committed
928
929
930
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

931
#if ( VACORE_MODULE_INTERFACE_ENABLED == 1 )
932
933
934

	VA_TRY
	{
935
		CVAObject* pModule = m_oModules.FindObjectByName( sModuleName );
936
		if( !pModule )
937
			VA_EXCEPT2( INVALID_PARAMETER, "Module '" + sModuleName + "' not found" );
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953

#ifdef VACORE_MODULE_INTERFACE_MECHANISM_EVENT_BASED

		CVACoreEvent ev;
		ev.iEventType = CVACoreEvent::VA_COREEVENT_SIGNALSOURCE_STATE_CHANGED;
		ev.pSender = this;
		ev.sObjectID = sModuleName;
		ev;
		m_pCoreEventMan->BroadcastEvent( ev );
		m_pCoreEventMan->

#else // not VACORE_MODULE_INTERFACE_MECHANISM_EVENT_BASED

		pModule->CallObject( oArgs, oReturn );

#endif // VACORE_MODULE_INTERFACE_MECHANISM_EVENT_BASED
Jonas Stienen's avatar
Jonas Stienen committed
954
955

	} VA_RETHROW;
956

957
#else // VACORE_MODULE_INTERFACE_ENABLED
958

959
#if ( VACORE_NO_MODULE_INTERFACE_THROW_EXCEPTION == 1 )
960
	VA_EXCEPT1( "This VACore version does not provide modules" );
961
962
963
964
965
#else
	// Ignore. No return values.
	oReturn.Clear();
#endif // VACORE_NO_MODULE_INTERFACE_THROW_EXCEPTION

966
#endif // VACORE_MODULE_INTERFACE_ENABLED
Jonas Stienen's avatar
Jonas Stienen committed
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
}

/* +----------------------------------------------------------+ *
 * |                                                          | *
 * |   Richtcharakteristiken (directivities)                  | *
 * |                                                          | *
 * +----------------------------------------------------------+ */

int CVACoreImpl::LoadDirectivity( const std::string& sFilename, const std::string& sName )
{
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

	VA_TRY
	{
		std::string sDestFilePath = FindFilePath( sFilename );
		if( sDestFilePath.empty() )
			VA_EXCEPT2( INVALID_PARAMETER, "Looked everywhere, but could not find file '" + sFilename + "'" );

986
		int iDirID = m_pDirMan->LoadDirectivity( sDestFilePath, sName );
Jonas Stienen's avatar
Jonas Stienen committed
987
988
989
990
991
992
993
994
995

		assert( iDirID != -1 );

		CVACoreEvent ev;
		ev.iEventType = CVACoreEvent::VA_COREEVENT_DIRECTIVITY_LOADED;
		ev.pSender = this;
		ev.iObjectID = iDirID;
		m_pCoreEventMan->BroadcastEvent( ev );

996
		VA_INFO( "Core", "Directivity \"" << sDestFilePath << "\" successfully loaded, assigned directivity ID " << iDirID );
Jonas Stienen's avatar
Jonas Stienen committed
997
998
999
1000
1001
1002

		return iDirID;

	} VA_RETHROW;
}

1003
bool CVACoreImpl::FreeDirectivity( int iDirID ) {
Jonas Stienen's avatar
Jonas Stienen committed
1004
1005
1006
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

1007
1008
	VA_TRY{
		bool bSuccess = m_pDirMan->FreeDirectivity( iDirID );
Jonas Stienen's avatar
Jonas Stienen committed
1009
1010
1011
1012
1013
1014
1015
		//assert( bSuccess );

		// Ereignis generieren, wenn Operation erfolgreich
		CVACoreEvent ev;
		ev.iEventType = CVACoreEvent::VA_COREEVENT_DIRECTIVITY_FREED;
		ev.pSender = this;
		ev.iObjectID = iDirID;
1016
		m_pCoreEventMan->BroadcastEvent( ev );
Jonas Stienen's avatar
Jonas Stienen committed
1017

1018
		VA_INFO( "Core", "FreeDirectivity successful, freed directivity " << iDirID );
Jonas Stienen's avatar
Jonas Stienen committed
1019
1020
1021
1022
1023
1024

		return bSuccess;

	} VA_RETHROW;
}

1025
CVADirectivityInfo CVACoreImpl::GetDirectivityInfo( int iDirID ) const {
Jonas Stienen's avatar
Jonas Stienen committed
1026
1027
1028
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

1029
1030
	VA_TRY{
		return m_pDirMan->GetDirectivityInfo( iDirID );
Jonas Stienen's avatar
Jonas Stienen committed
1031
1032
1033
	} VA_RETHROW;
}

1034
1035
void CVACoreImpl::GetDirectivityInfos( std::vector<CVADirectivityInfo>& vdiDest ) const
{
Jonas Stienen's avatar
Jonas Stienen committed
1036
1037
1038
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

1039
1040
	VA_TRY{
		m_pDirMan->GetDirectivityInfos( vdiDest );
Jonas Stienen's avatar
Jonas Stienen committed
1041
1042
1043
1044
1045
1046
1047
1048
1049
	} VA_RETHROW;
}

/* +----------------------------------------------------------+ *
 * |                                                          | *
 * |   Head-related impulse responses (HRIRs)                 | *
 * |                                                          | *
 * +----------------------------------------------------------+ */

1050
int CVACoreImpl::LoadHRIRDataset( const std::string& sFilename, const std::string& sName )
Jonas Stienen's avatar
Jonas Stienen committed
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
{
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

	VA_TRY
	{
		std::string sDestFilePath = FindFilePath( sFilename );
		if( sDestFilePath.empty() )
			VA_EXCEPT2( INVALID_PARAMETER, "Looked everywhere, but could not find file '" + sFilename + "'" );

		int iHRIRID = m_pHRIRMan->LoadHRIR( sDestFilePath, sName );
		assert( iHRIRID != -1 );

		CVACoreEvent ev;
		ev.iEventType = CVACoreEvent::VA_COREEVENT_HRIR_LOADED;
		ev.pSender = this;
		ev.iObjectID = iHRIRID;
1068
		m_pCoreEventMan->BroadcastEvent( ev );
Jonas Stienen's avatar
Jonas Stienen committed
1069

1070
		VA_INFO( "Core", "HRIR dataset \"" << sDestFilePath << "\" successfully loaded, assigned HRIR ID " << iHRIRID );
Jonas Stienen's avatar
Jonas Stienen committed
1071
1072
1073
1074
1075
1076

		return iHRIRID;

	} VA_RETHROW;
}

1077
1078
bool CVACoreImpl::FreeHRIRDataset( int iHRIRID )
{
Jonas Stienen's avatar
Jonas Stienen committed
1079
1080
1081
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

1082
	VA_TRY
1083
1084
	{
		if( !m_pHRIRMan->FreeHRIR( iHRIRID ) )
1085
1086
		return false;

Jonas Stienen's avatar
Jonas Stienen committed
1087
1088
1089
1090
1091
		// Ereignis generieren, wenn Operation erfolgreich
		CVACoreEvent ev;
		ev.iEventType = CVACoreEvent::VA_COREEVENT_HRIR_FREED;
		ev.pSender = this;
		ev.iObjectID = iHRIRID;
1092
		m_pCoreEventMan->BroadcastEvent( ev );
Jonas Stienen's avatar
Jonas Stienen committed
1093

1094
		VA_INFO( "Core", "Released HRIR dataset " << iHRIRID );
Jonas Stienen's avatar
Jonas Stienen committed
1095

1096
		return true;
Jonas Stienen's avatar
Jonas Stienen committed
1097

1098
1099
	}
	VA_RETHROW;
Jonas Stienen's avatar
Jonas Stienen committed
1100
1101
}

1102
CVAHRIRInfo CVACoreImpl::GetHRIRInfo( int iHRIRID ) const
1103
{
Jonas Stienen's avatar
Jonas Stienen committed
1104
1105
1106
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

1107
1108
1109
1110
1111
	VA_TRY
	{
		return m_pHRIRMan->GetHRIRInfo( iHRIRID );
	}
	VA_RETHROW;
Jonas Stienen's avatar
Jonas Stienen committed
1112
1113
}

1114
void CVACoreImpl::GetHRIRInfos( std::vector<CVAHRIRInfo>& vhiDest ) const {
Jonas Stienen's avatar
Jonas Stienen committed
1115
1116
1117
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

1118
1119
	VA_TRY{
		m_pHRIRMan->GetHRIRInfos( vhiDest );
Jonas Stienen's avatar
Jonas Stienen committed
1120
1121
1122
1123
1124
1125
1126
1127
	} VA_RETHROW;
}

/* +----------------------------------------------------------+ *
 * |                                                          | *
 * |   Signalquellen                                          | *
 * |                                                          | *
 * +----------------------------------------------------------+ */
1128

1129
std::string CVACoreImpl::CreateAudiofileSignalSource( const std::string& sFilename, const std::string& sName )
Jonas Stienen's avatar
Jonas Stienen committed
1130
1131
1132
1133
1134
1135
1136
1137
1138
{
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

	VA_TRY
	{
		std::string sDestFilePath = FindFilePath( sFilename );
		if( sDestFilePath.empty() )
			VA_EXCEPT2( INVALID_PARAMETER, "Looked everywhere, but could not find file '" + sFilename + "'" );
1139

Jonas Stienen's avatar
Jonas Stienen committed
1140
1141
1142
1143
1144
1145
1146
		std::string sID = m_pSignalSourceMan->CreateAudiofileSignalSource( sDestFilePath, sName );
		assert( !sID.empty() );

		CVACoreEvent ev;
		ev.iEventType = CVACoreEvent::VA_COREEVENT_SIGNALSOURCE_CREATED;
		ev.pSender = this;
		ev.sObjectID = sID;
1147
		m_pCoreEventMan->BroadcastEvent( ev );
Jonas Stienen's avatar
Jonas Stienen committed
1148

1149
		VA_INFO( "Core", "Created audiofile signal source (ID=" << sID << ", Name=\"" << sName << "\", Filename=\"" << sDestFilePath << "\")" );
Jonas Stienen's avatar
Jonas Stienen committed
1150
1151
1152
1153
1154
1155

		return sID;
	}
	VA_RETHROW;
}

1156
1157
std::string CVACoreImpl::CreateTextToSpeechSignalSource( const std::string& sName /*= "" */ )
{
Jonas Stienen's avatar
Jonas Stienen committed
1158
1159
1160
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
	VA_TRY
	{
		std::string sID = m_pSignalSourceMan->CreateTextToSpeechSignalSource( sName );
		assert( !sID.empty() );

		CVACoreEvent ev;
		ev.iEventType = CVACoreEvent::VA_COREEVENT_SIGNALSOURCE_CREATED;
		ev.pSender = this;
		ev.sObjectID = sID;
		m_pCoreEventMan->BroadcastEvent( ev );

1172
		VA_INFO( "Core", "Created text-to-speech signal source ( ID=" << sID << ", Name=\"" << sName << "\" )" );
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185

		return sID;
	}
	VA_RETHROW;
}

std::string CVACoreImpl::CreateSequencerSignalSource( const std::string& sName )
{
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

	VA_TRY
	{
1186
		std::string sID = m_pSignalSourceMan->CreateSequencerSignalSource( sName );
Jonas Stienen's avatar
Jonas Stienen committed
1187
1188
1189
1190
1191
1192

		// Ereignis generieren, wenn Operation erfolgreich
		CVACoreEvent ev;
		ev.iEventType = CVACoreEvent::VA_COREEVENT_SIGNALSOURCE_CREATED;
		ev.pSender = this;
		ev.sObjectID = sID;
1193
		m_pCoreEventMan->BroadcastEvent( ev );
Jonas Stienen's avatar
Jonas Stienen committed
1194

1195
		VA_INFO( "Core", "Created sequencer signal source (ID=" << sID << ", Name=\"" << sName << "\")" );
Jonas Stienen's avatar
Jonas Stienen committed
1196
1197

		return sID;
1198
1199
	}
	VA_RETHROW;
Jonas Stienen's avatar
Jonas Stienen committed
1200
1201
}

1202
1203
std::string CVACoreImpl::CreateNetworkStreamSignalSource( const std::string& sInterface, int iPort, const std::string& sName )
{
Jonas Stienen's avatar
Jonas Stienen committed
1204
1205
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;
1206

1207
1208
	VA_TRY
	{
1209
		VA_TRACE( "Core", "Attempting to connect to a netstream signal source on " << sInterface << " with port " << iPort );
1210
		std::string sID = m_pSignalSourceMan->CreateNetstreamSignalSource( sInterface, iPort, sName );
Jonas Stienen's avatar
Jonas Stienen committed
1211
1212
1213
1214
1215
1216
1217
		assert( !sID.empty() );

		// Ereignis generieren, wenn Operation erfolgreich
		CVACoreEvent ev;
		ev.iEventType = CVACoreEvent::VA_COREEVENT_SIGNALSOURCE_CREATED;
		ev.pSender = this;
		ev.sObjectID = sID;
Dipl.-Ing. Jonas Stienen's avatar
Style    
Dipl.-Ing. Jonas Stienen committed
1218
		m_pCoreEventMan->BroadcastEvent( ev );
Jonas Stienen's avatar
Jonas Stienen committed
1219

1220
		VA_INFO( "Core", "Created network stream signal source (ID=" << sID << ", Name='" << sName << "', Interface=" << sInterface << ":" << iPort << ")" );
Jonas Stienen's avatar
Jonas Stienen committed
1221
1222

		return sID;
1223
1224
	}
	VA_RETHROW;
Jonas Stienen's avatar
Jonas Stienen committed
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
}

std::string CVACoreImpl::CreateEngineSignalSource( const std::string& sName )
{
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

	VA_TRY
	{
		std::string sID = m_pSignalSourceMan->CreateEngineSignalSource( sName );

		// Ereignis generieren, wenn Operation erfolgreich
		CVACoreEvent ev;
		ev.iEventType = CVACoreEvent::VA_COREEVENT_SIGNALSOURCE_CREATED;
		ev.pSender = this;
		ev.sObjectID = sID;
		m_pCoreEventMan->BroadcastEvent( ev );

1243
		VA_INFO( "Core", "Created engine signal source (ID=" << sID << ", Name=\"" << sName << "\")" );
Jonas Stienen's avatar
Jonas Stienen committed
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262

		return sID;
	} VA_RETHROW;
}


std::string CVACoreImpl::CreateMachineSignalSource( const std::string& sName/*="" */ )
{
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

	VA_TRY
	{
		std::string sID = m_pSignalSourceMan->CreateMachineSignalSource( sName );
		return sID;
	} VA_RETHROW;
}


1263
bool CVACoreImpl::DeleteSignalSource( const std::string& sID )
Jonas Stienen's avatar
Jonas Stienen committed
1264
1265
1266
1267
{
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

1268
1269
	VA_TRY
	{
1270
		m_pSignalSourceMan->DeleteSignalSource( sID );
Jonas Stienen's avatar
Jonas Stienen committed
1271
1272
1273
1274
1275
1276

		// Ereignis generieren, wenn Operation erfolgreich
		CVACoreEvent ev;
		ev.iEventType = CVACoreEvent::VA_COREEVENT_SIGNALSOURCE_DELETED;
		ev.pSender = this;
		ev.sObjectID = sID;
1277
		m_pCoreEventMan->BroadcastEvent( ev );
Jonas Stienen's avatar
Jonas Stienen committed
1278

1279
		VA_INFO( "Core", "Deleted signal source " << sID );
1280
		return true;
1281
1282
	}
	VA_RETHROW;
Jonas Stienen's avatar
Jonas Stienen committed
1283
1284
}

1285
1286
std::string CVACoreImpl::RegisterSignalSource( IVAAudioSignalSource* pSignalSource, const std::string& sName )
{
Jonas Stienen's avatar
Jonas Stienen committed
1287
1288
1289
	VA_NO_REENTRANCE;
	VA_CHECK_INITIALIZED;

1290
	VA_TRY{
Jonas Stienen's avatar
Jonas Stienen committed
1291

1292
		std::string sID = m_pSignalSourceMan->RegisterSignalSource