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.

123 lines
3.1KB

  1. #pragma once
  2. #include <vector>
  3. #include "util.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. virtual void onSampleRateChange() {}
  56. /** Called when module is created by the Add Module popup, cloning, or when loading a patch or autosave */
  57. virtual void onCreate() {}
  58. /** Called when user explicitly deletes the module, not when Rack is closed or a new patch is loaded */
  59. virtual void onDelete() {}
  60. /** Called when user clicks Initialize in the module context menu */
  61. virtual void onReset() {
  62. // Call deprecated method
  63. reset();
  64. }
  65. /** Called when user clicks Randomize in the module context menu */
  66. virtual void onRandomize() {
  67. // Call deprecated method
  68. randomize();
  69. }
  70. /** Override these to store extra internal data in the "data" property */
  71. virtual json_t *toJson() { return NULL; }
  72. virtual void fromJson(json_t *root) {}
  73. /** Deprecated */
  74. virtual void reset() {}
  75. /** Deprecated */
  76. virtual void randomize() {}
  77. };
  78. struct Wire {
  79. Module *outputModule = NULL;
  80. int outputId;
  81. Module *inputModule = NULL;
  82. int inputId;
  83. void step();
  84. };
  85. void engineInit();
  86. void engineDestroy();
  87. /** Launches engine thread */
  88. void engineStart();
  89. void engineStop();
  90. /** Does not transfer pointer ownership */
  91. void engineAddModule(Module *module);
  92. void engineRemoveModule(Module *module);
  93. /** Does not transfer pointer ownership */
  94. void engineAddWire(Wire *wire);
  95. void engineRemoveWire(Wire *wire);
  96. void engineSetParam(Module *module, int paramId, float value);
  97. void engineSetParamSmooth(Module *module, int paramId, float value);
  98. void engineSetSampleRate(float sampleRate);
  99. float engineGetSampleRate();
  100. /** Returns the inverse of the current sample rate */
  101. float engineGetSampleTime();
  102. extern bool gPaused;
  103. } // namespace rack