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.

340 lines
9.3KB

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