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.

101 lines
2.4KB

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