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