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 9.3KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Carla Bridge OSC
  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 "CarlaBridgeClient.hpp"
  18. #include "CarlaMIDI.h"
  19. CARLA_BRIDGE_START_NAMESPACE
  20. // -----------------------------------------------------------------------
  21. CarlaBridgeOsc::CarlaBridgeOsc(CarlaBridgeClient* const client)
  22. : kClient(client),
  23. fServer(nullptr)
  24. {
  25. CARLA_ASSERT(client != nullptr);
  26. carla_debug("CarlaBridgeOsc::CarlaBridgeOsc(%p)", client);
  27. }
  28. CarlaBridgeOsc::~CarlaBridgeOsc()
  29. {
  30. CARLA_ASSERT(fControlData.source == nullptr); // must never be used
  31. CARLA_ASSERT(fName.isEmpty());
  32. CARLA_ASSERT(fServerPath.isEmpty());
  33. CARLA_ASSERT(fServer == nullptr);
  34. carla_debug("CarlaBridgeOsc::~CarlaBridgeOsc()");
  35. }
  36. void CarlaBridgeOsc::init(const char* const url)
  37. {
  38. CARLA_ASSERT(fName.isEmpty());
  39. CARLA_ASSERT(fServerPath.isEmpty());
  40. CARLA_ASSERT(fServer == nullptr);
  41. CARLA_ASSERT(url != nullptr);
  42. carla_debug("CarlaBridgeOsc::init(\"%s\")", url);
  43. std::srand((uint)(uintptr_t)this);
  44. std::srand((uint)(uintptr_t)&url);
  45. fName = "ui-";
  46. fName += CarlaString(std::rand() % 99999);
  47. fServer = lo_server_new_with_proto(nullptr, LO_UDP, osc_error_handler);
  48. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr,)
  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_UDP, host, port);
  54. std::free(host);
  55. std::free(port);
  56. }
  57. if (char* const tmpServerPath = lo_server_get_url(fServer))
  58. {
  59. fServerPath = tmpServerPath;
  60. fServerPath += fName;
  61. std::free(tmpServerPath);
  62. }
  63. lo_server_add_method(fServer, nullptr, nullptr, osc_message_handler, this);
  64. CARLA_ASSERT(fName.isNotEmpty());
  65. CARLA_ASSERT(fServerPath.isNotEmpty());
  66. }
  67. void CarlaBridgeOsc::idle() const
  68. {
  69. if (fServer == nullptr)
  70. return;
  71. for (; lo_server_recv_noblock(fServer, 0) != 0;) {}
  72. }
  73. void CarlaBridgeOsc::idleOnce() const
  74. {
  75. if (fServer == nullptr)
  76. return;
  77. lo_server_recv_noblock(fServer, 0);
  78. }
  79. void CarlaBridgeOsc::close()
  80. {
  81. CARLA_ASSERT(fControlData.source == nullptr); // must never be used
  82. CARLA_ASSERT(fName.isNotEmpty());
  83. CARLA_ASSERT(fServerPath.isNotEmpty());
  84. CARLA_ASSERT(fServer != nullptr);
  85. carla_debug("CarlaBridgeOsc::close()");
  86. fName.clear();
  87. if (fServer != nullptr)
  88. {
  89. lo_server_del_method(fServer, nullptr, nullptr);
  90. lo_server_free(fServer);
  91. fServer = nullptr;
  92. }
  93. fServerPath.clear();
  94. fControlData.clear();
  95. CARLA_ASSERT(fName.isEmpty());
  96. CARLA_ASSERT(fServerPath.isEmpty());
  97. CARLA_ASSERT(fServer == nullptr);
  98. }
  99. // -----------------------------------------------------------------------
  100. int CarlaBridgeOsc::handleMessage(const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  101. {
  102. CARLA_SAFE_ASSERT_RETURN(fName.isNotEmpty(), 1);
  103. CARLA_SAFE_ASSERT_RETURN(fServerPath.isNotEmpty(), 1);
  104. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  105. CARLA_SAFE_ASSERT_RETURN(path != nullptr, 1);
  106. CARLA_SAFE_ASSERT_RETURN(msg != nullptr, 1);
  107. carla_debug("CarlaBridgeOsc::handleMessage(\"%s\", %i, %p, \"%s\", %p)", path, argc, argv, types, msg);
  108. const size_t nameSize(fName.length());
  109. // Check if message is for this client
  110. if (std::strlen(path) <= nameSize || std::strncmp(path+1, fName, nameSize) != 0)
  111. {
  112. carla_stderr("CarlaBridgeOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, fName.buffer());
  113. return 1;
  114. }
  115. // Get method from path
  116. char method[32+1] = { '\0' };
  117. std::strncpy(method, path + (nameSize + 2), 32);
  118. if (method[0] == '\0')
  119. {
  120. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received message without method", path);
  121. return 1;
  122. }
  123. // Common methods
  124. if (std::strcmp(method, "show") == 0)
  125. return handleMsgShow();
  126. if (std::strcmp(method, "hide") == 0)
  127. return handleMsgHide();
  128. if (std::strcmp(method, "quit") == 0)
  129. return handleMsgQuit();
  130. // UI methods
  131. if (std::strcmp(method, "configure") == 0)
  132. return handleMsgConfigure(argc, argv, types);
  133. if (std::strcmp(method, "control") == 0)
  134. return handleMsgControl(argc, argv, types);
  135. if (std::strcmp(method, "program") == 0)
  136. return handleMsgProgram(argc, argv, types);
  137. if (std::strcmp(method, "midi-program") == 0)
  138. return handleMsgMidiProgram(argc, argv, types);
  139. if (std::strcmp(method, "midi") == 0)
  140. return handleMsgMidi(argc, argv, types);
  141. if (std::strcmp(method, "sample-rate") == 0)
  142. return 0; // unused
  143. // special
  144. if (std::strcmp(method, "update_url") == 0)
  145. return handleMsgUpdateURL(argc, argv, types);
  146. #ifdef BRIDGE_LV2
  147. // LV2 methods
  148. if (std::strcmp(method, "lv2_atom_transfer") == 0)
  149. return handleMsgLv2AtomTransfer(argc, argv, types);
  150. if (std::strcmp(method, "lv2_urid_map") == 0)
  151. return handleMsgLv2UridMap(argc, argv, types);
  152. #endif
  153. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received unsupported OSC method '%s'", path, method);
  154. return 1;
  155. }
  156. int CarlaBridgeOsc::handleMsgShow()
  157. {
  158. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  159. carla_debug("CarlaBridgeOsc::handleMsgShow()");
  160. kClient->toolkitShow();
  161. return 0;
  162. }
  163. int CarlaBridgeOsc::handleMsgHide()
  164. {
  165. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  166. carla_debug("CarlaBridgeOsc::handleMsgHide()");
  167. kClient->toolkitHide();
  168. return 0;
  169. }
  170. int CarlaBridgeOsc::handleMsgQuit()
  171. {
  172. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  173. carla_debug("CarlaBridgeOsc::handleMsgQuit()");
  174. kClient->toolkitQuit();
  175. return 0;
  176. }
  177. int CarlaBridgeOsc::handleMsgConfigure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  178. {
  179. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  180. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  181. carla_debug("CarlaBridgeOsc::handleMsgConfigure()");
  182. // nothing here for now
  183. return 0;
  184. // unused
  185. (void)argv;
  186. }
  187. int CarlaBridgeOsc::handleMsgControl(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  188. {
  189. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  190. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  191. carla_debug("CarlaBridgeOsc::handleMsgControl()");
  192. const int32_t index = argv[0]->i;
  193. const float value = argv[1]->f;
  194. CARLA_SAFE_ASSERT_RETURN(index != -1, 1);
  195. kClient->setParameter(index, value);
  196. return 0;
  197. }
  198. int CarlaBridgeOsc::handleMsgProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  199. {
  200. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  201. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  202. carla_debug("CarlaBridgeOsc::handleMsgProgram()");
  203. const int32_t index = argv[0]->i;
  204. CARLA_SAFE_ASSERT_RETURN(index >= 0, 1);
  205. kClient->setProgram(static_cast<uint32_t>(index));
  206. return 0;
  207. }
  208. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  209. {
  210. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  211. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  212. carla_debug("CarlaBridgeOsc::handleMsgMidiProgram()");
  213. const int32_t bank = argv[0]->i;
  214. const int32_t program = argv[1]->i;
  215. CARLA_SAFE_ASSERT_RETURN(bank >= 0, 1);
  216. CARLA_SAFE_ASSERT_RETURN(program >= 0, 1);
  217. kClient->setMidiProgram(static_cast<uint32_t>(bank), static_cast<uint32_t>(program));
  218. return 0;
  219. }
  220. int CarlaBridgeOsc::handleMsgMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  221. {
  222. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  223. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  224. carla_debug("CarlaBridgeOsc::handleMsgMidi()");
  225. const uint8_t* const data = argv[0]->m;
  226. uint8_t status = data[1];
  227. uint8_t channel = status & 0x0F;
  228. // Fix bad note-off
  229. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  230. status -= 0x10;
  231. if (MIDI_IS_STATUS_NOTE_OFF(status))
  232. {
  233. const uint8_t note = data[2];
  234. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, 1);
  235. kClient->noteOff(channel, note);
  236. }
  237. else if (MIDI_IS_STATUS_NOTE_ON(status))
  238. {
  239. const uint8_t note = data[2];
  240. const uint8_t velo = data[3];
  241. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, 1);
  242. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE, 1);
  243. kClient->noteOn(channel, note, velo);
  244. }
  245. return 0;
  246. }
  247. int CarlaBridgeOsc::handleMsgUpdateURL(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  248. {
  249. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "s");
  250. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  251. carla_debug("CarlaBridgeOsc::handleMsgUpdateURL()");
  252. const char* const url = (const char*)&argv[0]->s;
  253. fControlData.setNewURL(url);
  254. return 0;
  255. }
  256. // -----------------------------------------------------------------------
  257. CARLA_BRIDGE_END_NAMESPACE