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.

558 lines
17KB

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