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.

579 lines
18KB

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