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.

162 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 id;
  60. double value;
  61. while (engine->isRunning() && ! m_stopNow)
  62. {
  63. const ScopedLocker m(this);
  64. oscControlRegisted = engine->isOscControlRegisted();
  65. for (unsigned short i=0; i < engine->maxPluginNumber(); i++)
  66. {
  67. CarlaPlugin* const plugin = engine->getPluginUnchecked(i);
  68. if (plugin && plugin->enabled())
  69. {
  70. id = plugin->id();
  71. usesSingleThread = (plugin->hints() & PLUGIN_USES_SINGLE_THREAD);
  72. // -------------------------------------------------------
  73. // Process postponed events
  74. if (! usesSingleThread)
  75. plugin->postEventsRun();
  76. // -------------------------------------------------------
  77. // Update parameter outputs
  78. if (oscControlRegisted || ! usesSingleThread)
  79. {
  80. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  81. {
  82. if (! plugin->parameterIsOutput(i))
  83. continue;
  84. value = plugin->getParameterValue(i);
  85. // Update UI
  86. if (! usesSingleThread)
  87. plugin->uiParameterChange(i, value);
  88. // Update OSC control client
  89. if (oscControlRegisted)
  90. {
  91. #ifdef BUILD_BRIDGE
  92. engine->osc_send_bridge_set_parameter_value(i, value);
  93. #else
  94. engine->osc_send_control_set_parameter_value(id, i, value);
  95. #endif
  96. }
  97. }
  98. }
  99. // -------------------------------------------------------
  100. // Update OSC control client (peaks)
  101. if (oscControlRegisted)
  102. {
  103. // Peak values
  104. if (plugin->audioInCount() > 0)
  105. {
  106. #ifdef BUILD_BRIDGE
  107. engine->osc_send_bridge_set_inpeak(1);
  108. engine->osc_send_bridge_set_inpeak(2);
  109. #else
  110. engine->osc_send_control_set_input_peak_value(id, 1);
  111. engine->osc_send_control_set_input_peak_value(id, 2);
  112. #endif
  113. }
  114. if (plugin->audioOutCount() > 0)
  115. {
  116. #ifdef BUILD_BRIDGE
  117. engine->osc_send_bridge_set_outpeak(1);
  118. engine->osc_send_bridge_set_outpeak(2);
  119. #else
  120. engine->osc_send_control_set_output_peak_value(id, 1);
  121. engine->osc_send_control_set_output_peak_value(id, 2);
  122. #endif
  123. }
  124. }
  125. }
  126. }
  127. if (! engine->idleOsc())
  128. msleep(50);
  129. }
  130. }
  131. CARLA_BACKEND_END_NAMESPACE