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.

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