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.4KB

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