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.

CarlaBridgeOsc.cpp 10KB

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