Audio plugin host https://kx.studio/carla
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.

573 lines
14KB

  1. /*
  2. * Carla Bridge Plugin
  3. * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or 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 GPL.txt file
  16. */
  17. #ifdef BRIDGE_PLUGIN
  18. #include "CarlaBridgeClient.hpp"
  19. #include "CarlaBridgeToolkit.hpp"
  20. #include "CarlaStandalone.hpp"
  21. #include "CarlaEngine.hpp"
  22. #include "CarlaPlugin.hpp"
  23. #include <QtCore/QDir>
  24. #include <QtCore/QFile>
  25. #include <QtCore/QTextStream>
  26. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  27. # include <QtWidgets/QApplication>
  28. #else
  29. # include <QtGui/QApplication>
  30. #endif
  31. #ifdef CARLA_OS_UNIX
  32. # include <signal.h>
  33. #endif
  34. // -------------------------------------------------------------------------
  35. static bool gCloseNow = false;
  36. static bool gSaveNow = false;
  37. #ifdef CARLA_OS_WIN
  38. BOOL WINAPI closeSignalHandler(DWORD dwCtrlType)
  39. {
  40. if (dwCtrlType == CTRL_C_EVENT)
  41. {
  42. gCloseNow = true;
  43. return TRUE;
  44. }
  45. return FALSE;
  46. }
  47. #else
  48. void closeSignalHandler(int)
  49. {
  50. gCloseNow = true;
  51. }
  52. void saveSignalHandler(int)
  53. {
  54. gSaveNow = true;
  55. }
  56. #endif
  57. void initSignalHandler()
  58. {
  59. #ifdef CARLA_OS_WIN
  60. SetConsoleCtrlHandler(closeSignalHandler, TRUE);
  61. #elif defined(CARLA_OS_LINUX) || defined(CARLA_OS_HAIKU)
  62. struct sigaction sint;
  63. struct sigaction sterm;
  64. struct sigaction susr1;
  65. sint.sa_handler = closeSignalHandler;
  66. sint.sa_flags = SA_RESTART;
  67. sint.sa_restorer = nullptr;
  68. sigemptyset(&sint.sa_mask);
  69. sigaction(SIGINT, &sint, nullptr);
  70. sterm.sa_handler = closeSignalHandler;
  71. sterm.sa_flags = SA_RESTART;
  72. sterm.sa_restorer = nullptr;
  73. sigemptyset(&sterm.sa_mask);
  74. sigaction(SIGTERM, &sterm, nullptr);
  75. susr1.sa_handler = saveSignalHandler;
  76. susr1.sa_flags = SA_RESTART;
  77. susr1.sa_restorer = nullptr;
  78. sigemptyset(&susr1.sa_mask);
  79. sigaction(SIGUSR1, &susr1, nullptr);
  80. #endif
  81. }
  82. // -------------------------------------------------------------------------
  83. // Helpers
  84. extern CarlaBackend::CarlaEngine* carla_get_standalone_engine();
  85. CARLA_BACKEND_START_NAMESPACE
  86. extern const char* findDSSIGUI(const char* const filename, const char* const label);
  87. CARLA_BACKEND_END_NAMESPACE
  88. CARLA_BRIDGE_START_NAMESPACE
  89. #if 0
  90. } // Fix editor indentation
  91. #endif
  92. // -------------------------------------------------------------------------
  93. class CarlaPluginClient : public CarlaBridgeClient,
  94. public QObject
  95. {
  96. public:
  97. CarlaPluginClient(const char* const name)
  98. : CarlaBridgeClient(nullptr),
  99. QObject(nullptr),
  100. fEngine(nullptr),
  101. fPlugin(nullptr),
  102. fTimerId(0)
  103. {
  104. CARLA_ASSERT(name != nullptr);
  105. carla_debug("CarlaPluginClient::CarlaPluginClient()");
  106. carla_engine_init("JACK", name);
  107. carla_set_engine_callback(callback, this);
  108. }
  109. ~CarlaPluginClient()
  110. {
  111. CARLA_ASSERT(fTimerId == 0);
  112. carla_debug("CarlaPluginClient::~CarlaPluginClient()");
  113. carla_set_engine_about_to_close();
  114. carla_engine_close();
  115. }
  116. void ready()
  117. {
  118. CARLA_ASSERT(fTimerId == 0);
  119. fEngine = carla_get_standalone_engine();
  120. fPlugin = fEngine->getPlugin(0);
  121. fTimerId = startTimer(50);
  122. }
  123. void idle()
  124. {
  125. if (fEngine != nullptr)
  126. fEngine->idle();
  127. CarlaBridgeClient::oscIdle();
  128. if (gSaveNow)
  129. {
  130. // TODO
  131. gSaveNow = false;
  132. }
  133. if (gCloseNow)
  134. {
  135. if (fTimerId != 0)
  136. {
  137. killTimer(fTimerId);
  138. fTimerId = 0;
  139. }
  140. if (QApplication* const app = qApp)
  141. {
  142. if (! app->closingDown())
  143. app->quit();
  144. }
  145. }
  146. }
  147. void exec()
  148. {
  149. while (! gCloseNow)
  150. {
  151. idle();
  152. carla_msleep(50);
  153. }
  154. }
  155. // ---------------------------------------------------------------------
  156. // plugin management
  157. void saveNow()
  158. {
  159. qDebug("CarlaPluginClient::saveNow()");
  160. CARLA_ASSERT(fEngine != nullptr);
  161. CARLA_ASSERT(fPlugin != nullptr);
  162. if (fPlugin == nullptr || fEngine == nullptr)
  163. return;
  164. fPlugin->prepareForSave();
  165. for (uint32_t i=0; i < fPlugin->customDataCount(); i++)
  166. {
  167. const CarlaBackend::CustomData& cdata = fPlugin->customData(i);
  168. fEngine->osc_send_bridge_set_custom_data(cdata.type, cdata.key, cdata.value);
  169. }
  170. if (fPlugin->options() & CarlaBackend::PLUGIN_OPTION_USE_CHUNKS)
  171. {
  172. void* data = nullptr;
  173. int32_t dataSize = fPlugin->chunkData(&data);
  174. if (data && dataSize >= 4)
  175. {
  176. QString filePath;
  177. filePath = QDir::tempPath();
  178. #ifdef Q_OS_WIN
  179. filePath += "\\.CarlaChunk_";
  180. #else
  181. filePath += "/.CarlaChunk_";
  182. #endif
  183. filePath += fPlugin->name();
  184. QFile file(filePath);
  185. if (file.open(QIODevice::WriteOnly))
  186. {
  187. QByteArray chunk((const char*)data, dataSize);
  188. file.write(chunk);
  189. file.close();
  190. fEngine->osc_send_bridge_set_chunk_data(filePath.toUtf8().constData());
  191. }
  192. }
  193. }
  194. //engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_SAVED, "");
  195. }
  196. void setCustomData(const char* const type, const char* const key, const char* const value)
  197. {
  198. carla_debug("CarlaPluginClient::setCustomData(\"%s\", \"%s\", \"%s\")", type, key, value);
  199. CARLA_ASSERT(fPlugin != nullptr);
  200. if (fPlugin != nullptr)
  201. fPlugin->setCustomData(type, key, value, true);
  202. }
  203. void setChunkData(const char* const filePath)
  204. {
  205. carla_debug("CarlaPluginClient::setChunkData(\"%s\")", filePath);
  206. CARLA_ASSERT(fPlugin != nullptr);
  207. if (fPlugin == nullptr)
  208. return;
  209. QString chunkFilePath(filePath);
  210. #ifdef CARLA_OS_WIN
  211. if (chunkFilePath.startsWith("/"))
  212. {
  213. // running under Wine, posix host
  214. chunkFilePath = chunkFilePath.replace(0, 1, "Z:/");
  215. chunkFilePath = QDir::toNativeSeparators(chunkFilePath);
  216. }
  217. #endif
  218. QFile chunkFile(chunkFilePath);
  219. if (fPlugin != nullptr && chunkFile.open(QIODevice::ReadOnly | QIODevice::Text))
  220. {
  221. QTextStream in(&chunkFile);
  222. QString stringData(in.readAll());
  223. chunkFile.close();
  224. chunkFile.remove();
  225. fPlugin->setChunkData(stringData.toUtf8().constData());
  226. }
  227. }
  228. // ---------------------------------------------------------------------
  229. // processing
  230. void setParameter(const int32_t rindex, const float value)
  231. {
  232. carla_debug("CarlaPluginClient::setParameter(%i, %f)", rindex, value);
  233. CARLA_ASSERT(fPlugin != nullptr);
  234. if (fPlugin != nullptr)
  235. fPlugin->setParameterValueByRIndex(rindex, value, true, true, false);
  236. }
  237. void setProgram(const uint32_t index)
  238. {
  239. carla_debug("CarlaPluginClient::setProgram(%i)", index);
  240. CARLA_ASSERT(fPlugin != nullptr);
  241. if (fPlugin != nullptr)
  242. fPlugin->setProgram(index, true, true, false);
  243. }
  244. void setMidiProgram(const uint32_t index)
  245. {
  246. carla_debug("CarlaPluginClient::setMidiProgram(%i)", index);
  247. CARLA_ASSERT(fPlugin != nullptr);
  248. if (fPlugin != nullptr)
  249. fPlugin->setMidiProgram(index, true, true, false);
  250. }
  251. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  252. {
  253. carla_debug("CarlaPluginClient::noteOn(%i, %i, %i)", channel, note, velo);
  254. CARLA_ASSERT(fPlugin != nullptr);
  255. CARLA_ASSERT(velo > 0);
  256. if (fPlugin != nullptr)
  257. fPlugin->sendMidiSingleNote(channel, note, velo, true, true, false);
  258. }
  259. void noteOff(const uint8_t channel, const uint8_t note)
  260. {
  261. carla_debug("CarlaPluginClient::noteOff(%i, %i)", channel, note);
  262. CARLA_ASSERT(fPlugin != nullptr);
  263. if (fPlugin != nullptr)
  264. fPlugin->sendMidiSingleNote(channel, note, 0, true, true, false);
  265. }
  266. protected:
  267. void handleCallback(const CarlaBackend::CallbackType action, const int value1, const int value2, const float value3, const char* const valueStr)
  268. {
  269. CARLA_BACKEND_USE_NAMESPACE;
  270. // TODO
  271. switch (action)
  272. {
  273. case CALLBACK_SHOW_GUI:
  274. if (value1 != 1 && ! isOscControlRegistered())
  275. gCloseNow = true;
  276. break;
  277. default:
  278. break;
  279. }
  280. return;
  281. (void)value2;
  282. (void)value3;
  283. (void)valueStr;
  284. }
  285. private:
  286. CarlaBackend::CarlaEngine* fEngine;
  287. CarlaBackend::CarlaPlugin* fPlugin;
  288. int fTimerId;
  289. void timerEvent(QTimerEvent* const event)
  290. {
  291. if (event->timerId() == fTimerId)
  292. idle();
  293. QObject::timerEvent(event);
  294. }
  295. static void callback(void* ptr, CarlaBackend::CallbackType action, unsigned int pluginId, int value1, int value2, float value3, const char* valueStr)
  296. {
  297. return ((CarlaPluginClient*)ptr)->handleCallback(action, value1, value2, value3, valueStr);
  298. // unused
  299. (void)pluginId;
  300. }
  301. };
  302. // -------------------------------------------------------------------------
  303. int CarlaBridgeOsc::handleMsgShow()
  304. {
  305. carla_debug("CarlaBridgeOsc::handleMsgShow()");
  306. CARLA_ASSERT(kClient != nullptr);
  307. if (kClient == nullptr)
  308. return 1;
  309. carla_show_gui(0, true);
  310. return 0;
  311. }
  312. int CarlaBridgeOsc::handleMsgHide()
  313. {
  314. carla_debug("CarlaBridgeOsc::handleMsgHide()");
  315. CARLA_ASSERT(kClient != nullptr);
  316. if (kClient == nullptr)
  317. return 1;
  318. carla_show_gui(0, false);
  319. return 0;
  320. }
  321. int CarlaBridgeOsc::handleMsgQuit()
  322. {
  323. carla_debug("CarlaBridgeOsc::handleMsgQuit()");
  324. CARLA_ASSERT(kClient != nullptr);
  325. if (kClient == nullptr)
  326. return 1;
  327. gCloseNow = true;
  328. return 0;
  329. }
  330. // -------------------------------------------------------------------------
  331. int CarlaBridgeOsc::handleMsgPluginSaveNow()
  332. {
  333. carla_debug("CarlaBridgeOsc::handleMsgPluginSaveNow()");
  334. CARLA_ASSERT(kClient != nullptr);
  335. if (kClient == nullptr)
  336. return 1;
  337. CarlaPluginClient* const plugClient = (CarlaPluginClient*)kClient;
  338. plugClient->saveNow();
  339. return 0;
  340. }
  341. int CarlaBridgeOsc::handleMsgPluginSetChunk(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  342. {
  343. carla_debug("CarlaBridgeOsc::handleMsgPluginSaveNow()");
  344. CARLA_ASSERT(kClient != nullptr);
  345. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "s");
  346. if (kClient == nullptr)
  347. return 1;
  348. const char* const chunkFile = (const char*)&argv[0]->s;
  349. CarlaPluginClient* const plugClient = (CarlaPluginClient*)kClient;
  350. plugClient->setChunkData(chunkFile);
  351. return 0;
  352. }
  353. int CarlaBridgeOsc::handleMsgPluginSetCustomData(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  354. {
  355. carla_debug("CarlaBridgeOsc::handleMsgPluginSaveNow()");
  356. CARLA_ASSERT(kClient != nullptr);
  357. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(3, "sss");
  358. if (kClient == nullptr)
  359. return 1;
  360. const char* const type = (const char*)&argv[0]->s;
  361. const char* const key = (const char*)&argv[1]->s;
  362. const char* const value = (const char*)&argv[2]->s;
  363. CarlaPluginClient* const plugClient = (CarlaPluginClient*)kClient;
  364. plugClient->setCustomData(type, key, value);
  365. return 0;
  366. }
  367. // -------------------------------------------------------------------------
  368. CARLA_BRIDGE_END_NAMESPACE
  369. int main(int argc, char* argv[])
  370. {
  371. CARLA_BRIDGE_USE_NAMESPACE
  372. if (argc != 6)
  373. {
  374. carla_stdout("usage: %s <osc-url|\"null\"> <type> <filename> <name|\"(none)\"> <label>", argv[0]);
  375. return 1;
  376. }
  377. const char* const oscUrl = argv[1];
  378. const char* const stype = argv[2];
  379. const char* const filename = argv[3];
  380. const char* name = argv[4];
  381. const char* const label = argv[5];
  382. const bool useOsc = std::strcmp(oscUrl, "null");
  383. if (std::strcmp(name, "(none)") == 0)
  384. name = nullptr;
  385. CarlaBackend::PluginType itype;
  386. if (std::strcmp(stype, "LADSPA") == 0)
  387. itype = CarlaBackend::PLUGIN_LADSPA;
  388. else if (std::strcmp(stype, "DSSI") == 0)
  389. itype = CarlaBackend::PLUGIN_DSSI;
  390. else if (std::strcmp(stype, "LV2") == 0)
  391. itype = CarlaBackend::PLUGIN_LV2;
  392. else if (std::strcmp(stype, "VST") == 0)
  393. itype = CarlaBackend::PLUGIN_VST;
  394. else if (std::strcmp(stype, "VST3") == 0)
  395. itype = CarlaBackend::PLUGIN_VST3;
  396. else
  397. {
  398. carla_stderr("Invalid plugin type '%s'", stype);
  399. return 1;
  400. }
  401. QApplication app(argc, argv, true);
  402. app.setQuitOnLastWindowClosed(false);
  403. // Init Plugin client
  404. CarlaPluginClient client(name ? name : label);
  405. // Init OSC
  406. if (useOsc)
  407. client.oscInit(oscUrl);
  408. // Listen for ctrl+c or sigint/sigterm events
  409. initSignalHandler();
  410. const void* extraStuff = nullptr;
  411. if (itype == CarlaBackend::PLUGIN_DSSI)
  412. extraStuff = CarlaBackend::findDSSIGUI(filename, label);
  413. // Init plugin
  414. int ret;
  415. if (carla_add_plugin(CarlaBackend::BINARY_NATIVE, itype, filename, name, label, extraStuff))
  416. {
  417. if (useOsc)
  418. {
  419. client.sendOscUpdate();
  420. client.sendOscBridgeUpdate();
  421. }
  422. else
  423. {
  424. carla_set_active(0, true);
  425. carla_show_gui(0, true);
  426. }
  427. client.ready();
  428. ret = app.exec();
  429. carla_remove_plugin(0);
  430. }
  431. else
  432. {
  433. const char* const lastError = carla_get_last_error();
  434. carla_stderr("Plugin failed to load, error was:\n%s", lastError);
  435. if (useOsc)
  436. client.sendOscBridgeError(lastError);
  437. ret = 1;
  438. }
  439. if (extraStuff != nullptr && itype == CarlaBackend::PLUGIN_DSSI)
  440. delete[] (const char*)extraStuff;
  441. // Close OSC
  442. if (useOsc)
  443. client.oscClose();
  444. return ret;
  445. }
  446. #endif // BRIDGE_PLUGIN