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.

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