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.

CarlaBridgePlugin.cpp 14KB

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