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.

104 lines
2.5KB

  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. void setBrightness(float brightness) {
  29. value = brightness * brightness;
  30. }
  31. void setBrightnessSmooth(float brightness);
  32. };
  33. struct Module {
  34. std::vector<Param> params;
  35. std::vector<Input> inputs;
  36. std::vector<Output> outputs;
  37. std::vector<Light> lights;
  38. /** For CPU usage meter */
  39. float cpuTime = 0.0;
  40. /** Deprecated, use constructor below this one */
  41. Module() {}
  42. /** Constructs Module with a fixed number of params, inputs, and outputs */
  43. Module(int numParams, int numInputs, int numOutputs, int numLights = 0) {
  44. params.resize(numParams);
  45. inputs.resize(numInputs);
  46. outputs.resize(numOutputs);
  47. lights.resize(numLights);
  48. }
  49. virtual ~Module() {}
  50. /** Advances the module by 1 audio frame with duration 1.0 / gSampleRate */
  51. virtual void step() {}
  52. virtual void onSampleRateChange() {}
  53. /** Override these to store extra internal data in the "data" property */
  54. virtual json_t *toJson() { return NULL; }
  55. virtual void fromJson(json_t *root) {}
  56. /** Override these to implement spacial behavior when user clicks Initialize and Randomize */
  57. virtual void reset() {}
  58. virtual void randomize() {}
  59. /** Deprecated */
  60. virtual void initialize() final {}
  61. };
  62. struct Wire {
  63. Module *outputModule = NULL;
  64. int outputId;
  65. Module *inputModule = NULL;
  66. int inputId;
  67. void step();
  68. };
  69. void engineInit();
  70. void engineDestroy();
  71. /** Launches engine thread */
  72. void engineStart();
  73. void engineStop();
  74. /** Does not transfer pointer ownership */
  75. void engineAddModule(Module *module);
  76. void engineRemoveModule(Module *module);
  77. /** Does not transfer pointer ownership */
  78. void engineAddWire(Wire *wire);
  79. void engineRemoveWire(Wire *wire);
  80. void engineSetParam(Module *module, int paramId, float value);
  81. void engineSetParamSmooth(Module *module, int paramId, float value);
  82. void engineSetSampleRate(float sampleRate);
  83. float engineGetSampleRate();
  84. extern bool gPaused;
  85. } // namespace rack