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.

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