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.

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