CLAM-Development  1.4.0
PortMonitor.hxx
Go to the documentation of this file.
1 #ifndef __PortMonitor_hxx__
2 #define __PortMonitor_hxx__
3 
4 #include "Processing.hxx"
5 #include "ProcessingConfig.hxx"
6 #include "Mutex.hxx"
7 
8 // Temporary until concrete classes will be separated
9 #include "SpectralPeakArray.hxx"
10 #include "Spectrum.hxx"
11 #include "Fundamental.hxx"
12 #include "InPort.hxx"
13 #include "Audio.hxx"
14 #include "AudioInPort.hxx"
15 
16 // sigslot
17 #include "Signalv0.hxx"
18 #include "Slotv0.hxx"
19 
20 namespace CLAM
21 {
52  template <typename TheDataType, typename ThePortType=InPort<TheDataType> >
53  class PortMonitor : public Processing
54  {
55  public:
56  typedef TheDataType DataType;
57  typedef ThePortType PortType;
58 
59  inline PortMonitor(const Config & cfg= Config() );
60  inline virtual ~PortMonitor();
61 
62  inline bool Do();
63 
64  const char * GetClassName() const {return "PortMonitor";}
65 
66  inline const DataType & FreezeAndGetData();
67  inline void UnfreezeData();
68 
69  void AttachStartSlot(SigSlot::Slotv0& slot) {mSigStart.Connect(slot);}
70  void AttachStopSlot(SigSlot::Slotv0& slot) { mSigStop.Connect(slot);}
71  void AttachSlotNewData(SigSlot::Slotv0& slot) { mSigNewData.Connect(slot);}
72 
73  protected:
74  bool ConcreteStart() { mSigStart.Emit(); return true;}
75  bool ConcreteStop() { mSigStop.Emit(); return true;}
76 
77 
78  private:
79  PortType mInput;
80  DataType mData[2];
81  TryMutex mSwitchMutex;
82  unsigned mWhichDataToRead;
83  SigSlot::Signalv0 mSigStart;
84  SigSlot::Signalv0 mSigStop;
85  SigSlot::Signalv0 mSigNewData;
86  };
87 
88  namespace Hidden
89  {
90  template <typename T>
91  static void initData(DynamicType * selector, T & data)
92  {
93  data.AddAll();
94  data.UpdateData();
95  }
96  template <typename T>
97  static void initData(void * selector, T & data)
98  {
99  }
100  }
101 
102  template <typename PortDataType, typename PortType>
104  : mInput("Input", this)
105  , mWhichDataToRead(0)
106  {
107  Configure(cfg);
108  Hidden::initData(&mData[0],mData[0]);
109  Hidden::initData(&mData[1],mData[1]);
110  }
111 
112  template <typename PortDataType, typename PortType>
114  {
115  }
116 
117  template <typename PortDataType, typename PortType>
119  {
120  Hidden::LockOps<TryMutex>::Lock(mSwitchMutex);
121  return mData[mWhichDataToRead];
122  }
123 
124  template <typename PortDataType, typename PortType>
126  {
127  Hidden::LockOps<TryMutex>::Unlock(mSwitchMutex);
128  }
129 
130  template <typename PortDataType, typename PortType>
132  {
133  if(!AbleToExecute()) return true;
134  unsigned whichDataToWrite = mWhichDataToRead?0:1;
135  mData[whichDataToWrite] = mInput.GetData();
136  mSigNewData.Emit();
137  {
138  TryMutex::ScopedTryLock lock(mSwitchMutex,true);
139  if (lock.Locked())
140  mWhichDataToRead = whichDataToWrite;
141  }
142  mInput.Consume();
143  return true;
144  }
145 
146 
147 
148  class PeaksPortMonitor : public PortMonitor <SpectralPeakArray>
149  {
150  public:
151  const char * GetClassName() const {return "PeaksPortMonitor";}
152  };
153  class SinTracksPortMonitor : public PortMonitor<SpectralPeakArray>
154  {
155  public:
156  const char * GetClassName() const {return "SinTracksPortMonitor";}
157  };
158  class SpectrumPortMonitor : public PortMonitor <Spectrum>
159  {
160  public:
161  const char * GetClassName() const {return "SpectrumPortMonitor";}
162  };
163  class SpecgramPortMonitor : public PortMonitor<Spectrum>
164  {
165  public:
166  const char * GetClassName() const {return "SpecgramPortMonitor";}
167  };
168  class FundamentalPortMonitor : public PortMonitor <Fundamental>
169  {
170  public:
171  const char * GetClassName() const {return "FundamentalPortMonitor";}
172  };
173  class FundTrackPortMonitor : public PortMonitor<Fundamental>
174  {
175  public:
176  const char * GetClassName() const {return "FundTrackPortMonitor";}
177  };
178 
179  template <>
181 
182  template <>
184 
185  class AudioPortMonitor : public PortMonitor <Audio,AudioInPort>
186  {
187  public:
188  const char * GetClassName() const {return "AudioPortMonitor";}
189  };
190  class AudioBuffPortMonitor : public PortMonitor<Audio,AudioInPort>
191  {
192  public:
193  const char * GetClassName() const {return "AudioBuffPortMonitor";}
194  };
195 }
196 
197 #endif
198