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 15KB

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