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.

121 lines
3.7KB

  1. #pragma once
  2. #include <vector>
  3. #include <common.hpp>
  4. #include <engine/Module.hpp>
  5. #include <engine/Cable.hpp>
  6. #include <engine/ParamHandle.hpp>
  7. namespace rack {
  8. namespace engine {
  9. /** Manages Modules and Cables and steps them in time.
  10. All methods are thread-safe and can safely be called from anywhere.
  11. The methods clear, addModule, removeModule, moduleToJson, moduleFromJson, addCable, removeCable, addParamHandle, removeParamHandle, toJson, and fromJson cannot be run simultaneously with any other Engine method.
  12. Calling these methods inside any Engine method will result in a deadlock.
  13. */
  14. struct Engine {
  15. struct Internal;
  16. Internal* internal;
  17. Engine();
  18. ~Engine();
  19. /** Removes all modules and cables. */
  20. void clear();
  21. /** Advances the engine by `frames` frames.
  22. Only call this method from the primary module.
  23. */
  24. void step(int frames);
  25. void setPrimaryModule(Module* module);
  26. Module* getPrimaryModule();
  27. /** Returns the sample rate used by the engine for stepping each module.
  28. */
  29. float getSampleRate();
  30. /** Returns the inverse of the current sample rate.
  31. */
  32. float getSampleTime();
  33. /** Causes worker threads to block on a mutex instead of spinlock.
  34. Call this in your Module::step() method to hint that the operation will take more than ~0.1 ms.
  35. */
  36. void yieldWorkers();
  37. /** Returns the number of audio samples since the Engine's first sample.
  38. */
  39. int64_t getFrame();
  40. /** Returns the frame when step() was last called.
  41. */
  42. int64_t getStepFrame();
  43. /** Returns the timestamp in nanoseconds when step() was last called.
  44. */
  45. int64_t getStepTime();
  46. /** Returns the total number of frames in the current step() call.
  47. */
  48. int getStepFrames();
  49. // Modules
  50. size_t getNumModules();
  51. void getModuleIds(int* moduleIds, int len);
  52. std::vector<int> getModuleIds();
  53. /** Adds a module to the rack engine.
  54. The module ID must not be taken by another module.
  55. If the module ID is -1, an ID is automatically assigned.
  56. Does not transfer pointer ownership.
  57. */
  58. void addModule(Module* module);
  59. void removeModule(Module* module);
  60. Module* getModule(int moduleId);
  61. void resetModule(Module* module);
  62. void randomizeModule(Module* module);
  63. void bypassModule(Module* module, bool bypassed);
  64. /** Serializes/deserializes with locking, ensuring that Module::process() is not called during toJson()/fromJson().
  65. */
  66. json_t* moduleToJson(Module* module);
  67. void moduleFromJson(Module* module, json_t* rootJ);
  68. // Cables
  69. /** Adds a cable to the rack engine.
  70. The cable ID must not be taken by another cable.
  71. If the cable ID is -1, an ID is automatically assigned.
  72. Does not transfer pointer ownership.
  73. */
  74. void addCable(Cable* cable);
  75. void removeCable(Cable* cable);
  76. Cable* getCable(int cableId);
  77. // Params
  78. void setParam(Module* module, int paramId, float value);
  79. float getParam(Module* module, int paramId);
  80. /** Requests the parameter to smoothly change toward `value`.
  81. */
  82. void setSmoothParam(Module* module, int paramId, float value);
  83. /** Returns the target value before smoothing.
  84. */
  85. float getSmoothParam(Module* module, int paramId);
  86. // ParamHandles
  87. void addParamHandle(ParamHandle* paramHandle);
  88. void removeParamHandle(ParamHandle* paramHandle);
  89. /** Returns the unique ParamHandle for the given paramId
  90. */
  91. ParamHandle* getParamHandle(int moduleId, int paramId);
  92. /** Use getParamHandle(int, int) instead.
  93. */
  94. DEPRECATED ParamHandle* getParamHandle(Module* module, int paramId);
  95. /** Sets the ParamHandle IDs and module pointer.
  96. If `overwrite` is true and another ParamHandle points to the same param, unsets that one and replaces it with the given handle.
  97. */
  98. void updateParamHandle(ParamHandle* paramHandle, int moduleId, int paramId, bool overwrite = true);
  99. json_t* toJson();
  100. void fromJson(json_t* rootJ);
  101. };
  102. } // namespace engine
  103. } // namespace rack