Collection of tools useful for audio production
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.

412 lines
10KB

  1. /*
  2. * Carla Bridge OSC
  3. * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING 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. free(host);
  72. qCritical("CarlaBridgeOsc::init(\"%s\") - failed to get url path", url);
  73. return false;
  74. }
  75. if (! port)
  76. {
  77. free(host);
  78. free(path);
  79. qCritical("CarlaBridgeOsc::init(\"%s\") - failed to get url port", url);
  80. return false;
  81. }
  82. m_controlData.path = path;
  83. m_controlData.target = lo_address_new_with_proto(LO_TCP, host, port);
  84. free(host);
  85. free(port);
  86. if (! m_controlData.target)
  87. {
  88. qCritical("CarlaBridgeOsc::init(\"%s\") - failed to get new url address for host '%s' and port '%s'", url, host, port);
  89. return false;
  90. }
  91. m_server = lo_server_new_with_proto(nullptr, LO_TCP, osc_error_handler);
  92. if (! m_server)
  93. {
  94. qCritical("CarlaBridgeOsc::init(\"%s\") - failed to create new OSC server", url);
  95. return false;
  96. }
  97. if (char* const serverUrl = lo_server_get_url(m_server))
  98. {
  99. m_serverPath = strdup(QString("%1%2").arg(serverUrl).arg(m_name).toUtf8().constData());
  100. free(serverUrl);
  101. }
  102. else
  103. m_serverPath = strdup(QString("%1carla-bridge").arg(serverUrl).toUtf8().constData());
  104. lo_server_add_method(m_server, nullptr, nullptr, osc_message_handler, this);
  105. return true;
  106. }
  107. void CarlaBridgeOsc::idle()
  108. {
  109. CARLA_ASSERT(m_server);
  110. if (m_server)
  111. {
  112. while (lo_server_recv_noblock(m_server, 0) != 0) {}
  113. }
  114. }
  115. void CarlaBridgeOsc::close()
  116. {
  117. qDebug("CarlaBridgeOsc::close()");
  118. CARLA_ASSERT(m_name);
  119. CARLA_ASSERT(m_server);
  120. CARLA_ASSERT(m_serverPath);
  121. m_nameSize = 0;
  122. if (m_name)
  123. {
  124. free(m_name);
  125. m_name = nullptr;
  126. }
  127. if (m_server)
  128. {
  129. lo_server_del_method(m_server, nullptr, nullptr);
  130. lo_server_free(m_server);
  131. m_server = nullptr;
  132. }
  133. if (m_serverPath)
  134. {
  135. free(m_serverPath);
  136. m_serverPath = nullptr;
  137. }
  138. m_controlData.free();
  139. }
  140. // -----------------------------------------------------------------------
  141. int CarlaBridgeOsc::handleMessage(const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  142. {
  143. qDebug("CarlaBridgeOsc::handleMessage(\"%s\", %i, %p, \"%s\", %p)", path, argc, argv, types, msg);
  144. CARLA_ASSERT(m_name);
  145. CARLA_ASSERT(m_server);
  146. CARLA_ASSERT(m_serverPath);
  147. CARLA_ASSERT(path);
  148. if (! path)
  149. {
  150. qCritical("CarlaBridgeOsc::handleMessage() - got invalid path");
  151. return 1;
  152. }
  153. if (! (m_name && m_server && m_serverPath))
  154. {
  155. qCritical("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received message but client is offline", path);
  156. return 1;
  157. }
  158. if (strlen(path) <= m_nameSize || strncmp(path+1, m_name, m_nameSize) != 0)
  159. {
  160. qWarning("CarlaBridgeOsc::handleMessage() - message not for this client: '%s' != '/%s/'", path, m_name);
  161. return 1;
  162. }
  163. char method[32] = { 0 };
  164. strncpy(method, path + (m_nameSize + 2), 31);
  165. if (method[0] == '\0')
  166. {
  167. qWarning("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received message without method", path);
  168. return 1;
  169. }
  170. // Common OSC methods
  171. if (strcmp(method, "configure") == 0)
  172. return handleMsgConfigure(argc, argv, types);
  173. if (strcmp(method, "control") == 0)
  174. return handleMsgControl(argc, argv, types);
  175. if (strcmp(method, "program") == 0)
  176. return handleMsgProgram(argc, argv, types);
  177. if (strcmp(method, "midi_program") == 0)
  178. return handleMsgMidiProgram(argc, argv, types);
  179. if (strcmp(method, "midi") == 0)
  180. return handleMsgMidi(argc, argv, types);
  181. if (strcmp(method, "sample-rate") == 0)
  182. return 0; // unused
  183. if (strcmp(method, "show") == 0)
  184. return handleMsgShow();
  185. if (strcmp(method, "hide") == 0)
  186. return handleMsgHide();
  187. if (strcmp(method, "quit") == 0)
  188. return handleMsgQuit();
  189. #ifdef BRIDGE_LV2
  190. // LV2 UI methods
  191. if (strcmp(method, "lv2_atom_transfer") == 0)
  192. return handleMsgLv2TransferAtom(argc, argv, types);
  193. if (strcmp(method, "lv2_event_transfer") == 0)
  194. return handleMsgLv2TransferEvent(argc, argv, types);
  195. #endif
  196. #if 0
  197. // TODO
  198. else if (strcmp(method, "set_parameter_midi_channel") == 0)
  199. return osc_set_parameter_midi_channel_handler(argv);
  200. else if (strcmp(method, "set_parameter_midi_cc") == 0)
  201. return osc_set_parameter_midi_channel_handler(argv);
  202. #endif
  203. qWarning("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received unsupported OSC method '%s'", path, method);
  204. return 1;
  205. }
  206. int CarlaBridgeOsc::handleMsgConfigure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  207. {
  208. qDebug("CarlaBridgeOsc::handleMsgConfigure()");
  209. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  210. if (! client)
  211. return 1;
  212. #ifdef BUILD_BRIDGE_PLUGIN
  213. const char* const key = (const char*)&argv[0]->s;
  214. const char* const value = (const char*)&argv[1]->s;
  215. if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SAVE_NOW) == 0)
  216. {
  217. client->saveNow();
  218. }
  219. else if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SET_CHUNK) == 0)
  220. {
  221. client->setChunkData(value);
  222. }
  223. else if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SET_CUSTOM) == 0)
  224. {
  225. QStringList vList = QString(value).split("ยท", QString::KeepEmptyParts);
  226. if (vList.size() == 3)
  227. {
  228. const char* const cType = vList.at(0).toUtf8().constData();
  229. const char* const cKey = vList.at(1).toUtf8().constData();
  230. const char* const cValue = vList.at(2).toUtf8().constData();
  231. client->setCustomData(cType, cKey, cValue);
  232. }
  233. }
  234. #else
  235. Q_UNUSED(argv);
  236. #endif
  237. return 0;
  238. }
  239. int CarlaBridgeOsc::handleMsgControl(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  240. {
  241. qDebug("CarlaBridgeOsc::handleMsgControl()");
  242. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  243. if (! client)
  244. return 1;
  245. const int32_t index = argv[0]->i;
  246. const float value = argv[1]->f;
  247. client->setParameter(index, value);
  248. return 0;
  249. }
  250. int CarlaBridgeOsc::handleMsgProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  251. {
  252. qDebug("CarlaBridgeOsc::handleMsgProgram()");
  253. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  254. if (! client)
  255. return 1;
  256. const int32_t index = argv[0]->i;
  257. client->setProgram(index);
  258. return 0;
  259. }
  260. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  261. {
  262. qDebug("CarlaBridgeOsc::handleMsgMidiProgram()");
  263. #ifdef BUILD_BRIDGE_PLUGIN
  264. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  265. #else
  266. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  267. #endif
  268. if (! client)
  269. return 1;
  270. #ifdef BUILD_BRIDGE_PLUGIN
  271. const int32_t index = argv[0]->i;
  272. client->setMidiProgram(index);
  273. #else
  274. const int32_t bank = argv[0]->i;
  275. const int32_t program = argv[1]->i;
  276. client->setMidiProgram(bank, program);
  277. #endif
  278. return 0;
  279. }
  280. int CarlaBridgeOsc::handleMsgMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  281. {
  282. qDebug("CarlaBridgeOsc::handleMsgMidi()");
  283. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  284. if (! client)
  285. return 1;
  286. const uint8_t* const mdata = argv[0]->m;
  287. const uint8_t data[4] = { mdata[0], mdata[1], mdata[2], mdata[3] };
  288. uint8_t status = data[1];
  289. uint8_t channel = status & 0x0F;
  290. // Fix bad note-off
  291. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  292. status -= 0x10;
  293. if (MIDI_IS_STATUS_NOTE_OFF(status))
  294. {
  295. uint8_t note = data[2];
  296. client->noteOff(channel, note);
  297. }
  298. else if (MIDI_IS_STATUS_NOTE_ON(status))
  299. {
  300. uint8_t note = data[2];
  301. uint8_t velo = data[3];
  302. client->noteOn(channel, note, velo);
  303. }
  304. return 0;
  305. }
  306. int CarlaBridgeOsc::handleMsgShow()
  307. {
  308. qDebug("CarlaBridgeOsc::handleMsgShow()");
  309. if (! client)
  310. return 1;
  311. client->toolkitShow();
  312. return 0;
  313. }
  314. int CarlaBridgeOsc::handleMsgHide()
  315. {
  316. qDebug("CarlaBridgeOsc::handleMsgHide()");
  317. if (! client)
  318. return 1;
  319. client->toolkitHide();
  320. return 0;
  321. }
  322. int CarlaBridgeOsc::handleMsgQuit()
  323. {
  324. qDebug("CarlaBridgeOsc::handleMsgQuit()");
  325. if (! client)
  326. return 1;
  327. client->toolkitQuit();
  328. return 0;
  329. }
  330. CARLA_BRIDGE_END_NAMESPACE