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.

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