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.

320 lines
8.9KB

  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. // -----------------------------------------------------------------------
  21. CarlaBridgeOsc::CarlaBridgeOsc(CarlaBridgeClient* const client)
  22. : fClient(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. #ifdef BUILD_BRIDGE_PLUGIN
  46. fName = "plug-";
  47. fName += CarlaString(std::rand() % 99999);
  48. #else
  49. fName = "ui-";
  50. fName += CarlaString(std::rand() % 99999);
  51. #endif
  52. fServer = lo_server_new_with_proto(nullptr, LO_UDP, osc_error_handler);
  53. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr,)
  54. {
  55. char* const host = lo_url_get_hostname(url);
  56. char* const port = lo_url_get_port(url);
  57. fControlData.path = carla_strdup_free(lo_url_get_path(url));
  58. fControlData.target = lo_address_new_with_proto(LO_UDP, host, port);
  59. std::free(host);
  60. std::free(port);
  61. }
  62. if (char* const tmpServerPath = lo_server_get_url(fServer))
  63. {
  64. fServerPath = tmpServerPath;
  65. fServerPath += fName;
  66. std::free(tmpServerPath);
  67. }
  68. #ifdef BUILD_BRIDGE_UI
  69. lo_server_add_method(fServer, nullptr, nullptr, osc_message_handler, this);
  70. #endif
  71. CARLA_ASSERT(fName.isNotEmpty());
  72. CARLA_ASSERT(fServerPath.isNotEmpty());
  73. }
  74. void CarlaBridgeOsc::idle() const
  75. {
  76. if (fServer == nullptr)
  77. return;
  78. for (; lo_server_recv_noblock(fServer, 0) != 0;) {}
  79. }
  80. void CarlaBridgeOsc::close()
  81. {
  82. CARLA_ASSERT(fControlData.source == nullptr); // must never be used
  83. CARLA_ASSERT(fName.isNotEmpty());
  84. CARLA_ASSERT(fServerPath.isNotEmpty());
  85. CARLA_ASSERT(fServer != nullptr);
  86. carla_debug("CarlaBridgeOsc::close()");
  87. fName.clear();
  88. if (fServer != nullptr)
  89. {
  90. lo_server_del_method(fServer, nullptr, nullptr);
  91. lo_server_free(fServer);
  92. fServer = nullptr;
  93. }
  94. fServerPath.clear();
  95. fControlData.clear();
  96. CARLA_ASSERT(fName.isEmpty());
  97. CARLA_ASSERT(fServerPath.isEmpty());
  98. CARLA_ASSERT(fServer == nullptr);
  99. }
  100. #ifdef BUILD_BRIDGE_UI
  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. // UI methods
  133. if (std::strcmp(method, "configure") == 0)
  134. return handleMsgUiConfigure(argc, argv, types);
  135. if (std::strcmp(method, "control") == 0)
  136. return handleMsgUiControl(argc, argv, types);
  137. if (std::strcmp(method, "program") == 0)
  138. return handleMsgUiProgram(argc, argv, types);
  139. if (std::strcmp(method, "midi-program") == 0)
  140. return handleMsgUiMidiProgram(argc, argv, types);
  141. if (std::strcmp(method, "midi") == 0)
  142. return handleMsgUiMidi(argc, argv, types);
  143. if (std::strcmp(method, "sample-rate") == 0)
  144. return 0; // unused
  145. # ifdef BRIDGE_LV2
  146. // LV2 methods
  147. if (std::strcmp(method, "lv2_atom_transfer") == 0)
  148. return handleMsgLv2UiAtomTransfer(argc, argv, types);
  149. if (std::strcmp(method, "lv2_urid_map") == 0)
  150. return handleMsgLv2UiUridMap(argc, argv, types);
  151. # endif
  152. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received unsupported OSC method '%s'", path, method);
  153. return 1;
  154. }
  155. int CarlaBridgeOsc::handleMsgShow()
  156. {
  157. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  158. carla_debug("CarlaBridgeOsc::handleMsgShow()");
  159. fClient->toolkitShow();
  160. return 0;
  161. }
  162. int CarlaBridgeOsc::handleMsgHide()
  163. {
  164. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  165. carla_debug("CarlaBridgeOsc::handleMsgHide()");
  166. fClient->toolkitHide();
  167. return 0;
  168. }
  169. int CarlaBridgeOsc::handleMsgQuit()
  170. {
  171. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  172. carla_debug("CarlaBridgeOsc::handleMsgQuit()");
  173. fClient->toolkitQuit();
  174. return 0;
  175. }
  176. int CarlaBridgeOsc::handleMsgUiConfigure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  177. {
  178. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  179. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  180. carla_debug("CarlaBridgeOsc::handleMsgConfigure()");
  181. // nothing here for now
  182. return 0;
  183. // unused
  184. (void)argv;
  185. }
  186. int CarlaBridgeOsc::handleMsgUiControl(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  187. {
  188. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  189. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  190. carla_debug("CarlaBridgeOsc::handleMsgControl()");
  191. const int32_t index = argv[0]->i;
  192. const float value = argv[1]->f;
  193. CARLA_SAFE_ASSERT_RETURN(index != -1, 1);
  194. fClient->setParameter(index, value);
  195. return 0;
  196. }
  197. int CarlaBridgeOsc::handleMsgUiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  198. {
  199. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  200. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  201. carla_debug("CarlaBridgeOsc::handleMsgProgram()");
  202. const int32_t index = argv[0]->i;
  203. CARLA_SAFE_ASSERT_RETURN(index >= 0, 1);
  204. fClient->setProgram(static_cast<uint32_t>(index));
  205. return 0;
  206. }
  207. int CarlaBridgeOsc::handleMsgUiMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  208. {
  209. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  210. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  211. carla_debug("CarlaBridgeOsc::handleMsgMidiProgram()");
  212. const int32_t bank = argv[0]->i;
  213. const int32_t program = argv[1]->i;
  214. CARLA_SAFE_ASSERT_RETURN(bank >= 0, 1);
  215. CARLA_SAFE_ASSERT_RETURN(program >= 0, 1);
  216. fClient->setMidiProgram(static_cast<uint32_t>(bank), static_cast<uint32_t>(program));
  217. return 0;
  218. }
  219. int CarlaBridgeOsc::handleMsgUiMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  220. {
  221. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  222. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 1);
  223. carla_debug("CarlaBridgeOsc::handleMsgMidi()");
  224. const uint8_t* const data = argv[0]->m;
  225. uint8_t status = data[1];
  226. uint8_t channel = status & 0x0F;
  227. // Fix bad note-off
  228. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  229. status -= 0x10;
  230. if (MIDI_IS_STATUS_NOTE_OFF(status))
  231. {
  232. const uint8_t note = data[2];
  233. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, 1);
  234. fClient->noteOff(channel, note);
  235. }
  236. else if (MIDI_IS_STATUS_NOTE_ON(status))
  237. {
  238. const uint8_t note = data[2];
  239. const uint8_t velo = data[3];
  240. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, 1);
  241. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE, 1);
  242. fClient->noteOn(channel, note, velo);
  243. }
  244. return 0;
  245. }
  246. #endif // BUILD_BRIDGE_UI
  247. // -----------------------------------------------------------------------
  248. CARLA_BRIDGE_END_NAMESPACE