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.

113 lines
2.8KB

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