Jamba C++ API 8.0.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::VST {
34namespace Debug { class ParamDisplay; }
35namespace RT {
36
37using namespace Utils;
38
45{
46public:
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>
83
87 template<typename T, size_t N>
89
93 template<typename T>
95
99 template<typename T, size_t N>
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
164
165protected:
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)
183
184protected:
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
220private:
221 // computeLatestState
222 void computeLatestState();
223
224private:
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 holds 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//------------------------------------------------------------------------
237template<typename T>
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//------------------------------------------------------------------------
254template<size_t N>
256{
257 return stl::transform<RTRawVstParam, RawVstParam>(iParamDefs, [this](auto &p) { return add(p); });
258}
259
260//------------------------------------------------------------------------
261// RTState::add
262//------------------------------------------------------------------------
263template<typename T, size_t N>
265{
266 return stl::transform<VstParam<T>, RTVstParam<T>>(iParamDefs, [this](auto &p) { return add(p); });
267}
268
269//------------------------------------------------------------------------
270// RTState::addJmbOut
271//------------------------------------------------------------------------
272template<typename T>
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//------------------------------------------------------------------------
285template<typename T, size_t N>
287{
288 return stl::transform<RTJmbOutParam<T>, JmbParam<T>>(iParamDefs, [this](auto &p) { return addJmbOut(p); });
289}
290
291//------------------------------------------------------------------------
292// RTState::addJmbIn
293//------------------------------------------------------------------------
294template<typename T>
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//------------------------------------------------------------------------
308template<typename T, size_t N>
310{
311 return stl::transform<RTJmbInParam<T>, JmbParam<T>>(iParamDefs, [this](auto &p) { return addJmbIn(p); });
312}
313
314}
315}
This is the lock free version of the AtomicValue.
Definition Concurrent.h:331
This is the lock free version of the SingleElementQueue.
Definition Concurrent.h:162
This helper class is used to display the parameters (vst/jmb) WARNING: this class is allocating memor...
Definition ParamDisplay.h:33
Interface defining a message handler.
Definition MessageHandler.h:32
Abstraction for allocating and sending a message.
Definition MessageProducer.h:34
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:43
Used to communicate the state between the UI and the RT and read/write to stream.
Definition NormalizedState.h:39
This is the class that maintains all the registered parameters.
Definition Parameters.h:41
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:171
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:148
RTState(Parameters const &iParameters)
Definition RTState.cpp:27
std::map< ParamID, std::unique_ptr< IRTJmbInParameter > > fInboundMessagingParameters
Definition RTState.h:176
tresult addInboundMessagingParameter(std::unique_ptr< IRTJmbInParameter > iParameter)
Definition RTState.cpp:117
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:82
std::map< ParamID, std::unique_ptr< IRTJmbOutParameter > > fOutboundMessagingParameters
Definition RTState.h:173
std::vector< ParamID > const & getAllRegistrationOrder() const
Definition RTState.h:160
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:53
RTJmbInParam< T > addJmbIn(JmbParam< T > iParamDef)
This method should be called to add an rt inbound jmb parameter.
Definition RTState.h:295
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:37
Concurrent::LockFree::AtomicValue< NormalizedState > fLatestState
Definition RTState.h:231
Parameters const & fPluginParameters
Definition RTState.h:167
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:170
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
std::vector< ParamID > fAllRegistrationOrder
Definition RTState.h:179
MessageHandler fMessageHandler
Definition RTState.h:182
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:218
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:153
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:273
Concurrent::LockFree::SingleElementQueue< NormalizedState > fStateUpdate
Definition RTState.h:227
This is the main class that the plugin should use as it exposes only the necessary methods of the par...
Definition RTParameter.h:195
The typed version.
Definition RTParameter.h:96
Definition ParamDisplay.cpp:29
Definition RTJmbInParameter.h:26
std::array< RTRawVstParam, N > RTRawVstParams
An array of RTRawVstParam.
Definition RTParameter.h:340
std::array< RTJmbInParam< T >, N > RTJmbInParams
An array of RTJmbInParam.
Definition RTJmbInParameter.h:182
std::array< RTVstParam< T >, N > RTVstParams
An array of RTVstParam.
Definition RTParameter.h:267
std::array< RTJmbOutParam< T >, N > RTJmbOutParams
An array of RTJmbOutParam.
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:49