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.

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