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.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. 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. int64_t getFrame();
  34. /** Returns the frame when step() was last called. */
  35. int64_t getStepFrame();
  36. /** Returns the timestamp in nanoseconds when step() was last called. */
  37. int64_t getStepTime();
  38. /** Returns the total number of frames in the current step() call. */
  39. int getStepFrames();
  40. // Modules
  41. /** Adds a module to the rack engine.
  42. The module ID must not be taken by another module.
  43. If the module ID is -1, an ID is automatically assigned.
  44. Does not transfer pointer ownership.
  45. */
  46. void addModule(Module* module);
  47. void removeModule(Module* module);
  48. Module* getModule(int moduleId);
  49. void resetModule(Module* module);
  50. void randomizeModule(Module* module);
  51. void bypassModule(Module* module, bool bypassed);
  52. // Cables
  53. /** Adds a cable to the rack engine.
  54. The cable ID must not be taken by another cable.
  55. If the cable ID is -1, an ID is automatically assigned.
  56. Does not transfer pointer ownership.
  57. */
  58. void addCable(Cable* cable);
  59. void removeCable(Cable* cable);
  60. Cable* getCable(int cableId);
  61. // Params
  62. void setParam(Module* module, int paramId, float value);
  63. float getParam(Module* module, int paramId);
  64. /** Requests the parameter to smoothly change toward `value`. */
  65. void setSmoothParam(Module* module, int paramId, float value);
  66. /** Returns the target value before smoothing. */
  67. float getSmoothParam(Module* module, int paramId);
  68. // ParamHandles
  69. void addParamHandle(ParamHandle* paramHandle);
  70. void removeParamHandle(ParamHandle* paramHandle);
  71. /** Returns the unique ParamHandle for the given paramId */
  72. ParamHandle* getParamHandle(int moduleId, int paramId);
  73. /** Use getParamHandle(int, int) instead. */
  74. DEPRECATED ParamHandle* getParamHandle(Module* module, int paramId);
  75. /** Sets the ParamHandle IDs and module pointer.
  76. If `overwrite` is true and another ParamHandle points to the same param, unsets that one and replaces it with the given handle.
  77. */
  78. void updateParamHandle(ParamHandle* paramHandle, int moduleId, int paramId, bool overwrite = true);
  79. json_t* toJson();
  80. void fromJson(json_t* rootJ);
  81. };
  82. } // namespace engine
  83. } // namespace rack