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.

135 lines
4.4KB

  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 estimated timestamp corresponding to the current frame, based on the timestamp of when step() was last called.
  41. Calculated by `stepTime + framesSinceStep / sampleRate`.
  42. */
  43. int64_t getFrameTime();
  44. /** Returns the frame when step() was last called.
  45. */
  46. int64_t getStepFrame();
  47. /** Returns the timestamp in nanoseconds when step() was last called.
  48. */
  49. int64_t getStepTime();
  50. /** Returns the total number of frames in the current step() call.
  51. */
  52. int getStepFrames();
  53. /** Returns the total time that step() is advancing, in nanoseconds.
  54. Calculated by `stepFrames / sampleRate`.
  55. */
  56. int64_t getStepDuration();
  57. // Modules
  58. size_t getNumModules();
  59. /** Fills `moduleIds` with up to `len` module IDs in the rack.
  60. Returns the number of IDs written.
  61. This C-like method does no allocations. The vector C++ version below does.
  62. */
  63. size_t getModuleIds(int64_t* moduleIds, size_t len);
  64. std::vector<int64_t> getModuleIds();
  65. /** Adds a module to the rack engine.
  66. The module ID must not be taken by another module.
  67. If the module ID is -1, an ID is automatically assigned.
  68. Does not transfer pointer ownership.
  69. */
  70. void addModule(Module* module);
  71. void removeModule(Module* module);
  72. Module* getModule(int64_t moduleId);
  73. void resetModule(Module* module);
  74. void randomizeModule(Module* module);
  75. void bypassModule(Module* module, bool bypass);
  76. /** Serializes/deserializes with locking, ensuring that Module::process() is not called during toJson()/fromJson().
  77. */
  78. json_t* moduleToJson(Module* module);
  79. void moduleFromJson(Module* module, json_t* rootJ);
  80. // Cables
  81. size_t getNumCables();
  82. size_t getCableIds(int64_t* cableIds, size_t len);
  83. std::vector<int64_t> getCableIds();
  84. /** Adds a cable to the rack engine.
  85. The cable ID must not be taken by another cable.
  86. If the cable ID is -1, an ID is automatically assigned.
  87. Does not transfer pointer ownership.
  88. */
  89. void addCable(Cable* cable);
  90. void removeCable(Cable* cable);
  91. Cable* getCable(int64_t cableId);
  92. // Params
  93. void setParam(Module* module, int paramId, float value);
  94. float getParam(Module* module, int paramId);
  95. /** Requests the parameter to smoothly change toward `value`.
  96. */
  97. void setSmoothParam(Module* module, int paramId, float value);
  98. /** Returns the target value before smoothing.
  99. */
  100. float getSmoothParam(Module* module, int paramId);
  101. // ParamHandles
  102. void addParamHandle(ParamHandle* paramHandle);
  103. void removeParamHandle(ParamHandle* paramHandle);
  104. /** Returns the unique ParamHandle for the given paramId
  105. */
  106. ParamHandle* getParamHandle(int64_t moduleId, int paramId);
  107. /** Use getParamHandle(moduleId, paramId) instead. */
  108. DEPRECATED ParamHandle* getParamHandle(Module* module, int paramId);
  109. /** Sets the ParamHandle IDs and module pointer.
  110. If `overwrite` is true and another ParamHandle points to the same param, unsets that one and replaces it with the given handle.
  111. */
  112. void updateParamHandle(ParamHandle* paramHandle, int64_t moduleId, int paramId, bool overwrite = true);
  113. json_t* toJson();
  114. void fromJson(json_t* rootJ);
  115. };
  116. } // namespace engine
  117. } // namespace rack