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.

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