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.

161 lines
4.2KB

  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. fEngine(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(fEngine->isRunning());
  62. bool oscRegisted, usesSingleThread;
  63. int i, count;
  64. double value;
  65. while (fEngine->isRunning() && ! fStopNow)
  66. {
  67. const CarlaMutex::ScopedLocker sl(&fMutex);
  68. #ifndef BUILD_BRIDGE
  69. oscRegisted = fEngine->isOscControlRegistered();
  70. #else
  71. oscRegisted = fEngine->isOscBridgeRegistered();
  72. #endif
  73. for (i=0, count = fEngine->currentPluginCount(); i < count; i++)
  74. {
  75. CarlaPlugin* const plugin = fEngine->getPluginUnchecked(i);
  76. #if 0
  77. if (! (plugin && plugin->enabled()))
  78. continue;
  79. CARLA_ASSERT(i == plugin->id());
  80. usesSingleThread = (plugin->hints() & PLUGIN_USES_SINGLE_THREAD);
  81. // -------------------------------------------------------
  82. // Process postponed events
  83. if (! usesSingleThread)
  84. plugin->postEventsRun();
  85. if (oscRegisted || ! usesSingleThread)
  86. {
  87. // ---------------------------------------------------
  88. // Update parameter outputs
  89. for (uint32_t j=0; j < plugin->parameterCount(); j++)
  90. {
  91. if (! plugin->parameterIsOutput(j))
  92. continue;
  93. value = plugin->getParameterValue(j);
  94. // Update UI
  95. if (! usesSingleThread)
  96. plugin->uiParameterChange(j, value);
  97. // Update OSC engine client
  98. if (oscRegisted)
  99. {
  100. #ifdef BUILD_BRIDGE
  101. fEngine->osc_send_bridge_set_parameter_value(j, value);
  102. #else
  103. fEngine->osc_send_control_set_parameter_value(i, j, value);
  104. #endif
  105. }
  106. }
  107. // ---------------------------------------------------
  108. // Update OSC control client peaks
  109. if (oscRegisted)
  110. {
  111. #ifdef BUILD_BRIDGE
  112. fEngine->osc_send_peaks(plugin);
  113. #else
  114. fEngine->osc_send_peaks(plugin, i);
  115. #endif
  116. }
  117. }
  118. #endif
  119. }
  120. fEngine->idleOsc();
  121. msleep(oscRegisted ? 40 : 50);
  122. }
  123. }
  124. CARLA_BACKEND_END_NAMESPACE