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.

134 lines
3.7KB

  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. virtual void step() {}
  58. /** Called when the engine sample rate is changed
  59. */
  60. virtual void onSampleRateChange() {}
  61. /** Called when module is created by the Add Module popup, cloning, or when loading a patch or autosave */
  62. virtual void onCreate() {}
  63. /** Called when user explicitly deletes the module, not when Rack is closed or a new patch is loaded */
  64. virtual void onDelete() {}
  65. /** Called when user clicks Initialize in the module context menu */
  66. virtual void onReset() {
  67. // Call deprecated method
  68. reset();
  69. }
  70. /** Called when user clicks Randomize in the module context menu */
  71. virtual void onRandomize() {
  72. // Call deprecated method
  73. randomize();
  74. }
  75. /** Override these to store extra internal data in the "data" property */
  76. virtual json_t *toJson() { return NULL; }
  77. virtual void fromJson(json_t *root) {}
  78. /** Deprecated */
  79. virtual void reset() {}
  80. /** Deprecated */
  81. virtual void randomize() {}
  82. };
  83. struct Wire {
  84. Module *outputModule = NULL;
  85. int outputId;
  86. Module *inputModule = NULL;
  87. int inputId;
  88. void step();
  89. };
  90. void engineInit();
  91. void engineDestroy();
  92. /** Launches engine thread */
  93. void engineStart();
  94. void engineStop();
  95. /** Does not transfer pointer ownership */
  96. void engineAddModule(Module *module);
  97. void engineRemoveModule(Module *module);
  98. /** Does not transfer pointer ownership */
  99. void engineAddWire(Wire *wire);
  100. void engineRemoveWire(Wire *wire);
  101. void engineSetParam(Module *module, int paramId, float value);
  102. void engineSetParamSmooth(Module *module, int paramId, float value);
  103. void engineSetSampleRate(float sampleRate);
  104. float engineGetSampleRate();
  105. /** Returns the inverse of the current sample rate */
  106. float engineGetSampleTime();
  107. extern bool gPaused;
  108. /** Plugins should not manipulate other modules or wires unless that is the entire purpose of the module.
  109. Your plugin needs to have a clear purpose for manipulating other modules and wires and must be done with a good UX.
  110. */
  111. extern std::vector<Module*> gModules;
  112. extern std::vector<Wire*> gWires;
  113. extern bool gCpuMeters;
  114. } // namespace rack