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.

339 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.hpp"
  18. #include "carla_bridge_client.hpp"
  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. m_controlData.free();
  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. m_server = nullptr;
  90. }
  91. // -----------------------------------------------------------------------
  92. int CarlaBridgeOsc::handleMessage(const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  93. {
  94. qDebug("CarlaBridgeOsc::handleMessage(\"%s\", %i, %p, \"%s\", %p)", path, argc, argv, types, msg);
  95. CARLA_ASSERT(m_server);
  96. CARLA_ASSERT(m_serverPath);
  97. CARLA_ASSERT(path);
  98. // Check if message is for this client
  99. if ((! path) || strlen(path) <= m_nameSize || strncmp(path+1, m_name, m_nameSize) != 0)
  100. {
  101. qWarning("CarlaBridgeOsc::handleMessage() - message not for this client: '%s' != '/%s/'", path, m_name);
  102. return 1;
  103. }
  104. char method[32] = { 0 };
  105. memcpy(method, path + (m_nameSize + 1), uintMin(strlen(path), 32));
  106. if (method[0] == 0 || method[0] != '/')
  107. return 1;
  108. // Common OSC methods
  109. if (strcmp(method, "/configure") == 0)
  110. return handleMsgConfigure(argc, argv, types);
  111. if (strcmp(method, "/control") == 0)
  112. return handleMsgControl(argc, argv, types);
  113. if (strcmp(method, "/program") == 0)
  114. return handleMsgProgram(argc, argv, types);
  115. if (strcmp(method, "/midi_program") == 0)
  116. return handleMsgMidiProgram(argc, argv, types);
  117. if (strcmp(method, "/midi") == 0)
  118. return handleMsgMidi(argc, argv, types);
  119. if (strcmp(method, "/sample-rate") == 0)
  120. return 0; // unused
  121. if (strcmp(method, "/show") == 0)
  122. return handleMsgShow();
  123. if (strcmp(method, "/hide") == 0)
  124. return handleMsgHide();
  125. if (strcmp(method, "/quit") == 0)
  126. return handleMsgQuit();
  127. #ifdef BRIDGE_LV2
  128. if (strcmp(method, "/lv2_atom_transfer") == 0)
  129. return handleMsgLv2TransferAtom(argc, argv, types);
  130. if (strcmp(method, "/lv2_event_transfer") == 0)
  131. return handleMsgLv2TransferEvent(argc, argv, types);
  132. #endif
  133. #if 0
  134. else if (strcmp(method, "set_parameter_midi_channel") == 0)
  135. return osc_set_parameter_midi_channel_handler(argv);
  136. else if (strcmp(method, "set_parameter_midi_cc") == 0)
  137. return osc_set_parameter_midi_channel_handler(argv);
  138. #endif
  139. qWarning("CarlaBridgeOsc::handleMessage(\"%s\", ...) - got unsupported OSC method '%s'", path, method);
  140. return 1;
  141. }
  142. int CarlaBridgeOsc::handleMsgConfigure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  143. {
  144. qDebug("CarlaBridgeOsc::handleMsgConfigure()");
  145. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  146. if (! client)
  147. return 1;
  148. #ifdef BUILD_BRIDGE_PLUGIN
  149. const char* const key = (const char*)&argv[0]->s;
  150. const char* const value = (const char*)&argv[1]->s;
  151. if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SAVE_NOW) == 0)
  152. {
  153. client->saveNow();
  154. }
  155. else if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SET_CHUNK) == 0)
  156. {
  157. client->setChunkData(value);
  158. }
  159. else if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SET_CUSTOM) == 0)
  160. {
  161. QStringList vList = QString(value).split("ยท", QString::KeepEmptyParts);
  162. if (vList.size() == 3)
  163. {
  164. const char* const cType = vList.at(0).toUtf8().constData();
  165. const char* const cKey = vList.at(1).toUtf8().constData();
  166. const char* const cValue = vList.at(2).toUtf8().constData();
  167. client->setCustomData(cType, cKey, cValue);
  168. }
  169. }
  170. #else
  171. Q_UNUSED(argv);
  172. #endif
  173. return 0;
  174. }
  175. int CarlaBridgeOsc::handleMsgControl(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  176. {
  177. qDebug("CarlaBridgeOsc::handleMsgControl()");
  178. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  179. if (! client)
  180. return 1;
  181. const int32_t index = argv[0]->i;
  182. const float value = argv[1]->f;
  183. client->setParameter(index, value);
  184. return 0;
  185. }
  186. int CarlaBridgeOsc::handleMsgProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  187. {
  188. qDebug("CarlaBridgeOsc::handleMsgProgram()");
  189. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  190. if (! client)
  191. return 1;
  192. const int32_t index = argv[0]->i;
  193. client->setProgram(index);
  194. return 0;
  195. }
  196. int CarlaBridgeOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  197. {
  198. qDebug("CarlaBridgeOsc::handleMsgMidiProgram()");
  199. #ifdef BUILD_BRIDGE_PLUGIN
  200. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  201. #else
  202. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  203. #endif
  204. if (! client)
  205. return 1;
  206. #ifdef BUILD_BRIDGE_PLUGIN
  207. const int32_t index = argv[0]->i;
  208. client->setMidiProgram(index);
  209. #else
  210. const int32_t bank = argv[0]->i;
  211. const int32_t program = argv[1]->i;
  212. client->setMidiProgram(bank, program);
  213. #endif
  214. return 0;
  215. }
  216. int CarlaBridgeOsc::handleMsgMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  217. {
  218. qDebug("CarlaBridgeOsc::handleMsgMidi()");
  219. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  220. if (! client)
  221. return 1;
  222. const uint8_t* const mdata = argv[0]->m;
  223. const uint8_t data[4] = { mdata[0], mdata[1], mdata[2], mdata[3] };
  224. uint8_t status = data[1];
  225. uint8_t channel = status & 0x0F;
  226. // Fix bad note-off
  227. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  228. status -= 0x10;
  229. if (MIDI_IS_STATUS_NOTE_OFF(status))
  230. {
  231. uint8_t note = data[2];
  232. client->noteOff(channel, note);
  233. }
  234. else if (MIDI_IS_STATUS_NOTE_ON(status))
  235. {
  236. uint8_t note = data[2];
  237. uint8_t velo = data[3];
  238. client->noteOn(channel, note, velo);
  239. }
  240. return 0;
  241. }
  242. int CarlaBridgeOsc::handleMsgShow()
  243. {
  244. qDebug("CarlaBridgeOsc::handleMsgShow()");
  245. if (! client)
  246. return 1;
  247. client->toolkitShow();
  248. return 0;
  249. }
  250. int CarlaBridgeOsc::handleMsgHide()
  251. {
  252. qDebug("CarlaBridgeOsc::handleMsgHide()");
  253. if (! client)
  254. return 1;
  255. client->toolkitHide();
  256. return 0;
  257. }
  258. int CarlaBridgeOsc::handleMsgQuit()
  259. {
  260. qDebug("CarlaBridgeOsc::handleMsgQuit()");
  261. if (! client)
  262. return 1;
  263. client->toolkitQuit();
  264. return 0;
  265. }
  266. CARLA_BRIDGE_END_NAMESPACE