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.

559 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. {
  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. const char* tcpPort = nullptr;
  62. const char* udpPort = nullptr;
  63. #ifndef BUILD_BRIDGE
  64. if (fEngine->getType() != kEngineTypePlugin)
  65. {
  66. tcpPort = std::getenv("CARLA_OSC_TCP_PORT");
  67. udpPort = std::getenv("CARLA_OSC_UDP_PORT");
  68. }
  69. #endif
  70. fServerTCP = lo_server_new_with_proto(tcpPort, LO_TCP, osc_error_handler_TCP);
  71. if (fServerTCP != nullptr)
  72. {
  73. if (char* const tmpServerPathTCP = lo_server_get_url(fServerTCP))
  74. {
  75. fServerPathTCP = tmpServerPathTCP;
  76. fServerPathTCP += fName;
  77. std::free(tmpServerPathTCP);
  78. }
  79. lo_server_add_method(fServerTCP, nullptr, nullptr, osc_message_handler_TCP, this);
  80. }
  81. fServerUDP = lo_server_new_with_proto(udpPort, LO_UDP, osc_error_handler_UDP);
  82. if (fServerUDP != nullptr)
  83. {
  84. if (char* const tmpServerPathUDP = lo_server_get_url(fServerUDP))
  85. {
  86. fServerPathUDP = tmpServerPathUDP;
  87. fServerPathUDP += fName;
  88. std::free(tmpServerPathUDP);
  89. }
  90. lo_server_add_method(fServerUDP, nullptr, nullptr, osc_message_handler_UDP, this);
  91. }
  92. CARLA_SAFE_ASSERT(fName.isNotEmpty());
  93. CARLA_SAFE_ASSERT(fServerTCP != nullptr);
  94. CARLA_SAFE_ASSERT(fServerUDP != nullptr);
  95. }
  96. void CarlaEngineOsc::idle() const noexcept
  97. {
  98. if (fServerTCP != nullptr)
  99. {
  100. for (;;)
  101. {
  102. try {
  103. if (lo_server_recv_noblock(fServerTCP, 0) == 0)
  104. break;
  105. } CARLA_SAFE_EXCEPTION_CONTINUE("OSC idle TCP")
  106. }
  107. }
  108. if (fServerUDP != nullptr)
  109. {
  110. for (;;)
  111. {
  112. try {
  113. if (lo_server_recv_noblock(fServerUDP, 0) == 0)
  114. break;
  115. } CARLA_SAFE_EXCEPTION_CONTINUE("OSC idle UDP")
  116. }
  117. }
  118. }
  119. void CarlaEngineOsc::close() noexcept
  120. {
  121. CARLA_SAFE_ASSERT(fName.isNotEmpty());
  122. CARLA_SAFE_ASSERT(fServerPathTCP.isNotEmpty());
  123. CARLA_SAFE_ASSERT(fServerPathUDP.isNotEmpty());
  124. CARLA_SAFE_ASSERT(fServerTCP != nullptr);
  125. CARLA_SAFE_ASSERT(fServerUDP != nullptr);
  126. carla_debug("CarlaEngineOsc::close()");
  127. fName.clear();
  128. if (fServerTCP != nullptr)
  129. {
  130. lo_server_del_method(fServerTCP, nullptr, nullptr);
  131. lo_server_free(fServerTCP);
  132. fServerTCP = nullptr;
  133. }
  134. if (fServerUDP != nullptr)
  135. {
  136. lo_server_del_method(fServerUDP, nullptr, nullptr);
  137. lo_server_free(fServerUDP);
  138. fServerUDP = nullptr;
  139. }
  140. fServerPathTCP.clear();
  141. fServerPathUDP.clear();
  142. #ifndef BUILD_BRIDGE
  143. fControlData.clear();
  144. #endif
  145. }
  146. // -----------------------------------------------------------------------
  147. 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)
  148. {
  149. CARLA_SAFE_ASSERT_RETURN(fName.isNotEmpty(), 1);
  150. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', 1);
  151. #ifdef DEBUG
  152. if (std::strstr(path, "/bridge_pong") == nullptr) {
  153. carla_debug("CarlaEngineOsc::handleMessage(%s, \"%s\", %i, %p, \"%s\", %p)", bool2str(isTCP), path, argc, argv, types, msg);
  154. }
  155. #endif
  156. if (isTCP)
  157. {
  158. CARLA_SAFE_ASSERT_RETURN(fServerPathTCP.isNotEmpty(), 1);
  159. CARLA_SAFE_ASSERT_RETURN(fServerTCP != nullptr, 1);
  160. }
  161. else
  162. {
  163. CARLA_SAFE_ASSERT_RETURN(fServerPathUDP.isNotEmpty(), 1);
  164. CARLA_SAFE_ASSERT_RETURN(fServerUDP != nullptr, 1);
  165. }
  166. #ifndef BUILD_BRIDGE
  167. // Initial path check
  168. if (std::strcmp(path, "/register") == 0)
  169. {
  170. const lo_address source(lo_message_get_source(msg));
  171. return handleMsgRegister(isTCP, argc, argv, types, source);
  172. }
  173. if (std::strcmp(path, "/unregister") == 0)
  174. {
  175. return handleMsgUnregister();
  176. }
  177. #endif
  178. const std::size_t nameSize(fName.length());
  179. // Check if message is for this client
  180. if (std::strlen(path) <= nameSize || std::strncmp(path+1, fName, nameSize) != 0)
  181. {
  182. carla_stderr("CarlaEngineOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, fName.buffer());
  183. return 1;
  184. }
  185. // Get plugin id from path, "/carla/23/method" -> 23
  186. uint pluginId = 0;
  187. std::size_t offset;
  188. if (std::isdigit(path[nameSize+2]))
  189. {
  190. if (std::isdigit(path[nameSize+3]))
  191. {
  192. if (std::isdigit(path[nameSize+5]))
  193. {
  194. carla_stderr2("CarlaEngineOsc::handleMessage() - invalid plugin id, over 999? (value: \"%s\")", path+(nameSize+1));
  195. return 1;
  196. }
  197. else if (std::isdigit(path[nameSize+4]))
  198. {
  199. // 3 digits, /xyz/method
  200. offset = 6;
  201. pluginId += uint(path[nameSize+2]-'0')*100;
  202. pluginId += uint(path[nameSize+3]-'0')*10;
  203. pluginId += uint(path[nameSize+4]-'0');
  204. }
  205. else
  206. {
  207. // 2 digits, /xy/method
  208. offset = 5;
  209. pluginId += uint(path[nameSize+2]-'0')*10;
  210. pluginId += uint(path[nameSize+3]-'0');
  211. }
  212. }
  213. else
  214. {
  215. // single digit, /x/method
  216. offset = 4;
  217. pluginId += uint(path[nameSize+2]-'0');
  218. }
  219. }
  220. else
  221. {
  222. carla_stderr("CarlaEngineOsc::handleMessage() - invalid message '%s'", path);
  223. return 1;
  224. }
  225. if (pluginId > fEngine->getCurrentPluginCount())
  226. {
  227. carla_stderr("CarlaEngineOsc::handleMessage() - failed to get plugin, wrong id '%i'", pluginId);
  228. return 0;
  229. }
  230. // Get plugin
  231. CarlaPlugin* const plugin(fEngine->getPluginUnchecked(pluginId));
  232. if (plugin == nullptr || plugin->getId() != pluginId)
  233. {
  234. carla_stderr("CarlaEngineOsc::handleMessage() - invalid plugin id '%i', probably has been removed (path: '%s')", pluginId, path);
  235. return 0;
  236. }
  237. // Get method from path, "/Carla/i/method" -> "method"
  238. char method[32+1];
  239. method[32] = '\0';
  240. std::strncpy(method, path + (nameSize + offset), 32);
  241. if (method[0] == '\0')
  242. {
  243. carla_stderr("CarlaEngineOsc::handleMessage(%s, \"%s\", ...) - received message without method", bool2str(isTCP), path);
  244. return 0;
  245. }
  246. #ifndef BUILD_BRIDGE
  247. // Internal methods
  248. if (std::strcmp(method, "set_option") == 0)
  249. return 0; //handleMsgSetOption(plugin, argc, argv, types); // TODO
  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_ctrl_channel") == 0)
  263. return 0; //handleMsgSetControlChannel(plugin, argc, argv, types); // TODO
  264. if (std::strcmp(method, "set_parameter_value") == 0)
  265. return handleMsgSetParameterValue(plugin, argc, argv, types);
  266. if (std::strcmp(method, "set_parameter_midi_cc") == 0)
  267. return handleMsgSetParameterMidiCC(plugin, argc, argv, types);
  268. if (std::strcmp(method, "set_parameter_midi_channel") == 0)
  269. return handleMsgSetParameterMidiChannel(plugin, argc, argv, types);
  270. if (std::strcmp(method, "set_program") == 0)
  271. return handleMsgSetProgram(plugin, argc, argv, types);
  272. if (std::strcmp(method, "set_midi_program") == 0)
  273. return handleMsgSetMidiProgram(plugin, argc, argv, types);
  274. if (std::strcmp(method, "set_custom_data") == 0)
  275. return 0; //handleMsgSetCustomData(plugin, argc, argv, types); // TODO
  276. if (std::strcmp(method, "set_chunk") == 0)
  277. return 0; //handleMsgSetChunk(plugin, argc, argv, types); // TODO
  278. if (std::strcmp(method, "note_on") == 0)
  279. return handleMsgNoteOn(plugin, argc, argv, types);
  280. if (std::strcmp(method, "note_off") == 0)
  281. return handleMsgNoteOff(plugin, argc, argv, types);
  282. #endif
  283. // Send all other methods to plugins, TODO
  284. plugin->handleOscMessage(method, argc, argv, types, msg);
  285. return 0;
  286. }
  287. // -----------------------------------------------------------------------
  288. #ifndef BUILD_BRIDGE
  289. int CarlaEngineOsc::handleMsgRegister(const bool isTCP, const int argc, const lo_arg* const* const argv, const char* const types, const lo_address source)
  290. {
  291. carla_debug("CarlaEngineOsc::handleMsgRegister()");
  292. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "s");
  293. if (fControlData.path != nullptr)
  294. {
  295. carla_stderr("CarlaEngineOsc::handleMsgRegister() - OSC backend already registered to %s", fControlData.path);
  296. return 1;
  297. }
  298. const char* const url = &argv[0]->s;
  299. carla_debug("CarlaEngineOsc::handleMsgRegister() - OSC backend registered to %s", url);
  300. {
  301. const char* host = lo_address_get_hostname(source);
  302. const char* port = lo_address_get_port(source);
  303. fControlData.source = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  304. fControlData.path = carla_strdup_free(lo_url_get_path(url));
  305. fControlData.target = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, 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