VAMatlabHelpers.cpp 19.4 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 14 15 16 17 18 19 20 21 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 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 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 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 677 678 679
#include "VAMatlabHelpers.h"

#include <VAException.h>

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <sstream>
#include <stdint.h>

bool matlabIsScalar( const mxArray *A )
{
	mwSize s = mxGetNumberOfDimensions( A );

	// Es mssen zwei Dimensionen vorliegen
	if( s != 2 ) return false;
	const mwSize* d = mxGetDimensions( A );

	// Beide Dimensionen mssen 1 sein
	return ( ( d[ 0 ] == 1 ) && ( d[ 1 ] == 1 ) );
}

bool matlabIsVector( const mxArray *A, int& size )
{
	mwSize s = mxGetNumberOfDimensions( A );

	// Es mssen zwei Dimensionen vorliegen
	if( s != 2 ) return false;
	const mwSize* d = mxGetDimensions( A );

	// Mindestens eine der beiden Dimensionen muss 1 sein
	if( d[ 0 ] == 1 && d[ 1 ] > 1 )
	{
		size = ( int ) d[ 1 ];
		return true;
	}

	if( d[ 1 ] == 1 && d[ 0 ] > 1 )
	{
		size = ( int ) d[ 0 ];
		return true;
	}

	return false;
}

bool matlabIsVector( const mxArray *A ) {
	int iDummy;
	return matlabIsVector( A, iDummy );
}

bool matlabIsRowVector( const mxArray *A )
{
	mwSize s = mxGetNumberOfDimensions( A );
	if( s != 2 ) return false;
	const mwSize* d = mxGetDimensions( A );
	return ( d[ 0 ] == 1 );
}

bool matlabIsColumnVector( const mxArray *A )
{
	mwSize s = mxGetNumberOfDimensions( A );
	if( s != 2 ) return false;
	const mwSize* d = mxGetDimensions( A );
	return ( d[ 1 ] == 1 );
}

bool matlabGetBoolScalar( const mxArray* arg, const char* argname )
{
	const int buflen = 1024;
	char buf[ buflen ];

	if( mxIsLogicalScalar( arg ) )
	{
		mxLogical* p = mxGetLogicals( arg );
		return ( *p == true );
	}

	if( mxIsNumeric( arg ) && !mxIsComplex( arg ) && matlabIsScalar( arg ) ) 
	{
		void* p = mxGetData( arg );

		if( mxIsInt16( arg ) ) return ( *( ( int16_t* ) p ) != 0 );
		if( mxIsInt32( arg ) ) return ( *( ( int32_t* ) p ) != 0 );
		if( mxIsInt64( arg ) ) return ( *( ( int64_t* ) p ) != 0 );
		if( mxIsSingle( arg ) ) return ( *( ( float* ) p ) != 0 );
		if( mxIsDouble( arg ) ) return ( *( ( double* ) p ) != 0 );

		// Unsupported datatype
		sprintf_s( buf, buflen, "Argument '%s' could not be interpreted as a logical scalar", argname );
		VA_EXCEPT1( buf );
		return false;
	}

	if( mxIsChar( arg ) && matlabIsRowVector( arg ) ) 
	{
		mxGetString( arg, buf, buflen );
		std::string s( buf );
		std::transform( s.begin(), s.end(), s.begin(), tolower );

		if( ( s == "true" ) || ( s == "yes" ) ) return true;
		if( ( s == "false" ) || ( s == "no" ) ) return false;

		// Unsupported datatype
		sprintf_s( buf, buflen, "Argument '%s' could not be interpreted as a logical scalar", argname );
		VA_EXCEPT1( buf );
		return false;
	}

	sprintf_s( buf, buflen, "Argument '%s' must be a logical scalar", argname );
	VA_EXCEPT1( buf );

	return false;
}

int matlabGetIntegerScalar( const mxArray* arg, const char* argname )
{
	char buf[ 1024 ];

	if( mxIsNumeric( arg ) && !mxIsComplex( arg ) && matlabIsScalar( arg ) )
	{
		void* p = mxGetData( arg );

		if( mxIsInt16( arg ) ) return ( int ) ( *( ( int16_t* ) p ) );
		if( mxIsInt32( arg ) ) return ( int ) ( *( ( int32_t* ) p ) );
		if( mxIsInt64( arg ) ) return ( int ) ( *( ( int64_t* ) p ) );	// [fwe] Possible truncation!

		if( mxIsSingle( arg ) ) {
			float fValue = *( ( float* ) p );
			// Check for integer
			if( fValue == floor( fValue ) )
				return ( int ) fValue;
		}

		if( mxIsDouble( arg ) ) {
			double dValue = *( ( double* ) p );
			// Check for integer
			if( dValue == floor( dValue ) )
				return ( int ) dValue; // [fwe] Possible truncation!
		}

		// Unsupported datatype
		sprintf_s( buf, 1024, "Argument '%s' could not be interpreted as an integer scalar", argname );
		VA_EXCEPT1( buf );
		return -1;
	}

	sprintf_s( buf, 1024, "Argument '%s' must be an integer scalar", argname );
	VA_EXCEPT1( buf );

	return -1;
}

double matlabGetRealScalar( const mxArray* arg, const char* argname )
{
	char buf[ 1024 ];

	bool b1 = mxIsNumeric( arg );
	bool b2 = !mxIsComplex( arg );
	bool b3 = matlabIsScalar( arg );
	if( b1 && b2 && b3 )
	{
		void* p = mxGetData( arg );

		if( mxIsInt16( arg ) ) return ( double ) ( *( ( int16_t* ) p ) );
		if( mxIsInt32( arg ) ) return ( double ) ( *( ( int32_t* ) p ) );
		if( mxIsInt64( arg ) ) return ( double ) ( *( ( int64_t* ) p ) );	// [fwe] Possible truncation!

		if( mxIsSingle( arg ) ) return *( ( float* ) p );
		if( mxIsDouble( arg ) ) return *( ( double* ) p );

		// Unsupported datatype
		sprintf_s( buf, 1024, "Argument '%s' could not be interpreted as a real-valued scalar", argname );
		VA_EXCEPT1( buf );
		return -1;
	}

	sprintf_s( buf, 1024, "Argument '%s' must be a real-valued scalar", argname );
	VA_EXCEPT1( buf );

	return -1;
}

void matlabGetRealVector3( const mxArray* arg, const char* argname, double& x, double& y, double& z )
{
	char buf[ 1024 ];
	int size;

	if( mxIsNumeric( arg ) && !mxIsComplex( arg ) && matlabIsVector( arg, size ) )
		if( size == 3 ) {

		if( mxIsInt16( arg ) ) {
			int16_t* p = ( int16_t* ) mxGetData( arg );
			x = ( double ) p[ 0 ];
			y = ( double ) p[ 1 ];
			z = ( double ) p[ 2 ];
			return;
		}

		if( mxIsInt32( arg ) ) {
			int32_t* p = ( int32_t* ) mxGetData( arg );
			x = ( double ) p[ 0 ];
			y = ( double ) p[ 1 ];
			z = ( double ) p[ 2 ];
			return;
		}

		if( mxIsInt64( arg ) ) {
			int64_t* p = ( int64_t* ) mxGetData( arg );
			x = ( double ) p[ 0 ];
			y = ( double ) p[ 1 ];
			z = ( double ) p[ 2 ];
			return;
		}

		if( mxIsSingle( arg ) ) {
			float* p = ( float* ) mxGetData( arg );
			x = ( double ) p[ 0 ];
			y = ( double ) p[ 1 ];
			z = ( double ) p[ 2 ];
			return;
		}

		if( mxIsDouble( arg ) ) {
			double* p = ( double* ) mxGetData( arg );
			x = p[ 0 ];
			y = p[ 1 ];
			z = p[ 2 ];
			return;
		}
		}

	sprintf_s( buf, 1024, "Argument '%s' must be a real-valued vector with exactly three elements", argname );
	VA_EXCEPT1( buf );
}

void matlabGetQuaternion( const mxArray* arg, const char* argname, double& w, double& i, double& j, double& k )
{
	char buf[ 1024 ];
	int size;

	if( mxIsNumeric( arg ) && !mxIsComplex( arg ) && matlabIsVector( arg, size ) )
	{
		if( size == 4 )
		{

			if( mxIsInt16( arg ) ) {
				int16_t* p = ( int16_t* ) mxGetData( arg );
				w = ( double ) p[ 0 ];
				i = ( double ) p[ 1 ];
				j = ( double ) p[ 2 ];
				k = ( double ) p[ 3 ];
				return;
			}

			if( mxIsInt32( arg ) ) {
				int32_t* p = ( int32_t* ) mxGetData( arg );
				w = ( double ) p[ 0 ];
				i = ( double ) p[ 1 ];
				j = ( double ) p[ 2 ];
				k = ( double ) p[ 3 ];
				return;
			}

			if( mxIsInt64( arg ) ) {
				int64_t* p = ( int64_t* ) mxGetData( arg );
				w = ( double ) p[ 0 ];
				i = ( double ) p[ 1 ];
				j = ( double ) p[ 2 ];
				k = ( double ) p[ 3 ];
				return;
			}

			if( mxIsSingle( arg ) ) {
				float* p = ( float* ) mxGetData( arg );
				w = ( double ) p[ 0 ];
				i = ( double ) p[ 1 ];
				j = ( double ) p[ 2 ];
				k = ( double ) p[ 3 ];
				return;
			}

			if( mxIsDouble( arg ) ) {
				double* p = ( double* ) mxGetData( arg );
				w = ( double ) p[ 0 ];
				i = ( double ) p[ 1 ];
				j = ( double ) p[ 2 ];
				k = ( double ) p[ 3 ];
				return;
			}
		}
	}

	sprintf_s( buf, 1024, "Argument '%s' must be a real-valued vector with exactly four elements (same order as Matlab quaternion: w (real), i, j, k)", argname );
	VA_EXCEPT1( buf );
}

std::string matlabGetString( const mxArray* arg, const char* argname )
{
	const int buflen = 1024;
	char buf[ buflen ];

	// TODO: [fwe] Diese Implementierung ist begrenzt in der Lnge. Sollte man dynamisch machen...

	// Test auf Zeichenfolge
	if( mxIsChar( arg ) )
	{
		// Spezialfall: Leere Eingabe
		if( mxIsEmpty( arg ) ) return std::string();

		if( matlabIsRowVector( arg ) )
		{
			mxGetString( arg, buf, buflen );
			return buf;
		}
	}

	sprintf_s( buf, 1024, "Argument '%s' must be a string", argname );
	VA_EXCEPT1( buf );
	return std::string();
}

mxArray* matlabCreateRealVector3( double x, double y, double z )
{
	mxArray* p = mxCreateDoubleMatrix( 3, 1, mxREAL );
	double* d = mxGetPr( p );
	d[ 0 ] = x;
	d[ 1 ] = y;
	d[ 2 ] = z;
	return p;
}

mxArray* matlabCreateID( int iID ) {
	mwSize d[ 2 ] = { 1, 1 };
	mxArray* p = mxCreateNumericArray( 2, d, mxINT32_CLASS, mxREAL );
	*( ( int* ) mxGetData( p ) ) = iID;
	return p;
}

mxArray* matlabCreateIDList( const std::vector<int>& viID )
{
	if( viID.empty() )
	{
		// Special case: Handle empty matrix as 0x0 => Matlab convenience :-}
		return mxCreateNumericMatrix( 0, 0, mxINT32_CLASS, mxREAL );
	}
	else {
		mxArray* a = mxCreateNumericMatrix( 1, int( viID.size() ), mxINT32_CLASS, mxREAL );
		int* p = ( int* ) mxGetData( a );
		for( size_t i = 0; i < viID.size(); i++ ) p[ i ] = viID[ i ];
		return a;
	}
}

mxArray* matlabCreateDirectivityInfo( const CVADirectivityInfo& di )
{
	/*
	int iID;					// ID
	std::string sName;			// Name (optional)
	std::string sFilename;		// Filename
	double dAlphaResDEG;		// Alpha resolution []
	double dBetaResDEG;			// Beta resolution []
	int iReferences;			// Number of references
	// TODO: Auflsung usw.
	*/

	const mwSize nFields = 5;
	const char* ppszFieldNames[] = { "id", "name", "filename", "description", "references" };

	mxArray* pStruct = mxCreateStructMatrix( 1, 1, nFields, ppszFieldNames );
	mxSetField( pStruct, 0, ppszFieldNames[ 0 ], matlabCreateID( di.iID ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 1 ], mxCreateString( di.sName.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 2 ], mxCreateString( di.sFilename.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 3 ], mxCreateString( di.sDesc.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 4 ], mxCreateDoubleScalar( di.iReferences ) ); // no of references as double-value in matlab

	return pStruct;
}

mxArray* matlabCreateHRIRInfo( const CVAHRIRInfo& hriri )
{
	/*
	int iID;					// ID
	std::string sName;			// Name (optional)
	std::string sFilename;		// Filename
	double dAlphaResDEG;		// Alpha resolution []
	double dBetaResDEG;			// Beta resolution []
	int iReferences;			// Number of references
	// TODO: More fields?
	*/

	const mwSize nFields = 5;
	const char* ppszFieldNames[] = { "id", "name", "filename", "alphaResDeg", "betaResDeg", "references" };

	mxArray* pStruct = mxCreateStructMatrix( 1, 1, nFields, ppszFieldNames );
	mxSetField( pStruct, 0, ppszFieldNames[ 0 ], matlabCreateID( hriri.iID ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 1 ], mxCreateString( hriri.sName.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 2 ], mxCreateString( hriri.sFilename.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 3 ], mxCreateString( hriri.sDesc.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 4 ], mxCreateDoubleScalar( hriri.iReferences ) ); // no of references as double-value in matlab

	return pStruct;
}

mxArray* matlabCreateSoundInfo( const CVASoundInfo& si )
{
	/*
	int iID;				// ID
	std::string sName;		// Name (optional)
	std::string sFilename;	// Filename
	int iLength;			// Length [Samples]
	double dDuration;		// Duration [s]
	*/

	const mwSize nFields = 5;
	const char* ppszFieldNames[] = { "id", "name", "filename", "nsamples", "duration" };

	mxArray* pStruct = mxCreateStructMatrix( 1, 1, nFields, ppszFieldNames );
	mxSetField( pStruct, 0, ppszFieldNames[ 0 ], matlabCreateID( si.iID ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 1 ], mxCreateString( si.sName.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 2 ], mxCreateString( si.sFilename.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 3 ], mxCreateDoubleScalar( si.iLength ) );			// Anzahl Samples als Double in Matlab
	mxSetField( pStruct, 0, ppszFieldNames[ 4 ], mxCreateDoubleScalar( si.dDuration ) );

	return pStruct;
}

mxArray* matlabCreateSignalSourceInfo( const CVASignalSourceInfo& ssi ) {
	/*
	std::string	sID;			// ID (z.B. "af1")
	std::string	sType;			// Typ (Datei|Sampler|Gerte-Eingang|Netzwerkstream)
	std::string sName;			// Name (z.B. "Trompete 1")
	std::string sDesc;			// Beschreibung
	std::string sState;			// Zustandsbeschreibung
	int iReferences;			// Anzahl Referenzen
	*/

	const mwSize nFields = 6;
	const char* ppszFieldNames[] = { "id", "type", "name", "description", "state", "references" };

	mxArray* pStruct = mxCreateStructMatrix( 1, 1, nFields, ppszFieldNames );
	mxSetField( pStruct, 0, ppszFieldNames[ 0 ], mxCreateString( ssi.sID.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 1 ], mxCreateString( ssi.sType.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 2 ], mxCreateString( ssi.sName.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 3 ], mxCreateString( ssi.sDesc.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 4 ], mxCreateString( ssi.sState.c_str() ) );
	mxSetField( pStruct, 0, ppszFieldNames[ 5 ], mxCreateDoubleScalar( ssi.iReferences ) ); // no of references as double-value in matlab

	return pStruct;
}

mxArray* matlabCreateSceneInfo( const CVASceneInfo& sci )
{

	// TODO: Implementieren
	/*
	//std::string sName;		// Name (optional)
	//std::string sFilename;	// Scene filename (e.g. RAVEN project file)
	// TODO: More fields, num polygons, num portals, num rooms, etc.
	*/

	const mwSize nFields = 2;
	const char* ppszFieldNames[] = { "name", "filename" };

	mxArray* pStruct = mxCreateStructMatrix( 1, 1, nFields, ppszFieldNames );
	//mxSetField(pStruct, 0, ppszFieldNames[0], mxCreateString(sci.sName.c_str()));
	//mxSetField(pStruct, 0, ppszFieldNames[1], mxCreateString(sci.sFilename.c_str()));

	return pStruct;
}

mxArray* matlabCreateStruct( const CVAStruct& oStruct )
{
	std::vector< const char* > vcpFieldNames;
	CVAStruct::const_iterator it = oStruct.Begin();
	while( it != oStruct.End() )
	{
		const std::string sKey( ( *it ).first );
		char* cFieldName = _strdup( sKey.c_str() );
		vcpFieldNames.push_back( cFieldName );
		it++;
	}

	const size_t nDims = 1;
	const int nFields = int( vcpFieldNames.size() );
	const char** ppszFieldNames = &( *vcpFieldNames.begin() );
	mxArray* pStruct = mxCreateStructArray( 1, &nDims, nFields, ppszFieldNames );

	for( size_t i = 0; i < nFields; i++ )
	{
		const std::string sFieldName( vcpFieldNames[ i ] );
		assert( oStruct.HasKey( sFieldName ) );

		const CVAStructValue* pStructValue( oStruct.GetValue( sFieldName ) );

		mxArray* p;
		int iType = pStructValue->GetDatatype();
		switch( iType )
		{
		case CVAStructValue::BOOL:
		{
			mxSetField( pStruct, 0, sFieldName.c_str(), mxCreateLogicalScalar( *pStructValue ) );
			break;
		}
		case CVAStructValue::INT:
		{
			size_t iVal = 1;
			p = mxCreateNumericArray( 1, &iVal, mxINT32_CLASS, mxREAL );
			*( ( int* ) mxGetData( p ) ) = *pStructValue;
			mxSetField( pStruct, 0, sFieldName.c_str(), p );
			break;
		}
		case CVAStructValue::DOUBLE:
		{
			mxSetField( pStruct, 0, sFieldName.c_str(), mxCreateDoubleScalar( *pStructValue ) );
			break;
		}
		case CVAStructValue::STRING:
		{
			std::string s = *pStructValue;
			mxSetField( pStruct, 0, sFieldName.c_str(), mxCreateString( s.c_str() ) );
			break;
		}
		case CVAStructValue::STRUCT:
		{
			mxSetField( pStruct, 0, sFieldName.c_str(), matlabCreateStruct( *pStructValue ) );
			break;
		}
		case CVAStructValue::DATA:
		{
			int nSamples = int( pStructValue->GetDataSize() / 4 );
			size_t nDims[ 2 ] = { 1, nSamples };
			p = mxCreateNumericArray( 2, nDims, mxSINGLE_CLASS, mxREAL );
			mxSetField( pStruct, 0, sFieldName.c_str(), p );
			const float* pfSrcData = ( const float* ) ( pStructValue->GetData() );
			float* pfDestData = ( float* ) mxGetPr( p );
			for( size_t n = 0; n < nSamples; n++ )
				pfDestData[ n ] = pfSrcData[ n ];

			break;
		}
		default:
		{
			VA_EXCEPT2( INVALID_PARAMETER, "Could not read VAStruct value at key '" + sFieldName + "'" );
		}
		}

		delete vcpFieldNames[ i ];
	}

	return pStruct;
}

CVAStruct matlabGetStruct( const mxArray* pa, const char* argname )
{
	if( !mxIsStruct( pa ) )
		VA_EXCEPT2( INVALID_PARAMETER, "Argument is not a Matlab struct: " + std::string( argname ) );

	CVAStruct oRet;

	size_t nStructElements = mxGetNumberOfElements( pa );
	if( nStructElements != 1 )
		VA_EXCEPT2( INVALID_PARAMETER, "Only a single struct can be passed as argument" );

	int nNumFields = mxGetNumberOfFields( pa );
	for( size_t i = 0; i < nNumFields; i++ )
	{
		std::string sFieldName( mxGetFieldNameByNumber( pa, int( i ) ) );
		mxArray* pVal = mxGetFieldByNumber( pa, 0, int( i ) );

		if( pVal == nullptr )
			VA_EXCEPT2( INVALID_PARAMETER, "Could not resolve value of struct field '" + sFieldName + "'" );

		if( mxIsStruct( pVal ) )
		{
			oRet[ sFieldName ] = matlabGetStruct( pVal, sFieldName.c_str() );
			continue;
		}

		std::string sClassName( mxGetClassName( pVal ) );

		if( sClassName == "itaAudio" )
		{
			mxArray* pITAAudio( pVal );

			if( mxGetNumberOfElements( pITAAudio ) != 1 )
				VA_EXCEPT2( INVALID_PARAMETER, "only single itaAudio instances allowed" );

			mxArray* pITAAudioDomain = mxGetProperty( pITAAudio, 0, "domain" );
			if( pITAAudioDomain == nullptr )
				continue;

			std::string sDomain = matlabGetString( pITAAudioDomain, "domain" );
			if( sDomain != "time" )
				VA_EXCEPT2( INVALID_PARAMETER, "itaAudio objects has to be in time domain. In Matlab, do myitaaudio.' to convert from frequency domain to time domain." );

			mxArray* pITAAudioTimeData = mxGetProperty( pITAAudio, 0, "timeData" );
			if( pITAAudioTimeData == nullptr )
				VA_EXCEPT2( INVALID_PARAMETER, "itaAudio objects has no timeData." );

			size_t nChannels = size_t( mxGetN( pITAAudioTimeData ) );
			size_t nSamples = size_t( mxGetM( pITAAudioTimeData ) );
			size_t iOffset = 0;
			for( size_t n = 0; n < nChannels; n++ )
			{
				std::stringstream ssChannelName;
				ssChannelName << "IRDataCh" << n + 1; // Naming convention for IR data to a Renderer
				std::string sChannelName( ssChannelName.str() );

				mxArray* pChannelData = pITAAudioTimeData; // use offsset to access data

				iOffset += n * nSamples;

				std::vector< float > vfData( nSamples );
				if( mxIsSingle( pChannelData ) )
					for( size_t i = 0; i < nSamples; i++ )
						vfData[ i ] = ( ( float* ) mxGetData( pChannelData ) )[ i + iOffset ];
				if( mxIsDouble( pChannelData ) )
					for( size_t i = 0; i < nSamples; i++ )
						vfData[ i ] = float( ( ( double* ) mxGetData( pChannelData ) )[ i + iOffset ] );
				else
					VA_EXCEPT1( "Unsupported array data format, could not create VAStruct from this struct" );

				assert( !oRet.HasKey( sChannelName ) );
				oRet[ sChannelName ] = CVAStructValue( &vfData[ 0 ], int( sizeof( float ) * nSamples ) );
			}

			continue; // next field
		}

		if( mxIsNumeric( pVal ) ) // int or double
		{
			if( mxIsComplex( pVal ) )
				VA_EXCEPT2( INVALID_PARAMETER, "Complex values are not supported in VAStruct" );

			int iSize;
			if( matlabIsVector( pVal, iSize ) )
			{
				if( iSize <= 0 )
					VA_EXCEPT1( "Invalid size of matlab vector" );

				std::vector< float > vfData( iSize );
				if( mxIsSingle( pVal ) )
					for( int i = 0; i < iSize; i++ )
						vfData[ i ] = ( ( float* ) mxGetData( pVal ) )[ i ];
				if( mxIsDouble( pVal ) )
					for( int i = 0; i < iSize; i++ )
						vfData[ i ] = float( ( ( double* ) mxGetData( pVal ) )[ i ] );
				else
					VA_EXCEPT1( "Unsupported array data format, could not create VAStruct from this struct" );

				oRet[ sFieldName ] = CVAStructValue( &vfData[ 0 ], sizeof( float ) * iSize );
			}
			else if( mxIsInt16( pVal ) || mxIsInt32( pVal ) || mxIsInt64( pVal ) )
			{
				oRet[ sFieldName ] = int( matlabGetIntegerScalar( pVal, sFieldName.c_str() ) );
			}
			else
			{
				oRet[ sFieldName ] = double( matlabGetRealScalar( pVal, sFieldName.c_str() ) );
			}
		}
		else if( mxIsLogicalScalar( pVal ) )
		{
			mxLogical* p = mxGetLogicals( pVal );
			oRet[ sFieldName ] = bool( *p );
		}
		else if( mxIsChar( pVal ) )
		{
			oRet[ sFieldName ] = std::string( matlabGetString( pVal, sFieldName.c_str() ) );
		}
		else
		{
			VA_EXCEPT2( INVALID_PARAMETER, "Unkown type in field '" + sFieldName + "'" );
		}
	}

	return oRet;
}