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.

589 lines
19KB

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