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.

carla_engine_thread.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Carla Engine Thread
  3. * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING 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. engine(engine_)
  25. {
  26. qDebug("CarlaEngineThread::CarlaEngineThread(%p, %p)", engine, parent);
  27. CARLA_ASSERT(engine);
  28. m_stopNow = true;
  29. }
  30. CarlaEngineThread::~CarlaEngineThread()
  31. {
  32. qDebug("CarlaEngineThread::~CarlaEngineThread()");
  33. CARLA_ASSERT(m_stopNow);
  34. }
  35. // -----------------------------------------------------------------------
  36. void CarlaEngineThread::startNow()
  37. {
  38. qDebug("CarlaEngineThread::startNow()");
  39. CARLA_ASSERT(m_stopNow);
  40. m_stopNow = false;
  41. start(QThread::HighPriority);
  42. }
  43. void CarlaEngineThread::stopNow()
  44. {
  45. qDebug("CarlaEngineThread::stopNow()");
  46. if (m_stopNow)
  47. return;
  48. m_stopNow = true;
  49. const ScopedLocker m(this);
  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(engine->isRunning());
  62. bool oscRegisted, usesSingleThread;
  63. unsigned short i;
  64. double value;
  65. while (engine->isRunning() && ! m_stopNow)
  66. {
  67. const ScopedLocker m(this);
  68. #ifndef BUILD_BRIDGE
  69. oscRegisted = engine->isOscControlRegistered();
  70. #else
  71. oscRegisted = engine->isOscBridgeRegistered();
  72. #endif
  73. for (i=0; i < engine->maxPluginNumber(); i++)
  74. {
  75. CarlaPlugin* const plugin = engine->getPluginUnchecked(i);
  76. if (! (plugin && plugin->enabled()))
  77. continue;
  78. #ifndef BUILD_BRIDGE
  79. const unsigned short id = plugin->id();
  80. #endif
  81. usesSingleThread = (plugin->hints() & PLUGIN_USES_SINGLE_THREAD);
  82. // -------------------------------------------------------
  83. // Process postponed events
  84. if (! usesSingleThread)
  85. plugin->postEventsRun();
  86. if (oscRegisted || ! usesSingleThread)
  87. {
  88. // ---------------------------------------------------
  89. // Update parameter outputs
  90. for (uint32_t j=0; j < plugin->parameterCount(); j++)
  91. {
  92. if (! plugin->parameterIsOutput(j))
  93. continue;
  94. value = plugin->getParameterValue(j);
  95. // Update UI
  96. if (! usesSingleThread)
  97. plugin->uiParameterChange(j, value);
  98. // Update OSC engine client
  99. if (oscRegisted)
  100. {
  101. #ifdef BUILD_BRIDGE
  102. engine->osc_send_bridge_set_parameter_value(j, value);
  103. #else
  104. engine->osc_send_control_set_parameter_value(id, j, value);
  105. #endif
  106. }
  107. }
  108. // ---------------------------------------------------
  109. // Update OSC control client peaks
  110. if (oscRegisted)
  111. {
  112. #ifdef BUILD_BRIDGE
  113. engine->osc_send_peaks(plugin);
  114. #else
  115. engine->osc_send_peaks(plugin, id);
  116. #endif
  117. }
  118. }
  119. }
  120. engine->idleOsc();
  121. msleep(oscRegisted ? 40 : 50);
  122. }
  123. }
  124. CARLA_BACKEND_END_NAMESPACE