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.4KB

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