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.

434 lines
12KB

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