VAMachineSignalSource.cpp 15.2 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-2018
8 9 10 11 12 13
 *        VVVVVV       AAA       Institute of Technical Acoustics (ITA)
 *         VVVV         AAA      RWTH Aachen University
 *
 *  --------------------------------------------------------------------------------------------
 */

Jonas Stienen's avatar
Jonas Stienen committed
14 15 16
#include "VAMachineSignalSource.h"

#include "../VAAudiostreamTracker.h"
17
#include "../core/core.h"
Jonas Stienen's avatar
Jonas Stienen committed
18 19 20 21 22 23
#include "../VALog.h"

#include <ITAAudiofileReader.h>
#include <ITAConstants.h>
#include <ITAException.h>
#include <ITAFade.h>
Jonas Stienen's avatar
Jonas Stienen committed
24
#include <ITAFileSystemUtils.h>
Jonas Stienen's avatar
Jonas Stienen committed
25
#include <ITASampleFrame.h>
26
#include <ITAInterpolation.h>
Jonas Stienen's avatar
Jonas Stienen committed
27 28 29 30 31 32

#include <sstream>
#include <assert.h>
#include <math.h>

CVAMachineSignalSource::CVAMachineSignalSource( const CVAMachineSignalSource::Config& oConfig )
33
: ITADatasourceRealization( 1, oConfig.pCore->GetCoreConfig()->oAudioDriverConfig.dSampleRate, oConfig.pCore->GetCoreConfig()->oAudioDriverConfig.iBuffersize )
Jonas Stienen's avatar
Jonas Stienen committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
, m_oConfig( oConfig )
, m_iCurrentState( MACHINE_STATE_INVALID )
, m_iNewTransition( MACHINE_TRANSITION_NONE )
, m_fMachineSpeed( 1.0f )
, m_bHasStartSound( false )
, m_bHasIdleSound( false )
, m_bHasStopSound( false )
, m_bCrossfadeSounds( oConfig.bCrossfadeSounds )
, m_pAssociatedCore( NULL )
{
	if( !( oConfig.bRequiresStartSound && oConfig.bRequiresIdleSound && oConfig.bRequiresStopSound ) )
		VA_EXCEPT2( NOT_IMPLEMENTED, "Current implementation of machine signal source require start, idle and stop sound. Please activate in config first." );

	m_sbOut.Init( GetBlocklength(), true );

	ResetReadCursors();
	m_iCurrentState = MACHINE_STATE_INVALID;

	m_bStartSoundNew = m_bIdleSoundNew = m_bStopSoundNew = false;

	if( m_oConfig.bUseSplineInterpolation )
55
		m_pInterpRoutine = new CITASampleCubicSplineInterpolation;
Jonas Stienen's avatar
Jonas Stienen committed
56
	else
57
		m_pInterpRoutine = new CITASampleLinearInterpolation;
Jonas Stienen's avatar
Jonas Stienen committed
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

	int iLeft, iRight;
	m_pInterpRoutine->GetOverlapSamples( iLeft, iRight );
	int iMaxInterpolationBufferLength = iLeft + int( ceil( m_oConfig.dMaximumSpeed * GetBlocklength() ) ) + iRight;
	m_sbInterpolationSrc.Init( iMaxInterpolationBufferLength, true );
}

CVAMachineSignalSource::~CVAMachineSignalSource()
{
}

int CVAMachineSignalSource::GetType() const
{
	return IVAAudioSignalSource::VA_SS_MACHINE;
}

std::string CVAMachineSignalSource::GetTypeString() const
{
	return "machine";
}

std::string CVAMachineSignalSource::GetDesc() const
{
	return std::string( "Creates a machine that can be started and stopped" );
}

84
IVAInterface* CVAMachineSignalSource::GetAssociatedCore() const
Jonas Stienen's avatar
Jonas Stienen committed
85 86 87 88
{
	return m_oConfig.pCore;
}

89
const float* CVAMachineSignalSource::GetStreamBlock( const CVAAudiostreamState* )
Jonas Stienen's avatar
Jonas Stienen committed
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
{
	const int iNewTransition = m_iNewTransition;

	m_sbOut.Zero();

	// Missing sounds of machine not implemented yet
	if( !( m_oConfig.bRequiresIdleSound && m_oConfig.bRequiresIdleSound && m_oConfig.bRequiresStopSound ) )
		return m_sbOut.data();

	// Audiostream-synced data copy (todo use pointer)
	if( m_bStartSoundNew )
	{
		m_sbStartSound = m_sbStartSoundNew;
		m_bStartSoundNew = false;
	}
	if( m_bIdleSoundNew )
	{
		m_sbIdleSound = m_sbIdleSoundNew;
		m_bIdleSoundNew = false;
	}
	if( m_bStopSoundNew )
	{
		m_sbStopSound = m_sbStopSoundNew;
		m_bStopSoundNew = false;
	}

	switch( m_iCurrentState )
	{
	case MACHINE_STATE_INVALID:
		if( m_bHasStartSound || m_bHasIdleSound )
			m_iCurrentState = MACHINE_STATE_STOPPED;
		break;
	case MACHINE_STATE_STOPPED:
		if( iNewTransition == MACHINE_TRANSITION_START ) // Startup
		{
			int iSamplesWritten;
			bool bEndReached;
			AddSamplesStarting( m_sbOut, iSamplesWritten, bEndReached );

			if( bEndReached )
			{
				// Short starting sample, jump directly into idle state
				AddSamplesIdling( m_sbOut, iSamplesWritten );
				m_iCurrentState = MACHINE_STATE_IDLING;
			}
			else
			{
				m_iCurrentState = MACHINE_STATE_STARTING;
			}
		}
		break;

	case MACHINE_STATE_STARTING:
		if( iNewTransition == MACHINE_TRANSITION_HALT )
		{
			int iSamplesWritten;
			bool bEndReached;
			AddSamplesStopping( m_sbOut, iSamplesWritten, bEndReached );

			if( bEndReached )
			{
				m_iCurrentState = MACHINE_STATE_STOPPED;
				ResetReadCursors();
			}
		}
		else if( iNewTransition == MACHINE_TRANSITION_START )
		{
			int iSamplesWritten;
			bool bEndReached;
			AddSamplesStarting( m_sbOut, iSamplesWritten, bEndReached );

			if( bEndReached )
			{
				m_iCurrentState = MACHINE_STATE_IDLING;
				AddSamplesIdling( m_sbOut, iSamplesWritten );
			}
		}
		else
		{
			assert( false );
		}
		break;

	case MACHINE_STATE_IDLING:
		if( iNewTransition == MACHINE_TRANSITION_HALT )
		{
			AddSamplesIdling( m_sbOut, false );

			int iSamplesWritten;
			bool bEndReached;
			ITASampleBuffer sbOutTemp( m_sbOut );
			AddSamplesStopping( sbOutTemp, iSamplesWritten, bEndReached );
			
			if( m_bCrossfadeSounds )
			{
				// Fade in stopping sample buffer
186
				m_sbOut.Crossfade( sbOutTemp, 0, iSamplesWritten, ITABase::CrossfadeDirection::FROM_SOURCE, ITABase::FadingFunction::COSINE_SQUARE );
Jonas Stienen's avatar
Jonas Stienen committed
187 188 189 190 191
			}
			else
			{
				m_sbOut.write( sbOutTemp, iSamplesWritten );
				if( iSamplesWritten < m_sbOut.length() )
192
					m_sbOut.Zero( iSamplesWritten, m_sbOut.length() - iSamplesWritten );
Jonas Stienen's avatar
Jonas Stienen committed
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
			}

			if( bEndReached )
			{
				m_iCurrentState = MACHINE_STATE_STOPPED;
				ResetReadCursors();
			}
			else
			{
				m_iCurrentState = MACHINE_STATE_HALTING;
			}
		}
		else
		{
			// Idling ...
			AddSamplesIdling( m_sbOut, false );
		}
		break;

	case MACHINE_STATE_HALTING:
		int iSamplesWritten;
		bool bEndReached;
		AddSamplesStopping( m_sbOut, iSamplesWritten, bEndReached );
		if( bEndReached )
		{
			m_iCurrentState = MACHINE_STATE_STOPPED;
			ResetReadCursors();
		}
		break;

	default:
		m_sbOut.Zero();
	
	}

	for( int i=0; i<m_sbOut.length(); i++ )
	{
		if( m_sbOut[i] > 1.0f || m_sbOut[i] < -1.0f )
231
			VA_WARN( "MachineSignalSource", "high energy detected" );
Jonas Stienen's avatar
Jonas Stienen committed
232 233 234 235 236
	}
	
	return m_sbOut.data();
}

237
void CVAMachineSignalSource::HandleRegistration( IVAInterface* pCore )
Jonas Stienen's avatar
Jonas Stienen committed
238 239 240 241
{
	m_pAssociatedCore = pCore;
}

242
void CVAMachineSignalSource::HandleUnregistration( IVAInterface* )
Jonas Stienen's avatar
Jonas Stienen committed
243 244 245 246 247 248
{
	m_pAssociatedCore = NULL;
}

std::string CVAMachineSignalSource::GetStateString() const
{
249 250
	MachineState iState = (MachineState) (int) m_iCurrentState;
	return GetMachineStateString( iState );
Jonas Stienen's avatar
Jonas Stienen committed
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
}

std::string CVAMachineSignalSource::GetMachineStateString( MachineState iState ) const
{
	switch( iState )
	{
	case MACHINE_STATE_IDLING:
		return "idling";
	case MACHINE_STATE_INVALID:
		return "invalid";
	case MACHINE_STATE_STARTING:
		return "starting";
	case MACHINE_STATE_HALTING:
		return "halting";
	case MACHINE_STATE_STOPPED:
		return "stopped";
	default:
		return "unkown";
	}
}

void CVAMachineSignalSource::Reset()
{
	m_iCurrentState = MACHINE_STATE_INVALID;
	m_iNewTransition = MACHINE_TRANSITION_NONE;
	ResetReadCursors();
	m_bStartSoundNew = m_bIdleSoundNew = m_bStopSoundNew = false;
}

void CVAMachineSignalSource::SetSpeed( double dSpeed )
{
	if( dSpeed <= 0 )
		VA_EXCEPT2( INVALID_PARAMETER, "Machine speed must be positive" );
	if( m_oConfig.bDisableInterpolation == true )
		VA_EXCEPT2( MODAL_ERROR, "Machine speed can not be set beause interpolation is deactivated" );
	m_fMachineSpeed = float( dSpeed );
}

double CVAMachineSignalSource::GetSpeed() const
{
	return double( m_fMachineSpeed );
}

void CVAMachineSignalSource::SetStartSound( const std::string& sFilePath )
{
	std::string sDestFilePath = m_pAssociatedCore->FindFilePath( sFilePath );
	if( sDestFilePath.empty() )
		VA_EXCEPT2( INVALID_PARAMETER, "Looked everywhere, but could not find file '" + sFilePath + "'" );

	ITAAudiofileReader* pReader = ITAAudiofileReader::create( sDestFilePath );
301
	int N = pReader->getAudiofileProperties().iLength;
Jonas Stienen's avatar
Jonas Stienen committed
302 303 304 305 306 307 308 309 310 311 312 313 314
	m_sbStartSoundNew.Init( N, false );
	m_sbStartSoundNew.write( pReader->read( N )[0], N );
	m_bHasStartSound = true;
	m_bStartSoundNew = true;
}

void CVAMachineSignalSource::SetIdleSound( const std::string& sFilePath )
{
	std::string sDestFilePath = m_pAssociatedCore->FindFilePath( sFilePath );
	if( sDestFilePath.empty() )
		VA_EXCEPT2( INVALID_PARAMETER, "Looked everywhere, but could not find file '" + sFilePath + "'" );

	ITAAudiofileReader* pReader = ITAAudiofileReader::create( sDestFilePath );
315
	int N = pReader->getAudiofileProperties().iLength;
Jonas Stienen's avatar
Jonas Stienen committed
316 317 318 319 320 321 322 323 324 325 326 327 328
	m_sbIdleSoundNew.Init( N, false );
	m_sbIdleSoundNew.write( pReader->read( N )[0], N );
	m_bHasIdleSound = true;
	m_bIdleSoundNew = true;
}

void CVAMachineSignalSource::SetStopSound( const std::string& sFilePath )
{
	std::string sDestFilePath = m_pAssociatedCore->FindFilePath( sFilePath );
	if( sDestFilePath.empty() )
		VA_EXCEPT2( INVALID_PARAMETER, "Looked everywhere, but could not find file '" + sFilePath + "'" );

	ITAAudiofileReader* pReader = ITAAudiofileReader::create( sDestFilePath );
329
	int N = pReader->getAudiofileProperties().iLength;
Jonas Stienen's avatar
Jonas Stienen committed
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
	m_sbStopSoundNew.Init( N, false );
	m_sbStopSoundNew.write( pReader->read( N )[0], N );
	m_bHasStopSound = true;
	m_bStopSoundNew = true;
}

CVAStruct CVAMachineSignalSource::GetParameters( const CVAStruct& oArgs ) const
{
	CVAStruct oRet;
	if( oArgs.HasKey( "GET" ) )
	{
		if( toUppercase( oArgs["GET"] ) == "STATE" )
			oRet["STATE"] = GetStateString();
		else if( toUppercase( oArgs["GET"] ) == "SPEED" )
			oRet["SPEED"] = GetSpeed();
		else
			VA_EXCEPT2( INVALID_PARAMETER,std::string( "Unkown command: '" + oArgs["GET"].ToString() + "'" ) );
	}
	return oRet;
}

void CVAMachineSignalSource::SetParameters( const CVAStruct& oParams )
{
	const CVAStructValue* pStruct;

	if( ( pStruct = oParams.GetValue( "PRINT" ) ) != nullptr )
	{
		if( pStruct->GetDatatype() != CVAStructValue::STRING )
			VA_EXCEPT2(INVALID_PARAMETER, "Print value must be a string");
		std::string sValue = toUppercase( *pStruct );
		if( sValue == "HELP" )
		{
			VA_PRINT( "Available commands: 'print' 'help|status'; 'get' 'S'; 'set' 'S' 'value' <double>" );
			return;
		}

		if( sValue == "STATUS" )
		{
			VA_PRINT( "Machine speed: " << m_fMachineSpeed );
			return;
		}
	}
	else if( ( pStruct = oParams.GetValue( "SET" ) ) != nullptr )
	{
		if( pStruct->GetDatatype() != CVAStructValue::STRING )
			VA_EXCEPT2( INVALID_PARAMETER, "Setter key name must be a string");

		std::string sValue = toUppercase( *pStruct );
		if( sValue == "S" || sValue == "SPEED" )
		{
			pStruct = oParams.GetValue( "VALUE" );
			if( ( pStruct->GetDatatype() == CVAStructValue::DOUBLE ) ||
				( pStruct->GetDatatype() == CVAStructValue::INT ) ||
				( pStruct->GetDatatype() == CVAStructValue::BOOL ) )
			{
				double dS = *pStruct;
				if( dS > 0 )
					SetSpeed( dS );
				else
					VA_EXCEPT2( INVALID_PARAMETER, "Machine speed must be greater than zero" );
			}
			return;
		}
		else if( sValue == "ACTION" )
		{
			std::string sItem = toUppercase( *oParams.GetValue( "VALUE" ) );
			if( sItem == "START" )
				Start();
			else if( sItem == "STOP" )
				Halt();
			else
				VA_EXCEPT2( INVALID_PARAMETER, "Could not read action value" );

		}
		else if( sValue == "STARTSOUNDFILEPATH" )
		{
			pStruct = oParams.GetValue( "VALUE" );
			if( pStruct->GetDatatype() != CVAStructValue::STRING )
408
				VA_EXCEPT2( INVALID_PARAMETER, "File path has to be a string" );
Jonas Stienen's avatar
Jonas Stienen committed
409 410 411 412 413 414 415 416 417

			const std::string& sPathRaw = *pStruct;
			std::string sPath = m_oConfig.pCore->SubstituteMacros( sPathRaw );
			SetStartSound( sPath );
		}
		else if( sValue == "IDLESOUNDFILEPATH" )
		{
			pStruct = oParams.GetValue( "VALUE" );
			if( pStruct->GetDatatype() != CVAStructValue::STRING )
418
				VA_EXCEPT2( INVALID_PARAMETER, "File path has to be a string" );
Jonas Stienen's avatar
Jonas Stienen committed
419 420 421 422 423 424 425 426 427

			const std::string& sPathRaw = *pStruct;
			std::string sPath = m_oConfig.pCore->SubstituteMacros( sPathRaw );
			SetIdleSound( sPath );
		}
		else if( sValue == "STOPSOUNDFILEPATH" )
		{
			pStruct = oParams.GetValue( "VALUE" );
			if( pStruct->GetDatatype() != CVAStructValue::STRING )
428
				VA_EXCEPT2( INVALID_PARAMETER, "File path has to be a string" );
Jonas Stienen's avatar
Jonas Stienen committed
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

			const std::string& sPathRaw = *pStruct;
			std::string sPath = m_oConfig.pCore->SubstituteMacros( sPathRaw );
			SetStopSound( sPath );
		}
		return;
	}
	else
	{
		VA_EXCEPT2( INVALID_PARAMETER, "Could not interpret parameters" );
	}

	return;
}

void CVAMachineSignalSource::SetCrossfadeSoundsEnabled( bool bEnabled )
{
	m_bCrossfadeSounds = bEnabled;
}

bool CVAMachineSignalSource::GetCrossfadeSoundsEnabled() const
{
	return m_bCrossfadeSounds;
}

void CVAMachineSignalSource::AddSamplesStarting( ITASampleBuffer& sfOut, int& iSamplesWritten, bool& bEndReached )
{
	if( !m_bHasStartSound )
	{
		iSamplesWritten = 0;
		bEndReached = true;
		return;
	}

	int iSamples = m_sbStartSound.length() - m_iStartingSoundCursor;
	if( iSamples > 0 )
	{
		iSamples = (std::min)( iSamples, sfOut.length() );
		sfOut.write( m_sbStartSound, iSamples, m_iStartingSoundCursor );
		iSamplesWritten = iSamples;
		
		m_iStartingSoundCursor += iSamples;
		if( m_iStartingSoundCursor >= m_sbStartSound.length() )
			bEndReached = true;
		else
			bEndReached = false;
	}
	else
	{
		iSamplesWritten = 0;
		bEndReached = true;
		m_iStartingSoundCursor = 0;
	}
}

void CVAMachineSignalSource::AddSamplesIdling( ITASampleBuffer& sbOut, int iOutputOffset/*=0 */ )
{
	if( !m_bHasIdleSound )
	{
		return;
	}

491
	assert( sbOut.length() == int( GetBlocklength() ) );
Jonas Stienen's avatar
Jonas Stienen committed
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
	assert( iOutputOffset >= 0 && iOutputOffset < sbOut.length() );
	assert( iOutputOffset < sbOut.length() );
	
	int iSize;
	if( m_oConfig.bDisableInterpolation == true )
	{
		iSize = GetBlocklength() - iOutputOffset;
		m_sbIdleSound.cyclic_read( sbOut.data(), iSize, m_iIdlingSoundCursor );
	}
	else
	{
		int iLeft, iRight;
		m_pInterpRoutine->GetOverlapSamples( iLeft, iRight );
		iSize = int( m_fMachineSpeed * GetBlocklength() ) - iOutputOffset;
		assert( iLeft + iSize + iRight <= m_sbInterpolationSrc.length() );
		int iSrcOffset = m_iIdlingSoundCursor - iLeft;
		if( iSrcOffset < 0 )
			iSrcOffset += m_sbIdleSound.length();
		m_sbIdleSound.cyclic_read( m_sbInterpolationSrc.data(), iLeft + iSize + iRight, iSrcOffset ); // copy

		int iInputLength = iLeft+iSize;
		m_pInterpRoutine->Interpolate( &m_sbInterpolationSrc, iInputLength, iLeft, &sbOut, sbOut.length() - iOutputOffset, iOutputOffset );
	}

	m_iIdlingSoundCursor += iSize;
	if( m_iIdlingSoundCursor >= m_sbIdleSound.length() )
		m_iIdlingSoundCursor = m_iIdlingSoundCursor % m_sbIdleSound.length();
}

void CVAMachineSignalSource::AddSamplesStopping( ITASampleBuffer& sfOut, int& iSamplesWritten, bool& bEndReached )
{
	if( !m_bHasStopSound )
	{
		iSamplesWritten = 0;
		bEndReached = true;
		return;
	}

	int iResidualSamples = m_sbStopSound.length() - m_iStoppingSoundCursor;
	if( iResidualSamples > 0 )
	{
		iResidualSamples = (std::min)( iResidualSamples, sfOut.length() );
		sfOut.write( m_sbStopSound, iResidualSamples, m_iStoppingSoundCursor );
		iSamplesWritten = iResidualSamples;

		m_iStoppingSoundCursor += iResidualSamples;
		if( m_iStoppingSoundCursor >= m_sbStopSound.length() )
			bEndReached = true;
		else
			bEndReached = false;
	}
	else
	{
		iSamplesWritten = 0;
		bEndReached = true;
		m_iStoppingSoundCursor = 0;
	}
}

void CVAMachineSignalSource::ResetReadCursors()
{
	m_iStartingSoundCursor = 0;
	m_iIdlingSoundCursor = 0;
	m_iStoppingSoundCursor = 0;
}

void CVAMachineSignalSource::Start()
{
	m_iNewTransition = MACHINE_TRANSITION_START;
}

void CVAMachineSignalSource::Halt()
{
	m_iNewTransition = MACHINE_TRANSITION_HALT;
}