RedstartRunCirculatingSourceDialog.cpp 8.11 KB
Newer Older
1 2 3 4 5 6
/*
 *  --------------------------------------------------------------------------------------------
 *
 *    VVV        VVV A           Virtual Acoustics (VA) | http://www.virtualacoustics.org
 *     VVV      VVV AAA          Licensed under the Apache License, Version 2.0
 *      VVV    VVV   AAA
7
 *       VVV  VVV     AAA        Copyright 2015-2019
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *        VVVVVV       AAA       Institute of Technical Acoustics (ITA)
 *         VVVV         AAA      RWTH Aachen University
 *
 *  --------------------------------------------------------------------------------------------
 */

#include "RedstartRunCirculatingSourceDialog.h"
#include <ui_RedstartRunCirculatingSourceDialog.h>

#include <QFileDialog>
#include <QSettings>
#include <QErrorMessage>

#include "RedstartDefaultCoreConfig.h"

#include <VA.h>
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
24
#include <ITANumericUtils.h>
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

RedstartRunCirculatingSourceDialog::RedstartRunCirculatingSourceDialog( QWidget *parent )
	: QDialog( parent )
	, ui( new Ui::RedstartRunCirculatingSourceDialog )
	, m_pVAInterface( NULL )
{
	ui->setupUi( this );

	QDialog::setWindowTitle( "Run circulating source" );

	QSettings qSettings;
	QString sLastBrowseFolder = qSettings.value( "Redstart/last_browse_folder" ).toString();
	std::string s = sLastBrowseFolder.toStdString();

	if( QDir( sLastBrowseFolder ).exists() )
		m_oLastBasePath.setCurrent( sLastBrowseFolder );
41

42 43
	m_dAzimuthDEG = 0.0f;
	m_dElevationDEG = 0.0f;
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
44

45
	m_qVAUpdateTimer = new QTimer( this );
46
	m_qVAUpdateTimer->setTimerType( Qt::TimerType::PreciseTimer );
47 48

	connect( m_qVAUpdateTimer, SIGNAL( timeout() ), this, SLOT( UpdateScene() ) );
49 50 51 52 53 54 55
}

RedstartRunCirculatingSourceDialog::~RedstartRunCirculatingSourceDialog()
{
	if( m_oLastBasePath.exists() )
		m_qSettings.setValue( "Redstart/last_browse_folder", m_oLastBasePath.absolutePath() );
	delete ui;
56
	delete m_qVAUpdateTimer;
57 58 59 60 61 62 63 64 65 66 67 68 69
}

void RedstartRunCirculatingSourceDialog::SetVAInterface( IVAInterface* pVA )
{
	if( pVA )
		m_pVAInterface = pVA;
}

void RedstartRunCirculatingSourceDialog::Start()
{
	try
	{
		CreateScene();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
70
		SetUIElementsEnabledWhenNotRunning( false );
71
		StartUpdateTimer();
72 73 74
	}
	catch( CVAException& e )
	{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
75
		QString sErrorMessage = QString( "Could not create circulating source example scene: %1" ).arg( QString::fromStdString( e.ToString() ) );
76 77 78 79 80 81
		QErrorMessage oErrMsg;
		oErrMsg.showMessage( sErrorMessage );
		oErrMsg.exec();
	}
}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
82
void RedstartRunCirculatingSourceDialog::SetUIElementsEnabledWhenNotRunning( bool bYes )
83
{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
84 85 86 87 88 89 90 91
	const bool bNo = !bYes;
	ui->pushButton_start->setEnabled( bYes );
	ui->pushButton_stop->setEnabled( bNo );
	ui->pushButton_run->setEnabled( bYes );
	ui->lineEdit_macro_DefaultHRIR->setEnabled( bYes );
	ui->lineEdit_macro_DemoSound->setEnabled( bYes );
	ui->checkBox_folders_as_search_path->setEnabled( bYes );
	ui->doubleSpinBox_timeout->setEnabled( bYes );
92 93
}

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
94
void RedstartRunCirculatingSourceDialog::Stop()
95
{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
96 97
	StopUpdateTimer();
	SetUIElementsEnabledWhenNotRunning( true );
98 99 100 101
}

void RedstartRunCirculatingSourceDialog::on_pushButton_run_clicked()
{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
102
	Start();
103 104 105
	accept();
}

Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
106 107 108 109 110 111 112 113 114 115
void RedstartRunCirculatingSourceDialog::on_pushButton_start_clicked()
{
	Start();
}

void RedstartRunCirculatingSourceDialog::on_pushButton_stop_clicked()
{
	Stop();
}

116 117 118 119 120
void RedstartRunCirculatingSourceDialog::CreateScene()
{
	if( !m_pVAInterface )
		VA_EXCEPT2( MODAL_ERROR, "No connection to a VA core available" );

Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
121 122
	m_sSignalID = m_pVAInterface->CreateSignalSourceBufferFromFile( ui->lineEdit_macro_DemoSound->text().toStdString() ).c_str();
	m_pVAInterface->SetSignalSourceBufferLooping( m_sSignalID.toStdString(), true );
123

Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
124
	m_iSourceID = m_pVAInterface->CreateSoundSource( "Redstart C++ circulating sound source" );
125
	//m_pVAInterface->SetSoundSourcePose( m_iSourceID, VAVec3( 2.0f, 1.7f, 2.0f ), VAQuat( 0.0f, 0.0f, 0.0f, 1.0f ) );
126

Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
127
	m_pVAInterface->SetSoundSourceSignalSource( m_iSourceID, m_sSignalID.toStdString() );
128

Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
129
	m_iHRIRID = m_pVAInterface->CreateDirectivityFromFile( ui->lineEdit_macro_DefaultHRIR->text().toStdString() );
130

Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
131 132 133 134 135 136 137
	m_iReceiverID = m_pVAInterface->CreateSoundReceiver( "Redstart C++ example sound receiver" );
	m_pVAInterface->SetSoundReceiverPose( m_iReceiverID, VAVec3( 0.0f, 1.7f, 0.0f ), VAQuat( 0.0f, 0.0f, 0.0f, 1.0f ) );
	m_pVAInterface->SetSoundReceiverDirectivity( m_iReceiverID, m_iHRIRID );
}

void RedstartRunCirculatingSourceDialog::DeleteScene()
{
138 139 140 141 142
	m_pVAInterface->SetSoundSourceSignalSource( m_iSourceID, "" );
	m_pVAInterface->DeleteSoundSource( m_iSourceID );
	m_pVAInterface->DeleteSoundReceiver( m_iReceiverID );
	m_pVAInterface->DeleteDirectivity( m_iHRIRID );
	m_pVAInterface->DeleteSignalSource( m_sSignalID.toStdString() );
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
143 144 145 146 147
}

void RedstartRunCirculatingSourceDialog::UpdateScene()
{
	VAVec3 v3Pos;
148 149 150
	v3Pos.x = -std::sin( grad2rad( m_dAzimuthDEG ) ) * ui->doubleSpinBox_radius->value();
	v3Pos.y = std::sin( grad2rad( m_dElevationDEG ) ) * ui->doubleSpinBox_radius->value();
	v3Pos.z = -std::cos( grad2rad( m_dAzimuthDEG ) ) * ui->doubleSpinBox_radius->value();
151 152
	
	VAVec3 v3NewPos;
153 154 155
	try
	{
		m_pVAInterface->SetSoundSourcePosition( m_iSourceID, v3Pos );
156
		v3NewPos = m_pVAInterface->GetSoundSourcePosition( m_iSourceID );
157 158 159 160

		// Delayed plaback start so that no part of sentence is missing (source has to be placed in space first)
		if( m_pVAInterface->GetSignalSourceBufferPlaybackState( m_sSignalID.toStdString() ) != IVAInterface::VA_PLAYBACK_STATE_PLAYING )
			m_pVAInterface->SetSignalSourceBufferPlaybackAction( m_sSignalID.toStdString(), IVAInterface::VA_PLAYBACK_ACTION_PLAY );
161 162 163 164 165
	}
	catch( CVAException& e )
	{
		Stop();
	}
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
166

167 168 169 170 171 172 173 174 175
	ui->doubleSpinBox_azimuth->setValue( m_dAzimuthDEG );
	ui->doubleSpinBox_elevation->setValue( m_dElevationDEG );

	ui->doubleSpinBox_source_pos_x->setValue( v3NewPos.x );
	ui->doubleSpinBox_source_pos_y->setValue( v3NewPos.y );
	ui->doubleSpinBox_source_pos_z->setValue( v3NewPos.z );

	ui->doubleSpinBox_runtime_tracker->setValue( m_dRuntimeSeconds );

176
	m_dAzimuthDEG += ui->doubleSpinBox_azi_inc->value();
177
	m_dAzimuthDEG = anglef_proj_0_360_DEG( m_dAzimuthDEG );
178
	m_dElevationDEG += ui->doubleSpinBox_ele_inc->value();
179 180 181
	m_dElevationDEG = anglef_proj_N90_90_DEG( m_dElevationDEG );

	m_dRuntimeSeconds += ui->doubleSpinBox_timeout->value();
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
182

183
	if( m_dRuntimeSeconds > ui->doubleSpinBox_runtime->value() )
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
184
		Stop();
185 186 187 188
}

void RedstartRunCirculatingSourceDialog::StartUpdateTimer()
{
189 190 191 192
	const double dTimeout = ui->doubleSpinBox_timeout->value();
	const int iIntervalMS = ceil( dTimeout * 1000.0f );
	m_qVAUpdateTimer->setInterval( iIntervalMS );
	m_qVAUpdateTimer->start();
193
	m_dRuntimeSeconds = 0.0f;
194 195 196 197
}

void RedstartRunCirculatingSourceDialog::StopUpdateTimer()
{
198
	m_qVAUpdateTimer->stop();
199
	m_dRuntimeSeconds = 0.0f;
200 201
	m_dAzimuthDEG = 0.0f;
	m_dElevationDEG = 0.0f;
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
202

203 204 205 206 207 208
	try
	{
		DeleteScene();
	}
	catch( CVAException& e )
	{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
209 210 211
		// Quietly hide that something went wrong. It is possible, that a reset has been triggered or any
		// object managed by this dialog has been removed by another connection - we do not observer the scene, here.
		return;
212
	}
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
}

void RedstartRunCirculatingSourceDialog::on_pushButton_BrowseDemoSound_clicked()
{
	QFileDialog fd;
	fd.setNameFilter( "WAV files (*.wav)" );
	fd.setViewMode( QFileDialog::Detail );

	if( m_oLastBasePath.exists() )
		fd.setDirectory( m_oLastBasePath );
	else
		fd.setDirectory( QDir( QApplication::applicationDirPath() ) );

	if( fd.exec() )
	{
		QStringList lFiles = fd.selectedFiles();
		if( lFiles.empty() == false )
		{
			QString sFilePath = lFiles[ 0 ];
			QFile oFile( sFilePath );
			if( oFile.exists() )
			{
				m_oDemoSoundBasePath = fd.directory();
				m_oLastBasePath = m_oDemoSoundBasePath;
			}
			ui->lineEdit_macro_DemoSound->setText( oFile.fileName() );
		}
	}
}

void RedstartRunCirculatingSourceDialog::on_pushButton_BrowseHRIR_clicked()
{
	QFileDialog fd;
	fd.setNameFilter( "DAFF files (*.daff)" );
	fd.setViewMode( QFileDialog::Detail );

	if( m_oLastBasePath.exists() )
		fd.setDirectory( m_oLastBasePath );
	else
		fd.setDirectory( QDir( QApplication::applicationDirPath() ) );

	if( fd.exec() )
	{
		QStringList lFiles = fd.selectedFiles();
		if( lFiles.empty() == false )
		{
			QString sFilePath = lFiles[ 0 ];
			QFile oFile( sFilePath );
			if( oFile.exists() )
			{
				m_oHRIRBasePath = fd.directory();
				m_oLastBasePath = m_oHRIRBasePath;

			}

			ui->lineEdit_macro_DefaultHRIR->setText( sFilePath );
		}
	}
}