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.

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