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.

338 lines
8.8KB

  1. /*
  2. * Carla Plugin bridge code
  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.h"
  18. #include "carla_bridge_client.h"
  19. #include "carla_midi.h"
  20. #include <QtCore/QString>
  21. #include <QtCore/QStringList>
  22. CARLA_BRIDGE_START_NAMESPACE
  23. unsigned int uintMin(unsigned int value1, unsigned int value2)
  24. {
  25. return value1 < value2 ? value1 : value2;
  26. }
  27. void osc_error_handler(const int num, const char* const msg, const char* const path)
  28. {
  29. qCritical("osc_error_handler(%i, \"%s\", \"%s\")", num, msg, path);
  30. }
  31. // -----------------------------------------------------------------------
  32. CarlaBridgeOsc::CarlaBridgeOsc(CarlaClient* const client_, const char* const name)
  33. : client(client_)
  34. {
  35. qDebug("CarlaBridgeOsc::CarlaOsc(%p, \"%s\")", client, name);
  36. CARLA_ASSERT(client);
  37. CARLA_ASSERT(name);
  38. m_server = nullptr;
  39. m_serverPath = nullptr;
  40. m_controlData.path = nullptr;
  41. m_controlData.source = nullptr; // unused
  42. m_controlData.target = nullptr;
  43. m_name = strdup(name ? name : "");
  44. m_nameSize = strlen(m_name);
  45. }
  46. CarlaBridgeOsc::~CarlaBridgeOsc()
  47. {
  48. qDebug("CarlaBridgeOsc::~CarlaOsc()");
  49. if (m_name)
  50. free(m_name);
  51. }
  52. bool CarlaBridgeOsc::init(const char* const url)
  53. {
  54. qDebug("CarlaBridgeOsc::init(\"%s\")", url);
  55. CARLA_ASSERT(! m_server);
  56. CARLA_ASSERT(! m_serverPath);
  57. CARLA_ASSERT(url);
  58. char* host = lo_url_get_hostname(url);
  59. char* port = lo_url_get_port(url);
  60. m_controlData.path = lo_url_get_path(url);
  61. m_controlData.target = lo_address_new_with_proto(LO_TCP, host, port);
  62. free(host);
  63. free(port);
  64. if (! m_controlData.path)
  65. {
  66. qCritical("CarlaBridgeOsc::init(\"%s\") - failed to init OSC", url);
  67. return false;
  68. }
  69. // create new OSC thread
  70. m_server = lo_server_new_with_proto(nullptr, LO_TCP, osc_error_handler);
  71. // get our full OSC server path
  72. char* const threadPath = lo_server_get_url(m_server);
  73. m_serverPath = strdup(QString("%1%2").arg(threadPath).arg(m_name).toUtf8().constData());
  74. free(threadPath);
  75. // register message handler
  76. lo_server_add_method(m_server, nullptr, nullptr, osc_message_handler, this);
  77. return true;
  78. }
  79. void CarlaBridgeOsc::close()
  80. {
  81. qDebug("CarlaBridgeOsc::close()");
  82. CARLA_ASSERT(m_server);
  83. CARLA_ASSERT(m_serverPath);
  84. osc_clear_data(&m_controlData);
  85. lo_server_del_method(m_server, nullptr, nullptr);
  86. lo_server_free(m_server);
  87. free((void*)m_serverPath);
  88. m_serverPath = nullptr;
  89. }
  90. // -----------------------------------------------------------------------
  91. int CarlaBridgeOsc::handleMessage(const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  92. {
  93. qDebug("CarlaBridgeOsc::handleMessage(\"%s\", %i, %p, \"%s\", %p)", path, argc, argv, types, msg);
  94. CARLA_ASSERT(m_server);
  95. CARLA_ASSERT(m_serverPath);
  96. CARLA_ASSERT(path);
  97. // Check if message is for this client
  98. if ((! path) || strlen(path) <= m_nameSize || strncmp(path+1, m_name, m_nameSize) != 0)
  99. {
  100. qWarning("CarlaBridgeOsc::handleMessage() - message not for this client: '%s' != '/%s/'", path, m_name);
  101. return 1;
  102. }
  103. char method[32] = { 0 };
  104. memcpy(method, path + (m_nameSize + 1), uintMin(strlen(path), 32));
  105. if (method[0] == 0)
  106. return 1;
  107. // Common OSC methods
  108. if (strcmp(method, "/configure") == 0)
  109. return handleMsgConfigure(argc, argv, types);
  110. if (strcmp(method, "/control") == 0)
  111. return handleMsgControl(argc, argv, types);
  112. if (strcmp(method, "/program") == 0)
  113. return handleMsgProgram(argc, argv, types);
  114. if (strcmp(method, "/midi_program") == 0)
  115. return handleMsgMidiProgram(argc, argv, types);
  116. if (strcmp(method, "/midi") == 0)
  117. return handleMsgMidi(argc, argv, types);
  118. if (strcmp(method, "/sample-rate") == 0)
  119. return 0; // unused
  120. if (strcmp(method, "/show") == 0)
  121. return handleMsgShow();
  122. if (strcmp(method, "/hide") == 0)
  123. return handleMsgHide();
  124. if (strcmp(method, "/quit") == 0)
  125. return handleMsgQuit();
  126. #ifdef BRIDGE_LV2
  127. if (strcmp(method, "/lv2_atom_transfer") == 0)
  128. return handleMsgLv2TransferAtom(argc, argv, types);
  129. if (strcmp(method, "/lv2_event_transfer") == 0)
  130. return handleMsgLv2TransferEvent(argc, argv, types);
  131. #endif
  132. #if 0
  133. else if (strcmp(method, "set_parameter_midi_channel") == 0)
  134. return osc_set_parameter_midi_channel_handler(argv);
  135. else if (strcmp(method, "set_parameter_midi_cc") == 0)
  136. return osc_set_parameter_midi_channel_handler(argv);
  137. #endif
  138. qWarning("CarlaBridgeOsc::handleMessage(\"%s\", ...) - got unsupported OSC method '%s'", path, method);
  139. return 1;
  140. }
  141. int CarlaBridgeOsc::handleMsgConfigure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  142. {
  143. qDebug("CarlaBridgeOsc::handleMsgConfigure()");
  144. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  145. if (! client)
  146. return 1;
  147. #ifdef BUILD_BRIDGE_PLUGIN
  148. const char* const key = (const char*)&argv[0]->s;
  149. const char* const value = (const char*)&argv[1]->s;
  150. if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SAVE_NOW) == 0)
  151. {
  152. client->quequeMessage(MESSAGE_SAVE_NOW, 0, 0, 0.0);
  153. }
  154. else if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SET_CHUNK) == 0)
  155. {
  156. client->setChunkData(value);
  157. }
  158. else if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SET_CUSTOM) == 0)
  159. {
  160. QStringList vList = QString(value).split("ยท", QString::KeepEmptyParts);
  161. if (vList.size() == 3)
  162. {
  163. const char* const cType = vList.at(0).toUtf8().constData();
  164. const char* const cKey = vList.at(1).toUtf8().constData();
  165. const char* const cValue = vList.at(2).toUtf8().constData();
  166. client->setCustomData(cType, cKey, cValue);
  167. }
  168. }
  169. #else
  170. Q_UNUSED(argv);
  171. #endif
  172. return 0;
  173. }
  174. int CarlaBridgeOsc::handleMsgControl(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  175. {
  176. qDebug("CarlaBridgeOsc::handleMsgControl()");
  177. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  178. if (! client)
  179. return 1;
  180. const int32_t index = argv[0]->i;
  181. const float value = argv[1]->f;
  182. client->setParameter(index, value);
  183. return 0;
  184. }
  185. int CarlaBridgeOsc::handleMsgProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  186. {
  187. qDebug("CarlaBridgeOsc::handleMsgProgram()");
  188. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  189. if (! client)
  190. return 1;
  191. const int32_t index = argv[0]->i;
  192. client->setProgram(index);
  193. return 0;
  194. }
  195. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  196. {
  197. qDebug("CarlaBridgeOsc::handleMsgMidiProgram()");
  198. #ifdef BUILD_BRIDGE_PLUGIN
  199. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  200. #else
  201. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  202. #endif
  203. if (! client)
  204. return 1;
  205. #ifdef BUILD_BRIDGE_PLUGIN
  206. const int32_t index = argv[0]->i;
  207. client->setProgram(index);
  208. #else
  209. const int32_t bank = argv[0]->i;
  210. const int32_t program = argv[1]->i;
  211. client->setMidiProgram(bank, program);
  212. #endif
  213. return 0;
  214. }
  215. int CarlaBridgeOsc::handleMsgMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  216. {
  217. qDebug("CarlaBridgeOsc::handleMsgMidi()");
  218. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  219. if (! client)
  220. return 1;
  221. const uint8_t* const mdata = argv[0]->m;
  222. const uint8_t data[4] = { mdata[0], mdata[1], mdata[2], mdata[3] };
  223. uint8_t status = data[1];
  224. uint8_t channel = status & 0x0F;
  225. // Fix bad note-off
  226. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  227. status -= 0x10;
  228. if (MIDI_IS_STATUS_NOTE_OFF(status))
  229. {
  230. uint8_t note = data[2];
  231. client->noteOff(channel, note);
  232. }
  233. else if (MIDI_IS_STATUS_NOTE_ON(status))
  234. {
  235. uint8_t note = data[2];
  236. uint8_t velo = data[3];
  237. client->noteOn(channel, note, velo);
  238. }
  239. return 0;
  240. }
  241. int CarlaBridgeOsc::handleMsgShow()
  242. {
  243. qDebug("CarlaBridgeOsc::handleMsgShow()");
  244. if (! client)
  245. return 1;
  246. client->toolkitShow();
  247. return 0;
  248. }
  249. int CarlaBridgeOsc::handleMsgHide()
  250. {
  251. qDebug("CarlaBridgeOsc::handleMsgHide()");
  252. if (! client)
  253. return 1;
  254. client->toolkitHide();
  255. return 0;
  256. }
  257. int CarlaBridgeOsc::handleMsgQuit()
  258. {
  259. qDebug("CarlaBridgeOsc::handleMsgQuit()");
  260. if (! client)
  261. return 1;
  262. client->toolkitQuit();
  263. return 0;
  264. }
  265. CARLA_BRIDGE_END_NAMESPACE