CLAM-Development
1.1
|
00001 #ifndef __PortMonitor_hxx__ 00002 #define __PortMonitor_hxx__ 00003 00004 #include "Processing.hxx" 00005 #include "ProcessingConfig.hxx" 00006 #include "Mutex.hxx" 00007 00008 // Temporary until concrete classes will be separated 00009 #include "SpectralPeakArray.hxx" 00010 #include "Spectrum.hxx" 00011 #include "Fundamental.hxx" 00012 #include "InPort.hxx" 00013 #include "Audio.hxx" 00014 #include "AudioInPort.hxx" 00015 00016 // sigslot 00017 #include "Signalv0.hxx" 00018 #include "Slotv0.hxx" 00019 00020 namespace CLAM 00021 { 00052 template <typename TheDataType, typename ThePortType=InPort<TheDataType> > 00053 class PortMonitor : public Processing 00054 { 00055 public: 00056 typedef TheDataType DataType; 00057 typedef ThePortType PortType; 00058 00059 inline PortMonitor(const Config & cfg= Config() ); 00060 inline virtual ~PortMonitor(); 00061 00062 inline bool Do(); 00063 00064 const char * GetClassName() const {return "PortMonitor";} 00065 00066 inline const DataType & FreezeAndGetData(); 00067 inline void UnfreezeData(); 00068 00069 void AttachStartSlot(SigSlot::Slotv0& slot) {mSigStart.Connect(slot);} 00070 void AttachStopSlot(SigSlot::Slotv0& slot) { mSigStop.Connect(slot);} 00071 void AttachSlotNewData(SigSlot::Slotv0& slot) { mSigNewData.Connect(slot);} 00072 00073 protected: 00074 bool ConcreteStart() { mSigStart.Emit(); return true;} 00075 bool ConcreteStop() { mSigStop.Emit(); return true;} 00076 00077 00078 private: 00079 PortType mInput; 00080 DataType mData[2]; 00081 TryMutex mSwitchMutex; 00082 unsigned mWhichDataToRead; 00083 SigSlot::Signalv0 mSigStart; 00084 SigSlot::Signalv0 mSigStop; 00085 SigSlot::Signalv0 mSigNewData; 00086 }; 00087 00088 namespace Hidden 00089 { 00090 template <typename T> 00091 static void initData(DynamicType * selector, T & data) 00092 { 00093 data.AddAll(); 00094 data.UpdateData(); 00095 } 00096 template <typename T> 00097 static void initData(void * selector, T & data) 00098 { 00099 } 00100 } 00101 00102 template <typename PortDataType, typename PortType> 00103 PortMonitor<PortDataType,PortType>::PortMonitor(const Config& cfg) 00104 : mInput("Input", this) 00105 , mWhichDataToRead(0) 00106 { 00107 Configure(cfg); 00108 Hidden::initData(&mData[0],mData[0]); 00109 Hidden::initData(&mData[1],mData[1]); 00110 } 00111 00112 template <typename PortDataType, typename PortType> 00113 PortMonitor<PortDataType,PortType>::~PortMonitor() 00114 { 00115 } 00116 00117 template <typename PortDataType, typename PortType> 00118 const typename PortMonitor<PortDataType,PortType>::DataType & PortMonitor<PortDataType,PortType>::FreezeAndGetData() 00119 { 00120 Hidden::LockOps<TryMutex>::Lock(mSwitchMutex); 00121 return mData[mWhichDataToRead]; 00122 } 00123 00124 template <typename PortDataType, typename PortType> 00125 void PortMonitor<PortDataType,PortType>::UnfreezeData() 00126 { 00127 Hidden::LockOps<TryMutex>::Unlock(mSwitchMutex); 00128 } 00129 00130 template <typename PortDataType, typename PortType> 00131 bool PortMonitor<PortDataType,PortType>::Do() 00132 { 00133 if(!AbleToExecute()) return true; 00134 unsigned whichDataToWrite = mWhichDataToRead?0:1; 00135 mData[whichDataToWrite] = mInput.GetData(); 00136 mSigNewData.Emit(); 00137 { 00138 TryMutex::ScopedTryLock lock(mSwitchMutex,true); 00139 if (lock.Locked()) 00140 mWhichDataToRead = whichDataToWrite; 00141 } 00142 mInput.Consume(); 00143 return true; 00144 } 00145 00146 00147 00148 class PeaksPortMonitor : public PortMonitor <SpectralPeakArray> 00149 { 00150 public: 00151 const char * GetClassName() const {return "PeaksPortMonitor";} 00152 }; 00153 class SinTracksPortMonitor : public PortMonitor<SpectralPeakArray> 00154 { 00155 public: 00156 const char * GetClassName() const {return "SinTracksPortMonitor";} 00157 }; 00158 class SpectrumPortMonitor : public PortMonitor <Spectrum> 00159 { 00160 public: 00161 const char * GetClassName() const {return "SpectrumPortMonitor";} 00162 }; 00163 class SpecgramPortMonitor : public PortMonitor<Spectrum> 00164 { 00165 public: 00166 const char * GetClassName() const {return "SpecgramPortMonitor";} 00167 }; 00168 class FundamentalPortMonitor : public PortMonitor <Fundamental> 00169 { 00170 public: 00171 const char * GetClassName() const {return "FundamentalPortMonitor";} 00172 }; 00173 class FundTrackPortMonitor : public PortMonitor<Fundamental> 00174 { 00175 public: 00176 const char * GetClassName() const {return "FundTrackPortMonitor";} 00177 }; 00178 00179 template <> 00180 bool PortMonitor<Audio,AudioInPort>::Do(); 00181 00182 template <> 00183 PortMonitor<Audio,AudioInPort>::PortMonitor(const Config& cfg); 00184 00185 class AudioPortMonitor : public PortMonitor <Audio,AudioInPort> 00186 { 00187 public: 00188 const char * GetClassName() const {return "AudioPortMonitor";} 00189 }; 00190 class AudioBuffPortMonitor : public PortMonitor<Audio,AudioInPort> 00191 { 00192 public: 00193 const char * GetClassName() const {return "AudioBuffPortMonitor";} 00194 }; 00195 } 00196 00197 #endif 00198