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.

304 lines
9.3KB

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