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