ofxPDSP
OscInput.h
1 
2 // OscInput.h
3 // ofxPDSP
4 // Nicola Pisanti, MIT License, 2016
5 
6 #ifndef OFXPDSPMIDI_PDSPOSCINPUT_H_INCLUDED
7 #define OFXPDSPMIDI_PDSPOSCINPUT_H_INCLUDED
8 
9 #include "ofMain.h"
10 #include <chrono>
11 #include <algorithm>
12 #include <thread>
13 #include <mutex>
14 #include <condition_variable>
15 #include <atomic>
16 #include "../DSP/pdspCore.h"
17 #include "../sequencer/SequencerSection.h"
18 #include "ofxOsc.h"
19 #include "helper/OscParser.h"
20 
28 typedef osc::ReceivedMessage _PDSPOscReceivedMessage_t;
29 typedef osc::IpEndpointName _PDSPIpEndpointName_t;
30 typedef osc::osc_bundle_element_size_t _PDSPosc_bundle_element_size_t;
35 namespace pdsp {
36 class Engine;
37 }
38 
39 namespace pdsp{ namespace osc {
40 
41 class Input : public pdsp::Preparable {
42  friend class pdsp::Engine;
43 
44 private:
45 
46  class _PositionedOscMessage {
47  public:
48  _PositionedOscMessage(){ sample = -1; };
49  _PositionedOscMessage(ofxOscMessage message, int sample) : message(message), sample(sample){};
50 
51 
52  std::chrono::time_point<std::chrono::high_resolution_clock> timepoint;
53  ofxOscMessage message;
54  int sample;
55  };
56 
57  class CustomOscReceiver : public ofxOscReceiver {
58  public:
59  std::vector<_PositionedOscMessage> * circularBuffer;
60  std::atomic<int> * index;
61  protected:
62  void ProcessMessage(const _PDSPOscReceivedMessage_t &m, const _PDSPIpEndpointName_t &remoteEndpoint) override;
63  };
64 
65 public:
66  Input();
67  ~Input();
68 
69 
74  void openPort( int port );
75 
79  void close();
80 
81 
85  bool isConnected() { return connected; }
86 
91  void setVerbose( bool verbose );
92 
98  void linkTempo( string oscAddress, int argument=0 );
99 
105  pdsp::SequencerGateOutput& out_trig( string oscAddress, int argument=0 );
106 
112  pdsp::SequencerValueOutput& out_value( string oscAddress, int argument=0 );
113 
119  std::function<float(float)> & parser( string oscAddress, int argument=0 );
120 
127  void initTo( string oscAddress, int argument, float value );
128 
132  void clearAll();
133 
137  void processOsc( int bufferSize ) noexcept;
138  bool hasTempoChange();
139  double getTempo();
144 protected:
145  void prepareToPlay( int expectedBufferSize, double sampleRate ) override;
146  void releaseResources() override;
147 
148 private:
149  CustomOscReceiver receiver;
150 
151  std::vector<OscParser*> parsers;
152 
153  bool connected;
154  bool verbose;
155 
156  bool sendClearMessages;
157 
158  std::atomic<int> index;
159  int lastread;
160  std::vector<_PositionedOscMessage> circularBuffer;
161  std::vector<_PositionedOscMessage> readVector;
162 
163  double oneSlashMicrosecForSample;
164 
165  chrono::time_point<chrono::high_resolution_clock> bufferChrono;
166 
167 
168  bool tempoLinked;
169  std::string tempoAddress;
170  int tempoArgument;
171  double tempo;
172  bool tempoChanged;
173 
174  void pushToReadVector( _PositionedOscMessage & message );
175  int checkParser( std::string oscAddress );
176 
177  static std::vector<Input*> instances;
178 };
179 
180 }}
181 
182 #endif // OFXPDSPMIDI_PDSPOSCINPUT_H_INCLUDED
pdsp::SequencerGateOutput & out_trig(string oscAddress, int argument=0)
get a trigger output for the given OSC address. Only the first value of those address will be taken...
Definition: OscInput.cpp:95
Definition: Engine.h:36
void clearAll()
sends 0.0f as message to all the connected trigger and value outputs.
Definition: OscInput.cpp:123
void linkTempo(string oscAddress, int argument=0)
control the clock of the units and sequencer with an OSC message
Definition: OscInput.cpp:76
pdsp::SequencerValueOutput & out_value(string oscAddress, int argument=0)
get a value output for the given OSC address. Only the first value of those address will be taken...
Definition: OscInput.cpp:101
Takes messages from a MessageBuffer and convert them in an optionally smoothed value output...
Definition: SequencerValueOutput.h:23
Takes messages from a MessageBuffer and convert them in a trigger output.
Definition: SequencerGateOutput.h:20
void close()
shuts down the output
Definition: OscInput.cpp:65
bool isConnected()
return true if the port has been sucessfully opened
Definition: OscInput.h:85
Definition: OscInput.h:41
Thread-safe value control with smoothing.
Definition: SequencerBridge.h:11
void openPort(int port)
open the port with the given index
Definition: OscInput.cpp:53
void setVerbose(bool verbose)
enable or disable diagnostic messages
Definition: OscInput.cpp:48
void initTo(string oscAddress, int argument, float value)
sets an initial value for the selected addres and argument, valid until any other OSC message is rece...
Definition: OscInput.cpp:111
std::function< float(float)> & parser(string oscAddress, int argument=0)
get a reference to the parser for the selected address and argument. You can assign a lambda function...
Definition: OscInput.cpp:106
Definition: Preparable.h:36