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.

411 lines
10.0KB

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