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.

405 lines
11KB

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