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.

141 lines
3.6KB

  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. #ifdef USE_VST2
  41. // assigned when module is added to engine (see engine.cpp:engineAddModule())
  42. int vst2_unique_param_base_id;
  43. #endif // USE_VST2
  44. std::vector<Param> params;
  45. std::vector<Input> inputs;
  46. std::vector<Output> outputs;
  47. std::vector<Light> lights;
  48. /** For CPU usage meter */
  49. float cpuTime = 0.0;
  50. /** Constructs a Module with no params, inputs, outputs, and lights */
  51. Module() {}
  52. /** Constructs a Module with a fixed number of params, inputs, outputs, and lights */
  53. Module(int numParams, int numInputs, int numOutputs, int numLights = 0) {
  54. params.resize(numParams);
  55. inputs.resize(numInputs);
  56. outputs.resize(numOutputs);
  57. lights.resize(numLights);
  58. }
  59. virtual ~Module() {}
  60. /** Advances the module by 1 audio frame with duration 1.0 / gSampleRate
  61. Override this method to read inputs and params, and to write outputs and lights.
  62. */
  63. virtual void step() {}
  64. /** Called when the engine sample rate is changed
  65. */
  66. virtual void onSampleRateChange() {}
  67. /** Deprecated */
  68. virtual void onCreate() {}
  69. /** Deprecated */
  70. virtual void onDelete() {}
  71. /** Called when user clicks Initialize in the module context menu */
  72. virtual void onReset() {
  73. // Call deprecated method
  74. reset();
  75. }
  76. /** Called when user clicks Randomize in the module context menu */
  77. virtual void onRandomize() {
  78. // Call deprecated method
  79. randomize();
  80. }
  81. /** Override these to store extra internal data in the "data" property of the module's JSON object */
  82. virtual json_t *toJson() { return NULL; }
  83. virtual void fromJson(json_t *root) {}
  84. /** Deprecated */
  85. virtual void reset() {}
  86. /** Deprecated */
  87. virtual void randomize() {}
  88. };
  89. struct Wire {
  90. Module *outputModule = NULL;
  91. int outputId;
  92. Module *inputModule = NULL;
  93. int inputId;
  94. void step();
  95. };
  96. void engineInit();
  97. void engineDestroy();
  98. /** Launches engine thread */
  99. void engineStart();
  100. void engineStop();
  101. /** Does not transfer pointer ownership */
  102. void engineAddModule(Module *module);
  103. void engineRemoveModule(Module *module);
  104. /** Does not transfer pointer ownership */
  105. void engineAddWire(Wire *wire);
  106. void engineRemoveWire(Wire *wire);
  107. #ifdef USE_VST2
  108. void engineSetParam(Module *module, int paramId, float value, bool bVSTAutomate = true);
  109. #else
  110. void engineSetParam(Module *module, int paramId, float value);
  111. #endif
  112. void engineSetParamSmooth(Module *module, int paramId, float value);
  113. void engineSetSampleRate(float sampleRate);
  114. float engineGetSampleRate();
  115. /** Returns the inverse of the current sample rate */
  116. float engineGetSampleTime();
  117. } // namespace rack