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.

306 lines
9.6KB

  1. /*
  2. * Carla Backend
  3. * Copyright (C) 2011-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_threads.h"
  18. #include "carla_plugin.h"
  19. #include <QtCore/QDebug>
  20. #include <QtCore/QProcess>
  21. // -----------------------------------------------------------------------
  22. // CarlaCheckThread
  23. CarlaCheckThread::CarlaCheckThread(CarlaBackend::CarlaEngine* const engine_, QObject* const parent)
  24. : QThread(parent),
  25. engine(engine_)
  26. {
  27. qDebug("CarlaCheckThread::CarlaCheckThread(%p, %p)", engine, parent);
  28. CARLA_ASSERT(engine);
  29. }
  30. CarlaCheckThread::~CarlaCheckThread()
  31. {
  32. qDebug("CarlaCheckThread::~CarlaCheckThread()");
  33. }
  34. void CarlaCheckThread::startNow()
  35. {
  36. qDebug("CarlaCheckThread::startNow()");
  37. start(QThread::HighPriority);
  38. }
  39. void CarlaCheckThread::stopNow()
  40. {
  41. if (m_stopNow)
  42. return;
  43. m_stopNow = true;
  44. // TESTING - let processing finish first
  45. QMutexLocker(&this->mutex); // FIXME
  46. if (isRunning() && ! wait(200))
  47. {
  48. quit();
  49. if (isRunning() && ! wait(300))
  50. terminate();
  51. }
  52. }
  53. void CarlaCheckThread::run()
  54. {
  55. qDebug("CarlaCheckThread::run()");
  56. using namespace CarlaBackend;
  57. bool oscControlRegisted, usesSingleThread;
  58. unsigned short id, maxPluginNumber = CarlaEngine::maxPluginNumber();
  59. double value;
  60. m_stopNow = false;
  61. while (engine->isRunning() && ! m_stopNow)
  62. {
  63. const ScopedLocker m(this);
  64. oscControlRegisted = engine->isOscControlRegisted();
  65. #ifndef BUILD_BRIDGE
  66. engine->oscWaitEvents();
  67. #endif
  68. for (unsigned short i=0; i < maxPluginNumber; i++)
  69. {
  70. CarlaPlugin* const plugin = engine->getPluginUnchecked(i);
  71. if (plugin && plugin->enabled())
  72. {
  73. id = plugin->id();
  74. usesSingleThread = (plugin->hints() & PLUGIN_USES_SINGLE_THREAD);
  75. // -------------------------------------------------------
  76. // Process postponed events
  77. if (! usesSingleThread)
  78. plugin->postEventsRun();
  79. // -------------------------------------------------------
  80. // Update parameter outputs
  81. if (oscControlRegisted || ! usesSingleThread)
  82. {
  83. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  84. {
  85. if (plugin->parameterIsOutput(i))
  86. {
  87. value = plugin->getParameterValue(i);
  88. // Update UI
  89. if (! usesSingleThread)
  90. plugin->uiParameterChange(i, value);
  91. // Update OSC control client
  92. if (oscControlRegisted)
  93. {
  94. #ifdef BUILD_BRIDGE
  95. engine->osc_send_bridge_set_parameter_value(i, value);
  96. #else
  97. engine->osc_send_control_set_parameter_value(id, i, value);
  98. #endif
  99. }
  100. }
  101. }
  102. }
  103. // -------------------------------------------------------
  104. // Update OSC control client
  105. if (oscControlRegisted)
  106. {
  107. // Peak values
  108. if (plugin->audioInCount() > 0)
  109. {
  110. #ifdef BUILD_BRIDGE
  111. engine->osc_send_bridge_set_inpeak(1, engine->getInputPeak(id, 0));
  112. engine->osc_send_bridge_set_inpeak(2, engine->getInputPeak(id, 1));
  113. #else
  114. engine->osc_send_control_set_input_peak_value(id, 1, engine->getInputPeak(id, 0));
  115. engine->osc_send_control_set_input_peak_value(id, 2, engine->getInputPeak(id, 1));
  116. #endif
  117. }
  118. if (plugin->audioOutCount() > 0)
  119. {
  120. #ifdef BUILD_BRIDGE
  121. engine->osc_send_bridge_set_outpeak(1, engine->getOutputPeak(id, 0));
  122. engine->osc_send_bridge_set_outpeak(2, engine->getOutputPeak(id, 1));
  123. #else
  124. engine->osc_send_control_set_output_peak_value(id, 1, engine->getOutputPeak(id, 0));
  125. engine->osc_send_control_set_output_peak_value(id, 2, engine->getOutputPeak(id, 1));
  126. #endif
  127. }
  128. }
  129. }
  130. }
  131. msleep(50);
  132. }
  133. }
  134. // -----------------------------------------------------------------------
  135. // CarlaPluginThread
  136. #ifndef BUILD_BRIDGE
  137. const char* PluginThreadMode2str(const CarlaPluginThread::PluginThreadMode mode)
  138. {
  139. switch (mode)
  140. {
  141. case CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI:
  142. return "PLUGIN_THREAD_DSSI_GUI";
  143. case CarlaPluginThread::PLUGIN_THREAD_LV2_GUI:
  144. return "PLUGIN_THREAD_LV2_GUI";
  145. case CarlaPluginThread::PLUGIN_THREAD_VST_GUI:
  146. return "PLUGIN_THREAD_VST_GUI";
  147. case CarlaPluginThread::PLUGIN_THREAD_BRIDGE:
  148. return "PLUGIN_THREAD_BRIDGE";
  149. }
  150. qWarning("CarlaPluginThread::PluginThreadMode2str(%i) - invalid mode", mode);
  151. return nullptr;
  152. }
  153. CarlaPluginThread::CarlaPluginThread(CarlaBackend::CarlaEngine* const engine_, CarlaBackend::CarlaPlugin* const plugin_, const PluginThreadMode mode_, QObject* const parent)
  154. : QThread(parent),
  155. engine(engine_),
  156. plugin(plugin_),
  157. mode(mode_)
  158. {
  159. qDebug("CarlaPluginThread::CarlaPluginThread(plugin:\"%s\", engine:\"%s\", %s)", plugin->name(), engine->getName(), PluginThreadMode2str(mode));
  160. m_process = nullptr;
  161. }
  162. CarlaPluginThread::~CarlaPluginThread()
  163. {
  164. if (m_process)
  165. delete m_process;
  166. }
  167. void CarlaPluginThread::setOscData(const char* const binary, const char* const label, const char* const data1)
  168. {
  169. m_binary = QString(binary);
  170. m_label = QString(label);
  171. m_data1 = QString(data1);
  172. }
  173. void CarlaPluginThread::run()
  174. {
  175. qDebug("CarlaPluginThread::run()");
  176. if (! m_process)
  177. m_process = new QProcess(nullptr);
  178. m_process->setProcessChannelMode(QProcess::ForwardedChannels);
  179. QStringList arguments;
  180. const char* name = plugin->name() ? plugin->name() : "(none)";
  181. switch (mode)
  182. {
  183. case PLUGIN_THREAD_DSSI_GUI:
  184. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPathUDP()).arg(plugin->id());
  185. /* filename */ arguments << plugin->filename();
  186. /* label */ arguments << m_label;
  187. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  188. break;
  189. case PLUGIN_THREAD_LV2_GUI:
  190. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPathTCP()).arg(plugin->id());
  191. /* URI */ arguments << m_label;
  192. /* ui-URI */ arguments << m_data1;
  193. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  194. break;
  195. case PLUGIN_THREAD_VST_GUI:
  196. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPathTCP()).arg(plugin->id());
  197. /* filename */ arguments << plugin->filename();
  198. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  199. break;
  200. case PLUGIN_THREAD_BRIDGE:
  201. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPathTCP()).arg(plugin->id());
  202. /* stype */ arguments << m_data1;
  203. /* filename */ arguments << plugin->filename();
  204. /* name */ arguments << name;
  205. /* label */ arguments << m_label;
  206. break;
  207. }
  208. m_process->start(m_binary, arguments);
  209. m_process->waitForStarted();
  210. switch (mode)
  211. {
  212. case PLUGIN_THREAD_DSSI_GUI:
  213. case PLUGIN_THREAD_LV2_GUI:
  214. case PLUGIN_THREAD_VST_GUI:
  215. if (plugin->showOscGui())
  216. {
  217. m_process->waitForFinished(-1);
  218. if (m_process->exitCode() == 0)
  219. {
  220. // Hide
  221. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  222. qWarning("CarlaPluginThread::run() - GUI closed");
  223. }
  224. else
  225. {
  226. // Kill
  227. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), -1, 0, 0.0);
  228. qWarning("CarlaPluginThread::run() - GUI crashed");
  229. break;
  230. }
  231. }
  232. else
  233. {
  234. qDebug("CarlaPluginThread::run() - GUI timeout");
  235. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  236. }
  237. break;
  238. case PLUGIN_THREAD_BRIDGE:
  239. m_process->waitForFinished(-1);
  240. if (m_process->exitCode() != 0)
  241. {
  242. qWarning("CarlaPluginThread::run() - bridge crashed");
  243. QString errorString = QString("Plugin '%1' has crashed!\n"
  244. "Saving now will lose its current settings.\n"
  245. "Please remove this plugin, and not rely on it from this point.").arg(plugin->name());
  246. CarlaBackend::setLastError(errorString.toUtf8().constData());
  247. engine->callback(CarlaBackend::CALLBACK_ERROR, plugin->id(), 0, 0, 0.0);
  248. }
  249. break;
  250. }
  251. }
  252. #endif