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.

629 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(fPlugin != nullptr,);
  226. carla_debug("CarlaPluginClient::setChunkData(\"%s\")", filePath);
  227. #if 0
  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. #endif
  247. }
  248. // ---------------------------------------------------------------------
  249. protected:
  250. void handleCallback(const CarlaBackend::EngineCallbackOpcode action, const int value1, const int value2, const float value3, const char* const valueStr)
  251. {
  252. CARLA_BACKEND_USE_NAMESPACE;
  253. // TODO
  254. switch (action)
  255. {
  256. case ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED:
  257. if (isOscControlRegistered())
  258. sendOscControl(value1, value3);
  259. break;
  260. case ENGINE_CALLBACK_UI_STATE_CHANGED:
  261. if (! isOscControlRegistered())
  262. {
  263. if (value1 != 1)
  264. gCloseNow = true;
  265. }
  266. else
  267. {
  268. // show-gui button
  269. fEngine->oscSend_bridge_configure(CARLA_BRIDGE_MSG_HIDE_GUI, "");
  270. }
  271. break;
  272. default:
  273. break;
  274. }
  275. return;
  276. (void)value2;
  277. (void)value3;
  278. (void)valueStr;
  279. }
  280. private:
  281. CarlaBackend::CarlaPlugin* fPlugin;
  282. const CarlaBackend::CarlaEngine* fEngine;
  283. CarlaString fProjFileName;
  284. static void callback(void* ptr, CarlaBackend::EngineCallbackOpcode action, unsigned int pluginId, int value1, int value2, float value3, const char* valueStr)
  285. {
  286. carla_debug("CarlaPluginClient::callback(%p, %i:%s, %i, %i, %i, %f, \"%s\")", ptr, action, CarlaBackend::EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  287. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  288. CARLA_SAFE_ASSERT_RETURN(pluginId == 0,);
  289. return ((CarlaPluginClient*)ptr)->handleCallback(action, value1, value2, value3, valueStr);
  290. }
  291. };
  292. // -------------------------------------------------------------------------
  293. int CarlaBridgeOsc::handleMsgShow()
  294. {
  295. carla_debug("CarlaBridgeOsc::handleMsgShow()");
  296. carla_show_custom_ui(0, true);
  297. return 0;
  298. }
  299. int CarlaBridgeOsc::handleMsgHide()
  300. {
  301. carla_debug("CarlaBridgeOsc::handleMsgHide()");
  302. carla_show_custom_ui(0, false);
  303. return 0;
  304. }
  305. int CarlaBridgeOsc::handleMsgQuit()
  306. {
  307. carla_debug("CarlaBridgeOsc::handleMsgQuit()");
  308. gCloseNow = true;
  309. return 0;
  310. }
  311. // -------------------------------------------------------------------------
  312. int CarlaBridgeOsc::handleMsgPluginSaveNow()
  313. {
  314. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  315. carla_debug("CarlaBridgeOsc::handleMsgPluginSaveNow()");
  316. CarlaPluginClient* const plugClient((CarlaPluginClient*)fClient);
  317. plugClient->saveNow();
  318. return 0;
  319. }
  320. int CarlaBridgeOsc::handleMsgPluginSetParameterMidiChannel(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  321. {
  322. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  323. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  324. carla_debug("CarlaBridgeOsc::handleMsgPluginSetParameterMidiChannel()");
  325. const int32_t index = argv[0]->i;
  326. const int32_t channel = argv[1]->i;
  327. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  328. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  329. CarlaPluginClient* const plugClient((CarlaPluginClient*)fClient);
  330. plugClient->setParameterMidiChannel(static_cast<uint32_t>(index), static_cast<uint8_t>(channel));
  331. return 0;
  332. }
  333. int CarlaBridgeOsc::handleMsgPluginSetParameterMidiCC(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  334. {
  335. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  336. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  337. carla_debug("CarlaBridgeOsc::handleMsgPluginSetParameterMidiCC()");
  338. const int32_t index = argv[0]->i;
  339. const int32_t cc = argv[1]->i;
  340. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  341. CARLA_SAFE_ASSERT_RETURN(cc >= 1 && cc < 0x5F, 0);
  342. CarlaPluginClient* const plugClient((CarlaPluginClient*)fClient);
  343. plugClient->setParameterMidiCC(static_cast<uint32_t>(index), static_cast<int16_t>(cc));
  344. return 0;
  345. }
  346. int CarlaBridgeOsc::handleMsgPluginSetChunk(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  347. {
  348. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "s");
  349. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  350. carla_debug("CarlaBridgeOsc::handleMsgPluginSetChunk()");
  351. const char* const chunkFile = (const char*)&argv[0]->s;
  352. CarlaPluginClient* const plugClient((CarlaPluginClient*)fClient);
  353. plugClient->setChunkData(chunkFile);
  354. return 0;
  355. }
  356. int CarlaBridgeOsc::handleMsgPluginSetCustomData(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  357. {
  358. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(3, "sss");
  359. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  360. carla_debug("CarlaBridgeOsc::handleMsgPluginSetCustomData()");
  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*)fClient);
  365. plugClient->setCustomData(type, key, value);
  366. return 0;
  367. }
  368. // -------------------------------------------------------------------------
  369. CARLA_BRIDGE_END_NAMESPACE
  370. // -------------------------------------------------------------------------
  371. int main(int argc, char* argv[])
  372. {
  373. CARLA_BRIDGE_USE_NAMESPACE;
  374. // ---------------------------------------------------------------------
  375. // Check argument count
  376. if (argc != 6 && argc != 7)
  377. {
  378. carla_stdout("usage: %s <osc-url|\"null\"> <type> <filename> <name|\"(none)\"> <label>", argv[0]);
  379. return 1;
  380. }
  381. // ---------------------------------------------------------------------
  382. // Get args
  383. const char* const oscUrl = argv[1];
  384. const char* const stype = argv[2];
  385. const char* const filename = argv[3];
  386. const char* name = argv[4];
  387. const char* label = argv[5];
  388. // ---------------------------------------------------------------------
  389. // Setup args
  390. const bool useBridge = (argc == 7);
  391. const bool useOsc = (std::strcmp(oscUrl, "null") != 0 && std::strcmp(oscUrl, "(null)") != 0 && std::strcmp(oscUrl, "NULL") != 0);
  392. if (std::strcmp(name, "(none)") == 0)
  393. name = nullptr;
  394. if (std::strlen(label) == 0)
  395. label = nullptr;
  396. char bridgeBaseAudioName[6+1];
  397. char bridgeBaseControlName[6+1];
  398. if (useBridge)
  399. {
  400. CARLA_SAFE_ASSERT_RETURN(std::strlen(argv[6]) == 6*2, 1);
  401. std::strncpy(bridgeBaseAudioName, argv[6], 6);
  402. std::strncpy(bridgeBaseControlName, argv[6]+6, 6);
  403. bridgeBaseAudioName[6] = '\0';
  404. bridgeBaseControlName[6] = '\0';
  405. }
  406. else
  407. {
  408. bridgeBaseAudioName[0] = '\0';
  409. bridgeBaseControlName[0] = '\0';
  410. }
  411. // ---------------------------------------------------------------------
  412. // Check plugin type
  413. CarlaBackend::PluginType itype(CarlaBackend::getPluginTypeFromString(stype));
  414. if (itype == CarlaBackend::PLUGIN_NONE)
  415. {
  416. carla_stderr("Invalid plugin type '%s'", stype);
  417. return 1;
  418. }
  419. // ---------------------------------------------------------------------
  420. // Set client name
  421. CarlaString clientName((name != nullptr) ? name : label);
  422. //if (clientName.isEmpty())
  423. // clientName = File(filename).getFileNameWithoutExtension().toRawUTF8();
  424. // ---------------------------------------------------------------------
  425. // Set extraStuff
  426. const void* extraStuff = nullptr;
  427. if (itype == CarlaBackend::PLUGIN_GIG || itype == CarlaBackend::PLUGIN_SF2 || itype == CarlaBackend::PLUGIN_SFZ)
  428. {
  429. if (label == nullptr)
  430. label = clientName;
  431. if (std::strstr(label, " (16 outs)") == 0)
  432. extraStuff = (const void*)label; // dummy non-null pointer
  433. }
  434. // ---------------------------------------------------------------------
  435. // Init plugin client
  436. CarlaPluginClient client(useBridge, (const char*)clientName, bridgeBaseAudioName, bridgeBaseControlName);
  437. if (! client.isOk())
  438. {
  439. carla_stderr("Failed to init engine, error was:\n%s", carla_get_last_error());
  440. return 1;
  441. }
  442. // ---------------------------------------------------------------------
  443. // Init OSC
  444. if (useOsc)
  445. client.oscInit(oscUrl);
  446. // ---------------------------------------------------------------------
  447. // Listen for ctrl+c or sigint/sigterm events
  448. initSignalHandler();
  449. // ---------------------------------------------------------------------
  450. // Init plugin
  451. int ret;
  452. if (carla_add_plugin(CarlaBackend::BINARY_NATIVE, itype, filename, name, label, extraStuff))
  453. {
  454. if (useOsc)
  455. {
  456. client.sendOscUpdate();
  457. client.sendOscBridgeUpdate();
  458. }
  459. else
  460. {
  461. carla_set_active(0, true);
  462. if (const CarlaPluginInfo* const pluginInfo = carla_get_plugin_info(0))
  463. {
  464. if (pluginInfo->hints & CarlaBackend::PLUGIN_HAS_CUSTOM_UI)
  465. carla_show_custom_ui(0, true);
  466. }
  467. }
  468. client.ready(!useOsc);
  469. client.exec();
  470. carla_set_engine_about_to_close();
  471. carla_remove_plugin(0);
  472. ret = 0;
  473. }
  474. else
  475. {
  476. const char* const lastError(carla_get_last_error());
  477. carla_stderr("Plugin failed to load, error was:\n%s", lastError);
  478. if (useOsc)
  479. client.sendOscBridgeError(lastError);
  480. ret = 1;
  481. }
  482. // ---------------------------------------------------------------------
  483. // Close OSC
  484. if (useOsc)
  485. client.oscClose();
  486. return ret;
  487. }