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.

120 lines
3.3KB

  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 > 0.f) ? brightness * brightness : 0.f;
  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. /** Called when module is created by the Add Module popup, cloning, or when loading a patch or autosave */
  64. virtual void onCreate() {}
  65. /** Called when user explicitly deletes the module, not when Rack is closed or a new patch is loaded */
  66. virtual void onDelete() {}
  67. /** Called when user clicks Initialize in the module context menu */
  68. virtual void onReset() {}
  69. /** Called when user clicks Randomize in the module context menu */
  70. virtual void onRandomize() {}
  71. /** Override these to store extra internal data in the "data" property */
  72. virtual json_t *toJson() { return NULL; }
  73. virtual void fromJson(json_t *root) {}
  74. };
  75. struct Wire {
  76. Module *outputModule = NULL;
  77. int outputId;
  78. Module *inputModule = NULL;
  79. int inputId;
  80. void step();
  81. };
  82. void engineInit();
  83. void engineDestroy();
  84. /** Launches engine thread */
  85. void engineStart();
  86. void engineStop();
  87. /** Does not transfer pointer ownership */
  88. void engineAddModule(Module *module);
  89. void engineRemoveModule(Module *module);
  90. /** Does not transfer pointer ownership */
  91. void engineAddWire(Wire *wire);
  92. void engineRemoveWire(Wire *wire);
  93. void engineSetParam(Module *module, int paramId, float value);
  94. void engineSetParamSmooth(Module *module, int paramId, float value);
  95. void engineSetSampleRate(float sampleRate);
  96. float engineGetSampleRate();
  97. /** Returns the inverse of the current sample rate */
  98. float engineGetSampleTime();
  99. extern bool gPaused;
  100. } // namespace rack