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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 "CarlaBridgeClient.hpp"
  18. #include "CarlaMIDI.h"
  19. CARLA_BRIDGE_START_NAMESPACE
  20. #if 0
  21. } // Fix editor indentation
  22. #endif
  23. // -----------------------------------------------------------------------
  24. CarlaBridgeOsc::CarlaBridgeOsc(CarlaBridgeClient* const client)
  25. : kClient(client),
  26. fServer(nullptr)
  27. {
  28. CARLA_ASSERT(client != nullptr);
  29. carla_debug("CarlaBridgeOsc::CarlaBridgeOsc(%p)", client);
  30. }
  31. CarlaBridgeOsc::~CarlaBridgeOsc()
  32. {
  33. CARLA_ASSERT(fName.isEmpty());
  34. CARLA_ASSERT(fServerPath.isEmpty());
  35. CARLA_ASSERT(fServer == nullptr);
  36. CARLA_ASSERT(fControlData.source == nullptr); // must never be used
  37. carla_debug("CarlaBridgeOsc::~CarlaBridgeOsc()");
  38. }
  39. void CarlaBridgeOsc::init(const char* const url)
  40. {
  41. CARLA_ASSERT(fName.isEmpty());
  42. CARLA_ASSERT(fServerPath.isEmpty());
  43. CARLA_ASSERT(fServer == nullptr);
  44. CARLA_ASSERT(url != nullptr);
  45. carla_debug("CarlaBridgeOsc::init(\"%s\")", url);
  46. #ifdef BUILD_BRIDGE_PLUGIN
  47. fName = "carla-bridge-plugin";
  48. #else
  49. fName = "carla-bridge-ui";
  50. #endif
  51. {
  52. char* const host = lo_url_get_hostname(url);
  53. char* const port = lo_url_get_port(url);
  54. fControlData.path = carla_strdup_free(lo_url_get_path(url));
  55. fControlData.target = lo_address_new_with_proto(LO_TCP, host, port);
  56. std::free(host);
  57. std::free(port);
  58. }
  59. fServer = lo_server_new_with_proto(nullptr, LO_TCP, osc_error_handler);
  60. if (fServer != nullptr)
  61. {
  62. if (char* const tmpServerPath = lo_server_get_url(fServer))
  63. {
  64. fServerPath = tmpServerPath;
  65. fServerPath += fName;
  66. std::free(tmpServerPath);
  67. }
  68. lo_server_add_method(fServer, nullptr, nullptr, osc_message_handler, this);
  69. }
  70. CARLA_ASSERT(fName.isNotEmpty());
  71. CARLA_ASSERT(fServerPath.isNotEmpty());
  72. CARLA_ASSERT(fServer != nullptr);
  73. }
  74. void CarlaBridgeOsc::idle()
  75. {
  76. if (fServer != nullptr)
  77. {
  78. while (lo_server_recv_noblock(fServer, 0) != 0) {}
  79. }
  80. }
  81. void CarlaBridgeOsc::close()
  82. {
  83. CARLA_ASSERT(fName.isNotEmpty());
  84. CARLA_ASSERT(fServerPath.isNotEmpty());
  85. CARLA_ASSERT(fServer != nullptr);
  86. CARLA_ASSERT(fControlData.source == nullptr); // must never be used
  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+1] = { '\0' };
  128. std::strncpy(method, path + (nameSize + 2), 32);
  129. if (method[0] == '\0')
  130. {
  131. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received message without method", path);
  132. return 1;
  133. }
  134. #ifdef BUILD_BRIDGE_UI
  135. // Common UI methods
  136. if (std::strcmp(method, "configure") == 0)
  137. return handleMsgConfigure(argc, argv, types);
  138. if (std::strcmp(method, "control") == 0)
  139. return handleMsgControl(argc, argv, types);
  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. if (std::strcmp(method, "sample-rate") == 0)
  147. return 0; // unused
  148. #endif
  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 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. if (std::strcmp(method, "plugin_set_parameter_midi_channel") == 0)
  167. return handleMsgPluginSetParameterMidiChannel(argc, argv, types);
  168. if (std::strcmp(method, "plugin_set_parameter_midi_cc") == 0)
  169. return handleMsgPluginSetParameterMidiCC(argc, argv, types);
  170. if (std::strcmp(method, "plugin_set_chunk") == 0)
  171. return handleMsgPluginSetChunk(argc, argv, types);
  172. if (std::strcmp(method, "plugin_set_custom_data") == 0)
  173. return handleMsgPluginSetCustomData(argc, argv, types);
  174. #endif
  175. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received unsupported OSC method '%s'", path, method);
  176. return 1;
  177. }
  178. #ifdef BUILD_BRIDGE_UI
  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. int CarlaBridgeOsc::handleMsgProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  207. {
  208. CARLA_ASSERT(kClient != nullptr);
  209. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  210. carla_debug("CarlaBridgeOsc::handleMsgProgram()");
  211. if (kClient == nullptr)
  212. return 1;
  213. const int32_t index = argv[0]->i;
  214. CARLA_SAFE_ASSERT_INT(index >= 0, index);
  215. if (index < 0)
  216. return 1;
  217. kClient->setProgram(static_cast<uint32_t>(index));
  218. return 0;
  219. }
  220. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  221. {
  222. CARLA_ASSERT(kClient != nullptr);
  223. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  224. carla_debug("CarlaBridgeOsc::handleMsgMidiProgram()");
  225. if (kClient == nullptr)
  226. return 1;
  227. const int32_t bank = argv[0]->i;
  228. const int32_t program = argv[1]->i;
  229. CARLA_SAFE_ASSERT_INT(bank >= 0, bank);
  230. CARLA_SAFE_ASSERT_INT(program >= 0, program);
  231. if (bank < 0)
  232. return 1;
  233. if (program < 0)
  234. return 1;
  235. kClient->setMidiProgram(static_cast<uint32_t>(bank), static_cast<uint32_t>(program));
  236. return 0;
  237. }
  238. int CarlaBridgeOsc::handleMsgMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  239. {
  240. CARLA_ASSERT(kClient != nullptr);
  241. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  242. carla_debug("CarlaBridgeOsc::handleMsgMidi()");
  243. if (kClient == nullptr)
  244. return 1;
  245. const uint8_t* const data = argv[0]->m;
  246. uint8_t status = data[1];
  247. uint8_t channel = status & 0x0F;
  248. // Fix bad note-off
  249. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  250. status -= 0x10;
  251. if (MIDI_IS_STATUS_NOTE_OFF(status))
  252. {
  253. const uint8_t note = data[2];
  254. CARLA_SAFE_ASSERT_INT(note < MAX_MIDI_NOTE, note);
  255. if (note >= MAX_MIDI_NOTE)
  256. return 1;
  257. kClient->noteOff(channel, note);
  258. }
  259. else if (MIDI_IS_STATUS_NOTE_ON(status))
  260. {
  261. const uint8_t note = data[2];
  262. const uint8_t velo = data[3];
  263. CARLA_SAFE_ASSERT_INT(note < MAX_MIDI_NOTE, note);
  264. CARLA_SAFE_ASSERT_INT(velo < MAX_MIDI_VALUE, velo);
  265. if (note >= MAX_MIDI_NOTE)
  266. return 1;
  267. if (velo >= MAX_MIDI_VALUE)
  268. return 1;
  269. kClient->noteOn(channel, note, velo);
  270. }
  271. return 0;
  272. }
  273. int CarlaBridgeOsc::handleMsgShow()
  274. {
  275. CARLA_ASSERT(kClient != nullptr);
  276. carla_debug("CarlaBridgeOsc::handleMsgShow()");
  277. if (kClient == nullptr)
  278. return 1;
  279. kClient->toolkitShow();
  280. return 0;
  281. }
  282. int CarlaBridgeOsc::handleMsgHide()
  283. {
  284. CARLA_ASSERT(kClient != nullptr);
  285. carla_debug("CarlaBridgeOsc::handleMsgHide()");
  286. if (kClient == nullptr)
  287. return 1;
  288. kClient->toolkitHide();
  289. return 0;
  290. }
  291. int CarlaBridgeOsc::handleMsgQuit()
  292. {
  293. CARLA_ASSERT(kClient != nullptr);
  294. carla_debug("CarlaBridgeOsc::handleMsgQuit()");
  295. if (kClient == nullptr)
  296. return 1;
  297. kClient->toolkitQuit();
  298. return 0;
  299. }
  300. #endif // BUILD_BRIDGE_UI
  301. CARLA_BRIDGE_END_NAMESPACE