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.

410 lines
9.9KB

  1. /*
  2. * Carla Bridge OSC
  3. * Copyright (C) 2011-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. #ifdef BUILD_BRIDGE_PLUGIN
  197. // Plugin methods
  198. if (strcmp(method, "plugin_save_now") == 0)
  199. return handleMsgPluginSaveNow();
  200. if (strcmp(method, "plugin_set_chunk") == 0)
  201. return handleMsgPluginSetChunk(argc, argv, types);
  202. if (strcmp(method, "plugin_set_custom_data") == 0)
  203. return handleMsgPluginSetCustomData(argc, argv, types);
  204. #endif
  205. #if 0
  206. // TODO
  207. else if (strcmp(method, "set_parameter_midi_channel") == 0)
  208. return osc_set_parameter_midi_channel_handler(argv);
  209. else if (strcmp(method, "set_parameter_midi_cc") == 0)
  210. return osc_set_parameter_midi_channel_handler(argv);
  211. #endif
  212. qWarning("CarlaBridgeOsc::handleMessage(\"%s\", ...) - received unsupported OSC method '%s'", path, method);
  213. return 1;
  214. }
  215. int CarlaBridgeOsc::handleMsgConfigure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  216. {
  217. qDebug("CarlaBridgeOsc::handleMsgConfigure()");
  218. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  219. if (! client)
  220. return 1;
  221. // nothing here for now
  222. return 0;
  223. Q_UNUSED(argv);
  224. }
  225. int CarlaBridgeOsc::handleMsgControl(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  226. {
  227. qDebug("CarlaBridgeOsc::handleMsgControl()");
  228. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  229. if (! client)
  230. return 1;
  231. const int32_t index = argv[0]->i;
  232. const float value = argv[1]->f;
  233. client->setParameter(index, value);
  234. return 0;
  235. }
  236. int CarlaBridgeOsc::handleMsgProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  237. {
  238. qDebug("CarlaBridgeOsc::handleMsgProgram()");
  239. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  240. if (! client)
  241. return 1;
  242. const int32_t index = argv[0]->i;
  243. client->setProgram(index);
  244. return 0;
  245. }
  246. #ifdef BUILD_BRIDGE_PLUGIN
  247. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  248. {
  249. qDebug("CarlaBridgeOsc::handleMsgMidiProgram()");
  250. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  251. if (! client)
  252. return 1;
  253. const int32_t index = argv[0]->i;
  254. client->setMidiProgram(index);
  255. return 0;
  256. }
  257. #else
  258. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  259. {
  260. qDebug("CarlaBridgeOsc::handleMsgMidiProgram()");
  261. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  262. if (! client)
  263. return 1;
  264. const int32_t bank = argv[0]->i;
  265. const int32_t program = argv[1]->i;
  266. client->setMidiProgram(bank, program);
  267. return 0;
  268. }
  269. #endif
  270. int CarlaBridgeOsc::handleMsgMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  271. {
  272. qDebug("CarlaBridgeOsc::handleMsgMidi()");
  273. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  274. if (! client)
  275. return 1;
  276. const uint8_t* const mdata = argv[0]->m;
  277. const uint8_t data[4] = { mdata[0], mdata[1], mdata[2], mdata[3] };
  278. uint8_t status = data[1];
  279. uint8_t channel = status & 0x0F;
  280. // Fix bad note-off
  281. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  282. status -= 0x10;
  283. if (MIDI_IS_STATUS_NOTE_OFF(status))
  284. {
  285. const uint8_t note = data[2];
  286. client->noteOff(channel, note);
  287. }
  288. else if (MIDI_IS_STATUS_NOTE_ON(status))
  289. {
  290. const uint8_t note = data[2];
  291. const uint8_t velo = data[3];
  292. client->noteOn(channel, note, velo);
  293. }
  294. return 0;
  295. }
  296. int CarlaBridgeOsc::handleMsgShow()
  297. {
  298. qDebug("CarlaBridgeOsc::handleMsgShow()");
  299. if (! client)
  300. return 1;
  301. client->toolkitShow();
  302. return 0;
  303. }
  304. int CarlaBridgeOsc::handleMsgHide()
  305. {
  306. qDebug("CarlaBridgeOsc::handleMsgHide()");
  307. if (! client)
  308. return 1;
  309. client->toolkitHide();
  310. return 0;
  311. }
  312. int CarlaBridgeOsc::handleMsgQuit()
  313. {
  314. qDebug("CarlaBridgeOsc::handleMsgQuit()");
  315. if (! client)
  316. return 1;
  317. client->toolkitQuit();
  318. return 0;
  319. }
  320. CARLA_BRIDGE_END_NAMESPACE