Jamba C++ API  4.0.0
RTState.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019 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 
24 
25 #include "RTParameter.h"
26 #include "RTJmbOutParameter.h"
27 #include "RTJmbInParameter.h"
28 
29 #include <map>
30 
31 namespace pongasoft {
32 namespace VST {
33 namespace Debug { class ParamDisplay; }
34 namespace RT {
35 
36 using namespace Utils;
37 
43 class RTState : public IMessageHandler
44 {
45 public:
46  explicit RTState(Parameters const &iParameters);
47 
52  RTRawVstParam add(RawVstParam iParamDef);
53 
58  template<typename T>
59  RTVstParam<T> add(VstParam<T> iParamDef);
60 
64  template<typename T>
65  RTJmbOutParam<T> addJmbOut(JmbParam<T> iParamDef);
66 
70  template<typename T>
71  RTJmbInParam<T> addJmbIn(JmbParam<T> iParamDef);
72 
75  virtual tresult init();
76 
84  virtual bool beforeProcessing();
85 
89  virtual bool applyParameterChanges(IParameterChanges &inputParameterChanges);
90 
97  virtual int32 getParamUpdateSampleOffset(ProcessData &iData, ParamID iParamID) const;
98 
103  virtual void afterProcessing();
104 
109  virtual tresult readNewState(IBStreamer &iStreamer);
110 
115  virtual tresult writeLatestState(IBStreamer &oStreamer);
116 
119  bool isMessagingEnabled() const { return !fOutboundMessagingParameters.empty(); }
120 
123  virtual tresult sendPendingMessages(IMessageProducer *iMessageProducer);
124 
128  tresult handleMessage(Message const &iMessage) override;
129 
130  // getAllRegistrationOrder
131  std::vector<ParamID> const &getAllRegistrationOrder() const { return fAllRegistrationOrder; }
132 
133  // gives access for debug
134  friend class Debug::ParamDisplay;
135 
136 protected:
137  // the parameters
139 
140  // contains all the registered vst parameters (unique ID, will be checked on add)
141  std::map<ParamID, std::unique_ptr<RTRawVstParameter>> fVstParameters{};
142 
143  // contains all the registered outbound message parameters (unique ID, will be checked on add)
144  std::map<ParamID, std::unique_ptr<IRTJmbOutParameter>> fOutboundMessagingParameters{};
145 
146  // contains all the registered inbound message parameters (unique ID, will be checked on add)
147  std::map<ParamID, std::unique_ptr<IRTJmbInParameter>> fInboundMessagingParameters{};
148 
149  // order in which the parameters were registered
150  std::vector<ParamID> fAllRegistrationOrder{};
151 
152  // handles messages (receive messages)
153  MessageHandler fMessageHandler{};
154 
155 protected:
156  // add raw parameter to the structures
157  tresult addRawParameter(std::unique_ptr<RTRawVstParameter> iParameter);
158 
159  // add outbound messaging parameter
160  tresult addOutboundMessagingParameter(std::unique_ptr<IRTJmbOutParameter> iParameter);
161 
162  // add inbound messaging parameter
163  tresult addInboundMessagingParameter(std::unique_ptr<IRTJmbInParameter> iParameter);
164 
169  virtual bool onNewState(NormalizedState const *iLatestState);
170 
176  virtual bool resetPreviousValues();
177 
181  virtual void computeLatestState(NormalizedState *oLatestState) const;
182 
185  virtual void afterReadNewState(NormalizedState const *iState) {};
186 
189  virtual void beforeWriteNewState(NormalizedState const *iState) {};
190 
191 private:
192  // computeLatestState
193  void computeLatestState();
194 
195 private:
196  // this queue is used to propagate a Processor::setState call (made from the UI thread) to this state
197  // the check happens in beforeProcessing
199 
200  // this atomic value always hold the most current (and consistent) version of this state so that the UI thread
201  // can access it in Processor::getState. It is updated in afterProcessing.
203 };
204 
205 //------------------------------------------------------------------------
206 // RTState::add
207 //------------------------------------------------------------------------
208 template<typename T>
209 RTVstParam<T> RTState::add(VstParam<T> iParamDef)
210 {
211  // YP Impl note: this method exports the raw pointer on purpose. The map fVstParameters is storing unique_ptr so that
212  // when the map gets deleted, all the parameters get deleted as well. The raw pointer is wrapped inside the RTVstParam
213  // wrapper and the normal usage is that it will not outlive the map. Using shared_ptr would be the safest approach
214  // but in the RT code, we want to make sure that we do not pay the penalty of managing a ref counter (which may
215  // use a lock...)
216  auto rawPtr = new RTVstParameter<T>(std::move(iParamDef));
217  std::unique_ptr<RTRawVstParameter> rtParam{rawPtr};
218  addRawParameter(std::move(rtParam));
219  return rawPtr;
220 }
221 
222 //------------------------------------------------------------------------
223 // RTState::addJmbOut
224 //------------------------------------------------------------------------
225 template<typename T>
226 RTJmbOutParam<T> RTState::addJmbOut(JmbParam<T> iParamDef)
227 {
228  // YP Impl note: see add for similar impl note
229  auto rawPtr = new RTJmbOutParameter<T>(std::move(iParamDef));
230  std::unique_ptr<IRTJmbOutParameter> rtParam{rawPtr};
231  addOutboundMessagingParameter(std::move(rtParam));
232  return rawPtr;
233 }
234 
235 //------------------------------------------------------------------------
236 // RTState::addJmbIn
237 //------------------------------------------------------------------------
238 template<typename T>
239 RTJmbInParam<T> RTState::addJmbIn(JmbParam<T> iParamDef)
240 {
241  // YP Impl note: see add for similar impl note
242  auto rawPtr = new RTJmbInParameter<T>(iParamDef);
243  std::unique_ptr<IRTJmbInParameter> rtParam{rawPtr};
244  addInboundMessagingParameter(std::move(rtParam));
245  fMessageHandler.registerHandler(iParamDef->fParamID, rawPtr);
246  return rawPtr;
247 }
248 
249 }
250 }
251 }
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: RTJmbOutParameter.h:172
Templated class for RT Jamba parameter.
Definition: RTJmbOutParameter.h:71
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:191
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:226
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:185
Simple wrapper class with better api.
Definition: Messaging.h:42
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition: RTJmbInParameter.h:142
Definition: Clock.h:22
Interface defining a message handler.
Definition: MessageHandler.h:30
std::shared_ptr< RawVstParamDef > RawVstParam
Definition: ParamDef.h:444
Manages the state used by the processor: you add all the parameters that the state manages using the ...
Definition: RTState.h:43
Used to communicate the state between the UI and the RT and read/write to stream.
Definition: NormalizedState.h:38
virtual void beforeWriteNewState(NormalizedState const *iState)
Gives a chance to subclasses to tweak and/or display the state before being written.
Definition: RTState.h:189
bool isMessagingEnabled() const
Definition: RTState.h:119
std::vector< ParamID > const & getAllRegistrationOrder() const
Definition: RTState.h:131
Templated class for RT Jamba Inbound parameter.
Definition: RTJmbInParameter.h:72
RTRawVstParam add(RawVstParam iParamDef)
This method is called for each parameter managed by RTState.
Definition: RTState.cpp:37
Concurrent::LockFree::AtomicValue< NormalizedState > fLatestState
Definition: RTState.h:202
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:261
Parameters const & fPluginParameters
Definition: RTState.h:138
RTJmbInParam< T > addJmbIn(JmbParam< T > iParamDef)
This method should be called to add an rt inbound jmb parameter.
Definition: RTState.h:239
The typed version.
Definition: RTParameter.h:92
Concurrent::LockFree::SingleElementQueue< NormalizedState > fStateUpdate
Definition: RTState.h:198
This is the lock free version of the SingleElementQueue.
Definition: Concurrent.h:148