VCV Rack API v2
Loading...
Searching...
No Matches
Module.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
3
4#include <jansson.h>
5
6#include <common.hpp>
7#include <string.hpp>
8#include <plugin/Model.hpp>
9#include <engine/Param.hpp>
10#include <engine/Port.hpp>
11#include <engine/Light.hpp>
13#include <engine/PortInfo.hpp>
14#include <engine/LightInfo.hpp>
15
16
17namespace rack {
18
19
20namespace plugin {
21struct Model;
22}
23
24
25namespace engine {
26
27
29struct Module {
30 struct Internal;
31 Internal* internal;
32
35
40 int64_t id = -1;
41
47 std::vector<Param> params;
48 std::vector<Input> inputs;
49 std::vector<Output> outputs;
50 std::vector<Light> lights;
51
58 std::vector<ParamQuantity*> paramQuantities;
59 std::vector<PortInfo*> inputInfos;
60 std::vector<PortInfo*> outputInfos;
61 std::vector<LightInfo*> lightInfos;
62
64 struct Expander {
66 int64_t moduleId = -1;
68 Module* module = NULL;
89 void* producerMessage = NULL;
90 void* consumerMessage = NULL;
92
95 }
96 };
97
100
101 struct BypassRoute {
102 int inputId = -1;
103 int outputId = -1;
104 };
105 std::vector<BypassRoute> bypassRoutes;
106
110 DEPRECATED Module(int numParams, int numInputs, int numOutputs, int numLights = 0) : Module() {
111 config(numParams, numInputs, numOutputs, numLights);
112 }
113 virtual ~Module();
114
118 void config(int numParams, int numInputs, int numOutputs, int numLights = 0);
119
124 template <class TParamQuantity = ParamQuantity>
125 TParamQuantity* configParam(int paramId, float minValue, float maxValue, float defaultValue, std::string name = "", std::string unit = "", float displayBase = 0.f, float displayMultiplier = 1.f, float displayOffset = 0.f) {
126 assert(paramId < (int) params.size() && paramId < (int) paramQuantities.size());
127 if (paramQuantities[paramId])
128 delete paramQuantities[paramId];
129
130 TParamQuantity* q = new TParamQuantity;
131 q->ParamQuantity::module = this;
132 q->ParamQuantity::paramId = paramId;
133 q->ParamQuantity::minValue = minValue;
134 q->ParamQuantity::maxValue = maxValue;
135 q->ParamQuantity::defaultValue = defaultValue;
136 q->ParamQuantity::name = name;
137 q->ParamQuantity::unit = unit;
138 q->ParamQuantity::displayBase = displayBase;
139 q->ParamQuantity::displayMultiplier = displayMultiplier;
140 q->ParamQuantity::displayOffset = displayOffset;
141 paramQuantities[paramId] = q;
142
143 Param* p = &params[paramId];
144 p->value = q->getDefaultValue();
145 return q;
146 }
147
152 template <class TSwitchQuantity = SwitchQuantity>
153 TSwitchQuantity* configSwitch(int paramId, float minValue, float maxValue, float defaultValue, std::string name = "", std::vector<std::string> labels = {}) {
154 TSwitchQuantity* sq = configParam<TSwitchQuantity>(paramId, minValue, maxValue, defaultValue, name);
155 sq->ParamQuantity::snapEnabled = true;
156 sq->ParamQuantity::smoothEnabled = false;
157 sq->SwitchQuantity::labels = labels;
158 return sq;
159 }
160
164 template <class TSwitchQuantity = SwitchQuantity>
165 TSwitchQuantity* configButton(int paramId, std::string name = "") {
166 TSwitchQuantity* sq = configParam<TSwitchQuantity>(paramId, 0.f, 1.f, 0.f, name);
167 sq->ParamQuantity::snapEnabled = true;
168 sq->ParamQuantity::smoothEnabled = false;
169 sq->ParamQuantity::randomizeEnabled = false;
170 return sq;
171 }
172
177 template <class TPortInfo = PortInfo>
178 TPortInfo* configInput(int portId, std::string name = "") {
179 assert(portId < (int) inputs.size() && portId < (int) inputInfos.size());
180 if (inputInfos[portId])
181 delete inputInfos[portId];
182
183 TPortInfo* info = new TPortInfo;
184 info->PortInfo::module = this;
185 info->PortInfo::type = Port::INPUT;
186 info->PortInfo::portId = portId;
187 info->PortInfo::name = name;
188 inputInfos[portId] = info;
189 return info;
190 }
191
196 template <class TPortInfo = PortInfo>
197 TPortInfo* configOutput(int portId, std::string name = "") {
198 assert(portId < (int) outputs.size() && portId < (int) outputInfos.size());
199 if (outputInfos[portId])
200 delete outputInfos[portId];
201
202 TPortInfo* info = new TPortInfo;
203 info->PortInfo::module = this;
204 info->PortInfo::type = Port::OUTPUT;
205 info->PortInfo::portId = portId;
206 info->PortInfo::name = name;
207 outputInfos[portId] = info;
208 return info;
209 }
210
216 template <class TLightInfo = LightInfo>
217 TLightInfo* configLight(int lightId, std::string name = "") {
218 assert(lightId < (int) lights.size() && lightId < (int) lightInfos.size());
219 if (lightInfos[lightId])
220 delete lightInfos[lightId];
221
222 TLightInfo* info = new TLightInfo;
223 info->LightInfo::module = this;
224 info->LightInfo::lightId = lightId;
225 info->LightInfo::name = name;
226 lightInfos[lightId] = info;
227 return info;
228 }
229
233 void configBypass(int inputId, int outputId) {
234 assert(inputId < (int) inputs.size());
235 assert(outputId < (int) outputs.size());
236 // Check that output is not yet routed
237 for (BypassRoute& br : bypassRoutes) {
238 // Prevent unused variable warning for compilers that ignore assert()
239 (void) br;
240 assert(br.outputId != outputId);
241 }
242
243 BypassRoute br;
244 br.inputId = inputId;
245 br.outputId = outputId;
246 bypassRoutes.push_back(br);
247 }
248
261
264 return model;
265 }
266 int64_t getId() {
267 return id;
268 }
270 return params.size();
271 }
272 Param& getParam(int index) {
273 return params[index];
274 }
276 return inputs.size();
277 }
278 Input& getInput(int index) {
279 return inputs[index];
280 }
282 return outputs.size();
283 }
284 Output& getOutput(int index) {
285 return outputs[index];
286 }
288 return lights.size();
289 }
290 Light& getLight(int index) {
291 return lights[index];
292 }
294 return paramQuantities[index];
295 }
296 PortInfo* getInputInfo(int index) {
297 return inputInfos[index];
298 }
300 return outputInfos[index];
301 }
303 return lightInfos[index];
304 }
306 return leftExpander;
307 }
312 Expander& getExpander(uint8_t side) {
313 return side ? rightExpander : leftExpander;
314 }
315
316 // Virtual methods
317
318 struct ProcessArgs {
326 int64_t frame;
327 };
331 virtual void process(const ProcessArgs& args) {
332 step();
333 }
334
336 virtual void step() {}
337
342 virtual void processBypass(const ProcessArgs& args);
343
347 virtual json_t* toJson();
352 virtual void fromJson(json_t* rootJ);
353
355 virtual json_t* paramsToJson();
356 virtual void paramsFromJson(json_t* rootJ);
357
359 virtual json_t* dataToJson() {
360 return NULL;
361 }
365 virtual void dataFromJson(json_t* rootJ) {}
366
368 // Events
370
371 // All of these events are thread-safe with process().
372
373 struct AddEvent {};
376 virtual void onAdd(const AddEvent& e) {
377 // Call deprecated event method by default
378 onAdd();
379 }
380
381 struct RemoveEvent {};
384 virtual void onRemove(const RemoveEvent& e) {
385 // Call deprecated event method by default
386 onRemove();
387 }
388
389 struct BypassEvent {};
392 virtual void onBypass(const BypassEvent& e) {}
393
394 struct UnBypassEvent {};
397 virtual void onUnBypass(const UnBypassEvent& e) {}
398
409 virtual void onPortChange(const PortChangeEvent& e) {}
410
418 // Call deprecated event method by default
420 }
421
424 uint8_t side;
425 };
428 virtual void onExpanderChange(const ExpanderChangeEvent& e) {}
429
430 struct ResetEvent {};
434 virtual void onReset(const ResetEvent& e);
435
436 struct RandomizeEvent {};
440 virtual void onRandomize(const RandomizeEvent& e);
441
442 struct SaveEvent {};
446 virtual void onSave(const SaveEvent& e) {}
447
448 struct SetMasterEvent {};
449 virtual void onSetMaster(const SetMasterEvent& e) {}
450
452 virtual void onUnsetMaster(const UnsetMasterEvent& e) {}
453
455 virtual void onAdd() {}
457 virtual void onRemove() {}
459 virtual void onReset() {}
461 virtual void onRandomize() {}
463 virtual void onSampleRateChange() {}
464
466 PRIVATE void setBypassed(bool bypassed);
467 PRIVATE const float* meterBuffer();
470 PRIVATE void doProcess(const ProcessArgs& args);
471 PRIVATE static void jsonStripIds(json_t* rootJ);
473 PRIVATE void setExpanderModule(Module* module, uint8_t side);
474};
475
476
477} // namespace engine
478} // namespace rack
#define PRIVATE
Attribute for private functions not intended to be called by plugins.
Definition common.hpp:32
#define DEPRECATED
Attribute for deprecated functions and symbols.
Definition common.hpp:26
std::string plugin(plugin::Plugin *plugin, std::string filename="")
Returns the path of an asset in the plugin's dir.
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
Definition Port.hpp:207
Definition LightInfo.hpp:12
Definition Light.hpp:9
Definition Module.hpp:373
Definition Module.hpp:389
Definition Module.hpp:101
int inputId
Definition Module.hpp:102
int outputId
Definition Module.hpp:103
uint8_t side
0 for left, 1 for right.
Definition Module.hpp:424
Represents a message-passing channel for an adjacent module.
Definition Module.hpp:64
void * consumerMessage
Definition Module.hpp:90
int64_t moduleId
ID of the expander module, or -1 if nonexistent.
Definition Module.hpp:66
void requestMessageFlip()
Definition Module.hpp:93
void * producerMessage
Double buffer for receiving messages from the expander module.
Definition Module.hpp:89
bool messageFlipRequested
Definition Module.hpp:91
Definition Module.hpp:399
int portId
Definition Module.hpp:404
Port::Type type
Port::INPUT or Port::OUTPUT.
Definition Module.hpp:403
bool connecting
True if connecting, false if disconnecting.
Definition Module.hpp:401
Definition Module.hpp:318
float sampleTime
The timestep of process() in seconds.
Definition Module.hpp:324
float sampleRate
The current sample rate in Hz.
Definition Module.hpp:320
int64_t frame
Number of audio samples since the Engine's first sample.
Definition Module.hpp:326
Definition Module.hpp:436
Definition Module.hpp:381
Definition Module.hpp:430
float sampleTime
Definition Module.hpp:413
float sampleRate
Definition Module.hpp:412
Definition Module.hpp:442
Definition Module.hpp:448
Definition Module.hpp:394
DSP processor instance for your module.
Definition Module.hpp:29
std::vector< Input > inputs
Definition Module.hpp:48
virtual void onUnBypass(const UnBypassEvent &e)
Called after enabling the module.
Definition Module.hpp:397
int getNumInputs()
Definition Module.hpp:275
void configBypass(int inputId, int outputId)
Adds a direct route from an input to an output when the module is bypassed.
Definition Module.hpp:233
std::vector< LightInfo * > lightInfos
Definition Module.hpp:61
PRIVATE const float * meterBuffer()
virtual void onRemove()
DEPRECATED.
Definition Module.hpp:457
virtual void onAdd()
DEPRECATED.
Definition Module.hpp:455
PRIVATE int meterIndex()
Param & getParam(int index)
Definition Module.hpp:272
TSwitchQuantity * configSwitch(int paramId, float minValue, float maxValue, float defaultValue, std::string name="", std::vector< std::string > labels={})
Helper for creating a SwitchQuantity and setting its label strings.
Definition Module.hpp:153
Expander & getExpander(uint8_t side)
Returns the left Expander for side = 0 and the right Expander for side = 1.
Definition Module.hpp:312
virtual void processBypass(const ProcessArgs &args)
Called instead of process() when Module is bypassed.
int getNumLights()
Definition Module.hpp:287
virtual void onRandomize(const RandomizeEvent &e)
Called when the user randomizes the module.
virtual void onSampleRateChange()
DEPRECATED.
Definition Module.hpp:463
plugin::Model * getModel()
Getters for members.
Definition Module.hpp:263
virtual void onRandomize()
DEPRECATED.
Definition Module.hpp:461
std::string createPatchStorageDirectory()
Creates and returns the module's patch storage directory path.
virtual void onUnsetMaster(const UnsetMasterEvent &e)
Definition Module.hpp:452
virtual void onSave(const SaveEvent &e)
Called when the user saves the patch to a file.
Definition Module.hpp:446
PRIVATE int meterLength()
Expander rightExpander
Definition Module.hpp:99
static PRIVATE void jsonStripIds(json_t *rootJ)
TParamQuantity * configParam(int paramId, float minValue, float maxValue, float defaultValue, std::string name="", std::string unit="", float displayBase=0.f, float displayMultiplier=1.f, float displayOffset=0.f)
Helper for creating a ParamQuantity and setting its properties.
Definition Module.hpp:125
TLightInfo * configLight(int lightId, std::string name="")
Helper for creating a LightInfo and setting its properties.
Definition Module.hpp:217
virtual void onReset()
DEPRECATED.
Definition Module.hpp:459
virtual void onSetMaster(const SetMasterEvent &e)
Definition Module.hpp:449
Input & getInput(int index)
Definition Module.hpp:278
virtual void onBypass(const BypassEvent &e)
Called after bypassing the module.
Definition Module.hpp:392
virtual json_t * toJson()
Usually you should override dataToJson() instead.
virtual void process(const ProcessArgs &args)
Advances the module by one audio sample.
Definition Module.hpp:331
void config(int numParams, int numInputs, int numOutputs, int numLights=0)
Configures the number of Params, Outputs, Inputs, and Lights.
std::vector< Output > outputs
Definition Module.hpp:49
Internal * internal
Definition Module.hpp:31
Light & getLight(int index)
Definition Module.hpp:290
std::vector< Param > params
Arrays of components.
Definition Module.hpp:47
virtual json_t * dataToJson()
Override to store extra internal data in the "data" property of the module's JSON object.
Definition Module.hpp:359
std::vector< BypassRoute > bypassRoutes
Definition Module.hpp:105
virtual void onRemove(const RemoveEvent &e)
Called before removing the module from the Engine.
Definition Module.hpp:384
TPortInfo * configInput(int portId, std::string name="")
Helper for creating a PortInfo for an input port and setting its properties.
Definition Module.hpp:178
Module()
Constructs a Module with no params, inputs, outputs, and lights.
Expander leftExpander
Definition Module.hpp:98
virtual void dataFromJson(json_t *rootJ)
Override to load internal data from the "data" property of the module's JSON object.
Definition Module.hpp:365
virtual void paramsFromJson(json_t *rootJ)
virtual void onAdd(const AddEvent &e)
Called after adding the module to the Engine.
Definition Module.hpp:376
TSwitchQuantity * configButton(int paramId, std::string name="")
Helper for creating a SwitchQuantity with no label.
Definition Module.hpp:165
TPortInfo * configOutput(int portId, std::string name="")
Helper for creating a PortInfo for an output port and setting its properties.
Definition Module.hpp:197
DEPRECATED Module(int numParams, int numInputs, int numOutputs, int numLights=0)
Use config() instead.
Definition Module.hpp:110
virtual void onPortChange(const PortChangeEvent &e)
Called after a cable connects to or disconnects from a port.
Definition Module.hpp:409
ParamQuantity * getParamQuantity(int index)
Definition Module.hpp:293
Expander & getRightExpander()
Definition Module.hpp:308
PRIVATE void setExpanderModule(Module *module, uint8_t side)
Sets module of expander and dispatches ExpanderChangeEvent if changed.
PRIVATE void setBypassed(bool bypassed)
virtual void onExpanderChange(const ExpanderChangeEvent &e)
Called after an expander is added, removed, or changed on either the left or right side of the Module...
Definition Module.hpp:428
PRIVATE void doProcess(const ProcessArgs &args)
virtual void step()
DEPRECATED.
Definition Module.hpp:336
Output & getOutput(int index)
Definition Module.hpp:284
virtual void onSampleRateChange(const SampleRateChangeEvent &e)
Called when the Engine sample rate changes, and when the Module is added to the Engine.
Definition Module.hpp:417
plugin::Model * model
Not owned.
Definition Module.hpp:34
PortInfo * getOutputInfo(int index)
Definition Module.hpp:299
PortInfo * getInputInfo(int index)
Definition Module.hpp:296
std::vector< PortInfo * > inputInfos
Definition Module.hpp:59
int64_t id
Unique ID for referring to the module in the engine.
Definition Module.hpp:40
Expander & getLeftExpander()
Definition Module.hpp:305
std::vector< PortInfo * > outputInfos
Definition Module.hpp:60
std::vector< ParamQuantity * > paramQuantities
Arrays of component metadata.
Definition Module.hpp:58
virtual void onReset(const ResetEvent &e)
Called when the user resets (initializes) the module.
LightInfo * getLightInfo(int index)
Definition Module.hpp:302
int getNumOutputs()
Definition Module.hpp:281
virtual void fromJson(json_t *rootJ)
This is virtual only for the purpose of unserializing legacy data when you could set properties of th...
std::string getPatchStorageDirectory()
std::vector< Light > lights
Definition Module.hpp:50
int getNumParams()
Definition Module.hpp:269
int64_t getId()
Definition Module.hpp:266
virtual json_t * paramsToJson()
Serializes the "params" object.
Definition Port.hpp:205
A Quantity that wraps an engine::Param.
Definition ParamQuantity.hpp:21
Definition Param.hpp:10
float value
Unstable API.
Definition Param.hpp:12
Definition PortInfo.hpp:13
Type
Definition Port.hpp:37
@ OUTPUT
Definition Port.hpp:39
@ INPUT
Definition Port.hpp:38
Type information for a module.
Definition Model.hpp:34