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.

238 lines
7.2KB

  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 read/write mutex that locks when the Engine state is being read or written (manipulated).
  13. Methods that read-lock (stated in their documentation) can be called simultaneously with other read-locking methods.
  14. Methods that write-lock cannot be called simultaneously or recursively with another read-locking or write-locking method.
  15. */
  16. struct Engine {
  17. struct Internal;
  18. Internal* internal;
  19. Engine();
  20. ~Engine();
  21. /** Removes all modules and cables.
  22. Write-locks.
  23. */
  24. void clear();
  25. INTERNAL void clear_NoLock();
  26. /** Advances the engine by `frames` frames.
  27. Only call this method from the master module.
  28. Read-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. Write-locks.
  35. */
  36. void setMasterModule(Module* module);
  37. INTERNAL 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. Write-locks.
  44. */
  45. INTERNAL void setSampleRate(float sampleRate);
  46. /** Sets the sample rate if the sample rate in the settings is "Auto".
  47. Write-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. Read-locks.
  92. */
  93. size_t getModuleIds(int64_t* moduleIds, size_t len);
  94. /** Returns a vector of module IDs in the rack.
  95. Read-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. Write-locks.
  103. */
  104. void addModule(Module* module);
  105. /** Removes a Module from the rack.
  106. Write-locks.
  107. */
  108. void removeModule(Module* module);
  109. INTERNAL void removeModule_NoLock(Module* module);
  110. /** Checks whether a Module is in the rack.
  111. Read-locks.
  112. */
  113. bool hasModule(Module* module);
  114. /** Returns the Module with the given ID in the rack.
  115. Read-locks.
  116. */
  117. Module* getModule(int64_t moduleId);
  118. /** Triggers a ResetEvent for the given Module.
  119. Write-locks.
  120. */
  121. void resetModule(Module* module);
  122. /** Triggers a RandomizeEvent for the given Module.
  123. Write-locks.
  124. */
  125. void randomizeModule(Module* module);
  126. /** Sets the bypassed state and triggers a BypassEvent or UnBypassEvent of the given Module.
  127. Write-locks.
  128. */
  129. void bypassModule(Module* module, bool bypassed);
  130. /** Serializes the given Module with locking, ensuring that Module::process() is not called simultaneously.
  131. Read-locks.
  132. */
  133. json_t* moduleToJson(Module* module);
  134. /** Serializes the given Module with locking, ensuring that Module::process() is not called simultaneously.
  135. Write-locks.
  136. */
  137. void moduleFromJson(Module* module, json_t* rootJ);
  138. /** Dispatches Save event to all modules
  139. Read-locks.
  140. */
  141. void prepareSave();
  142. // Cables
  143. size_t getNumCables();
  144. /** Fills `cableIds` with up to `len` cable IDs in the rack.
  145. Returns the number of IDs written.
  146. This C-like method does no allocations. The vector C++ version below does.
  147. Read-locks.
  148. */
  149. size_t getCableIds(int64_t* cableIds, size_t len);
  150. /** Returns a vector of cable IDs in the rack.
  151. Read-locks.
  152. */
  153. std::vector<int64_t> getCableIds();
  154. /** Adds a Cable to the rack.
  155. The cable ID must not be taken by another cable.
  156. If the cable ID is -1, an ID is automatically assigned.
  157. Does not transfer pointer ownership.
  158. Write-locks.
  159. */
  160. void addCable(Cable* cable);
  161. /** Removes a Cable from the rack.
  162. Write-locks.
  163. */
  164. void removeCable(Cable* cable);
  165. INTERNAL void removeCable_NoLock(Cable* cable);
  166. /** Checks whether a Cable is in the rack.
  167. Read-locks.
  168. */
  169. bool hasCable(Cable* cable);
  170. /** Returns the Cable with the given ID in the rack.
  171. Read-locks.
  172. */
  173. Cable* getCable(int64_t cableId);
  174. // Params
  175. void setParamValue(Module* module, int paramId, float value);
  176. float getParamValue(Module* module, int paramId);
  177. /** Requests the parameter to smoothly change toward `value`.
  178. */
  179. void setParamSmoothValue(Module* module, int paramId, float value);
  180. /** Returns the target value before smoothing.
  181. */
  182. float getParamSmoothValue(Module* module, int paramId);
  183. // ParamHandles
  184. /** Adds a ParamHandle to the rack.
  185. Does not automatically update the ParamHandle.
  186. Write-locks.
  187. */
  188. void addParamHandle(ParamHandle* paramHandle);
  189. /**
  190. Write-locks.
  191. */
  192. void removeParamHandle(ParamHandle* paramHandle);
  193. INTERNAL void removeParamHandle_NoLock(ParamHandle* paramHandle);
  194. /** Returns the unique ParamHandle for the given paramId
  195. Read-locks.
  196. */
  197. ParamHandle* getParamHandle(int64_t moduleId, int paramId);
  198. /** Use getParamHandle(moduleId, paramId) instead.
  199. Read-locks.
  200. */
  201. DEPRECATED ParamHandle* getParamHandle(Module* module, int paramId);
  202. /** Sets the ParamHandle IDs and module pointer.
  203. If `overwrite` is true and another ParamHandle points to the same param, unsets that one and replaces it with the given handle.
  204. Read-locks.
  205. */
  206. void updateParamHandle(ParamHandle* paramHandle, int64_t moduleId, int paramId, bool overwrite = true);
  207. /** Serializes the rack.
  208. Read-locks.
  209. */
  210. json_t* toJson();
  211. /** Deserializes the rack.
  212. Write-locks.
  213. */
  214. void fromJson(json_t* rootJ);
  215. /** If no master module is set, the fallback Engine thread will step blocks, using the CPU clock for timing.
  216. */
  217. void startFallbackThread();
  218. };
  219. } // namespace engine
  220. } // namespace rack