RedstartWindow.cpp 14 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>
18

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

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
22
23
24
25
#include <VA.h>
#include <VACore.h>
#include <VANet.h>

26
#include <ITAAsioInterface.h>
27
#include <ITAPortaudioInterface.h>
28
29
30
#include <ITAException.h>


31
RedstartWindow::RedstartWindow( bool bFailSafeMode, bool bAutoStart, bool bSkipConfig, QWidget* pParent )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
32
33
	: QMainWindow( pParent )
	, ui( new Ui::RedstartWindow )
34
	, m_pVAInterface( NULL )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
35
36
37
{
	ui->setupUi( this );

38
39
40
	m_pVANetServer = IVANetServer::Create();
	m_pVANetClient = IVANetClient::Create();

41
42
43
	ui->comboBox_audio_driver->addItem( "ASIO" );
	ui->comboBox_audio_driver->addItem( "Portaudio" );

44
45
46
47
48
49
	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 );

50
51
52
53
54
	if( !bSkipConfig )
		LoadConfiguration();
	
	if( ui->checkBox_redstart_network_connect_as_client->isChecked() )
		ui->groupBox_redstart_audio_iface->setEnabled( false );
55

56
57
58
	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
59
60
61
62
63
	//connect( &m_qRenderTimer, SIGNAL( timeout() ), this, SLOT( Render() ) );
}

RedstartWindow::~RedstartWindow()
{
64
	StoreConfiguration();
65
66
67
68
69
70
71
72
73
74
	if( m_pVANetServer->IsClientConnected() )
	{
		m_pVANetServer->Finalize();
		delete m_pVAInterface;
	}
	delete m_pVANetServer;

	if( m_pVANetClient->IsConnected() )
		m_pVANetClient->Disconnect();
	delete m_pVANetClient;
75
76
77
78
79
80
}

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

81
82
	bool bValOK;
	int iAudioBackend = m_qSettings.value( "Redstart/audio/backend_idx" ).toInt( &bValOK );
83
84
85
86
87
88
89
90
91
	if( !bValOK )
		iAudioBackend = AudioBackend::ASIO;
	ui->comboBox_audio_driver->setCurrentIndex( iAudioBackend );


	if( iAudioBackend == AudioBackend::PORTAUDIO )
		PopulatePortaudioDevicesFromSettings();
	else
		PopulateAsioDevicesFromSettings();
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

	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" ) )
108
		ui->checkBox_redstart_auto_start->setChecked( m_qSettings.value( "Redstart/autostart" ).toBool() );
109
110
111
112
113
114
115
116

	QVariantList voAudioDevices;
	voAudioDevices = m_qSettings.value( "Redstart/audio/devicelist" ).toList();

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

	PopulateAudioDevices();
117
118
119
120
121
122
123
}

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

124
125
126
127
128
129
130
131
132
133
134
135
136
137
	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() );
138
139
140
141
142
143

	QVariantList voAudioDevices;
	for( const CAudioDeviceSpecs& oDevice : m_voAudioDevices )
		voAudioDevices.push_back( oDevice.asHash() );

	m_qSettings.setValue( "Redstart/audio/devicelist", voAudioDevices );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
144
145
146
147
148
149
}

void RedstartWindow::on_actionQuit_triggered()
{
	close();
}
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
150
151
152

void RedstartWindow::on_pushButton_start_stop_clicked()
{
153
154
	try
	{
155
		if( !ui->checkBox_redstart_network_connect_as_client->isChecked() )
156
		{
157
158
159
160
161
162
			if( m_pVAInterface )
			{
				m_pVANetServer->Finalize();

				if( m_pVAInterface->GetState() == IVAInterface::VA_CORESTATE_READY )
					m_pVAInterface->Finalize();
163

164
165
				delete m_pVAInterface;
				m_pVAInterface = nullptr;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
166

167
168
169
170
171
				ui->statusBar->showMessage( "VA core has been stopped." );
				PostCoreStop();
			}
			else
			{
172
				// @todo get config from session
173
174
				CVAStruct oVAConfigArgs = VACore::GetCoreConfigFromFile( "../VACore/conf/VACore.ini" );

175

176
				// Override configs
177
				std::string sBackend, sDevice;
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
				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;

				oVAConfigArgs[ "Paths" ][ "redstart_data" ] = "../VACore/data";

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

				// @todo attach event handler
				//m_pVAInterface->AttachEventHandler( this );

				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 )
				{
					ui->statusBar->showMessage( "VA core successfully initialized and network server started." );

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

					delete m_pVAInterface;
					m_pVAInterface = NULL;
				}
			}
234
235
236
		}
		else
		{
237
238
239
			// Client mode
			if( m_pVANetClient->IsConnected() )
				m_pVANetClient->Disconnect();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
240

241
			m_pVAInterface = NULL;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
242

243
244
245
246
247
			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 );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
248
			else
249
250
251
				iStatus = m_pVANetClient->Initialize( ui->lineEdit_redstart_network_address->text().toStdString() );

			if( iStatus != IVANetClient::VA_NO_ERROR )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
252
			{
253
				VA_EXCEPT2( INVALID_PARAMETER, "Could not initialize client, connection failed." );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
254
			}
255
256
257
258
259

			m_pVAInterface = m_pVANetClient->GetCoreInstance();

			// @todo attach event handler
			//m_pVANetClient->AttachEventHandler( this );
260
261
262
263
264
		}
	}
	catch( const CVAException& ex )
	{
		ui->statusBar->showMessage( QString::fromStdString( ex.ToString() ) );
265
		ui->checkBox_redstart_auto_start->setChecked( false );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
266
267
268
269
270
271
272
273
	}
}

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

	// Switch UI elements enabled
274
	ui->groupBox_redstart_audio_iface->setEnabled( false );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
275
	ui->groupBox_redstart_network->setEnabled( false );
276
	ui->horizontalLayout_redstart_session->setEnabled( false );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
277
278
279
280
281
282
	ui->groupBox_core->setEnabled( true );
	ui->groupBox_paths->setEnabled( true );
	ui->groupBox_macros->setEnabled( true );


	CVAVersionInfo oVersion;
283
	m_pVAInterface->GetVersionInfo( &oVersion );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
284
285
286
287
	ui->lineEdit_core_settings_version->setText( QString::fromStdString( oVersion.ToString() ) );

	CVAStruct oArgs;
	oArgs[ "getloglevel" ] = true;
288
	const CVAStruct oReturn = m_pVAInterface->CallModule( "VACore", oArgs );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
289
	const int iCurrentLogLevel = oReturn[ "loglevel" ];
290
	ui->comboBox_core_settings_log_level->addItem( QString::fromStdString( m_pVAInterface->GetLogLevelStr( iCurrentLogLevel ) ) );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
291
292
293
294
295
296
297
}

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

	// Switch UI elements enabled
298
	ui->groupBox_redstart_audio_iface->setEnabled( true );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
299
	ui->groupBox_redstart_network->setEnabled( true );
300
	ui->horizontalLayout_redstart_session->setEnabled( true );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
301
302
303
304
	ui->groupBox_core->setEnabled( false );
	ui->groupBox_paths->setEnabled( false );
	ui->groupBox_macros->setEnabled( false );
}
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

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

void RedstartWindow::AcquireAsioDevices()
{
	try
	{
		ITAsioInitializeLibrary();

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

322
		m_voAudioDevices.clear();
323
324
325
326
327

		for( long i = 0; i < lDrivers; i++ )
		{
			CAudioDeviceSpecs oDeviceSpecs;
			oDeviceSpecs.iDriverNumber = i;
328
			oDeviceSpecs.iBackend = AudioBackend::ASIO;
329
330
331
332
333
334
			oDeviceSpecs.sName = ITAsioGetDriverName( i );

			try
			{
				ITAsioInitializeDriver( i );

335
336
				long in = 0;
				long out = 0;
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
				ITAsioGetChannels( &in, &out );
				oDeviceSpecs.iNumInputChannels = int( in );
				oDeviceSpecs.iNumInputChannels = int( out );

				ITAsioFinalizeDriver();

				oDeviceSpecs.bInitializable = true;

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

			if( oDeviceSpecs.bInitializable )
353
				m_voAudioDevices.push_back( oDeviceSpecs );
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
		}

		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();
	}
}

void RedstartWindow::PopulateAsioDevicesFromSettings()
{

}

372
373
374
void RedstartWindow::PopulateAudioDevices()
{
	ui->comboBox_audio_iface_device->clear();
375
	int iCurrentBackend = ui->comboBox_audio_driver->currentIndex();
376
377
	for( const CAudioDeviceSpecs& oDevice : m_voAudioDevices )
	{
378
379
380
381
382
383
384
385
386
387
388
389
390
391
		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 );
	}
	else
	{
		ui->checkBox_portaudio_default_device->setEnabled( false );
		ui->comboBox_audio_iface_device->setEnabled( true );
392
393
394
	}
}

395
396
397
398
399
400
401
402
403
void RedstartWindow::AcquirePortaudioDevices()
{

}

void RedstartWindow::PopulatePortaudioDevicesFromSettings()
{

}
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419

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

	PopulateAudioDevices();
}

420
void RedstartWindow::on_comboBox_audio_driver_currentIndexChanged( int iIndex )
421
422
423
424
{
	PopulateAudioDevices();
}

425
void RedstartWindow::on_comboBox_audio_iface_device_currentIndexChanged( int index )
426
427
428
{

}
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
429
430
431

void RedstartWindow::on_actionWebsite_triggered()
{
432
433
	QUrl urlVAWebsite( "http://www.virtualacoustics.org/work" );
	QDesktopServices::openUrl( urlVAWebsite );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
434
435
436
437
}

void RedstartWindow::on_actionGet_started_triggered()
{
438
439
	QUrl urlVAWebsiteGetStarted( "http://www.virtualacoustics.org/work/start.html" );
	QDesktopServices::openUrl( urlVAWebsiteGetStarted );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
440
441
442
443
}

void RedstartWindow::on_actionDocumentation_triggered()
{
444
445
	QUrl urlVAWebsiteDocumentation( "http://www.virtualacoustics.org/work/documentation.html" );
	QDesktopServices::openUrl( urlVAWebsiteDocumentation );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
446
447
448
449
}

void RedstartWindow::on_actionGet_help_triggered()
{
450
451
	QUrl urlVAWebsiteGetHelp( "http://www.virtualacoustics.org/work/help.html" );
	QDesktopServices::openUrl( urlVAWebsiteGetHelp );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
452
}
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474

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() )
		ui->comboBox_audio_iface_device->setEnabled( false );
	else
		ui->comboBox_audio_iface_device->setEnabled( true );

}

void RedstartWindow::on_listView_redstart_session_list_clicked(const QModelIndex &index)
{
	// m_pSessionManager->SetCurrentSession( index );
}