ESP  0.1
The Example-based Sensor Predictions (ESP) system tries to bring machine learning to the maker community.
istream.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include "GRT/GRT.h"
12 #include "ofMain.h"
13 #include "ofxNetwork.h"
14 #include "ofxOsc.h"
15 #include "stream.h"
16 
17 #include <cstdint>
18 
19 // See more documentation:
20 // http://openframeworks.cc/documentation/sound/ofSoundStream/#show_setup
21 const uint32_t kOfSoundStream_SamplingRate = 44100;
22 const uint32_t kOfSoundStream_BufferSize = 256;
23 const uint32_t kOfSoundStream_nBuffers = 4;
24 
31 class InputStream : public virtual Stream {
32  public:
33  InputStream();
34  virtual ~InputStream() = default;
35 
40  virtual int getNumInputDimensions() = 0;
41  virtual int getNumOutputDimensions() {
42  vector<double> input(getNumInputDimensions(), 1.0);
43  vector<double> output = normalize(input);
44  return output.size();
45  }
46 
47  using normalizeFunc = std::function<double(double)>;
48  using vectorNormalizeFunc = std::function<vector<double>(vector<double>)>;
49 
50  // Supply a normalization function: double -> double.
51  // Applied to each dimension of each vector of incoming data.
53  normalizer_ = f;
54  vectorNormalizer_ = nullptr;
55  }
56 
57  // Supply a normalization function: vector<double> -> vector<double>
58  // Applied to each vector of incoming data.
60  normalizer_ = nullptr;
62  }
63 
64  typedef std::function<void(GRT::MatrixDouble)> onDataReadyCallback;
65 
66  void onDataReadyEvent(onDataReadyCallback callback) {
67  data_ready_callback_ = callback;
68  }
69 
70  template<typename T1, typename arg, class T>
71  void onDataReadyEvent(T1* owner, void (T::*listenerMethod)(arg)) {
72  using namespace std::placeholders;
73  data_ready_callback_ = std::bind(listenerMethod, owner, _1);
74  }
75 
76  // Set labels on all input dimension. This function takes either a vector of
77  // strings, or an initialization list (such as {"left", "right"}).
78  void setLabelsForAllDimensions(const vector<string> labels);
79  void setLabelsForAllDimensions(std::initializer_list<string> list);
80 
81  const vector<string>& getLabels() const;
82 
83  protected:
84  vector<string> InputStream_labels_;
85  onDataReadyCallback data_ready_callback_;
88 
89  vector<double> normalize(vector<double>);
90 };
91 
95 class AudioStream : public ofBaseApp, public InputStream {
96  public:
97  AudioStream(uint32_t downsample_rate = 1);
98  void audioIn(float *input, int buffer_size, int nChannel);
99  virtual bool start() final;
100  virtual void stop() final;
101  virtual int getNumInputDimensions() final;
102  private:
103  uint32_t downsample_rate_;
104  unique_ptr<ofSoundStream> sound_stream_;
105  bool setup_successful_;
106 };
107 
114 class AudioFileStream : public InputStream {
115  public:
116  AudioFileStream(char *file, bool loop = false);
117  virtual bool start() final;
118  virtual void stop() final;
119  virtual int getNumInputDimensions() final;
120  private:
121  void readSpectrum();
122 
123  ofSoundPlayer player_;
124  unique_ptr<std::thread> update_thread_;
125 };
126 
127 class BaseSerialInputStream : public virtual InputStream {
128  public:
136  BaseSerialInputStream(uint32_t usb_port_num, uint32_t baud, int numDimensions);
137 
146  BaseSerialInputStream(uint32_t baud, int numDimensions);
147 
148  virtual bool start() final;
149  virtual void stop() final;
150  virtual int getNumInputDimensions() final;
151 
152  vector<string> getSerialDeviceList() {
153  serial_->listDevices();
154  vector<string> retval;
155  vector<ofSerialDeviceInfo> device_list = serial_->getDeviceList();
156  retval.reserve(device_list.size());
157  for (auto& d : device_list) {
158  retval.push_back(d.getDevicePath());
159  }
160  return retval;
161  }
162 
163  bool selectSerialDevice(uint32_t port) {
164  assert(has_started_ == false
165  && "Should only reach here if ASCIISerialStream hasn't started");
166 
167  port_ = port;
168  if (!serial_->setup(port_, baud_)) {
169  return false;
170  }
171 
172  reading_thread_.reset(new std::thread(&BaseSerialInputStream::readSerial, this));
173  has_started_ = true;
174  return true;
175  }
176 
177  protected:
178  virtual void parseSerial(vector<unsigned char> &buffer) = 0;
179  unique_ptr<ofSerial> serial_;
180 
181  private:
182  uint32_t port_ = -1;
183  uint32_t baud_;
184  int dimensions_;
185 
186  vector<unsigned char> buffer_;
187 
188  // A separate reading thread to read data from Serial.
189  unique_ptr<std::thread> reading_thread_;
190  void readSerial();
191 };
192 
193 class SerialStream : public InputStream {
194  public:
195  SerialStream(uint32_t usb_port_num, uint32_t baud);
196  virtual bool start() final;
197  virtual void stop() final;
198  virtual int getNumInputDimensions() final;
199  private:
200  uint32_t port_ = -1;
201  uint32_t baud_;
202  // Serial buffer size
203  uint32_t kBufferSize_ = 64;
204 
205  unique_ptr<ofSerial> serial_;
206 
207  // A separate reading thread to read data from Serial.
208  unique_ptr<std::thread> reading_thread_;
209  void readSerial();
210 };
211 
217 class FirmataStream : public InputStream {
218  public:
224  FirmataStream(uint32_t port);
225  virtual bool start() final;
226  virtual void stop() final;
227  virtual int getNumInputDimensions() final;
228 
237  void useAnalogPin(int i);
238  private:
239  uint32_t port_;
240 
241  vector<int> pins_;
242 
243  bool configured_arduino_;
244 
245  ofArduino arduino_;
246  unique_ptr<std::thread> update_thread_;
247  void update();
248 };
249 
250 // Forward declaration.
251 class ofxTCPServer;
252 
256 class TcpInputStream : public InputStream {
257  public:
258  TcpInputStream(int port_num, int dimension)
259  : port_num_(port_num), dim_(dimension) {
260  }
261 
262  virtual bool start() final;
263  virtual void stop() final;
264  virtual int getNumInputDimensions() final;
265 
266  private:
267  void parseInput(const string& buffer);
268  ofxTCPServer server_;
269  unique_ptr<std::thread> reading_thread_;
270  int port_num_;
271  int dim_;
272 };
273 
277 class OscInputStream : public InputStream {
278  public:
279  OscInputStream(int port_num, string addr, int dimension)
280  : port_num_(port_num), addr_(addr), dim_(dimension) {
281  }
282 
283  void handleMessage(ofxOscMessage& m);
284  virtual bool start() final;
285  virtual void stop() final;
286  virtual int getNumInputDimensions() final;
287 
288  private:
289  void parseInput(const string& buffer);
290  ofxOscReceiver receiver_;
291  unique_ptr<std::thread> reading_thread_;
292  int port_num_;
293  string addr_;
294  int dim_;
295 };
void onDataReadyEvent(T1 *owner, void(T::*listenerMethod)(arg))
Definition: istream.h:71
Definition: stream.h:6
Listening for data inputs over a TCP socket.
Definition: istream.h:256
virtual int getNumOutputDimensions()
Definition: istream.h:41
vector< double > normalize(vector< double >)
Definition: istream.cpp:9
unique_ptr< ofSerial > serial_
Definition: istream.h:179
std::function< void(GRT::MatrixDouble)> onDataReadyCallback
Definition: istream.h:64
Input stream for reading audio from the computer&#39;s microphone.
Definition: istream.h:95
normalizeFunc normalizer_
Definition: istream.h:86
void onDataReadyEvent(onDataReadyCallback callback)
Definition: istream.h:66
Listening for data inputs over OSC.
Definition: istream.h:277
vector< string > getSerialDeviceList()
Definition: istream.h:152
TcpInputStream(int port_num, int dimension)
Definition: istream.h:258
std::atomic_bool has_started_
Definition: stream.h:24
std::function< vector< double >(vector< double >)> vectorNormalizeFunc
Definition: istream.h:48
vectorNormalizeFunc vectorNormalizer_
Definition: istream.h:87
virtual int getNumInputDimensions()=0
virtual bool start()
Definition: stream.h:13
const vector< string > & getLabels() const
Definition: istream.cpp:31
void useNormalizer(normalizeFunc f)
Definition: istream.h:52
void useNormalizer(vectorNormalizeFunc f)
Definition: istream.h:59
virtual ~InputStream()=default
Definition: istream.h:127
InputStream()
Definition: istream.cpp:7
Base class for input streams that provide live sensor data to the ESP system. To use an InputStream i...
Definition: istream.h:31
const uint32_t kOfSoundStream_BufferSize
Definition: istream.h:22
const uint32_t kOfSoundStream_nBuffers
Definition: istream.h:23
const uint32_t kOfSoundStream_SamplingRate
Definition: istream.h:21
OscInputStream(int port_num, string addr, int dimension)
Definition: istream.h:279
std::function< double(double)> normalizeFunc
Definition: istream.h:47
virtual void stop()
Definition: stream.h:14
constexpr uint32_t downsample_rate
Definition: cli.cpp:16
Input stream for getting the FFT spectrum of an audio file as it plays. This class plays an audio fil...
Definition: istream.h:114
Input stream for reading analog data from an Arduino running Firmata. To use an FirmataStream in your...
Definition: istream.h:217
void setLabelsForAllDimensions(const vector< string > labels)
Definition: istream.cpp:21
vector< string > InputStream_labels_
Definition: istream.h:84
onDataReadyCallback data_ready_callback_
Definition: istream.h:85
Definition: istream.h:193
bool selectSerialDevice(uint32_t port)
Definition: istream.h:163