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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #pragma once
  2. #include <common.hpp>
  3. #include <string.hpp>
  4. #include <plugin/Model.hpp>
  5. #include <engine/Param.hpp>
  6. #include <engine/Port.hpp>
  7. #include <engine/Light.hpp>
  8. #include <engine/ParamQuantity.hpp>
  9. #include <vector>
  10. #include <jansson.h>
  11. namespace rack {
  12. namespace plugin {
  13. struct Model;
  14. }
  15. namespace engine {
  16. /** DSP processor instance for your module. */
  17. struct Module {
  18. plugin::Model* model = NULL; /** Unique ID for referring to the module in the engine.
  19. Assigned when added to the engine.
  20. */
  21. int id = -1;
  22. /** Arrays of components.
  23. Initialized with config().
  24. */
  25. std::vector<Param> params;
  26. std::vector<Output> outputs;
  27. std::vector<Input> inputs;
  28. std::vector<Light> lights;
  29. std::vector<ParamQuantity*> paramQuantities;
  30. /** Represents a message-passing channel for an adjacent module. */
  31. struct Expander {
  32. /** ID of the expander module, or -1 if nonexistent. */
  33. int moduleId = -1;
  34. /** Pointer to the expander Module, or NULL if nonexistent. */
  35. Module* module = NULL;
  36. /** Double buffer for receiving messages from the expander module.
  37. If you intend to receive messages from an expander, allocate both message buffers with identical blocks of memory (arrays, structs, etc).
  38. Remember to free the buffer in the Module destructor.
  39. Example:
  40. rightExpander.producerMessage = new MyExpanderMessage;
  41. rightExpander.consumerMessage = new MyExpanderMessage;
  42. You must check the expander module's `model` before attempting to write its message buffer.
  43. Once the module is checked, you can reinterpret_cast its producerMessage at no performance cost.
  44. Producer messages are intended to be write-only.
  45. Consumer messages are intended to be read-only.
  46. Once you write a message, set messageFlipRequested to true to request that the messages are flipped at the end of the timestep.
  47. This means that message-passing has 1-sample latency.
  48. 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".
  49. As long as this convention is followed by the other module, this is fine.
  50. */
  51. void* producerMessage = NULL;
  52. void* consumerMessage = NULL;
  53. bool messageFlipRequested = false;
  54. };
  55. Expander leftExpander;
  56. Expander rightExpander;
  57. /** Seconds spent in the process() method, with exponential smoothing.
  58. Only written when CPU timing is enabled, since time measurement is expensive.
  59. */
  60. float cpuTime = 0.f;
  61. /** Whether the Module is skipped from stepping by the engine.
  62. Module subclasses should not read/write this variable.
  63. */
  64. bool bypass = false;
  65. /** Constructs a Module with no params, inputs, outputs, and lights. */
  66. Module();
  67. /** Use config() instead. */
  68. DEPRECATED Module(int numParams, int numInputs, int numOutputs, int numLights = 0) : Module() {
  69. config(numParams, numInputs, numOutputs, numLights);
  70. }
  71. virtual ~Module();
  72. /** Configures the number of Params, Outputs, Inputs, and Lights. */
  73. void config(int numParams, int numInputs, int numOutputs, int numLights = 0);
  74. template <class TParamQuantity = ParamQuantity>
  75. void configParam(int paramId, float minValue, float maxValue, float defaultValue, std::string label = "", std::string unit = "", float displayBase = 0.f, float displayMultiplier = 1.f, float displayOffset = 0.f) {
  76. assert(paramId < (int) params.size() && paramId < (int) paramQuantities.size());
  77. if (paramQuantities[paramId])
  78. delete paramQuantities[paramId];
  79. Param* p = &params[paramId];
  80. p->value = defaultValue;
  81. ParamQuantity* q = new TParamQuantity;
  82. q->module = this;
  83. q->paramId = paramId;
  84. q->minValue = minValue;
  85. q->maxValue = maxValue;
  86. q->defaultValue = defaultValue;
  87. if (!label.empty())
  88. q->label = label;
  89. else
  90. q->label = string::f("#%d", paramId + 1);
  91. q->unit = unit;
  92. q->displayBase = displayBase;
  93. q->displayMultiplier = displayMultiplier;
  94. q->displayOffset = displayOffset;
  95. paramQuantities[paramId] = q;
  96. }
  97. struct ProcessArgs {
  98. float sampleRate;
  99. float sampleTime;
  100. };
  101. /** Advances the module by one audio sample.
  102. Override this method to read Inputs and Params and to write Outputs and Lights.
  103. */
  104. virtual void process(const ProcessArgs& args) {
  105. #pragma GCC diagnostic push
  106. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  107. step();
  108. #pragma GCC diagnostic pop
  109. }
  110. /** Override process(const ProcessArgs &args) instead. */
  111. DEPRECATED virtual void step() {}
  112. /** Called when the engine sample rate is changed. */
  113. virtual void onSampleRateChange() {}
  114. /** Called when user clicks Initialize in the module context menu. */
  115. virtual void onReset() {}
  116. /** Called when user clicks Randomize in the module context menu. */
  117. virtual void onRandomize() {}
  118. /** Called when the Module is added to the Engine */
  119. virtual void onAdd() {}
  120. /** Called when the Module is removed from the Engine */
  121. virtual void onRemove() {}
  122. json_t* toJson();
  123. void fromJson(json_t* rootJ);
  124. /** Override to store extra internal data in the "data" property of the module's JSON object. */
  125. virtual json_t* dataToJson() {
  126. return NULL;
  127. }
  128. virtual void dataFromJson(json_t* root) {}
  129. };
  130. } // namespace engine
  131. } // namespace rack