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.

144 lines
3.8KB

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