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.

284 lines
8.6KB

  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 oscControllerRegisted, usesSingleThread;
  55. unsigned short id;
  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. CarlaBackend::CarlaPlugin* const plugin = engine->getPluginUnchecked(i);
  65. if (plugin && plugin->enabled())
  66. {
  67. id = plugin->id();
  68. usesSingleThread = (plugin->hints() & CarlaBackend::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. engine->osc_send_set_parameter_value(id, i, value);
  88. }
  89. }
  90. }
  91. // -------------------------------------------------------
  92. // Update OSC control client
  93. if (oscControllerRegisted)
  94. {
  95. // Peak values
  96. if (plugin->audioInCount() > 0)
  97. {
  98. engine->osc_send_set_input_peak_value(id, 1, engine->getInputPeak(id, 0));
  99. engine->osc_send_set_input_peak_value(id, 2, engine->getInputPeak(id, 1));
  100. }
  101. if (plugin->audioOutCount() > 0)
  102. {
  103. engine->osc_send_set_output_peak_value(id, 1, engine->getOutputPeak(id, 0));
  104. engine->osc_send_set_output_peak_value(id, 2, engine->getOutputPeak(id, 1));
  105. }
  106. }
  107. }
  108. }
  109. msleep(50);
  110. }
  111. }
  112. // -----------------------------------------------------------------------
  113. // CarlaPluginThread
  114. const char* PluginThreadMode2str(const CarlaPluginThread::PluginThreadMode mode)
  115. {
  116. switch (mode)
  117. {
  118. case CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI:
  119. return "PLUGIN_THREAD_DSSI_GUI";
  120. case CarlaPluginThread::PLUGIN_THREAD_LV2_GUI:
  121. return "PLUGIN_THREAD_LV2_GUI";
  122. case CarlaPluginThread::PLUGIN_THREAD_VST_GUI:
  123. return "PLUGIN_THREAD_VST_GUI";
  124. case CarlaPluginThread::PLUGIN_THREAD_BRIDGE:
  125. return "PLUGIN_THREAD_BRIDGE";
  126. }
  127. qWarning("CarlaPluginThread::PluginThreadMode2str(%i) - invalid mode", mode);
  128. return nullptr;
  129. }
  130. CarlaPluginThread::CarlaPluginThread(CarlaBackend::CarlaEngine* const engine_, CarlaBackend::CarlaPlugin* const plugin_, const PluginThreadMode mode_, QObject* const parent) :
  131. QThread(parent),
  132. engine(engine_),
  133. plugin(plugin_),
  134. mode(mode_)
  135. {
  136. qDebug("CarlaPluginThread::CarlaPluginThread(plugin:\"%s\", engine:\"%s\", %s)", plugin->name(), engine->getName(), PluginThreadMode2str(mode));
  137. m_process = nullptr;
  138. }
  139. CarlaPluginThread::~CarlaPluginThread()
  140. {
  141. if (m_process)
  142. delete m_process;
  143. }
  144. void CarlaPluginThread::setOscData(const char* const binary, const char* const label, const char* const data1)
  145. {
  146. m_binary = QString(binary);
  147. m_label = QString(label);
  148. m_data1 = QString(data1);
  149. }
  150. void CarlaPluginThread::run()
  151. {
  152. qDebug("CarlaPluginThread::run()");
  153. if (! m_process)
  154. m_process = new QProcess(nullptr);
  155. m_process->setProcessChannelMode(QProcess::ForwardedChannels);
  156. QStringList arguments;
  157. switch (mode)
  158. {
  159. case PLUGIN_THREAD_DSSI_GUI:
  160. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPath()).arg(plugin->id());
  161. /* filename */ arguments << plugin->filename();
  162. /* label */ arguments << m_label;
  163. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  164. break;
  165. case PLUGIN_THREAD_LV2_GUI:
  166. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPath()).arg(plugin->id());
  167. /* URI */ arguments << m_label;
  168. /* ui-URI */ arguments << m_data1;
  169. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  170. break;
  171. case PLUGIN_THREAD_VST_GUI:
  172. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPath()).arg(plugin->id());
  173. /* filename */ arguments << plugin->filename();
  174. /* label */ arguments << m_label;
  175. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  176. break;
  177. case PLUGIN_THREAD_BRIDGE:
  178. {
  179. const char* name = plugin->name();
  180. if (! name)
  181. name = "(none)";
  182. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPath()).arg(plugin->id());
  183. /* stype */ arguments << m_data1;
  184. /* filename */ arguments << plugin->filename();
  185. /* name */ arguments << name;
  186. /* label */ arguments << m_label;
  187. break;
  188. }
  189. }
  190. qDebug() << m_binary;
  191. qDebug() << arguments;
  192. m_process->start(m_binary, arguments);
  193. m_process->waitForStarted();
  194. switch (mode)
  195. {
  196. case PLUGIN_THREAD_DSSI_GUI:
  197. case PLUGIN_THREAD_LV2_GUI:
  198. case PLUGIN_THREAD_VST_GUI:
  199. if (plugin->showOscGui())
  200. {
  201. m_process->waitForFinished(-1);
  202. if (m_process->exitCode() == 0)
  203. {
  204. // Hide
  205. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  206. qWarning("CarlaPluginThread::run() - GUI closed");
  207. }
  208. else
  209. {
  210. // Kill
  211. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), -1, 0, 0.0);
  212. qWarning("CarlaPluginThread::run() - GUI crashed");
  213. break;
  214. }
  215. }
  216. else
  217. {
  218. qDebug("CarlaPluginThread::run() - GUI timeout");
  219. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  220. }
  221. break;
  222. case PLUGIN_THREAD_BRIDGE:
  223. m_process->waitForFinished(-1);
  224. #ifdef DEBUG
  225. if (m_process->exitCode() == 0)
  226. qDebug("CarlaPluginThread::run() - bridge closed");
  227. else
  228. qDebug("CarlaPluginThread::run() - bridge crashed");
  229. qDebug("%s", QString(m_process->readAllStandardOutput()).toUtf8().constData());
  230. #endif
  231. break;
  232. }
  233. }