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

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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::close()
  74. {
  75. CARLA_ASSERT(fControlData.source == nullptr); // must never be used
  76. CARLA_ASSERT(fName.isNotEmpty());
  77. CARLA_ASSERT(fServerPath.isNotEmpty());
  78. CARLA_ASSERT(fServer != nullptr);
  79. carla_debug("CarlaBridgeOsc::close()");
  80. fName.clear();
  81. if (fServer != nullptr)
  82. {
  83. lo_server_del_method(fServer, nullptr, nullptr);
  84. lo_server_free(fServer);
  85. fServer = nullptr;
  86. }
  87. fServerPath.clear();
  88. fControlData.clear();
  89. CARLA_ASSERT(fName.isEmpty());
  90. CARLA_ASSERT(fServerPath.isEmpty());
  91. CARLA_ASSERT(fServer == nullptr);
  92. }
  93. // -----------------------------------------------------------------------
  94. int CarlaBridgeOsc::handleMessage(const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  95. {
  96. CARLA_SAFE_ASSERT_RETURN(fName.isNotEmpty(), 1);
  97. CARLA_SAFE_ASSERT_RETURN(fServerPath.isNotEmpty(), 1);
  98. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  99. CARLA_SAFE_ASSERT_RETURN(path != nullptr, 1);
  100. CARLA_SAFE_ASSERT_RETURN(msg != nullptr, 1);
  101. carla_debug("CarlaBridgeOsc::handleMessage(\"%s\", %i, %p, \"%s\", %p)", path, argc, argv, types, msg);
  102. const size_t nameSize(fName.length());
  103. // Check if message is for this client
  104. if (std::strlen(path) <= nameSize || std::strncmp(path+1, fName, nameSize) != 0)
  105. {
  106. carla_stderr("CarlaBridgeOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, fName.buffer());
  107. return 1;
  108. }
  109. // Get method from path
  110. char method[32+1] = { '\0' };
  111. std::strncpy(method, path + (nameSize + 2), 32);
  112. if (method[0] == '\0')
  113. {
  114. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received message without method", path);
  115. return 1;
  116. }
  117. // Common methods
  118. if (std::strcmp(method, "show") == 0)
  119. return handleMsgShow();
  120. if (std::strcmp(method, "hide") == 0)
  121. return handleMsgHide();
  122. if (std::strcmp(method, "quit") == 0)
  123. return handleMsgQuit();
  124. // UI methods
  125. if (std::strcmp(method, "configure") == 0)
  126. return handleMsgConfigure(argc, argv, types);
  127. if (std::strcmp(method, "control") == 0)
  128. return handleMsgControl(argc, argv, types);
  129. if (std::strcmp(method, "program") == 0)
  130. return handleMsgProgram(argc, argv, types);
  131. if (std::strcmp(method, "midi-program") == 0)
  132. return handleMsgMidiProgram(argc, argv, types);
  133. if (std::strcmp(method, "midi") == 0)
  134. return handleMsgMidi(argc, argv, types);
  135. if (std::strcmp(method, "sample-rate") == 0)
  136. return 0; // unused
  137. // special
  138. if (std::strcmp(method, "update_url") == 0)
  139. return handleMsgUpdateURL(argc, argv, types);
  140. #ifdef BRIDGE_LV2
  141. // LV2 methods
  142. if (std::strcmp(method, "lv2_atom_transfer") == 0)
  143. return handleMsgLv2AtomTransfer(argc, argv, types);
  144. if (std::strcmp(method, "lv2_urid_map") == 0)
  145. return handleMsgLv2UridMap(argc, argv, types);
  146. #endif
  147. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received unsupported OSC method '%s'", path, method);
  148. return 1;
  149. }
  150. int CarlaBridgeOsc::handleMsgShow()
  151. {
  152. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  153. carla_debug("CarlaBridgeOsc::handleMsgShow()");
  154. kClient->toolkitShow();
  155. return 0;
  156. }
  157. int CarlaBridgeOsc::handleMsgHide()
  158. {
  159. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  160. carla_debug("CarlaBridgeOsc::handleMsgHide()");
  161. kClient->toolkitHide();
  162. return 0;
  163. }
  164. int CarlaBridgeOsc::handleMsgQuit()
  165. {
  166. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  167. carla_debug("CarlaBridgeOsc::handleMsgQuit()");
  168. kClient->toolkitQuit();
  169. return 0;
  170. }
  171. int CarlaBridgeOsc::handleMsgConfigure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  172. {
  173. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  174. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  175. carla_debug("CarlaBridgeOsc::handleMsgConfigure()");
  176. // nothing here for now
  177. return 0;
  178. // unused
  179. (void)argv;
  180. }
  181. int CarlaBridgeOsc::handleMsgControl(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  182. {
  183. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  184. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  185. carla_debug("CarlaBridgeOsc::handleMsgControl()");
  186. const int32_t index = argv[0]->i;
  187. const float value = argv[1]->f;
  188. CARLA_SAFE_ASSERT_RETURN(index != -1, 1);
  189. kClient->setParameter(index, value);
  190. return 0;
  191. }
  192. int CarlaBridgeOsc::handleMsgProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  193. {
  194. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  195. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  196. carla_debug("CarlaBridgeOsc::handleMsgProgram()");
  197. const int32_t index = argv[0]->i;
  198. CARLA_SAFE_ASSERT_RETURN(index >= 0, 1);
  199. kClient->setProgram(static_cast<uint32_t>(index));
  200. return 0;
  201. }
  202. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  203. {
  204. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  205. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  206. carla_debug("CarlaBridgeOsc::handleMsgMidiProgram()");
  207. const int32_t bank = argv[0]->i;
  208. const int32_t program = argv[1]->i;
  209. CARLA_SAFE_ASSERT_RETURN(bank >= 0, 1);
  210. CARLA_SAFE_ASSERT_RETURN(program >= 0, 1);
  211. kClient->setMidiProgram(static_cast<uint32_t>(bank), static_cast<uint32_t>(program));
  212. return 0;
  213. }
  214. int CarlaBridgeOsc::handleMsgMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  215. {
  216. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  217. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  218. carla_debug("CarlaBridgeOsc::handleMsgMidi()");
  219. const uint8_t* const data = argv[0]->m;
  220. uint8_t status = data[1];
  221. uint8_t channel = status & 0x0F;
  222. // Fix bad note-off
  223. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  224. status -= 0x10;
  225. if (MIDI_IS_STATUS_NOTE_OFF(status))
  226. {
  227. const uint8_t note = data[2];
  228. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, 1);
  229. kClient->noteOff(channel, note);
  230. }
  231. else if (MIDI_IS_STATUS_NOTE_ON(status))
  232. {
  233. const uint8_t note = data[2];
  234. const uint8_t velo = data[3];
  235. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, 1);
  236. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE, 1);
  237. kClient->noteOn(channel, note, velo);
  238. }
  239. return 0;
  240. }
  241. int CarlaBridgeOsc::handleMsgUpdateURL(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  242. {
  243. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "s");
  244. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  245. carla_debug("CarlaBridgeOsc::handleMsgUpdateURL()");
  246. const char* const url = (const char*)&argv[0]->s;
  247. fControlData.setNewURL(url);
  248. return 0;
  249. }
  250. // -----------------------------------------------------------------------
  251. CARLA_BRIDGE_END_NAMESPACE