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.

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