Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
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 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#pragma once
20
22#include <pongasoft/Utils/stl.h>
26
27#include "RTParameter.h"
28#include "RTJmbOutParameter.h"
29#include "RTJmbInParameter.h"
30
31#include <map>
32
33namespace pongasoft {
34namespace VST {
35namespace Debug { class ParamDisplay; }
36namespace RT {
37
38using namespace Utils;
39
46{
47public:
48 explicit RTState(Parameters const &iParameters);
49
54 RTRawVstParam add(RawVstParam iParamDef);
55
61 template<size_t N>
62 RTRawVstParams<N> add(RawVstParams<N> const &iParamDefs);
63
68 template<typename T>
69 RTVstParam<T> add(VstParam<T> iParamDef);
70
76 template<typename T, size_t N>
77 RTVstParams<T, N> add(VstParams<T, N> const &iParamDefs);
78
82 template<typename T>
84
88 template<typename T, size_t N>
90
94 template<typename T>
96
100 template<typename T, size_t N>
102
105 virtual tresult init();
106
114 virtual bool beforeProcessing();
115
119 virtual bool applyParameterChanges(IParameterChanges &inputParameterChanges);
120
127 virtual int32 getParamUpdateSampleOffset(ProcessData &iData, ParamID iParamID) const;
128
133 virtual void afterProcessing();
134
139 virtual tresult readNewState(IBStreamer &iStreamer);
140
145 virtual tresult writeLatestState(IBStreamer &oStreamer);
146
149 bool isMessagingEnabled() const { return !fOutboundMessagingParameters.empty(); }
150
153 virtual tresult sendPendingMessages(IMessageProducer *iMessageProducer);
154
158 tresult handleMessage(Message const &iMessage) override;
159
160 // getAllRegistrationOrder
161 std::vector<ParamID> const &getAllRegistrationOrder() const { return fAllRegistrationOrder; }
162
163 // gives access for debug
165
166protected:
167 // the parameters
169
170 // contains all the registered vst parameters (unique ID, will be checked on add)
171 std::map<ParamID, std::unique_ptr<RTRawVstParameter>> fVstParameters{};
172
173 // contains all the registered outbound message parameters (unique ID, will be checked on add)
174 std::map<ParamID, std::unique_ptr<IRTJmbOutParameter>> fOutboundMessagingParameters{};
175
176 // contains all the registered inbound message parameters (unique ID, will be checked on add)
177 std::map<ParamID, std::unique_ptr<IRTJmbInParameter>> fInboundMessagingParameters{};
178
179 // order in which the parameters were registered
180 std::vector<ParamID> fAllRegistrationOrder{};
181
182 // handles messages (receive messages)
184
185protected:
186 // add raw parameter to the structures
187 tresult addRawParameter(std::unique_ptr<RTRawVstParameter> iParameter);
188
189 // add outbound messaging parameter
190 tresult addOutboundMessagingParameter(std::unique_ptr<IRTJmbOutParameter> iParameter);
191
192 // add inbound messaging parameter
193 tresult addInboundMessagingParameter(std::unique_ptr<IRTJmbInParameter> iParameter);
194
199 virtual bool onNewState(NormalizedState const *iLatestState);
200
206 virtual bool resetPreviousValues();
207
211 virtual void computeLatestState(NormalizedState *oLatestState) const;
212
215 virtual void afterReadNewState(NormalizedState const *iState) {};
216
219 virtual void beforeWriteNewState(NormalizedState const *iState) {};
220
221private:
222 // computeLatestState
223 void computeLatestState();
224
225private:
226 // this queue is used to propagate a Processor::setState call (made from the UI thread) to this state
227 // the check happens in beforeProcessing
229
230 // this atomic value always hold the most current (and consistent) version of this state so that the UI thread
231 // can access it in Processor::getState. It is updated in afterProcessing.
233};
234
235//------------------------------------------------------------------------
236// RTState::add
237//------------------------------------------------------------------------
238template<typename T>
240{
241 // YP Impl note: this method exports the raw pointer on purpose. The map fVstParameters is storing unique_ptr so that
242 // when the map gets deleted, all the parameters get deleted as well. The raw pointer is wrapped inside the RTVstParam
243 // wrapper and the normal usage is that it will not outlive the map. Using shared_ptr would be the safest approach
244 // but in the RT code, we want to make sure that we do not pay the penalty of managing a ref counter (which may
245 // use a lock...)
246 auto rawPtr = new RTVstParameter<T>(std::move(iParamDef));
247 std::unique_ptr<RTRawVstParameter> rtParam{rawPtr};
248 addRawParameter(std::move(rtParam));
249 return rawPtr;
250}
251
252//------------------------------------------------------------------------
253// RTState::add
254//------------------------------------------------------------------------
255template<size_t N>
257{
258 return stl::transform<RTRawVstParam, RawVstParam>(iParamDefs, [this](auto &p) { return add(p); });
259}
260
261//------------------------------------------------------------------------
262// RTState::add
263//------------------------------------------------------------------------
264template<typename T, size_t N>
266{
267 return stl::transform<VstParam<T>, RTVstParam<T>>(iParamDefs, [this](auto &p) { return add(p); });
268}
269
270//------------------------------------------------------------------------
271// RTState::addJmbOut
272//------------------------------------------------------------------------
273template<typename T>
275{
276 // YP Impl note: see add for similar impl note
277 auto rawPtr = new RTJmbOutParameter<T>(std::move(iParamDef));
278 std::unique_ptr<IRTJmbOutParameter> rtParam{rawPtr};
279 addOutboundMessagingParameter(std::move(rtParam));
280 return rawPtr;
281}
282
283//------------------------------------------------------------------------
284// RTState::addJmbOut
285//------------------------------------------------------------------------
286template<typename T, size_t N>
288{
289 return stl::transform<RTJmbOutParam<T>, JmbParam<T>>(iParamDefs, [this](auto &p) { return addJmbOut(p); });
290}
291
292//------------------------------------------------------------------------
293// RTState::addJmbIn
294//------------------------------------------------------------------------
295template<typename T>
297{
298 // YP Impl note: see add for similar impl note
299 auto rawPtr = new RTJmbInParameter<T>(iParamDef);
300 std::unique_ptr<IRTJmbInParameter> rtParam{rawPtr};
301 addInboundMessagingParameter(std::move(rtParam));
302 fMessageHandler.registerHandler(iParamDef->fParamID, rawPtr);
303 return rawPtr;
304}
305
306//------------------------------------------------------------------------
307// RTState::addJmbIn
308//------------------------------------------------------------------------
309template<typename T, size_t N>
311{
312 return stl::transform<RTJmbInParam<T>, JmbParam<T>>(iParamDefs, [this](auto &p) { return addJmbIn(p); });
313}
314
315}
316}
317}
This is the lock free version of the AtomicValue.
Definition Concurrent.h:319
This is the lock free version of the SingleElementQueue.
Definition Concurrent.h:150
This helper class is used to display the parameters (vst/jmb) WARNING: this class is allocating memor...
Definition ParamDisplay.h:35
Interface defining a message handler.
Definition MessageHandler.h:32
Abstraction for allocating and sending a message.
Definition MessageProducer.h:35
Simple implementation of IMessageHandler which will delegate the message handling based on MessageID.
Definition MessageHandler.h:41
Simple wrapper class with better api.
Definition Messaging.h:46
Used to communicate the state between the UI and the RT and read/write to stream.
Definition NormalizedState.h:39
This is the class which maintains all the registered parameters.
Definition Parameters.h:39
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition RTJmbInParameter.h:142
Templated class for RT Jamba Inbound parameter.
Definition RTJmbInParameter.h:72
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 main class that the plugin should use as it exposes only the necessary methods of the par...
Definition RTParameter.h:277
bool isMessagingEnabled() const
Definition RTState.h:149
RTState(Parameters const &iParameters)
Definition RTState.cpp:26
std::map< ParamID, std::unique_ptr< IRTJmbInParameter > > fInboundMessagingParameters
Definition RTState.h:177
tresult addInboundMessagingParameter(std::unique_ptr< IRTJmbInParameter > iParameter)
Definition RTState.cpp:116
tresult handleMessage(Message const &iMessage) override
Called by the UI thread (from RTProcessor) to handle messages.
Definition RTState.cpp:369
virtual bool beforeProcessing()
This method should be call at the beginning of the process(ProcessData &data) method before doing any...
Definition RTState.cpp:275
tresult addOutboundMessagingParameter(std::unique_ptr< IRTJmbOutParameter > iParameter)
Definition RTState.cpp:81
std::map< ParamID, std::unique_ptr< IRTJmbOutParameter > > fOutboundMessagingParameters
Definition RTState.h:174
std::vector< ParamID > const & getAllRegistrationOrder() const
Definition RTState.h:161
virtual void afterProcessing()
This method should be called at the end of process(ProcessData &data) method.
Definition RTState.cpp:308
virtual tresult sendPendingMessages(IMessageProducer *iMessageProducer)
Called (from a GUI timer) to send the messages to the GUI (JmbParam for the moment).
Definition RTState.cpp:334
virtual tresult writeLatestState(IBStreamer &oStreamer)
This method should be called from Processor::getState to store the latest state to the stream.
Definition RTState.cpp:263
tresult addRawParameter(std::unique_ptr< RTRawVstParameter > iParameter)
Definition RTState.cpp:52
RTJmbInParam< T > addJmbIn(JmbParam< T > iParamDef)
This method should be called to add an rt inbound jmb parameter.
Definition RTState.h:296
virtual int32 getParamUpdateSampleOffset(ProcessData &iData, ParamID iParamID) const
This uses the same algorithm as when the param value is updated (implemented in applyParameterChanges...
Definition RTState.cpp:187
RTRawVstParam add(RawVstParam iParamDef)
This method is called for each parameter managed by RTState.
Definition RTState.cpp:36
Concurrent::LockFree::AtomicValue< NormalizedState > fLatestState
Definition RTState.h:232
Parameters const & fPluginParameters
Definition RTState.h:168
virtual bool resetPreviousValues()
Called from the RT thread from afterProcessing to reset previous values (copy current value to previo...
Definition RTState.cpp:320
std::map< ParamID, std::unique_ptr< RTRawVstParameter > > fVstParameters
Definition RTState.h:171
virtual void afterReadNewState(NormalizedState const *iState)
Gives a chance to subclasses to tweak and/or display the state after being read.
Definition RTState.h:215
std::vector< ParamID > fAllRegistrationOrder
Definition RTState.h:180
MessageHandler fMessageHandler
Definition RTState.h:183
virtual bool onNewState(NormalizedState const *iLatestState)
Called from the RT thread from beforeProcessing to set the new state.
Definition RTState.cpp:291
virtual tresult init()
Call this method after adding all the parameters.
Definition RTState.cpp:377
virtual void beforeWriteNewState(NormalizedState const *iState)
Gives a chance to subclasses to tweak and/or display the state before being written.
Definition RTState.h:219
virtual tresult readNewState(IBStreamer &iStreamer)
This method should be called from Processor::setState to update this state to the state stored in the...
Definition RTState.cpp:247
virtual bool applyParameterChanges(IParameterChanges &inputParameterChanges)
This method should be called in every frame when there are parameter changes to update this state acc...
Definition RTState.cpp:152
void computeLatestState()
Definition RTState.cpp:237
RTJmbOutParam< T > addJmbOut(JmbParam< T > iParamDef)
This method should be called to add an rt outbound jmb parameter.
Definition RTState.h:274
Concurrent::LockFree::SingleElementQueue< NormalizedState > fStateUpdate
Definition RTState.h:228
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition RTParameter.h:192
The typed version.
Definition RTParameter.h:93
Definition ParamDisplay.cpp:29
Definition RTJmbInParameter.h:26
std::array< RTRawVstParam, N > RTRawVstParams
Definition RTParameter.h:343
std::array< RTJmbInParam< T >, N > RTJmbInParams
Definition RTJmbInParameter.h:181
std::array< RTVstParam< T >, N > RTVstParams
Definition RTParameter.h:267
std::array< RTJmbOutParam< T >, N > RTJmbOutParams
Definition RTJmbOutParameter.h:208
Definition Clock.h:24
std::array< VstParam< T >, N > VstParams
Definition ParamDef.h:513
std::array< JmbParam< T >, N > JmbParams
Definition ParamDef.h:525
std::array< RawVstParam, N > RawVstParams
Definition ParamDef.h:516
std::shared_ptr< JmbParamDef< T > > JmbParam
Definition ParamDef.h:522
std::shared_ptr< VstParamDef< T > > VstParam
Definition ParamDef.h:509
std::shared_ptr< RawVstParamDef > RawVstParam
Definition ParamDef.h:510
constexpr std::array< U, N > transform(std::array< T, N > const &a, F &&f)
Transforms an array containing elements of type T into an array containing elements of type U by appl...
Definition stl.h:41
Definition Clock.h:23