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.

295 lines
8.8KB

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