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.

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