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.

327 lines
8.7KB

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