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.

556 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. return handleMsgRegister(isTCP, argc, argv, types);
  170. if (std::strcmp(path, "/unregister") == 0)
  171. return handleMsgUnregister();
  172. #endif
  173. const std::size_t nameSize(fName.length());
  174. // Check if message is for this client
  175. if (std::strlen(path) <= nameSize || std::strncmp(path+1, fName, nameSize) != 0)
  176. {
  177. carla_stderr("CarlaEngineOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, fName.buffer());
  178. return 1;
  179. }
  180. // Get plugin id from path, "/carla/23/method" -> 23
  181. uint pluginId = 0;
  182. std::size_t offset;
  183. if (std::isdigit(path[nameSize+2]))
  184. {
  185. if (std::isdigit(path[nameSize+3]))
  186. {
  187. if (std::isdigit(path[nameSize+5]))
  188. {
  189. carla_stderr2("CarlaEngineOsc::handleMessage() - invalid plugin id, over 999? (value: \"%s\")", path+(nameSize+1));
  190. return 1;
  191. }
  192. else if (std::isdigit(path[nameSize+4]))
  193. {
  194. // 3 digits, /xyz/method
  195. offset = 6;
  196. pluginId += uint(path[nameSize+2]-'0')*100;
  197. pluginId += uint(path[nameSize+3]-'0')*10;
  198. pluginId += uint(path[nameSize+4]-'0');
  199. }
  200. else
  201. {
  202. // 2 digits, /xy/method
  203. offset = 5;
  204. pluginId += uint(path[nameSize+2]-'0')*10;
  205. pluginId += uint(path[nameSize+3]-'0');
  206. }
  207. }
  208. else
  209. {
  210. // single digit, /x/method
  211. offset = 4;
  212. pluginId += uint(path[nameSize+2]-'0');
  213. }
  214. }
  215. else
  216. {
  217. carla_stderr("CarlaEngineOsc::handleMessage() - invalid message '%s'", path);
  218. return 1;
  219. }
  220. if (pluginId > fEngine->getCurrentPluginCount())
  221. {
  222. carla_stderr("CarlaEngineOsc::handleMessage() - failed to get plugin, wrong id '%i'", pluginId);
  223. return 0;
  224. }
  225. // Get plugin
  226. CarlaPlugin* const plugin(fEngine->getPluginUnchecked(pluginId));
  227. if (plugin == nullptr || plugin->getId() != pluginId)
  228. {
  229. carla_stderr("CarlaEngineOsc::handleMessage() - invalid plugin id '%i', probably has been removed (path: '%s')", pluginId, path);
  230. return 0;
  231. }
  232. // Get method from path, "/Carla/i/method" -> "method"
  233. char method[32+1];
  234. method[32] = '\0';
  235. std::strncpy(method, path + (nameSize + offset), 32);
  236. if (method[0] == '\0')
  237. {
  238. carla_stderr("CarlaEngineOsc::handleMessage(%s, \"%s\", ...) - received message without method", bool2str(isTCP), path);
  239. return 0;
  240. }
  241. #ifndef BUILD_BRIDGE
  242. // Internal methods
  243. if (std::strcmp(method, "set_option") == 0)
  244. return 0; //handleMsgSetOption(plugin, argc, argv, types); // TODO
  245. if (std::strcmp(method, "set_active") == 0)
  246. return handleMsgSetActive(plugin, argc, argv, types);
  247. if (std::strcmp(method, "set_drywet") == 0)
  248. return handleMsgSetDryWet(plugin, argc, argv, types);
  249. if (std::strcmp(method, "set_volume") == 0)
  250. return handleMsgSetVolume(plugin, argc, argv, types);
  251. if (std::strcmp(method, "set_balance_left") == 0)
  252. return handleMsgSetBalanceLeft(plugin, argc, argv, types);
  253. if (std::strcmp(method, "set_balance_right") == 0)
  254. return handleMsgSetBalanceRight(plugin, argc, argv, types);
  255. if (std::strcmp(method, "set_panning") == 0)
  256. return handleMsgSetPanning(plugin, argc, argv, types);
  257. if (std::strcmp(method, "set_ctrl_channel") == 0)
  258. return 0; //handleMsgSetControlChannel(plugin, argc, argv, types); // TODO
  259. if (std::strcmp(method, "set_parameter_value") == 0)
  260. return handleMsgSetParameterValue(plugin, argc, argv, types);
  261. if (std::strcmp(method, "set_parameter_midi_cc") == 0)
  262. return handleMsgSetParameterMidiCC(plugin, argc, argv, types);
  263. if (std::strcmp(method, "set_parameter_midi_channel") == 0)
  264. return handleMsgSetParameterMidiChannel(plugin, argc, argv, types);
  265. if (std::strcmp(method, "set_program") == 0)
  266. return handleMsgSetProgram(plugin, argc, argv, types);
  267. if (std::strcmp(method, "set_midi_program") == 0)
  268. return handleMsgSetMidiProgram(plugin, argc, argv, types);
  269. if (std::strcmp(method, "set_custom_data") == 0)
  270. return 0; //handleMsgSetCustomData(plugin, argc, argv, types); // TODO
  271. if (std::strcmp(method, "set_chunk") == 0)
  272. return 0; //handleMsgSetChunk(plugin, argc, argv, types); // TODO
  273. if (std::strcmp(method, "note_on") == 0)
  274. return handleMsgNoteOn(plugin, argc, argv, types);
  275. if (std::strcmp(method, "note_off") == 0)
  276. return handleMsgNoteOff(plugin, argc, argv, types);
  277. #endif
  278. // Send all other methods to plugins, TODO
  279. plugin->handleOscMessage(method, argc, argv, types, msg);
  280. return 0;
  281. }
  282. // -----------------------------------------------------------------------
  283. #ifndef BUILD_BRIDGE
  284. int CarlaEngineOsc::handleMsgRegister(const bool isTCP, const int argc, const lo_arg* const* const argv, const char* const types)
  285. {
  286. carla_debug("CarlaEngineOsc::handleMsgRegister()");
  287. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "s");
  288. if (fControlData.path != nullptr)
  289. {
  290. carla_stderr("CarlaEngineOsc::handleMsgRegister() - OSC backend already registered to %s", fControlData.path);
  291. return 1;
  292. }
  293. const char* const url = &argv[0]->s;
  294. carla_debug("CarlaEngineOsc::handleMsgRegister() - OSC backend registered to %s", url);
  295. {
  296. const lo_address addr = lo_address_new_from_url(url);
  297. const char* const host = lo_address_get_hostname(addr);
  298. const char* const port = lo_address_get_port(addr);
  299. fControlData.source = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  300. fControlData.path = carla_strdup_free(lo_url_get_path(url));
  301. fControlData.target = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  302. }
  303. for (uint i=0, count=fEngine->getCurrentPluginCount(); i < count; ++i)
  304. {
  305. CarlaPlugin* const plugin(fEngine->getPluginUnchecked(i));
  306. if (plugin != nullptr && plugin->isEnabled())
  307. plugin->registerToOscClient();
  308. }
  309. return 0;
  310. }
  311. int CarlaEngineOsc::handleMsgUnregister()
  312. {
  313. carla_debug("CarlaEngineOsc::handleMsgUnregister()");
  314. if (fControlData.path == nullptr)
  315. {
  316. carla_stderr("CarlaEngineOsc::handleMsgUnregister() - OSC backend is not registered yet");
  317. return 1;
  318. }
  319. fControlData.clear();
  320. return 0;
  321. }
  322. // -----------------------------------------------------------------------
  323. int CarlaEngineOsc::handleMsgSetActive(CARLA_ENGINE_OSC_HANDLE_ARGS)
  324. {
  325. carla_debug("CarlaEngineOsc::handleMsgSetActive()");
  326. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  327. const bool active = (argv[0]->i != 0);
  328. plugin->setActive(active, false, true);
  329. return 0;
  330. }
  331. int CarlaEngineOsc::handleMsgSetDryWet(CARLA_ENGINE_OSC_HANDLE_ARGS)
  332. {
  333. carla_debug("CarlaEngineOsc::handleMsgSetDryWet()");
  334. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  335. const float value = argv[0]->f;
  336. plugin->setDryWet(value, false, true);
  337. return 0;
  338. }
  339. int CarlaEngineOsc::handleMsgSetVolume(CARLA_ENGINE_OSC_HANDLE_ARGS)
  340. {
  341. carla_debug("CarlaEngineOsc::handleMsgSetVolume()");
  342. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  343. const float value = argv[0]->f;
  344. plugin->setVolume(value, false, true);
  345. return 0;
  346. }
  347. int CarlaEngineOsc::handleMsgSetBalanceLeft(CARLA_ENGINE_OSC_HANDLE_ARGS)
  348. {
  349. carla_debug("CarlaEngineOsc::handleMsgSetBalanceLeft()");
  350. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  351. const float value = argv[0]->f;
  352. plugin->setBalanceLeft(value, false, true);
  353. return 0;
  354. }
  355. int CarlaEngineOsc::handleMsgSetBalanceRight(CARLA_ENGINE_OSC_HANDLE_ARGS)
  356. {
  357. carla_debug("CarlaEngineOsc::handleMsgSetBalanceRight()");
  358. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  359. const float value = argv[0]->f;
  360. plugin->setBalanceRight(value, false, true);
  361. return 0;
  362. }
  363. int CarlaEngineOsc::handleMsgSetPanning(CARLA_ENGINE_OSC_HANDLE_ARGS)
  364. {
  365. carla_debug("CarlaEngineOsc::handleMsgSetPanning()");
  366. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  367. const float value = argv[0]->f;
  368. plugin->setPanning(value, false, true);
  369. return 0;
  370. }
  371. int CarlaEngineOsc::handleMsgSetParameterValue(CARLA_ENGINE_OSC_HANDLE_ARGS)
  372. {
  373. carla_debug("CarlaEngineOsc::handleMsgSetParameterValue()");
  374. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "if");
  375. const int32_t index = argv[0]->i;
  376. const float value = argv[1]->f;
  377. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  378. plugin->setParameterValue(static_cast<uint32_t>(index), value, true, false, true);
  379. return 0;
  380. }
  381. int CarlaEngineOsc::handleMsgSetParameterMidiCC(CARLA_ENGINE_OSC_HANDLE_ARGS)
  382. {
  383. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiCC()");
  384. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  385. const int32_t index = argv[0]->i;
  386. const int32_t cc = argv[1]->i;
  387. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  388. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL, 0);
  389. plugin->setParameterMidiCC(static_cast<uint32_t>(index), static_cast<int16_t>(cc), false, true);
  390. return 0;
  391. }
  392. int CarlaEngineOsc::handleMsgSetParameterMidiChannel(CARLA_ENGINE_OSC_HANDLE_ARGS)
  393. {
  394. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiChannel()");
  395. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  396. const int32_t index = argv[0]->i;
  397. const int32_t channel = argv[1]->i;
  398. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  399. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  400. plugin->setParameterMidiChannel(static_cast<uint32_t>(index), static_cast<uint8_t>(channel), false, true);
  401. return 0;
  402. }
  403. int CarlaEngineOsc::handleMsgSetProgram(CARLA_ENGINE_OSC_HANDLE_ARGS)
  404. {
  405. carla_debug("CarlaEngineOsc::handleMsgSetProgram()");
  406. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  407. const int32_t index = argv[0]->i;
  408. CARLA_SAFE_ASSERT_RETURN(index >= -1, 0);
  409. plugin->setProgram(index, true, false, true);
  410. return 0;
  411. }
  412. int CarlaEngineOsc::handleMsgSetMidiProgram(CARLA_ENGINE_OSC_HANDLE_ARGS)
  413. {
  414. carla_debug("CarlaEngineOsc::handleMsgSetMidiProgram()");
  415. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  416. const int32_t index = argv[0]->i;
  417. CARLA_SAFE_ASSERT_RETURN(index >= -1, 0);
  418. plugin->setMidiProgram(index, true, false, true);
  419. return 0;
  420. }
  421. int CarlaEngineOsc::handleMsgNoteOn(CARLA_ENGINE_OSC_HANDLE_ARGS)
  422. {
  423. carla_debug("CarlaEngineOsc::handleMsgNoteOn()");
  424. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iii");
  425. const int32_t channel = argv[0]->i;
  426. const int32_t note = argv[1]->i;
  427. const int32_t velo = argv[2]->i;
  428. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  429. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE, 0);
  430. CARLA_SAFE_ASSERT_RETURN(velo >= 0 && velo < MAX_MIDI_VALUE, 0);
  431. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), static_cast<uint8_t>(velo), true, false, true);
  432. return 0;
  433. }
  434. int CarlaEngineOsc::handleMsgNoteOff(CARLA_ENGINE_OSC_HANDLE_ARGS)
  435. {
  436. carla_debug("CarlaEngineOsc::handleMsgNoteOff()");
  437. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  438. const int32_t channel = argv[0]->i;
  439. const int32_t note = argv[1]->i;
  440. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  441. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE, 0);
  442. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), 0, true, false, true);
  443. return 0;
  444. }
  445. #endif // ! BUILD_BRIDGE
  446. // -----------------------------------------------------------------------
  447. CARLA_BACKEND_END_NAMESPACE
  448. #endif // HAVE_LIBLO