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.

160 lines
4.9KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2020 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 "CarlaEngineThread.hpp"
  18. #include "CarlaEngineInternal.hpp"
  19. #include "CarlaPlugin.hpp"
  20. #include "water/misc/Time.h"
  21. CARLA_BACKEND_START_NAMESPACE
  22. // -----------------------------------------------------------------------
  23. CarlaEngineThread::CarlaEngineThread(CarlaEngine* const engine) noexcept
  24. : CarlaThread("CarlaEngineThread"),
  25. kEngine(engine)
  26. {
  27. CARLA_SAFE_ASSERT(engine != nullptr);
  28. carla_debug("CarlaEngineThread::CarlaEngineThread(%p)", engine);
  29. }
  30. CarlaEngineThread::~CarlaEngineThread() noexcept
  31. {
  32. carla_debug("CarlaEngineThread::~CarlaEngineThread()");
  33. }
  34. // -----------------------------------------------------------------------
  35. void CarlaEngineThread::run() noexcept
  36. {
  37. CARLA_SAFE_ASSERT_RETURN(kEngine != nullptr,);
  38. carla_debug("CarlaEngineThread::run()");
  39. const bool kIsPlugin = kEngine->getType() == kEngineTypePlugin;
  40. const bool kIsAlwaysRunning = kEngine->getType() == kEngineTypeBridge || kIsPlugin;
  41. float value;
  42. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  43. // int64_t lastPingTime = 0;
  44. const CarlaEngineOsc& engineOsc(kEngine->pData->osc);
  45. #endif
  46. // thread must do something...
  47. CARLA_SAFE_ASSERT_RETURN(kIsAlwaysRunning || kEngine->isRunning(),);
  48. for (; (kIsAlwaysRunning || kEngine->isRunning()) && ! shouldThreadExit();)
  49. {
  50. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  51. const bool oscRegistedForUDP = engineOsc.isControlRegisteredForUDP();
  52. #else
  53. const bool oscRegistedForUDP = false;
  54. #endif
  55. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  56. if (kIsPlugin)
  57. engineOsc.idle();
  58. #endif
  59. for (uint i=0, count = kEngine->getCurrentPluginCount(); i < count; ++i)
  60. {
  61. const CarlaPluginPtr plugin = kEngine->getPluginUnchecked(i);
  62. CARLA_SAFE_ASSERT_CONTINUE(plugin.get() != nullptr && plugin->isEnabled());
  63. CARLA_SAFE_ASSERT_UINT2(i == plugin->getId(), i, plugin->getId());
  64. const uint hints(plugin->getHints());
  65. const bool updateUI((hints & PLUGIN_HAS_CUSTOM_UI) != 0 && (hints & PLUGIN_NEEDS_UI_MAIN_THREAD) == 0);
  66. // -----------------------------------------------------------
  67. // DSP Idle
  68. try {
  69. plugin->idle();
  70. } CARLA_SAFE_EXCEPTION("idle()")
  71. // -----------------------------------------------------------
  72. // Post-poned events
  73. if (oscRegistedForUDP || updateUI)
  74. {
  75. // -------------------------------------------------------
  76. // Update parameter outputs
  77. for (uint32_t j=0, pcount=plugin->getParameterCount(); j < pcount; ++j)
  78. {
  79. if (! plugin->isParameterOutput(j))
  80. continue;
  81. value = plugin->getParameterValue(j);
  82. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  83. // Update OSC engine client
  84. if (oscRegistedForUDP)
  85. engineOsc.sendParameterValue(i, j, value);
  86. #endif
  87. // Update UI
  88. if (updateUI)
  89. plugin->uiParameterChange(j, value);
  90. }
  91. if (updateUI)
  92. {
  93. try {
  94. plugin->uiIdle();
  95. } CARLA_SAFE_EXCEPTION("uiIdle()")
  96. }
  97. }
  98. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  99. // -----------------------------------------------------------
  100. // Update OSC control client peaks
  101. if (oscRegistedForUDP)
  102. engineOsc.sendPeaks(i, kEngine->getPeaks(i));
  103. #endif
  104. }
  105. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  106. if (oscRegistedForUDP)
  107. engineOsc.sendRuntimeInfo();
  108. /*
  109. if (engineOsc.isControlRegisteredForTCP())
  110. {
  111. const int64_t timeNow = water::Time::currentTimeMillis();
  112. if (timeNow - lastPingTime > 1000)
  113. {
  114. engineOsc.sendPing();
  115. lastPingTime = timeNow;
  116. }
  117. }
  118. */
  119. #endif
  120. carla_msleep(25);
  121. }
  122. carla_debug("CarlaEngineThread closed");
  123. }
  124. // -----------------------------------------------------------------------
  125. CARLA_BACKEND_END_NAMESPACE