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.

596 lines
15KB

  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 bool useBridge, const char* const driverName, const char* audioBaseName, const char* controlBaseName)
  98. : CarlaBridgeClient(nullptr),
  99. QObject(nullptr),
  100. fEngine(nullptr),
  101. fPlugin(nullptr),
  102. fTimerId(0)
  103. {
  104. CARLA_ASSERT(driverName != nullptr);
  105. carla_debug("CarlaPluginClient::CarlaPluginClient(%s, \"%s\", %s, %s)", bool2str(useBridge), driverName, audioBaseName, controlBaseName);
  106. if (useBridge)
  107. carla_engine_init_bridge(audioBaseName, controlBaseName, driverName);
  108. else
  109. carla_engine_init("JACK", driverName);
  110. carla_set_engine_callback(callback, this);
  111. }
  112. ~CarlaPluginClient()
  113. {
  114. CARLA_ASSERT(fTimerId == 0);
  115. carla_debug("CarlaPluginClient::~CarlaPluginClient()");
  116. carla_set_engine_about_to_close();
  117. carla_engine_close();
  118. }
  119. void oscInit(const char* const url)
  120. {
  121. CarlaBridgeClient::oscInit(url);
  122. fEngine = carla_get_standalone_engine();
  123. fEngine->setOscBridgeData(fOscData);
  124. }
  125. void ready()
  126. {
  127. CARLA_ASSERT(fTimerId == 0);
  128. fEngine = carla_get_standalone_engine();
  129. fPlugin = fEngine->getPlugin(0);
  130. fTimerId = startTimer(50);
  131. }
  132. void idle()
  133. {
  134. if (fEngine != nullptr)
  135. fEngine->idle();
  136. CarlaBridgeClient::oscIdle();
  137. if (gSaveNow)
  138. {
  139. // TODO
  140. gSaveNow = false;
  141. }
  142. if (gCloseNow)
  143. {
  144. if (fTimerId != 0)
  145. {
  146. killTimer(fTimerId);
  147. fTimerId = 0;
  148. }
  149. if (QApplication* const app = qApp)
  150. {
  151. if (! app->closingDown())
  152. app->quit();
  153. }
  154. }
  155. }
  156. void exec()
  157. {
  158. while (! gCloseNow)
  159. {
  160. idle();
  161. carla_msleep(50);
  162. }
  163. }
  164. // ---------------------------------------------------------------------
  165. // plugin management
  166. void saveNow()
  167. {
  168. qDebug("CarlaPluginClient::saveNow()");
  169. CARLA_ASSERT(fEngine != nullptr);
  170. CARLA_ASSERT(fPlugin != nullptr);
  171. if (fPlugin == nullptr || fEngine == nullptr)
  172. return;
  173. fPlugin->prepareForSave();
  174. for (uint32_t i=0; i < fPlugin->customDataCount(); i++)
  175. {
  176. const CarlaBackend::CustomData& cdata = fPlugin->customData(i);
  177. fEngine->osc_send_bridge_set_custom_data(cdata.type, cdata.key, cdata.value);
  178. }
  179. if (fPlugin->options() & CarlaBackend::PLUGIN_OPTION_USE_CHUNKS)
  180. {
  181. void* data = nullptr;
  182. int32_t dataSize = fPlugin->chunkData(&data);
  183. if (data && dataSize >= 4)
  184. {
  185. QString filePath;
  186. filePath = QDir::tempPath();
  187. #ifdef Q_OS_WIN
  188. filePath += "\\.CarlaChunk_";
  189. #else
  190. filePath += "/.CarlaChunk_";
  191. #endif
  192. filePath += fPlugin->name();
  193. QFile file(filePath);
  194. if (file.open(QIODevice::WriteOnly))
  195. {
  196. QByteArray chunk((const char*)data, dataSize);
  197. file.write(chunk);
  198. file.close();
  199. fEngine->osc_send_bridge_set_chunk_data(filePath.toUtf8().constData());
  200. }
  201. }
  202. }
  203. //engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_SAVED, "");
  204. }
  205. void setCustomData(const char* const type, const char* const key, const char* const value)
  206. {
  207. carla_debug("CarlaPluginClient::setCustomData(\"%s\", \"%s\", \"%s\")", type, key, value);
  208. CARLA_ASSERT(fPlugin != nullptr);
  209. if (fPlugin != nullptr)
  210. fPlugin->setCustomData(type, key, value, true);
  211. }
  212. void setChunkData(const char* const filePath)
  213. {
  214. carla_debug("CarlaPluginClient::setChunkData(\"%s\")", filePath);
  215. CARLA_ASSERT(fPlugin != nullptr);
  216. if (fPlugin == nullptr)
  217. return;
  218. QString chunkFilePath(filePath);
  219. #ifdef CARLA_OS_WIN
  220. if (chunkFilePath.startsWith("/"))
  221. {
  222. // running under Wine, posix host
  223. chunkFilePath = chunkFilePath.replace(0, 1, "Z:/");
  224. chunkFilePath = QDir::toNativeSeparators(chunkFilePath);
  225. }
  226. #endif
  227. QFile chunkFile(chunkFilePath);
  228. if (fPlugin != nullptr && chunkFile.open(QIODevice::ReadOnly | QIODevice::Text))
  229. {
  230. QTextStream in(&chunkFile);
  231. QString stringData(in.readAll());
  232. chunkFile.close();
  233. chunkFile.remove();
  234. fPlugin->setChunkData(stringData.toUtf8().constData());
  235. }
  236. }
  237. // ---------------------------------------------------------------------
  238. // processing
  239. void setParameter(const int32_t rindex, const float value)
  240. {
  241. carla_debug("CarlaPluginClient::setParameter(%i, %f)", rindex, value);
  242. CARLA_ASSERT(fPlugin != nullptr);
  243. if (fPlugin != nullptr)
  244. fPlugin->setParameterValueByRIndex(rindex, value, true, true, false);
  245. }
  246. void setProgram(const uint32_t index)
  247. {
  248. carla_debug("CarlaPluginClient::setProgram(%i)", index);
  249. CARLA_ASSERT(fPlugin != nullptr);
  250. if (fPlugin != nullptr)
  251. fPlugin->setProgram(index, true, true, false);
  252. }
  253. void setMidiProgram(const uint32_t index)
  254. {
  255. carla_debug("CarlaPluginClient::setMidiProgram(%i)", index);
  256. CARLA_ASSERT(fPlugin != nullptr);
  257. if (fPlugin != nullptr)
  258. fPlugin->setMidiProgram(index, true, true, false);
  259. }
  260. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  261. {
  262. carla_debug("CarlaPluginClient::noteOn(%i, %i, %i)", channel, note, velo);
  263. CARLA_ASSERT(fPlugin != nullptr);
  264. CARLA_ASSERT(velo > 0);
  265. if (fPlugin != nullptr)
  266. fPlugin->sendMidiSingleNote(channel, note, velo, true, true, false);
  267. }
  268. void noteOff(const uint8_t channel, const uint8_t note)
  269. {
  270. carla_debug("CarlaPluginClient::noteOff(%i, %i)", channel, note);
  271. CARLA_ASSERT(fPlugin != nullptr);
  272. if (fPlugin != nullptr)
  273. fPlugin->sendMidiSingleNote(channel, note, 0, true, true, false);
  274. }
  275. protected:
  276. void handleCallback(const CarlaBackend::CallbackType action, const int value1, const int value2, const float value3, const char* const valueStr)
  277. {
  278. CARLA_BACKEND_USE_NAMESPACE;
  279. // TODO
  280. switch (action)
  281. {
  282. case CALLBACK_SHOW_GUI:
  283. if (value1 != 1 && ! isOscControlRegistered())
  284. gCloseNow = true;
  285. break;
  286. default:
  287. break;
  288. }
  289. return;
  290. (void)value2;
  291. (void)value3;
  292. (void)valueStr;
  293. }
  294. private:
  295. CarlaBackend::CarlaEngine* fEngine;
  296. CarlaBackend::CarlaPlugin* fPlugin;
  297. int fTimerId;
  298. void timerEvent(QTimerEvent* const event)
  299. {
  300. if (event->timerId() == fTimerId)
  301. idle();
  302. QObject::timerEvent(event);
  303. }
  304. static void callback(void* ptr, CarlaBackend::CallbackType action, unsigned int pluginId, int value1, int value2, float value3, const char* valueStr)
  305. {
  306. return ((CarlaPluginClient*)ptr)->handleCallback(action, value1, value2, value3, valueStr);
  307. // unused
  308. (void)pluginId;
  309. }
  310. };
  311. // -------------------------------------------------------------------------
  312. int CarlaBridgeOsc::handleMsgShow()
  313. {
  314. carla_debug("CarlaBridgeOsc::handleMsgShow()");
  315. CARLA_ASSERT(kClient != nullptr);
  316. if (kClient == nullptr)
  317. return 1;
  318. carla_show_gui(0, true);
  319. return 0;
  320. }
  321. int CarlaBridgeOsc::handleMsgHide()
  322. {
  323. carla_debug("CarlaBridgeOsc::handleMsgHide()");
  324. CARLA_ASSERT(kClient != nullptr);
  325. if (kClient == nullptr)
  326. return 1;
  327. carla_show_gui(0, false);
  328. return 0;
  329. }
  330. int CarlaBridgeOsc::handleMsgQuit()
  331. {
  332. carla_debug("CarlaBridgeOsc::handleMsgQuit()");
  333. CARLA_ASSERT(kClient != nullptr);
  334. gCloseNow = true;
  335. return 0;
  336. }
  337. // -------------------------------------------------------------------------
  338. int CarlaBridgeOsc::handleMsgPluginSaveNow()
  339. {
  340. carla_debug("CarlaBridgeOsc::handleMsgPluginSaveNow()");
  341. CARLA_ASSERT(kClient != nullptr);
  342. if (kClient == nullptr)
  343. return 1;
  344. CarlaPluginClient* const plugClient = (CarlaPluginClient*)kClient;
  345. plugClient->saveNow();
  346. return 0;
  347. }
  348. int CarlaBridgeOsc::handleMsgPluginSetChunk(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  349. {
  350. carla_debug("CarlaBridgeOsc::handleMsgPluginSaveNow()");
  351. CARLA_ASSERT(kClient != nullptr);
  352. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "s");
  353. if (kClient == nullptr)
  354. return 1;
  355. const char* const chunkFile = (const char*)&argv[0]->s;
  356. CarlaPluginClient* const plugClient = (CarlaPluginClient*)kClient;
  357. plugClient->setChunkData(chunkFile);
  358. return 0;
  359. }
  360. int CarlaBridgeOsc::handleMsgPluginSetCustomData(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  361. {
  362. carla_debug("CarlaBridgeOsc::handleMsgPluginSaveNow()");
  363. CARLA_ASSERT(kClient != nullptr);
  364. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(3, "sss");
  365. if (kClient == nullptr)
  366. return 1;
  367. const char* const type = (const char*)&argv[0]->s;
  368. const char* const key = (const char*)&argv[1]->s;
  369. const char* const value = (const char*)&argv[2]->s;
  370. CarlaPluginClient* const plugClient = (CarlaPluginClient*)kClient;
  371. plugClient->setCustomData(type, key, value);
  372. return 0;
  373. }
  374. // -------------------------------------------------------------------------
  375. CARLA_BRIDGE_END_NAMESPACE
  376. int main(int argc, char* argv[])
  377. {
  378. CARLA_BRIDGE_USE_NAMESPACE
  379. if (argc != 6 && argc != 7)
  380. {
  381. carla_stdout("usage: %s <osc-url|\"null\"> <type> <filename> <name|\"(none)\"> <label>", argv[0]);
  382. return 1;
  383. }
  384. const char* const oscUrl = argv[1];
  385. const char* const stype = argv[2];
  386. const char* const filename = argv[3];
  387. const char* name = argv[4];
  388. const char* const label = argv[5];
  389. const bool useBridge = (argc == 7);
  390. const bool useOsc = std::strcmp(oscUrl, "null");
  391. if (std::strcmp(name, "(none)") == 0)
  392. name = nullptr;
  393. char bridgeBaseAudioName[6+1] = { 0 };
  394. char bridgeBaseControlName[6+1] = { 0 };
  395. if (useBridge)
  396. {
  397. std::strncpy(bridgeBaseAudioName, argv[6], 6);
  398. std::strncpy(bridgeBaseControlName, argv[6]+6, 6);
  399. }
  400. CarlaBackend::PluginType itype;
  401. if (std::strcmp(stype, "LADSPA") == 0)
  402. itype = CarlaBackend::PLUGIN_LADSPA;
  403. else if (std::strcmp(stype, "DSSI") == 0)
  404. itype = CarlaBackend::PLUGIN_DSSI;
  405. else if (std::strcmp(stype, "LV2") == 0)
  406. itype = CarlaBackend::PLUGIN_LV2;
  407. else if (std::strcmp(stype, "VST") == 0)
  408. itype = CarlaBackend::PLUGIN_VST;
  409. else if (std::strcmp(stype, "VST3") == 0)
  410. itype = CarlaBackend::PLUGIN_VST3;
  411. else
  412. {
  413. carla_stderr("Invalid plugin type '%s'", stype);
  414. return 1;
  415. }
  416. QApplication app(argc, argv, true);
  417. app.setQuitOnLastWindowClosed(false);
  418. // Init Plugin client
  419. CarlaPluginClient client(useBridge, (name != nullptr) ? name : label, bridgeBaseAudioName, bridgeBaseControlName);
  420. // Init OSC
  421. if (useOsc)
  422. client.oscInit(oscUrl);
  423. // Listen for ctrl+c or sigint/sigterm events
  424. initSignalHandler();
  425. const void* extraStuff = nullptr;
  426. if (itype == CarlaBackend::PLUGIN_DSSI)
  427. extraStuff = CarlaBackend::findDSSIGUI(filename, label);
  428. // Init plugin
  429. int ret;
  430. if (carla_add_plugin(CarlaBackend::BINARY_NATIVE, itype, filename, name, label, extraStuff))
  431. {
  432. if (useOsc)
  433. {
  434. client.sendOscUpdate();
  435. client.sendOscBridgeUpdate();
  436. // TESTING!!
  437. carla_set_active(0, true);
  438. carla_show_gui(0, true);
  439. }
  440. else
  441. {
  442. carla_set_active(0, true);
  443. carla_show_gui(0, true);
  444. }
  445. client.ready();
  446. ret = app.exec();
  447. carla_remove_plugin(0);
  448. }
  449. else
  450. {
  451. const char* const lastError = carla_get_last_error();
  452. carla_stderr("Plugin failed to load, error was:\n%s", lastError);
  453. if (useOsc)
  454. client.sendOscBridgeError(lastError);
  455. ret = 1;
  456. }
  457. if (extraStuff != nullptr && itype == CarlaBackend::PLUGIN_DSSI)
  458. delete[] (const char*)extraStuff;
  459. // Close OSC
  460. if (useOsc)
  461. client.oscClose();
  462. return ret;
  463. }
  464. #endif // BRIDGE_PLUGIN