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.4KB

  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. 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 oscControlRegisted, 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. oscControlRegisted = engine->isOscControlRegisted();
  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 (oscControlRegisted || ! 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 (oscControlRegisted)
  87. {
  88. #ifdef BUILD_BRIDGE
  89. engine->osc_send_bridge_set_parameter_value(i, value);
  90. #else
  91. engine->osc_send_control_set_parameter_value(id, i, value);
  92. #endif
  93. }
  94. }
  95. }
  96. }
  97. // -------------------------------------------------------
  98. // Update OSC control client
  99. if (oscControlRegisted)
  100. {
  101. // Peak values
  102. if (plugin->audioInCount() > 0)
  103. {
  104. #ifdef BUILD_BRIDGE
  105. engine->osc_send_bridge_set_input_peak_value(1, engine->getInputPeak(id, 0));
  106. engine->osc_send_bridge_set_input_peak_value(2, engine->getInputPeak(id, 1));
  107. #else
  108. engine->osc_send_control_set_input_peak_value(id, 1, engine->getInputPeak(id, 0));
  109. engine->osc_send_control_set_input_peak_value(id, 2, engine->getInputPeak(id, 1));
  110. #endif
  111. }
  112. if (plugin->audioOutCount() > 0)
  113. {
  114. #ifdef BUILD_BRIDGE
  115. engine->osc_send_bridge_set_output_peak_value(1, engine->getOutputPeak(id, 0));
  116. engine->osc_send_bridge_set_output_peak_value(2, engine->getOutputPeak(id, 1));
  117. #else
  118. engine->osc_send_control_set_output_peak_value(id, 1, engine->getOutputPeak(id, 0));
  119. engine->osc_send_control_set_output_peak_value(id, 2, engine->getOutputPeak(id, 1));
  120. #endif
  121. }
  122. }
  123. }
  124. }
  125. msleep(50);
  126. }
  127. }
  128. // -----------------------------------------------------------------------
  129. // CarlaPluginThread
  130. #ifndef BUILD_BRIDGE
  131. const char* PluginThreadMode2str(const CarlaPluginThread::PluginThreadMode mode)
  132. {
  133. switch (mode)
  134. {
  135. case CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI:
  136. return "PLUGIN_THREAD_DSSI_GUI";
  137. case CarlaPluginThread::PLUGIN_THREAD_LV2_GUI:
  138. return "PLUGIN_THREAD_LV2_GUI";
  139. case CarlaPluginThread::PLUGIN_THREAD_VST_GUI:
  140. return "PLUGIN_THREAD_VST_GUI";
  141. case CarlaPluginThread::PLUGIN_THREAD_BRIDGE:
  142. return "PLUGIN_THREAD_BRIDGE";
  143. }
  144. qWarning("CarlaPluginThread::PluginThreadMode2str(%i) - invalid mode", mode);
  145. return nullptr;
  146. }
  147. CarlaPluginThread::CarlaPluginThread(CarlaBackend::CarlaEngine* const engine_, CarlaBackend::CarlaPlugin* const plugin_, const PluginThreadMode mode_, QObject* const parent) :
  148. QThread(parent),
  149. engine(engine_),
  150. plugin(plugin_),
  151. mode(mode_)
  152. {
  153. qDebug("CarlaPluginThread::CarlaPluginThread(plugin:\"%s\", engine:\"%s\", %s)", plugin->name(), engine->getName(), PluginThreadMode2str(mode));
  154. m_process = nullptr;
  155. }
  156. CarlaPluginThread::~CarlaPluginThread()
  157. {
  158. if (m_process)
  159. delete m_process;
  160. }
  161. void CarlaPluginThread::setOscData(const char* const binary, const char* const label, const char* const data1)
  162. {
  163. m_binary = QString(binary);
  164. m_label = QString(label);
  165. m_data1 = QString(data1);
  166. }
  167. void CarlaPluginThread::run()
  168. {
  169. qDebug("CarlaPluginThread::run()");
  170. if (! m_process)
  171. m_process = new QProcess(nullptr);
  172. m_process->setProcessChannelMode(QProcess::ForwardedChannels);
  173. QStringList arguments;
  174. switch (mode)
  175. {
  176. case PLUGIN_THREAD_DSSI_GUI:
  177. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPathUDP()).arg(plugin->id());
  178. /* filename */ arguments << plugin->filename();
  179. /* label */ arguments << m_label;
  180. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  181. break;
  182. case PLUGIN_THREAD_LV2_GUI:
  183. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPathTCP()).arg(plugin->id());
  184. /* URI */ arguments << m_label;
  185. /* ui-URI */ arguments << m_data1;
  186. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  187. break;
  188. case PLUGIN_THREAD_VST_GUI:
  189. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPathTCP()).arg(plugin->id());
  190. /* filename */ arguments << plugin->filename();
  191. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  192. break;
  193. case PLUGIN_THREAD_BRIDGE:
  194. {
  195. const char* name = plugin->name();
  196. if (! name)
  197. name = "(none)";
  198. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPathTCP()).arg(plugin->id());
  199. /* stype */ arguments << m_data1;
  200. /* filename */ arguments << plugin->filename();
  201. /* name */ arguments << name;
  202. /* label */ arguments << m_label;
  203. break;
  204. }
  205. }
  206. qDebug() << m_binary;
  207. qDebug() << arguments;
  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. qDebug("CarlaPluginThread::run() - bridge starting...");
  240. m_process->waitForFinished(-1);
  241. qDebug("CarlaPluginThread::run() - bridge ended");
  242. #ifdef DEBUG
  243. if (m_process->exitCode() == 0)
  244. qDebug("CarlaPluginThread::run() - bridge closed");
  245. else
  246. qDebug("CarlaPluginThread::run() - bridge crashed");
  247. qDebug("%s", QString(m_process->readAllStandardOutput()).toUtf8().constData());
  248. #endif
  249. break;
  250. }
  251. }
  252. #endif