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.

546 lines
17KB

  1. /*
  2. * Carla Backend
  3. * Copyright (C) 2011-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_osc.h"
  18. #include "carla_plugin.h"
  19. CarlaOsc::CarlaOsc(CarlaBackend::CarlaEngine* const engine_) :
  20. engine(engine_)
  21. {
  22. qDebug("CarlaOsc::CarlaOsc(%p)", engine_);
  23. assert(engine);
  24. m_serverPath = nullptr;
  25. m_serverThread = nullptr;
  26. m_controllerData.path = nullptr;
  27. m_controllerData.source = nullptr;
  28. m_controllerData.target = nullptr;
  29. m_name = nullptr;
  30. m_name_len = 0;
  31. }
  32. CarlaOsc::~CarlaOsc()
  33. {
  34. qDebug("CarlaOsc::~CarlaOsc()");
  35. }
  36. void CarlaOsc::init(const char* const name)
  37. {
  38. qDebug("CarlaOsc::init(\"%s\")", name);
  39. assert(name);
  40. assert(m_name_len == 0);
  41. m_name = strdup(name);
  42. m_name_len = strlen(name);
  43. // create new OSC thread
  44. m_serverThread = lo_server_thread_new(nullptr, osc_error_handler);
  45. // get our full OSC server path
  46. char* const threadPath = lo_server_thread_get_url(m_serverThread);
  47. m_serverPath = strdup(QString("%1%2").arg(threadPath).arg(name).toUtf8().constData());
  48. free(threadPath);
  49. // register message handler and start OSC thread
  50. lo_server_thread_add_method(m_serverThread, nullptr, nullptr, osc_message_handler, this);
  51. lo_server_thread_start(m_serverThread);
  52. }
  53. void CarlaOsc::close()
  54. {
  55. qDebug("CarlaOsc::close()");
  56. assert(m_name);
  57. osc_clear_data(&m_controllerData);
  58. lo_server_thread_stop(m_serverThread);
  59. lo_server_thread_del_method(m_serverThread, nullptr, nullptr);
  60. lo_server_thread_free(m_serverThread);
  61. free((void*)m_serverPath);
  62. m_serverPath = nullptr;
  63. free((void*)m_name);
  64. m_name = nullptr;
  65. m_name_len = 0;
  66. }
  67. // -------------------------------------------------------------------------------------------------------------------
  68. int CarlaOsc::handleMessage(const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  69. {
  70. qDebug("CarlaOsc::handleMessage(%s, %i, %p, %s, %p)", path, argc, argv, types, msg);
  71. assert(m_serverThread);
  72. assert(path);
  73. // Initial path check
  74. if (strcmp(path, "/register") == 0)
  75. {
  76. const lo_address source = lo_message_get_source(msg);
  77. return handle_register(argc, argv, types, source);
  78. }
  79. if (strcmp(path, "/unregister") == 0)
  80. {
  81. return handle_unregister();
  82. }
  83. // Check if message is for this client
  84. if (strlen(path) <= m_name_len || strncmp(path+1, m_name, m_name_len) != 0)
  85. {
  86. qWarning("CarlaOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, m_name);
  87. return 1;
  88. }
  89. // Get plugin id from message
  90. int pluginId = 0;
  91. if (std::isdigit(path[m_name_len+2]))
  92. pluginId += path[m_name_len+2]-'0';
  93. if (std::isdigit(path[m_name_len+3]))
  94. pluginId += (path[m_name_len+3]-'0')*10;
  95. if (pluginId < 0 || pluginId > CarlaBackend::MAX_PLUGINS)
  96. {
  97. qCritical("CarlaOsc::handleMessage() - failed to get plugin, wrong id -> %i", pluginId);
  98. return 1;
  99. }
  100. // Get plugin
  101. CarlaBackend::CarlaPlugin* const plugin = engine->getPlugin(pluginId);
  102. if (plugin == nullptr || plugin->id() != pluginId)
  103. {
  104. qWarning("CarlaOsc::handleMessage() - invalid plugin '%i', probably has been removed", pluginId);
  105. return 1;
  106. }
  107. // Get method from path, "/Carla/i/method"
  108. int offset = (pluginId >= 10) ? 4 : 3;
  109. char method[32] = { 0 };
  110. memcpy(method, path + (m_name_len + offset), 32);
  111. // Common OSC methods
  112. if (strcmp(method, "/update") == 0)
  113. {
  114. const lo_address source = lo_message_get_source(msg);
  115. return handle_update(plugin, argc, argv, types, source);
  116. }
  117. if (strcmp(method, "/configure") == 0)
  118. return handle_configure(plugin, argc, argv, types);
  119. if (strcmp(method, "/control") == 0)
  120. return handle_control(plugin, argc, argv, types);
  121. if (strcmp(method, "/program") == 0)
  122. return handle_program(plugin, argc, argv, types);
  123. if (strcmp(method, "/midi") == 0)
  124. return handle_midi(plugin, argc, argv, types);
  125. if (strcmp(method, "/exiting") == 0)
  126. return handle_exiting(plugin);
  127. // Internal methods
  128. if (strcmp(method, "/set_active") == 0)
  129. return handle_set_active(plugin, argc, argv, types);
  130. if (strcmp(method, "/set_drywet") == 0)
  131. return handle_set_drywet(plugin, argc, argv, types);
  132. if (strcmp(method, "/set_volume") == 0)
  133. return handle_set_volume(plugin, argc, argv, types);
  134. if (strcmp(method, "/set_balance_left") == 0)
  135. return handle_set_balance_left(plugin, argc, argv, types);
  136. if (strcmp(method, "/set_balance_right") == 0)
  137. return handle_set_balance_right(plugin, argc, argv, types);
  138. if (strcmp(method, "/set_parameter") == 0)
  139. return handle_set_parameter(plugin, argc, argv, types);
  140. if (strcmp(method, "/set_program") == 0)
  141. return handle_set_program(plugin, argc, argv, types);
  142. if (strcmp(method, "/set_midi_program") == 0)
  143. return handle_set_midi_program(plugin, argc, argv, types);
  144. if (strcmp(method, "/note_on") == 0)
  145. return handle_note_on(plugin, argc, argv, types);
  146. if (strcmp(method, "/note_off") == 0)
  147. return handle_note_off(plugin, argc, argv, types);
  148. // Plugin-specific methods
  149. if (strcmp(method, "/lv2_atom_transfer") == 0)
  150. return handle_lv2_atom_transfer(plugin, argc, argv, types);
  151. if (strcmp(method, "/lv2_event_transfer") == 0)
  152. return handle_lv2_event_transfer(plugin, argc, argv, types);
  153. // Plugin Bridges
  154. if (plugin->hints() & CarlaBackend::PLUGIN_IS_BRIDGE)
  155. {
  156. if (strcmp(method, "/bridge_ains_peak") == 0)
  157. return handle_bridge_ains_peak(plugin, argc, argv, types);
  158. if (strcmp(method, "/bridge_aouts_peak") == 0)
  159. return handle_bridge_aouts_peak(plugin, argc, argv, types);
  160. if (strcmp(method, "/bridge_audio_count") == 0)
  161. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeAudioCount, argv);
  162. if (strcmp(method, "/bridge_midi_count") == 0)
  163. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeMidiCount, argv);
  164. if (strcmp(method, "/bridge_param_count") == 0)
  165. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeParameterCount, argv);
  166. if (strcmp(method, "/bridge_program_count") == 0)
  167. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeProgramCount, argv);
  168. if (strcmp(method, "/bridge_midi_program_count") == 0)
  169. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeMidiProgramCount, argv);
  170. if (strcmp(method, "/bridge_plugin_info") == 0)
  171. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgePluginInfo, argv);
  172. if (strcmp(method, "/bridge_param_info") == 0)
  173. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeParameterInfo, argv);
  174. if (strcmp(method, "/bridge_param_data") == 0)
  175. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeParameterDataInfo, argv);
  176. if (strcmp(method, "/bridge_param_ranges") == 0)
  177. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeParameterRangesInfo, argv);
  178. if (strcmp(method, "/bridge_program_info") == 0)
  179. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeProgramInfo, argv);
  180. if (strcmp(method, "/bridge_midi_program_info") == 0)
  181. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeMidiProgramInfo, argv);
  182. if (strcmp(method, "/bridge_custom_data") == 0)
  183. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeCustomData, argv);
  184. if (strcmp(method, "/bridge_chunk_data") == 0)
  185. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeChunkData, argv);
  186. if (strcmp(method, "/bridge_update") == 0)
  187. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeUpdateNow, argv);
  188. }
  189. qWarning("CarlaOsc::handleMessage() - unsupported OSC method '%s'", method);
  190. return 1;
  191. }
  192. // -------------------------------------------------------------------------------------------------------------------
  193. int CarlaOsc::handle_register(const int argc, const lo_arg* const* const argv, const char* const types, const lo_address source)
  194. {
  195. qDebug("CarlaOsc::handle_register()");
  196. CARLA_OSC_CHECK_OSC_TYPES(1, "s");
  197. if (m_controllerData.path)
  198. {
  199. qWarning("CarlaOsc::handle_register() - OSC backend already registered to %s", m_controllerData.path);
  200. return 1;
  201. }
  202. const char* const url = (const char*)&argv[0]->s;
  203. const char* host;
  204. const char* port;
  205. qDebug("CarlaOsc::handle_register() - OSC backend registered to %s", url);
  206. host = lo_address_get_hostname(source);
  207. port = lo_address_get_port(source);
  208. m_controllerData.source = lo_address_new(host, port);
  209. host = lo_url_get_hostname(url);
  210. port = lo_url_get_port(url);
  211. m_controllerData.path = lo_url_get_path(url);
  212. m_controllerData.target = lo_address_new(host, port);
  213. free((void*)host);
  214. free((void*)port);
  215. for (unsigned short i=0; i < CarlaBackend::MAX_PLUGINS; i++)
  216. {
  217. CarlaBackend::CarlaPlugin* const plugin = engine->getPlugin(i);
  218. if (plugin && plugin->enabled())
  219. plugin->registerToOsc();
  220. }
  221. return 0;
  222. }
  223. int CarlaOsc::handle_unregister()
  224. {
  225. qDebug("CarlaOsc::handle_unregister()");
  226. if (! m_controllerData.path)
  227. {
  228. qWarning("CarlaOsc::handle_unregister() - OSC backend is not registered yet");
  229. return 1;
  230. }
  231. osc_clear_data(&m_controllerData);
  232. return 0;
  233. }
  234. // -------------------------------------------------------------------------------------------------------------------
  235. int CarlaOsc::handle_update(CARLA_OSC_HANDLE_ARGS2, const lo_address source)
  236. {
  237. qDebug("CarlaOsc::handle_update()");
  238. CARLA_OSC_CHECK_OSC_TYPES(1, "s");
  239. const char* const url = (const char*)&argv[0]->s;
  240. plugin->updateOscData(source, url);
  241. return 0;
  242. }
  243. int CarlaOsc::handle_configure(CARLA_OSC_HANDLE_ARGS2)
  244. {
  245. qDebug("CarlaOsc::handle_configure()");
  246. CARLA_OSC_CHECK_OSC_TYPES(2, "ss");
  247. const char* const key = (const char*)&argv[0]->s;
  248. const char* const value = (const char*)&argv[1]->s;
  249. if (plugin->hints() & CarlaBackend::PLUGIN_IS_BRIDGE)
  250. {
  251. if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_HIDE_GUI) == 0)
  252. {
  253. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  254. return 0;
  255. }
  256. if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SAVED) == 0)
  257. {
  258. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeSaved, nullptr);
  259. }
  260. }
  261. plugin->setCustomData(CarlaBackend::CUSTOM_DATA_STRING, key, value, false);
  262. return 0;
  263. }
  264. int CarlaOsc::handle_control(CARLA_OSC_HANDLE_ARGS2)
  265. {
  266. qDebug("CarlaOsc::handle_control()");
  267. CARLA_OSC_CHECK_OSC_TYPES(2, "if");
  268. const int rindex = argv[0]->i;
  269. const float value = argv[1]->f;
  270. plugin->setParameterValueByRIndex(rindex, value, false, true, true);
  271. return 0;
  272. }
  273. int CarlaOsc::handle_program(CARLA_OSC_HANDLE_ARGS2)
  274. {
  275. qDebug("CarlaOsc::handle_program()");
  276. if (argc == 2)
  277. {
  278. CARLA_OSC_CHECK_OSC_TYPES(2, "ii");
  279. const uint32_t bank_id = argv[0]->i;
  280. const uint32_t program_id = argv[1]->i;
  281. plugin->setMidiProgramById(bank_id, program_id, false, true, true, true);
  282. return 0;
  283. }
  284. else
  285. {
  286. CARLA_OSC_CHECK_OSC_TYPES(1, "i");
  287. const uint32_t program_id = argv[0]->i;
  288. if (program_id < plugin->programCount())
  289. {
  290. plugin->setProgram(program_id, false, true, true, true);
  291. return 0;
  292. }
  293. qCritical("CarlaOsc::handle_program() - program_id '%i' out of bounds", program_id);
  294. }
  295. return 1;
  296. }
  297. int CarlaOsc::handle_midi(CARLA_OSC_HANDLE_ARGS2)
  298. {
  299. qDebug("CarlaOsc::handle_midi()");
  300. CARLA_OSC_CHECK_OSC_TYPES(1, "m");
  301. if (plugin->midiInCount() > 0)
  302. {
  303. const uint8_t* const data = argv[0]->m;
  304. uint8_t status = data[1];
  305. uint8_t channel = status & 0x0F;
  306. // Fix bad note-off
  307. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  308. status -= 0x10;
  309. if (MIDI_IS_STATUS_NOTE_OFF(status))
  310. {
  311. uint8_t note = data[2];
  312. plugin->sendMidiSingleNote(channel, note, 0, false, true, true);
  313. }
  314. else if (MIDI_IS_STATUS_NOTE_ON(status))
  315. {
  316. uint8_t note = data[2];
  317. uint8_t velo = data[3];
  318. plugin->sendMidiSingleNote(channel, note, velo, false, true, true);
  319. }
  320. return 0;
  321. }
  322. qWarning("CarlaOsc::handle_midi() - recived midi when plugin has no midi inputs");
  323. return 1;
  324. }
  325. int CarlaOsc::handle_exiting(CARLA_OSC_HANDLE_ARGS1)
  326. {
  327. qDebug("CarlaOsc::handle_exiting()");
  328. // TODO - check for non-UIs (dssi-vst) and set to -1 instead
  329. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  330. plugin->clearOscData();
  331. return 0;
  332. }
  333. // -------------------------------------------------------------------------------------------------------------------
  334. int CarlaOsc::handle_set_active(CARLA_OSC_HANDLE_ARGS2)
  335. {
  336. qDebug("CarlaOsc::handle_set_active()");
  337. CARLA_OSC_CHECK_OSC_TYPES(1, "i");
  338. bool value = (bool)argv[0]->i;
  339. plugin->setActive(value, false, true);
  340. return 0;
  341. }
  342. int CarlaOsc::handle_set_drywet(CARLA_OSC_HANDLE_ARGS2)
  343. {
  344. qDebug("CarlaOsc::handle_set_drywet()");
  345. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  346. double value = argv[0]->d;
  347. plugin->setDryWet(value, false, true);
  348. return 0;
  349. }
  350. int CarlaOsc::handle_set_volume(CARLA_OSC_HANDLE_ARGS2)
  351. {
  352. qDebug("CarlaOsc::handle_set_volume()");
  353. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  354. double value = argv[0]->d;
  355. plugin->setVolume(value, false, true);
  356. return 0;
  357. }
  358. int CarlaOsc::handle_set_balance_left(CARLA_OSC_HANDLE_ARGS2)
  359. {
  360. qDebug("CarlaOsc::handle_set_balance_left()");
  361. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  362. double value = argv[0]->d;
  363. plugin->setBalanceLeft(value, false, true);
  364. return 0;
  365. }
  366. int CarlaOsc::handle_set_balance_right(CARLA_OSC_HANDLE_ARGS2)
  367. {
  368. qDebug("CarlaOsc::handle_set_balance_right()");
  369. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  370. double value = argv[0]->d;
  371. plugin->setBalanceRight(value, false, true);
  372. return 0;
  373. }
  374. int CarlaOsc::handle_set_parameter(CARLA_OSC_HANDLE_ARGS2)
  375. {
  376. qDebug("CarlaOsc::handle_set_parameter()");
  377. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  378. const uint32_t parameter_id = argv[0]->i;
  379. const double value = argv[1]->d;
  380. plugin->setParameterValue(parameter_id, value, true, false, true);
  381. return 0;
  382. }
  383. int CarlaOsc::handle_set_program(CARLA_OSC_HANDLE_ARGS2)
  384. {
  385. qDebug("CarlaOsc::handle_set_program()");
  386. CARLA_OSC_CHECK_OSC_TYPES(1, "i");
  387. const uint32_t index = argv[0]->i;
  388. plugin->setProgram(index, true, false, true, true);
  389. return 0;
  390. }
  391. int CarlaOsc::handle_set_midi_program(CARLA_OSC_HANDLE_ARGS2)
  392. {
  393. qDebug("CarlaOsc::handle_set_midi_program()");
  394. CARLA_OSC_CHECK_OSC_TYPES(1, "i");
  395. const uint32_t index = argv[0]->i;
  396. plugin->setMidiProgram(index, true, false, true, true);
  397. return 0;
  398. }
  399. int CarlaOsc::handle_note_on(CARLA_OSC_HANDLE_ARGS2)
  400. {
  401. qDebug("CarlaOsc::handle_note_on()");
  402. CARLA_OSC_CHECK_OSC_TYPES(3, "iii");
  403. const int channel = argv[0]->i;
  404. const int note = argv[1]->i;
  405. const int velo = argv[2]->i;
  406. plugin->sendMidiSingleNote(channel, note, velo, true, false, true);
  407. return 0;
  408. }
  409. int CarlaOsc::handle_note_off(CARLA_OSC_HANDLE_ARGS2)
  410. {
  411. qDebug("CarlaOsc::handle_note_off()");
  412. CARLA_OSC_CHECK_OSC_TYPES(2, "ii");
  413. const int channel = argv[0]->i;
  414. const int note = argv[1]->i;
  415. plugin->sendMidiSingleNote(channel, note, 0, true, false, true);
  416. return 0;
  417. }
  418. int CarlaOsc::handle_bridge_ains_peak(CARLA_OSC_HANDLE_ARGS2)
  419. {
  420. CARLA_OSC_CHECK_OSC_TYPES(2, "id");
  421. const int index = argv[0]->i;
  422. const double value = argv[1]->d;
  423. engine->setInputPeak(plugin->id(), index-1, value);
  424. return 0;
  425. }
  426. int CarlaOsc::handle_bridge_aouts_peak(CARLA_OSC_HANDLE_ARGS2)
  427. {
  428. CARLA_OSC_CHECK_OSC_TYPES(2, "id");
  429. const int index = argv[0]->i;
  430. const double value = argv[1]->d;
  431. engine->setOutputPeak(plugin->id(), index-1, value);
  432. return 0;
  433. }