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.

634 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. static 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. static 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. for (; ! 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 uint32_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 uint32_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. if (carla_get_plugin_info(0)->hints & CarlaBackend::PLUGIN_HAS_CUSTOM_UI)
  299. carla_show_custom_ui(0, true);
  300. return 0;
  301. }
  302. int CarlaBridgeOsc::handleMsgHide()
  303. {
  304. carla_debug("CarlaBridgeOsc::handleMsgHide()");
  305. if (carla_get_plugin_info(0)->hints & CarlaBackend::PLUGIN_HAS_CUSTOM_UI)
  306. carla_show_custom_ui(0, false);
  307. return 0;
  308. }
  309. int CarlaBridgeOsc::handleMsgQuit()
  310. {
  311. carla_debug("CarlaBridgeOsc::handleMsgQuit()");
  312. gCloseNow = true;
  313. return 0;
  314. }
  315. // -------------------------------------------------------------------------
  316. int CarlaBridgeOsc::handleMsgPluginSaveNow()
  317. {
  318. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  319. carla_debug("CarlaBridgeOsc::handleMsgPluginSaveNow()");
  320. CarlaPluginClient* const plugClient((CarlaPluginClient*)fClient);
  321. plugClient->saveNow();
  322. return 0;
  323. }
  324. int CarlaBridgeOsc::handleMsgPluginSetParameterMidiChannel(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  325. {
  326. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  327. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  328. carla_debug("CarlaBridgeOsc::handleMsgPluginSetParameterMidiChannel()");
  329. const int32_t index = argv[0]->i;
  330. const int32_t channel = argv[1]->i;
  331. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  332. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  333. CarlaPluginClient* const plugClient((CarlaPluginClient*)fClient);
  334. plugClient->setParameterMidiChannel(static_cast<uint32_t>(index), static_cast<uint8_t>(channel));
  335. return 0;
  336. }
  337. int CarlaBridgeOsc::handleMsgPluginSetParameterMidiCC(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  338. {
  339. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  340. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  341. carla_debug("CarlaBridgeOsc::handleMsgPluginSetParameterMidiCC()");
  342. const int32_t index = argv[0]->i;
  343. const int32_t cc = argv[1]->i;
  344. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  345. CARLA_SAFE_ASSERT_RETURN(cc >= 1 && cc < 0x5F, 0);
  346. CarlaPluginClient* const plugClient((CarlaPluginClient*)fClient);
  347. plugClient->setParameterMidiCC(static_cast<uint32_t>(index), static_cast<int16_t>(cc));
  348. return 0;
  349. }
  350. int CarlaBridgeOsc::handleMsgPluginSetChunk(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  351. {
  352. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "s");
  353. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  354. carla_debug("CarlaBridgeOsc::handleMsgPluginSetChunk()");
  355. const char* const chunkFile = (const char*)&argv[0]->s;
  356. CarlaPluginClient* const plugClient((CarlaPluginClient*)fClient);
  357. plugClient->setChunkData(chunkFile);
  358. return 0;
  359. }
  360. int CarlaBridgeOsc::handleMsgPluginSetCustomData(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  361. {
  362. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(3, "sss");
  363. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  364. carla_debug("CarlaBridgeOsc::handleMsgPluginSetCustomData()");
  365. const char* const type = (const char*)&argv[0]->s;
  366. const char* const key = (const char*)&argv[1]->s;
  367. const char* const value = (const char*)&argv[2]->s;
  368. CarlaPluginClient* const plugClient((CarlaPluginClient*)fClient);
  369. plugClient->setCustomData(type, key, value);
  370. return 0;
  371. }
  372. // -------------------------------------------------------------------------
  373. CARLA_BRIDGE_END_NAMESPACE
  374. // -------------------------------------------------------------------------
  375. int main(int argc, char* argv[])
  376. {
  377. CARLA_BRIDGE_USE_NAMESPACE;
  378. // ---------------------------------------------------------------------
  379. // Check argument count
  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. // ---------------------------------------------------------------------
  386. // Get args
  387. const char* const oscUrl = argv[1];
  388. const char* const stype = argv[2];
  389. const char* const filename = argv[3];
  390. const char* name = argv[4];
  391. const char* label = argv[5];
  392. // ---------------------------------------------------------------------
  393. // Setup args
  394. const bool useBridge = (argc == 7);
  395. const bool useOsc = (std::strcmp(oscUrl, "null") != 0 && std::strcmp(oscUrl, "(null)") != 0 && std::strcmp(oscUrl, "NULL") != 0);
  396. if (std::strcmp(name, "(none)") == 0)
  397. name = nullptr;
  398. if (std::strlen(label) == 0)
  399. label = nullptr;
  400. char bridgeBaseAudioName[6+1];
  401. char bridgeBaseControlName[6+1];
  402. if (useBridge)
  403. {
  404. CARLA_SAFE_ASSERT_RETURN(std::strlen(argv[6]) == 6*2, 1);
  405. std::strncpy(bridgeBaseAudioName, argv[6], 6);
  406. std::strncpy(bridgeBaseControlName, argv[6]+6, 6);
  407. bridgeBaseAudioName[6] = '\0';
  408. bridgeBaseControlName[6] = '\0';
  409. }
  410. else
  411. {
  412. bridgeBaseAudioName[0] = '\0';
  413. bridgeBaseControlName[0] = '\0';
  414. }
  415. // ---------------------------------------------------------------------
  416. // Check plugin type
  417. CarlaBackend::PluginType itype(CarlaBackend::getPluginTypeFromString(stype));
  418. if (itype == CarlaBackend::PLUGIN_NONE)
  419. {
  420. carla_stderr("Invalid plugin type '%s'", stype);
  421. return 1;
  422. }
  423. // ---------------------------------------------------------------------
  424. // Set client name
  425. CarlaString clientName((name != nullptr) ? name : label);
  426. //if (clientName.isEmpty())
  427. // clientName = File(filename).getFileNameWithoutExtension().toRawUTF8();
  428. // ---------------------------------------------------------------------
  429. // Set extraStuff
  430. const void* extraStuff = nullptr;
  431. if (itype == CarlaBackend::PLUGIN_FILE_GIG || itype == CarlaBackend::PLUGIN_FILE_SF2)
  432. {
  433. if (label == nullptr)
  434. label = clientName;
  435. if (std::strstr(label, " (16 outs)") == 0)
  436. extraStuff = "true";
  437. }
  438. // ---------------------------------------------------------------------
  439. // Init plugin client
  440. CarlaPluginClient client(useBridge, (const char*)clientName, bridgeBaseAudioName, bridgeBaseControlName);
  441. if (! client.isOk())
  442. {
  443. carla_stderr("Failed to init engine, error was:\n%s", carla_get_last_error());
  444. return 1;
  445. }
  446. // ---------------------------------------------------------------------
  447. // Init OSC
  448. if (useOsc)
  449. client.oscInit(oscUrl);
  450. // ---------------------------------------------------------------------
  451. // Listen for ctrl+c or sigint/sigterm events
  452. initSignalHandler();
  453. // ---------------------------------------------------------------------
  454. // Init plugin
  455. int ret;
  456. if (carla_add_plugin(CarlaBackend::BINARY_NATIVE, itype, filename, name, label, extraStuff))
  457. {
  458. if (useOsc)
  459. {
  460. client.sendOscUpdate();
  461. client.sendOscBridgeUpdate();
  462. }
  463. else
  464. {
  465. carla_set_active(0, true);
  466. if (const CarlaPluginInfo* const pluginInfo = carla_get_plugin_info(0))
  467. {
  468. if (pluginInfo->hints & CarlaBackend::PLUGIN_HAS_CUSTOM_UI)
  469. carla_show_custom_ui(0, true);
  470. }
  471. }
  472. client.ready(!useOsc);
  473. client.exec();
  474. carla_set_engine_about_to_close();
  475. carla_remove_plugin(0);
  476. ret = 0;
  477. }
  478. else
  479. {
  480. const char* const lastError(carla_get_last_error());
  481. carla_stderr("Plugin failed to load, error was:\n%s", lastError);
  482. if (useOsc)
  483. client.sendOscBridgeError(lastError);
  484. ret = 1;
  485. }
  486. // ---------------------------------------------------------------------
  487. // Close OSC
  488. if (useOsc)
  489. client.oscClose();
  490. return ret;
  491. }