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.

584 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. // FIXME - max plugins
  225. for (unsigned short i=0; i < CarlaBackend::MAX_PLUGINS; i++)
  226. {
  227. CarlaBackend::CarlaPlugin* const plugin = engine->getPluginUnchecked(i);
  228. if (plugin && plugin->enabled())
  229. plugin->registerToOsc();
  230. }
  231. return 0;
  232. }
  233. int CarlaOsc::handle_unregister()
  234. {
  235. qDebug("CarlaOsc::handle_unregister()");
  236. if (! m_controllerData.path)
  237. {
  238. qWarning("CarlaOsc::handle_unregister() - OSC backend is not registered yet");
  239. return 1;
  240. }
  241. osc_clear_data(&m_controllerData);
  242. return 0;
  243. }
  244. // -------------------------------------------------------------------------------------------------------------------
  245. int CarlaOsc::handle_update(CARLA_OSC_HANDLE_ARGS2, const lo_address source)
  246. {
  247. qDebug("CarlaOsc::handle_update()");
  248. CARLA_OSC_CHECK_OSC_TYPES(1, "s");
  249. const char* const url = (const char*)&argv[0]->s;
  250. plugin->updateOscData(source, url);
  251. return 0;
  252. }
  253. int CarlaOsc::handle_configure(CARLA_OSC_HANDLE_ARGS2)
  254. {
  255. qDebug("CarlaOsc::handle_configure()");
  256. CARLA_OSC_CHECK_OSC_TYPES(2, "ss");
  257. const char* const key = (const char*)&argv[0]->s;
  258. const char* const value = (const char*)&argv[1]->s;
  259. if (plugin->hints() & CarlaBackend::PLUGIN_IS_BRIDGE)
  260. {
  261. if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_HIDE_GUI) == 0)
  262. {
  263. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  264. return 0;
  265. }
  266. if (strcmp(key, CarlaBackend::CARLA_BRIDGE_MSG_SAVED) == 0)
  267. {
  268. return plugin->setOscBridgeInfo(CarlaBackend::PluginBridgeSaved, nullptr);
  269. }
  270. }
  271. plugin->setCustomData(CarlaBackend::CUSTOM_DATA_STRING, key, value, false);
  272. return 0;
  273. }
  274. int CarlaOsc::handle_control(CARLA_OSC_HANDLE_ARGS2)
  275. {
  276. qDebug("CarlaOsc::handle_control()");
  277. CARLA_OSC_CHECK_OSC_TYPES(2, "if");
  278. const int rindex = argv[0]->i;
  279. const float value = argv[1]->f;
  280. plugin->setParameterValueByRIndex(rindex, value, false, true, true);
  281. return 0;
  282. }
  283. int CarlaOsc::handle_program(CARLA_OSC_HANDLE_ARGS2)
  284. {
  285. qDebug("CarlaOsc::handle_program()");
  286. if (argc == 2)
  287. {
  288. CARLA_OSC_CHECK_OSC_TYPES(2, "ii");
  289. const uint32_t bank_id = argv[0]->i;
  290. const uint32_t program_id = argv[1]->i;
  291. plugin->setMidiProgramById(bank_id, program_id, false, true, true, true);
  292. return 0;
  293. }
  294. else
  295. {
  296. CARLA_OSC_CHECK_OSC_TYPES(1, "i");
  297. const uint32_t program_id = argv[0]->i;
  298. if (program_id < plugin->programCount())
  299. {
  300. plugin->setProgram(program_id, false, true, true, true);
  301. return 0;
  302. }
  303. qCritical("CarlaOsc::handle_program() - program_id '%i' out of bounds", program_id);
  304. }
  305. return 1;
  306. }
  307. int CarlaOsc::handle_midi(CARLA_OSC_HANDLE_ARGS2)
  308. {
  309. qDebug("CarlaOsc::handle_midi()");
  310. CARLA_OSC_CHECK_OSC_TYPES(1, "m");
  311. if (plugin->midiInCount() > 0)
  312. {
  313. const uint8_t* const data = argv[0]->m;
  314. uint8_t status = data[1];
  315. uint8_t channel = status & 0x0F;
  316. // Fix bad note-off
  317. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  318. status -= 0x10;
  319. if (MIDI_IS_STATUS_NOTE_OFF(status))
  320. {
  321. uint8_t note = data[2];
  322. plugin->sendMidiSingleNote(channel, note, 0, false, true, true);
  323. }
  324. else if (MIDI_IS_STATUS_NOTE_ON(status))
  325. {
  326. uint8_t note = data[2];
  327. uint8_t velo = data[3];
  328. plugin->sendMidiSingleNote(channel, note, velo, false, true, true);
  329. }
  330. return 0;
  331. }
  332. qWarning("CarlaOsc::handle_midi() - recived midi when plugin has no midi inputs");
  333. return 1;
  334. }
  335. int CarlaOsc::handle_exiting(CARLA_OSC_HANDLE_ARGS1)
  336. {
  337. qDebug("CarlaOsc::handle_exiting()");
  338. // TODO - check for non-UIs (dssi-vst) and set to -1 instead
  339. engine->callback(CarlaBackend::CALLBACK_SHOW_GUI, plugin->id(), 0, 0, 0.0);
  340. plugin->clearOscData();
  341. return 0;
  342. }
  343. // -------------------------------------------------------------------------------------------------------------------
  344. int CarlaOsc::handle_set_active(CARLA_OSC_HANDLE_ARGS2)
  345. {
  346. qDebug("CarlaOsc::handle_set_active()");
  347. CARLA_OSC_CHECK_OSC_TYPES(1, "i");
  348. bool active = (bool)argv[0]->i;
  349. plugin->setActive(active, false, true);
  350. return 0;
  351. }
  352. int CarlaOsc::handle_set_drywet(CARLA_OSC_HANDLE_ARGS2)
  353. {
  354. qDebug("CarlaOsc::handle_set_drywet()");
  355. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  356. double value = argv[0]->d;
  357. plugin->setDryWet(value, false, true);
  358. return 0;
  359. }
  360. int CarlaOsc::handle_set_volume(CARLA_OSC_HANDLE_ARGS2)
  361. {
  362. qDebug("CarlaOsc::handle_set_volume()");
  363. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  364. double value = argv[0]->d;
  365. plugin->setVolume(value, false, true);
  366. return 0;
  367. }
  368. int CarlaOsc::handle_set_balance_left(CARLA_OSC_HANDLE_ARGS2)
  369. {
  370. qDebug("CarlaOsc::handle_set_balance_left()");
  371. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  372. double value = argv[0]->d;
  373. plugin->setBalanceLeft(value, false, true);
  374. return 0;
  375. }
  376. int CarlaOsc::handle_set_balance_right(CARLA_OSC_HANDLE_ARGS2)
  377. {
  378. qDebug("CarlaOsc::handle_set_balance_right()");
  379. CARLA_OSC_CHECK_OSC_TYPES(1, "d");
  380. double value = argv[0]->d;
  381. plugin->setBalanceRight(value, false, true);
  382. return 0;
  383. }
  384. int CarlaOsc::handle_set_parameter_value(CARLA_OSC_HANDLE_ARGS2)
  385. {
  386. qDebug("CarlaOsc::handle_set_parameter_value()");
  387. CARLA_OSC_CHECK_OSC_TYPES(2, "id");
  388. const uint32_t index = argv[0]->i;
  389. const double value = argv[1]->d;
  390. plugin->setParameterValue(index, value, true, false, true);
  391. return 0;
  392. }
  393. int CarlaOsc::handle_set_parameter_midi_cc(CARLA_OSC_HANDLE_ARGS2)
  394. {
  395. qDebug("CarlaOsc::handle_set_parameter_midi_cc()");
  396. CARLA_OSC_CHECK_OSC_TYPES(2, "ii");
  397. const uint32_t index = argv[0]->i;
  398. const int32_t cc = argv[1]->i;
  399. plugin->setParameterMidiCC(index, cc, false, true);
  400. return 0;
  401. }
  402. int CarlaOsc::handle_set_parameter_midi_channel(CARLA_OSC_HANDLE_ARGS2)
  403. {
  404. qDebug("CarlaOsc::handle_set_parameter_midi_channel()");
  405. CARLA_OSC_CHECK_OSC_TYPES(2, "ii");
  406. const uint32_t index = argv[0]->i;
  407. const uint32_t channel = argv[1]->i;
  408. plugin->setParameterMidiChannel(index, channel, false, true);
  409. return 0;
  410. }
  411. int CarlaOsc::handle_set_program(CARLA_OSC_HANDLE_ARGS2)
  412. {
  413. qDebug("CarlaOsc::handle_set_program()");
  414. CARLA_OSC_CHECK_OSC_TYPES(1, "i");
  415. const uint32_t index = argv[0]->i;
  416. plugin->setProgram(index, true, false, true, true);
  417. return 0;
  418. }
  419. int CarlaOsc::handle_set_midi_program(CARLA_OSC_HANDLE_ARGS2)
  420. {
  421. qDebug("CarlaOsc::handle_set_midi_program()");
  422. CARLA_OSC_CHECK_OSC_TYPES(1, "i");
  423. const uint32_t index = argv[0]->i;
  424. plugin->setMidiProgram(index, true, false, true, true);
  425. return 0;
  426. }
  427. int CarlaOsc::handle_note_on(CARLA_OSC_HANDLE_ARGS2)
  428. {
  429. qDebug("CarlaOsc::handle_note_on()");
  430. CARLA_OSC_CHECK_OSC_TYPES(3, "iii");
  431. const int channel = argv[0]->i;
  432. const int note = argv[1]->i;
  433. const int velo = argv[2]->i;
  434. plugin->sendMidiSingleNote(channel, note, velo, true, false, true);
  435. return 0;
  436. }
  437. int CarlaOsc::handle_note_off(CARLA_OSC_HANDLE_ARGS2)
  438. {
  439. qDebug("CarlaOsc::handle_note_off()");
  440. CARLA_OSC_CHECK_OSC_TYPES(2, "ii");
  441. const int channel = argv[0]->i;
  442. const int note = argv[1]->i;
  443. plugin->sendMidiSingleNote(channel, note, 0, true, false, true);
  444. return 0;
  445. }
  446. int CarlaOsc::handle_bridge_ains_peak(CARLA_OSC_HANDLE_ARGS2)
  447. {
  448. CARLA_OSC_CHECK_OSC_TYPES(2, "id");
  449. const int index = argv[0]->i;
  450. const double value = argv[1]->d;
  451. engine->setInputPeak(plugin->id(), index-1, value);
  452. return 0;
  453. }
  454. int CarlaOsc::handle_bridge_aouts_peak(CARLA_OSC_HANDLE_ARGS2)
  455. {
  456. CARLA_OSC_CHECK_OSC_TYPES(2, "id");
  457. const int index = argv[0]->i;
  458. const double value = argv[1]->d;
  459. engine->setOutputPeak(plugin->id(), index-1, value);
  460. return 0;
  461. }
  462. CARLA_BACKEND_END_NAMESPACE