RedstartWindow.cpp 27.6 KB
Newer Older
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 *  --------------------------------------------------------------------------------------------
 *
 *    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
 *
 *  --------------------------------------------------------------------------------------------
 */

14
#include <QErrorMessage>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
15
#include <QUrl>
16
17
#include <QDesktopServices>
#include <QHash>
Dipl.-Ing. Jonas Stienen's avatar
Mooore    
Dipl.-Ing. Jonas Stienen committed
18
#include <QShortcut>
19
#include <QFileDialog>
20

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
21
22
23
#include "RedstartWindow.h"
#include <ui_RedstartWindow.h>

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
24
25
#include "RedstartRunSimpleExample.h"
#include "RedstartSessionBinauralHeadphonesDialog.h"
26
#include "RedstartSessionExperimentalTalkthroughDialog.h"
27
#include "RedstartSessionImportDialog.h"
28
#include "RedstartSessionWizardDialog.h"
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
29
30
#include "RedstartUtils.h"

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
31
32
33
34
#include <VA.h>
#include <VACore.h>
#include <VANet.h>

35
#include <ITAAsioInterface.h>
36
#include <ITAPortaudioInterface.h>
37
38
39
#include <ITAException.h>


40
RedstartWindow::RedstartWindow( bool bFailSafeMode, bool bAutoStart, bool bSkipConfig, QWidget* pParent )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
41
42
	: QMainWindow( pParent )
	, ui( new Ui::RedstartWindow )
43
	, m_pVAInterface( NULL )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
44
45
46
{
	ui->setupUi( this );

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
47
48
	ui->statusBar->showMessage( "Welcome to VA." );

Dipl.-Ing. Jonas Stienen's avatar
Mooore    
Dipl.-Ing. Jonas Stienen committed
49
50
51
52
53
54
55
56
57
58
59
	// Shortcuts
	QShortcut* start_b = new QShortcut( this );
	start_b->setKey( Qt::Key_F5 );
	start_b->setContext( Qt::WindowShortcut );
	QShortcut* start_c = new QShortcut( this );
	start_c->setKey( QKeySequence( tr( "CTRL+R" ) ) );
	start_c->setContext( Qt::WindowShortcut );

	connect( start_b, SIGNAL( activated() ), this, SLOT( on_pushButton_start_stop_clicked() ) );
	connect( start_c, SIGNAL( activated() ), this, SLOT( on_pushButton_start_stop_clicked() ) );

60
61
62
	m_pVANetServer = IVANetServer::Create();
	m_pVANetClient = IVANetClient::Create();

63
64
65
	ui->comboBox_audio_driver->addItem( "ASIO" );
	ui->comboBox_audio_driver->addItem( "Portaudio" );

66
67
68
69
70
71
	ui->comboBox_audio_iface_sampling_rate->addItem( "44.1 kHz", AudioSamplingRate::FS_44kHz );
	ui->comboBox_audio_iface_sampling_rate->addItem( "48 kHz", AudioSamplingRate::FS_48kHz );
	//ui->comboBox_audio_iface_sampling_rate->addItem( "96 kHz", AudioSamplingRate::FS_96kHz );

	ui->comboBox_audio_iface_buffer_size->addItem( "AUTO", AudioBufferSize::AUTO );

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
72
	m_iPortaudioDefaultDevice = -1;
73

74
75
	if( !bSkipConfig )
		LoadConfiguration();
76

77
78
	ui->listView_redstart_session_list;

79
80
	if( ui->checkBox_redstart_network_connect_as_client->isChecked() )
		ui->groupBox_redstart_audio_iface->setEnabled( false );
81

82
83
84
	if( ( ui->checkBox_redstart_auto_start->isChecked() || bAutoStart ) && !bFailSafeMode )
		on_pushButton_start_stop_clicked();

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
85
86
87
88
89
	//connect( &m_qRenderTimer, SIGNAL( timeout() ), this, SLOT( Render() ) );
}

RedstartWindow::~RedstartWindow()
{
90
	StoreConfiguration();
91
92
93
94
95
96
97
98
99
100
	if( m_pVANetServer->IsClientConnected() )
	{
		m_pVANetServer->Finalize();
		delete m_pVAInterface;
	}
	delete m_pVANetServer;

	if( m_pVANetClient->IsConnected() )
		m_pVANetClient->Disconnect();
	delete m_pVANetClient;
101
102
103
104
105
106
}

void RedstartWindow::LoadConfiguration()
{
	RestoreWindowSize();

107
108
	bool bValOK;
	int iAudioBackend = m_qSettings.value( "Redstart/audio/backend_idx" ).toInt( &bValOK );
109
110
111
	if( !bValOK )
		iAudioBackend = AudioBackend::ASIO;
	ui->comboBox_audio_driver->setCurrentIndex( iAudioBackend );
112

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	const int iBufferSizeIndex = m_qSettings.value( "Redstart/audio/buffersize_idx" ).toInt( &bValOK );
	if( bValOK && iBufferSizeIndex < ui->comboBox_audio_iface_buffer_size->count() )
		ui->comboBox_audio_iface_buffer_size->setCurrentIndex( iBufferSizeIndex );

	const int iSamplingRateIndex = m_qSettings.value( "Redstart/audio/fs_idx" ).toInt( &bValOK );
	if( bValOK && iSamplingRateIndex < ui->comboBox_audio_iface_sampling_rate->count() )
		ui->comboBox_audio_iface_sampling_rate->setCurrentIndex( iSamplingRateIndex );

	if( m_qSettings.contains( "Redstart/network/address" ) )
		ui->lineEdit_redstart_network_address->setText( m_qSettings.value( "Redstart/network/address" ).toString() );
	if( m_qSettings.contains( "Redstart/network/port" ) )
		ui->lineEdit_redstart_network_address->setText( m_qSettings.value( "Redstart/network/port" ).toString() );
	ui->checkBox_redstart_network_connect_as_client->setChecked( m_qSettings.value( "Redstart/network/client_mode" ).toBool() );

	if( m_qSettings.contains( "Redstart/autostart" ) )
128
		ui->checkBox_redstart_auto_start->setChecked( m_qSettings.value( "Redstart/autostart" ).toBool() );
129

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
130

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
131
132
133
	m_iPortaudioDefaultDevice = m_qSettings.value( "Redstart/audio/portaudio_default_device", -1 ).toInt();

	ui->checkBox_portaudio_default_device->setChecked( m_qSettings.value( "Redstart/audio/portaudio_use_default_device", true ).toBool() );
134

135
136
137
138
139
140
141
	QVariantList voAudioDevices;
	voAudioDevices = m_qSettings.value( "Redstart/audio/devicelist" ).toList();

	for( const QVariant oAudioDevice : voAudioDevices )
		m_voAudioDevices.push_back( CAudioDeviceSpecs( oAudioDevice.toHash() ) );

	PopulateAudioDevices();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
142
143
144
145

	int iLastDevice = m_qSettings.value( "Redstart/audio/current_device", 0 ).toInt( &bValOK );
	if( bValOK && ui->comboBox_audio_iface_device->count() >= iLastDevice )
		ui->comboBox_audio_iface_device->setCurrentIndex( iLastDevice );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
146

147
148
149
150
151
152
153
	// Core control
	ui->comboBox_core_settings_log_level->addItem( QString::fromStdString( m_pVAInterface->GetLogLevelStr( IVAInterface::VA_LOG_LEVEL_QUIET ) ) );
	ui->comboBox_core_settings_log_level->addItem( QString::fromStdString( m_pVAInterface->GetLogLevelStr( IVAInterface::VA_LOG_LEVEL_ERROR ) ) );
	ui->comboBox_core_settings_log_level->addItem( QString::fromStdString( m_pVAInterface->GetLogLevelStr( IVAInterface::VA_LOG_LEVEL_WARNING ) ) );
	ui->comboBox_core_settings_log_level->addItem( QString::fromStdString( m_pVAInterface->GetLogLevelStr( IVAInterface::VA_LOG_LEVEL_INFO ) ) );
	ui->comboBox_core_settings_log_level->addItem( QString::fromStdString( m_pVAInterface->GetLogLevelStr( IVAInterface::VA_LOG_LEVEL_VERBOSE ) ) );
	ui->comboBox_core_settings_log_level->addItem( QString::fromStdString( m_pVAInterface->GetLogLevelStr( IVAInterface::VA_LOG_LEVEL_TRACE ) ) );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
154

Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
155
156
157
	if( ui->listView_redstart_session_list->GetCurrentSessionID().isEmpty() == false )
		ui->treeView_session_details->SetStruct( ui->listView_redstart_session_list->GetCurrentConfig() );

158
159
160
161
162
163
164
}

void RedstartWindow::StoreConfiguration()
{
	m_qSettings.setValue( "Redstart/gui/WindowGeometry", saveGeometry() );
	m_qSettings.setValue( "Redstart/gui/WindowState", saveState() );

165
166
167
168
169
170
171
172
173
174
175
176
177
178
	m_qSettings.setValue( "Redstart/audio/backend_idx", ui->comboBox_audio_driver->currentIndex() );

	m_qSettings.setValue( "Redstart/audio/buffersize_idx", ui->comboBox_audio_iface_buffer_size->currentIndex() );
	m_qSettings.setValue( "Redstart/audio/fs_idx", ui->comboBox_audio_iface_sampling_rate->currentIndex() );

	m_qSettings.setValue( "Redstart/network/address", ui->lineEdit_redstart_network_address->text() );

	bool bValOK;
	int iPort = ui->lineEdit_redstart_network_address->text().toInt( &bValOK );
	if( bValOK )
		m_qSettings.setValue( "Redstart/network/port", iPort );

	m_qSettings.setValue( "Redstart/network/client_mode", ui->checkBox_redstart_network_connect_as_client->isChecked() );
	m_qSettings.setValue( "Redstart/autostart", ui->checkBox_redstart_auto_start->isChecked() );
179

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
180
181
182
	m_qSettings.setValue( "Redstart/audio/portaudio_default_device", m_iPortaudioDefaultDevice );
	int iLastDevice = ui->comboBox_audio_iface_device->currentIndex();
	m_qSettings.setValue( "Redstart/audio/current_device", iLastDevice );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
183

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
184
	m_qSettings.setValue( "Redstart/audio/portaudio_use_default_device", ui->checkBox_portaudio_default_device->isChecked() );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
185

186
187
188
	QVariantList voAudioDevices;
	for( const CAudioDeviceSpecs& oDevice : m_voAudioDevices )
		voAudioDevices.push_back( oDevice.asHash() );
189

190
	m_qSettings.setValue( "Redstart/audio/devicelist", voAudioDevices );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
191
}
192

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
193
194
195
void RedstartWindow::on_actionQuit_triggered()
{
	close();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
196
197
198
199
200
201
202
}

void RedstartWindow::PostCoreStart()
{
	ui->pushButton_start_stop->setText( "Stop" );

	// Switch UI elements enabled
203
	ui->groupBox_redstart_audio_iface->setEnabled( false );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
204
	ui->groupBox_redstart_network->setEnabled( false );
205
	ui->horizontalLayout_redstart_session->setEnabled( false );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
206
207
208
	ui->groupBox_core->setEnabled( true );
	ui->groupBox_paths->setEnabled( true );
	ui->groupBox_macros->setEnabled( true );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
209
	ui->menuRun->setEnabled( true );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
210
211

	CVAVersionInfo oVersion;
212
	m_pVAInterface->GetVersionInfo( &oVersion );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
213
214
215
216
	ui->lineEdit_core_settings_version->setText( QString::fromStdString( oVersion.ToString() ) );

	CVAStruct oArgs;
	oArgs[ "getloglevel" ] = true;
217
	const CVAStruct oReturn = m_pVAInterface->CallModule( "VACore", oArgs );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
218
	const int iCurrentLogLevel = oReturn[ "loglevel" ];
219
220

	ui->comboBox_core_settings_log_level->setCurrentIndex( iCurrentLogLevel );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
221
222
223
224
225
226
227
}

void RedstartWindow::PostCoreStop()
{
	ui->pushButton_start_stop->setText( "Start" );

	// Switch UI elements enabled
228
	ui->groupBox_redstart_audio_iface->setEnabled( true );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
229
	ui->groupBox_redstart_network->setEnabled( true );
230
	ui->horizontalLayout_redstart_session->setEnabled( true );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
231
232
233
	ui->groupBox_core->setEnabled( false );
	ui->groupBox_paths->setEnabled( false );
	ui->groupBox_macros->setEnabled( false );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
234
	ui->menuRun->setEnabled( false );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
235
}
236
237
238
239
240
241
242

void RedstartWindow::RestoreWindowSize()
{
	restoreGeometry( m_qSettings.value( "Redstart/gui/WindowGeometry" ).toByteArray() );
	restoreState( m_qSettings.value( "Redstart/gui/WindowState" ).toByteArray() );
}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
243
244
245
246
247
248
void RedstartWindow::HandleVAEvent( const CVAEvent* pEvent )
{
	// Handle pushable events
	switch( pEvent->iEventType )
	{
	case CVAEvent::VA_EVENT_PROGRESS_UPDATE:
249
		//ui->statusBar->showMessage( pEvent->sParam.c_str() );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
250
251
252
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
		break;
	case CVAEvent::VA_EVENT_MEASURES_UPDATE:
		UpdateMeasures( pEvent->vfInputRMSs, pEvent->vfInputPeaks, pEvent->vfOutputRMSs, pEvent->vfOutputPeaks );
		break;
	}

	// Don't handle events that have been triggered from this interface connection
	if( pEvent->pSender == m_pVAInterface )
		return;

	switch( pEvent->iEventType )
	{
	case CVAEvent::VA_EVENT_INPUT_GAIN_CHANGED:
		ui->dial_core_control_input_gain->setValue( pEvent->dVolume );
		break;
	case CVAEvent::VA_EVENT_OUTPUT_GAIN_CHANGED:
		ui->dial_core_control_output_gain->setValue( pEvent->dVolume );
		break;
	case CVAEvent::VA_EVENT_INPUT_MUTING_CHANGED:
		ui->checkBox_core_control_input_mute->setChecked( pEvent->bMuted );
		break;
	case CVAEvent::VA_EVENT_OUTPUT_MUTING_CHANGED:
		ui->checkBox_core_control_output_mute->setChecked( pEvent->bMuted );
		break;
	}
}

void RedstartWindow::UpdateMeasures( const std::vector< float >& vfInputRMSs, const std::vector< float >& vfInputPeaks, const std::vector< float >& vfOutputRMSs, const std::vector< float >& vfOutputPeaks )
{
	// @todo feed level meters 
}

282
283
void RedstartWindow::AcquireAsioDevices()
{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
284
285
	std::vector< CAudioDeviceSpecs > voAudioDevices = m_voAudioDevices;

286
287
288
289
290
291
292
293
	try
	{
		ITAsioInitializeLibrary();

		long lDrivers = ITAsioGetNumDrivers();
		if( lDrivers <= 0 )
			ITA_EXCEPT1( INVALID_PARAMETER, "No ASIO-compatibel drivers could be found on this system." );

294
		m_voAudioDevices.clear();
295
296
297
298
299

		for( long i = 0; i < lDrivers; i++ )
		{
			CAudioDeviceSpecs oDeviceSpecs;
			oDeviceSpecs.iDriverNumber = i;
300
			oDeviceSpecs.iBackend = AudioBackend::ASIO;
301
302
303
304
305
306
			oDeviceSpecs.sName = ITAsioGetDriverName( i );

			try
			{
				ITAsioInitializeDriver( i );

307
308
				long in = 0;
				long out = 0;
309
310
				ITAsioGetChannels( &in, &out );
				oDeviceSpecs.iNumInputChannels = int( in );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
311
				oDeviceSpecs.iNumOutputChannels = int( out );
312
313
314
315
316
317
318
319
320
321
322
323
324

				ITAsioFinalizeDriver();

				oDeviceSpecs.bInitializable = true;

			}
			catch( const ITAException& )
			{
				oDeviceSpecs.bInitializable = false;
				continue;
			}

			if( oDeviceSpecs.bInitializable )
325
				m_voAudioDevices.push_back( oDeviceSpecs );
326
327
328
329
330
331
332
333
334
335
336
337
		}

		ITAsioFinalizeLibrary();
	}
	catch( const ITAException& e )
	{
		QString sErrorMessage = QString( "Error during ASIO device acquisition: %1" ).arg( QString::fromStdString( e.ToString() ) );
		QErrorMessage oErrMsg;
		oErrMsg.showMessage( sErrorMessage );
		oErrMsg.exec();
	}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
338
339
340
	for( const CAudioDeviceSpecs oDevice : voAudioDevices )
		if( oDevice.iBackend != AudioBackend::ASIO )
			m_voAudioDevices.push_back( oDevice );
341
342
}

343
344
345
void RedstartWindow::PopulateAudioDevices()
{
	ui->comboBox_audio_iface_device->clear();
346
	int iCurrentBackend = ui->comboBox_audio_driver->currentIndex();
347
348
	for( const CAudioDeviceSpecs& oDevice : m_voAudioDevices )
	{
349
350
351
352
353
354
355
356
357
		if( oDevice.iBackend == iCurrentBackend )
			ui->comboBox_audio_iface_device->addItem( oDevice.sName, QVariant( oDevice.iDriverNumber ) );
	}

	if( ui->comboBox_audio_driver->currentIndex() == AudioBackend::PORTAUDIO )
	{
		ui->checkBox_portaudio_default_device->setEnabled( true );
		if( ui->checkBox_portaudio_default_device->isChecked() )
			ui->comboBox_audio_iface_device->setEnabled( false );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
358
359
		else
			ui->comboBox_audio_iface_device->setEnabled( true );
360
361
362
363
364
	}
	else
	{
		ui->checkBox_portaudio_default_device->setEnabled( false );
		ui->comboBox_audio_iface_device->setEnabled( true );
365
366
367
	}
}

368
369
void RedstartWindow::AcquirePortaudioDevices()
{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
	std::vector< CAudioDeviceSpecs > voAudioDevices = m_voAudioDevices;

	try
	{
		ITAPortaudioInterface oPortaudio( 44100, 1024 );
		oPortaudio.Initialize();
		int lDrivers = oPortaudio.GetNumDevices();

		if( lDrivers <= 0 )
			ITA_EXCEPT1( INVALID_PARAMETER, "No Portaudio output devices could be found on this system." );

		m_iPortaudioDefaultDevice = oPortaudio.GetDefaultOutputDevice();

		m_voAudioDevices.clear();

		for( long i = 0; i < lDrivers; i++ )
		{
			CAudioDeviceSpecs oDeviceSpecs;
			oDeviceSpecs.iDriverNumber = i;
			oDeviceSpecs.iBackend = AudioBackend::PORTAUDIO;
			oDeviceSpecs.sName = oPortaudio.GetDeviceName( i ).c_str();
391

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
392
393
394
395
396
397
398
399
400
401
			try
			{
				oPortaudio.GetNumChannels( i, oDeviceSpecs.iNumInputChannels, oDeviceSpecs.iNumOutputChannels );

				oDeviceSpecs.bInitializable = true; // only a guess

				if( i == oPortaudio.GetDefaultOutputDevice() )
					oDeviceSpecs.bDefaultDevice = true;
				else
					oDeviceSpecs.bDefaultDevice = false;
402

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
			}
			catch( const ITAException& )
			{
				oDeviceSpecs.bInitializable = false;
				continue;
			}

			if( oDeviceSpecs.bInitializable )
				m_voAudioDevices.push_back( oDeviceSpecs );
		}

		oPortaudio.Finalize();
	}
	catch( const ITAException& e )
	{
		QString sErrorMessage = QString( "Error during Portaudio device acquisition: %1" ).arg( QString::fromStdString( e.ToString() ) );
		QErrorMessage oErrMsg;
		oErrMsg.showMessage( sErrorMessage );
		oErrMsg.exec();
	}

	for( const CAudioDeviceSpecs oDevice : voAudioDevices )
		if( oDevice.iBackend != AudioBackend::ASIO )
			m_voAudioDevices.push_back( oDevice );
427
428
}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
429
void RedstartWindow::on_pushButton_start_stop_clicked()
430
{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
431
432
433
434
435
436
437
	try
	{
		if( !ui->checkBox_redstart_network_connect_as_client->isChecked() )
		{
			if( m_pVAInterface )
			{
				m_pVANetServer->Finalize();
438

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
439
440
441
442
443
444
445
446
447
448
449
				if( m_pVAInterface->GetState() == IVAInterface::VA_CORESTATE_READY )
					m_pVAInterface->Finalize();

				delete m_pVAInterface;
				m_pVAInterface = nullptr;

				ui->statusBar->showMessage( "VA core has been stopped." );
				PostCoreStop();
			}
			else
			{
450
				CVAStruct oVAConfigArgs = ui->listView_redstart_session_list->GetCurrentConfig();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476

				// Override configs
				std::string sBackend, sDevice;
				switch( ui->comboBox_audio_driver->currentIndex() )
				{
				case AudioBackend::ASIO:
				{
					sBackend = "ASIO";
					sDevice = ui->comboBox_audio_iface_device->currentText().toStdString();
					break;
				}
				case AudioBackend::PORTAUDIO:
				default:
				{
					sBackend = "Portaudio";
					if( ui->checkBox_portaudio_default_device->isChecked() )
						sDevice = "default";
					else
						sDevice = ui->comboBox_audio_iface_device->currentIndex();
				}
				}

				oVAConfigArgs[ "Audio driver" ] = CVAStruct();
				oVAConfigArgs[ "Audio driver" ][ "Driver" ] = sBackend;
				oVAConfigArgs[ "Audio driver" ][ "Device" ] = sDevice;

477
478
479
480
				if( !oVAConfigArgs.HasKey( "Paths" ) )
					oVAConfigArgs[ "Paths" ] = CVAStruct();
				oVAConfigArgs[ "Paths" ][ "redstart_data" ] = "data";
				oVAConfigArgs[ "Paths" ][ "redstart_data_dev" ] = "../VACore/data";
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
481
482
483
484
485

				m_pVAInterface = VACore::CreateCoreInstance( oVAConfigArgs );
				m_pVAInterface->Initialize();

				// @todo attach event handler
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
486
				m_pVAInterface->AttachEventHandler( this );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
487
488
489
490
491
492
493
494
495
496
497
498
499

				m_pVANetServer->SetCoreInstance( m_pVAInterface );

				bool bOK;
				int iPort = ui->lineEdit_redstart_network_port->text().toInt( &bOK );
				int iStatus = -1;
				if( bOK )
					iStatus = m_pVANetServer->Initialize( ui->lineEdit_redstart_network_address->text().toStdString(), iPort );
				else
					iStatus = m_pVANetServer->Initialize( ui->lineEdit_redstart_network_address->text().toStdString() );

				if( m_pVAInterface->GetState() == IVAInterface::VA_CORESTATE_READY && iStatus == IVANetServer::VA_NO_ERROR )
				{
500
					ui->statusBar->showMessage( "VA core successfully initialized and network server started. Running session " + ui->listView_redstart_session_list->GetCurrentSessionID() + "." );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526

					PostCoreStart();
				}
				else
				{
					ui->statusBar->showMessage( "VA core initialization failed." );
					m_pVANetServer->Finalize();

					delete m_pVAInterface;
					m_pVAInterface = NULL;
				}
			}
		}
		else
		{
			// Client mode
			if( m_pVANetClient->IsConnected() )
				m_pVANetClient->Disconnect();

			bool bOK;
			int iPort = ui->lineEdit_redstart_network_port->text().toInt( &bOK );
			int iStatus = -1;
			if( bOK )
				iStatus = m_pVANetClient->Initialize( ui->lineEdit_redstart_network_address->text().toStdString(), iPort );
			else
				iStatus = m_pVANetClient->Initialize( ui->lineEdit_redstart_network_address->text().toStdString() );
527

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
528
529
530
531
532
533
534
535
536
537
538
539
540
			if( iStatus != IVANetClient::VA_NO_ERROR )
			{
				VA_EXCEPT2( INVALID_PARAMETER, "Could not initialize client, connection failed." );
			}

			m_pVAInterface = m_pVANetClient->GetCoreInstance();

			// @todo attach event handler
			//m_pVANetClient->AttachEventHandler( this );
		}
	}
	catch( const CVAException& ex )
	{
Dipl.-Ing. Jonas Stienen's avatar
Mooore    
Dipl.-Ing. Jonas Stienen committed
541
542
543
544
545
546
547

		if( m_pVAInterface )
		{
			delete m_pVAInterface;
			m_pVAInterface = NULL;
		}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
548
549
550
		ui->statusBar->showMessage( QString::fromStdString( ex.ToString() ) );
		ui->checkBox_redstart_auto_start->setChecked( false );
	}
551
}
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567

void RedstartWindow::on_pushButton_refresh_clicked()
{
	const int iAudioBackend = ui->comboBox_audio_driver->currentIndex();
	if( iAudioBackend == AudioBackend::PORTAUDIO )
	{
		AcquirePortaudioDevices();
	}
	else
	{
		AcquireAsioDevices();
	}

	PopulateAudioDevices();
}

Dipl.-Ing. Jonas Stienen's avatar
Mooore    
Dipl.-Ing. Jonas Stienen committed
568
void RedstartWindow::on_comboBox_audio_driver_currentIndexChanged( int )
569
570
571
572
{
	PopulateAudioDevices();
}

Dipl.-Ing. Jonas Stienen's avatar
Mooore    
Dipl.-Ing. Jonas Stienen committed
573
void RedstartWindow::on_comboBox_audio_iface_device_currentIndexChanged( int )
574
575
576
{

}
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
577
578
579

void RedstartWindow::on_actionWebsite_triggered()
{
580
581
	QUrl urlVAWebsite( "http://www.virtualacoustics.org/work" );
	QDesktopServices::openUrl( urlVAWebsite );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
582
583
584
585
}

void RedstartWindow::on_actionGet_started_triggered()
{
586
587
	QUrl urlVAWebsiteGetStarted( "http://www.virtualacoustics.org/work/start.html" );
	QDesktopServices::openUrl( urlVAWebsiteGetStarted );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
588
589
590
591
}

void RedstartWindow::on_actionDocumentation_triggered()
{
592
593
	QUrl urlVAWebsiteDocumentation( "http://www.virtualacoustics.org/work/documentation.html" );
	QDesktopServices::openUrl( urlVAWebsiteDocumentation );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
594
595
596
597
}

void RedstartWindow::on_actionGet_help_triggered()
{
598
599
	QUrl urlVAWebsiteGetHelp( "http://www.virtualacoustics.org/work/help.html" );
	QDesktopServices::openUrl( urlVAWebsiteGetHelp );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
600
}
601
602
603
604
605
606
607
608
609
610
611
612

void RedstartWindow::on_checkBox_redstart_network_connect_as_client_clicked()
{
	if( ui->checkBox_redstart_network_connect_as_client->isChecked() )
		ui->groupBox_redstart_audio_iface->setEnabled( false );
	else
		ui->groupBox_redstart_audio_iface->setEnabled( true );
}

void RedstartWindow::on_checkBox_portaudio_default_device_clicked()
{
	if( ui->checkBox_portaudio_default_device->isChecked() )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
613
	{
614
		ui->comboBox_audio_iface_device->setEnabled( false );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
615
616
617
		if( ui->comboBox_audio_iface_device->count() >= m_iPortaudioDefaultDevice )
			ui->comboBox_audio_iface_device->setCurrentIndex( m_iPortaudioDefaultDevice );
	}
618
619
620
621
	else
		ui->comboBox_audio_iface_device->setEnabled( true );
}

622
void RedstartWindow::on_listView_redstart_session_list_clicked( const QModelIndex &index )
623
{
Dipl.-Ing. Jonas Stienen's avatar
Mooore    
Dipl.-Ing. Jonas Stienen committed
624
625
626
627
628
629
	if( ui->listView_redstart_session_list->model()->data( index ).isValid() )
	{
		std::string sSessionName = ui->listView_redstart_session_list->model()->data( index ).toString().toStdString();
		CVAStruct oStruct = ui->listView_redstart_session_list->GetCurrentConfig();
		ui->treeView_session_details->SetStruct( oStruct );
	}
630
}
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
631
632
633
634
635

void RedstartWindow::on_actionBinauralHeadphones_triggered()
{
	RedstartSessionBinauralHeadphonesDialog d( this );
	if( d.exec() )
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
636
	{
637
638
639
640
641
642
		// Enforce unique session id if id is already existing
		QString sNewID = d.GetSessionName();
		while( ui->listView_redstart_session_list->SessionNameExists( sNewID ) )
			sNewID += "_new";
		ui->listView_redstart_session_list->AddSession( sNewID, d.GetCoreConfig() ); // In any case save the session so no data is lost

Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
643
644
645
		CVAStruct oStruct = ui->listView_redstart_session_list->GetCurrentConfig();
		ui->treeView_session_details->SetStruct( oStruct );
	}
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
646
647
}

648
649
650
651
652
653
654
void RedstartWindow::on_actionImport_session_triggered()
{
	RedstartSessionImportDialog d( this );
	if( d.exec() )
	{
		try
		{
655
656
657
658
659
			// Enforce unique session id if id is already existing
			QString sNewID = d.GetSessionName();
			while( ui->listView_redstart_session_list->SessionNameExists( sNewID ) )
				sNewID += "_new";
			ui->listView_redstart_session_list->AddSession( sNewID, d.GetCoreConfig() ); // In any case save the session so no data is lost
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
660
661
662
			CVAStruct oStruct = ui->listView_redstart_session_list->GetCurrentConfig();
			ui->treeView_session_details->SetStruct( oStruct );

663
664
665
666
667
668
669
670
671
672
673
		}
		catch( CVAException& e )
		{
			QString sErrorMessage = QString( "Could not run simple example scene: %1" ).arg( QString::fromStdString( e.ToString() ) );
			QErrorMessage oErrMsg;
			oErrMsg.showMessage( sErrorMessage );
			oErrMsg.exec();
		}
	}
}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
674
675
void RedstartWindow::on_actionDefault_experimental_session_triggered()
{
676
677
	RedstartSessionExperimentalTalkthroughDialog d( this );
	if( d.exec() )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
678
	{
679
680
681
682
683
684
		// Enforce unique session id if id is already existing
		QString sNewID = d.GetSessionName();
		while( ui->listView_redstart_session_list->SessionNameExists( sNewID ) )
			sNewID += "_new";
		ui->listView_redstart_session_list->AddSession( sNewID, d.GetCoreConfig() ); // In any case save the session so no data is lost

Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
685
686
		CVAStruct oStruct = ui->listView_redstart_session_list->GetCurrentConfig();
		ui->treeView_session_details->SetStruct( oStruct );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
687
688
	}
}
689
690
691
692
693

void RedstartWindow::on_actionCirculating_source_triggered()
{

}
694
695
696
697
698

void RedstartWindow::on_actionAmbisonics_triggered()
{

}
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740

void RedstartWindow::on_actionExport_to_file_triggered()
{
	QFileDialog fd;
	fd.setNameFilters( QStringList() << "INI files (*.ini)" << "Any file (*.*)" );
	fd.setViewMode( QFileDialog::Detail );
	fd.setAcceptMode( QFileDialog::AcceptSave );

	QDir oLastDir( m_qSettings.value( "Redstart/last_browse_folder" ).toString() );
	if( oLastDir.exists() )
		fd.setDirectory( oLastDir );
	else
		fd.setDirectory( QDir( QApplication::applicationDirPath() ) );

	QString sFileName = ui->listView_redstart_session_list->GetCurrentSessionID();
	if( sFileName.isEmpty() == false )
		fd.selectFile( sFileName + ".ini" );

	if( fd.exec() )
	{
		QStringList lFiles = fd.selectedFiles();
		if( lFiles.empty() == false )
		{
			QString sFilePath = lFiles[ 0 ];
			QFileInfo oFile( sFilePath );
			if( fd.directory().exists() )
				m_qSettings.setValue( "Redstart/last_browse_folder", oFile.absolutePath() );

			try
			{
				CVAStruct oConfig = ui->listView_redstart_session_list->GetCurrentConfig();
				VACore::StoreCoreConfigToFile( oConfig, sFilePath.toStdString() );
			}
			catch( CVAException& e )
			{
				QString sErrorMessage = QString( "Could not export session configuration: %1" ).arg( QString::fromStdString( e.ToString() ) );
				QErrorMessage oErrMsg;
				oErrMsg.showMessage( sErrorMessage );
				oErrMsg.exec();
			}
		}
	}
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
741
742
743
744
745
}

void RedstartWindow::on_actionRemove_triggered()
{
	ui->listView_redstart_session_list->RemoveCurrentSession();
746

Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
747
748
749
	CVAStruct oStruct;
	if( !ui->listView_redstart_session_list->GetCurrentSessionID().isEmpty() )
		oStruct = ui->listView_redstart_session_list->GetCurrentConfig();
750

Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
751
	ui->treeView_session_details->SetStruct( oStruct );
752
}
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
753
754
755
756
757
758
759

void RedstartWindow::on_actionEdit_session_triggered()
{
	RedstartSessionWizardDialog d( this );

	QString sCurrentSessionID = ui->listView_redstart_session_list->GetCurrentSessionID();
	CVAStruct oCurrentConfig;
760
761
762
763
764
765
766
	if( sCurrentSessionID.isEmpty() )
		return;

	oCurrentConfig = ui->listView_redstart_session_list->GetCurrentConfig();
	d.SetSession( sCurrentSessionID, ConvertVAStructToQHash( oCurrentConfig ) );

	if( d.exec() )
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
767
	{
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
		bool bEnforceDuplicate = true;
		if( sCurrentSessionID != d.GetSessionName() && !ui->listView_redstart_session_list->SessionNameExists( d.GetSessionName() ) )
		{
			ui->listView_redstart_session_list->RenameSession( sCurrentSessionID, d.GetSessionName() );
			bEnforceDuplicate = false;
		}

		QVariantHash oNewConfig = d.GetCoreConfig();
		if( d.GetDuplicationRequested() || bEnforceDuplicate )
		{
			// Enforce unique session id if duplicate requested or session should be renamed to an already existing session
			QString sNewID = d.GetSessionName();
			while( ui->listView_redstart_session_list->SessionNameExists( sNewID ) )
				sNewID += "_copy";

			ui->listView_redstart_session_list->AddSession( sNewID, oNewConfig );
		}
		else
		{
			ui->listView_redstart_session_list->UpdateSession( d.GetSessionName(), oNewConfig );
		}

		ui->treeView_session_details->SetStruct( ConvertQHashToVAStruct( oNewConfig ) );
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
791
	}
792
793
794
795
796
797
798
}

void RedstartWindow::on_actionSession_wizard_triggered()
{
	RedstartSessionWizardDialog d( this );

	d.SetDuplicationOptionDeactivated();
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
799
800
801
802

	if( d.exec() )
	{
		QVariantHash oNewConfig = d.GetCoreConfig();
803
804
805
806
807
808
809
810
		if( ui->listView_redstart_session_list->SessionNameExists( d.GetSessionName() ) )
		{
			// Enforce unique session id if id is already existing
			QString sNewID = d.GetSessionName();
			while( ui->listView_redstart_session_list->SessionNameExists( sNewID ) )
				sNewID += "_new";
			ui->listView_redstart_session_list->AddSession( sNewID, oNewConfig ); // In any case save the session so no data is lost
		}
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
811
		else
812
813
814
815
		{
			ui->listView_redstart_session_list->AddSession( d.GetSessionName(), oNewConfig );
		}

Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
816
817
818
819
		ui->treeView_session_details->SetStruct( ConvertQHashToVAStruct( oNewConfig ) );
	}
}

820
void RedstartWindow::on_actionDuplicate_session_triggered()
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
821
{
822
823
824
825
826
827
828
829
830
831
832
833
	RedstartSessionWizardDialog d( this );

	QString sCurrentSessionID = ui->listView_redstart_session_list->GetCurrentSessionID();
	CVAStruct oCurrentConfig;
	if( sCurrentSessionID.isEmpty() )
		return;

	oCurrentConfig = ui->listView_redstart_session_list->GetCurrentConfig();
	d.SetSession( sCurrentSessionID, ConvertVAStructToQHash( oCurrentConfig ) );

	d.SetDuplicationOptionActivated();

Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
834
835
	if( d.exec() )
	{
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
		QVariantHash oNewConfig = d.GetCoreConfig();
		if( ui->listView_redstart_session_list->SessionNameExists( d.GetSessionName() ) )
		{
			// Enforce unique session id if id is already existing
			QString sNewID = d.GetSessionName();
			while( ui->listView_redstart_session_list->SessionNameExists( sNewID ) )
				sNewID += "_copy";
			ui->listView_redstart_session_list->AddSession( sNewID, oNewConfig ); // In any case save the session so no data is lost
		}
		else
		{
			ui->listView_redstart_session_list->AddSession( d.GetSessionName(), oNewConfig );
		}

		ui->treeView_session_details->SetStruct( ConvertQHashToVAStruct( oNewConfig ) );
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
	}
}

void RedstartWindow::on_actionRunSimpleExample_triggered()
{
	try
	{
		if( !m_pVAInterface )
			VA_EXCEPT2( INVALID_PARAMETER, "No VA interface available. Please start a session, first." );

		RunSimpleExample( m_pVAInterface );
	}
	catch( CVAException& e )
	{
		QString sErrorMessage = QString( "Could not run simple example scene: %1" ).arg( QString::fromStdString( e.ToString() ) );
		QErrorMessage oErrMsg;
		oErrMsg.showMessage( sErrorMessage );
		oErrMsg.exec();
	}
}