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.

Engine.hpp 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /** High-performance classes handling modules and voltage signals between them
  9. */
  10. namespace engine {
  11. /** Manages Modules and Cables and steps them in time.
  12. Engine contains a shared mutex that locks when the Engine state is being read or written (manipulated).
  13. Methods that share-lock (stated in their documentation) can be called simultaneously with other share-locking methods.
  14. Methods that exclusively lock cannot be called simultaneously or recursively with another share-locking or exclusive-locking method.
  15. */
  16. struct Engine {
  17. struct Internal;
  18. Internal* internal;
  19. PRIVATE Engine();
  20. PRIVATE ~Engine();
  21. /** Removes all modules and cables.
  22. Exclusively locks.
  23. */
  24. void clear();
  25. PRIVATE void clear_NoLock();
  26. /** Advances the engine by `frames` frames.
  27. Only call this method from the master module.
  28. Share-locks. Also locks so only one stepBlock() can be called simultaneously or recursively.
  29. */
  30. void stepBlock(int frames);
  31. /** Module does not need to belong to the Engine.
  32. However, Engine will unset the master module when it is removed from the Engine.
  33. NULL will unset the master module.
  34. Exclusively locks.
  35. */
  36. void setMasterModule(Module* module);
  37. void setMasterModule_NoLock(Module* module);
  38. Module* getMasterModule();
  39. /** Returns the sample rate used by the engine for stepping each module.
  40. */
  41. float getSampleRate();
  42. /** Sets the sample rate to step the modules.
  43. Exclusively locks.
  44. */
  45. PRIVATE void setSampleRate(float sampleRate);
  46. /** Sets the sample rate if the sample rate in the settings is "Auto".
  47. Exclusively locks.
  48. */
  49. void setSuggestedSampleRate(float suggestedSampleRate);
  50. /** Returns the inverse of the current sample rate.
  51. */
  52. float getSampleTime();
  53. /** Causes worker threads to block on a mutex instead of spinlock.
  54. Call this in your Module::stepBlock() method to hint that the operation will take more than ~0.1 ms.
  55. */
  56. void yieldWorkers();
  57. /** Returns the number of stepBlock() calls since the Engine was created.
  58. */
  59. int64_t getBlock();
  60. /** Returns the frame counter which increases every sample step.
  61. Not necessarily monotonically increasing. Can be reset at any time.
  62. */
  63. int64_t getFrame();
  64. /** Sets the frame counter.
  65. Useful for when the DAW playhead position jumps to a new position.
  66. Rack plugins and standalone Rack should not call this function.
  67. */
  68. void setFrame(int64_t frame);
  69. /** Returns the frame when stepBlock() was last called.
  70. */
  71. int64_t getBlockFrame();
  72. /** Returns the time in seconds when stepBlock() was last called.
  73. */
  74. double getBlockTime();
  75. /** Returns the total number of frames in the current stepBlock() call.
  76. */
  77. int getBlockFrames();
  78. /** Returns the total time that stepBlock() is advancing, in seconds.
  79. Calculated by `stepFrames / sampleRate`.
  80. */
  81. double getBlockDuration();
  82. /** Returns the average block processing time divided by block time in the last T seconds.
  83. */
  84. double getMeterAverage();
  85. double getMeterMax();
  86. // Modules
  87. size_t getNumModules();
  88. /** Fills `moduleIds` with up to `len` module IDs in the rack.
  89. Returns the number of IDs written.
  90. This C-like method does no allocations. The vector C++ version below does.
  91. Share-locks.
  92. */
  93. size_t getModuleIds(int64_t* moduleIds, size_t len);
  94. /** Returns a vector of module IDs in the rack.
  95. Share-locks.
  96. */
  97. std::vector<int64_t> getModuleIds();
  98. /** Adds a Module to the rack.
  99. The module ID must not be taken by another Module.
  100. If the module ID is -1, an ID is automatically assigned.
  101. Does not transfer pointer ownership.
  102. Exclusively locks.
  103. */
  104. void addModule(Module* module);
  105. /** Removes a Module from the rack.
  106. Exclusively locks.
  107. */
  108. void removeModule(Module* module);
  109. PRIVATE void removeModule_NoLock(Module* module);
  110. /** Checks whether a Module is in the rack.
  111. Share-locks.
  112. */
  113. bool hasModule(Module* module);
  114. /** Returns the Module with the given ID in the rack.
  115. Share-locks.
  116. */
  117. Module* getModule(int64_t moduleId);
  118. Module* getModule_NoLock(int64_t moduleId);
  119. /** Triggers a ResetEvent for the given Module.
  120. Exclusively locks.
  121. */
  122. void resetModule(Module* module);
  123. /** Triggers a RandomizeEvent for the given Module.
  124. Exclusively locks.
  125. */
  126. void randomizeModule(Module* module);
  127. /** Sets the bypassed state and triggers a BypassEvent or UnBypassEvent of the given Module.
  128. Exclusively locks.
  129. */
  130. void bypassModule(Module* module, bool bypassed);
  131. /** Serializes the given Module with locking, ensuring that Module::process() is not called simultaneously.
  132. Share-locks.
  133. */
  134. json_t* moduleToJson(Module* module);
  135. /** Serializes the given Module with locking, ensuring that Module::process() is not called simultaneously.
  136. Exclusively locks.
  137. */
  138. void moduleFromJson(Module* module, json_t* rootJ);
  139. /** Dispatches Save event to a module.
  140. Share-locks.
  141. */
  142. void prepareSaveModule(Module* module);
  143. /** Dispatches Save event to all modules.
  144. Share-locks.
  145. */
  146. void prepareSave();
  147. // Cables
  148. size_t getNumCables();
  149. /** Fills `cableIds` with up to `len` cable IDs in the rack.
  150. Returns the number of IDs written.
  151. This C-like method does no allocations. The vector C++ version below does.
  152. Share-locks.
  153. */
  154. size_t getCableIds(int64_t* cableIds, size_t len);
  155. /** Returns a vector of cable IDs in the rack.
  156. Share-locks.
  157. */
  158. std::vector<int64_t> getCableIds();
  159. /** Adds a Cable to the rack.
  160. The cable ID must not be taken by another cable.
  161. If the cable ID is -1, an ID is automatically assigned.
  162. Does not transfer pointer ownership.
  163. Exclusively locks.
  164. */
  165. void addCable(Cable* cable);
  166. /** Removes a Cable from the rack.
  167. Exclusively locks.
  168. */
  169. void removeCable(Cable* cable);
  170. PRIVATE void removeCable_NoLock(Cable* cable);
  171. /** Checks whether a Cable is in the rack.
  172. Share-locks.
  173. */
  174. bool hasCable(Cable* cable);
  175. /** Returns the Cable with the given ID in the rack.
  176. Share-locks.
  177. */
  178. Cable* getCable(int64_t cableId);
  179. // Params
  180. void setParamValue(Module* module, int paramId, float value);
  181. float getParamValue(Module* module, int paramId);
  182. /** Requests the parameter to smoothly change toward `value`.
  183. */
  184. void setParamSmoothValue(Module* module, int paramId, float value);
  185. /** Returns the target value before smoothing.
  186. */
  187. float getParamSmoothValue(Module* module, int paramId);
  188. // ParamHandles
  189. /** Adds a ParamHandle to the rack.
  190. Does not automatically update the ParamHandle.
  191. Exclusively locks.
  192. */
  193. void addParamHandle(ParamHandle* paramHandle);
  194. /**
  195. Exclusively locks.
  196. */
  197. void removeParamHandle(ParamHandle* paramHandle);
  198. PRIVATE void removeParamHandle_NoLock(ParamHandle* paramHandle);
  199. /** Returns the unique ParamHandle for the given paramId
  200. Share-locks.
  201. */
  202. ParamHandle* getParamHandle(int64_t moduleId, int paramId);
  203. ParamHandle* getParamHandle_NoLock(int64_t moduleId, int paramId);
  204. /** Use getParamHandle(moduleId, paramId) instead.
  205. Share-locks.
  206. */
  207. DEPRECATED ParamHandle* getParamHandle(Module* module, int paramId);
  208. /** Sets the ParamHandle IDs and module pointer.
  209. If `overwrite` is true and another ParamHandle points to the same param, unsets that one and replaces it with the given handle.
  210. Exclusively locks.
  211. */
  212. void updateParamHandle(ParamHandle* paramHandle, int64_t moduleId, int paramId, bool overwrite = true);
  213. void updateParamHandle_NoLock(ParamHandle* paramHandle, int64_t moduleId, int paramId, bool overwrite = true);
  214. /** Serializes the rack.
  215. Share-locks.
  216. */
  217. json_t* toJson();
  218. /** Deserializes the rack.
  219. Exclusively locks.
  220. */
  221. void fromJson(json_t* rootJ);
  222. /** If no master module is set, the fallback Engine thread will step blocks, using the CPU clock for timing.
  223. */
  224. PRIVATE void startFallbackThread();
  225. };
  226. } // namespace engine
  227. } // namespace rack