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.

310 lines
8.7KB

  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. 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. fName = "ui-";
  46. fName += CarlaString(std::rand() % 99999);
  47. fServer = lo_server_new_with_proto(nullptr, LO_UDP, osc_error_handler);
  48. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr,)
  49. {
  50. char* const host = lo_url_get_hostname(url);
  51. char* const port = lo_url_get_port(url);
  52. fControlData.path = carla_strdup_free(lo_url_get_path(url));
  53. fControlData.target = lo_address_new_with_proto(LO_UDP, host, port);
  54. std::free(host);
  55. std::free(port);
  56. }
  57. if (char* const tmpServerPath = lo_server_get_url(fServer))
  58. {
  59. fServerPath = tmpServerPath;
  60. fServerPath += fName;
  61. std::free(tmpServerPath);
  62. }
  63. lo_server_add_method(fServer, nullptr, nullptr, osc_message_handler, this);
  64. CARLA_ASSERT(fName.isNotEmpty());
  65. CARLA_ASSERT(fServerPath.isNotEmpty());
  66. }
  67. void CarlaBridgeOsc::idle() const
  68. {
  69. if (fServer == nullptr)
  70. return;
  71. for (; lo_server_recv_noblock(fServer, 0) != 0;) {}
  72. }
  73. void CarlaBridgeOsc::close()
  74. {
  75. CARLA_ASSERT(fControlData.source == nullptr); // must never be used
  76. CARLA_ASSERT(fName.isNotEmpty());
  77. CARLA_ASSERT(fServerPath.isNotEmpty());
  78. CARLA_ASSERT(fServer != nullptr);
  79. carla_debug("CarlaBridgeOsc::close()");
  80. fName.clear();
  81. if (fServer != nullptr)
  82. {
  83. lo_server_del_method(fServer, nullptr, nullptr);
  84. lo_server_free(fServer);
  85. fServer = nullptr;
  86. }
  87. fServerPath.clear();
  88. fControlData.clear();
  89. CARLA_ASSERT(fName.isEmpty());
  90. CARLA_ASSERT(fServerPath.isEmpty());
  91. CARLA_ASSERT(fServer == nullptr);
  92. }
  93. // -----------------------------------------------------------------------
  94. int CarlaBridgeOsc::handleMessage(const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  95. {
  96. CARLA_SAFE_ASSERT_RETURN(fName.isNotEmpty(), 1);
  97. CARLA_SAFE_ASSERT_RETURN(fServerPath.isNotEmpty(), 1);
  98. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  99. CARLA_SAFE_ASSERT_RETURN(path != nullptr, 1);
  100. CARLA_SAFE_ASSERT_RETURN(msg != nullptr, 1);
  101. carla_debug("CarlaBridgeOsc::handleMessage(\"%s\", %i, %p, \"%s\", %p)", path, argc, argv, types, msg);
  102. const size_t nameSize(fName.length());
  103. // Check if message is for this client
  104. if (std::strlen(path) <= nameSize || std::strncmp(path+1, fName, nameSize) != 0)
  105. {
  106. carla_stderr("CarlaBridgeOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, fName.buffer());
  107. return 1;
  108. }
  109. // Get method from path
  110. char method[32+1] = { '\0' };
  111. std::strncpy(method, path + (nameSize + 2), 32);
  112. if (method[0] == '\0')
  113. {
  114. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received message without method", path);
  115. return 1;
  116. }
  117. // Common methods
  118. if (std::strcmp(method, "show") == 0)
  119. return handleMsgShow();
  120. if (std::strcmp(method, "hide") == 0)
  121. return handleMsgHide();
  122. if (std::strcmp(method, "quit") == 0)
  123. return handleMsgQuit();
  124. // UI methods
  125. if (std::strcmp(method, "configure") == 0)
  126. return handleMsgConfigure(argc, argv, types);
  127. if (std::strcmp(method, "control") == 0)
  128. return handleMsgControl(argc, argv, types);
  129. if (std::strcmp(method, "program") == 0)
  130. return handleMsgProgram(argc, argv, types);
  131. if (std::strcmp(method, "midi-program") == 0)
  132. return handleMsgMidiProgram(argc, argv, types);
  133. if (std::strcmp(method, "midi") == 0)
  134. return handleMsgMidi(argc, argv, types);
  135. if (std::strcmp(method, "sample-rate") == 0)
  136. return 0; // unused
  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. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received unsupported OSC method '%s'", path, method);
  145. return 1;
  146. }
  147. int CarlaBridgeOsc::handleMsgShow()
  148. {
  149. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  150. carla_debug("CarlaBridgeOsc::handleMsgShow()");
  151. kClient->toolkitShow();
  152. return 0;
  153. }
  154. int CarlaBridgeOsc::handleMsgHide()
  155. {
  156. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  157. carla_debug("CarlaBridgeOsc::handleMsgHide()");
  158. kClient->toolkitHide();
  159. return 0;
  160. }
  161. int CarlaBridgeOsc::handleMsgQuit()
  162. {
  163. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  164. carla_debug("CarlaBridgeOsc::handleMsgQuit()");
  165. kClient->toolkitQuit();
  166. return 0;
  167. }
  168. int CarlaBridgeOsc::handleMsgConfigure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  169. {
  170. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  171. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  172. carla_debug("CarlaBridgeOsc::handleMsgConfigure()");
  173. // nothing here for now
  174. return 0;
  175. // unused
  176. (void)argv;
  177. }
  178. int CarlaBridgeOsc::handleMsgControl(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  179. {
  180. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  181. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  182. carla_debug("CarlaBridgeOsc::handleMsgControl()");
  183. const int32_t index = argv[0]->i;
  184. const float value = argv[1]->f;
  185. CARLA_SAFE_ASSERT_RETURN(index != -1, 1);
  186. kClient->setParameter(index, value);
  187. return 0;
  188. }
  189. int CarlaBridgeOsc::handleMsgProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  190. {
  191. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  192. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  193. carla_debug("CarlaBridgeOsc::handleMsgProgram()");
  194. const int32_t index = argv[0]->i;
  195. CARLA_SAFE_ASSERT_RETURN(index >= 0, 1);
  196. kClient->setProgram(static_cast<uint32_t>(index));
  197. return 0;
  198. }
  199. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  200. {
  201. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  202. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  203. carla_debug("CarlaBridgeOsc::handleMsgMidiProgram()");
  204. const int32_t bank = argv[0]->i;
  205. const int32_t program = argv[1]->i;
  206. CARLA_SAFE_ASSERT_RETURN(bank >= 0, 1);
  207. CARLA_SAFE_ASSERT_RETURN(program >= 0, 1);
  208. kClient->setMidiProgram(static_cast<uint32_t>(bank), static_cast<uint32_t>(program));
  209. return 0;
  210. }
  211. int CarlaBridgeOsc::handleMsgMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  212. {
  213. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  214. CARLA_SAFE_ASSERT_RETURN(kClient != nullptr, 1);
  215. carla_debug("CarlaBridgeOsc::handleMsgMidi()");
  216. const uint8_t* const data = argv[0]->m;
  217. uint8_t status = data[1];
  218. uint8_t channel = status & 0x0F;
  219. // Fix bad note-off
  220. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  221. status -= 0x10;
  222. if (MIDI_IS_STATUS_NOTE_OFF(status))
  223. {
  224. const uint8_t note = data[2];
  225. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, 1);
  226. kClient->noteOff(channel, note);
  227. }
  228. else if (MIDI_IS_STATUS_NOTE_ON(status))
  229. {
  230. const uint8_t note = data[2];
  231. const uint8_t velo = data[3];
  232. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, 1);
  233. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE, 1);
  234. kClient->noteOn(channel, note, velo);
  235. }
  236. return 0;
  237. }
  238. // -----------------------------------------------------------------------
  239. CARLA_BRIDGE_END_NAMESPACE