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.

144 lines
3.7KB

  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. continue;
  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. if (oscControlRegisted || ! usesSingleThread)
  79. {
  80. // ---------------------------------------------------
  81. // Update parameter outputs
  82. for (uint32_t j=0; j < plugin->parameterCount(); j++)
  83. {
  84. if (! plugin->parameterIsOutput(j))
  85. continue;
  86. value = plugin->getParameterValue(j);
  87. // Update UI
  88. if (! usesSingleThread)
  89. plugin->uiParameterChange(j, value);
  90. // Update OSC control client
  91. if (oscControlRegisted)
  92. {
  93. #ifdef BUILD_BRIDGE
  94. engine->osc_send_bridge_set_parameter_value(j, value);
  95. #else
  96. engine->osc_send_control_set_parameter_value(id, j, value);
  97. #endif
  98. }
  99. }
  100. // ---------------------------------------------------
  101. // Update OSC control client peaks
  102. if (oscControlRegisted)
  103. engine->osc_send_peaks(plugin, id);
  104. }
  105. }
  106. #ifndef BUILD_BRIDGE
  107. if (! engine->idleOsc())
  108. #endif
  109. msleep(50);
  110. }
  111. }
  112. CARLA_BACKEND_END_NAMESPACE