C++ API Reference RNBO: src/RNBO_ParameterEventInterface.h Source File

RNBO: src/RNBO_ParameterEventInterface.h Source File

1 #ifndef RNBO_ParameterEventInterface_h
2 #define RNBO_ParameterEventInterface_h
3 
4 #include "RNBO_Types.h"
5 #include "RNBO_ParameterInterface.h"
6 #include "RNBO_List.h"
7 #include "RNBO_EventVariant.h"
8 #include "RNBO_UniquePtr.h"
9 #include "RNBO_EventList.h"
10 
11 namespace RNBO {
12 
13  using ScheduleCallback = std::function<void(MillisecondTime)>;
14 
21 
22  friend class EventHandler;
23 
24  public:
25 
26  virtual ~ParameterEventInterface() { }
27 
28  enum Type {
34  };
36 
37  virtual void scheduleEvent(EventVariant event) = 0;
38 
39  void sendMessage(MessageTag tag, number payload, MessageTag objectId = 0, MillisecondTime eventTime = RNBOTimeNow)
40  {
41 #ifdef RNBO_NOMESSAGEEVENT
42  RNBO_ASSERT(false); // not support without std:lib
43 #else
44  scheduleEvent(MessageEvent(tag, eventTime, payload, objectId));
45 #endif
46  }
47 
48  void sendMessage(MessageTag tag, UniqueListPtr payload, MessageTag objectId = 0, MillisecondTime eventTime = RNBOTimeNow)
49  {
50 #ifdef RNBO_NOMESSAGEEVENT
51  RNBO_ASSERT(false); // not support without std:lib
52 #else
53  scheduleEvent(MessageEvent(tag, eventTime, std::move(payload), objectId));
54 #endif
55  }
56 
57  void sendMessage(MessageTag tag, MessageTag objectId = 0, MillisecondTime eventTime = RNBOTimeNow)
58  {
59 #ifdef RNBO_NOMESSAGEEVENT
60  RNBO_ASSERT(false); // not support without std:lib
61 #else
62  scheduleEvent(MessageEvent(tag, eventTime, objectId));
63 #endif
64  }
65 
66  virtual void setScheduleCallback(ScheduleCallback callback)
67  {
68  RNBO_UNUSED(callback)
69  // allows you to get a synchronous notification whenever a clock
70  // event is scheduled, makes only sense in combination with a
71  // sync trigger parameter interface
72  RNBO_ASSERT(false);
73  }
74 
75  private:
76 
77  // your handler will call drain (preferrably in an async manner) to receive event callbacks
78  virtual void drainEvents() = 0;
79 
80  };
81 
85  class ParameterEventInterfaceImpl : public ParameterEventInterface {
86 
87  public:
88 
89  virtual ~ParameterEventInterfaceImpl() { }
90 
91  virtual void refreshParameterCountAndValues() = 0;
92  virtual void notifyParameterValueChanged(ParameterEvent sourceEvent, ParameterIndex index, ParameterValue value) = 0;
93  virtual void drainIncomingQueueToEventList(EventList<EventVariant>& eventList, MillisecondTime currentTime) = 0;
94  virtual void pushDirtyParameters(MillisecondTime currentTime) = 0;
95  virtual void pushOutgoingEvent(EventVariant event) = 0;
96  virtual void notifyOutgoingEvents() = 0;
97 
98  virtual void drainEvents() = 0;
99 
100  // those are needed for the async case
101  virtual bool isActive() = 0;
102  virtual void deactivate() = 0;
103  virtual void notifyParameterInterfaceDeleted() = 0;
104 
105  protected:
106 
107  bool _eventsPushed = false;
108 
109  };
110 
117  class EventHandler {
118 
119  public:
120 
121  virtual ~EventHandler() {}
122 
123  // TAKE CARE if you drain events here directly, you will block the audio thread
124  virtual void eventsAvailable() = 0;
125 
126  // your actual event handlers, implement the ones below to get the events in your handler
127  virtual void handleParameterEvent(const ParameterEvent& event) { RNBO_UNUSED(event); }
128  virtual void handleMidiEvent(const MidiEvent& event) { RNBO_UNUSED(event); }
129  virtual void handleMessageEvent(const MessageEvent& event) { RNBO_UNUSED(event); }
130  virtual void handlePresetEvent(const PresetEvent& event) { RNBO_UNUSED(event); }
131  virtual void handleTempoEvent(const TempoEvent& event) { RNBO_UNUSED(event); }
132  virtual void handleTransportEvent(const TransportEvent& event) { RNBO_UNUSED(event); }
133  virtual void handleBeatTimeEvent(const BeatTimeEvent& event) { RNBO_UNUSED(event); }
134  virtual void handleTimeSignatureEvent(const TimeSignatureEvent& event) { RNBO_UNUSED(event); }
135  virtual void handleStartupEvent(const StartupEvent& event) { RNBO_UNUSED(event); }
136  virtual void handleBBUEvent(const BBUEvent& event) { RNBO_UNUSED(event); }
137 
144  //we either need to be unsetting the _peInterface or setting a null
145  //_peInterface, otherwise we're silently dropping an interface and
146  //drainEvents won't ever be called on the previous interface
147  RNBO_ASSERT(peInterface == nullptr || _peInterface == nullptr);
148  _peInterface = peInterface;
149  }
150 
151  protected:
152 
153  // call this in a deferred manner from eventsAvailable (NOT IN THE AUDIO THREAD)
154  void drainEvents() {
155  if (_peInterface) {
156  _peInterface->drainEvents();
157  }
158  }
159 
160  private:
161 
162  ParameterEventInterface* _peInterface = nullptr;
163 
164  };
165 
166  using ParameterEventInterfaceUniquePtr = UniquePtr<ParameterEventInterface>;
167 
168 } // namespace RNBO
169 
170 #endif // RNBO_ParameterEventInterface_h