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 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #pragma once
  2. #include <common.hpp>
  3. #include <engine/Module.hpp>
  4. #include <engine/Cable.hpp>
  5. #include <engine/ParamHandle.hpp>
  6. #include <vector>
  7. namespace rack {
  8. namespace engine {
  9. struct Engine {
  10. struct Internal;
  11. Internal* internal;
  12. Engine();
  13. ~Engine();
  14. void clear();
  15. /** Advances the engine by `frames` frames.
  16. Only call this method from the primary module.
  17. */
  18. void step(int frames);
  19. void setPrimaryModule(Module* module);
  20. Module* getPrimaryModule();
  21. void setPaused(bool paused);
  22. bool isPaused();
  23. float getSampleRate();
  24. /** Returns the inverse of the current sample rate.
  25. */
  26. float getSampleTime();
  27. /** Causes worker threads to block on a mutex instead of spinlock.
  28. Call this in your Module::step() method to hint that the operation will take more than ~0.1 ms.
  29. */
  30. void yieldWorkers();
  31. /** Returns the number of audio samples since the Engine's first sample.
  32. */
  33. uint64_t getFrame();
  34. // Modules
  35. /** Adds a module to the rack engine.
  36. The module ID must not be taken by another module.
  37. If the module ID is -1, an ID is automatically assigned.
  38. Does not transfer pointer ownership.
  39. */
  40. void addModule(Module* module);
  41. void removeModule(Module* module);
  42. Module* getModule(int moduleId);
  43. void resetModule(Module* module);
  44. void randomizeModule(Module* module);
  45. void disableModule(Module* module, bool disabled);
  46. // Cables
  47. /** Adds a cable to the rack engine.
  48. The cable ID must not be taken by another cable.
  49. If the cable ID is -1, an ID is automatically assigned.
  50. Does not transfer pointer ownership.
  51. */
  52. void addCable(Cable* cable);
  53. void removeCable(Cable* cable);
  54. Cable* getCable(int cableId);
  55. // Params
  56. void setParam(Module* module, int paramId, float value);
  57. float getParam(Module* module, int paramId);
  58. /** Requests the parameter to smoothly change toward `value`. */
  59. void setSmoothParam(Module* module, int paramId, float value);
  60. /** Returns the target value before smoothing. */
  61. float getSmoothParam(Module* module, int paramId);
  62. // ParamHandles
  63. void addParamHandle(ParamHandle* paramHandle);
  64. void removeParamHandle(ParamHandle* paramHandle);
  65. /** Returns the unique ParamHandle for the given paramId */
  66. ParamHandle* getParamHandle(int moduleId, int paramId);
  67. /** Use getParamHandle(int, int) instead. */
  68. DEPRECATED ParamHandle* getParamHandle(Module* module, int paramId);
  69. /** Sets the ParamHandle IDs and module pointer.
  70. If `overwrite` is true and another ParamHandle points to the same param, unsets that one and replaces it with the given handle.
  71. */
  72. void updateParamHandle(ParamHandle* paramHandle, int moduleId, int paramId, bool overwrite = true);
  73. json_t* toJson();
  74. void fromJson(json_t* rootJ);
  75. };
  76. /** Creates a Module from a JSON module object.
  77. Throws an Exception if the model is not found.
  78. */
  79. Module* moduleFromJson(json_t* moduleJ);
  80. } // namespace engine
  81. } // namespace rack