Jamba  3.0.2
RTState.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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);
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>
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>
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>
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 }
Definition: RTJmbOutParameter.h:172
Definition: RTJmbOutParameter.h:71
Definition: Parameters.h:39
Definition: RTParameter.h:191
std::shared_ptr< VstParamDef< T > > VstParam
Definition: ParamDef.h:358
virtual void afterReadNewState(NormalizedState const *iState)
Definition: RTState.h:185
virtual void beforeWriteNewState(NormalizedState const *iState)
Definition: RTState.h:189
Definition: MessageProducer.h:33
Definition: ParamDisplay.h:33
Definition: Messaging.h:43
Definition: RTJmbInParameter.h:142
Definition: Clock.h:22
bool isMessagingEnabled() const
Definition: RTState.h:119
Definition: MessageHandler.h:30
std::vector< ParamID > const & getAllRegistrationOrder() const
Definition: RTState.h:131
Definition: RTState.h:43
Parameters const & fPluginParameters
Definition: RTState.h:138
std::shared_ptr< JmbParamDef< T > > JmbParam
Definition: ParamDef.h:365
Definition: NormalizedState.h:38
Definition: RTJmbInParameter.h:72
Concurrent::LockFree::SingleElementQueue< NormalizedState > fStateUpdate
Definition: RTState.h:198
std::shared_ptr< RawVstParamDef > RawVstParam
Definition: ParamDef.h:359
Definition: MessageHandler.h:39
Definition: RTParameter.h:261
RTJmbInParam< T > addJmbIn(JmbParam< T > iParamDef)
Definition: RTState.h:239
RTJmbOutParam< T > addJmbOut(JmbParam< T > iParamDef)
Definition: RTState.h:226
Concurrent::LockFree::AtomicValue< NormalizedState > fLatestState
Definition: RTState.h:202
Definition: RTParameter.h:92
RTRawVstParam add(RawVstParam iParamDef)
Definition: RTState.cpp:37