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.

574 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. 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_value") == 0)
  139. return handle_set_parameter_value(plugin, argc, argv, types);
  140. if (strcmp(method, "/set_parameter_midi_cc") == 0)
  141. return handle_set_parameter_midi_cc(plugin, argc, argv, types);
  142. if (strcmp(method, "/set_parameter_midi_channel") == 0)
  143. return handle_set_parameter_midi_channel(plugin, argc, argv, types);
  144. if (strcmp(method, "/set_program") == 0)
  145. return handle_set_program(plugin, argc, argv, types);
  146. if (strcmp(method, "/set_midi_program") == 0)
  147. return handle_set_midi_program(plugin, argc, argv, types);
  148. if (strcmp(method, "/note_on") == 0)
  149. return handle_note_on(plugin, argc, argv, types);
  150. if (strcmp(method, "/note_off") == 0)
  151. return handle_note_off(plugin, argc, argv, types);
  152. // Plugin-specific methods
  153. if (strcmp(method, "/lv2_atom_transfer") == 0)
  154. return handle_lv2_atom_transfer(plugin, argc, argv, types);
  155. if (strcmp(method, "/lv2_event_transfer") == 0)
  156. return handle_lv2_event_transfer(plugin, argc, argv, types);
  157. // Plugin Bridges
  158. if (plugin->hints() & CarlaBackend::PLUGIN_IS_BRIDGE)
  159. {
  160. if (strcmp(method, "/bridge_ains_peak") == 0)
  161. return handle_bridge_ains_peak(plugin, argc, argv, types);
  162. if (strcmp(method, "/bridge_aouts_peak") == 0)
  163. return handle_bridge_aouts_peak(plugin, argc, argv, types);
  164. if (strcmp(method, "/bridge_audio_count") == 0)
  165. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeAudioCount, argv);
  166. if (strcmp(method, "/bridge_midi_count") == 0)
  167. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeMidiCount, argv);
  168. if (strcmp(method, "/bridge_param_count") == 0)
  169. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeParameterCount, argv);
  170. if (strcmp(method, "/bridge_program_count") == 0)
  171. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeProgramCount, argv);
  172. if (strcmp(method, "/bridge_midi_program_count") == 0)
  173. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeMidiProgramCount, argv);
  174. if (strcmp(method, "/bridge_plugin_info") == 0)
  175. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgePluginInfo, argv);
  176. if (strcmp(method, "/bridge_param_info") == 0)
  177. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeParameterInfo, argv);
  178. if (strcmp(method, "/bridge_param_data") == 0)
  179. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeParameterDataInfo, argv);
  180. if (strcmp(method, "/bridge_param_ranges") == 0)
  181. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeParameterRangesInfo, argv);
  182. if (strcmp(method, "/bridge_program_info") == 0)
  183. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeProgramInfo, argv);
  184. if (strcmp(method, "/bridge_midi_program_info") == 0)
  185. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeMidiProgramInfo, argv);
  186. if (strcmp(method, "/bridge_custom_data") == 0)
  187. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeCustomData, argv);
  188. if (strcmp(method, "/bridge_chunk_data") == 0)
  189. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeChunkData, argv);
  190. if (strcmp(method, "/bridge_update") == 0)
  191. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeUpdateNow, argv);
  192. }
  193. qWarning("CarlaOsc::handleMessage() - unsupported OSC method '%s'", method);
  194. return 1;
  195. }
  196. // -------------------------------------------------------------------------------------------------------------------
  197. int CarlaOsc::handle_register(const int argc, const lo_arg* const* const argv, const char* const types, const lo_address source)
  198. {
  199. qDebug("CarlaOsc::handle_register()");
  200. CARLA_OSC_CHECK_OSC_TYPES(1, "s");
  201. if (m_controllerData.path)
  202. {
  203. qWarning("CarlaOsc::handle_register() - OSC backend already registered to %s", m_controllerData.path);
  204. return 1;
  205. }
  206. const char* const url = (const char*)&argv[0]->s;
  207. const char* host;
  208. const char* port;
  209. qDebug("CarlaOsc::handle_register() - OSC backend registered to %s", url);
  210. host = lo_address_get_hostname(source);
  211. port = lo_address_get_port(source);
  212. m_controllerData.source = lo_address_new(host, port);
  213. host = lo_url_get_hostname(url);
  214. port = lo_url_get_port(url);
  215. m_controllerData.path = lo_url_get_path(url);
  216. m_controllerData.target = lo_address_new(host, port);
  217. free((void*)host);
  218. free((void*)port);
  219. for (unsigned short i=0; i < CarlaBackend::MAX_PLUGINS; i++)
  220. {
  221. CarlaBackend::CarlaPlugin* const plugin = engine->getPlugin(i);
  222. if (plugin && plugin->enabled())
  223. plugin->registerToOsc();
  224. }
  225. return 0;
  226. }
  227. int CarlaOsc::handle_unregister()
  228. {
  229. qDebug("CarlaOsc::handle_unregister()");
  230. if (! m_controllerData.path)
  231. {
  232. qWarning("CarlaOsc::handle_unregister() - OSC backend is not registered yet");
  233. return 1;
  234. }
  235. osc_clear_data(&m_controllerData);
  236. return 0;
  237. }
  238. // -------------------------------------------------------------------------------------------------------------------
  239. int CarlaOsc::handle_update(CARLA_OSC_HANDLE_ARGS2, const lo_address source)
  240. {
  241. qDebug("CarlaOsc::handle_update()");
  242. CARLA_OSC_CHECK_OSC_TYPES(1, "s");
  243. const char* const url = (const char*)&argv[0]->s;
  244. plugin->updateOscData(source, url);
  245. return 0;
  246. }
  247. int CarlaOsc::handle_configure(CARLA_OSC_HANDLE_ARGS2)
  248. {
  249. qDebug("CarlaOsc::handle_configure()");
  250. CARLA_OSC_CHECK_OSC_TYPES(2, "ss");
  251. const char* const key = (const char*)&argv[0]->s;
  252. const char* const value = (const char*)&argv[1]->s;
  253. if (plugin->hints() & CarlaBackend::PLUGIN_IS_BRIDGE)
  254. {
  255. if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_HIDE_GUI) == 0)
  256. {
  257. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  258. return 0;
  259. }
  260. if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SAVED) == 0)
  261. {
  262. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeSaved, nullptr);
  263. }
  264. }
  265. plugin->setCustomData(CarlaBackend::CUSTOM_DATA_STRING, key, value, false);
  266. return 0;
  267. }
  268. int CarlaOsc::handle_control(CARLA_OSC_HANDLE_ARGS2)
  269. {
  270. qDebug("CarlaOsc::handle_control()");
  271. CARLA_OSC_CHECK_OSC_TYPES(2, "if");
  272. const int rindex = argv[0]->i;
  273. const float value = argv[1]->f;
  274. plugin->setParameterValueByRIndex(rindex, value, false, true, true);
  275. return 0;
  276. }
  277. int CarlaOsc::handle_program(CARLA_OSC_HANDLE_ARGS2)
  278. {
  279. qDebug("CarlaOsc::handle_program()");
  280. if (argc == 2)
  281. {
  282. CARLA_OSC_CHECK_OSC_TYPES(2, "ii");
  283. const uint32_t bank_id = argv[0]->i;
  284. const uint32_t program_id = argv[1]->i;
  285. plugin->setMidiProgramById(bank_id, program_id, false, true, true, true);
  286. return 0;
  287. }
  288. else
  289. {
  290. CARLA_OSC_CHECK_OSC_TYPES(1, "i");
  291. const uint32_t program_id = argv[0]->i;
  292. if (program_id < plugin->programCount())
  293. {
  294. plugin->setProgram(program_id, false, true, true, true);
  295. return 0;
  296. }
  297. qCritical("CarlaOsc::handle_program() - program_id '%i' out of bounds", program_id);
  298. }
  299. return 1;
  300. }
  301. int CarlaOsc::handle_midi(CARLA_OSC_HANDLE_ARGS2)
  302. {
  303. qDebug("CarlaOsc::handle_midi()");
  304. CARLA_OSC_CHECK_OSC_TYPES(1, "m");
  305. if (plugin->midiInCount() > 0)
  306. {
  307. const uint8_t* const data = argv[0]->m;
  308. uint8_t status = data[1];
  309. uint8_t channel = status & 0x0F;
  310. // Fix bad note-off
  311. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  312. status -= 0x10;
  313. if (MIDI_IS_STATUS_NOTE_OFF(status))
  314. {
  315. uint8_t note = data[2];
  316. plugin->sendMidiSingleNote(channel, note, 0, false, true, true);
  317. }
  318. else if (MIDI_IS_STATUS_NOTE_ON(status))
  319. {
  320. uint8_t note = data[2];
  321. uint8_t velo = data[3];
  322. plugin->sendMidiSingleNote(channel, note, velo, false, true, true);
  323. }
  324. return 0;
  325. }
  326. qWarning("CarlaOsc::handle_midi() - recived midi when plugin has no midi inputs");
  327. return 1;
  328. }
  329. int CarlaOsc::handle_exiting(CARLA_OSC_HANDLE_ARGS1)
  330. {
  331. qDebug("CarlaOsc::handle_exiting()");
  332. // TODO - check for non-UIs (dssi-vst) and set to -1 instead
  333. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  334. plugin->clearOscData();
  335. return 0;
  336. }
  337. // -------------------------------------------------------------------------------------------------------------------
  338. int CarlaOsc::handle_set_active(CARLA_OSC_HANDLE_ARGS2)
  339. {
  340. qDebug("CarlaOsc::handle_set_active()");
  341. CARLA_OSC_CHECK_OSC_TYPES(1, "i");
  342. bool active = (bool)argv[0]->i;
  343. plugin->setActive(active, false, true);
  344. return 0;
  345. }
  346. int CarlaOsc::handle_set_drywet(CARLA_OSC_HANDLE_ARGS2)
  347. {
  348. qDebug("CarlaOsc::handle_set_drywet()");
  349. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  350. double value = argv[0]->d;
  351. plugin->setDryWet(value, false, true);
  352. return 0;
  353. }
  354. int CarlaOsc::handle_set_volume(CARLA_OSC_HANDLE_ARGS2)
  355. {
  356. qDebug("CarlaOsc::handle_set_volume()");
  357. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  358. double value = argv[0]->d;
  359. plugin->setVolume(value, false, true);
  360. return 0;
  361. }
  362. int CarlaOsc::handle_set_balance_left(CARLA_OSC_HANDLE_ARGS2)
  363. {
  364. qDebug("CarlaOsc::handle_set_balance_left()");
  365. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  366. double value = argv[0]->d;
  367. plugin->setBalanceLeft(value, false, true);
  368. return 0;
  369. }
  370. int CarlaOsc::handle_set_balance_right(CARLA_OSC_HANDLE_ARGS2)
  371. {
  372. qDebug("CarlaOsc::handle_set_balance_right()");
  373. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  374. double value = argv[0]->d;
  375. plugin->setBalanceRight(value, false, true);
  376. return 0;
  377. }
  378. int CarlaOsc::handle_set_parameter_value(CARLA_OSC_HANDLE_ARGS2)
  379. {
  380. qDebug("CarlaOsc::handle_set_parameter_value()");
  381. CARLA_OSC_CHECK_OSC_TYPES(2, "id");
  382. const uint32_t index = argv[0]->i;
  383. const double value = argv[1]->d;
  384. plugin->setParameterValue(index, value, true, false, true);
  385. return 0;
  386. }
  387. int CarlaOsc::handle_set_parameter_midi_cc(CARLA_OSC_HANDLE_ARGS2)
  388. {
  389. qDebug("CarlaOsc::handle_set_parameter_midi_cc()");
  390. CARLA_OSC_CHECK_OSC_TYPES(2, "ii");
  391. const uint32_t index = argv[0]->i;
  392. const int32_t cc = argv[1]->i;
  393. plugin->setParameterMidiCC(index, cc, false, true);
  394. return 0;
  395. }
  396. int CarlaOsc::handle_set_parameter_midi_channel(CARLA_OSC_HANDLE_ARGS2)
  397. {
  398. qDebug("CarlaOsc::handle_set_parameter_midi_channel()");
  399. CARLA_OSC_CHECK_OSC_TYPES(2, "ii");
  400. const uint32_t index = argv[0]->i;
  401. const uint32_t channel = argv[1]->i;
  402. plugin->setParameterMidiChannel(index, channel, false, true);
  403. return 0;
  404. }
  405. int CarlaOsc::handle_set_program(CARLA_OSC_HANDLE_ARGS2)
  406. {
  407. qDebug("CarlaOsc::handle_set_program()");
  408. CARLA_OSC_CHECK_OSC_TYPES(1, "i");
  409. const uint32_t index = argv[0]->i;
  410. plugin->setProgram(index, true, false, true, true);
  411. return 0;
  412. }
  413. int CarlaOsc::handle_set_midi_program(CARLA_OSC_HANDLE_ARGS2)
  414. {
  415. qDebug("CarlaOsc::handle_set_midi_program()");
  416. CARLA_OSC_CHECK_OSC_TYPES(1, "i");
  417. const uint32_t index = argv[0]->i;
  418. plugin->setMidiProgram(index, true, false, true, true);
  419. return 0;
  420. }
  421. int CarlaOsc::handle_note_on(CARLA_OSC_HANDLE_ARGS2)
  422. {
  423. qDebug("CarlaOsc::handle_note_on()");
  424. CARLA_OSC_CHECK_OSC_TYPES(3, "iii");
  425. const int channel = argv[0]->i;
  426. const int note = argv[1]->i;
  427. const int velo = argv[2]->i;
  428. plugin->sendMidiSingleNote(channel, note, velo, true, false, true);
  429. return 0;
  430. }
  431. int CarlaOsc::handle_note_off(CARLA_OSC_HANDLE_ARGS2)
  432. {
  433. qDebug("CarlaOsc::handle_note_off()");
  434. CARLA_OSC_CHECK_OSC_TYPES(2, "ii");
  435. const int channel = argv[0]->i;
  436. const int note = argv[1]->i;
  437. plugin->sendMidiSingleNote(channel, note, 0, true, false, true);
  438. return 0;
  439. }
  440. int CarlaOsc::handle_bridge_ains_peak(CARLA_OSC_HANDLE_ARGS2)
  441. {
  442. CARLA_OSC_CHECK_OSC_TYPES(2, "id");
  443. const int index = argv[0]->i;
  444. const double value = argv[1]->d;
  445. engine->setInputPeak(plugin->id(), index-1, value);
  446. return 0;
  447. }
  448. int CarlaOsc::handle_bridge_aouts_peak(CARLA_OSC_HANDLE_ARGS2)
  449. {
  450. CARLA_OSC_CHECK_OSC_TYPES(2, "id");
  451. const int index = argv[0]->i;
  452. const double value = argv[1]->d;
  453. engine->setOutputPeak(plugin->id(), index-1, value);
  454. return 0;
  455. }