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.

549 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. // -----------------------------------------------------------------------
  27. CarlaEngineOsc::CarlaEngineOsc(CarlaEngine* const engine) noexcept
  28. : fEngine(engine),
  29. #ifndef BUILD_BRIDGE
  30. fControlData(),
  31. #endif
  32. fName(),
  33. fServerPathTCP(),
  34. fServerPathUDP(),
  35. fServerTCP(nullptr),
  36. fServerUDP(nullptr),
  37. leakDetector_CarlaEngineOsc()
  38. {
  39. CARLA_SAFE_ASSERT(engine != nullptr);
  40. carla_debug("CarlaEngineOsc::CarlaEngineOsc(%p)", engine);
  41. }
  42. CarlaEngineOsc::~CarlaEngineOsc() noexcept
  43. {
  44. CARLA_SAFE_ASSERT(fName.isEmpty());
  45. CARLA_SAFE_ASSERT(fServerPathTCP.isEmpty());
  46. CARLA_SAFE_ASSERT(fServerPathUDP.isEmpty());
  47. CARLA_SAFE_ASSERT(fServerTCP == nullptr);
  48. CARLA_SAFE_ASSERT(fServerUDP == nullptr);
  49. carla_debug("CarlaEngineOsc::~CarlaEngineOsc()");
  50. }
  51. // -----------------------------------------------------------------------
  52. void CarlaEngineOsc::init(const char* const name) noexcept
  53. {
  54. CARLA_SAFE_ASSERT_RETURN(fName.isEmpty(),);
  55. CARLA_SAFE_ASSERT_RETURN(fServerPathTCP.isEmpty(),);
  56. CARLA_SAFE_ASSERT_RETURN(fServerPathUDP.isEmpty(),);
  57. CARLA_SAFE_ASSERT_RETURN(fServerTCP == nullptr,);
  58. CARLA_SAFE_ASSERT_RETURN(fServerUDP == nullptr,);
  59. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  60. carla_debug("CarlaEngineOsc::init(\"%s\")", name);
  61. fName = name;
  62. fName.toBasic();
  63. fServerTCP = lo_server_new_with_proto(nullptr, LO_TCP, osc_error_handler_TCP);
  64. if (fServerTCP != nullptr)
  65. {
  66. if (char* const tmpServerPathTCP = lo_server_get_url(fServerTCP))
  67. {
  68. fServerPathTCP = tmpServerPathTCP;
  69. fServerPathTCP += fName;
  70. std::free(tmpServerPathTCP);
  71. }
  72. lo_server_add_method(fServerTCP, nullptr, nullptr, osc_message_handler_TCP, this);
  73. }
  74. fServerUDP = lo_server_new_with_proto(nullptr, LO_UDP, osc_error_handler_UDP);
  75. if (fServerUDP != nullptr)
  76. {
  77. if (char* const tmpServerPathUDP = lo_server_get_url(fServerUDP))
  78. {
  79. fServerPathUDP = tmpServerPathUDP;
  80. fServerPathUDP += fName;
  81. std::free(tmpServerPathUDP);
  82. }
  83. lo_server_add_method(fServerUDP, nullptr, nullptr, osc_message_handler_UDP, this);
  84. }
  85. CARLA_SAFE_ASSERT(fName.isNotEmpty());
  86. CARLA_SAFE_ASSERT(fServerPathTCP.isNotEmpty());
  87. CARLA_SAFE_ASSERT(fServerPathUDP.isNotEmpty());
  88. CARLA_SAFE_ASSERT(fServerTCP != nullptr);
  89. CARLA_SAFE_ASSERT(fServerUDP != nullptr);
  90. }
  91. void CarlaEngineOsc::idle() const noexcept
  92. {
  93. if (fServerTCP != nullptr)
  94. {
  95. for (;;)
  96. {
  97. try {
  98. if (lo_server_recv_noblock(fServerTCP, 0) == 0)
  99. break;
  100. } CARLA_SAFE_EXCEPTION_CONTINUE("OSC idle TCP")
  101. }
  102. }
  103. if (fServerUDP != nullptr)
  104. {
  105. for (;;)
  106. {
  107. try {
  108. if (lo_server_recv_noblock(fServerUDP, 0) == 0)
  109. break;
  110. } CARLA_SAFE_EXCEPTION_CONTINUE("OSC idle UDP")
  111. }
  112. }
  113. }
  114. void CarlaEngineOsc::close() noexcept
  115. {
  116. CARLA_SAFE_ASSERT(fName.isNotEmpty());
  117. CARLA_SAFE_ASSERT(fServerPathTCP.isNotEmpty());
  118. CARLA_SAFE_ASSERT(fServerPathUDP.isNotEmpty());
  119. CARLA_SAFE_ASSERT(fServerTCP != nullptr);
  120. CARLA_SAFE_ASSERT(fServerUDP != nullptr);
  121. carla_debug("CarlaEngineOsc::close()");
  122. fName.clear();
  123. if (fServerTCP != nullptr)
  124. {
  125. lo_server_del_method(fServerTCP, nullptr, nullptr);
  126. lo_server_free(fServerTCP);
  127. fServerTCP = nullptr;
  128. }
  129. if (fServerUDP != nullptr)
  130. {
  131. lo_server_del_method(fServerUDP, nullptr, nullptr);
  132. lo_server_free(fServerUDP);
  133. fServerUDP = nullptr;
  134. }
  135. fServerPathTCP.clear();
  136. fServerPathUDP.clear();
  137. #ifndef BUILD_BRIDGE
  138. fControlData.clear();
  139. #endif
  140. }
  141. // -----------------------------------------------------------------------
  142. 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)
  143. {
  144. CARLA_SAFE_ASSERT_RETURN(fName.isNotEmpty(), 1);
  145. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', 1);
  146. #ifdef DEBUG
  147. if (std::strstr(path, "/bridge_pong") == nullptr) {
  148. carla_debug("CarlaEngineOsc::handleMessage(%s, \"%s\", %i, %p, \"%s\", %p)", bool2str(isTCP), path, argc, argv, types, msg);
  149. }
  150. #endif
  151. //if (isTCP)
  152. {
  153. CARLA_SAFE_ASSERT_RETURN(fServerPathTCP.isNotEmpty(), 1);
  154. CARLA_SAFE_ASSERT_RETURN(fServerTCP != nullptr, 1);
  155. }
  156. //else
  157. {
  158. CARLA_SAFE_ASSERT_RETURN(fServerPathUDP.isNotEmpty(), 1);
  159. CARLA_SAFE_ASSERT_RETURN(fServerUDP != nullptr, 1);
  160. }
  161. #ifndef BUILD_BRIDGE
  162. // Initial path check
  163. if (std::strcmp(path, "/register") == 0)
  164. {
  165. const lo_address source(lo_message_get_source(msg));
  166. return handleMsgRegister(isTCP, argc, argv, types, source);
  167. }
  168. if (std::strcmp(path, "/unregister") == 0)
  169. {
  170. return handleMsgUnregister();
  171. }
  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_active") == 0)
  244. return handleMsgSetActive(plugin, argc, argv, types);
  245. if (std::strcmp(method, "set_drywet") == 0)
  246. return handleMsgSetDryWet(plugin, argc, argv, types);
  247. if (std::strcmp(method, "set_volume") == 0)
  248. return handleMsgSetVolume(plugin, argc, argv, types);
  249. if (std::strcmp(method, "set_balance_left") == 0)
  250. return handleMsgSetBalanceLeft(plugin, argc, argv, types);
  251. if (std::strcmp(method, "set_balance_right") == 0)
  252. return handleMsgSetBalanceRight(plugin, argc, argv, types);
  253. if (std::strcmp(method, "set_panning") == 0)
  254. return handleMsgSetPanning(plugin, argc, argv, types);
  255. if (std::strcmp(method, "set_parameter_value") == 0)
  256. return handleMsgSetParameterValue(plugin, argc, argv, types);
  257. if (std::strcmp(method, "set_parameter_midi_cc") == 0)
  258. return handleMsgSetParameterMidiCC(plugin, argc, argv, types);
  259. if (std::strcmp(method, "set_parameter_midi_channel") == 0)
  260. return handleMsgSetParameterMidiChannel(plugin, argc, argv, types);
  261. if (std::strcmp(method, "set_program") == 0)
  262. return handleMsgSetProgram(plugin, argc, argv, types);
  263. if (std::strcmp(method, "set_midi_program") == 0)
  264. return handleMsgSetMidiProgram(plugin, argc, argv, types);
  265. if (std::strcmp(method, "note_on") == 0)
  266. return handleMsgNoteOn(plugin, argc, argv, types);
  267. if (std::strcmp(method, "note_off") == 0)
  268. return handleMsgNoteOff(plugin, argc, argv, types);
  269. #endif
  270. // Send all other methods to plugins, TODO
  271. plugin->handleOscMessage(method, argc, argv, types, msg);
  272. return 0;
  273. }
  274. // -----------------------------------------------------------------------
  275. #ifndef BUILD_BRIDGE
  276. int CarlaEngineOsc::handleMsgRegister(const bool isTCP, const int argc, const lo_arg* const* const argv, const char* const types, const lo_address source)
  277. {
  278. carla_debug("CarlaEngineOsc::handleMsgRegister()");
  279. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "s");
  280. if (fControlData.path != nullptr)
  281. {
  282. carla_stderr("CarlaEngineOsc::handleMsgRegister() - OSC backend already registered to %s", fControlData.path);
  283. return 1;
  284. }
  285. const char* const url = &argv[0]->s;
  286. carla_debug("CarlaEngineOsc::handleMsgRegister() - OSC backend registered to %s", url);
  287. {
  288. const char* host = lo_address_get_hostname(source);
  289. const char* port = lo_address_get_port(source);
  290. fControlData.source = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  291. }
  292. {
  293. char* host = lo_url_get_hostname(url);
  294. char* port = lo_url_get_port(url);
  295. fControlData.path = carla_strdup_free(lo_url_get_path(url));
  296. fControlData.target = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  297. std::free(host);
  298. std::free(port);
  299. }
  300. for (uint i=0, count=fEngine->getCurrentPluginCount(); i < count; ++i)
  301. {
  302. CarlaPlugin* const plugin(fEngine->getPluginUnchecked(i));
  303. if (plugin != nullptr && plugin->isEnabled())
  304. plugin->registerToOscClient();
  305. }
  306. return 0;
  307. }
  308. int CarlaEngineOsc::handleMsgUnregister()
  309. {
  310. carla_debug("CarlaEngineOsc::handleMsgUnregister()");
  311. if (fControlData.path == nullptr)
  312. {
  313. carla_stderr("CarlaEngineOsc::handleMsgUnregister() - OSC backend is not registered yet");
  314. return 1;
  315. }
  316. fControlData.clear();
  317. return 0;
  318. }
  319. // -----------------------------------------------------------------------
  320. int CarlaEngineOsc::handleMsgSetActive(CARLA_ENGINE_OSC_HANDLE_ARGS)
  321. {
  322. carla_debug("CarlaEngineOsc::handleMsgSetActive()");
  323. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  324. const bool active = (argv[0]->i != 0);
  325. plugin->setActive(active, false, true);
  326. return 0;
  327. }
  328. int CarlaEngineOsc::handleMsgSetDryWet(CARLA_ENGINE_OSC_HANDLE_ARGS)
  329. {
  330. carla_debug("CarlaEngineOsc::handleMsgSetDryWet()");
  331. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  332. const float value = argv[0]->f;
  333. plugin->setDryWet(value, false, true);
  334. return 0;
  335. }
  336. int CarlaEngineOsc::handleMsgSetVolume(CARLA_ENGINE_OSC_HANDLE_ARGS)
  337. {
  338. carla_debug("CarlaEngineOsc::handleMsgSetVolume()");
  339. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  340. const float value = argv[0]->f;
  341. plugin->setVolume(value, false, true);
  342. return 0;
  343. }
  344. int CarlaEngineOsc::handleMsgSetBalanceLeft(CARLA_ENGINE_OSC_HANDLE_ARGS)
  345. {
  346. carla_debug("CarlaEngineOsc::handleMsgSetBalanceLeft()");
  347. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  348. const float value = argv[0]->f;
  349. plugin->setBalanceLeft(value, false, true);
  350. return 0;
  351. }
  352. int CarlaEngineOsc::handleMsgSetBalanceRight(CARLA_ENGINE_OSC_HANDLE_ARGS)
  353. {
  354. carla_debug("CarlaEngineOsc::handleMsgSetBalanceRight()");
  355. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  356. const float value = argv[0]->f;
  357. plugin->setBalanceRight(value, false, true);
  358. return 0;
  359. }
  360. int CarlaEngineOsc::handleMsgSetPanning(CARLA_ENGINE_OSC_HANDLE_ARGS)
  361. {
  362. carla_debug("CarlaEngineOsc::handleMsgSetPanning()");
  363. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  364. const float value = argv[0]->f;
  365. plugin->setPanning(value, false, true);
  366. return 0;
  367. }
  368. int CarlaEngineOsc::handleMsgSetParameterValue(CARLA_ENGINE_OSC_HANDLE_ARGS)
  369. {
  370. carla_debug("CarlaEngineOsc::handleMsgSetParameterValue()");
  371. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "if");
  372. const int32_t index = argv[0]->i;
  373. const float value = argv[1]->f;
  374. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  375. plugin->setParameterValue(static_cast<uint32_t>(index), value, true, false, true);
  376. return 0;
  377. }
  378. int CarlaEngineOsc::handleMsgSetParameterMidiCC(CARLA_ENGINE_OSC_HANDLE_ARGS)
  379. {
  380. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiCC()");
  381. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  382. const int32_t index = argv[0]->i;
  383. const int32_t cc = argv[1]->i;
  384. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  385. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL, 0);
  386. plugin->setParameterMidiCC(static_cast<uint32_t>(index), static_cast<int16_t>(cc), false, true);
  387. return 0;
  388. }
  389. int CarlaEngineOsc::handleMsgSetParameterMidiChannel(CARLA_ENGINE_OSC_HANDLE_ARGS)
  390. {
  391. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiChannel()");
  392. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  393. const int32_t index = argv[0]->i;
  394. const int32_t channel = argv[1]->i;
  395. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  396. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  397. plugin->setParameterMidiChannel(static_cast<uint32_t>(index), static_cast<uint8_t>(channel), false, true);
  398. return 0;
  399. }
  400. int CarlaEngineOsc::handleMsgSetProgram(CARLA_ENGINE_OSC_HANDLE_ARGS)
  401. {
  402. carla_debug("CarlaEngineOsc::handleMsgSetProgram()");
  403. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  404. const int32_t index = argv[0]->i;
  405. CARLA_SAFE_ASSERT_RETURN(index >= -1, 0);
  406. plugin->setProgram(index, true, false, true);
  407. return 0;
  408. }
  409. int CarlaEngineOsc::handleMsgSetMidiProgram(CARLA_ENGINE_OSC_HANDLE_ARGS)
  410. {
  411. carla_debug("CarlaEngineOsc::handleMsgSetMidiProgram()");
  412. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  413. const int32_t index = argv[0]->i;
  414. CARLA_SAFE_ASSERT_RETURN(index >= -1, 0);
  415. plugin->setMidiProgram(index, true, false, true);
  416. return 0;
  417. }
  418. int CarlaEngineOsc::handleMsgNoteOn(CARLA_ENGINE_OSC_HANDLE_ARGS)
  419. {
  420. carla_debug("CarlaEngineOsc::handleMsgNoteOn()");
  421. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iii");
  422. const int32_t channel = argv[0]->i;
  423. const int32_t note = argv[1]->i;
  424. const int32_t velo = argv[2]->i;
  425. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  426. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE, 0);
  427. CARLA_SAFE_ASSERT_RETURN(velo >= 0 && velo < MAX_MIDI_VALUE, 0);
  428. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), static_cast<uint8_t>(velo), true, false, true);
  429. return 0;
  430. }
  431. int CarlaEngineOsc::handleMsgNoteOff(CARLA_ENGINE_OSC_HANDLE_ARGS)
  432. {
  433. carla_debug("CarlaEngineOsc::handleMsgNoteOff()");
  434. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  435. const int32_t channel = argv[0]->i;
  436. const int32_t note = argv[1]->i;
  437. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  438. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE, 0);
  439. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), 0, true, false, true);
  440. return 0;
  441. }
  442. #endif // ! BUILD_BRIDGE
  443. // -----------------------------------------------------------------------
  444. CARLA_BACKEND_END_NAMESPACE