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.

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