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.

89 lines
2.1KB

  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. /** Override these to store extra internal data in the "data" property */
  43. virtual json_t *toJson() { return NULL; }
  44. virtual void fromJson(json_t *root) {}
  45. /** Override these to implement behavior when user clicks Initialize and Randomize */
  46. virtual void initialize() {}
  47. virtual void randomize() {}
  48. };
  49. struct Wire {
  50. Module *outputModule = NULL;
  51. int outputId;
  52. Module *inputModule = NULL;
  53. int inputId;
  54. void step();
  55. };
  56. void engineInit();
  57. void engineDestroy();
  58. /** Launches engine thread */
  59. void engineStart();
  60. void engineStop();
  61. /** Does not transfer pointer ownership */
  62. void engineAddModule(Module *module);
  63. void engineRemoveModule(Module *module);
  64. /** Does not transfer pointer ownership */
  65. void engineAddWire(Wire *wire);
  66. void engineRemoveWire(Wire *wire);
  67. void engineSetParam(Module *module, int paramId, float value);
  68. void engineSetParamSmooth(Module *module, int paramId, float value);
  69. extern float gSampleRate;
  70. extern bool gPaused;
  71. } // namespace rack