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.

310 lines
9.5KB

  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. for (unsigned short i=0; i < 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. {
  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. // -------------------------------------------------------
  101. // Update OSC control client
  102. if (oscControlRegisted)
  103. {
  104. // Peak values
  105. if (plugin->audioInCount() > 0)
  106. {
  107. #ifdef BUILD_BRIDGE
  108. engine->osc_send_bridge_set_input_peak_value(1, engine->getInputPeak(id, 0));
  109. engine->osc_send_bridge_set_input_peak_value(2, engine->getInputPeak(id, 1));
  110. #else
  111. engine->osc_send_control_set_input_peak_value(id, 1, engine->getInputPeak(id, 0));
  112. engine->osc_send_control_set_input_peak_value(id, 2, engine->getInputPeak(id, 1));
  113. #endif
  114. }
  115. if (plugin->audioOutCount() > 0)
  116. {
  117. #ifdef BUILD_BRIDGE
  118. engine->osc_send_bridge_set_output_peak_value(1, engine->getOutputPeak(id, 0));
  119. engine->osc_send_bridge_set_output_peak_value(2, engine->getOutputPeak(id, 1));
  120. #else
  121. engine->osc_send_control_set_output_peak_value(id, 1, engine->getOutputPeak(id, 0));
  122. engine->osc_send_control_set_output_peak_value(id, 2, engine->getOutputPeak(id, 1));
  123. #endif
  124. }
  125. }
  126. }
  127. }
  128. msleep(50);
  129. }
  130. }
  131. // -----------------------------------------------------------------------
  132. // CarlaPluginThread
  133. #ifndef BUILD_BRIDGE
  134. const char* PluginThreadMode2str(const CarlaPluginThread::PluginThreadMode mode)
  135. {
  136. switch (mode)
  137. {
  138. case CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI:
  139. return "PLUGIN_THREAD_DSSI_GUI";
  140. case CarlaPluginThread::PLUGIN_THREAD_LV2_GUI:
  141. return "PLUGIN_THREAD_LV2_GUI";
  142. case CarlaPluginThread::PLUGIN_THREAD_VST_GUI:
  143. return "PLUGIN_THREAD_VST_GUI";
  144. case CarlaPluginThread::PLUGIN_THREAD_BRIDGE:
  145. return "PLUGIN_THREAD_BRIDGE";
  146. }
  147. qWarning("CarlaPluginThread::PluginThreadMode2str(%i) - invalid mode", mode);
  148. return nullptr;
  149. }
  150. CarlaPluginThread::CarlaPluginThread(CarlaBackend::CarlaEngine* const engine_, CarlaBackend::CarlaPlugin* const plugin_, const PluginThreadMode mode_, QObject* const parent)
  151. : QThread(parent),
  152. engine(engine_),
  153. plugin(plugin_),
  154. mode(mode_)
  155. {
  156. qDebug("CarlaPluginThread::CarlaPluginThread(plugin:\"%s\", engine:\"%s\", %s)", plugin->name(), engine->getName(), PluginThreadMode2str(mode));
  157. m_process = nullptr;
  158. }
  159. CarlaPluginThread::~CarlaPluginThread()
  160. {
  161. if (m_process)
  162. delete m_process;
  163. }
  164. void CarlaPluginThread::setOscData(const char* const binary, const char* const label, const char* const data1)
  165. {
  166. m_binary = QString(binary);
  167. m_label = QString(label);
  168. m_data1 = QString(data1);
  169. }
  170. void CarlaPluginThread::run()
  171. {
  172. qDebug("CarlaPluginThread::run()");
  173. if (! m_process)
  174. m_process = new QProcess(nullptr);
  175. m_process->setProcessChannelMode(QProcess::ForwardedChannels);
  176. QStringList arguments;
  177. switch (mode)
  178. {
  179. case PLUGIN_THREAD_DSSI_GUI:
  180. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPathUDP()).arg(plugin->id());
  181. /* filename */ arguments << plugin->filename();
  182. /* label */ arguments << m_label;
  183. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  184. break;
  185. case PLUGIN_THREAD_LV2_GUI:
  186. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPathTCP()).arg(plugin->id());
  187. /* URI */ arguments << m_label;
  188. /* ui-URI */ arguments << m_data1;
  189. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  190. break;
  191. case PLUGIN_THREAD_VST_GUI:
  192. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPathTCP()).arg(plugin->id());
  193. /* filename */ arguments << plugin->filename();
  194. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  195. break;
  196. case PLUGIN_THREAD_BRIDGE:
  197. {
  198. const char* name = plugin->name();
  199. if (! name)
  200. name = "(none)";
  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. }
  209. qDebug() << m_binary;
  210. qDebug() << arguments;
  211. m_process->start(m_binary, arguments);
  212. m_process->waitForStarted();
  213. switch (mode)
  214. {
  215. case PLUGIN_THREAD_DSSI_GUI:
  216. case PLUGIN_THREAD_LV2_GUI:
  217. case PLUGIN_THREAD_VST_GUI:
  218. if (plugin->showOscGui())
  219. {
  220. m_process->waitForFinished(-1);
  221. if (m_process->exitCode() == 0)
  222. {
  223. // Hide
  224. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  225. qWarning("CarlaPluginThread::run() - GUI closed");
  226. }
  227. else
  228. {
  229. // Kill
  230. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), -1, 0, 0.0);
  231. qWarning("CarlaPluginThread::run() - GUI crashed");
  232. break;
  233. }
  234. }
  235. else
  236. {
  237. qDebug("CarlaPluginThread::run() - GUI timeout");
  238. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  239. }
  240. break;
  241. case PLUGIN_THREAD_BRIDGE:
  242. qDebug("CarlaPluginThread::run() - bridge starting...");
  243. m_process->waitForFinished(-1);
  244. qDebug("CarlaPluginThread::run() - bridge ended");
  245. #ifdef DEBUG
  246. if (m_process->exitCode() == 0)
  247. qDebug("CarlaPluginThread::run() - bridge closed");
  248. else
  249. qDebug("CarlaPluginThread::run() - bridge crashed");
  250. qDebug("%s", QString(m_process->readAllStandardOutput()).toUtf8().constData());
  251. #endif
  252. break;
  253. }
  254. }
  255. #endif