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.

611 lines
17KB

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