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.

159 lines
4.2KB

  1. /*
  2. * Carla Engine Thread
  3. * Copyright (C) 2012-2013 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 GPL.txt file
  16. */
  17. #include "carla_engine.hpp"
  18. #include "carla_engine_thread.hpp"
  19. #include "carla_plugin.hpp"
  20. CARLA_BACKEND_START_NAMESPACE
  21. // -----------------------------------------------------------------------
  22. CarlaEngineThread::CarlaEngineThread(CarlaEngine* const engine, QObject* const parent)
  23. : QThread(parent),
  24. kEngine(engine)
  25. {
  26. qDebug("CarlaEngineThread::CarlaEngineThread(%p, %p)", engine, parent);
  27. CARLA_ASSERT(engine);
  28. fStopNow = true;
  29. }
  30. CarlaEngineThread::~CarlaEngineThread()
  31. {
  32. qDebug("CarlaEngineThread::~CarlaEngineThread()");
  33. CARLA_ASSERT(fStopNow);
  34. }
  35. // -----------------------------------------------------------------------
  36. void CarlaEngineThread::startNow()
  37. {
  38. qDebug("CarlaEngineThread::startNow()");
  39. CARLA_ASSERT(fStopNow);
  40. fStopNow = false;
  41. start(QThread::HighPriority);
  42. }
  43. void CarlaEngineThread::stopNow()
  44. {
  45. qDebug("CarlaEngineThread::stopNow()");
  46. if (fStopNow)
  47. return;
  48. fStopNow = true;
  49. const CarlaMutex::ScopedLocker sl(&fMutex);
  50. if (isRunning() && ! wait(200))
  51. {
  52. quit();
  53. if (isRunning() && ! wait(300))
  54. terminate();
  55. }
  56. }
  57. // -----------------------------------------------------------------------
  58. void CarlaEngineThread::run()
  59. {
  60. qDebug("CarlaEngineThread::run()");
  61. CARLA_ASSERT(kEngine->isRunning());
  62. bool oscRegisted, usesSingleThread;
  63. int i, count;
  64. double value;
  65. while (kEngine->isRunning() && ! fStopNow)
  66. {
  67. const CarlaMutex::ScopedLocker sl(&fMutex);
  68. #ifdef BUILD_BRIDGE
  69. oscRegisted = kEngine->isOscBridgeRegistered();
  70. #else
  71. oscRegisted = kEngine->isOscControlRegistered();
  72. #endif
  73. for (i=0, count = kEngine->currentPluginCount(); i < count; i++)
  74. {
  75. CarlaPlugin* const plugin = kEngine->getPluginUnchecked(i);
  76. if (plugin == nullptr || ! plugin->enabled())
  77. continue;
  78. CARLA_ASSERT(i == plugin->id());
  79. usesSingleThread = (plugin->hints() & PLUGIN_USES_SINGLE_THREAD);
  80. // -------------------------------------------------------
  81. // Process postponed events
  82. if (oscRegisted || ! usesSingleThread)
  83. {
  84. if (! usesSingleThread)
  85. plugin->postRtEventsRun();
  86. // ---------------------------------------------------
  87. // Update parameter outputs
  88. for (uint32_t j=0; j < plugin->parameterCount(); j++)
  89. {
  90. if (! plugin->parameterIsOutput(j))
  91. continue;
  92. value = plugin->getParameterValue(j);
  93. // Update UI
  94. if (! usesSingleThread)
  95. plugin->uiParameterChange(j, value);
  96. // Update OSC engine client
  97. if (oscRegisted)
  98. {
  99. #ifdef BUILD_BRIDGE
  100. kEngine->osc_send_bridge_set_parameter_value(j, value);
  101. #else
  102. kEngine->osc_send_control_set_parameter_value(i, j, value);
  103. #endif
  104. }
  105. }
  106. // ---------------------------------------------------
  107. // Update OSC control client peaks
  108. if (oscRegisted)
  109. {
  110. #ifdef BUILD_BRIDGE
  111. kEngine->osc_send_peaks(plugin);
  112. #else
  113. kEngine->osc_send_peaks(plugin, i);
  114. #endif
  115. }
  116. }
  117. }
  118. kEngine->idleOsc();
  119. msleep(oscRegisted ? 40 : 50);
  120. }
  121. }
  122. CARLA_BACKEND_END_NAMESPACE