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.

596 lines
19KB

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