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.

112 lines
3.0KB

  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. /** Called when user clicks Randomize in the module context menu */
  63. virtual void onRandomize() {}
  64. /** Override these to store extra internal data in the "data" property */
  65. virtual json_t *toJson() { return NULL; }
  66. virtual void fromJson(json_t *root) {}
  67. };
  68. struct Wire {
  69. Module *outputModule = NULL;
  70. int outputId;
  71. Module *inputModule = NULL;
  72. int inputId;
  73. void step();
  74. };
  75. void engineInit();
  76. void engineDestroy();
  77. /** Launches engine thread */
  78. void engineStart();
  79. void engineStop();
  80. /** Does not transfer pointer ownership */
  81. void engineAddModule(Module *module);
  82. void engineRemoveModule(Module *module);
  83. /** Does not transfer pointer ownership */
  84. void engineAddWire(Wire *wire);
  85. void engineRemoveWire(Wire *wire);
  86. void engineSetParam(Module *module, int paramId, float value);
  87. void engineSetParamSmooth(Module *module, int paramId, float value);
  88. void engineSetSampleRate(float sampleRate);
  89. float engineGetSampleRate();
  90. /** Returns the inverse of the current sample rate */
  91. float engineGetSampleTime();
  92. extern bool gPaused;
  93. } // namespace rack