VAMachineSignalSource.cpp 14.6 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1
2
3
4
5
6
7
8
9
10
#include "VAMachineSignalSource.h"

#include "../VAAudiostreamTracker.h"
#include "../VACoreImpl.h"
#include "../VALog.h"

#include <ITAAudiofileReader.h>
#include <ITAConstants.h>
#include <ITAException.h>
#include <ITAFade.h>
Jonas Stienen's avatar
Jonas Stienen committed
11
#include <ITAFileSystemUtils.h>
Jonas Stienen's avatar
Jonas Stienen committed
12
#include <ITASampleFrame.h>
13
#include <ITAInterpolation.h>
Jonas Stienen's avatar
Jonas Stienen committed
14
15
16
17
18
19

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

CVAMachineSignalSource::CVAMachineSignalSource( const CVAMachineSignalSource::Config& oConfig )
20
: ITADatasourceRealization( 1, oConfig.pCore->GetCoreConfig()->oAudioDriverConfig.dSampleRate, oConfig.pCore->GetCoreConfig()->oAudioDriverConfig.iBuffersize )
Jonas Stienen's avatar
Jonas Stienen committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
, 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 )
42
		m_pInterpRoutine = new CITASampleCubicSplineInterpolation;
Jonas Stienen's avatar
Jonas Stienen committed
43
	else
44
		m_pInterpRoutine = new CITASampleLinearInterpolation;
Jonas Stienen's avatar
Jonas Stienen committed
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

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

IVACore* CVAMachineSignalSource::GetAssociatedCore() const
{
	return m_oConfig.pCore;
}

const float* CVAMachineSignalSource::GetStreamBlock( const CVAAudiostreamState* pStreamInfo )
{
	const int iCurrentState = m_iCurrentState;
	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
				m_sbOut.Crossfade( sbOutTemp, 0, iSamplesWritten, ITA_FADE_IN, ITASampleBuffer::COSINE_SQUARE );
			}
			else
			{
				m_sbOut.write( sbOutTemp, iSamplesWritten );
				if( iSamplesWritten < m_sbOut.length() )
180
					m_sbOut.Zero( iSamplesWritten, m_sbOut.length() - iSamplesWritten );
Jonas Stienen's avatar
Jonas Stienen committed
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
			}

			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 )
219
			VA_WARN( "MachineSignalSource", "high energy detected" );
Jonas Stienen's avatar
Jonas Stienen committed
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
	}
	
	return m_sbOut.data();
}

void CVAMachineSignalSource::HandleRegistration( IVACore* pCore )
{
	m_pAssociatedCore = pCore;
}

void CVAMachineSignalSource::HandleUnregistration( IVACore* )
{
	m_pAssociatedCore = NULL;
}

std::string CVAMachineSignalSource::GetStateString() const
{
	return GetMachineStateString( MachineState( m_iCurrentState.get() ) );
}

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 );
288
	int N = pReader->getAudiofileProperties().iLength;
Jonas Stienen's avatar
Jonas Stienen committed
289
290
291
292
293
294
295
296
297
298
299
300
301
	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 );
302
	int N = pReader->getAudiofileProperties().iLength;
Jonas Stienen's avatar
Jonas Stienen committed
303
304
305
306
307
308
309
310
311
312
313
314
315
	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 );
316
	int N = pReader->getAudiofileProperties().iLength;
Jonas Stienen's avatar
Jonas Stienen committed
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
	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 )
				ITA_EXCEPT1( INVALID_PARAMETER, "File path has to be a string" );

			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 )
				ITA_EXCEPT1( INVALID_PARAMETER, "File path has to be a string" );

			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 )
				ITA_EXCEPT1( INVALID_PARAMETER, "File path has to be a string" );

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

	assert( sbOut.length() == GetBlocklength() );
	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;
		int iInputStartOffset = iLeft;
		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;
}