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.

344 lines
9.1KB

  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, "/sample_rate") == 0)
  124. return 0; // unused
  125. if (strcmp(method, "/show") == 0)
  126. return handleMsgShow();
  127. if (strcmp(method, "/hide") == 0)
  128. return handleMsgHide();
  129. if (strcmp(method, "/quit") == 0)
  130. return handleMsgQuit();
  131. #ifdef BRIDGE_LV2
  132. if (strcmp(method, "/lv2_atom_transfer") == 0)
  133. return handleMsgLv2TransferAtom(argc, argv, types);
  134. if (strcmp(method, "/lv2_event_transfer") == 0)
  135. return handleMsgLv2TransferEvent(argc, argv, types);
  136. #endif
  137. #if 0
  138. else if (strcmp(method, "set_parameter_midi_channel") == 0)
  139. return osc_set_parameter_midi_channel_handler(argv);
  140. else if (strcmp(method, "set_parameter_midi_cc") == 0)
  141. return osc_set_parameter_midi_channel_handler(argv);
  142. #endif
  143. qWarning("CarlaOsc::handleMessage(\"%s\", ...) - got unsupported OSC method '%s'", path, method);
  144. return 1;
  145. }
  146. int CarlaOsc::handleMsgConfigure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  147. {
  148. qDebug("CarlaOsc::handleMsgConfigure()");
  149. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  150. if (! client)
  151. return 1;
  152. #ifdef BUILD_BRIDGE_PLUGIN
  153. const char* const key = (const char*)&argv[0]->s;
  154. const char* const value = (const char*)&argv[1]->s;
  155. if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SAVE_NOW) == 0)
  156. {
  157. client->quequeMessage(MESSAGE_SAVE_NOW, 0, 0, 0.0);
  158. }
  159. else if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SET_CHUNK) == 0)
  160. {
  161. client->setChunkData(value);
  162. }
  163. else if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SET_CUSTOM) == 0)
  164. {
  165. QStringList vList = QString(value).split("ยท", QString::KeepEmptyParts);
  166. if (vList.size() == 3)
  167. {
  168. const char* const cType = vList.at(0).toUtf8().constData();
  169. const char* const cKey = vList.at(1).toUtf8().constData();
  170. const char* const cValue = vList.at(2).toUtf8().constData();
  171. client->setCustomData(cType, cKey, cValue);
  172. }
  173. }
  174. #else
  175. Q_UNUSED(argv);
  176. #endif
  177. return 0;
  178. }
  179. int CarlaOsc::handleMsgControl(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  180. {
  181. qDebug("CarlaOsc::handleMsgControl()");
  182. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  183. if (! client)
  184. return 1;
  185. const int32_t index = argv[0]->i;
  186. const float value = argv[1]->f;
  187. client->quequeMessage(MESSAGE_PARAMETER, index, 0, value);
  188. return 0;
  189. }
  190. int CarlaOsc::handleMsgProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  191. {
  192. qDebug("CarlaOsc::handleMsgProgram()");
  193. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  194. if (! client)
  195. return 1;
  196. const int32_t index = argv[0]->i;
  197. client->quequeMessage(MESSAGE_PROGRAM, index, 0, 0.0);
  198. return 0;
  199. }
  200. int CarlaOsc::handleMsgMidiProgram(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  201. {
  202. qDebug("CarlaOsc::handleMsgMidiProgram()");
  203. #ifdef BUILD_BRIDGE_PLUGIN
  204. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  205. #else
  206. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  207. #endif
  208. if (! client)
  209. return 1;
  210. #ifdef BUILD_BRIDGE_PLUGIN
  211. const int32_t index = argv[0]->i;
  212. client->quequeMessage(MESSAGE_MIDI_PROGRAM, index, 0, 0.0);
  213. #else
  214. const int32_t bank = argv[0]->i;
  215. const int32_t program = argv[1]->i;
  216. client->quequeMessage(MESSAGE_MIDI_PROGRAM, bank, program, 0.0);
  217. #endif
  218. return 0;
  219. }
  220. int CarlaOsc::handleMsgMidi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  221. {
  222. qDebug("CarlaOsc::handleMsgMidi()");
  223. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  224. if (! client)
  225. return 1;
  226. const uint8_t* const mdata = argv[0]->m;
  227. const uint8_t data[4] = { mdata[0], mdata[1], mdata[2], mdata[3] };
  228. uint8_t status = data[1];
  229. uint8_t channel = status & 0x0F;
  230. // Fix bad note-off
  231. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  232. status -= 0x10;
  233. if (MIDI_IS_STATUS_NOTE_OFF(status))
  234. {
  235. uint8_t note = data[2];
  236. client->quequeMessage(MESSAGE_NOTE_OFF, channel, note, 0);
  237. }
  238. else if (MIDI_IS_STATUS_NOTE_ON(status))
  239. {
  240. uint8_t note = data[2];
  241. uint8_t velo = data[3];
  242. client->quequeMessage(MESSAGE_NOTE_ON, channel, note, velo);
  243. }
  244. return 0;
  245. }
  246. int CarlaOsc::handleMsgShow()
  247. {
  248. qDebug("CarlaOsc::handleMsgShow()");
  249. if (! client)
  250. return 1;
  251. client->quequeMessage(MESSAGE_SHOW_GUI, 1, 0, 0.0);
  252. return 0;
  253. }
  254. int CarlaOsc::handleMsgHide()
  255. {
  256. qDebug("CarlaOsc::handleMsgHide()");
  257. if (! client)
  258. return 1;
  259. client->quequeMessage(MESSAGE_SHOW_GUI, 0, 0, 0.0);
  260. return 0;
  261. }
  262. int CarlaOsc::handleMsgQuit()
  263. {
  264. qDebug("CarlaOsc::handleMsgQuit()");
  265. if (! client)
  266. return 1;
  267. client->quequeMessage(MESSAGE_QUIT, 0, 0, 0.0);
  268. return 0;
  269. }
  270. CARLA_BRIDGE_END_NAMESPACE