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.

405 lines
11KB

  1. /*
  2. * Carla Bridge OSC
  3. * Copyright (C) 2011-2013 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 GPL.txt file
  16. */
  17. #include "CarlaBridgeOsc.hpp"
  18. #include "CarlaBridgeClient.hpp"
  19. #include "CarlaMIDI.h"
  20. #include "CarlaUtils.hpp"
  21. CARLA_BRIDGE_START_NAMESPACE
  22. // -----------------------------------------------------------------------
  23. CarlaBridgeOsc::CarlaBridgeOsc(CarlaBridgeClient* const client)
  24. : kClient(client),
  25. fServer(nullptr)
  26. {
  27. carla_debug("CarlaBridgeOsc::CarlaBridgeOsc(%p)", client);
  28. CARLA_ASSERT(client != nullptr);
  29. }
  30. CarlaBridgeOsc::~CarlaBridgeOsc()
  31. {
  32. carla_debug("CarlaBridgeOsc::~CarlaBridgeOsc()");
  33. CARLA_ASSERT(fName.isEmpty());
  34. CARLA_ASSERT(fServerPath.isEmpty());
  35. CARLA_ASSERT(fServer == nullptr);
  36. }
  37. void CarlaBridgeOsc::init(const char* const url)
  38. {
  39. carla_debug("CarlaBridgeOsc::init(\"%s\")", url);
  40. CARLA_ASSERT(fName.isEmpty());
  41. CARLA_ASSERT(fServerPath.isEmpty());
  42. CARLA_ASSERT(fServer == nullptr);
  43. CARLA_ASSERT(url != nullptr);
  44. #ifdef BUILD_BRIDGE_PLUGIN
  45. fName = "carla-bridge-plugin";
  46. #else
  47. fName = "carla-bridge-ui";
  48. #endif
  49. {
  50. char* const host = lo_url_get_hostname(url);
  51. char* const port = lo_url_get_port(url);
  52. fControlData.path = carla_strdup_free(lo_url_get_path(url));
  53. fControlData.target = lo_address_new_with_proto(LO_TCP, host, port);
  54. std::free(host);
  55. std::free(port);
  56. }
  57. fServer = lo_server_new_with_proto(nullptr, LO_TCP, osc_error_handler);
  58. if (fServer != nullptr)
  59. {
  60. if (char* const tmpServerPath = lo_server_get_url(fServer))
  61. {
  62. fServerPath = tmpServerPath;
  63. fServerPath += fName;
  64. std::free(tmpServerPath);
  65. }
  66. lo_server_add_method(fServer, nullptr, nullptr, osc_message_handler, this);
  67. }
  68. CARLA_ASSERT(fName.isNotEmpty());
  69. CARLA_ASSERT(fServerPath.isNotEmpty());
  70. CARLA_ASSERT(fServer != nullptr);
  71. }
  72. void CarlaBridgeOsc::idle()
  73. {
  74. if (fServer != nullptr)
  75. {
  76. while (lo_server_recv_noblock(fServer, 0) != 0) {}
  77. }
  78. }
  79. void CarlaBridgeOsc::close()
  80. {
  81. carla_debug("CarlaBridgeOsc::close()");
  82. CARLA_ASSERT(fName.isNotEmpty());
  83. CARLA_ASSERT(fServerPath.isNotEmpty());
  84. CARLA_ASSERT(fServer != nullptr);
  85. fName.clear();
  86. if (fServer != nullptr)
  87. {
  88. lo_server_del_method(fServer, nullptr, nullptr);
  89. lo_server_free(fServer);
  90. fServer = nullptr;
  91. }
  92. fServerPath.clear();
  93. fControlData.free();
  94. CARLA_ASSERT(fName.isEmpty());
  95. CARLA_ASSERT(fServerPath.isEmpty());
  96. CARLA_ASSERT(fServer == nullptr);
  97. }
  98. // -----------------------------------------------------------------------
  99. int CarlaBridgeOsc::handleMessage(const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  100. {
  101. carla_debug("CarlaBridgeOsc::handleMessage(\"%s\", %i, %p, \"%s\", %p)", path, argc, argv, types, msg);
  102. CARLA_ASSERT(fName.isNotEmpty());
  103. CARLA_ASSERT(fServerPath.isNotEmpty());
  104. CARLA_ASSERT(fServer != nullptr);
  105. CARLA_ASSERT(path != nullptr);
  106. if (path == nullptr)
  107. {
  108. carla_stderr("CarlaBridgeOsc::handleMessage() - got invalid path");
  109. return 1;
  110. }
  111. if (fName.isEmpty())
  112. {
  113. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received message but client is offline", path);
  114. return 1;
  115. }
  116. const size_t nameSize = fName.length();
  117. // Check if message is for this client
  118. if (std::strlen(path) <= nameSize || std::strncmp(path+1, (const char*)fName, nameSize) != 0)
  119. {
  120. carla_stderr("CarlaBridgeOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, (const char*)fName);
  121. return 1;
  122. }
  123. // Get method from path
  124. char method[32] = { 0 };
  125. std::strncpy(method, path + (nameSize + 2), 31);
  126. if (method[0] == '\0')
  127. {
  128. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received message without method", path);
  129. return 1;
  130. }
  131. // Common OSC methods
  132. if (std::strcmp(method, "configure") == 0)
  133. return handleMsgConfigure(argc, argv, types);
  134. if (std::strcmp(method, "control") == 0)
  135. return handleMsgControl(argc, argv, types);
  136. if (std::strcmp(method, "program") == 0)
  137. return handleMsgProgram(argc, argv, types);
  138. if (std::strcmp(method, "midi-program") == 0)
  139. return handleMsgMidiProgram(argc, argv, types);
  140. if (std::strcmp(method, "midi") == 0)
  141. return handleMsgMidi(argc, argv, types);
  142. if (std::strcmp(method, "sample-rate") == 0)
  143. return 0; // unused
  144. if (std::strcmp(method, "show") == 0)
  145. return handleMsgShow();
  146. if (std::strcmp(method, "hide") == 0)
  147. return handleMsgHide();
  148. if (std::strcmp(method, "quit") == 0)
  149. return handleMsgQuit();
  150. #ifdef BRIDGE_LV2
  151. // LV2 UI methods
  152. if (std::strcmp(method, "lv2_atom_transfer") == 0)
  153. return handleMsgLv2TransferAtom(argc, argv, types);
  154. if (std::strcmp(method, "lv2_event_transfer") == 0)
  155. return handleMsgLv2TransferEvent(argc, argv, types);
  156. #endif
  157. #ifdef BUILD_BRIDGE_PLUGIN
  158. // Plugin methods
  159. if (std::strcmp(method, "plugin_save_now") == 0)
  160. return handleMsgPluginSaveNow();
  161. if (std::strcmp(method, "plugin_set_chunk") == 0)
  162. return handleMsgPluginSetChunk(argc, argv, types);
  163. if (std::strcmp(method, "plugin_set_custom_data") == 0)
  164. return handleMsgPluginSetCustomData(argc, argv, types);
  165. #if 0
  166. // TODO
  167. if (std::strcmp(method, "set_parameter_midi_channel") == 0)
  168. return osc_set_parameter_midi_channel_handler(argv);
  169. if (std::strcmp(method, "set_parameter_midi_cc") == 0)
  170. return osc_set_parameter_midi_channel_handler(argv);
  171. #endif
  172. #endif
  173. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received unsupported OSC method '%s'", path, method);
  174. return 1;
  175. }
  176. int CarlaBridgeOsc::handleMsgConfigure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  177. {
  178. carla_debug("CarlaBridgeOsc::handleMsgConfigure()");
  179. CARLA_ASSERT(kClient != nullptr);
  180. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  181. if (kClient == nullptr)
  182. return 1;
  183. // nothing here for now
  184. return 0;
  185. // unused
  186. (void)argv;
  187. }
  188. int CarlaBridgeOsc::handleMsgControl(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  189. {
  190. carla_debug("CarlaBridgeOsc::handleMsgControl()");
  191. CARLA_ASSERT(kClient != nullptr);
  192. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  193. if (kClient == nullptr)
  194. return 1;
  195. const int32_t index = argv[0]->i;
  196. const float value = argv[1]->f;
  197. CARLA_SAFE_ASSERT_INT(index != -1, index);
  198. if (index == -1)
  199. return 1;
  200. kClient->setParameter(index, value);
  201. return 0;
  202. }
  203. int CarlaBridgeOsc::handleMsgProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  204. {
  205. carla_debug("CarlaBridgeOsc::handleMsgProgram()");
  206. CARLA_ASSERT(kClient != nullptr);
  207. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  208. if (kClient == nullptr)
  209. return 1;
  210. const int32_t index = argv[0]->i;
  211. CARLA_SAFE_ASSERT_INT(index >= 0, index);
  212. if (index < 0)
  213. return 1;
  214. kClient->setProgram(static_cast<uint32_t>(index));
  215. return 0;
  216. }
  217. #ifdef BUILD_BRIDGE_PLUGIN
  218. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  219. {
  220. carla_debug("CarlaBridgeOsc::handleMsgMidiProgram()");
  221. CARLA_ASSERT(kClient != nullptr);
  222. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  223. if (kClient == nullptr)
  224. return 1;
  225. const int32_t index = argv[0]->i;
  226. CARLA_SAFE_ASSERT_INT(index >= 0, index);
  227. if (index < 0)
  228. return 1;
  229. kClient->setMidiProgram(static_cast<uint32_t>(index));
  230. return 0;
  231. }
  232. #else
  233. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  234. {
  235. carla_debug("CarlaBridgeOsc::handleMsgMidiProgram()");
  236. CARLA_ASSERT(kClient != nullptr);
  237. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  238. if (kClient == nullptr)
  239. return 1;
  240. const int32_t bank = argv[0]->i;
  241. const int32_t program = argv[1]->i;
  242. CARLA_SAFE_ASSERT_INT(bank >= 0, bank);
  243. CARLA_SAFE_ASSERT_INT(program >= 0, program);
  244. if (bank < 0)
  245. return 1;
  246. if (program < 0)
  247. return 1;
  248. kClient->setMidiProgram(static_cast<uint32_t>(bank), static_cast<uint32_t>(program));
  249. return 0;
  250. }
  251. #endif
  252. int CarlaBridgeOsc::handleMsgMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  253. {
  254. carla_debug("CarlaBridgeOsc::handleMsgMidi()");
  255. CARLA_ASSERT(kClient != nullptr);
  256. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  257. if (kClient == nullptr)
  258. return 1;
  259. const uint8_t* const data = argv[0]->m;
  260. uint8_t status = data[1];
  261. uint8_t channel = status & 0x0F;
  262. // Fix bad note-off
  263. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  264. status -= 0x10;
  265. if (MIDI_IS_STATUS_NOTE_OFF(status))
  266. {
  267. const uint8_t note = data[2];
  268. CARLA_SAFE_ASSERT_INT(note < MAX_MIDI_NOTE, note);
  269. if (note >= MAX_MIDI_NOTE)
  270. return 1;
  271. kClient->noteOff(channel, note);
  272. }
  273. else if (MIDI_IS_STATUS_NOTE_ON(status))
  274. {
  275. const uint8_t note = data[2];
  276. const uint8_t velo = data[3];
  277. CARLA_SAFE_ASSERT_INT(note < MAX_MIDI_NOTE, note);
  278. CARLA_SAFE_ASSERT_INT(velo < MAX_MIDI_VALUE, velo);
  279. if (note >= MAX_MIDI_NOTE)
  280. return 1;
  281. if (velo >= MAX_MIDI_VALUE)
  282. return 1;
  283. kClient->noteOn(channel, note, velo);
  284. }
  285. return 0;
  286. }
  287. #ifdef BUILD_BRIDGE_UI
  288. int CarlaBridgeOsc::handleMsgShow()
  289. {
  290. carla_debug("CarlaBridgeOsc::handleMsgShow()");
  291. CARLA_ASSERT(kClient != nullptr);
  292. if (kClient == nullptr)
  293. return 1;
  294. kClient->toolkitShow();
  295. return 0;
  296. }
  297. int CarlaBridgeOsc::handleMsgHide()
  298. {
  299. carla_debug("CarlaBridgeOsc::handleMsgHide()");
  300. CARLA_ASSERT(kClient != nullptr);
  301. if (kClient == nullptr)
  302. return 1;
  303. kClient->toolkitHide();
  304. return 0;
  305. }
  306. int CarlaBridgeOsc::handleMsgQuit()
  307. {
  308. carla_debug("CarlaBridgeOsc::handleMsgQuit()");
  309. CARLA_ASSERT(kClient != nullptr);
  310. if (kClient == nullptr)
  311. return 1;
  312. kClient->toolkitQuit();
  313. return 0;
  314. }
  315. #endif
  316. CARLA_BRIDGE_END_NAMESPACE