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.

1346 lines
33KB

  1. #include <algorithm>
  2. #include <set>
  3. #include <thread>
  4. #include <condition_variable>
  5. #include <mutex>
  6. #include <atomic>
  7. #include <tuple>
  8. #if defined ARCH_X64
  9. #include <pmmintrin.h>
  10. #endif
  11. #include <engine/Engine.hpp>
  12. #include <settings.hpp>
  13. #include <system.hpp>
  14. #include <random.hpp>
  15. #include <context.hpp>
  16. #include <patch.hpp>
  17. #include <plugin.hpp>
  18. #include <mutex.hpp>
  19. namespace rack {
  20. namespace engine {
  21. #if defined ARCH_X64
  22. static void initMXCSR() {
  23. // Set CPU to flush-to-zero (FTZ) and denormals-are-zero (DAZ) mode
  24. // https://software.intel.com/en-us/node/682949
  25. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
  26. _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
  27. // Reset other flags
  28. _MM_SET_ROUNDING_MODE(_MM_ROUND_NEAREST);
  29. }
  30. #endif
  31. /** Barrier based on mutexes.
  32. Not finished or tested, do not use.
  33. */
  34. struct Barrier {
  35. int count = 0;
  36. uint8_t step = 0;
  37. int threads = 0;
  38. std::mutex mutex;
  39. std::condition_variable cv;
  40. void setThreads(int threads) {
  41. this->threads = threads;
  42. }
  43. void wait() {
  44. std::unique_lock<std::mutex> lock(mutex);
  45. uint8_t s = step;
  46. if (++count >= threads) {
  47. // We're the last thread. Reset next phase.
  48. count = 0;
  49. // Allow other threads to exit wait()
  50. step++;
  51. cv.notify_all();
  52. return;
  53. }
  54. cv.wait(lock, [&] {
  55. return step != s;
  56. });
  57. }
  58. };
  59. /** 2-phase barrier based on spin-locking.
  60. */
  61. struct SpinBarrier {
  62. std::atomic<int> count{0};
  63. std::atomic<uint8_t> step{0};
  64. int threads = 0;
  65. /** Must be called when no threads are calling wait().
  66. */
  67. void setThreads(int threads) {
  68. this->threads = threads;
  69. }
  70. void wait() {
  71. uint8_t s = step;
  72. if (count.fetch_add(1, std::memory_order_acquire) + 1 >= threads) {
  73. // We're the last thread. Reset next phase.
  74. count = 0;
  75. // Allow other threads to exit wait()
  76. step++;
  77. return;
  78. }
  79. // Spin until the last thread begins waiting
  80. while (true) {
  81. if (step.load(std::memory_order_relaxed) != s)
  82. return;
  83. #if defined ARCH_X64
  84. __builtin_ia32_pause();
  85. #endif
  86. }
  87. }
  88. };
  89. /** Barrier that spin-locks until yield() is called, and then all threads switch to a mutex.
  90. yield() should be called if it is likely that all threads will block for a while and continuing to spin-lock is unnecessary.
  91. Saves CPU power after yield is called.
  92. */
  93. struct HybridBarrier {
  94. std::atomic<int> count{0};
  95. std::atomic<uint8_t> step{0};
  96. int threads = 0;
  97. std::atomic<bool> yielded{false};
  98. std::mutex mutex;
  99. std::condition_variable cv;
  100. void setThreads(int threads) {
  101. this->threads = threads;
  102. }
  103. void yield() {
  104. yielded = true;
  105. }
  106. void wait() {
  107. uint8_t s = step;
  108. if (count.fetch_add(1, std::memory_order_acquire) + 1 >= threads) {
  109. // We're the last thread. Reset next phase.
  110. count = 0;
  111. bool wasYielded = yielded;
  112. yielded = false;
  113. // Allow other threads to exit wait()
  114. step++;
  115. if (wasYielded) {
  116. std::unique_lock<std::mutex> lock(mutex);
  117. cv.notify_all();
  118. }
  119. return;
  120. }
  121. // Spin until the last thread begins waiting
  122. while (!yielded.load(std::memory_order_relaxed)) {
  123. if (step.load(std::memory_order_relaxed) != s)
  124. return;
  125. #if defined ARCH_X64
  126. __builtin_ia32_pause();
  127. #endif
  128. }
  129. // Wait on mutex CV
  130. std::unique_lock<std::mutex> lock(mutex);
  131. cv.wait(lock, [&] {
  132. return step != s;
  133. });
  134. }
  135. };
  136. struct EngineWorker {
  137. Engine* engine;
  138. int id;
  139. std::thread thread;
  140. bool running = false;
  141. void start() {
  142. assert(!running);
  143. running = true;
  144. thread = std::thread([&] {
  145. run();
  146. });
  147. }
  148. void requestStop() {
  149. running = false;
  150. }
  151. void join() {
  152. assert(thread.joinable());
  153. thread.join();
  154. }
  155. void run();
  156. };
  157. struct Engine::Internal {
  158. std::vector<Module*> modules;
  159. std::vector<Cable*> cables;
  160. std::set<ParamHandle*> paramHandles;
  161. Module* masterModule = NULL;
  162. // moduleId
  163. std::map<int64_t, Module*> modulesCache;
  164. // cableId
  165. std::map<int64_t, Cable*> cablesCache;
  166. // (moduleId, paramId)
  167. std::map<std::tuple<int64_t, int>, ParamHandle*> paramHandlesCache;
  168. float sampleRate = 0.f;
  169. float sampleTime = 0.f;
  170. int64_t frame = 0;
  171. int64_t block = 0;
  172. int64_t blockFrame = 0;
  173. double blockTime = 0.0;
  174. int blockFrames = 0;
  175. // Meter
  176. int meterCount = 0;
  177. double meterTotal = 0.0;
  178. double meterMax = 0.0;
  179. double meterLastTime = -INFINITY;
  180. double meterLastAverage = 0.0;
  181. double meterLastMax = 0.0;
  182. // Parameter smoothing
  183. Module* smoothModule = NULL;
  184. int smoothParamId = 0;
  185. float smoothValue = 0.f;
  186. /** Mutex that guards the Engine state, such as settings, Modules, and Cables.
  187. Writers lock when mutating the engine's state or stepping the block.
  188. Readers lock when using the engine's state.
  189. */
  190. SharedMutex mutex;
  191. /** Mutex that guards stepBlock() so it's not called simultaneously.
  192. */
  193. std::mutex blockMutex;
  194. int threadCount = 0;
  195. std::vector<EngineWorker> workers;
  196. HybridBarrier engineBarrier;
  197. HybridBarrier workerBarrier;
  198. std::atomic<int> workerModuleIndex;
  199. // For worker threads
  200. Context* context;
  201. bool fallbackRunning = false;
  202. std::thread fallbackThread;
  203. std::mutex fallbackMutex;
  204. std::condition_variable fallbackCv;
  205. };
  206. static void Engine_updateExpander_NoLock(Engine* that, Module* module, uint8_t side) {
  207. Module::Expander& expander = side ? module->rightExpander : module->leftExpander;
  208. Module* oldExpanderModule = expander.module;
  209. if (expander.moduleId >= 0) {
  210. if (!expander.module || expander.module->id != expander.moduleId) {
  211. expander.module = that->getModule_NoLock(expander.moduleId);
  212. }
  213. }
  214. else {
  215. if (expander.module) {
  216. expander.module = NULL;
  217. }
  218. }
  219. if (expander.module != oldExpanderModule) {
  220. // Dispatch ExpanderChangeEvent
  221. Module::ExpanderChangeEvent e;
  222. e.side = side;
  223. module->onExpanderChange(e);
  224. }
  225. }
  226. static void Engine_relaunchWorkers(Engine* that, int threadCount) {
  227. Engine::Internal* internal = that->internal;
  228. if (threadCount == internal->threadCount)
  229. return;
  230. if (internal->threadCount > 0) {
  231. // Stop engine workers
  232. for (EngineWorker& worker : internal->workers) {
  233. worker.requestStop();
  234. }
  235. internal->engineBarrier.wait();
  236. // Join and destroy engine workers
  237. for (EngineWorker& worker : internal->workers) {
  238. worker.join();
  239. }
  240. internal->workers.resize(0);
  241. }
  242. // Configure engine
  243. internal->threadCount = threadCount;
  244. // Set barrier counts
  245. internal->engineBarrier.setThreads(threadCount);
  246. internal->workerBarrier.setThreads(threadCount);
  247. if (threadCount > 0) {
  248. // Create and start engine workers
  249. internal->workers.resize(threadCount - 1);
  250. for (int id = 1; id < threadCount; id++) {
  251. EngineWorker& worker = internal->workers[id - 1];
  252. worker.id = id;
  253. worker.engine = that;
  254. worker.start();
  255. }
  256. }
  257. }
  258. static void Engine_stepWorker(Engine* that, int threadId) {
  259. Engine::Internal* internal = that->internal;
  260. // int threadCount = internal->threadCount;
  261. int modulesLen = internal->modules.size();
  262. // Build ProcessArgs
  263. Module::ProcessArgs processArgs;
  264. processArgs.sampleRate = internal->sampleRate;
  265. processArgs.sampleTime = internal->sampleTime;
  266. processArgs.frame = internal->frame;
  267. // Step each module
  268. while (true) {
  269. // Choose next module
  270. // First-come-first serve module-to-thread allocation algorithm
  271. int i = internal->workerModuleIndex++;
  272. if (i >= modulesLen)
  273. break;
  274. Module* module = internal->modules[i];
  275. module->doProcess(processArgs);
  276. }
  277. }
  278. static void Cable_step(Cable* that) {
  279. Output* output = &that->outputModule->outputs[that->outputId];
  280. Input* input = &that->inputModule->inputs[that->inputId];
  281. // Match number of polyphonic channels to output port
  282. int channels = output->channels;
  283. // Copy all voltages from output to input
  284. for (int c = 0; c < channels; c++) {
  285. float v = output->voltages[c];
  286. // Set 0V if infinite or NaN
  287. if (!std::isfinite(v))
  288. v = 0.f;
  289. input->voltages[c] = v;
  290. }
  291. // Set higher channel voltages to 0
  292. for (int c = channels; c < input->channels; c++) {
  293. input->voltages[c] = 0.f;
  294. }
  295. input->channels = channels;
  296. }
  297. /** Steps a single frame
  298. */
  299. static void Engine_stepFrame(Engine* that) {
  300. Engine::Internal* internal = that->internal;
  301. // Param smoothing
  302. Module* smoothModule = internal->smoothModule;
  303. if (smoothModule) {
  304. int smoothParamId = internal->smoothParamId;
  305. float smoothValue = internal->smoothValue;
  306. Param* smoothParam = &smoothModule->params[smoothParamId];
  307. float value = smoothParam->value;
  308. // Use decay rate of roughly 1 graphics frame
  309. const float smoothLambda = 60.f;
  310. float newValue = value + (smoothValue - value) * smoothLambda * internal->sampleTime;
  311. if (value == newValue) {
  312. // Snap to actual smooth value if the value doesn't change enough (due to the granularity of floats)
  313. smoothParam->setValue(smoothValue);
  314. internal->smoothModule = NULL;
  315. internal->smoothParamId = 0;
  316. }
  317. else {
  318. smoothParam->setValue(newValue);
  319. }
  320. }
  321. // Step modules along with workers
  322. internal->workerModuleIndex = 0;
  323. internal->engineBarrier.wait();
  324. Engine_stepWorker(that, 0);
  325. internal->workerBarrier.wait();
  326. // Step cables
  327. for (Cable* cable : that->internal->cables) {
  328. Cable_step(cable);
  329. }
  330. // Flip messages for each module
  331. for (Module* module : that->internal->modules) {
  332. if (module->leftExpander.messageFlipRequested) {
  333. std::swap(module->leftExpander.producerMessage, module->leftExpander.consumerMessage);
  334. module->leftExpander.messageFlipRequested = false;
  335. }
  336. if (module->rightExpander.messageFlipRequested) {
  337. std::swap(module->rightExpander.producerMessage, module->rightExpander.consumerMessage);
  338. module->rightExpander.messageFlipRequested = false;
  339. }
  340. }
  341. internal->frame++;
  342. }
  343. static void Engine_refreshParamHandleCache(Engine* that) {
  344. // Clear cache
  345. that->internal->paramHandlesCache.clear();
  346. // Add active ParamHandles to cache
  347. for (ParamHandle* paramHandle : that->internal->paramHandles) {
  348. if (paramHandle->moduleId >= 0) {
  349. that->internal->paramHandlesCache[std::make_tuple(paramHandle->moduleId, paramHandle->paramId)] = paramHandle;
  350. }
  351. }
  352. }
  353. Engine::Engine() {
  354. internal = new Internal;
  355. internal->context = contextGet();
  356. setSuggestedSampleRate(0.f);
  357. }
  358. Engine::~Engine() {
  359. // Stop fallback thread if running
  360. {
  361. std::lock_guard<std::mutex> lock(internal->fallbackMutex);
  362. internal->fallbackRunning = false;
  363. internal->fallbackCv.notify_all();
  364. }
  365. if (internal->fallbackThread.joinable())
  366. internal->fallbackThread.join();
  367. // Shut down workers
  368. Engine_relaunchWorkers(this, 0);
  369. // Clear modules, cables, etc
  370. clear();
  371. // Make sure there are no cables or modules in the rack on destruction.
  372. // If this happens, a module must have failed to remove itself before the RackWidget was destroyed.
  373. assert(internal->cables.empty());
  374. assert(internal->modules.empty());
  375. assert(internal->paramHandles.empty());
  376. assert(internal->modulesCache.empty());
  377. assert(internal->cablesCache.empty());
  378. assert(internal->paramHandlesCache.empty());
  379. delete internal;
  380. }
  381. void Engine::clear() {
  382. std::lock_guard<SharedMutex> lock(internal->mutex);
  383. clear_NoLock();
  384. }
  385. void Engine::clear_NoLock() {
  386. // Copy lists because we'll be removing while iterating
  387. std::set<ParamHandle*> paramHandles = internal->paramHandles;
  388. for (ParamHandle* paramHandle : paramHandles) {
  389. removeParamHandle_NoLock(paramHandle);
  390. // Don't delete paramHandle because they're normally owned by Module subclasses
  391. }
  392. std::vector<Cable*> cables = internal->cables;
  393. for (Cable* cable : cables) {
  394. removeCable_NoLock(cable);
  395. delete cable;
  396. }
  397. std::vector<Module*> modules = internal->modules;
  398. for (Module* module : modules) {
  399. removeModule_NoLock(module);
  400. delete module;
  401. }
  402. }
  403. void Engine::stepBlock(int frames) {
  404. // Start timer before locking
  405. double startTime = system::getTime();
  406. std::lock_guard<std::mutex> stepLock(internal->blockMutex);
  407. SharedLock<SharedMutex> lock(internal->mutex);
  408. // Configure thread
  409. #if defined ARCH_X64
  410. uint32_t csr = _mm_getcsr();
  411. initMXCSR();
  412. #endif
  413. random::init();
  414. internal->blockFrame = internal->frame;
  415. internal->blockTime = system::getTime();
  416. internal->blockFrames = frames;
  417. // Update expander pointers
  418. for (Module* module : internal->modules) {
  419. Engine_updateExpander_NoLock(this, module, false);
  420. Engine_updateExpander_NoLock(this, module, true);
  421. }
  422. // Launch workers
  423. Engine_relaunchWorkers(this, settings::threadCount);
  424. // Step individual frames
  425. for (int i = 0; i < frames; i++) {
  426. Engine_stepFrame(this);
  427. }
  428. yieldWorkers();
  429. internal->block++;
  430. // Stop timer
  431. double endTime = system::getTime();
  432. double meter = (endTime - startTime) / (frames * internal->sampleTime);
  433. internal->meterTotal += meter;
  434. internal->meterMax = std::fmax(internal->meterMax, meter);
  435. internal->meterCount++;
  436. // Update meter values
  437. const double meterUpdateDuration = 1.0;
  438. if (startTime - internal->meterLastTime >= meterUpdateDuration) {
  439. internal->meterLastAverage = internal->meterTotal / internal->meterCount;
  440. internal->meterLastMax = internal->meterMax;
  441. internal->meterLastTime = startTime;
  442. internal->meterCount = 0;
  443. internal->meterTotal = 0.0;
  444. internal->meterMax = 0.0;
  445. }
  446. #if defined ARCH_X64
  447. // Reset MXCSR back to original value
  448. _mm_setcsr(csr);
  449. #endif
  450. }
  451. void Engine::setMasterModule(Module* module) {
  452. if (module == internal->masterModule)
  453. return;
  454. std::lock_guard<SharedMutex> lock(internal->mutex);
  455. setMasterModule_NoLock(module);
  456. }
  457. void Engine::setMasterModule_NoLock(Module* module) {
  458. if (module == internal->masterModule)
  459. return;
  460. if (internal->masterModule) {
  461. // Dispatch UnsetMasterEvent
  462. Module::UnsetMasterEvent e;
  463. internal->masterModule->onUnsetMaster(e);
  464. }
  465. internal->masterModule = module;
  466. if (internal->masterModule) {
  467. // Dispatch SetMasterEvent
  468. Module::SetMasterEvent e;
  469. internal->masterModule->onSetMaster(e);
  470. }
  471. // Wake up fallback thread if master module was unset
  472. if (!internal->masterModule) {
  473. internal->fallbackCv.notify_all();
  474. }
  475. }
  476. Module* Engine::getMasterModule() {
  477. return internal->masterModule;
  478. }
  479. float Engine::getSampleRate() {
  480. return internal->sampleRate;
  481. }
  482. void Engine::setSampleRate(float sampleRate) {
  483. if (sampleRate == internal->sampleRate)
  484. return;
  485. std::lock_guard<SharedMutex> lock(internal->mutex);
  486. internal->sampleRate = sampleRate;
  487. internal->sampleTime = 1.f / sampleRate;
  488. // Dispatch SampleRateChangeEvent
  489. Module::SampleRateChangeEvent e;
  490. e.sampleRate = internal->sampleRate;
  491. e.sampleTime = internal->sampleTime;
  492. for (Module* module : internal->modules) {
  493. module->onSampleRateChange(e);
  494. }
  495. }
  496. void Engine::setSuggestedSampleRate(float suggestedSampleRate) {
  497. if (settings::sampleRate > 0) {
  498. setSampleRate(settings::sampleRate);
  499. }
  500. else if (suggestedSampleRate > 0) {
  501. setSampleRate(suggestedSampleRate);
  502. }
  503. else {
  504. // Fallback sample rate
  505. setSampleRate(44100.f);
  506. }
  507. }
  508. float Engine::getSampleTime() {
  509. return internal->sampleTime;
  510. }
  511. void Engine::yieldWorkers() {
  512. internal->workerBarrier.yield();
  513. }
  514. int64_t Engine::getFrame() {
  515. return internal->frame;
  516. }
  517. int64_t Engine::getBlock() {
  518. return internal->block;
  519. }
  520. int64_t Engine::getBlockFrame() {
  521. return internal->blockFrame;
  522. }
  523. double Engine::getBlockTime() {
  524. return internal->blockTime;
  525. }
  526. int Engine::getBlockFrames() {
  527. return internal->blockFrames;
  528. }
  529. double Engine::getBlockDuration() {
  530. return internal->blockFrames * internal->sampleTime;
  531. }
  532. double Engine::getMeterAverage() {
  533. return internal->meterLastAverage;
  534. }
  535. double Engine::getMeterMax() {
  536. return internal->meterLastMax;
  537. }
  538. size_t Engine::getNumModules() {
  539. return internal->modules.size();
  540. }
  541. size_t Engine::getModuleIds(int64_t* moduleIds, size_t len) {
  542. SharedLock<SharedMutex> lock(internal->mutex);
  543. size_t i = 0;
  544. for (Module* m : internal->modules) {
  545. if (i >= len)
  546. break;
  547. moduleIds[i] = m->id;
  548. i++;
  549. }
  550. return i;
  551. }
  552. std::vector<int64_t> Engine::getModuleIds() {
  553. SharedLock<SharedMutex> lock(internal->mutex);
  554. std::vector<int64_t> moduleIds;
  555. moduleIds.reserve(internal->modules.size());
  556. for (Module* m : internal->modules) {
  557. moduleIds.push_back(m->id);
  558. }
  559. return moduleIds;
  560. }
  561. void Engine::addModule(Module* module) {
  562. std::lock_guard<SharedMutex> lock(internal->mutex);
  563. addModule_NoLock(module);
  564. }
  565. void Engine::addModule_NoLock(Module* module) {
  566. assert(module);
  567. // Check that the module is not already added
  568. auto it = std::find(internal->modules.begin(), internal->modules.end(), module);
  569. assert(it == internal->modules.end());
  570. // Set ID if unset or collides with an existing ID
  571. while (module->id < 0 || internal->modulesCache.find(module->id) != internal->modulesCache.end()) {
  572. // Randomly generate ID
  573. module->id = random::u64() % (1ull << 53);
  574. }
  575. // Add module
  576. internal->modules.push_back(module);
  577. internal->modulesCache[module->id] = module;
  578. // Dispatch AddEvent
  579. Module::AddEvent eAdd;
  580. module->onAdd(eAdd);
  581. // Dispatch SampleRateChangeEvent
  582. Module::SampleRateChangeEvent eSrc;
  583. eSrc.sampleRate = internal->sampleRate;
  584. eSrc.sampleTime = internal->sampleTime;
  585. module->onSampleRateChange(eSrc);
  586. // Update ParamHandles' module pointers
  587. for (ParamHandle* paramHandle : internal->paramHandles) {
  588. if (paramHandle->moduleId == module->id)
  589. paramHandle->module = module;
  590. }
  591. }
  592. void Engine::removeModule(Module* module) {
  593. std::lock_guard<SharedMutex> lock(internal->mutex);
  594. removeModule_NoLock(module);
  595. }
  596. void Engine::removeModule_NoLock(Module* module) {
  597. assert(module);
  598. // Check that the module actually exists
  599. auto it = std::find(internal->modules.begin(), internal->modules.end(), module);
  600. assert(it != internal->modules.end());
  601. // Dispatch RemoveEvent
  602. Module::RemoveEvent eRemove;
  603. module->onRemove(eRemove);
  604. // Update ParamHandles' module pointers
  605. for (ParamHandle* paramHandle : internal->paramHandles) {
  606. if (paramHandle->moduleId == module->id)
  607. paramHandle->module = NULL;
  608. }
  609. // Unset master module
  610. if (getMasterModule() == module) {
  611. setMasterModule_NoLock(NULL);
  612. }
  613. // If a param is being smoothed on this module, stop smoothing it immediately
  614. if (module == internal->smoothModule) {
  615. internal->smoothModule = NULL;
  616. }
  617. // Check that all cables are disconnected
  618. for (Cable* cable : internal->cables) {
  619. assert(cable->inputModule != module);
  620. assert(cable->outputModule != module);
  621. }
  622. // Update expanders of other modules
  623. for (Module* m : internal->modules) {
  624. if (m->leftExpander.module == module) {
  625. m->leftExpander.moduleId = -1;
  626. m->leftExpander.module = NULL;
  627. }
  628. if (m->rightExpander.module == module) {
  629. m->rightExpander.moduleId = -1;
  630. m->rightExpander.module = NULL;
  631. }
  632. }
  633. // Remove module
  634. internal->modulesCache.erase(module->id);
  635. internal->modules.erase(it);
  636. // Reset expanders
  637. module->leftExpander.moduleId = -1;
  638. module->leftExpander.module = NULL;
  639. module->rightExpander.moduleId = -1;
  640. module->rightExpander.module = NULL;
  641. }
  642. bool Engine::hasModule(Module* module) {
  643. SharedLock<SharedMutex> lock(internal->mutex);
  644. // TODO Performance could be improved by searching modulesCache, but more testing would be needed to make sure it's always valid.
  645. auto it = std::find(internal->modules.begin(), internal->modules.end(), module);
  646. return it != internal->modules.end();
  647. }
  648. Module* Engine::getModule(int64_t moduleId) {
  649. SharedLock<SharedMutex> lock(internal->mutex);
  650. return getModule_NoLock(moduleId);
  651. }
  652. Module* Engine::getModule_NoLock(int64_t moduleId) {
  653. auto it = internal->modulesCache.find(moduleId);
  654. if (it == internal->modulesCache.end())
  655. return NULL;
  656. return it->second;
  657. }
  658. void Engine::resetModule(Module* module) {
  659. std::lock_guard<SharedMutex> lock(internal->mutex);
  660. assert(module);
  661. Module::ResetEvent eReset;
  662. module->onReset(eReset);
  663. }
  664. void Engine::randomizeModule(Module* module) {
  665. std::lock_guard<SharedMutex> lock(internal->mutex);
  666. assert(module);
  667. Module::RandomizeEvent eRandomize;
  668. module->onRandomize(eRandomize);
  669. }
  670. void Engine::bypassModule(Module* module, bool bypassed) {
  671. assert(module);
  672. if (module->isBypassed() == bypassed)
  673. return;
  674. std::lock_guard<SharedMutex> lock(internal->mutex);
  675. // Clear outputs and set to 1 channel
  676. for (Output& output : module->outputs) {
  677. // This zeros all voltages, but the channel is set to 1 if connected
  678. output.setChannels(0);
  679. }
  680. // Set bypassed state
  681. module->setBypassed(bypassed);
  682. if (bypassed) {
  683. // Dispatch BypassEvent
  684. Module::BypassEvent eBypass;
  685. module->onBypass(eBypass);
  686. }
  687. else {
  688. // Dispatch UnBypassEvent
  689. Module::UnBypassEvent eUnBypass;
  690. module->onUnBypass(eUnBypass);
  691. }
  692. }
  693. json_t* Engine::moduleToJson(Module* module) {
  694. SharedLock<SharedMutex> lock(internal->mutex);
  695. return module->toJson();
  696. }
  697. void Engine::moduleFromJson(Module* module, json_t* rootJ) {
  698. std::lock_guard<SharedMutex> lock(internal->mutex);
  699. module->fromJson(rootJ);
  700. }
  701. void Engine::prepareSaveModule(Module* module) {
  702. SharedLock<SharedMutex> lock(internal->mutex);
  703. Module::SaveEvent e;
  704. module->onSave(e);
  705. }
  706. void Engine::prepareSave() {
  707. SharedLock<SharedMutex> lock(internal->mutex);
  708. for (Module* module : internal->modules) {
  709. Module::SaveEvent e;
  710. module->onSave(e);
  711. }
  712. }
  713. size_t Engine::getNumCables() {
  714. return internal->cables.size();
  715. }
  716. size_t Engine::getCableIds(int64_t* cableIds, size_t len) {
  717. SharedLock<SharedMutex> lock(internal->mutex);
  718. size_t i = 0;
  719. for (Cable* c : internal->cables) {
  720. if (i >= len)
  721. break;
  722. cableIds[i] = c->id;
  723. i++;
  724. }
  725. return i;
  726. }
  727. std::vector<int64_t> Engine::getCableIds() {
  728. SharedLock<SharedMutex> lock(internal->mutex);
  729. std::vector<int64_t> cableIds;
  730. cableIds.reserve(internal->cables.size());
  731. for (Cable* c : internal->cables) {
  732. cableIds.push_back(c->id);
  733. }
  734. return cableIds;
  735. }
  736. void Engine::addCable(Cable* cable) {
  737. std::lock_guard<SharedMutex> lock(internal->mutex);
  738. addCable_NoLock(cable);
  739. }
  740. void Engine::addCable_NoLock(Cable* cable) {
  741. assert(cable);
  742. // Check cable properties
  743. assert(cable->inputModule);
  744. assert(cable->outputModule);
  745. bool outputWasConnected = false;
  746. for (Cable* cable2 : internal->cables) {
  747. // Check that the cable is not already added
  748. assert(cable2 != cable);
  749. // Check that the input is not already used by another cable
  750. assert(!(cable2->inputModule == cable->inputModule && cable2->inputId == cable->inputId));
  751. // Check if output is already connected to a cable
  752. if (cable2->outputModule == cable->outputModule && cable2->outputId == cable->outputId)
  753. outputWasConnected = true;
  754. }
  755. // Set ID if unset or collides with an existing ID
  756. while (cable->id < 0 || internal->cablesCache.find(cable->id) != internal->cablesCache.end()) {
  757. // Randomly generate ID
  758. cable->id = random::u64() % (1ull << 53);
  759. }
  760. // Add the cable
  761. internal->cables.push_back(cable);
  762. internal->cablesCache[cable->id] = cable;
  763. // Set input as connected
  764. Input& input = cable->inputModule->inputs[cable->inputId];
  765. input.channels = 1;
  766. // Set output as connected, which might already be connected
  767. Output& output = cable->outputModule->outputs[cable->outputId];
  768. if (output.channels == 0) {
  769. output.channels = 1;
  770. }
  771. // Dispatch input port event
  772. {
  773. Module::PortChangeEvent e;
  774. e.connecting = true;
  775. e.type = Port::INPUT;
  776. e.portId = cable->inputId;
  777. cable->inputModule->onPortChange(e);
  778. }
  779. // Dispatch output port event if its state went from disconnected to connected.
  780. if (!outputWasConnected) {
  781. Module::PortChangeEvent e;
  782. e.connecting = true;
  783. e.type = Port::OUTPUT;
  784. e.portId = cable->outputId;
  785. cable->outputModule->onPortChange(e);
  786. }
  787. }
  788. void Engine::removeCable(Cable* cable) {
  789. std::lock_guard<SharedMutex> lock(internal->mutex);
  790. removeCable_NoLock(cable);
  791. }
  792. void Engine::removeCable_NoLock(Cable* cable) {
  793. assert(cable);
  794. // Check that the cable is already added
  795. auto it = std::find(internal->cables.begin(), internal->cables.end(), cable);
  796. assert(it != internal->cables.end());
  797. // Remove the cable
  798. internal->cablesCache.erase(cable->id);
  799. internal->cables.erase(it);
  800. // Set input as disconnected
  801. Input& input = cable->inputModule->inputs[cable->inputId];
  802. input.channels = 0;
  803. // Clear input values
  804. for (uint8_t c = 0; c < PORT_MAX_CHANNELS; c++) {
  805. input.setVoltage(0.f, c);
  806. }
  807. // Check if output is still connected to a cable
  808. bool outputIsConnected = false;
  809. for (Cable* cable2 : internal->cables) {
  810. if (cable2->outputModule == cable->outputModule && cable2->outputId == cable->outputId) {
  811. outputIsConnected = true;
  812. break;
  813. }
  814. }
  815. // Set output as disconnected if disconnected from all cables
  816. if (!outputIsConnected) {
  817. Output& output = cable->outputModule->outputs[cable->outputId];
  818. output.channels = 0;
  819. // Don't clear output values
  820. }
  821. // Dispatch input port event
  822. {
  823. Module::PortChangeEvent e;
  824. e.connecting = false;
  825. e.type = Port::INPUT;
  826. e.portId = cable->inputId;
  827. cable->inputModule->onPortChange(e);
  828. }
  829. // Dispatch output port event if its state went from connected to disconnected.
  830. if (!outputIsConnected) {
  831. Module::PortChangeEvent e;
  832. e.connecting = false;
  833. e.type = Port::OUTPUT;
  834. e.portId = cable->outputId;
  835. cable->outputModule->onPortChange(e);
  836. }
  837. }
  838. bool Engine::hasCable(Cable* cable) {
  839. SharedLock<SharedMutex> lock(internal->mutex);
  840. // TODO Performance could be improved by searching cablesCache, but more testing would be needed to make sure it's always valid.
  841. auto it = std::find(internal->cables.begin(), internal->cables.end(), cable);
  842. return it != internal->cables.end();
  843. }
  844. Cable* Engine::getCable(int64_t cableId) {
  845. SharedLock<SharedMutex> lock(internal->mutex);
  846. auto it = internal->cablesCache.find(cableId);
  847. if (it == internal->cablesCache.end())
  848. return NULL;
  849. return it->second;
  850. }
  851. void Engine::setParamValue(Module* module, int paramId, float value) {
  852. // If param is being smoothed, cancel smoothing.
  853. if (internal->smoothModule == module && internal->smoothParamId == paramId) {
  854. internal->smoothModule = NULL;
  855. internal->smoothParamId = 0;
  856. }
  857. module->params[paramId].setValue(value);
  858. }
  859. float Engine::getParamValue(Module* module, int paramId) {
  860. return module->params[paramId].getValue();
  861. }
  862. void Engine::setParamSmoothValue(Module* module, int paramId, float value) {
  863. // If another param is being smoothed, jump value
  864. if (internal->smoothModule && !(internal->smoothModule == module && internal->smoothParamId == paramId)) {
  865. internal->smoothModule->params[internal->smoothParamId].setValue(internal->smoothValue);
  866. }
  867. internal->smoothParamId = paramId;
  868. internal->smoothValue = value;
  869. // Set this last so the above values are valid as soon as it is set
  870. internal->smoothModule = module;
  871. }
  872. float Engine::getParamSmoothValue(Module* module, int paramId) {
  873. if (internal->smoothModule == module && internal->smoothParamId == paramId)
  874. return internal->smoothValue;
  875. return module->params[paramId].getValue();
  876. }
  877. void Engine::addParamHandle(ParamHandle* paramHandle) {
  878. std::lock_guard<SharedMutex> lock(internal->mutex);
  879. // New ParamHandles must be blank.
  880. // This means we don't have to refresh the cache.
  881. assert(paramHandle->moduleId < 0);
  882. // Check that the ParamHandle is not already added
  883. auto it = internal->paramHandles.find(paramHandle);
  884. assert(it == internal->paramHandles.end());
  885. // Add it
  886. internal->paramHandles.insert(paramHandle);
  887. // No need to refresh the cache because the moduleId is not set.
  888. }
  889. void Engine::removeParamHandle(ParamHandle* paramHandle) {
  890. std::lock_guard<SharedMutex> lock(internal->mutex);
  891. removeParamHandle_NoLock(paramHandle);
  892. }
  893. void Engine::removeParamHandle_NoLock(ParamHandle* paramHandle) {
  894. // Check that the ParamHandle is already added
  895. auto it = internal->paramHandles.find(paramHandle);
  896. assert(it != internal->paramHandles.end());
  897. // Remove it
  898. paramHandle->module = NULL;
  899. internal->paramHandles.erase(it);
  900. Engine_refreshParamHandleCache(this);
  901. }
  902. ParamHandle* Engine::getParamHandle(int64_t moduleId, int paramId) {
  903. SharedLock<SharedMutex> lock(internal->mutex);
  904. return getParamHandle_NoLock(moduleId, paramId);
  905. }
  906. ParamHandle* Engine::getParamHandle_NoLock(int64_t moduleId, int paramId) {
  907. auto it = internal->paramHandlesCache.find(std::make_tuple(moduleId, paramId));
  908. if (it == internal->paramHandlesCache.end())
  909. return NULL;
  910. return it->second;
  911. }
  912. ParamHandle* Engine::getParamHandle(Module* module, int paramId) {
  913. return getParamHandle(module->id, paramId);
  914. }
  915. void Engine::updateParamHandle(ParamHandle* paramHandle, int64_t moduleId, int paramId, bool overwrite) {
  916. std::lock_guard<SharedMutex> lock(internal->mutex);
  917. updateParamHandle_NoLock(paramHandle, moduleId, paramId, overwrite);
  918. }
  919. void Engine::updateParamHandle_NoLock(ParamHandle* paramHandle, int64_t moduleId, int paramId, bool overwrite) {
  920. // Check that it exists
  921. auto it = internal->paramHandles.find(paramHandle);
  922. assert(it != internal->paramHandles.end());
  923. // Set IDs
  924. paramHandle->moduleId = moduleId;
  925. paramHandle->paramId = paramId;
  926. paramHandle->module = NULL;
  927. // At this point, the ParamHandle cache might be invalid.
  928. if (paramHandle->moduleId >= 0) {
  929. // Replace old ParamHandle, or reset the current ParamHandle
  930. ParamHandle* oldParamHandle = getParamHandle_NoLock(moduleId, paramId);
  931. if (oldParamHandle) {
  932. if (overwrite) {
  933. oldParamHandle->moduleId = -1;
  934. oldParamHandle->paramId = 0;
  935. oldParamHandle->module = NULL;
  936. }
  937. else {
  938. paramHandle->moduleId = -1;
  939. paramHandle->paramId = 0;
  940. paramHandle->module = NULL;
  941. }
  942. }
  943. }
  944. // Set module pointer if the above block didn't reset it
  945. if (paramHandle->moduleId >= 0) {
  946. paramHandle->module = getModule_NoLock(paramHandle->moduleId);
  947. }
  948. Engine_refreshParamHandleCache(this);
  949. }
  950. json_t* Engine::toJson() {
  951. SharedLock<SharedMutex> lock(internal->mutex);
  952. json_t* rootJ = json_object();
  953. // modules
  954. json_t* modulesJ = json_array();
  955. for (Module* module : internal->modules) {
  956. json_t* moduleJ = module->toJson();
  957. json_array_append_new(modulesJ, moduleJ);
  958. }
  959. json_object_set_new(rootJ, "modules", modulesJ);
  960. // cables
  961. json_t* cablesJ = json_array();
  962. for (Cable* cable : internal->cables) {
  963. json_t* cableJ = cable->toJson();
  964. json_array_append_new(cablesJ, cableJ);
  965. }
  966. json_object_set_new(rootJ, "cables", cablesJ);
  967. // masterModule
  968. if (internal->masterModule) {
  969. json_object_set_new(rootJ, "masterModuleId", json_integer(internal->masterModule->id));
  970. }
  971. return rootJ;
  972. }
  973. void Engine::fromJson(json_t* rootJ) {
  974. // modules
  975. std::vector<Module*> modules;
  976. json_t* modulesJ = json_object_get(rootJ, "modules");
  977. if (!modulesJ)
  978. return;
  979. size_t moduleIndex;
  980. json_t* moduleJ;
  981. json_array_foreach(modulesJ, moduleIndex, moduleJ) {
  982. // Get model
  983. plugin::Model* model;
  984. try {
  985. model = plugin::modelFromJson(moduleJ);
  986. }
  987. catch (Exception& e) {
  988. WARN("Cannot load model: %s", e.what());
  989. continue;
  990. }
  991. // Create module
  992. INFO("Creating module %s", model->getFullName().c_str());
  993. Module* module = model->createModule();
  994. assert(module);
  995. try {
  996. module->fromJson(moduleJ);
  997. // Before 1.0, the module ID was the index in the "modules" array
  998. if (module->id < 0) {
  999. module->id = moduleIndex;
  1000. }
  1001. }
  1002. catch (Exception& e) {
  1003. WARN("Cannot load module: %s", e.what());
  1004. delete module;
  1005. continue;
  1006. }
  1007. modules.push_back(module);
  1008. }
  1009. std::lock_guard<SharedMutex> lock(internal->mutex);
  1010. clear_NoLock();
  1011. // Add modules
  1012. for (Module* module : modules) {
  1013. addModule_NoLock(module);
  1014. }
  1015. // cables
  1016. json_t* cablesJ = json_object_get(rootJ, "cables");
  1017. // Before 1.0, cables were called wires
  1018. if (!cablesJ)
  1019. cablesJ = json_object_get(rootJ, "wires");
  1020. if (!cablesJ)
  1021. return;
  1022. size_t cableIndex;
  1023. json_t* cableJ;
  1024. json_array_foreach(cablesJ, cableIndex, cableJ) {
  1025. // cable
  1026. Cable* cable = new Cable;
  1027. try {
  1028. cable->fromJson(cableJ);
  1029. // Before 1.0, the cable ID was the index in the "cables" array
  1030. if (cable->id < 0) {
  1031. cable->id = cableIndex;
  1032. }
  1033. addCable_NoLock(cable);
  1034. }
  1035. catch (Exception& e) {
  1036. WARN("Cannot load cable: %s", e.what());
  1037. delete cable;
  1038. // Don't log exceptions because missing modules create unnecessary complaining when cables try to connect to them.
  1039. continue;
  1040. }
  1041. }
  1042. // masterModule
  1043. json_t* masterModuleIdJ = json_object_get(rootJ, "masterModuleId");
  1044. if (masterModuleIdJ) {
  1045. Module* masterModule = getModule_NoLock(json_integer_value(masterModuleIdJ));
  1046. setMasterModule_NoLock(masterModule);
  1047. }
  1048. }
  1049. void EngineWorker::run() {
  1050. // Configure thread
  1051. contextSet(engine->internal->context);
  1052. system::setThreadName(string::f("Worker %d", id));
  1053. #if defined ARCH_X64
  1054. initMXCSR();
  1055. #endif
  1056. random::init();
  1057. while (true) {
  1058. engine->internal->engineBarrier.wait();
  1059. if (!running)
  1060. return;
  1061. Engine_stepWorker(engine, id);
  1062. engine->internal->workerBarrier.wait();
  1063. }
  1064. }
  1065. static void Engine_fallbackRun(Engine* that) {
  1066. system::setThreadName("Engine fallback");
  1067. contextSet(that->internal->context);
  1068. while (that->internal->fallbackRunning) {
  1069. if (!that->getMasterModule()) {
  1070. // Step blocks and wait
  1071. double start = system::getTime();
  1072. int frames = std::floor(that->getSampleRate() / 60);
  1073. that->stepBlock(frames);
  1074. double end = system::getTime();
  1075. double duration = frames * that->getSampleTime() - (end - start);
  1076. if (duration > 0.0) {
  1077. std::this_thread::sleep_for(std::chrono::duration<double>(duration));
  1078. }
  1079. }
  1080. else {
  1081. // Wait for master module to be unset, or for the request to stop running
  1082. std::unique_lock<std::mutex> lock(that->internal->fallbackMutex);
  1083. that->internal->fallbackCv.wait(lock, [&]() {
  1084. return !that->internal->fallbackRunning || !that->getMasterModule();
  1085. });
  1086. }
  1087. }
  1088. }
  1089. void Engine::startFallbackThread() {
  1090. if (internal->fallbackThread.joinable())
  1091. return;
  1092. internal->fallbackRunning = true;
  1093. internal->fallbackThread = std::thread(Engine_fallbackRun, this);
  1094. }
  1095. } // namespace engine
  1096. } // namespace rack