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.

107 lines
2.6KB

  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 Input {
  10. /** Voltage of the port, zero if not plugged in. Read-only by Module */
  11. float value = 0.0;
  12. /** Whether a wire is plugged in */
  13. bool active = false;
  14. /** Returns the value if a wire is plugged in, otherwise returns the given default value */
  15. float normalize(float normalValue) {
  16. return active ? value : normalValue;
  17. }
  18. };
  19. struct Output {
  20. /** Voltage of the port. Write-only by Module */
  21. float value = 0.0;
  22. /** Whether a wire is plugged in */
  23. bool active = false;
  24. };
  25. struct Light {
  26. /** The square of the brightness value */
  27. float value = 0.0;
  28. float getBrightness();
  29. void setBrightness(float brightness) {
  30. value = brightness * brightness;
  31. }
  32. void setBrightnessSmooth(float brightness);
  33. };
  34. struct Module {
  35. std::vector<Param> params;
  36. std::vector<Input> inputs;
  37. std::vector<Output> outputs;
  38. std::vector<Light> lights;
  39. /** For CPU usage meter */
  40. float cpuTime = 0.0;
  41. /** Deprecated, use constructor below this one */
  42. Module() {}
  43. /** Constructs Module with a fixed number of params, inputs, and outputs */
  44. Module(int numParams, int numInputs, int numOutputs, int numLights = 0) {
  45. params.resize(numParams);
  46. inputs.resize(numInputs);
  47. outputs.resize(numOutputs);
  48. lights.resize(numLights);
  49. }
  50. virtual ~Module() {}
  51. /** Advances the module by 1 audio frame with duration 1.0 / gSampleRate */
  52. virtual void step() {}
  53. virtual void onSampleRateChange() {}
  54. /** Override these to implement spacial behavior when user clicks Initialize and Randomize */
  55. virtual void reset() {}
  56. virtual void randomize() {}
  57. /** Deprecated */
  58. virtual void initialize() final {}
  59. /** Override these to store extra internal data in the "data" property */
  60. virtual json_t *toJson() { return NULL; }
  61. virtual void fromJson(json_t *root) {}
  62. };
  63. struct Wire {
  64. Module *outputModule = NULL;
  65. int outputId;
  66. Module *inputModule = NULL;
  67. int inputId;
  68. void step();
  69. };
  70. void engineInit();
  71. void engineDestroy();
  72. /** Launches engine thread */
  73. void engineStart();
  74. void engineStop();
  75. /** Does not transfer pointer ownership */
  76. void engineAddModule(Module *module);
  77. void engineRemoveModule(Module *module);
  78. /** Does not transfer pointer ownership */
  79. void engineAddWire(Wire *wire);
  80. void engineRemoveWire(Wire *wire);
  81. void engineSetParam(Module *module, int paramId, float value);
  82. void engineSetParamSmooth(Module *module, int paramId, float value);
  83. void engineSetSampleRate(float sampleRate);
  84. float engineGetSampleRate();
  85. /** Returns the inverse of the current sample rate */
  86. float engineGetSampleTime();
  87. extern bool gPaused;
  88. } // namespace rack