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.

engine.hpp 2.7KB

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