Audio plugin host https://kx.studio/carla
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.

CarlaEngineRunner.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2022 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaEngineRunner.hpp"
  18. #include "CarlaEngineInternal.hpp"
  19. #include "CarlaPlugin.hpp"
  20. #include "water/misc/Time.h"
  21. CARLA_BACKEND_START_NAMESPACE
  22. // -----------------------------------------------------------------------
  23. CarlaEngineRunner::CarlaEngineRunner(CarlaEngine* const engine) noexcept
  24. : CarlaRunner("CarlaEngineRunner"),
  25. kEngine(engine),
  26. fEngineHasIdleOnMainThread(false),
  27. fIsAlwaysRunning(false),
  28. fIsPlugin(false)
  29. {
  30. CARLA_SAFE_ASSERT(engine != nullptr);
  31. carla_debug("CarlaEngineRunner::CarlaEngineRunner(%p)", engine);
  32. }
  33. CarlaEngineRunner::~CarlaEngineRunner() noexcept
  34. {
  35. carla_debug("CarlaEngineRunner::~CarlaEngineRunner()");
  36. }
  37. void CarlaEngineRunner::start()
  38. {
  39. carla_debug("CarlaEngineRunner::start()");
  40. if (isRunnerActive())
  41. stopRunner();
  42. fEngineHasIdleOnMainThread = kEngine->hasIdleOnMainThread();
  43. fIsPlugin = kEngine->getType() == kEngineTypePlugin;
  44. fIsAlwaysRunning = kEngine->getType() == kEngineTypeBridge || fIsPlugin;
  45. startRunner(25);
  46. }
  47. void CarlaEngineRunner::stop()
  48. {
  49. carla_debug("CarlaEngineRunner::stop()");
  50. stopRunner();
  51. }
  52. // -----------------------------------------------------------------------
  53. bool CarlaEngineRunner::run() noexcept
  54. {
  55. CARLA_SAFE_ASSERT_RETURN(kEngine != nullptr, false);
  56. float value;
  57. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  58. // int64_t lastPingTime = 0;
  59. const CarlaEngineOsc& engineOsc(kEngine->pData->osc);
  60. #endif
  61. // runner must do something...
  62. CARLA_SAFE_ASSERT_RETURN(fIsAlwaysRunning || kEngine->isRunning(), false);
  63. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  64. const bool oscRegistedForUDP = engineOsc.isControlRegisteredForUDP();
  65. #else
  66. const bool oscRegistedForUDP = false;
  67. #endif
  68. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  69. if (fIsPlugin)
  70. engineOsc.idle();
  71. #endif
  72. for (uint i=0, count = kEngine->getCurrentPluginCount(); i < count; ++i)
  73. {
  74. const CarlaPluginPtr plugin = kEngine->getPluginUnchecked(i);
  75. CARLA_SAFE_ASSERT_CONTINUE(plugin.get() != nullptr && plugin->isEnabled());
  76. CARLA_SAFE_ASSERT_UINT2(i == plugin->getId(), i, plugin->getId());
  77. const uint hints = plugin->getHints();
  78. const bool useIdle = (hints & PLUGIN_NEEDS_MAIN_THREAD_IDLE) == 0 || !fEngineHasIdleOnMainThread;
  79. const bool updateUI = (hints & PLUGIN_HAS_CUSTOM_UI) != 0 && (hints & PLUGIN_NEEDS_UI_MAIN_THREAD) == 0;
  80. // -----------------------------------------------------------
  81. // DSP Idle
  82. if (useIdle)
  83. {
  84. try {
  85. plugin->idle();
  86. } CARLA_SAFE_EXCEPTION("idle()")
  87. }
  88. // -----------------------------------------------------------
  89. // Post-poned events
  90. if (oscRegistedForUDP || updateUI)
  91. {
  92. // -------------------------------------------------------
  93. // Update parameter outputs
  94. for (uint32_t j=0, pcount=plugin->getParameterCount(); j < pcount; ++j)
  95. {
  96. if (! plugin->isParameterOutput(j))
  97. continue;
  98. value = plugin->getParameterValue(j);
  99. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  100. // Update OSC engine client
  101. if (oscRegistedForUDP)
  102. engineOsc.sendParameterValue(i, j, value);
  103. #endif
  104. // Update UI
  105. if (updateUI)
  106. plugin->uiParameterChange(j, value);
  107. }
  108. if (updateUI)
  109. {
  110. try {
  111. plugin->uiIdle();
  112. } CARLA_SAFE_EXCEPTION("uiIdle()")
  113. }
  114. }
  115. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  116. // -----------------------------------------------------------
  117. // Update OSC control client peaks
  118. if (oscRegistedForUDP)
  119. engineOsc.sendPeaks(i, kEngine->getPeaks(i));
  120. #endif
  121. }
  122. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  123. if (oscRegistedForUDP)
  124. engineOsc.sendRuntimeInfo();
  125. /*
  126. if (engineOsc.isControlRegisteredForTCP())
  127. {
  128. const int64_t timeNow = water::Time::currentTimeMillis();
  129. if (timeNow - lastPingTime > 1000)
  130. {
  131. engineOsc.sendPing();
  132. lastPingTime = timeNow;
  133. }
  134. }
  135. */
  136. #endif
  137. return true;
  138. }
  139. // -----------------------------------------------------------------------
  140. CARLA_BACKEND_END_NAMESPACE