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.

447 lines
15KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2019 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 "CarlaEngineOsc.hpp"
  18. #ifdef HAVE_LIBLO
  19. #include "CarlaEngine.hpp"
  20. #include "CarlaPlugin.hpp"
  21. #include "CarlaMIDI.h"
  22. #include <cctype>
  23. CARLA_BACKEND_START_NAMESPACE
  24. // -----------------------------------------------------------------------
  25. 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)
  26. {
  27. CARLA_SAFE_ASSERT_RETURN(fName.isNotEmpty(), 1);
  28. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', 1);
  29. #ifdef DEBUG
  30. if (std::strstr(path, "/bridge_pong") == nullptr) {
  31. carla_debug("CarlaEngineOsc::handleMessage(%s, \"%s\", %i, %p, \"%s\", %p)", bool2str(isTCP), path, argc, argv, types, msg);
  32. }
  33. #endif
  34. if (isTCP)
  35. {
  36. CARLA_SAFE_ASSERT_RETURN(fServerPathTCP.isNotEmpty(), 1);
  37. CARLA_SAFE_ASSERT_RETURN(fServerTCP != nullptr, 1);
  38. }
  39. else
  40. {
  41. CARLA_SAFE_ASSERT_RETURN(fServerPathUDP.isNotEmpty(), 1);
  42. CARLA_SAFE_ASSERT_RETURN(fServerUDP != nullptr, 1);
  43. }
  44. // Initial path check
  45. if (std::strcmp(path, "/register") == 0)
  46. return handleMsgRegister(isTCP, argc, argv, types);
  47. if (std::strcmp(path, "/unregister") == 0)
  48. return handleMsgUnregister(isTCP, argc, argv, types);
  49. const std::size_t nameSize(fName.length());
  50. // Check if message is for this client
  51. if (std::strlen(path) <= nameSize || std::strncmp(path+1, fName, nameSize) != 0)
  52. {
  53. carla_stderr("CarlaEngineOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, fName.buffer());
  54. return 1;
  55. }
  56. // Get plugin id from path, "/carla/23/method" -> 23
  57. uint pluginId = 0;
  58. std::size_t offset;
  59. if (std::isdigit(path[nameSize+2]))
  60. {
  61. if (std::isdigit(path[nameSize+3]))
  62. {
  63. if (std::isdigit(path[nameSize+5]))
  64. {
  65. carla_stderr2("CarlaEngineOsc::handleMessage() - invalid plugin id, over 999? (value: \"%s\")", path+(nameSize+1));
  66. return 1;
  67. }
  68. else if (std::isdigit(path[nameSize+4]))
  69. {
  70. // 3 digits, /xyz/method
  71. offset = 6;
  72. pluginId += uint(path[nameSize+2]-'0')*100;
  73. pluginId += uint(path[nameSize+3]-'0')*10;
  74. pluginId += uint(path[nameSize+4]-'0');
  75. }
  76. else
  77. {
  78. // 2 digits, /xy/method
  79. offset = 5;
  80. pluginId += uint(path[nameSize+2]-'0')*10;
  81. pluginId += uint(path[nameSize+3]-'0');
  82. }
  83. }
  84. else
  85. {
  86. // single digit, /x/method
  87. offset = 4;
  88. pluginId += uint(path[nameSize+2]-'0');
  89. }
  90. }
  91. else
  92. {
  93. carla_stderr("CarlaEngineOsc::handleMessage() - invalid message '%s'", path);
  94. return 1;
  95. }
  96. if (pluginId > fEngine->getCurrentPluginCount())
  97. {
  98. carla_stderr("CarlaEngineOsc::handleMessage() - failed to get plugin, wrong id '%i'", pluginId);
  99. return 0;
  100. }
  101. // Get plugin
  102. CarlaPlugin* const plugin(fEngine->getPluginUnchecked(pluginId));
  103. if (plugin == nullptr || plugin->getId() != pluginId)
  104. {
  105. carla_stderr("CarlaEngineOsc::handleMessage() - invalid plugin id '%i', probably has been removed (path: '%s')", pluginId, path);
  106. return 0;
  107. }
  108. // Get method from path, "/Carla/i/method" -> "method"
  109. char method[32+1];
  110. method[32] = '\0';
  111. std::strncpy(method, path + (nameSize + offset), 32);
  112. if (method[0] == '\0')
  113. {
  114. carla_stderr("CarlaEngineOsc::handleMessage(%s, \"%s\", ...) - received message without method", bool2str(isTCP), path);
  115. return 0;
  116. }
  117. // Internal methods
  118. if (std::strcmp(method, "set_option") == 0)
  119. return 0; //handleMsgSetOption(plugin, argc, argv, types); // TODO
  120. if (std::strcmp(method, "set_active") == 0)
  121. return handleMsgSetActive(plugin, argc, argv, types);
  122. if (std::strcmp(method, "set_drywet") == 0)
  123. return handleMsgSetDryWet(plugin, argc, argv, types);
  124. if (std::strcmp(method, "set_volume") == 0)
  125. return handleMsgSetVolume(plugin, argc, argv, types);
  126. if (std::strcmp(method, "set_balance_left") == 0)
  127. return handleMsgSetBalanceLeft(plugin, argc, argv, types);
  128. if (std::strcmp(method, "set_balance_right") == 0)
  129. return handleMsgSetBalanceRight(plugin, argc, argv, types);
  130. if (std::strcmp(method, "set_panning") == 0)
  131. return handleMsgSetPanning(plugin, argc, argv, types);
  132. if (std::strcmp(method, "set_ctrl_channel") == 0)
  133. return 0; //handleMsgSetControlChannel(plugin, argc, argv, types); // TODO
  134. if (std::strcmp(method, "set_parameter_value") == 0)
  135. return handleMsgSetParameterValue(plugin, argc, argv, types);
  136. if (std::strcmp(method, "set_parameter_midi_cc") == 0)
  137. return handleMsgSetParameterMidiCC(plugin, argc, argv, types);
  138. if (std::strcmp(method, "set_parameter_midi_channel") == 0)
  139. return handleMsgSetParameterMidiChannel(plugin, argc, argv, types);
  140. if (std::strcmp(method, "set_program") == 0)
  141. return handleMsgSetProgram(plugin, argc, argv, types);
  142. if (std::strcmp(method, "set_midi_program") == 0)
  143. return handleMsgSetMidiProgram(plugin, argc, argv, types);
  144. if (std::strcmp(method, "set_custom_data") == 0)
  145. return 0; //handleMsgSetCustomData(plugin, argc, argv, types); // TODO
  146. if (std::strcmp(method, "set_chunk") == 0)
  147. return 0; //handleMsgSetChunk(plugin, argc, argv, types); // TODO
  148. if (std::strcmp(method, "note_on") == 0)
  149. return handleMsgNoteOn(plugin, argc, argv, types);
  150. if (std::strcmp(method, "note_off") == 0)
  151. return handleMsgNoteOff(plugin, argc, argv, types);
  152. // Send all other methods to plugins, TODO
  153. plugin->handleOscMessage(method, argc, argv, types, msg);
  154. return 0;
  155. }
  156. // -----------------------------------------------------------------------
  157. int CarlaEngineOsc::handleMsgRegister(const bool isTCP,
  158. const int argc, const lo_arg* const* const argv, const char* const types)
  159. {
  160. carla_debug("CarlaEngineOsc::handleMsgRegister()");
  161. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "s");
  162. const char* const url = &argv[0]->s;
  163. const lo_address addr = lo_address_new_from_url(url);
  164. CarlaOscData& oscData(isTCP ? fControlDataTCP : fControlDataUDP);
  165. if (oscData.owner != nullptr)
  166. {
  167. carla_stderr("OSC backend already registered to %s", oscData.owner);
  168. char* const path = lo_url_get_path(url);
  169. char targetPath[std::strlen(path)+18];
  170. std::strcpy(targetPath, path);
  171. std::strcat(targetPath, "/exit-error");
  172. lo_send_from(addr, isTCP ? fServerTCP : fServerUDP, LO_TT_IMMEDIATE,
  173. targetPath, "s", "OSC already registered to another client");
  174. free(path);
  175. }
  176. else
  177. {
  178. carla_stdout("OSC backend registered to %s", url);
  179. const char* const host = lo_address_get_hostname(addr);
  180. const char* const port = lo_address_get_port(addr);
  181. const lo_address target = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  182. oscData.owner = carla_strdup_safe(url);
  183. oscData.path = carla_strdup_free(lo_url_get_path(url));
  184. oscData.source = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  185. oscData.target = target;
  186. for (uint i=0, count=fEngine->getCurrentPluginCount(); i < count; ++i)
  187. {
  188. CarlaPlugin* const plugin(fEngine->getPluginUnchecked(i));
  189. CARLA_SAFE_ASSERT_CONTINUE(plugin != nullptr);
  190. fEngine->callback(false, true, ENGINE_CALLBACK_PLUGIN_ADDED, i, 0, 0, 0, 0.0f, plugin->getName());
  191. }
  192. const EngineOptions& opts(fEngine->getOptions());
  193. fEngine->callback(false, true,
  194. ENGINE_CALLBACK_ENGINE_STARTED, 0,
  195. opts.processMode,
  196. opts.transportMode,
  197. static_cast<int>(fEngine->getBufferSize()),
  198. static_cast<float>(fEngine->getSampleRate()),
  199. fEngine->getCurrentDriverName());
  200. // TODO
  201. // fEngine->patchbayRefresh();
  202. }
  203. lo_address_free(addr);
  204. return 0;
  205. }
  206. int CarlaEngineOsc::handleMsgUnregister(const bool isTCP,
  207. const int argc, const lo_arg* const* const argv, const char* const types)
  208. {
  209. carla_debug("CarlaEngineOsc::handleMsgUnregister()");
  210. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "s");
  211. CarlaOscData& oscData(isTCP ? fControlDataTCP : fControlDataUDP);
  212. if (oscData.owner == nullptr)
  213. {
  214. carla_stderr("OSC backend is not registered yet, unregister failed");
  215. return 0;
  216. }
  217. const char* const url = &argv[0]->s;
  218. if (std::strcmp(oscData.owner, url) == 0)
  219. {
  220. carla_stdout("OSC client %s unregistered", url);
  221. oscData.clear();
  222. return 0;
  223. }
  224. carla_stderr("OSC backend unregister failed, current owner %s does not match requested %s", oscData.owner, url);
  225. return 0;
  226. }
  227. // -----------------------------------------------------------------------
  228. int CarlaEngineOsc::handleMsgSetActive(CARLA_ENGINE_OSC_HANDLE_ARGS)
  229. {
  230. carla_debug("CarlaEngineOsc::handleMsgSetActive()");
  231. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  232. const bool active = (argv[0]->i != 0);
  233. plugin->setActive(active, false, true);
  234. return 0;
  235. }
  236. int CarlaEngineOsc::handleMsgSetDryWet(CARLA_ENGINE_OSC_HANDLE_ARGS)
  237. {
  238. carla_debug("CarlaEngineOsc::handleMsgSetDryWet()");
  239. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  240. const float value = argv[0]->f;
  241. plugin->setDryWet(value, false, true);
  242. return 0;
  243. }
  244. int CarlaEngineOsc::handleMsgSetVolume(CARLA_ENGINE_OSC_HANDLE_ARGS)
  245. {
  246. carla_debug("CarlaEngineOsc::handleMsgSetVolume()");
  247. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  248. const float value = argv[0]->f;
  249. plugin->setVolume(value, false, true);
  250. return 0;
  251. }
  252. int CarlaEngineOsc::handleMsgSetBalanceLeft(CARLA_ENGINE_OSC_HANDLE_ARGS)
  253. {
  254. carla_debug("CarlaEngineOsc::handleMsgSetBalanceLeft()");
  255. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  256. const float value = argv[0]->f;
  257. plugin->setBalanceLeft(value, false, true);
  258. return 0;
  259. }
  260. int CarlaEngineOsc::handleMsgSetBalanceRight(CARLA_ENGINE_OSC_HANDLE_ARGS)
  261. {
  262. carla_debug("CarlaEngineOsc::handleMsgSetBalanceRight()");
  263. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  264. const float value = argv[0]->f;
  265. plugin->setBalanceRight(value, false, true);
  266. return 0;
  267. }
  268. int CarlaEngineOsc::handleMsgSetPanning(CARLA_ENGINE_OSC_HANDLE_ARGS)
  269. {
  270. carla_debug("CarlaEngineOsc::handleMsgSetPanning()");
  271. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  272. const float value = argv[0]->f;
  273. plugin->setPanning(value, false, true);
  274. return 0;
  275. }
  276. int CarlaEngineOsc::handleMsgSetParameterValue(CARLA_ENGINE_OSC_HANDLE_ARGS)
  277. {
  278. carla_debug("CarlaEngineOsc::handleMsgSetParameterValue()");
  279. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "if");
  280. const int32_t index = argv[0]->i;
  281. const float value = argv[1]->f;
  282. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  283. plugin->setParameterValue(static_cast<uint32_t>(index), value, true, false, true);
  284. return 0;
  285. }
  286. int CarlaEngineOsc::handleMsgSetParameterMidiCC(CARLA_ENGINE_OSC_HANDLE_ARGS)
  287. {
  288. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiCC()");
  289. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  290. const int32_t index = argv[0]->i;
  291. const int32_t cc = argv[1]->i;
  292. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  293. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL, 0);
  294. plugin->setParameterMidiCC(static_cast<uint32_t>(index), static_cast<int16_t>(cc), false, true);
  295. return 0;
  296. }
  297. int CarlaEngineOsc::handleMsgSetParameterMidiChannel(CARLA_ENGINE_OSC_HANDLE_ARGS)
  298. {
  299. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiChannel()");
  300. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  301. const int32_t index = argv[0]->i;
  302. const int32_t channel = argv[1]->i;
  303. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  304. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  305. plugin->setParameterMidiChannel(static_cast<uint32_t>(index), static_cast<uint8_t>(channel), false, true);
  306. return 0;
  307. }
  308. int CarlaEngineOsc::handleMsgSetProgram(CARLA_ENGINE_OSC_HANDLE_ARGS)
  309. {
  310. carla_debug("CarlaEngineOsc::handleMsgSetProgram()");
  311. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  312. const int32_t index = argv[0]->i;
  313. CARLA_SAFE_ASSERT_RETURN(index >= -1, 0);
  314. plugin->setProgram(index, true, false, true);
  315. return 0;
  316. }
  317. int CarlaEngineOsc::handleMsgSetMidiProgram(CARLA_ENGINE_OSC_HANDLE_ARGS)
  318. {
  319. carla_debug("CarlaEngineOsc::handleMsgSetMidiProgram()");
  320. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  321. const int32_t index = argv[0]->i;
  322. CARLA_SAFE_ASSERT_RETURN(index >= -1, 0);
  323. plugin->setMidiProgram(index, true, false, true);
  324. return 0;
  325. }
  326. int CarlaEngineOsc::handleMsgNoteOn(CARLA_ENGINE_OSC_HANDLE_ARGS)
  327. {
  328. carla_debug("CarlaEngineOsc::handleMsgNoteOn()");
  329. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iii");
  330. const int32_t channel = argv[0]->i;
  331. const int32_t note = argv[1]->i;
  332. const int32_t velo = argv[2]->i;
  333. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  334. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE, 0);
  335. CARLA_SAFE_ASSERT_RETURN(velo >= 0 && velo < MAX_MIDI_VALUE, 0);
  336. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), static_cast<uint8_t>(velo), true, false, true);
  337. return 0;
  338. }
  339. int CarlaEngineOsc::handleMsgNoteOff(CARLA_ENGINE_OSC_HANDLE_ARGS)
  340. {
  341. carla_debug("CarlaEngineOsc::handleMsgNoteOff()");
  342. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  343. const int32_t channel = argv[0]->i;
  344. const int32_t note = argv[1]->i;
  345. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  346. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE, 0);
  347. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), 0, true, false, true);
  348. return 0;
  349. }
  350. // -----------------------------------------------------------------------
  351. CARLA_BACKEND_END_NAMESPACE
  352. #endif // HAVE_LIBLO