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

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