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.

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