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