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

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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-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 doc/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. : fClient(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(fControlData.source == nullptr); // must never be used
  34. CARLA_ASSERT(fName.isEmpty());
  35. CARLA_ASSERT(fServerPath.isEmpty());
  36. CARLA_ASSERT(fServer == nullptr);
  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. std::srand((uint)(uintptr_t)this);
  47. std::srand((uint)(uintptr_t)&url);
  48. #ifdef BUILD_BRIDGE_PLUGIN
  49. fName = "plug-";
  50. fName += CarlaString(std::rand() % 99999);
  51. #else
  52. fName = "ui-";
  53. fName += CarlaString(std::rand() % 99999);
  54. #endif
  55. fServer = lo_server_new_with_proto(nullptr, LO_UDP, osc_error_handler);
  56. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr,)
  57. {
  58. char* const host = lo_url_get_hostname(url);
  59. char* const port = lo_url_get_port(url);
  60. fControlData.path = carla_strdup_free(lo_url_get_path(url));
  61. fControlData.target = lo_address_new_with_proto(LO_UDP, host, port);
  62. std::free(host);
  63. std::free(port);
  64. }
  65. if (char* const tmpServerPath = lo_server_get_url(fServer))
  66. {
  67. fServerPath = tmpServerPath;
  68. fServerPath += fName;
  69. std::free(tmpServerPath);
  70. }
  71. lo_server_add_method(fServer, nullptr, nullptr, osc_message_handler, this);
  72. CARLA_ASSERT(fName.isNotEmpty());
  73. CARLA_ASSERT(fServerPath.isNotEmpty());
  74. }
  75. void CarlaBridgeOsc::idle() const
  76. {
  77. if (fServer == nullptr)
  78. return;
  79. for (; lo_server_recv_noblock(fServer, 0) != 0;) {}
  80. }
  81. void CarlaBridgeOsc::close()
  82. {
  83. CARLA_ASSERT(fControlData.source == nullptr); // must never be used
  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_SAFE_ASSERT_RETURN(fName.isNotEmpty(), 1);
  105. CARLA_SAFE_ASSERT_RETURN(fServerPath.isNotEmpty(), 1);
  106. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  107. CARLA_SAFE_ASSERT_RETURN(path != nullptr, 1);
  108. CARLA_SAFE_ASSERT_RETURN(msg != nullptr, 1);
  109. carla_debug("CarlaBridgeOsc::handleMessage(\"%s\", %i, %p, \"%s\", %p)", path, argc, argv, types, msg);
  110. const size_t nameSize(fName.length());
  111. // Check if message is for this client
  112. if (std::strlen(path) <= nameSize || std::strncmp(path+1, fName, nameSize) != 0)
  113. {
  114. carla_stderr("CarlaBridgeOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, fName.buffer());
  115. return 1;
  116. }
  117. // Get method from path
  118. char method[32+1] = { '\0' };
  119. std::strncpy(method, path + (nameSize + 2), 32);
  120. if (method[0] == '\0')
  121. {
  122. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received message without method", path);
  123. return 1;
  124. }
  125. // Common methods
  126. if (std::strcmp(method, "show") == 0)
  127. return handleMsgShow();
  128. if (std::strcmp(method, "hide") == 0)
  129. return handleMsgHide();
  130. if (std::strcmp(method, "quit") == 0)
  131. return handleMsgQuit();
  132. #ifdef BRIDGE_LV2
  133. // LV2 methods
  134. if (std::strcmp(method, "lv2_atom_transfer") == 0)
  135. return handleMsgLv2AtomTransfer(argc, argv, types);
  136. if (std::strcmp(method, "lv2_urid_map") == 0)
  137. return handleMsgLv2UridMap(argc, argv, types);
  138. #endif
  139. #ifdef BUILD_BRIDGE_UI
  140. // UI methods
  141. if (std::strcmp(method, "configure") == 0)
  142. return handleMsgConfigure(argc, argv, types);
  143. if (std::strcmp(method, "control") == 0)
  144. return handleMsgControl(argc, argv, types);
  145. if (std::strcmp(method, "program") == 0)
  146. return handleMsgProgram(argc, argv, types);
  147. if (std::strcmp(method, "midi-program") == 0)
  148. return handleMsgMidiProgram(argc, argv, types);
  149. if (std::strcmp(method, "midi") == 0)
  150. return handleMsgMidi(argc, argv, types);
  151. if (std::strcmp(method, "sample-rate") == 0)
  152. return 0; // unused
  153. #endif
  154. #if defined(BUILD_BRIDGE_PLUGIN) && ! defined(BRIDGE_JACK)
  155. // Plugin methods
  156. if (std::strcmp(method, "plugin_save_now") == 0)
  157. return handleMsgPluginSaveNow();
  158. if (std::strcmp(method, "plugin_set_parameter_midi_channel") == 0)
  159. return handleMsgPluginSetParameterMidiChannel(argc, argv, types);
  160. if (std::strcmp(method, "plugin_set_parameter_midi_cc") == 0)
  161. return handleMsgPluginSetParameterMidiCC(argc, argv, types);
  162. if (std::strcmp(method, "plugin_set_chunk") == 0)
  163. return handleMsgPluginSetChunk(argc, argv, types);
  164. if (std::strcmp(method, "plugin_set_custom_data") == 0)
  165. return handleMsgPluginSetCustomData(argc, argv, types);
  166. #endif
  167. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received unsupported OSC method '%s'", path, method);
  168. return 1;
  169. }
  170. #ifdef BUILD_BRIDGE_UI
  171. int CarlaBridgeOsc::handleMsgShow()
  172. {
  173. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  174. carla_debug("CarlaBridgeOsc::handleMsgShow()");
  175. fClient->toolkitShow();
  176. return 0;
  177. }
  178. int CarlaBridgeOsc::handleMsgHide()
  179. {
  180. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  181. carla_debug("CarlaBridgeOsc::handleMsgHide()");
  182. fClient->toolkitHide();
  183. return 0;
  184. }
  185. int CarlaBridgeOsc::handleMsgQuit()
  186. {
  187. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  188. carla_debug("CarlaBridgeOsc::handleMsgQuit()");
  189. fClient->toolkitQuit();
  190. return 0;
  191. }
  192. int CarlaBridgeOsc::handleMsgConfigure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  193. {
  194. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  195. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  196. carla_debug("CarlaBridgeOsc::handleMsgConfigure()");
  197. // nothing here for now
  198. return 0;
  199. // unused
  200. (void)argv;
  201. }
  202. int CarlaBridgeOsc::handleMsgControl(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  203. {
  204. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  205. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  206. carla_debug("CarlaBridgeOsc::handleMsgControl()");
  207. const int32_t index = argv[0]->i;
  208. const float value = argv[1]->f;
  209. CARLA_SAFE_ASSERT_RETURN(index != -1, 1);
  210. fClient->setParameter(index, value);
  211. return 0;
  212. }
  213. int CarlaBridgeOsc::handleMsgProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  214. {
  215. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  216. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  217. carla_debug("CarlaBridgeOsc::handleMsgProgram()");
  218. const int32_t index = argv[0]->i;
  219. CARLA_SAFE_ASSERT_RETURN(index >= 0, 1);
  220. fClient->setProgram(static_cast<uint32_t>(index));
  221. return 0;
  222. }
  223. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  224. {
  225. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  226. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  227. carla_debug("CarlaBridgeOsc::handleMsgMidiProgram()");
  228. const int32_t bank = argv[0]->i;
  229. const int32_t program = argv[1]->i;
  230. CARLA_SAFE_ASSERT_RETURN(bank >= 0, 1);
  231. CARLA_SAFE_ASSERT_RETURN(program >= 0, 1);
  232. fClient->setMidiProgram(static_cast<uint32_t>(bank), static_cast<uint32_t>(program));
  233. return 0;
  234. }
  235. int CarlaBridgeOsc::handleMsgMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  236. {
  237. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  238. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  239. carla_debug("CarlaBridgeOsc::handleMsgMidi()");
  240. const uint8_t* const data = argv[0]->m;
  241. uint8_t status = data[1];
  242. uint8_t channel = status & 0x0F;
  243. // Fix bad note-off
  244. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  245. status -= 0x10;
  246. if (MIDI_IS_STATUS_NOTE_OFF(status))
  247. {
  248. const uint8_t note = data[2];
  249. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, 1);
  250. fClient->noteOff(channel, note);
  251. }
  252. else if (MIDI_IS_STATUS_NOTE_ON(status))
  253. {
  254. const uint8_t note = data[2];
  255. const uint8_t velo = data[3];
  256. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, 1);
  257. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE, 1);
  258. fClient->noteOn(channel, note, velo);
  259. }
  260. return 0;
  261. }
  262. #endif // BUILD_BRIDGE_UI
  263. CARLA_BRIDGE_END_NAMESPACE