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.

CarlaBridgePlugin.cpp 13KB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Carla Bridge Plugin
  3. * Copyright (C) 2012-2014 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 "CarlaEngine.hpp"
  18. #include "CarlaHost.h"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #ifdef CARLA_OS_UNIX
  22. # include <signal.h>
  23. #endif
  24. #include "jackbridge/JackBridge.hpp"
  25. #include "juce_core.h"
  26. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  27. # include "juce_gui_basics.h"
  28. using juce::JUCEApplication;
  29. using juce::JUCEApplicationBase;
  30. using juce::Timer;
  31. #endif
  32. using CarlaBackend::CarlaEngine;
  33. using CarlaBackend::EngineCallbackOpcode;
  34. using CarlaBackend::EngineCallbackOpcode2Str;
  35. using juce::CharPointer_UTF8;
  36. using juce::File;
  37. using juce::String;
  38. // -------------------------------------------------------------------------
  39. static bool gIsInitiated = false;
  40. static volatile bool gCloseNow = false;
  41. static volatile bool gSaveNow = false;
  42. #ifdef CARLA_OS_WIN
  43. static BOOL WINAPI winSignalHandler(DWORD dwCtrlType) noexcept
  44. {
  45. if (dwCtrlType == CTRL_C_EVENT)
  46. {
  47. gCloseNow = true;
  48. return TRUE;
  49. }
  50. return FALSE;
  51. }
  52. #elif defined(CARLA_OS_LINUX)
  53. static void closeSignalHandler(int) noexcept
  54. {
  55. gCloseNow = true;
  56. }
  57. static void saveSignalHandler(int) noexcept
  58. {
  59. gSaveNow = true;
  60. }
  61. #endif
  62. static void initSignalHandler()
  63. {
  64. #ifdef CARLA_OS_WIN
  65. SetConsoleCtrlHandler(winSignalHandler, TRUE);
  66. #elif defined(CARLA_OS_LINUX)
  67. struct sigaction sint;
  68. struct sigaction sterm;
  69. struct sigaction susr1;
  70. sint.sa_handler = closeSignalHandler;
  71. sint.sa_flags = SA_RESTART;
  72. sint.sa_restorer = nullptr;
  73. sigemptyset(&sint.sa_mask);
  74. sigaction(SIGINT, &sint, nullptr);
  75. sterm.sa_handler = closeSignalHandler;
  76. sterm.sa_flags = SA_RESTART;
  77. sterm.sa_restorer = nullptr;
  78. sigemptyset(&sterm.sa_mask);
  79. sigaction(SIGTERM, &sterm, nullptr);
  80. susr1.sa_handler = saveSignalHandler;
  81. susr1.sa_flags = SA_RESTART;
  82. susr1.sa_restorer = nullptr;
  83. sigemptyset(&susr1.sa_mask);
  84. sigaction(SIGUSR1, &susr1, nullptr);
  85. #endif
  86. }
  87. // -------------------------------------------------------------------------
  88. static String gProjectFilename;
  89. static void gIdle()
  90. {
  91. carla_engine_idle();
  92. if (gSaveNow)
  93. {
  94. gSaveNow = false;
  95. if (gProjectFilename.isNotEmpty())
  96. {
  97. if (! carla_save_plugin_state(0, gProjectFilename.toRawUTF8()))
  98. carla_stderr("Plugin preset save failed, error was:\n%s", carla_get_last_error());
  99. }
  100. }
  101. }
  102. // -------------------------------------------------------------------------
  103. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  104. class CarlaJuceApp : public JUCEApplication,
  105. private Timer
  106. {
  107. public:
  108. CarlaJuceApp() {}
  109. ~CarlaJuceApp() {}
  110. void initialise(const String&) override
  111. {
  112. startTimer(8);
  113. }
  114. void shutdown() override
  115. {
  116. gCloseNow = true;
  117. stopTimer();
  118. }
  119. const String getApplicationName() override
  120. {
  121. return "CarlaPlugin";
  122. }
  123. const String getApplicationVersion() override
  124. {
  125. return CARLA_VERSION_STRING;
  126. }
  127. void timerCallback() override
  128. {
  129. gIdle();
  130. if (gCloseNow)
  131. {
  132. quit();
  133. gCloseNow = false;
  134. }
  135. }
  136. };
  137. static JUCEApplicationBase* juce_CreateApplication() { return new CarlaJuceApp(); }
  138. #endif
  139. // -------------------------------------------------------------------------
  140. class CarlaBridgePlugin
  141. {
  142. public:
  143. CarlaBridgePlugin(const bool useBridge, const char* const clientName, const char* const audioPoolBaseName,
  144. const char* const rtClientBaseName, const char* const nonRtClientBaseName, const char* const nonRtServerBaseName)
  145. : fEngine(nullptr),
  146. fUsingBridge(false),
  147. leakDetector_CarlaBridgePlugin()
  148. {
  149. CARLA_ASSERT(clientName != nullptr && clientName[0] != '\0');
  150. carla_debug("CarlaBridgePlugin::CarlaBridgePlugin(%s, \"%s\", %s, %s, %s, %s)", bool2str(useBridge), clientName, audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  151. carla_set_engine_callback(callback, this);
  152. if (useBridge)
  153. carla_engine_init_bridge(audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName, clientName);
  154. else
  155. carla_engine_init("JACK", clientName);
  156. fEngine = carla_get_engine();
  157. }
  158. ~CarlaBridgePlugin()
  159. {
  160. carla_debug("CarlaBridgePlugin::~CarlaBridgePlugin()");
  161. carla_engine_close();
  162. }
  163. bool isOk() const noexcept
  164. {
  165. return (fEngine != nullptr);
  166. }
  167. // ---------------------------------------------------------------------
  168. void exec(const bool useBridge, int argc, char* argv[])
  169. {
  170. fUsingBridge = useBridge;
  171. if (! useBridge)
  172. {
  173. const CarlaPluginInfo* const pInfo(carla_get_plugin_info(0));
  174. CARLA_SAFE_ASSERT_RETURN(pInfo != nullptr,);
  175. gProjectFilename = CharPointer_UTF8(pInfo->name);
  176. gProjectFilename += ".carxs";
  177. if (! File::isAbsolutePath(gProjectFilename))
  178. gProjectFilename = File::getCurrentWorkingDirectory().getChildFile(gProjectFilename).getFullPathName();
  179. if (File(gProjectFilename).existsAsFile() && ! carla_load_plugin_state(0, gProjectFilename.toRawUTF8()))
  180. carla_stderr("Plugin preset load failed, error was:\n%s", carla_get_last_error());
  181. }
  182. gIsInitiated = true;
  183. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  184. JUCEApplicationBase::createInstance = &juce_CreateApplication;
  185. JUCEApplicationBase::main(JUCE_MAIN_FUNCTION_ARGS);
  186. #else
  187. for (; ! gCloseNow;)
  188. {
  189. gIdle();
  190. carla_msleep(8);
  191. }
  192. #endif
  193. carla_set_engine_about_to_close();
  194. carla_remove_plugin(0);
  195. // may be unused
  196. return; (void)argc; (void)argv;
  197. }
  198. // ---------------------------------------------------------------------
  199. protected:
  200. void handleCallback(const EngineCallbackOpcode action, const int value1, const int, const float, const char* const)
  201. {
  202. CARLA_BACKEND_USE_NAMESPACE;
  203. switch (action)
  204. {
  205. case ENGINE_CALLBACK_ENGINE_STOPPED:
  206. case ENGINE_CALLBACK_PLUGIN_REMOVED:
  207. case ENGINE_CALLBACK_QUIT:
  208. gCloseNow = true;
  209. break;
  210. case ENGINE_CALLBACK_UI_STATE_CHANGED:
  211. if (gIsInitiated && value1 != 1 && ! fUsingBridge)
  212. gCloseNow = true;
  213. break;
  214. default:
  215. break;
  216. }
  217. }
  218. private:
  219. const CarlaEngine* fEngine;
  220. bool fUsingBridge;
  221. static void callback(void* ptr, EngineCallbackOpcode action, unsigned int pluginId, int value1, int value2, float value3, const char* valueStr)
  222. {
  223. carla_debug("CarlaBridgePlugin::callback(%p, %i:%s, %i, %i, %i, %f, \"%s\")", ptr, action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  224. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  225. CARLA_SAFE_ASSERT_RETURN(pluginId == 0,);
  226. return ((CarlaBridgePlugin*)ptr)->handleCallback(action, value1, value2, value3, valueStr);
  227. }
  228. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgePlugin)
  229. };
  230. // -------------------------------------------------------------------------
  231. int main(int argc, char* argv[])
  232. {
  233. // ---------------------------------------------------------------------
  234. // Check argument count
  235. if (argc != 4 && argc != 5)
  236. {
  237. carla_stdout("usage: %s <type> <filename> <label> [uniqueId]", argv[0]);
  238. return 1;
  239. }
  240. #if defined(CARLA_OS_WIN) && ! defined(BUILDING_CARLA_FOR_WINDOWS)
  241. // ---------------------------------------------------------------------
  242. // Test if bridge is working
  243. if (! jackbridge_is_ok())
  244. {
  245. carla_stderr("A JACK or Wine library is missing, cannot continue");
  246. return 1;
  247. }
  248. #endif
  249. // ---------------------------------------------------------------------
  250. // Get args
  251. const char* const stype = argv[1];
  252. const char* filename = argv[2];
  253. const char* label = argv[3];
  254. const int64_t uniqueId = (argc == 5) ? static_cast<int64_t>(std::atoll(argv[4])) : 0;
  255. if (filename[0] == '\0' || std::strcmp(filename, "(none)") == 0)
  256. filename = nullptr;
  257. if (label[0] == '\0' || std::strcmp(label, "(none)") == 0)
  258. label = nullptr;
  259. // ---------------------------------------------------------------------
  260. // Check binary type
  261. CarlaBackend::BinaryType btype = CarlaBackend::BINARY_NATIVE;
  262. if (const char* const binaryTypeStr = std::getenv("CARLA_BRIDGE_PLUGIN_BINARY_TYPE"))
  263. btype = CarlaBackend::getBinaryTypeFromString(binaryTypeStr);
  264. if (btype == CarlaBackend::BINARY_NONE)
  265. {
  266. carla_stderr("Invalid binary type '%i'", btype);
  267. return 1;
  268. }
  269. // ---------------------------------------------------------------------
  270. // Check plugin type
  271. CarlaBackend::PluginType itype(CarlaBackend::getPluginTypeFromString(stype));
  272. if (itype == CarlaBackend::PLUGIN_NONE)
  273. {
  274. carla_stderr("Invalid plugin type '%s'", stype);
  275. return 1;
  276. }
  277. // ---------------------------------------------------------------------
  278. // Set name
  279. const char* name(std::getenv("CARLA_CLIENT_NAME"));
  280. if (name != nullptr && (name[0] == '\0' || std::strcmp(name, "(none)") == 0))
  281. name = nullptr;
  282. // ---------------------------------------------------------------------
  283. // Setup options
  284. const char* const shmIds(std::getenv("ENGINE_BRIDGE_SHM_IDS"));
  285. const bool useBridge = (shmIds != nullptr);
  286. // ---------------------------------------------------------------------
  287. // Setup bridge ids
  288. char audioPoolBaseName[6+1];
  289. char rtClientBaseName[6+1];
  290. char nonRtClientBaseName[6+1];
  291. char nonRtServerBaseName[6+1];
  292. if (useBridge)
  293. {
  294. CARLA_SAFE_ASSERT_RETURN(std::strlen(shmIds) == 6*4, 1);
  295. std::strncpy(audioPoolBaseName, shmIds+6*0, 6);
  296. std::strncpy(rtClientBaseName, shmIds+6*1, 6);
  297. std::strncpy(nonRtClientBaseName, shmIds+6*2, 6);
  298. std::strncpy(nonRtServerBaseName, shmIds+6*3, 6);
  299. audioPoolBaseName[6] = '\0';
  300. rtClientBaseName[6] = '\0';
  301. nonRtClientBaseName[6] = '\0';
  302. nonRtServerBaseName[6] = '\0';
  303. }
  304. else
  305. {
  306. audioPoolBaseName[0] = '\0';
  307. rtClientBaseName[0] = '\0';
  308. nonRtClientBaseName[0] = '\0';
  309. nonRtServerBaseName[0] = '\0';
  310. }
  311. // ---------------------------------------------------------------------
  312. // Set client name
  313. CarlaString clientName(name != nullptr ? name : label);
  314. if (clientName.isEmpty())
  315. {
  316. const String jfilename = String(CharPointer_UTF8(filename));
  317. clientName = File(jfilename).getFileNameWithoutExtension().toRawUTF8();
  318. }
  319. // ---------------------------------------------------------------------
  320. // Set extraStuff
  321. const void* extraStuff = nullptr;
  322. if (itype == CarlaBackend::PLUGIN_GIG || itype == CarlaBackend::PLUGIN_SF2)
  323. {
  324. if (label == nullptr)
  325. label = clientName;
  326. if (std::strstr(label, " (16 outs)") != nullptr)
  327. extraStuff = "true";
  328. }
  329. // ---------------------------------------------------------------------
  330. // Init plugin bridge
  331. CarlaBridgePlugin bridge(useBridge, clientName, audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  332. if (! bridge.isOk())
  333. {
  334. carla_stderr("Failed to init engine, error was:\n%s", carla_get_last_error());
  335. return 1;
  336. }
  337. // ---------------------------------------------------------------------
  338. // Listen for ctrl+c or sigint/sigterm events
  339. initSignalHandler();
  340. // ---------------------------------------------------------------------
  341. // Init plugin
  342. int ret;
  343. if (carla_add_plugin(btype, itype, filename, name, label, uniqueId, extraStuff, 0x0))
  344. {
  345. ret = 0;
  346. if (! useBridge)
  347. {
  348. carla_set_active(0, true);
  349. if (const CarlaPluginInfo* const pluginInfo = carla_get_plugin_info(0))
  350. {
  351. if (pluginInfo->hints & CarlaBackend::PLUGIN_HAS_CUSTOM_UI)
  352. carla_show_custom_ui(0, true);
  353. }
  354. }
  355. bridge.exec(useBridge, argc, argv);
  356. }
  357. else
  358. {
  359. ret = 1;
  360. const char* const lastError(carla_get_last_error());
  361. carla_stderr("Plugin failed to load, error was:\n%s", lastError);
  362. //if (useBridge)
  363. // bridge.sendOscBridgeError(lastError);
  364. }
  365. return ret;
  366. }