Rapid-Mix API  v2.2.0
A simple library for machine learning & signal processing
rapidPiPoHost.h
Go to the documentation of this file.
1 #ifndef _RAPID_PIPO_HOST_H_
2 #define _RAPID_PIPO_HOST_H_
3 
4 #include "PiPo.h"
5 #include "PiPoHost.h"
6 #include "PiPoCollection.h"
7 
8 //#ifdef EXTERNAL_JSONCPP_PATH
9 #define EXTERNAL_JSONCPP_PATH "json.h"
10 #include EXTERNAL_JSONCPP_PATH
11 //#endif /* EXTERNAL_JSONCPP_PATH */
12 
13 #define MIN_PIPO_SAMPLERATE (1.0 / 31536000000.0) /* once a year */
14 #define MAX_PIPO_SAMPLERATE (96000000000.0)
15 
16 #define PIPO_OUT_RING_SIZE 2
17 
19  pipoStreamAttributes() : // default parameters suited for audio
20  hasTimeTags(false),
22  offset(0),
23  width(1),
24  height(1),
25  labels({ "" }),
26  hasVarSize(false),
27  domain(0),
28  maxFrames(1) {}
29 
31  double rate;
32  double offset;
33  unsigned int width;
34  unsigned int height;
35  std::vector<std::string> labels;
36  bool hasVarSize;
37  double domain;
38  unsigned int maxFrames;
39 };
40 
41 
42 //class PiPoObserver;
43 class PiPoOut;
44 
45 //================================ H O S T ===================================//
46 
47 class PiPoHost : public PiPo::Parent {
48  friend class PiPoOut;
49 
50 private:
51  PiPo *graph;
52  std::string graphName;
53  PiPoOut *out;
54  // PiPoObserver *obs;
55  PiPoStreamAttributes inputStreamAttrs;
56  PiPoStreamAttributes outputStreamAttrs;
57 
58 public:
59  // PiPoHost(PiPoObserver *obs);
60  PiPoHost();
61  ~PiPoHost();
62 
63  // PiPoObserver *getObserver();
64 
65  virtual bool setGraph(std::string name);
66  virtual void clearGraph();
67 
68  // override this method when inheriting !!!
69  virtual void onNewFrameOut(double time, std::vector<PiPoValue> &frame);
70  virtual std::vector<PiPoValue> getLastFrameOut();
71 
72  virtual int setInputStreamAttributes(pipoStreamAttributes &sa, bool propagate = true);
73  virtual pipoStreamAttributes getOutputStreamAttributes();
74 
75  virtual int frames(double time, double weight, PiPoValue *values, unsigned int size,
76  unsigned int num);
77 
78  // virtual bool setAttr(const std::string &attrName, bool value);
79  // virtual bool setAttr(const std::string &attrName, int value);
80  virtual bool setAttr(const std::string &attrName, double value);
81  virtual bool setAttr(const std::string &attrName, const std::vector<double> &values);
82  virtual bool setAttr(const std::string &attrName, const std::string &value); // for enums
83 
84  // virtual const std::vector<std::string>& getAttrNames();
85  // virtual bool isBoolAttr(const std::string &attrName);
86  // virtual bool isEnumAttr(const std::string &attrName);
87  // virtual bool isIntAttr(const std::string &attrName);
88  // virtual bool isIntArrayAttr(const std::string &attrName);
89  // virtual bool isFloatAttr(const std::string &attrName);
90  // virtual bool isFloatArrayAttr(const std::string &attrName);
91  // virtual bool isStringAttr(const std::string &attrName);
92 
93  virtual std::vector<std::string> getAttrNames();
94 
95  virtual double getDoubleAttr(const std::string &attrName);
96  virtual std::vector<double> getDoubleArrayAttr(const std::string &attrName);
97  virtual std::string getEnumAttr(const std::string &attrName);
98 
100  virtual std::string getJSON();
102  virtual void writeJSON(const std::string &filepath);
104  virtual bool putJSON(const std::string &jsonMessage);
106  virtual bool readJSON(const std::string &filepath);
107 
108 
109  // int streamAttributes(bool hasTimeTags, double rate, double offset,
110  // unsigned int width, unsigned int height,
111  // const std::vector<std::string> &labels,
112  // bool hasVarSize, double domain, unsigned int maxFrames,
113  // bool propagate = true);
114 
115  // void propagateInputAttributes();
116 
117 
118  // void streamAttributesChanged(PiPo *pipo, PiPo::Attr *attr);
119  // void signalError(PiPo *pipo, std::string errorMsg);
120  // void signalWarning(PiPo *pipo, std::string warningMsg);
121 
122  /*
123  void setInputHasTimeTags(bool hasTimeTags, bool propagate = true);
124  void setInputFrameRate(double rate, bool propagate = true);
125  void setInputFrameOffset(double offset, bool propagate = true);
126  void setInputDims(int width, int height, bool propagate = true);
127  void setInputLabels(const std::vector<std::string> &labels, bool propagate = true);
128  void setInputHasVarSize(bool hasVarSize, bool propagate = true);
129  void setInputDomain(double domain, bool propagate = true);
130  void setInputMaxFrames(int maxFrames, bool propagate = true);
131 
132  bool getInputHasTimeTags();
133  double getInputFrameRate();
134  double getInputFrameOffset();
135  void getInputDims(int &width, int &height);
136  void getInputLabels(std::vector<std::string> &labels);
137  bool getInputHasVarSize();
138  double getInputDomain();
139  int getInputMaxFrames();
140 
141  bool getOutputHasTimeTags();
142  double getOutputFrameRate();
143  double getOutputFrameOffset();
144  void getOutputDims(int &width, int &height);
145  void getOutputLabels(std::vector<std::string> &labels);
146  bool getOutputHasVarSize();
147  double getOutputDomain();
148  int getOutputMaxFrames();
149 
150  // void setPiPoParam(PiPoParam *param);
151  //*/
152 protected:
153  Json::Value toJSON();
154  bool fromJSON(Json::Value &jv);
155 
156 private:
157  int propagateInputStreamAttributes();
158  void setOutputAttributes(bool hasTimeTags, double rate, double offset,
159  unsigned int width, unsigned int height,
160  const char **labels, bool hasVarSize,
161  double domain, unsigned int maxFrames);
162 
163 };
164 
165 //================================= PiPoOut ==================================//
166 
167  class PiPoOut : public PiPo {
168 private:
169  PiPoHost *host;
170  std::atomic<int> writeIndex, readIndex;
171  std::vector<std::vector<PiPoValue>> ringBuffer;
172  // std::function<void(std::vector<PiPoValue>, PiPoObserver *rpo)> frameCallback;
173  // std::function<void(std::vector<PiPoValue>)> simpleFrameCallback;
174 
175 public:
176  PiPoOut(PiPoHost *host) :
177  PiPo((PiPo::Parent *)host) {
178  this->host = host;
179  writeIndex = 0;
180  readIndex = 0;
181  ringBuffer.resize(PIPO_OUT_RING_SIZE);
182  }
183 
184  ~PiPoOut() {}
185 
187  double rate, double offset,
188  unsigned int width, unsigned int height,
189  const char **labels, bool hasVarSize,
190  double domain, unsigned int maxFrames) {
191 
192  this->host->setOutputAttributes(hasTimeTags, rate, offset, width, height,
193  labels, hasVarSize, domain, maxFrames);
194 
195  for (int i = 0; i < PIPO_OUT_RING_SIZE; ++i) {
196  ringBuffer[i].resize(width * height);
197  }
198 
199  return 0;
200  }
201 
202  int frames(double time, double weight, float *values,
203  unsigned int size, unsigned int num) {
204 
205  if (num > 0) {
206  for (int i = 0; i < num; ++i) {
207 
208  for (int j = 0; j < size; ++j) {
209  ringBuffer[writeIndex][j] = values[i * size + j];
210  }
211 
212  // atomic swap ?
213  writeIndex = 1 - writeIndex;
214  readIndex = 1 - writeIndex;
215 
216  this->host->onNewFrameOut(time, ringBuffer[readIndex]);
217 
218  if (writeIndex + 1 == PIPO_OUT_RING_SIZE) {
219  writeIndex = 0;
220  } else {
221  writeIndex++;
222  }
223  }
224  }
225 
226  return 0;
227  }
228 
229  //void setFrameCallback(std::function<void(std::vector<PiPoValue>,
230  // PiPoObserver *obs)> f) {
231  // frameCallback = f;
232  //}
233 
234  // void setSimpleFrameCallback(std::function<void(std::vector<PiPoValue>)> f) {
235  // simpleFrameCallback = f;
236  // }
237 
238  std::vector<PiPoValue> getLastFrame() {
239  std::vector<PiPoValue> f;
240 
241  if (readIndex > -1) {
242  f = ringBuffer[readIndex];
243  }
244 
245  return f;
246  }
247 };
248 
249 #endif /* _RAPID_PIPO_HOST_H_ */
~PiPoOut()
Definition: rapidPiPoHost.h:184
#define PIPO_OUT_RING_SIZE
Definition: rapidPiPoHost.h:16
Definition: rapidPiPoHost.h:18
bool hasTimeTags
Definition: rapidPiPoHost.h:30
virtual void onNewFrameOut(double time, std::vector< PiPoValue > &frame)
Definition: rapidPiPoHost.cpp:157
int streamAttributes(bool hasTimeTags, double rate, double offset, unsigned int width, unsigned int height, const char **labels, bool hasVarSize, double domain, unsigned int maxFrames)
Definition: rapidPiPoHost.h:186
std::vector< PiPoValue > getLastFrame()
Definition: rapidPiPoHost.h:238
Definition: rapidPiPoHost.h:47
double rate
Definition: rapidPiPoHost.h:31
double domain
Definition: rapidPiPoHost.h:37
unsigned int maxFrames
Definition: rapidPiPoHost.h:38
double offset
Definition: rapidPiPoHost.h:32
pipoStreamAttributes()
Definition: rapidPiPoHost.h:19
bool hasVarSize
Definition: rapidPiPoHost.h:36
int frames(double time, double weight, float *values, unsigned int size, unsigned int num)
Definition: rapidPiPoHost.h:202
#define MIN_PIPO_SAMPLERATE
Definition: rapidPiPoHost.h:13
unsigned int height
Definition: rapidPiPoHost.h:34
PiPoOut(PiPoHost *host)
Definition: rapidPiPoHost.h:176
std::vector< std::string > labels
Definition: rapidPiPoHost.h:35
Definition: rapidPiPoHost.h:167
unsigned int width
Definition: rapidPiPoHost.h:33