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.

93 lines
2.3KB

  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 Module {
  26. std::vector<Param> params;
  27. std::vector<Input> inputs;
  28. std::vector<Output> outputs;
  29. /** For CPU usage meter */
  30. float cpuTime = 0.0;
  31. /** Deprecated, use constructor below this one */
  32. Module() {}
  33. /** Constructs Module with a fixed number of params, inputs, and outputs */
  34. Module(int numParams, int numInputs, int numOutputs) {
  35. params.resize(numParams);
  36. inputs.resize(numInputs);
  37. outputs.resize(numOutputs);
  38. }
  39. virtual ~Module() {}
  40. /** Advances the module by 1 audio frame with duration 1.0 / gSampleRate */
  41. virtual void step() {}
  42. virtual void onSampleRateChange() {}
  43. /** Override these to store extra internal data in the "data" property */
  44. virtual json_t *toJson() { return NULL; }
  45. virtual void fromJson(json_t *root) {}
  46. /** Override these to implement spacial behavior when user clicks Initialize and Randomize */
  47. virtual void reset() {}
  48. virtual void randomize() {}
  49. /** Deprecated */
  50. virtual void initialize() final {}
  51. };
  52. struct Wire {
  53. Module *outputModule = NULL;
  54. int outputId;
  55. Module *inputModule = NULL;
  56. int inputId;
  57. void step();
  58. };
  59. void engineInit();
  60. void engineDestroy();
  61. /** Launches engine thread */
  62. void engineStart();
  63. void engineStop();
  64. /** Does not transfer pointer ownership */
  65. void engineAddModule(Module *module);
  66. void engineRemoveModule(Module *module);
  67. /** Does not transfer pointer ownership */
  68. void engineAddWire(Wire *wire);
  69. void engineRemoveWire(Wire *wire);
  70. void engineSetParam(Module *module, int paramId, float value);
  71. void engineSetParamSmooth(Module *module, int paramId, float value);
  72. void engineSetSampleRate(float sampleRate);
  73. float engineGetSampleRate();
  74. extern bool gPaused;
  75. } // namespace rack