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.

114 lines
2.9KB

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