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.

338 lines
9.4KB

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