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.

169 lines
4.5KB

  1. #pragma once
  2. #include <vector>
  3. #include "common.hpp"
  4. #include <jansson.h>
  5. namespace rack {
  6. struct Param {
  7. float value = 0.f;
  8. float minValue = 0.f;
  9. float maxValue = 1.f;
  10. float defaultValue = 0.f;
  11. // For formatting/displaying the value
  12. /** Set to 0 for linear, nonzero for exponential */
  13. float displayBase = 0.f;
  14. float displayMultiplier = 1.f;
  15. int displayPrecision = 2;
  16. std::string label;
  17. std::string unit;
  18. // TODO Change this horrible method name
  19. void setup(float minValue, float maxValue, float defaultValue, std::string label = "", std::string unit = "", int displayPrecision = 2) {
  20. this->value = defaultValue;
  21. this->minValue = minValue;
  22. this->maxValue = maxValue;
  23. this->defaultValue = defaultValue;
  24. this->label = label;
  25. this->unit = unit;
  26. this->displayPrecision = displayPrecision;
  27. }
  28. json_t *toJson();
  29. void fromJson(json_t *rootJ);
  30. };
  31. struct Light {
  32. /** The square of the brightness value */
  33. float value = 0.0;
  34. float getBrightness();
  35. void setBrightness(float brightness) {
  36. value = (brightness > 0.f) ? brightness * brightness : 0.f;
  37. }
  38. /** Emulates slow fall (but immediate rise) of LED brightness.
  39. `frames` rescales the timestep. For example, if your module calls this method every 16 frames, use 16.0.
  40. */
  41. void setBrightnessSmooth(float brightness, float frames = 1.f);
  42. };
  43. struct Input {
  44. /** Voltage of the port, zero if not plugged in. Read-only by Module */
  45. float value = 0.0;
  46. /** Whether a wire is plugged in */
  47. bool active = false;
  48. Light plugLights[2];
  49. /** Returns the value if a wire is plugged in, otherwise returns the given default value */
  50. float normalize(float normalValue) {
  51. return active ? value : normalValue;
  52. }
  53. };
  54. struct Output {
  55. /** Voltage of the port. Write-only by Module */
  56. float value = 0.0;
  57. /** Whether a wire is plugged in */
  58. bool active = false;
  59. Light plugLights[2];
  60. };
  61. struct Module {
  62. std::vector<Param> params;
  63. std::vector<Input> inputs;
  64. std::vector<Output> outputs;
  65. std::vector<Light> lights;
  66. /** For CPU usage meter */
  67. float cpuTime = 0.0;
  68. /** Constructs a Module with no params, inputs, outputs, and lights */
  69. Module() {}
  70. /** Constructs a Module with a fixed number of params, inputs, outputs, and lights */
  71. Module(int numParams, int numInputs, int numOutputs, int numLights = 0) {
  72. params.resize(numParams);
  73. inputs.resize(numInputs);
  74. outputs.resize(numOutputs);
  75. lights.resize(numLights);
  76. }
  77. virtual ~Module() {}
  78. /** Advances the module by 1 audio frame with duration 1.0 / gSampleRate
  79. Override this method to read inputs and params, and to write outputs and lights.
  80. */
  81. virtual void step() {}
  82. /** Called when the engine sample rate is changed
  83. */
  84. virtual void onSampleRateChange() {}
  85. /** Deprecated */
  86. virtual void onCreate() {}
  87. /** Deprecated */
  88. virtual void onDelete() {}
  89. /** Called when user clicks Initialize in the module context menu */
  90. virtual void onReset() {
  91. // Call deprecated method
  92. reset();
  93. }
  94. /** Called when user clicks Randomize in the module context menu */
  95. virtual void onRandomize() {
  96. // Call deprecated method
  97. randomize();
  98. }
  99. /** Override these to store extra internal data in the "data" property of the module's JSON object */
  100. virtual json_t *toJson() { return NULL; }
  101. virtual void fromJson(json_t *root) {}
  102. /** Deprecated */
  103. virtual void reset() {}
  104. /** Deprecated */
  105. virtual void randomize() {}
  106. };
  107. struct Wire {
  108. Module *outputModule = NULL;
  109. int outputId;
  110. Module *inputModule = NULL;
  111. int inputId;
  112. void step();
  113. };
  114. void engineInit();
  115. void engineDestroy();
  116. /** Launches engine thread */
  117. void engineStart();
  118. void engineStop();
  119. /** Does not transfer pointer ownership */
  120. void engineAddModule(Module *module);
  121. void engineRemoveModule(Module *module);
  122. void engineResetModule(Module *module);
  123. void engineRandomizeModule(Module *module);
  124. /** Does not transfer pointer ownership */
  125. void engineAddWire(Wire *wire);
  126. void engineRemoveWire(Wire *wire);
  127. void engineSetParam(Module *module, int paramId, float value);
  128. void engineSetParamSmooth(Module *module, int paramId, float value);
  129. void engineSetSampleRate(float sampleRate);
  130. float engineGetSampleRate();
  131. /** Returns the inverse of the current sample rate */
  132. float engineGetSampleTime();
  133. extern bool gPaused;
  134. /** Plugins should not manipulate other modules or wires unless that is the entire purpose of the module.
  135. Your plugin needs to have a clear purpose for manipulating other modules and wires and must be done with a good UX.
  136. */
  137. extern std::vector<Module*> gModules;
  138. extern std::vector<Wire*> gWires;
  139. extern bool gPowerMeter;
  140. } // namespace rack