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.

472 lines
14KB

  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. #include <QtCore/QString>
  21. #include <QtCore/QStringList>
  22. //CARLA_BRIDGE_START_NAMESPACE
  23. namespace CarlaBridge {
  24. CarlaBridgeOsc::CarlaBridgeOsc(CarlaBridgeClient* const client_, const char* const name) :
  25. client(client_)
  26. {
  27. qDebug("CarlaBridgeOsc::CarlaBridgeOsc(%p, %s)", client_, name);
  28. assert(client_);
  29. assert(name);
  30. m_serverPath = nullptr;
  31. m_serverThread = nullptr;
  32. m_serverData.path = nullptr;
  33. m_serverData.source = nullptr;
  34. m_serverData.target = nullptr;
  35. m_name = strdup(name);
  36. m_name_len = strlen(name);
  37. }
  38. CarlaBridgeOsc::~CarlaBridgeOsc()
  39. {
  40. qDebug("CarlaBridgeOsc::~CarlaBridgeOsc()");
  41. free((void*)m_name);
  42. }
  43. void CarlaBridgeOsc::init(const char* const url)
  44. {
  45. qDebug("CarlaBridgeOsc::init(%s)", url);
  46. assert(url);
  47. const char* host = lo_url_get_hostname(url);
  48. const char* port = lo_url_get_port(url);
  49. m_serverData.path = lo_url_get_path(url);
  50. m_serverData.target = lo_address_new(host, port);
  51. free((void*)host);
  52. free((void*)port);
  53. // create new OSC thread
  54. m_serverThread = lo_server_thread_new(nullptr, osc_error_handler);
  55. // get our full OSC server path
  56. char* const threadPath = lo_server_thread_get_url(m_serverThread);
  57. m_serverPath = strdup(QString("%1%2").arg(threadPath).arg(m_name).toUtf8().constData());
  58. free(threadPath);
  59. // register message handler and start OSC thread
  60. lo_server_thread_add_method(m_serverThread, nullptr, nullptr, osc_message_handler, this);
  61. lo_server_thread_start(m_serverThread);
  62. }
  63. void CarlaBridgeOsc::close()
  64. {
  65. qDebug("CarlaBridgeOsc::close()");
  66. assert(client);
  67. osc_clear_data(&m_serverData);
  68. lo_server_thread_stop(m_serverThread);
  69. lo_server_thread_del_method(m_serverThread, nullptr, nullptr);
  70. lo_server_thread_free(m_serverThread);
  71. free((void*)m_serverPath);
  72. m_serverPath = nullptr;
  73. }
  74. int CarlaBridgeOsc::handleMessage(const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  75. {
  76. qDebug("CarlaBridgeOsc::handleMessage(%s, %i, %p, %s, %p)", path, argc, argv, types, msg);
  77. assert(m_serverThread);
  78. assert(path);
  79. // Check if message is for this client
  80. if (strlen(path) <= m_name_len || strncmp(path+1, m_name, m_name_len) != 0)
  81. {
  82. qWarning("CarlaBridgeOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, m_name);
  83. return 1;
  84. }
  85. char method[32] = { 0 };
  86. memcpy(method, path + (m_name_len + 1), 32);
  87. // Common OSC methods
  88. if (strcmp(method, "/configure") == 0)
  89. return handle_configure(argc, argv, types);
  90. if (strcmp(method, "/control") == 0)
  91. return handle_control(argc, argv, types);
  92. if (strcmp(method, "/program") == 0)
  93. return handle_program(argc, argv, types);
  94. if (strcmp(method, "/midi_program") == 0)
  95. return handle_midi_program(argc, argv, types);
  96. if (strcmp(method, "/midi") == 0)
  97. return handle_midi(argc, argv, types);
  98. if (strcmp(method, "/show") == 0)
  99. return handle_show();
  100. if (strcmp(method, "/hide") == 0)
  101. return handle_hide();
  102. if (strcmp(method, "/quit") == 0)
  103. return handle_quit();
  104. // client specific methods
  105. #if LV2_UI_GTK2 || LV2_UI_QT4 || LV2_UI_X11
  106. if (strcmp(method, "/lv2_atom_transfer") == 0)
  107. return handle_lv2_atom_transfer(argc, argv, types);
  108. if (strcmp(method, "/lv2_event_transfer") == 0)
  109. return handle_lv2_event_transfer(argc, argv, types);
  110. #endif
  111. #if 0
  112. else if (strcmp(method, "set_parameter_midi_channel") == 0)
  113. return osc_set_parameter_midi_channel_handler(argv);
  114. else if (strcmp(method, "set_parameter_midi_cc") == 0)
  115. return osc_set_parameter_midi_channel_handler(argv);
  116. #endif
  117. else
  118. qWarning("Got unsupported OSC method '%s' on '%s'", method, path);
  119. return 1;
  120. }
  121. int CarlaBridgeOsc::handle_configure(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  122. {
  123. qDebug("CarlaOsc::handle_configure()");
  124. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  125. if (! client)
  126. return 1;
  127. #ifdef BUILD_BRIDGE_PLUGIN
  128. const char* const key = (const char*)&argv[0]->s;
  129. const char* const value = (const char*)&argv[1]->s;
  130. if (strcmp(key, CARLA_BRIDGE_MSG_SAVE_NOW) == 0)
  131. {
  132. client->quequeMessage(BRIDGE_MESSAGE_SAVE_NOW, 0, 0, 0.0);
  133. }
  134. else if (strcmp(key, CARLA_BRIDGE_MSG_SET_CHUNK) == 0)
  135. {
  136. client->setChunkData(value);
  137. }
  138. else if (strcmp(key, CARLA_BRIDGE_MSG_SET_CUSTOM) == 0)
  139. {
  140. QStringList vList = QString(value).split("ยท", QString::KeepEmptyParts);
  141. if (vList.size() == 3)
  142. {
  143. const char* const cType = vList.at(0).toUtf8().constData();
  144. const char* const cKey = vList.at(1).toUtf8().constData();
  145. const char* const cValue = vList.at(2).toUtf8().constData();
  146. client->set_custom_data(cType, cKey, cValue);
  147. }
  148. }
  149. #else
  150. Q_UNUSED(argv);
  151. #endif
  152. return 0;
  153. }
  154. int CarlaBridgeOsc::handle_control(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  155. {
  156. //qDebug("CarlaOsc::handle_control()");
  157. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "if");
  158. if (! client)
  159. return 1;
  160. const int rindex = argv[0]->i;
  161. const float value = argv[1]->f;
  162. client->quequeMessage(MESSAGE_PARAMETER, rindex, 0, value);
  163. return 0;
  164. }
  165. int CarlaBridgeOsc::handle_program(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  166. {
  167. qDebug("CarlaOsc::handle_program()");
  168. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "i");
  169. if (! client)
  170. return 1;
  171. const int index = argv[0]->i;
  172. client->quequeMessage(MESSAGE_PROGRAM, index, 0, 0.0);
  173. return 0;
  174. }
  175. int CarlaBridgeOsc::handle_midi_program(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  176. {
  177. qDebug("CarlaOsc::handle_program()");
  178. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ii");
  179. if (! client)
  180. return 1;
  181. const int bank = argv[0]->i;
  182. const int program = argv[1]->i;
  183. client->quequeMessage(MESSAGE_MIDI_PROGRAM, bank, program, 0.0);
  184. return 0;
  185. }
  186. int CarlaBridgeOsc::handle_midi(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  187. {
  188. qDebug("CarlaOsc::handle_midi()");
  189. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(1, "m");
  190. if (! client)
  191. return 1;
  192. const uint8_t* const data = argv[0]->m;
  193. uint8_t status = data[1];
  194. // Fix bad note-off
  195. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  196. status -= 0x10;
  197. if (MIDI_IS_STATUS_NOTE_OFF(status))
  198. {
  199. uint8_t note = data[2];
  200. client->quequeMessage(MESSAGE_NOTE_OFF, note, 0, 0.0);
  201. }
  202. else if (MIDI_IS_STATUS_NOTE_ON(status))
  203. {
  204. uint8_t note = data[2];
  205. uint8_t velo = data[3];
  206. client->quequeMessage(MESSAGE_NOTE_ON, note, velo, 0.0);
  207. }
  208. return 0;
  209. }
  210. int CarlaBridgeOsc::handle_show()
  211. {
  212. if (! client)
  213. return 1;
  214. client->quequeMessage(MESSAGE_SHOW_GUI, 1, 0, 0.0);
  215. return 0;
  216. }
  217. int CarlaBridgeOsc::handle_hide()
  218. {
  219. if (! client)
  220. return 1;
  221. client->quequeMessage(MESSAGE_SHOW_GUI, 0, 0, 0.0);
  222. return 0;
  223. }
  224. int CarlaBridgeOsc::handle_quit()
  225. {
  226. if (! client)
  227. return 1;
  228. client->quequeMessage(MESSAGE_QUIT, 0, 0, 0.0);
  229. return 0;
  230. }
  231. #ifdef BUILD_BRIDGE_PLUGIN
  232. void osc_send_bridge_ains_peak(int index, double value)
  233. {
  234. if (global_osc_data.target)
  235. {
  236. char target_path[strlen(global_osc_data.path)+18];
  237. strcpy(target_path, global_osc_data.path);
  238. strcat(target_path, "/bridge_ains_peak");
  239. lo_send(global_osc_data.target, target_path, "if", index, value);
  240. }
  241. }
  242. void osc_send_bridge_aouts_peak(int index, double value)
  243. {
  244. if (global_osc_data.target)
  245. {
  246. char target_path[strlen(global_osc_data.path)+19];
  247. strcpy(target_path, global_osc_data.path);
  248. strcat(target_path, "/bridge_aouts_peak");
  249. lo_send(global_osc_data.target, target_path, "if", index, value);
  250. }
  251. }
  252. void osc_send_bridge_audio_count(int ins, int outs, int total)
  253. {
  254. if (global_osc_data.target)
  255. {
  256. char target_path[strlen(global_osc_data.path)+20];
  257. strcpy(target_path, global_osc_data.path);
  258. strcat(target_path, "/bridge_audio_count");
  259. lo_send(global_osc_data.target, target_path, "iii", ins, outs, total);
  260. }
  261. }
  262. void osc_send_bridge_midi_count(int ins, int outs, int total)
  263. {
  264. if (global_osc_data.target)
  265. {
  266. char target_path[strlen(global_osc_data.path)+19];
  267. strcpy(target_path, global_osc_data.path);
  268. strcat(target_path, "/bridge_midi_count");
  269. lo_send(global_osc_data.target, target_path, "iii", ins, outs, total);
  270. }
  271. }
  272. void osc_send_bridge_param_count(int ins, int outs, int total)
  273. {
  274. if (global_osc_data.target)
  275. {
  276. char target_path[strlen(global_osc_data.path)+20];
  277. strcpy(target_path, global_osc_data.path);
  278. strcat(target_path, "/bridge_param_count");
  279. lo_send(global_osc_data.target, target_path, "iii", ins, outs, total);
  280. }
  281. }
  282. void osc_send_bridge_program_count(int count)
  283. {
  284. if (global_osc_data.target)
  285. {
  286. char target_path[strlen(global_osc_data.path)+22];
  287. strcpy(target_path, global_osc_data.path);
  288. strcat(target_path, "/bridge_program_count");
  289. lo_send(global_osc_data.target, target_path, "i", count);
  290. }
  291. }
  292. void osc_send_bridge_midi_program_count(int count)
  293. {
  294. if (global_osc_data.target)
  295. {
  296. char target_path[strlen(global_osc_data.path)+27];
  297. strcpy(target_path, global_osc_data.path);
  298. strcat(target_path, "/bridge_midi_program_count");
  299. lo_send(global_osc_data.target, target_path, "i", count);
  300. }
  301. }
  302. void osc_send_bridge_plugin_info(int category, int hints, const char* name, const char* label, const char* maker, const char* copyright, long uniqueId)
  303. {
  304. if (global_osc_data.target)
  305. {
  306. char target_path[strlen(global_osc_data.path)+20];
  307. strcpy(target_path, global_osc_data.path);
  308. strcat(target_path, "/bridge_plugin_info");
  309. lo_send(global_osc_data.target, target_path, "iissssi", category, hints, name, label, maker, copyright, uniqueId);
  310. // FIXME - should be long type
  311. }
  312. }
  313. void osc_send_bridge_param_info(int index, const char* name, const char* unit)
  314. {
  315. if (global_osc_data.target)
  316. {
  317. char target_path[strlen(global_osc_data.path)+19];
  318. strcpy(target_path, global_osc_data.path);
  319. strcat(target_path, "/bridge_param_info");
  320. lo_send(global_osc_data.target, target_path, "iss", index, name, unit);
  321. }
  322. }
  323. void osc_send_bridge_param_data(int index, int type, int rindex, int hints, int midi_channel, int midi_cc)
  324. {
  325. if (global_osc_data.target)
  326. {
  327. char target_path[strlen(global_osc_data.path)+19];
  328. strcpy(target_path, global_osc_data.path);
  329. strcat(target_path, "/bridge_param_data");
  330. lo_send(global_osc_data.target, target_path, "iiiiii", index, type, rindex, hints, midi_channel, midi_cc);
  331. }
  332. }
  333. void osc_send_bridge_param_ranges(int index, double def, double min, double max, double step, double step_small, double step_large)
  334. {
  335. if (global_osc_data.target)
  336. {
  337. char target_path[strlen(global_osc_data.path)+21];
  338. strcpy(target_path, global_osc_data.path);
  339. strcat(target_path, "/bridge_param_ranges");
  340. lo_send(global_osc_data.target, target_path, "iffffff", index, def, min, max, step, step_small, step_large);
  341. }
  342. }
  343. void osc_send_bridge_program_info(int index, const char* name)
  344. {
  345. if (global_osc_data.target)
  346. {
  347. char target_path[strlen(global_osc_data.path)+21];
  348. strcpy(target_path, global_osc_data.path);
  349. strcat(target_path, "/bridge_program_info");
  350. lo_send(global_osc_data.target, target_path, "is", index, name);
  351. }
  352. }
  353. void osc_send_bridge_midi_program_info(int index, int bank, int program, const char* label)
  354. {
  355. if (global_osc_data.target)
  356. {
  357. char target_path[strlen(global_osc_data.path)+26];
  358. strcpy(target_path, global_osc_data.path);
  359. strcat(target_path, "/bridge_midi_program_info");
  360. lo_send(global_osc_data.target, target_path, "iiis", index, bank, program, label);
  361. }
  362. }
  363. void osc_send_bridge_custom_data(const char* stype, const char* key, const char* value)
  364. {
  365. if (global_osc_data.target)
  366. {
  367. char target_path[strlen(global_osc_data.path)+20];
  368. strcpy(target_path, global_osc_data.path);
  369. strcat(target_path, "/bridge_custom_data");
  370. lo_send(global_osc_data.target, target_path, "sss", stype, key, value);
  371. }
  372. }
  373. void osc_send_bridge_chunk_data(const char* string_data)
  374. {
  375. if (global_osc_data.target)
  376. {
  377. char target_path[strlen(global_osc_data.path)+19];
  378. strcpy(target_path, global_osc_data.path);
  379. strcat(target_path, "/bridge_chunk_data");
  380. lo_send(global_osc_data.target, target_path, "s", string_data);
  381. }
  382. }
  383. void osc_send_bridge_update()
  384. {
  385. if (global_osc_data.target)
  386. {
  387. char target_path[strlen(global_osc_data.path)+15];
  388. strcpy(target_path, global_osc_data.path);
  389. strcat(target_path, "/bridge_update");
  390. lo_send(global_osc_data.target, target_path, "");
  391. }
  392. }
  393. #endif
  394. CARLA_BRIDGE_END_NAMESPACE