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.

386 lines
10KB

  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 GPL.txt file
  16. */
  17. #include "CarlaBridgeOsc.hpp"
  18. #include "CarlaBridgeClient.hpp"
  19. #include "CarlaMIDI.h"
  20. #include "CarlaUtils.hpp"
  21. CARLA_BRIDGE_START_NAMESPACE
  22. // -----------------------------------------------------------------------
  23. CarlaBridgeOsc::CarlaBridgeOsc(CarlaBridgeClient* const client)
  24. : kClient(client),
  25. fServer(nullptr)
  26. {
  27. carla_debug("CarlaBridgeOsc::CarlaBridgeOsc(%p)", client);
  28. CARLA_ASSERT(client != nullptr);
  29. }
  30. CarlaBridgeOsc::~CarlaBridgeOsc()
  31. {
  32. carla_debug("CarlaBridgeOsc::~CarlaBridgeOsc()");
  33. CARLA_ASSERT(fName.isEmpty());
  34. CARLA_ASSERT(fServerPath.isEmpty());
  35. CARLA_ASSERT(fServer == nullptr);
  36. }
  37. void CarlaBridgeOsc::init(const char* const url)
  38. {
  39. carla_debug("CarlaBridgeOsc::init(\"%s\")", url);
  40. CARLA_ASSERT(fName.isEmpty());
  41. CARLA_ASSERT(fServerPath.isEmpty());
  42. CARLA_ASSERT(fServer == nullptr);
  43. CARLA_ASSERT(url != nullptr);
  44. #ifdef BUILD_BRIDGE_PLUGIN
  45. fName = "carla-bridge-plugin";
  46. #else
  47. fName = "carla-bridge-ui";
  48. #endif
  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_TCP, host, port);
  54. std::free(host);
  55. std::free(port);
  56. }
  57. fServer = lo_server_new_with_proto(nullptr, LO_TCP, osc_error_handler);
  58. if (fServer != nullptr)
  59. {
  60. if (char* const tmpServerPath = lo_server_get_url(fServer))
  61. {
  62. fServerPath = tmpServerPath;
  63. fServerPath += fName;
  64. std::free(tmpServerPath);
  65. }
  66. lo_server_add_method(fServer, nullptr, nullptr, osc_message_handler, this);
  67. }
  68. CARLA_ASSERT(fName.isNotEmpty());
  69. CARLA_ASSERT(fServerPath.isNotEmpty());
  70. CARLA_ASSERT(fServer != nullptr);
  71. }
  72. void CarlaBridgeOsc::idle()
  73. {
  74. if (fServer != nullptr)
  75. {
  76. while (lo_server_recv_noblock(fServer, 0) != 0) {}
  77. }
  78. }
  79. void CarlaBridgeOsc::close()
  80. {
  81. carla_debug("CarlaBridgeOsc::close()");
  82. CARLA_ASSERT(fName.isNotEmpty());
  83. CARLA_ASSERT(fServerPath.isNotEmpty());
  84. CARLA_ASSERT(fServer != nullptr);
  85. fName.clear();
  86. if (fServer != nullptr)
  87. {
  88. lo_server_del_method(fServer, nullptr, nullptr);
  89. lo_server_free(fServer);
  90. fServer = nullptr;
  91. }
  92. fServerPath.clear();
  93. fControlData.free();
  94. CARLA_ASSERT(fName.isEmpty());
  95. CARLA_ASSERT(fServerPath.isEmpty());
  96. CARLA_ASSERT(fServer == nullptr);
  97. }
  98. // -----------------------------------------------------------------------
  99. int CarlaBridgeOsc::handleMessage(const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  100. {
  101. carla_debug("CarlaBridgeOsc::handleMessage(\"%s\", %i, %p, \"%s\", %p)", path, argc, argv, types, msg);
  102. CARLA_ASSERT(fName.isNotEmpty());
  103. CARLA_ASSERT(fServerPath.isNotEmpty());
  104. CARLA_ASSERT(fServer != nullptr);
  105. CARLA_ASSERT(path != nullptr);
  106. if (path == nullptr)
  107. {
  108. carla_stderr("CarlaBridgeOsc::handleMessage() - got invalid path");
  109. return 1;
  110. }
  111. if (fName.isEmpty())
  112. {
  113. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received message but client is offline", path);
  114. return 1;
  115. }
  116. const size_t nameSize = fName.length();
  117. // Check if message is for this client
  118. if (std::strlen(path) <= nameSize || std::strncmp(path+1, (const char*)fName, nameSize) != 0)
  119. {
  120. carla_stderr("CarlaBridgeOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, (const char*)fName);
  121. return 1;
  122. }
  123. // Get method from path
  124. char method[32] = { 0 };
  125. std::strncpy(method, path + (nameSize + 2), 31);
  126. if (method[0] == '\0')
  127. {
  128. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received message without method", path);
  129. return 1;
  130. }
  131. // Common OSC methods
  132. if (std::strcmp(method, "configure") == 0)
  133. return handleMsgConfigure(argc, argv, types);
  134. if (std::strcmp(method, "control") == 0)
  135. return handleMsgControl(argc, argv, types);
  136. #ifndef BUILD_BRIDGE_PLUGIN
  137. if (std::strcmp(method, "program") == 0)
  138. return handleMsgProgram(argc, argv, types);
  139. if (std::strcmp(method, "midi-program") == 0)
  140. return handleMsgMidiProgram(argc, argv, types);
  141. if (std::strcmp(method, "midi") == 0)
  142. return handleMsgMidi(argc, argv, types);
  143. #endif
  144. if (std::strcmp(method, "sample-rate") == 0)
  145. return 0; // unused
  146. if (std::strcmp(method, "show") == 0)
  147. return handleMsgShow();
  148. if (std::strcmp(method, "hide") == 0)
  149. return handleMsgHide();
  150. if (std::strcmp(method, "quit") == 0)
  151. return handleMsgQuit();
  152. #ifdef BRIDGE_LV2
  153. // LV2 UI methods
  154. if (std::strcmp(method, "lv2_atom_transfer") == 0)
  155. return handleMsgLv2TransferAtom(argc, argv, types);
  156. if (std::strcmp(method, "lv2_event_transfer") == 0)
  157. return handleMsgLv2TransferEvent(argc, argv, types);
  158. #endif
  159. #ifdef BUILD_BRIDGE_PLUGIN
  160. // Plugin methods
  161. if (std::strcmp(method, "plugin_save_now") == 0)
  162. return handleMsgPluginSaveNow();
  163. if (std::strcmp(method, "plugin_set_chunk") == 0)
  164. return handleMsgPluginSetChunk(argc, argv, types);
  165. if (std::strcmp(method, "plugin_set_custom_data") == 0)
  166. return handleMsgPluginSetCustomData(argc, argv, types);
  167. #if 0
  168. // TODO
  169. if (std::strcmp(method, "set_parameter_midi_channel") == 0)
  170. return osc_set_parameter_midi_channel_handler(argv);
  171. if (std::strcmp(method, "set_parameter_midi_cc") == 0)
  172. return osc_set_parameter_midi_channel_handler(argv);
  173. #endif
  174. #endif
  175. carla_stderr("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received unsupported OSC method '%s'", path, method);
  176. return 1;
  177. }
  178. int CarlaBridgeOsc::handleMsgConfigure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  179. {
  180. carla_debug("CarlaBridgeOsc::handleMsgConfigure()");
  181. CARLA_ASSERT(kClient != nullptr);
  182. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  183. if (kClient == nullptr)
  184. return 1;
  185. // nothing here for now
  186. return 0;
  187. // unused
  188. (void)argv;
  189. }
  190. int CarlaBridgeOsc::handleMsgControl(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  191. {
  192. carla_debug("CarlaBridgeOsc::handleMsgControl()");
  193. CARLA_ASSERT(kClient != nullptr);
  194. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  195. if (kClient == nullptr)
  196. return 1;
  197. const int32_t index = argv[0]->i;
  198. const float value = argv[1]->f;
  199. CARLA_SAFE_ASSERT_INT(index != -1, index);
  200. if (index == -1)
  201. return 1;
  202. kClient->setParameter(index, value);
  203. return 0;
  204. }
  205. #ifndef BUILD_BRIDGE_PLUGIN
  206. int CarlaBridgeOsc::handleMsgProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  207. {
  208. carla_debug("CarlaBridgeOsc::handleMsgProgram()");
  209. CARLA_ASSERT(kClient != nullptr);
  210. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  211. if (kClient == nullptr)
  212. return 1;
  213. const int32_t index = argv[0]->i;
  214. CARLA_SAFE_ASSERT_INT(index >= 0, index);
  215. if (index < 0)
  216. return 1;
  217. kClient->setProgram(static_cast<uint32_t>(index));
  218. return 0;
  219. }
  220. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  221. {
  222. carla_debug("CarlaBridgeOsc::handleMsgMidiProgram()");
  223. CARLA_ASSERT(kClient != nullptr);
  224. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  225. if (kClient == nullptr)
  226. return 1;
  227. const int32_t bank = argv[0]->i;
  228. const int32_t program = argv[1]->i;
  229. CARLA_SAFE_ASSERT_INT(bank >= 0, bank);
  230. CARLA_SAFE_ASSERT_INT(program >= 0, program);
  231. if (bank < 0)
  232. return 1;
  233. if (program < 0)
  234. return 1;
  235. kClient->setMidiProgram(static_cast<uint32_t>(bank), static_cast<uint32_t>(program));
  236. return 0;
  237. }
  238. int CarlaBridgeOsc::handleMsgMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  239. {
  240. carla_debug("CarlaBridgeOsc::handleMsgMidi()");
  241. CARLA_ASSERT(kClient != nullptr);
  242. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  243. if (kClient == nullptr)
  244. return 1;
  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_INT(note < MAX_MIDI_NOTE, note);
  255. if (note >= MAX_MIDI_NOTE)
  256. return 1;
  257. kClient->noteOff(channel, note);
  258. }
  259. else if (MIDI_IS_STATUS_NOTE_ON(status))
  260. {
  261. const uint8_t note = data[2];
  262. const uint8_t velo = data[3];
  263. CARLA_SAFE_ASSERT_INT(note < MAX_MIDI_NOTE, note);
  264. CARLA_SAFE_ASSERT_INT(velo < MAX_MIDI_VALUE, velo);
  265. if (note >= MAX_MIDI_NOTE)
  266. return 1;
  267. if (velo >= MAX_MIDI_VALUE)
  268. return 1;
  269. kClient->noteOn(channel, note, velo);
  270. }
  271. return 0;
  272. }
  273. #endif
  274. #ifdef BUILD_BRIDGE_UI
  275. int CarlaBridgeOsc::handleMsgShow()
  276. {
  277. carla_debug("CarlaBridgeOsc::handleMsgShow()");
  278. CARLA_ASSERT(kClient != nullptr);
  279. if (kClient == nullptr)
  280. return 1;
  281. kClient->toolkitShow();
  282. return 0;
  283. }
  284. int CarlaBridgeOsc::handleMsgHide()
  285. {
  286. carla_debug("CarlaBridgeOsc::handleMsgHide()");
  287. CARLA_ASSERT(kClient != nullptr);
  288. if (kClient == nullptr)
  289. return 1;
  290. kClient->toolkitHide();
  291. return 0;
  292. }
  293. int CarlaBridgeOsc::handleMsgQuit()
  294. {
  295. carla_debug("CarlaBridgeOsc::handleMsgQuit()");
  296. CARLA_ASSERT(kClient != nullptr);
  297. if (kClient == nullptr)
  298. return 1;
  299. kClient->toolkitQuit();
  300. return 0;
  301. }
  302. #endif
  303. CARLA_BRIDGE_END_NAMESPACE