Jamba C++ API  6.2.0
RTState.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2023 pongasoft
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #pragma once
19 
21 #include <pongasoft/Utils/stl.h>
25 
26 #include "RTParameter.h"
27 #include "RTJmbOutParameter.h"
28 #include "RTJmbInParameter.h"
29 
30 #include <map>
31 
32 namespace pongasoft {
33 namespace VST {
34 namespace Debug { class ParamDisplay; }
35 namespace RT {
36 
37 using namespace Utils;
38 
44 class RTState : public IMessageHandler
45 {
46 public:
47  explicit RTState(Parameters const &iParameters);
48 
53  RTRawVstParam add(RawVstParam iParamDef);
54 
60  template<size_t N>
61  RTRawVstParams<N> add(RawVstParams<N> const &iParamDefs);
62 
67  template<typename T>
68  RTVstParam<T> add(VstParam<T> iParamDef);
69 
75  template<typename T, size_t N>
76  RTVstParams<T, N> add(VstParams<T, N> const &iParamDefs);
77 
81  template<typename T>
82  RTJmbOutParam<T> addJmbOut(JmbParam<T> iParamDef);
83 
87  template<typename T, size_t N>
88  RTJmbOutParams<T, N> addJmbOut(JmbParams<T, N> const &iParamDefs);
89 
93  template<typename T>
94  RTJmbInParam<T> addJmbIn(JmbParam<T> iParamDef);
95 
99  template<typename T, size_t N>
100  RTJmbInParams<T, N> addJmbIn(JmbParams<T, N> const &iParamDefs);
101 
104  virtual tresult init();
105 
113  virtual bool beforeProcessing();
114 
118  virtual bool applyParameterChanges(IParameterChanges &inputParameterChanges);
119 
126  virtual int32 getParamUpdateSampleOffset(ProcessData &iData, ParamID iParamID) const;
127 
132  virtual void afterProcessing();
133 
138  virtual tresult readNewState(IBStreamer &iStreamer);
139 
144  virtual tresult writeLatestState(IBStreamer &oStreamer);
145 
148  bool isMessagingEnabled() const { return !fOutboundMessagingParameters.empty(); }
149 
152  virtual tresult sendPendingMessages(IMessageProducer *iMessageProducer);
153 
157  tresult handleMessage(Message const &iMessage) override;
158 
159  // getAllRegistrationOrder
160  std::vector<ParamID> const &getAllRegistrationOrder() const { return fAllRegistrationOrder; }
161 
162  // gives access for debug
163  friend class Debug::ParamDisplay;
164 
165 protected:
166  // the parameters
168 
169  // contains all the registered vst parameters (unique ID, will be checked on add)
170  std::map<ParamID, std::unique_ptr<RTRawVstParameter>> fVstParameters{};
171 
172  // contains all the registered outbound message parameters (unique ID, will be checked on add)
173  std::map<ParamID, std::unique_ptr<IRTJmbOutParameter>> fOutboundMessagingParameters{};
174 
175  // contains all the registered inbound message parameters (unique ID, will be checked on add)
176  std::map<ParamID, std::unique_ptr<IRTJmbInParameter>> fInboundMessagingParameters{};
177 
178  // order in which the parameters were registered
179  std::vector<ParamID> fAllRegistrationOrder{};
180 
181  // handles messages (receive messages)
182  MessageHandler fMessageHandler{};
183 
184 protected:
185  // add raw parameter to the structures
186  tresult addRawParameter(std::unique_ptr<RTRawVstParameter> iParameter);
187 
188  // add outbound messaging parameter
189  tresult addOutboundMessagingParameter(std::unique_ptr<IRTJmbOutParameter> iParameter);
190 
191  // add inbound messaging parameter
192  tresult addInboundMessagingParameter(std::unique_ptr<IRTJmbInParameter> iParameter);
193 
198  virtual bool onNewState(NormalizedState const *iLatestState);
199 
205  virtual bool resetPreviousValues();
206 
210  virtual void computeLatestState(NormalizedState *oLatestState) const;
211 
214  virtual void afterReadNewState(NormalizedState const *iState) {};
215 
218  virtual void beforeWriteNewState(NormalizedState const *iState) {};
219 
220 private:
221  // computeLatestState
222  void computeLatestState();
223 
224 private:
225  // this queue is used to propagate a Processor::setState call (made from the UI thread) to this state
226  // the check happens in beforeProcessing
228 
229  // this atomic value always hold the most current (and consistent) version of this state so that the UI thread
230  // can access it in Processor::getState. It is updated in afterProcessing.
232 };
233 
234 //------------------------------------------------------------------------
235 // RTState::add
236 //------------------------------------------------------------------------
237 template<typename T>
238 RTVstParam<T> RTState::add(VstParam<T> iParamDef)
239 {
240  // YP Impl note: this method exports the raw pointer on purpose. The map fVstParameters is storing unique_ptr so that
241  // when the map gets deleted, all the parameters get deleted as well. The raw pointer is wrapped inside the RTVstParam
242  // wrapper and the normal usage is that it will not outlive the map. Using shared_ptr would be the safest approach
243  // but in the RT code, we want to make sure that we do not pay the penalty of managing a ref counter (which may
244  // use a lock...)
245  auto rawPtr = new RTVstParameter<T>(std::move(iParamDef));
246  std::unique_ptr<RTRawVstParameter> rtParam{rawPtr};
247  addRawParameter(std::move(rtParam));
248  return rawPtr;
249 }
250 
251 //------------------------------------------------------------------------
252 // RTState::add
253 //------------------------------------------------------------------------
254 template<size_t N>
255 RTRawVstParams<N> RTState::add(RawVstParams<N> const &iParamDefs)
256 {
257  return stl::transform<RTRawVstParam, RawVstParam>(iParamDefs, [this](auto &p) { return add(p); });
258 }
259 
260 //------------------------------------------------------------------------
261 // RTState::add
262 //------------------------------------------------------------------------
263 template<typename T, size_t N>
264 RTVstParams<T, N> RTState::add(VstParams<T, N> const &iParamDefs)
265 {
266  return stl::transform<VstParam<T>, RTVstParam<T>>(iParamDefs, [this](auto &p) { return add(p); });
267 }
268 
269 //------------------------------------------------------------------------
270 // RTState::addJmbOut
271 //------------------------------------------------------------------------
272 template<typename T>
273 RTJmbOutParam<T> RTState::addJmbOut(JmbParam<T> iParamDef)
274 {
275  // YP Impl note: see add for similar impl note
276  auto rawPtr = new RTJmbOutParameter<T>(std::move(iParamDef));
277  std::unique_ptr<IRTJmbOutParameter> rtParam{rawPtr};
278  addOutboundMessagingParameter(std::move(rtParam));
279  return rawPtr;
280 }
281 
282 //------------------------------------------------------------------------
283 // RTState::addJmbOut
284 //------------------------------------------------------------------------
285 template<typename T, size_t N>
286 RTJmbOutParams<T, N> RTState::addJmbOut(JmbParams<T, N> const &iParamDefs)
287 {
288  return stl::transform<RTJmbOutParam<T>, JmbParam<T>>(iParamDefs, [this](auto &p) { return addJmbOut(p); });
289 }
290 
291 //------------------------------------------------------------------------
292 // RTState::addJmbIn
293 //------------------------------------------------------------------------
294 template<typename T>
295 RTJmbInParam<T> RTState::addJmbIn(JmbParam<T> iParamDef)
296 {
297  // YP Impl note: see add for similar impl note
298  auto rawPtr = new RTJmbInParameter<T>(iParamDef);
299  std::unique_ptr<IRTJmbInParameter> rtParam{rawPtr};
300  addInboundMessagingParameter(std::move(rtParam));
301  fMessageHandler.registerHandler(iParamDef->fParamID, rawPtr);
302  return rawPtr;
303 }
304 
305 //------------------------------------------------------------------------
306 // RTState::addJmbIn
307 //------------------------------------------------------------------------
308 template<typename T, size_t N>
309 RTJmbInParams<T, N> RTState::addJmbIn(JmbParams<T, N> const &iParamDefs)
310 {
311  return stl::transform<RTJmbInParam<T>, JmbParam<T>>(iParamDefs, [this](auto &p) { return addJmbIn(p); });
312 }
313 
314 }
315 }
316 }
std::array< RTJmbInParam< T >, N > RTJmbInParams
Definition: RTJmbInParameter.h:180
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: RTJmbOutParameter.h:170
Templated class for RT Jamba parameter.
Definition: RTJmbOutParameter.h:69
This is the class which maintains all the registered parameters.
Definition: Parameters.h:37
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: RTParameter.h:190
std::array< RTRawVstParam, N > RTRawVstParams
Definition: RTParameter.h:342
This is the lock free version of the AtomicValue.
Definition: Concurrent.h:317
RTJmbOutParam< T > addJmbOut(JmbParam< T > iParamDef)
This method should be called to add an rt outbound jmb parameter.
Definition: RTState.h:273
Abstraction for allocating and sending a message.
Definition: MessageProducer.h:33
This helper class is used to display the parameters (vst/jmb) WARNING: this class is allocating memor...
Definition: ParamDisplay.h:33
virtual void afterReadNewState(NormalizedState const *iState)
Gives a chance to subclasses to tweak and/or display the state after being read.
Definition: RTState.h:214
Simple wrapper class with better api.
Definition: Messaging.h:44
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: RTJmbInParameter.h:140
Definition: Clock.h:22
Interface defining a message handler.
Definition: MessageHandler.h:30
std::shared_ptr< RawVstParamDef > RawVstParam
Definition: ParamDef.h:509
Manages the state used by the processor: you add all the parameters that the state manages using the ...
Definition: RTState.h:44
Used to communicate the state between the UI and the RT and read/write to stream.
Definition: NormalizedState.h:37
virtual void beforeWriteNewState(NormalizedState const *iState)
Gives a chance to subclasses to tweak and/or display the state before being written.
Definition: RTState.h:218
bool isMessagingEnabled() const
Definition: RTState.h:148
std::vector< ParamID > const & getAllRegistrationOrder() const
Definition: RTState.h:160
std::array< RTVstParam< T >, N > RTVstParams
Definition: RTParameter.h:266
Templated class for RT Jamba Inbound parameter.
Definition: RTJmbInParameter.h:70
std::array< RTJmbOutParam< T >, N > RTJmbOutParams
Definition: RTJmbOutParameter.h:207
RTRawVstParam add(RawVstParam iParamDef)
This method is called for each parameter managed by RTState.
Definition: RTState.cpp:35
Concurrent::LockFree::AtomicValue< NormalizedState > fLatestState
Definition: RTState.h:231
Simple implementation of IMessageHandler which will delegate the message handling based on MessageID.
Definition: MessageHandler.h:39
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: RTParameter.h:275
Parameters const & fPluginParameters
Definition: RTState.h:167
RTJmbInParam< T > addJmbIn(JmbParam< T > iParamDef)
This method should be called to add an rt inbound jmb parameter.
Definition: RTState.h:295
The typed version.
Definition: RTParameter.h:91
Concurrent::LockFree::SingleElementQueue< NormalizedState > fStateUpdate
Definition: RTState.h:227
This is the lock free version of the SingleElementQueue.
Definition: Concurrent.h:148