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.

122 lines
3.4KB

  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. struct Module {
  17. plugin::Model *model = NULL;
  18. /** Automatically generated by the engine. */
  19. int id = -1;
  20. /** Arrays of components */
  21. std::vector<Param> params;
  22. std::vector<Output> outputs;
  23. std::vector<Input> inputs;
  24. std::vector<Light> lights;
  25. std::vector<ParamQuantity*> paramQuantities;
  26. /** Access to adjacent modules */
  27. int leftModuleId = -1;
  28. Module *leftModule = NULL;
  29. void *leftProducerMessage = NULL;
  30. void *leftConsumerMessage = NULL;
  31. int rightModuleId = -1;
  32. Module *rightModule = NULL;
  33. void *rightProducerMessage = NULL;
  34. void *rightConsumerMessage = NULL;
  35. /** For CPU meter. */
  36. float cpuTime = 0.f;
  37. bool bypass = false;
  38. /** Constructs a Module with no params, inputs, outputs, and lights. */
  39. Module();
  40. /** Use config() instead. */
  41. DEPRECATED Module(int numParams, int numInputs, int numOutputs, int numLights = 0) : Module() {
  42. config(numParams, numInputs, numOutputs, numLights);
  43. }
  44. virtual ~Module();
  45. /** Configures the number of Params, Outputs, Inputs, and Lights. */
  46. void config(int numParams, int numInputs, int numOutputs, int numLights = 0);
  47. template <class TParamQuantity = ParamQuantity>
  48. 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) {
  49. if (paramQuantities[paramId])
  50. delete paramQuantities[paramId];
  51. Param *p = &params[paramId];
  52. p->value = defaultValue;
  53. ParamQuantity *q = new TParamQuantity;
  54. q->module = this;
  55. q->paramId = paramId;
  56. q->minValue = minValue;
  57. q->maxValue = maxValue;
  58. q->defaultValue = defaultValue;
  59. if (!label.empty())
  60. q->label = label;
  61. else
  62. q->label = string::f("#%d", paramId + 1);
  63. q->unit = unit;
  64. q->displayBase = displayBase;
  65. q->displayMultiplier = displayMultiplier;
  66. q->displayOffset = displayOffset;
  67. paramQuantities[paramId] = q;
  68. }
  69. struct ProcessArgs {
  70. float sampleRate;
  71. float sampleTime;
  72. };
  73. /** Advances the module by one audio sample.
  74. Override this method to read Inputs and Params and to write Outputs and Lights.
  75. */
  76. virtual void process(const ProcessArgs &args) {
  77. #pragma GCC diagnostic push
  78. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  79. step();
  80. #pragma GCC diagnostic pop
  81. }
  82. /** Override process(const ProcessArgs &args) instead. */
  83. DEPRECATED virtual void step() {}
  84. /** Called when the engine sample rate is changed. */
  85. virtual void onSampleRateChange() {}
  86. /** Called when user clicks Initialize in the module context menu. */
  87. virtual void onReset() {}
  88. /** Called when user clicks Randomize in the module context menu. */
  89. virtual void onRandomize() {}
  90. /** Called when the Module is added to the Engine */
  91. virtual void onAdd() {}
  92. /** Called when the Module is removed from the Engine */
  93. virtual void onRemove() {}
  94. json_t *toJson();
  95. void fromJson(json_t *rootJ);
  96. /** Override to store extra internal data in the "data" property of the module's JSON object. */
  97. virtual json_t *dataToJson() { return NULL; }
  98. virtual void dataFromJson(json_t *root) {}
  99. };
  100. } // namespace engine
  101. } // namespace rack