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.

557 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 "CarlaEngine.hpp"
  18. #include "CarlaEngineOsc.hpp"
  19. #include "CarlaPlugin.hpp"
  20. #ifndef BUILD_BRIDGE
  21. # include "CarlaBridgeUtils.hpp"
  22. #endif
  23. #include "CarlaMIDI.h"
  24. #include <cctype>
  25. CARLA_BACKEND_START_NAMESPACE
  26. #ifndef BUILD_BRIDGE
  27. // -------------------------------------------------------------------
  28. // Bridge Helper, defined in BridgePlugin.cpp
  29. extern int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeOscInfoType type,
  30. const int argc, const lo_arg* const* const argv, const char* const types);
  31. #endif
  32. // -----------------------------------------------------------------------
  33. CarlaEngineOsc::CarlaEngineOsc(CarlaEngine* const engine) noexcept
  34. : fEngine(engine),
  35. #ifndef BUILD_BRIDGE
  36. fControlData(),
  37. #endif
  38. fName(),
  39. fServerPathTCP(),
  40. fServerPathUDP(),
  41. fServerTCP(nullptr),
  42. fServerUDP(nullptr),
  43. leakDetector_CarlaEngineOsc()
  44. {
  45. CARLA_SAFE_ASSERT(engine != nullptr);
  46. carla_debug("CarlaEngineOsc::CarlaEngineOsc(%p)", engine);
  47. }
  48. CarlaEngineOsc::~CarlaEngineOsc() noexcept
  49. {
  50. CARLA_SAFE_ASSERT(fName.isEmpty());
  51. CARLA_SAFE_ASSERT(fServerPathTCP.isEmpty());
  52. CARLA_SAFE_ASSERT(fServerPathUDP.isEmpty());
  53. CARLA_SAFE_ASSERT(fServerTCP == nullptr);
  54. CARLA_SAFE_ASSERT(fServerUDP == nullptr);
  55. carla_debug("CarlaEngineOsc::~CarlaEngineOsc()");
  56. }
  57. // -----------------------------------------------------------------------
  58. void CarlaEngineOsc::init(const char* const name) noexcept
  59. {
  60. CARLA_SAFE_ASSERT_RETURN(fName.isEmpty(),);
  61. CARLA_SAFE_ASSERT_RETURN(fServerPathTCP.isEmpty(),);
  62. CARLA_SAFE_ASSERT_RETURN(fServerPathUDP.isEmpty(),);
  63. CARLA_SAFE_ASSERT_RETURN(fServerTCP == nullptr,);
  64. CARLA_SAFE_ASSERT_RETURN(fServerUDP == nullptr,);
  65. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  66. carla_debug("CarlaEngineOsc::init(\"%s\")", name);
  67. fName = name;
  68. fName.toBasic();
  69. fServerTCP = lo_server_new_with_proto(nullptr, LO_TCP, osc_error_handler_TCP);
  70. if (fServerTCP != nullptr)
  71. {
  72. if (char* const tmpServerPathTCP = lo_server_get_url(fServerTCP))
  73. {
  74. fServerPathTCP = tmpServerPathTCP;
  75. fServerPathTCP += fName;
  76. std::free(tmpServerPathTCP);
  77. }
  78. lo_server_add_method(fServerTCP, nullptr, nullptr, osc_message_handler_TCP, this);
  79. }
  80. fServerUDP = lo_server_new_with_proto(nullptr, LO_UDP, osc_error_handler_UDP);
  81. if (fServerUDP != nullptr)
  82. {
  83. if (char* const tmpServerPathUDP = lo_server_get_url(fServerUDP))
  84. {
  85. fServerPathUDP = tmpServerPathUDP;
  86. fServerPathUDP += fName;
  87. std::free(tmpServerPathUDP);
  88. }
  89. lo_server_add_method(fServerUDP, nullptr, nullptr, osc_message_handler_UDP, this);
  90. }
  91. CARLA_SAFE_ASSERT(fName.isNotEmpty());
  92. CARLA_SAFE_ASSERT(fServerPathTCP.isNotEmpty());
  93. CARLA_SAFE_ASSERT(fServerPathUDP.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_active") == 0)
  250. return handleMsgSetActive(plugin, argc, argv, types);
  251. if (std::strcmp(method, "set_drywet") == 0)
  252. return handleMsgSetDryWet(plugin, argc, argv, types);
  253. if (std::strcmp(method, "set_volume") == 0)
  254. return handleMsgSetVolume(plugin, argc, argv, types);
  255. if (std::strcmp(method, "set_balance_left") == 0)
  256. return handleMsgSetBalanceLeft(plugin, argc, argv, types);
  257. if (std::strcmp(method, "set_balance_right") == 0)
  258. return handleMsgSetBalanceRight(plugin, argc, argv, types);
  259. if (std::strcmp(method, "set_panning") == 0)
  260. return handleMsgSetPanning(plugin, argc, argv, types);
  261. if (std::strcmp(method, "set_parameter_value") == 0)
  262. return handleMsgSetParameterValue(plugin, argc, argv, types);
  263. if (std::strcmp(method, "set_parameter_midi_cc") == 0)
  264. return handleMsgSetParameterMidiCC(plugin, argc, argv, types);
  265. if (std::strcmp(method, "set_parameter_midi_channel") == 0)
  266. return handleMsgSetParameterMidiChannel(plugin, argc, argv, types);
  267. if (std::strcmp(method, "set_program") == 0)
  268. return handleMsgSetProgram(plugin, argc, argv, types);
  269. if (std::strcmp(method, "set_midi_program") == 0)
  270. return handleMsgSetMidiProgram(plugin, argc, argv, types);
  271. if (std::strcmp(method, "note_on") == 0)
  272. return handleMsgNoteOn(plugin, argc, argv, types);
  273. if (std::strcmp(method, "note_off") == 0)
  274. return handleMsgNoteOff(plugin, argc, argv, types);
  275. #endif
  276. // Send all other methods to plugins, TODO
  277. plugin->handleOscMessage(method, argc, argv, types, msg);
  278. return 0;
  279. }
  280. // -----------------------------------------------------------------------
  281. #ifndef BUILD_BRIDGE
  282. int CarlaEngineOsc::handleMsgRegister(const bool isTCP, const int argc, const lo_arg* const* const argv, const char* const types, const lo_address source)
  283. {
  284. carla_debug("CarlaEngineOsc::handleMsgRegister()");
  285. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "s");
  286. if (fControlData.path != nullptr)
  287. {
  288. carla_stderr("CarlaEngineOsc::handleMsgRegister() - OSC backend already registered to %s", fControlData.path);
  289. return 1;
  290. }
  291. const char* const url = &argv[0]->s;
  292. carla_debug("CarlaEngineOsc::handleMsgRegister() - OSC backend registered to %s", url);
  293. {
  294. const char* host = lo_address_get_hostname(source);
  295. const char* port = lo_address_get_port(source);
  296. fControlData.source = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  297. }
  298. {
  299. char* host = lo_url_get_hostname(url);
  300. char* port = lo_url_get_port(url);
  301. fControlData.path = carla_strdup_free(lo_url_get_path(url));
  302. fControlData.target = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  303. std::free(host);
  304. std::free(port);
  305. }
  306. for (uint i=0, count=fEngine->getCurrentPluginCount(); i < count; ++i)
  307. {
  308. CarlaPlugin* const plugin(fEngine->getPluginUnchecked(i));
  309. if (plugin != nullptr && plugin->isEnabled())
  310. plugin->registerToOscClient();
  311. }
  312. return 0;
  313. }
  314. int CarlaEngineOsc::handleMsgUnregister()
  315. {
  316. carla_debug("CarlaEngineOsc::handleMsgUnregister()");
  317. if (fControlData.path == nullptr)
  318. {
  319. carla_stderr("CarlaEngineOsc::handleMsgUnregister() - OSC backend is not registered yet");
  320. return 1;
  321. }
  322. fControlData.clear();
  323. return 0;
  324. }
  325. // -----------------------------------------------------------------------
  326. int CarlaEngineOsc::handleMsgSetActive(CARLA_ENGINE_OSC_HANDLE_ARGS)
  327. {
  328. carla_debug("CarlaEngineOsc::handleMsgSetActive()");
  329. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  330. const bool active = (argv[0]->i != 0);
  331. plugin->setActive(active, false, true);
  332. return 0;
  333. }
  334. int CarlaEngineOsc::handleMsgSetDryWet(CARLA_ENGINE_OSC_HANDLE_ARGS)
  335. {
  336. carla_debug("CarlaEngineOsc::handleMsgSetDryWet()");
  337. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  338. const float value = argv[0]->f;
  339. plugin->setDryWet(value, false, true);
  340. return 0;
  341. }
  342. int CarlaEngineOsc::handleMsgSetVolume(CARLA_ENGINE_OSC_HANDLE_ARGS)
  343. {
  344. carla_debug("CarlaEngineOsc::handleMsgSetVolume()");
  345. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  346. const float value = argv[0]->f;
  347. plugin->setVolume(value, false, true);
  348. return 0;
  349. }
  350. int CarlaEngineOsc::handleMsgSetBalanceLeft(CARLA_ENGINE_OSC_HANDLE_ARGS)
  351. {
  352. carla_debug("CarlaEngineOsc::handleMsgSetBalanceLeft()");
  353. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  354. const float value = argv[0]->f;
  355. plugin->setBalanceLeft(value, false, true);
  356. return 0;
  357. }
  358. int CarlaEngineOsc::handleMsgSetBalanceRight(CARLA_ENGINE_OSC_HANDLE_ARGS)
  359. {
  360. carla_debug("CarlaEngineOsc::handleMsgSetBalanceRight()");
  361. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  362. const float value = argv[0]->f;
  363. plugin->setBalanceRight(value, false, true);
  364. return 0;
  365. }
  366. int CarlaEngineOsc::handleMsgSetPanning(CARLA_ENGINE_OSC_HANDLE_ARGS)
  367. {
  368. carla_debug("CarlaEngineOsc::handleMsgSetPanning()");
  369. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  370. const float value = argv[0]->f;
  371. plugin->setPanning(value, false, true);
  372. return 0;
  373. }
  374. int CarlaEngineOsc::handleMsgSetParameterValue(CARLA_ENGINE_OSC_HANDLE_ARGS)
  375. {
  376. carla_debug("CarlaEngineOsc::handleMsgSetParameterValue()");
  377. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "if");
  378. const int32_t index = argv[0]->i;
  379. const float value = argv[1]->f;
  380. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  381. plugin->setParameterValue(static_cast<uint32_t>(index), value, true, false, true);
  382. return 0;
  383. }
  384. int CarlaEngineOsc::handleMsgSetParameterMidiCC(CARLA_ENGINE_OSC_HANDLE_ARGS)
  385. {
  386. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiCC()");
  387. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  388. const int32_t index = argv[0]->i;
  389. const int32_t cc = argv[1]->i;
  390. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  391. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL, 0);
  392. plugin->setParameterMidiCC(static_cast<uint32_t>(index), static_cast<int16_t>(cc), false, true);
  393. return 0;
  394. }
  395. int CarlaEngineOsc::handleMsgSetParameterMidiChannel(CARLA_ENGINE_OSC_HANDLE_ARGS)
  396. {
  397. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiChannel()");
  398. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  399. const int32_t index = argv[0]->i;
  400. const int32_t channel = argv[1]->i;
  401. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  402. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  403. plugin->setParameterMidiChannel(static_cast<uint32_t>(index), static_cast<uint8_t>(channel), false, true);
  404. return 0;
  405. }
  406. int CarlaEngineOsc::handleMsgSetProgram(CARLA_ENGINE_OSC_HANDLE_ARGS)
  407. {
  408. carla_debug("CarlaEngineOsc::handleMsgSetProgram()");
  409. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  410. const int32_t index = argv[0]->i;
  411. CARLA_SAFE_ASSERT_RETURN(index >= -1, 0);
  412. plugin->setProgram(index, true, false, true);
  413. return 0;
  414. }
  415. int CarlaEngineOsc::handleMsgSetMidiProgram(CARLA_ENGINE_OSC_HANDLE_ARGS)
  416. {
  417. carla_debug("CarlaEngineOsc::handleMsgSetMidiProgram()");
  418. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  419. const int32_t index = argv[0]->i;
  420. CARLA_SAFE_ASSERT_RETURN(index >= -1, 0);
  421. plugin->setMidiProgram(index, true, false, true);
  422. return 0;
  423. }
  424. int CarlaEngineOsc::handleMsgNoteOn(CARLA_ENGINE_OSC_HANDLE_ARGS)
  425. {
  426. carla_debug("CarlaEngineOsc::handleMsgNoteOn()");
  427. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iii");
  428. const int32_t channel = argv[0]->i;
  429. const int32_t note = argv[1]->i;
  430. const int32_t velo = argv[2]->i;
  431. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  432. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE, 0);
  433. CARLA_SAFE_ASSERT_RETURN(velo >= 0 && velo < MAX_MIDI_VALUE, 0);
  434. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), static_cast<uint8_t>(velo), true, false, true);
  435. return 0;
  436. }
  437. int CarlaEngineOsc::handleMsgNoteOff(CARLA_ENGINE_OSC_HANDLE_ARGS)
  438. {
  439. carla_debug("CarlaEngineOsc::handleMsgNoteOff()");
  440. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  441. const int32_t channel = argv[0]->i;
  442. const int32_t note = argv[1]->i;
  443. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  444. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE, 0);
  445. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), 0, true, false, true);
  446. return 0;
  447. }
  448. #endif // ! BUILD_BRIDGE
  449. // -----------------------------------------------------------------------
  450. CARLA_BACKEND_END_NAMESPACE