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.

152 lines
3.9KB

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