25 #include "../include/DSPatch.h"
27 #include "DspCircuit.h"
28 #include "DspComponent.h"
29 #include "DspComponentThread.h"
34 DspComponent::DspComponent()
35 : _parentCircuit( NULL ),
38 _isAutoTickRunning( false ),
39 _isAutoTickPaused( false ),
42 _componentThread.Initialise(
this );
47 DspComponent::~DspComponent()
49 if( _parentCircuit != NULL )
51 _parentCircuit->RemoveComponent(
this );
61 void DspComponent::SetComponentName( std::string componentName )
63 _componentName = componentName;
68 std::string DspComponent::GetComponentName()
const
70 return _componentName;
75 void DspComponent::SetParentCircuit(
DspCircuit* parentCircuit )
77 if( _parentCircuit != parentCircuit && parentCircuit !=
this )
80 _parentCircuit = NULL;
83 if( currentParent != NULL )
85 currentParent->RemoveComponent(
this );
88 _parentCircuit = parentCircuit;
98 return _parentCircuit;
103 void DspComponent::DisconnectInput(
unsigned short inputIndex )
108 for(
unsigned short i = 0; i < _inputWires.GetWireCount(); i++ )
110 DspWire* wire = _inputWires.GetWire( i );
111 if( wire->toSignalIndex == inputIndex )
113 _inputWires.RemoveWire( i );
123 void DspComponent::DisconnectInput( std::string inputName )
125 unsigned short inputIndex;
127 if( FindInput( inputName, inputIndex ) )
129 DisconnectInput( inputIndex );
135 void DspComponent::DisconnectInput(
DspComponent* inputComponent )
140 for(
unsigned short i = 0; i < _inputWires.GetWireCount(); i++ )
142 DspWire* wire = _inputWires.GetWire( i );
143 if( wire->linkedComponent == inputComponent )
145 _inputWires.RemoveWire( i );
154 void DspComponent::DisconnectInputs()
157 _inputWires.RemoveAllWires();
163 unsigned short DspComponent::GetInputCount()
const
165 return _inputBus.GetSignalCount();
170 unsigned short DspComponent::GetOutputCount()
const
172 return _outputBus.GetSignalCount();
177 bool DspComponent::FindInput( std::string signalName,
unsigned short& returnIndex )
const
179 return _inputBus.FindSignal( signalName, returnIndex );
184 bool DspComponent::FindInput(
unsigned short signalIndex,
unsigned short& returnIndex )
const
186 if( signalIndex < _inputBus.GetSignalCount() )
188 returnIndex = signalIndex;
197 bool DspComponent::FindOutput( std::string signalName,
unsigned short& returnIndex )
const
199 return _outputBus.FindSignal( signalName, returnIndex );
204 bool DspComponent::FindOutput(
unsigned short signalIndex,
unsigned short& returnIndex )
const
206 if( signalIndex < _outputBus.GetSignalCount() )
208 returnIndex = signalIndex;
217 void DspComponent::Tick()
226 for(
unsigned short i = 0; i < _inputWires.GetWireCount(); i++ )
228 DspWire* wire = _inputWires.GetWire( i );
229 wire->linkedComponent->Tick();
231 DspSignal* signal = wire->linkedComponent->_outputBus.GetSignal( wire->fromSignalIndex );
232 _inputBus.SetSignal( wire->toSignalIndex, signal );
236 _outputBus.ClearAllValues();
239 Process_( _inputBus, _outputBus );
245 void DspComponent::Reset()
248 _inputBus.ClearAllValues();
256 void DspComponent::StartAutoTick()
263 if( DSPatch::IsThisGlobalCircuit(
this ) )
265 if( _componentThread.IsStopped() )
267 _componentThread.Start();
269 _isAutoTickRunning =
true;
270 _isAutoTickPaused =
false;
278 else if( _parentCircuit == NULL || DSPatch::IsThisGlobalCircuit( _parentCircuit ) )
280 DSPatch::AddGlobalComponent(
this );
281 DSPatch::StartGlobalAutoTick();
287 void DspComponent::StopAutoTick()
294 if( DSPatch::IsThisGlobalCircuit(
this ) && !_componentThread.IsStopped() )
296 _componentThread.Stop();
298 _isAutoTickRunning =
false;
299 _isAutoTickPaused =
false;
302 else if( DSPatch::IsThisGlobalCircuit( _parentCircuit ) )
304 DSPatch::RemoveGlobalComponent(
this );
306 if( DSPatch::GetGlobalComponentCount() == 0 )
308 DSPatch::StopGlobalAutoTick();
315 void DspComponent::PauseAutoTick()
321 if( DSPatch::IsThisGlobalCircuit(
this ) && !_componentThread.IsStopped() )
323 if( _isAutoTickRunning )
325 _componentThread.Pause();
326 _isAutoTickPaused =
true;
327 _isAutoTickRunning =
false;
330 else if( _parentCircuit != NULL )
332 _parentCircuit->PauseAutoTick();
338 void DspComponent::ResumeAutoTick()
344 if( DSPatch::IsThisGlobalCircuit(
this ) && _isAutoTickPaused )
346 _componentThread.Resume();
347 _isAutoTickPaused =
false;
348 _isAutoTickRunning =
true;
350 else if( _parentCircuit != NULL )
352 _parentCircuit->ResumeAutoTick();
358 void DspComponent::SetBufferCount(
unsigned short bufferCount )
363 for(
long i = _bufferCount - 1; i >= ( long ) bufferCount; i-- )
365 delete _hasTickeds[i];
369 _hasTickeds.resize( bufferCount );
372 for(
unsigned short i = _bufferCount; i < bufferCount; i++ )
374 _hasTickeds[i] =
new bool();
377 _inputBuses.resize( bufferCount );
378 _outputBuses.resize( bufferCount );
380 _gotReleases.resize( bufferCount );
381 _releaseMutexes.resize( bufferCount );
382 _releaseCondts.resize( bufferCount );
384 for(
unsigned short i = _bufferCount; i < bufferCount; i++ )
386 *_hasTickeds[i] =
false;
387 _gotReleases[i] =
false;
389 for(
unsigned short j = 0; j < _inputBus.GetSignalCount(); j++ )
391 _inputBuses[i].AddSignal( _inputBus.GetSignal( j )->GetSignalName() );
394 for(
unsigned short j = 0; j < _outputBus.GetSignalCount(); j++ )
396 _outputBuses[i].AddSignal( _outputBus.GetSignal( j )->GetSignalName() );
400 if( bufferCount > 0 )
402 _gotReleases[0] =
true;
405 _bufferCount = bufferCount;
410 unsigned short DspComponent::GetBufferCount()
const
417 void DspComponent::ThreadTick(
unsigned short threadNo )
420 if( *_hasTickeds[threadNo] ==
false )
423 *_hasTickeds[threadNo] =
true;
426 for(
unsigned short i = 0; i < _inputWires.GetWireCount(); i++ )
428 DspWire* wire = _inputWires.GetWire( i );
429 wire->linkedComponent->ThreadTick( threadNo );
431 DspSignal* signal = wire->linkedComponent->_outputBuses[threadNo].GetSignal( wire->fromSignalIndex );
432 _inputBuses[threadNo].SetSignal( wire->toSignalIndex, signal );
436 _outputBuses[threadNo].ClearAllValues();
439 _WaitForRelease( threadNo );
442 Process_( _inputBuses[threadNo], _outputBuses[threadNo] );
445 _ReleaseThread( threadNo );
451 void DspComponent::ThreadReset(
unsigned short threadNo )
454 _inputBuses[threadNo].ClearAllValues();
457 *_hasTickeds[threadNo] =
false;
462 bool DspComponent::SetInputSignal(
unsigned short inputIndex,
const DspSignal* newSignal )
464 return _inputBus.SetSignal( inputIndex, newSignal );
469 bool DspComponent::SetInputSignal(
unsigned short inputIndex,
unsigned short threadIndex,
const DspSignal* newSignal )
471 return _inputBuses[threadIndex].SetSignal( inputIndex, newSignal );
476 DspSignal* DspComponent::GetOutputSignal(
unsigned short outputIndex )
478 return _outputBus.GetSignal( outputIndex );
483 DspSignal* DspComponent::GetOutputSignal(
unsigned short outputIndex,
unsigned short threadIndex )
485 return _outputBuses[threadIndex].GetSignal( outputIndex );
490 bool DspComponent::AddInput_( std::string inputName )
492 for(
unsigned short i = 0; i < _inputBuses.size(); i++ )
494 _inputBuses[i].AddSignal( inputName );
496 return _inputBus.AddSignal( inputName );
501 bool DspComponent::AddOutput_( std::string outputName )
503 for(
unsigned short i = 0; i < _outputBuses.size(); i++ )
505 _outputBuses[i].AddSignal( outputName );
507 return _outputBus.AddSignal( outputName );
512 void DspComponent::ClearInputs_()
514 for(
unsigned short i = 0; i < _inputBuses.size(); i++ )
516 _inputBuses[i].RemoveAllSignals();
518 return _inputBus.RemoveAllSignals();
523 void DspComponent::ClearOutputs_()
525 for(
unsigned short i = 0; i < _outputBuses.size(); i++ )
527 _outputBuses[i].RemoveAllSignals();
529 return _outputBus.RemoveAllSignals();
534 void DspComponent::_WaitForRelease(
unsigned short threadNo )
536 _releaseMutexes[threadNo].Lock();
537 if( !_gotReleases[threadNo] )
539 _releaseCondts[threadNo].Wait( _releaseMutexes[threadNo] );
541 _gotReleases[threadNo] =
false;
542 _releaseMutexes[threadNo].Unlock();
547 void DspComponent::_ReleaseThread(
unsigned short threadNo )
549 unsigned short nextThread = threadNo + 1;
551 if( nextThread >= _bufferCount )
554 _releaseMutexes[nextThread].Lock();
555 _gotReleases[nextThread] =
true;
556 _releaseCondts[nextThread].WakeAll();
557 _releaseMutexes[nextThread].Unlock();