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.

287 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. #ifndef BUILD_BRIDGE
  115. const char* PluginThreadMode2str(const CarlaPluginThread::PluginThreadMode mode)
  116. {
  117. switch (mode)
  118. {
  119. case CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI:
  120. return "PLUGIN_THREAD_DSSI_GUI";
  121. case CarlaPluginThread::PLUGIN_THREAD_LV2_GUI:
  122. return "PLUGIN_THREAD_LV2_GUI";
  123. case CarlaPluginThread::PLUGIN_THREAD_VST_GUI:
  124. return "PLUGIN_THREAD_VST_GUI";
  125. case CarlaPluginThread::PLUGIN_THREAD_BRIDGE:
  126. return "PLUGIN_THREAD_BRIDGE";
  127. }
  128. qWarning("CarlaPluginThread::PluginThreadMode2str(%i) - invalid mode", mode);
  129. return nullptr;
  130. }
  131. CarlaPluginThread::CarlaPluginThread(CarlaBackend::CarlaEngine* const engine_, CarlaBackend::CarlaPlugin* const plugin_, const PluginThreadMode mode_, QObject* const parent) :
  132. QThread(parent),
  133. engine(engine_),
  134. plugin(plugin_),
  135. mode(mode_)
  136. {
  137. qDebug("CarlaPluginThread::CarlaPluginThread(plugin:\"%s\", engine:\"%s\", %s)", plugin->name(), engine->getName(), PluginThreadMode2str(mode));
  138. m_process = nullptr;
  139. }
  140. CarlaPluginThread::~CarlaPluginThread()
  141. {
  142. if (m_process)
  143. delete m_process;
  144. }
  145. void CarlaPluginThread::setOscData(const char* const binary, const char* const label, const char* const data1)
  146. {
  147. m_binary = QString(binary);
  148. m_label = QString(label);
  149. m_data1 = QString(data1);
  150. }
  151. void CarlaPluginThread::run()
  152. {
  153. qDebug("CarlaPluginThread::run()");
  154. if (! m_process)
  155. m_process = new QProcess(nullptr);
  156. m_process->setProcessChannelMode(QProcess::ForwardedChannels);
  157. QStringList arguments;
  158. switch (mode)
  159. {
  160. case PLUGIN_THREAD_DSSI_GUI:
  161. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPath()).arg(plugin->id());
  162. /* filename */ arguments << plugin->filename();
  163. /* label */ arguments << m_label;
  164. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  165. break;
  166. case PLUGIN_THREAD_LV2_GUI:
  167. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPath()).arg(plugin->id());
  168. /* URI */ arguments << m_label;
  169. /* ui-URI */ arguments << m_data1;
  170. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  171. break;
  172. case PLUGIN_THREAD_VST_GUI:
  173. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPath()).arg(plugin->id());
  174. /* filename */ arguments << plugin->filename();
  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. }
  234. #endif