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.

577 lines
18KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2019 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 "CarlaDefines.h"
  18. #ifdef HAVE_LIBLO
  19. #include "CarlaEngine.hpp"
  20. #include "CarlaEngineOsc.hpp"
  21. #include "CarlaPlugin.hpp"
  22. #include "CarlaMIDI.h"
  23. #include <cctype>
  24. CARLA_BACKEND_START_NAMESPACE
  25. // -----------------------------------------------------------------------
  26. CarlaEngineOsc::CarlaEngineOsc(CarlaEngine* const engine) noexcept
  27. : fEngine(engine),
  28. fControlData(),
  29. fName(),
  30. fServerPathTCP(),
  31. fServerPathUDP(),
  32. fServerTCP(nullptr),
  33. fServerUDP(nullptr)
  34. {
  35. CARLA_SAFE_ASSERT(engine != nullptr);
  36. carla_debug("CarlaEngineOsc::CarlaEngineOsc(%p)", engine);
  37. }
  38. CarlaEngineOsc::~CarlaEngineOsc() noexcept
  39. {
  40. CARLA_SAFE_ASSERT(fName.isEmpty());
  41. CARLA_SAFE_ASSERT(fServerPathTCP.isEmpty());
  42. CARLA_SAFE_ASSERT(fServerPathUDP.isEmpty());
  43. CARLA_SAFE_ASSERT(fServerTCP == nullptr);
  44. CARLA_SAFE_ASSERT(fServerUDP == nullptr);
  45. carla_debug("CarlaEngineOsc::~CarlaEngineOsc()");
  46. }
  47. // -----------------------------------------------------------------------
  48. void CarlaEngineOsc::init(const char* const name, int tcpPort, int udpPort) noexcept
  49. {
  50. CARLA_SAFE_ASSERT_RETURN(fName.isEmpty(),);
  51. CARLA_SAFE_ASSERT_RETURN(fServerPathTCP.isEmpty(),);
  52. CARLA_SAFE_ASSERT_RETURN(fServerPathUDP.isEmpty(),);
  53. CARLA_SAFE_ASSERT_RETURN(fServerTCP == nullptr,);
  54. CARLA_SAFE_ASSERT_RETURN(fServerUDP == nullptr,);
  55. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  56. carla_debug("CarlaEngineOsc::init(\"%s\")", name);
  57. fName = name;
  58. fName.toBasic();
  59. if (fEngine->getType() != kEngineTypePlugin)
  60. {
  61. const char* const tcpPortEnv = std::getenv("CARLA_OSC_TCP_PORT");
  62. const char* const udpPortEnv = std::getenv("CARLA_OSC_UDP_PORT");
  63. if (tcpPortEnv != nullptr)
  64. tcpPort = std::atoi(tcpPortEnv);
  65. if (udpPortEnv != nullptr)
  66. udpPort = std::atoi(udpPortEnv);
  67. }
  68. // port == 0 means to pick a random one
  69. // port < 0 will get osc disabled
  70. // ----------------------------------------------------------------------------------------------------------------
  71. if (tcpPort == 0)
  72. {
  73. for (int i=0; i < 5 && fServerTCP == nullptr; ++i)
  74. fServerTCP = lo_server_new_with_proto(nullptr, LO_TCP, osc_error_handler_TCP);
  75. }
  76. else if (tcpPort >= 1024)
  77. {
  78. char strBuf[0xff];
  79. std::snprintf(strBuf, 0xff-1, "%d", tcpPort);
  80. strBuf[0xff-1] = '\0';
  81. fServerTCP = lo_server_new_with_proto(strBuf, LO_TCP, osc_error_handler_TCP);
  82. }
  83. if (fServerTCP != nullptr)
  84. {
  85. if (char* const tmpServerPathTCP = lo_server_get_url(fServerTCP))
  86. {
  87. fServerPathTCP = tmpServerPathTCP;
  88. fServerPathTCP += fName;
  89. std::free(tmpServerPathTCP);
  90. }
  91. lo_server_add_method(fServerTCP, nullptr, nullptr, osc_message_handler_TCP, this);
  92. }
  93. // ----------------------------------------------------------------------------------------------------------------
  94. if (udpPort == 0)
  95. {
  96. for (int i=0; i < 5 && fServerUDP == nullptr; ++i)
  97. fServerUDP = lo_server_new_with_proto(nullptr, LO_UDP, osc_error_handler_UDP);
  98. }
  99. else if (udpPort >= 1024)
  100. {
  101. char strBuf[0xff];
  102. std::snprintf(strBuf, 0xff-1, "%d", udpPort);
  103. strBuf[0xff-1] = '\0';
  104. fServerUDP = lo_server_new_with_proto(strBuf, LO_UDP, osc_error_handler_UDP);
  105. }
  106. if (fServerUDP != nullptr)
  107. {
  108. if (char* const tmpServerPathUDP = lo_server_get_url(fServerUDP))
  109. {
  110. fServerPathUDP = tmpServerPathUDP;
  111. fServerPathUDP += fName;
  112. std::free(tmpServerPathUDP);
  113. }
  114. lo_server_add_method(fServerUDP, nullptr, nullptr, osc_message_handler_UDP, this);
  115. }
  116. // ----------------------------------------------------------------------------------------------------------------
  117. CARLA_SAFE_ASSERT(fName.isNotEmpty());
  118. }
  119. void CarlaEngineOsc::idle() const noexcept
  120. {
  121. if (fServerTCP != nullptr)
  122. {
  123. for (;;)
  124. {
  125. try {
  126. if (lo_server_recv_noblock(fServerTCP, 0) == 0)
  127. break;
  128. } CARLA_SAFE_EXCEPTION_CONTINUE("OSC idle TCP")
  129. }
  130. }
  131. if (fServerUDP != nullptr)
  132. {
  133. for (;;)
  134. {
  135. try {
  136. if (lo_server_recv_noblock(fServerUDP, 0) == 0)
  137. break;
  138. } CARLA_SAFE_EXCEPTION_CONTINUE("OSC idle UDP")
  139. }
  140. }
  141. }
  142. void CarlaEngineOsc::close() noexcept
  143. {
  144. CARLA_SAFE_ASSERT(fName.isNotEmpty());
  145. CARLA_SAFE_ASSERT(fServerPathTCP.isNotEmpty());
  146. CARLA_SAFE_ASSERT(fServerPathUDP.isNotEmpty());
  147. CARLA_SAFE_ASSERT(fServerTCP != nullptr);
  148. CARLA_SAFE_ASSERT(fServerUDP != nullptr);
  149. carla_debug("CarlaEngineOsc::close()");
  150. fName.clear();
  151. if (fServerTCP != nullptr)
  152. {
  153. lo_server_del_method(fServerTCP, nullptr, nullptr);
  154. lo_server_free(fServerTCP);
  155. fServerTCP = nullptr;
  156. }
  157. if (fServerUDP != nullptr)
  158. {
  159. lo_server_del_method(fServerUDP, nullptr, nullptr);
  160. lo_server_free(fServerUDP);
  161. fServerUDP = nullptr;
  162. }
  163. fServerPathTCP.clear();
  164. fServerPathUDP.clear();
  165. fControlData.clear();
  166. }
  167. // -----------------------------------------------------------------------
  168. int CarlaEngineOsc::handleMessage(const bool isTCP, const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  169. {
  170. CARLA_SAFE_ASSERT_RETURN(fName.isNotEmpty(), 1);
  171. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', 1);
  172. #ifdef DEBUG
  173. if (std::strstr(path, "/bridge_pong") == nullptr) {
  174. carla_debug("CarlaEngineOsc::handleMessage(%s, \"%s\", %i, %p, \"%s\", %p)", bool2str(isTCP), path, argc, argv, types, msg);
  175. }
  176. #endif
  177. if (isTCP)
  178. {
  179. CARLA_SAFE_ASSERT_RETURN(fServerPathTCP.isNotEmpty(), 1);
  180. CARLA_SAFE_ASSERT_RETURN(fServerTCP != nullptr, 1);
  181. }
  182. else
  183. {
  184. CARLA_SAFE_ASSERT_RETURN(fServerPathUDP.isNotEmpty(), 1);
  185. CARLA_SAFE_ASSERT_RETURN(fServerUDP != nullptr, 1);
  186. }
  187. // Initial path check
  188. if (std::strcmp(path, "/register") == 0)
  189. return handleMsgRegister(isTCP, argc, argv, types);
  190. if (std::strcmp(path, "/unregister") == 0)
  191. return handleMsgUnregister();
  192. const std::size_t nameSize(fName.length());
  193. // Check if message is for this client
  194. if (std::strlen(path) <= nameSize || std::strncmp(path+1, fName, nameSize) != 0)
  195. {
  196. carla_stderr("CarlaEngineOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, fName.buffer());
  197. return 1;
  198. }
  199. // Get plugin id from path, "/carla/23/method" -> 23
  200. uint pluginId = 0;
  201. std::size_t offset;
  202. if (std::isdigit(path[nameSize+2]))
  203. {
  204. if (std::isdigit(path[nameSize+3]))
  205. {
  206. if (std::isdigit(path[nameSize+5]))
  207. {
  208. carla_stderr2("CarlaEngineOsc::handleMessage() - invalid plugin id, over 999? (value: \"%s\")", path+(nameSize+1));
  209. return 1;
  210. }
  211. else if (std::isdigit(path[nameSize+4]))
  212. {
  213. // 3 digits, /xyz/method
  214. offset = 6;
  215. pluginId += uint(path[nameSize+2]-'0')*100;
  216. pluginId += uint(path[nameSize+3]-'0')*10;
  217. pluginId += uint(path[nameSize+4]-'0');
  218. }
  219. else
  220. {
  221. // 2 digits, /xy/method
  222. offset = 5;
  223. pluginId += uint(path[nameSize+2]-'0')*10;
  224. pluginId += uint(path[nameSize+3]-'0');
  225. }
  226. }
  227. else
  228. {
  229. // single digit, /x/method
  230. offset = 4;
  231. pluginId += uint(path[nameSize+2]-'0');
  232. }
  233. }
  234. else
  235. {
  236. carla_stderr("CarlaEngineOsc::handleMessage() - invalid message '%s'", path);
  237. return 1;
  238. }
  239. if (pluginId > fEngine->getCurrentPluginCount())
  240. {
  241. carla_stderr("CarlaEngineOsc::handleMessage() - failed to get plugin, wrong id '%i'", pluginId);
  242. return 0;
  243. }
  244. // Get plugin
  245. CarlaPlugin* const plugin(fEngine->getPluginUnchecked(pluginId));
  246. if (plugin == nullptr || plugin->getId() != pluginId)
  247. {
  248. carla_stderr("CarlaEngineOsc::handleMessage() - invalid plugin id '%i', probably has been removed (path: '%s')", pluginId, path);
  249. return 0;
  250. }
  251. // Get method from path, "/Carla/i/method" -> "method"
  252. char method[32+1];
  253. method[32] = '\0';
  254. std::strncpy(method, path + (nameSize + offset), 32);
  255. if (method[0] == '\0')
  256. {
  257. carla_stderr("CarlaEngineOsc::handleMessage(%s, \"%s\", ...) - received message without method", bool2str(isTCP), path);
  258. return 0;
  259. }
  260. // Internal methods
  261. if (std::strcmp(method, "set_option") == 0)
  262. return 0; //handleMsgSetOption(plugin, argc, argv, types); // TODO
  263. if (std::strcmp(method, "set_active") == 0)
  264. return handleMsgSetActive(plugin, argc, argv, types);
  265. if (std::strcmp(method, "set_drywet") == 0)
  266. return handleMsgSetDryWet(plugin, argc, argv, types);
  267. if (std::strcmp(method, "set_volume") == 0)
  268. return handleMsgSetVolume(plugin, argc, argv, types);
  269. if (std::strcmp(method, "set_balance_left") == 0)
  270. return handleMsgSetBalanceLeft(plugin, argc, argv, types);
  271. if (std::strcmp(method, "set_balance_right") == 0)
  272. return handleMsgSetBalanceRight(plugin, argc, argv, types);
  273. if (std::strcmp(method, "set_panning") == 0)
  274. return handleMsgSetPanning(plugin, argc, argv, types);
  275. if (std::strcmp(method, "set_ctrl_channel") == 0)
  276. return 0; //handleMsgSetControlChannel(plugin, argc, argv, types); // TODO
  277. if (std::strcmp(method, "set_parameter_value") == 0)
  278. return handleMsgSetParameterValue(plugin, argc, argv, types);
  279. if (std::strcmp(method, "set_parameter_midi_cc") == 0)
  280. return handleMsgSetParameterMidiCC(plugin, argc, argv, types);
  281. if (std::strcmp(method, "set_parameter_midi_channel") == 0)
  282. return handleMsgSetParameterMidiChannel(plugin, argc, argv, types);
  283. if (std::strcmp(method, "set_program") == 0)
  284. return handleMsgSetProgram(plugin, argc, argv, types);
  285. if (std::strcmp(method, "set_midi_program") == 0)
  286. return handleMsgSetMidiProgram(plugin, argc, argv, types);
  287. if (std::strcmp(method, "set_custom_data") == 0)
  288. return 0; //handleMsgSetCustomData(plugin, argc, argv, types); // TODO
  289. if (std::strcmp(method, "set_chunk") == 0)
  290. return 0; //handleMsgSetChunk(plugin, argc, argv, types); // TODO
  291. if (std::strcmp(method, "note_on") == 0)
  292. return handleMsgNoteOn(plugin, argc, argv, types);
  293. if (std::strcmp(method, "note_off") == 0)
  294. return handleMsgNoteOff(plugin, argc, argv, types);
  295. // Send all other methods to plugins, TODO
  296. plugin->handleOscMessage(method, argc, argv, types, msg);
  297. return 0;
  298. }
  299. // -----------------------------------------------------------------------
  300. int CarlaEngineOsc::handleMsgRegister(const bool isTCP, const int argc, const lo_arg* const* const argv, const char* const types)
  301. {
  302. carla_debug("CarlaEngineOsc::handleMsgRegister()");
  303. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "s");
  304. if (fControlData.path != nullptr)
  305. {
  306. carla_stderr("CarlaEngineOsc::handleMsgRegister() - OSC backend already registered to %s", fControlData.path);
  307. return 1;
  308. }
  309. const char* const url = &argv[0]->s;
  310. carla_debug("CarlaEngineOsc::handleMsgRegister() - OSC backend registered to %s", url);
  311. {
  312. const lo_address addr = lo_address_new_from_url(url);
  313. const char* const host = lo_address_get_hostname(addr);
  314. const char* const port = lo_address_get_port(addr);
  315. fControlData.source = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  316. fControlData.path = carla_strdup_free(lo_url_get_path(url));
  317. fControlData.target = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  318. }
  319. for (uint i=0, count=fEngine->getCurrentPluginCount(); i < count; ++i)
  320. {
  321. CarlaPlugin* const plugin(fEngine->getPluginUnchecked(i));
  322. if (plugin != nullptr && plugin->isEnabled())
  323. plugin->registerToOscClient();
  324. }
  325. return 0;
  326. }
  327. int CarlaEngineOsc::handleMsgUnregister()
  328. {
  329. carla_debug("CarlaEngineOsc::handleMsgUnregister()");
  330. if (fControlData.path == nullptr)
  331. {
  332. carla_stderr("CarlaEngineOsc::handleMsgUnregister() - OSC backend is not registered yet");
  333. return 1;
  334. }
  335. fControlData.clear();
  336. return 0;
  337. }
  338. // -----------------------------------------------------------------------
  339. int CarlaEngineOsc::handleMsgSetActive(CARLA_ENGINE_OSC_HANDLE_ARGS)
  340. {
  341. carla_debug("CarlaEngineOsc::handleMsgSetActive()");
  342. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  343. const bool active = (argv[0]->i != 0);
  344. plugin->setActive(active, false, true);
  345. return 0;
  346. }
  347. int CarlaEngineOsc::handleMsgSetDryWet(CARLA_ENGINE_OSC_HANDLE_ARGS)
  348. {
  349. carla_debug("CarlaEngineOsc::handleMsgSetDryWet()");
  350. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  351. const float value = argv[0]->f;
  352. plugin->setDryWet(value, false, true);
  353. return 0;
  354. }
  355. int CarlaEngineOsc::handleMsgSetVolume(CARLA_ENGINE_OSC_HANDLE_ARGS)
  356. {
  357. carla_debug("CarlaEngineOsc::handleMsgSetVolume()");
  358. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  359. const float value = argv[0]->f;
  360. plugin->setVolume(value, false, true);
  361. return 0;
  362. }
  363. int CarlaEngineOsc::handleMsgSetBalanceLeft(CARLA_ENGINE_OSC_HANDLE_ARGS)
  364. {
  365. carla_debug("CarlaEngineOsc::handleMsgSetBalanceLeft()");
  366. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  367. const float value = argv[0]->f;
  368. plugin->setBalanceLeft(value, false, true);
  369. return 0;
  370. }
  371. int CarlaEngineOsc::handleMsgSetBalanceRight(CARLA_ENGINE_OSC_HANDLE_ARGS)
  372. {
  373. carla_debug("CarlaEngineOsc::handleMsgSetBalanceRight()");
  374. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  375. const float value = argv[0]->f;
  376. plugin->setBalanceRight(value, false, true);
  377. return 0;
  378. }
  379. int CarlaEngineOsc::handleMsgSetPanning(CARLA_ENGINE_OSC_HANDLE_ARGS)
  380. {
  381. carla_debug("CarlaEngineOsc::handleMsgSetPanning()");
  382. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  383. const float value = argv[0]->f;
  384. plugin->setPanning(value, false, true);
  385. return 0;
  386. }
  387. int CarlaEngineOsc::handleMsgSetParameterValue(CARLA_ENGINE_OSC_HANDLE_ARGS)
  388. {
  389. carla_debug("CarlaEngineOsc::handleMsgSetParameterValue()");
  390. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "if");
  391. const int32_t index = argv[0]->i;
  392. const float value = argv[1]->f;
  393. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  394. plugin->setParameterValue(static_cast<uint32_t>(index), value, true, false, true);
  395. return 0;
  396. }
  397. int CarlaEngineOsc::handleMsgSetParameterMidiCC(CARLA_ENGINE_OSC_HANDLE_ARGS)
  398. {
  399. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiCC()");
  400. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  401. const int32_t index = argv[0]->i;
  402. const int32_t cc = argv[1]->i;
  403. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  404. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL, 0);
  405. plugin->setParameterMidiCC(static_cast<uint32_t>(index), static_cast<int16_t>(cc), false, true);
  406. return 0;
  407. }
  408. int CarlaEngineOsc::handleMsgSetParameterMidiChannel(CARLA_ENGINE_OSC_HANDLE_ARGS)
  409. {
  410. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiChannel()");
  411. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  412. const int32_t index = argv[0]->i;
  413. const int32_t channel = argv[1]->i;
  414. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  415. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  416. plugin->setParameterMidiChannel(static_cast<uint32_t>(index), static_cast<uint8_t>(channel), false, true);
  417. return 0;
  418. }
  419. int CarlaEngineOsc::handleMsgSetProgram(CARLA_ENGINE_OSC_HANDLE_ARGS)
  420. {
  421. carla_debug("CarlaEngineOsc::handleMsgSetProgram()");
  422. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  423. const int32_t index = argv[0]->i;
  424. CARLA_SAFE_ASSERT_RETURN(index >= -1, 0);
  425. plugin->setProgram(index, true, false, true);
  426. return 0;
  427. }
  428. int CarlaEngineOsc::handleMsgSetMidiProgram(CARLA_ENGINE_OSC_HANDLE_ARGS)
  429. {
  430. carla_debug("CarlaEngineOsc::handleMsgSetMidiProgram()");
  431. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  432. const int32_t index = argv[0]->i;
  433. CARLA_SAFE_ASSERT_RETURN(index >= -1, 0);
  434. plugin->setMidiProgram(index, true, false, true);
  435. return 0;
  436. }
  437. int CarlaEngineOsc::handleMsgNoteOn(CARLA_ENGINE_OSC_HANDLE_ARGS)
  438. {
  439. carla_debug("CarlaEngineOsc::handleMsgNoteOn()");
  440. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iii");
  441. const int32_t channel = argv[0]->i;
  442. const int32_t note = argv[1]->i;
  443. const int32_t velo = argv[2]->i;
  444. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  445. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE, 0);
  446. CARLA_SAFE_ASSERT_RETURN(velo >= 0 && velo < MAX_MIDI_VALUE, 0);
  447. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), static_cast<uint8_t>(velo), true, false, true);
  448. return 0;
  449. }
  450. int CarlaEngineOsc::handleMsgNoteOff(CARLA_ENGINE_OSC_HANDLE_ARGS)
  451. {
  452. carla_debug("CarlaEngineOsc::handleMsgNoteOff()");
  453. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  454. const int32_t channel = argv[0]->i;
  455. const int32_t note = argv[1]->i;
  456. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  457. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE, 0);
  458. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), 0, true, false, true);
  459. return 0;
  460. }
  461. // -----------------------------------------------------------------------
  462. CARLA_BACKEND_END_NAMESPACE
  463. #endif // HAVE_LIBLO