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.

117 lines
3.0KB

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