You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Module.hpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #pragma once
  2. #include <vector>
  3. #include <jansson.h>
  4. #include <common.hpp>
  5. #include <string.hpp>
  6. #include <plugin/Model.hpp>
  7. #include <engine/Param.hpp>
  8. #include <engine/Port.hpp>
  9. #include <engine/Light.hpp>
  10. #include <engine/ParamQuantity.hpp>
  11. #include <engine/PortInfo.hpp>
  12. #include <engine/LightInfo.hpp>
  13. namespace rack {
  14. namespace plugin {
  15. struct Model;
  16. }
  17. namespace engine {
  18. /** DSP processor instance for your module. */
  19. struct Module {
  20. struct Internal;
  21. Internal* internal;
  22. plugin::Model* model = NULL;
  23. /** Unique ID for referring to the module in the engine.
  24. Between 0 and 2^53 since this is serialized with JSON.
  25. Assigned when added to the engine.
  26. */
  27. int64_t id = -1;
  28. /** Arrays of components.
  29. Initialized with config().
  30. */
  31. std::vector<Param> params;
  32. std::vector<Input> inputs;
  33. std::vector<Output> outputs;
  34. std::vector<Light> lights;
  35. /** Arrays of component metadata.
  36. Initialized with configParam(), configInput(), configOutput(), and configLight().
  37. LightInfos are initialized to null unless configLight() is called.
  38. */
  39. std::vector<ParamQuantity*> paramQuantities;
  40. std::vector<PortInfo*> inputInfos;
  41. std::vector<PortInfo*> outputInfos;
  42. std::vector<LightInfo*> lightInfos;
  43. /** Represents a message-passing channel for an adjacent module. */
  44. struct Expander {
  45. /** ID of the expander module, or -1 if nonexistent. */
  46. int64_t moduleId = -1;
  47. /** Pointer to the expander Module, or NULL if nonexistent. */
  48. Module* module = NULL;
  49. /** Double buffer for receiving messages from the expander module.
  50. If you intend to receive messages from an expander, allocate both message buffers with identical blocks of memory (arrays, structs, etc).
  51. Remember to free the buffer in the Module destructor.
  52. Example:
  53. rightExpander.producerMessage = new MyExpanderMessage;
  54. rightExpander.consumerMessage = new MyExpanderMessage;
  55. You must check the expander module's `model` before attempting to write its message buffer.
  56. Once the module is checked, you can reinterpret_cast its producerMessage at no performance cost.
  57. Producer messages are intended to be write-only.
  58. Consumer messages are intended to be read-only.
  59. Once you write a message, set messageFlipRequested to true to request that the messages are flipped at the end of the timestep.
  60. This means that message-passing has 1-sample latency.
  61. You may choose for your Module to instead write to its own message buffer for consumption by other modules, i.e. the expander "pulls" rather than this module "pushing".
  62. As long as this convention is followed by the other module, this is fine.
  63. */
  64. void* producerMessage = NULL;
  65. void* consumerMessage = NULL;
  66. bool messageFlipRequested = false;
  67. void requestMessageFlip() {
  68. messageFlipRequested = true;
  69. }
  70. };
  71. Expander leftExpander;
  72. Expander rightExpander;
  73. struct BypassRoute {
  74. int inputId = -1;
  75. int outputId = -1;
  76. };
  77. std::vector<BypassRoute> bypassRoutes;
  78. /** Constructs a Module with no params, inputs, outputs, and lights. */
  79. Module();
  80. /** Use config() instead. */
  81. DEPRECATED Module(int numParams, int numInputs, int numOutputs, int numLights = 0) : Module() {
  82. config(numParams, numInputs, numOutputs, numLights);
  83. }
  84. virtual ~Module();
  85. /** Configures the number of Params, Outputs, Inputs, and Lights. */
  86. void config(int numParams, int numInputs, int numOutputs, int numLights = 0);
  87. /** Helper for creating a ParamQuantity and setting its properties.
  88. See ParamQuantity for documentation of arguments.
  89. */
  90. template <class TParamQuantity = ParamQuantity>
  91. 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) {
  92. assert(paramId < (int) params.size() && paramId < (int) paramQuantities.size());
  93. if (paramQuantities[paramId])
  94. delete paramQuantities[paramId];
  95. TParamQuantity* q = new TParamQuantity;
  96. q->module = this;
  97. q->paramId = paramId;
  98. q->minValue = minValue;
  99. q->maxValue = maxValue;
  100. q->defaultValue = defaultValue;
  101. q->name = name;
  102. q->unit = unit;
  103. q->displayBase = displayBase;
  104. q->displayMultiplier = displayMultiplier;
  105. q->displayOffset = displayOffset;
  106. paramQuantities[paramId] = q;
  107. Param* p = &params[paramId];
  108. p->value = q->getDefaultValue();
  109. return q;
  110. }
  111. /** Helper for creating a SwitchQuantity and setting its label strings.
  112. See ParamQuantity and SwitchQuantity for documentation of arguments.
  113. */
  114. template <class TSwitchQuantity = SwitchQuantity>
  115. TSwitchQuantity* configSwitch(int paramId, float minValue, float maxValue, float defaultValue, std::string name = "", std::vector<std::string> labels = {}) {
  116. TSwitchQuantity* sq = configParam<TSwitchQuantity>(paramId, minValue, maxValue, defaultValue, name);
  117. sq->labels = labels;
  118. return sq;
  119. }
  120. template <class TSwitchQuantity = SwitchQuantity>
  121. TSwitchQuantity* configButton(int paramId, std::string name = "") {
  122. TSwitchQuantity* sq = configParam<TSwitchQuantity>(paramId, 0.f, 1.f, 0.f, name);
  123. sq->randomizeEnabled = false;
  124. return sq;
  125. }
  126. /** Helper for creating a PortInfo for an input port and setting its properties.
  127. See PortInfo for documentation of arguments.
  128. */
  129. template <class TPortInfo = PortInfo>
  130. TPortInfo* configInput(int portId, std::string name = "") {
  131. assert(portId < (int) inputs.size() && portId < (int) inputInfos.size());
  132. if (inputInfos[portId])
  133. delete inputInfos[portId];
  134. TPortInfo* info = new TPortInfo;
  135. info->module = this;
  136. info->type = Port::INPUT;
  137. info->portId = portId;
  138. info->name = name;
  139. inputInfos[portId] = info;
  140. return info;
  141. }
  142. /** Helper for creating a PortInfo for an output port and setting its properties.
  143. See PortInfo for documentation of arguments.
  144. */
  145. template <class TPortInfo = PortInfo>
  146. TPortInfo* configOutput(int portId, std::string name = "") {
  147. assert(portId < (int) outputs.size() && portId < (int) outputInfos.size());
  148. if (outputInfos[portId])
  149. delete outputInfos[portId];
  150. TPortInfo* info = new TPortInfo;
  151. info->module = this;
  152. info->type = Port::OUTPUT;
  153. info->portId = portId;
  154. info->name = name;
  155. outputInfos[portId] = info;
  156. return info;
  157. }
  158. /** Helper for creating a LightInfo and setting its properties.
  159. For multi-colored lights, use the first lightId.
  160. See LightInfo for documentation of arguments.
  161. */
  162. template <class TLightInfo = LightInfo>
  163. TLightInfo* configLight(int lightId, std::string name = "") {
  164. assert(lightId < (int) lights.size() && lightId < (int) lightInfos.size());
  165. if (lightInfos[lightId])
  166. delete lightInfos[lightId];
  167. TLightInfo* info = new TLightInfo;
  168. info->module = this;
  169. info->lightId = lightId;
  170. info->name = name;
  171. lightInfos[lightId] = info;
  172. return info;
  173. }
  174. /** Adds a direct route from an input to an output when the module is bypassed.
  175. */
  176. void configBypass(int inputId, int outputId) {
  177. assert(inputId < (int) inputs.size());
  178. assert(outputId < (int) outputs.size());
  179. // Check that output is not yet routed
  180. for (BypassRoute& br : bypassRoutes) {
  181. assert(br.outputId != outputId);
  182. }
  183. BypassRoute br;
  184. br.inputId = inputId;
  185. br.outputId = outputId;
  186. bypassRoutes.push_back(br);
  187. }
  188. /** Creates and returns the module's patch storage directory path.
  189. Do not call this method in process() since filesystem operations block the audio thread.
  190. Throws an Exception if Module is not yet added to the Engine.
  191. Therefore, you may not call these methods in your Module constructor.
  192. Instead, load patch storage files in onAdd() and save them in onSave().
  193. Patch storage files of deleted modules are garbage collected when user saves the patch.
  194. To allow the Undo feature to restore patch storage if the module is accidentally deleted, it is recommended to not delete patch storage in onRemove().
  195. */
  196. std::string createPatchStorageDirectory();
  197. std::string getPatchStorageDirectory();
  198. /** Getters for members */
  199. plugin::Model* getModel() {
  200. return model;
  201. }
  202. int64_t getId() {
  203. return id;
  204. }
  205. int getNumParams() {
  206. return params.size();
  207. }
  208. Param& getParam(int index) {
  209. return params[index];
  210. }
  211. int getNumInputs() {
  212. return inputs.size();
  213. }
  214. Input& getInput(int index) {
  215. return inputs[index];
  216. }
  217. int getNumOutputs() {
  218. return outputs.size();
  219. }
  220. Output& getOutput(int index) {
  221. return outputs[index];
  222. }
  223. int getNumLights() {
  224. return lights.size();
  225. }
  226. Light& getLight(int index) {
  227. return lights[index];
  228. }
  229. ParamQuantity* getParamQuantity(int index) {
  230. return paramQuantities[index];
  231. }
  232. PortInfo* getInputInfo(int index) {
  233. return inputInfos[index];
  234. }
  235. PortInfo* getOutputInfo(int index) {
  236. return outputInfos[index];
  237. }
  238. LightInfo* getLightInfo(int index) {
  239. return lightInfos[index];
  240. }
  241. Expander& getLeftExpander() {
  242. return leftExpander;
  243. }
  244. Expander& getRightExpander() {
  245. return rightExpander;
  246. }
  247. /** Returns the left Expander if `side` is false, andright Expander if `side` is true. */
  248. Expander& getExpander(bool side) {
  249. return side ? rightExpander : leftExpander;
  250. }
  251. // Virtual methods
  252. struct ProcessArgs {
  253. /** The current sample rate in Hz. */
  254. float sampleRate;
  255. /** The timestep of process() in seconds.
  256. Defined by `1 / sampleRate`.
  257. */
  258. float sampleTime;
  259. /** Number of audio samples since the Engine's first sample. */
  260. int64_t frame;
  261. };
  262. /** Advances the module by one audio sample.
  263. Override this method to read Inputs and Params and to write Outputs and Lights.
  264. */
  265. virtual void process(const ProcessArgs& args) {
  266. step();
  267. }
  268. /** DEPRECATED. Override `process(const ProcessArgs& args)` instead. */
  269. virtual void step() {}
  270. /** Called instead of process() when Module is bypassed.
  271. Typically you do not need to override this. Use configBypass() instead.
  272. If you do override it, avoid reading param values, since the state of the module should have no effect on routing.
  273. */
  274. virtual void processBypass(const ProcessArgs& args);
  275. /** Usually you should override dataToJson() instead.
  276. There are very few reasons you should override this (perhaps to lock a mutex while serialization is occurring).
  277. */
  278. virtual json_t* toJson();
  279. /** This is virtual only for the purpose of unserializing legacy data when you could set properties of the `.modules[]` object itself.
  280. Normally you should override dataFromJson().
  281. Remember to call `Module::fromJson(rootJ)` within your overridden method.
  282. */
  283. virtual void fromJson(json_t* rootJ);
  284. virtual json_t* paramsToJson();
  285. virtual void paramsFromJson(json_t* rootJ);
  286. /** Override to store extra internal data in the "data" property of the module's JSON object. */
  287. virtual json_t* dataToJson() {
  288. return NULL;
  289. }
  290. /** Override to load internal data from the "data" property of the module's JSON object.
  291. Not called if "data" property is not present.
  292. */
  293. virtual void dataFromJson(json_t* rootJ) {}
  294. ///////////////////////
  295. // Events
  296. ///////////////////////
  297. // All of these events are thread-safe with process().
  298. struct AddEvent {};
  299. /** Called after adding the module to the Engine.
  300. */
  301. virtual void onAdd(const AddEvent& e) {
  302. // Call deprecated event method by default
  303. onAdd();
  304. }
  305. struct RemoveEvent {};
  306. /** Called before removing the module from the Engine.
  307. */
  308. virtual void onRemove(const RemoveEvent& e) {
  309. // Call deprecated event method by default
  310. onRemove();
  311. }
  312. struct BypassEvent {};
  313. /** Called after bypassing the module.
  314. */
  315. virtual void onBypass(const BypassEvent& e) {}
  316. struct UnBypassEvent {};
  317. /** Called after enabling the module.
  318. */
  319. virtual void onUnBypass(const UnBypassEvent& e) {}
  320. struct PortChangeEvent {
  321. /** True if connecting, false if disconnecting. */
  322. bool connecting;
  323. /** Port::INPUT or Port::OUTPUT */
  324. Port::Type type;
  325. int portId;
  326. };
  327. /** Called after a cable connects to or disconnects from a port.
  328. This event is not called for output ports if a stackable cable was added/removed and did not change the port's connected state.
  329. */
  330. virtual void onPortChange(const PortChangeEvent& e) {}
  331. struct SampleRateChangeEvent {
  332. float sampleRate;
  333. float sampleTime;
  334. };
  335. /** Called after the Engine sample rate changes.
  336. */
  337. virtual void onSampleRateChange(const SampleRateChangeEvent& e) {
  338. // Call deprecated event method by default
  339. onSampleRateChange();
  340. }
  341. struct ExpanderChangeEvent {
  342. /** False for left, true for right. */
  343. bool side;
  344. };
  345. /** Called after an expander is added, removed, or changed on either the left or right side of the Module.
  346. */
  347. virtual void onExpanderChange(const ExpanderChangeEvent& e) {}
  348. struct ResetEvent {};
  349. /** Called when the user resets (initializes) the module.
  350. The default implementation resets all parameters to their default value, so you must call `Module::onReset(e)` in your overridden method if you want to keep this behavior.
  351. */
  352. virtual void onReset(const ResetEvent& e);
  353. struct RandomizeEvent {};
  354. /** Called when the user randomizes the module.
  355. The default implementation randomizes all parameters by default, so you must call `Module::onRandomize(e)` in your overridden method if you want to keep this behavior.
  356. */
  357. virtual void onRandomize(const RandomizeEvent& e);
  358. struct SaveEvent {};
  359. /** Called when the user saves the patch to a file.
  360. If your module uses patch asset storage, make sure all files are saved in this event.
  361. */
  362. virtual void onSave(const SaveEvent& e) {}
  363. struct SetMasterEvent {};
  364. virtual void onSetMaster(const SetMasterEvent& e) {}
  365. struct UnsetMasterEvent {};
  366. virtual void onUnsetMaster(const UnsetMasterEvent& e) {}
  367. /** DEPRECATED. Override `onAdd(e)` instead. */
  368. virtual void onAdd() {}
  369. /** DEPRECATED. Override `onRemove(e)` instead. */
  370. virtual void onRemove() {}
  371. /** DEPRECATED. Override `onReset(e)` instead. */
  372. virtual void onReset() {}
  373. /** DEPRECATED. Override `onRandomize(e)` instead. */
  374. virtual void onRandomize() {}
  375. /** DEPRECATED. Override `onSampleRateChange(e)` instead. */
  376. virtual void onSampleRateChange() {}
  377. bool isBypassed();
  378. INTERNAL void setBypassed(bool bypassed);
  379. INTERNAL const float* meterBuffer();
  380. INTERNAL int meterLength();
  381. INTERNAL int meterIndex();
  382. INTERNAL void doProcess(const ProcessArgs& args);
  383. INTERNAL static void jsonStripIds(json_t* rootJ);
  384. };
  385. } // namespace engine
  386. } // namespace rack