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.

244 lines
7.3KB

  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. qDebug("CarlaCheckThread::CarlaCheckThread(%p)", parent);
  28. }
  29. CarlaCheckThread::~CarlaCheckThread()
  30. {
  31. qDebug("CarlaCheckThread::~CarlaCheckThread()");
  32. }
  33. void CarlaCheckThread::stopNow()
  34. {
  35. m_stopNow = true;
  36. if (! wait(200))
  37. quit();
  38. if (isRunning() && ! wait(200))
  39. terminate();
  40. }
  41. void CarlaCheckThread::run()
  42. {
  43. qDebug("CarlaCheckThread::run()");
  44. m_stopNow = false;
  45. while (engine->isRunning() && ! m_stopNow)
  46. {
  47. for (unsigned short i=0; i < CarlaBackend::MAX_PLUGINS; i++)
  48. {
  49. CarlaBackend::CarlaPlugin* const plugin = engine->getPlugin(i);
  50. if (plugin && plugin->enabled())
  51. {
  52. // -------------------------------------------------------
  53. // Process postponed events
  54. plugin->postEventsRun();
  55. // -------------------------------------------------------
  56. // Update parameters (OSC)
  57. plugin->updateOscParameterOutputs();
  58. // -------------------------------------------------------
  59. // Send peak values (OSC)
  60. if (engine->isOscControllerRegisted())
  61. {
  62. const unsigned short id = plugin->id();
  63. if (plugin->audioInCount() > 0)
  64. {
  65. engine->osc_send_set_input_peak_value(id, 1, engine->getInputPeak(id, 0));
  66. engine->osc_send_set_input_peak_value(id, 2, engine->getInputPeak(id, 1));
  67. }
  68. if (plugin->audioOutCount() > 0)
  69. {
  70. engine->osc_send_set_output_peak_value(id, 1, engine->getOutputPeak(id, 0));
  71. engine->osc_send_set_output_peak_value(id, 2, engine->getOutputPeak(id, 1));
  72. }
  73. }
  74. }
  75. }
  76. msleep(50);
  77. }
  78. }
  79. // -----------------------------------------------------------------------
  80. // CarlaPluginThread
  81. const char* PluginThreadMode2str(const CarlaPluginThread::PluginThreadMode mode)
  82. {
  83. switch (mode)
  84. {
  85. case CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI:
  86. return "PLUGIN_THREAD_DSSI_GUI";
  87. case CarlaPluginThread::PLUGIN_THREAD_LV2_GUI:
  88. return "PLUGIN_THREAD_LV2_GUI";
  89. case CarlaPluginThread::PLUGIN_THREAD_VST_GUI:
  90. return "PLUGIN_THREAD_VST_GUI";
  91. case CarlaPluginThread::PLUGIN_THREAD_BRIDGE:
  92. return "PLUGIN_THREAD_BRIDGE";
  93. }
  94. qWarning("CarlaPluginThread::PluginThreadMode2str(%i) - invalid mode", mode);
  95. return nullptr;
  96. }
  97. CarlaPluginThread::CarlaPluginThread(CarlaBackend::CarlaEngine* const engine_, CarlaBackend::CarlaPlugin* const plugin_, const PluginThreadMode mode_, QObject* const parent) :
  98. QThread(parent),
  99. engine(engine_),
  100. plugin(plugin_),
  101. mode(mode_)
  102. {
  103. qDebug("CarlaPluginThread::CarlaPluginThread(plugin:\"%s\", engine:\"%s\", %s)", plugin->name(), engine->getName(), PluginThreadMode2str(mode));
  104. m_process = nullptr;
  105. }
  106. CarlaPluginThread::~CarlaPluginThread()
  107. {
  108. if (m_process)
  109. delete m_process;
  110. }
  111. void CarlaPluginThread::setOscData(const char* const binary, const char* const label, const char* const data1)
  112. {
  113. m_binary = QString(binary);
  114. m_label = QString(label);
  115. m_data1 = QString(data1);
  116. }
  117. void CarlaPluginThread::run()
  118. {
  119. qDebug("CarlaPluginThread::run()");
  120. if (m_process == nullptr)
  121. m_process = new QProcess(nullptr);
  122. m_process->setProcessChannelMode(QProcess::ForwardedChannels);
  123. QStringList arguments;
  124. switch (mode)
  125. {
  126. case PLUGIN_THREAD_DSSI_GUI:
  127. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPath()).arg(plugin->id());
  128. /* filename */ arguments << plugin->filename();
  129. /* label */ arguments << m_label;
  130. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  131. break;
  132. case PLUGIN_THREAD_LV2_GUI:
  133. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPath()).arg(plugin->id());
  134. /* URI */ arguments << m_label;
  135. /* ui-URI */ arguments << m_data1;
  136. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  137. break;
  138. case PLUGIN_THREAD_VST_GUI:
  139. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPath()).arg(plugin->id());
  140. /* filename */ arguments << plugin->filename();
  141. /* label */ arguments << m_label;
  142. /* ui-title */ arguments << QString("%1 (GUI)").arg(plugin->name());
  143. break;
  144. case PLUGIN_THREAD_BRIDGE:
  145. {
  146. const char* name = plugin->name();
  147. if (! name)
  148. name = "(none)";
  149. /* osc_url */ arguments << QString("%1/%2").arg(engine->getOscServerPath()).arg(plugin->id());
  150. /* stype */ arguments << m_data1;
  151. /* filename */ arguments << plugin->filename();
  152. /* name */ arguments << name;
  153. /* label */ arguments << m_label;
  154. break;
  155. }
  156. }
  157. qDebug() << m_binary;
  158. qDebug() << arguments;
  159. m_process->start(m_binary, arguments);
  160. m_process->waitForStarted();
  161. switch (mode)
  162. {
  163. case PLUGIN_THREAD_DSSI_GUI:
  164. case PLUGIN_THREAD_LV2_GUI:
  165. case PLUGIN_THREAD_VST_GUI:
  166. if (plugin->showOscGui())
  167. {
  168. m_process->waitForFinished(-1);
  169. if (m_process->exitCode() == 0)
  170. {
  171. // Hide
  172. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  173. qWarning("CarlaPluginThread::run() - GUI closed");
  174. }
  175. else
  176. {
  177. // Kill
  178. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), -1, 0, 0.0);
  179. qWarning("CarlaPluginThread::run() - GUI crashed");
  180. break;
  181. }
  182. }
  183. else
  184. {
  185. qDebug("CarlaPluginThread::run() - GUI timeout");
  186. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  187. }
  188. break;
  189. case PLUGIN_THREAD_BRIDGE:
  190. m_process->waitForFinished(-1);
  191. #ifdef DEBUG
  192. if (m_process->exitCode() == 0)
  193. qDebug("CarlaPluginThread::run() - bridge closed");
  194. else
  195. qDebug("CarlaPluginThread::run() - bridge crashed");
  196. qDebug("%s", QString(m_process->readAllStandardOutput()).toUtf8().constData());
  197. #endif
  198. break;
  199. }
  200. }