Collection of tools useful for audio production
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.

158 lines
4.1KB

  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_plugin.hpp"
  19. CARLA_BACKEND_START_NAMESPACE
  20. // -----------------------------------------------------------------------
  21. CarlaEngineThread::CarlaEngineThread(CarlaEngine* const engine_, QObject* const parent)
  22. : QThread(parent),
  23. engine(engine_)
  24. {
  25. qDebug("CarlaEngineThread::CarlaEngineThread(%p, %p)", engine, parent);
  26. CARLA_ASSERT(engine);
  27. m_stopNow = true;
  28. }
  29. CarlaEngineThread::~CarlaEngineThread()
  30. {
  31. qDebug("CarlaEngineThread::~CarlaEngineThread()");
  32. CARLA_ASSERT(m_stopNow);
  33. }
  34. // -----------------------------------------------------------------------
  35. void CarlaEngineThread::startNow()
  36. {
  37. qDebug("CarlaEngineThread::startNow()");
  38. CARLA_ASSERT(m_stopNow);
  39. m_stopNow = false;
  40. start(QThread::HighPriority);
  41. }
  42. void CarlaEngineThread::stopNow()
  43. {
  44. qDebug("CarlaEngineThread::stopNow()");
  45. if (m_stopNow)
  46. return;
  47. m_stopNow = true;
  48. const ScopedLocker m(this);
  49. if (isRunning() && ! wait(200))
  50. {
  51. quit();
  52. if (isRunning() && ! wait(300))
  53. terminate();
  54. }
  55. }
  56. // -----------------------------------------------------------------------
  57. void CarlaEngineThread::run()
  58. {
  59. qDebug("CarlaEngineThread::run()");
  60. CARLA_ASSERT(engine->isRunning());
  61. bool oscRegisted, usesSingleThread;
  62. unsigned short i;
  63. double value;
  64. while (engine->isRunning() && ! m_stopNow)
  65. {
  66. const ScopedLocker m(this);
  67. #ifndef BUILD_BRIDGE
  68. oscRegisted = engine->isOscControlRegistered();
  69. #else
  70. oscRegisted = engine->isOscBridgeRegistered();
  71. #endif
  72. for (i=0; i < engine->maxPluginNumber(); i++)
  73. {
  74. CarlaPlugin* const plugin = engine->getPluginUnchecked(i);
  75. if (! (plugin && plugin->enabled()))
  76. continue;
  77. #ifndef BUILD_BRIDGE
  78. const unsigned short id = plugin->id();
  79. #endif
  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. engine->osc_send_bridge_set_parameter_value(j, value);
  102. #else
  103. engine->osc_send_control_set_parameter_value(id, j, value);
  104. #endif
  105. }
  106. }
  107. // ---------------------------------------------------
  108. // Update OSC control client peaks
  109. if (oscRegisted)
  110. {
  111. #ifdef BUILD_BRIDGE
  112. engine->osc_send_peaks(plugin);
  113. #else
  114. engine->osc_send_peaks(plugin, id);
  115. #endif
  116. }
  117. }
  118. }
  119. engine->idleOsc();
  120. msleep(oscRegisted ? 40 : 50);
  121. }
  122. }
  123. CARLA_BACKEND_END_NAMESPACE