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.

124 lines
3.2KB

  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. void setBrightnessSmooth(float brightness);
  17. };
  18. struct Input {
  19. /** Voltage of the port, zero if not plugged in. Read-only by Module */
  20. float value = 0.0;
  21. /** Whether a wire is plugged in */
  22. bool active = false;
  23. Light plugLights[2];
  24. /** Returns the value if a wire is plugged in, otherwise returns the given default value */
  25. float normalize(float normalValue) {
  26. return active ? value : normalValue;
  27. }
  28. };
  29. struct Output {
  30. /** Voltage of the port. Write-only by Module */
  31. float value = 0.0;
  32. /** Whether a wire is plugged in */
  33. bool active = false;
  34. Light plugLights[2];
  35. };
  36. struct Module {
  37. std::vector<Param> params;
  38. std::vector<Input> inputs;
  39. std::vector<Output> outputs;
  40. std::vector<Light> lights;
  41. /** For CPU usage meter */
  42. float cpuTime = 0.0;
  43. /** Deprecated, use constructor below this one */
  44. Module() DEPRECATED {}
  45. /** Constructs Module with a fixed number of params, inputs, and outputs */
  46. Module(int numParams, int numInputs, int numOutputs, int numLights = 0) {
  47. params.resize(numParams);
  48. inputs.resize(numInputs);
  49. outputs.resize(numOutputs);
  50. lights.resize(numLights);
  51. }
  52. virtual ~Module() {}
  53. /** Advances the module by 1 audio frame with duration 1.0 / gSampleRate */
  54. virtual void step() {}
  55. /** Called when the engine sample rate is changed */
  56. virtual void onSampleRateChange() {}
  57. /** Called when module is created by the Add Module popup, cloning, or when loading a patch or autosave */
  58. virtual void onCreate() {}
  59. /** Called when user explicitly deletes the module, not when Rack is closed or a new patch is loaded */
  60. virtual void onDelete() {}
  61. /** Called when user clicks Initialize in the module context menu */
  62. virtual void onReset() {
  63. // Call deprecated method
  64. reset();
  65. }
  66. /** Called when user clicks Randomize in the module context menu */
  67. virtual void onRandomize() {
  68. // Call deprecated method
  69. randomize();
  70. }
  71. /** Override these to store extra internal data in the "data" property */
  72. virtual json_t *toJson() { return NULL; }
  73. virtual void fromJson(json_t *root) {}
  74. /** Deprecated */
  75. virtual void reset() {}
  76. /** Deprecated */
  77. virtual void randomize() {}
  78. };
  79. struct Wire {
  80. Module *outputModule = NULL;
  81. int outputId;
  82. Module *inputModule = NULL;
  83. int inputId;
  84. void step();
  85. };
  86. void engineInit();
  87. void engineDestroy();
  88. /** Launches engine thread */
  89. void engineStart();
  90. void engineStop();
  91. /** Does not transfer pointer ownership */
  92. void engineAddModule(Module *module);
  93. void engineRemoveModule(Module *module);
  94. /** Does not transfer pointer ownership */
  95. void engineAddWire(Wire *wire);
  96. void engineRemoveWire(Wire *wire);
  97. void engineSetParam(Module *module, int paramId, float value);
  98. void engineSetParamSmooth(Module *module, int paramId, float value);
  99. void engineSetSampleRate(float sampleRate);
  100. float engineGetSampleRate();
  101. /** Returns the inverse of the current sample rate */
  102. float engineGetSampleTime();
  103. extern bool gPaused;
  104. } // namespace rack