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.

166 lines
4.6KB

  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. CarlaEngineThread::CarlaEngineThread(CarlaEngine* const engine_, QObject* const parent)
  21. : QThread(parent),
  22. engine(engine_)
  23. {
  24. qDebug("CarlaEngineThread::CarlaEngineThread(%p, %p)", engine, parent);
  25. CARLA_ASSERT(engine);
  26. m_stopNow = true;
  27. }
  28. CarlaEngineThread::~CarlaEngineThread()
  29. {
  30. qDebug("CarlaEngineThread::~CarlaEngineThread()");
  31. CARLA_ASSERT(m_stopNow);
  32. }
  33. void CarlaEngineThread::startNow()
  34. {
  35. qDebug("CarlaEngineThread::startNow()");
  36. CARLA_ASSERT(m_stopNow);
  37. m_stopNow = false;
  38. start(QThread::HighPriority);
  39. }
  40. void CarlaEngineThread::stopNow()
  41. {
  42. qDebug("CarlaEngineThread::stopNow()");
  43. if (m_stopNow)
  44. return;
  45. m_stopNow = true;
  46. const ScopedLocker m(this);
  47. if (isRunning() && ! wait(200))
  48. {
  49. quit();
  50. if (isRunning() && ! wait(300))
  51. terminate();
  52. }
  53. }
  54. void CarlaEngineThread::run()
  55. {
  56. qDebug("CarlaEngineThread::run()");
  57. CARLA_ASSERT(engine->isRunning());
  58. bool oscControlRegisted, usesSingleThread;
  59. unsigned short i;
  60. double value;
  61. while (engine->isRunning() && ! m_stopNow)
  62. {
  63. const ScopedLocker m(this);
  64. oscControlRegisted = engine->isOscControlRegisted();
  65. for (i=0; i < engine->maxPluginNumber(); i++)
  66. {
  67. CarlaPlugin* const plugin = engine->getPluginUnchecked(i);
  68. if (plugin && plugin->enabled())
  69. {
  70. #ifndef BUILD_BRIDGE
  71. const unsigned short id = plugin->id();
  72. #endif
  73. usesSingleThread = (plugin->hints() & PLUGIN_USES_SINGLE_THREAD);
  74. // -------------------------------------------------------
  75. // Process postponed events
  76. if (! usesSingleThread)
  77. plugin->postEventsRun();
  78. // -------------------------------------------------------
  79. // Update parameter outputs
  80. if (oscControlRegisted || ! usesSingleThread)
  81. {
  82. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  83. {
  84. if (! plugin->parameterIsOutput(i))
  85. continue;
  86. value = plugin->getParameterValue(i);
  87. // Update UI
  88. if (! usesSingleThread)
  89. plugin->uiParameterChange(i, value);
  90. // Update OSC control client
  91. if (oscControlRegisted)
  92. {
  93. #ifdef BUILD_BRIDGE
  94. engine->osc_send_bridge_set_parameter_value(i, value);
  95. #else
  96. engine->osc_send_control_set_parameter_value(id, i, value);
  97. #endif
  98. }
  99. }
  100. }
  101. // -------------------------------------------------------
  102. // Update OSC control client (peaks)
  103. if (oscControlRegisted)
  104. {
  105. // Peak values
  106. if (plugin->audioInCount() > 0)
  107. {
  108. #ifdef BUILD_BRIDGE
  109. engine->osc_send_bridge_set_inpeak(1);
  110. engine->osc_send_bridge_set_inpeak(2);
  111. #else
  112. engine->osc_send_control_set_input_peak_value(id, 1);
  113. engine->osc_send_control_set_input_peak_value(id, 2);
  114. #endif
  115. }
  116. if (plugin->audioOutCount() > 0)
  117. {
  118. #ifdef BUILD_BRIDGE
  119. engine->osc_send_bridge_set_outpeak(1);
  120. engine->osc_send_bridge_set_outpeak(2);
  121. #else
  122. engine->osc_send_control_set_output_peak_value(id, 1);
  123. engine->osc_send_control_set_output_peak_value(id, 2);
  124. #endif
  125. }
  126. }
  127. }
  128. }
  129. #ifndef BUILD_BRIDGE
  130. if (! engine->idleOsc())
  131. #endif
  132. msleep(50);
  133. }
  134. }
  135. CARLA_BACKEND_END_NAMESPACE