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.

636 lines
18KB

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