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.

64 lines
1.8KB

  1. #pragma once
  2. #include "common.hpp"
  3. #include "string.hpp"
  4. #include "engine/Param.hpp"
  5. #include "engine/Input.hpp"
  6. #include "engine/Output.hpp"
  7. #include "engine/Light.hpp"
  8. #include <vector>
  9. #include <jansson.h>
  10. namespace rack {
  11. struct Module {
  12. std::vector<Param> params;
  13. std::vector<Input> inputs;
  14. std::vector<Output> outputs;
  15. std::vector<Light> lights;
  16. /** For CPU usage meter */
  17. float cpuTime = 0.f;
  18. /** Constructs a Module with no params, inputs, outputs, and lights */
  19. Module() {}
  20. /** Constructs a Module with a fixed number of params, inputs, outputs, and lights */
  21. Module(int numParams, int numInputs, int numOutputs, int numLights = 0) {
  22. setup(numParams, numInputs, numOutputs, numLights);
  23. }
  24. virtual ~Module() {}
  25. void setup(int numParams, int numInputs, int numOutputs, int numLights = 0) {
  26. params.resize(numParams);
  27. // Create default param labels
  28. for (int i = 0; i < numParams; i++) {
  29. params[i].label = string::f("#%d", i + 1);
  30. }
  31. inputs.resize(numInputs);
  32. outputs.resize(numOutputs);
  33. lights.resize(numLights);
  34. }
  35. /** Advances the module by 1 audio frame with duration 1.0 / gSampleRate
  36. Override this method to read inputs and params, and to write outputs and lights.
  37. */
  38. virtual void step() {}
  39. /** Called when the engine sample rate is changed */
  40. virtual void onSampleRateChange() {}
  41. /** Called when user clicks Initialize in the module context menu */
  42. virtual void onReset() {}
  43. /** Called when user clicks Randomize in the module context menu */
  44. virtual void onRandomize() {}
  45. json_t *toJson();
  46. void fromJson(json_t *rootJ);
  47. /** Override these to store extra internal data in the "data" property of the module's JSON object */
  48. virtual json_t *dataToJson() { return NULL; }
  49. virtual void dataFromJson(json_t *root) {}
  50. };
  51. } // namespace rack