Audio plugin host https://kx.studio/carla
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.

760 lines
25KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or 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 doc/GPL.txt file.
  16. */
  17. #include "CarlaEngine.hpp"
  18. #include "CarlaEngineOsc.hpp"
  19. #include "CarlaPlugin.hpp"
  20. #ifndef BUILD_BRIDGE
  21. # include "CarlaBridgeUtils.hpp"
  22. #endif
  23. #include "CarlaMIDI.h"
  24. #include <cctype>
  25. CARLA_BACKEND_START_NAMESPACE
  26. #ifndef BUILD_BRIDGE
  27. // -------------------------------------------------------------------
  28. // Bridge Helper, defined in BridgePlugin.cpp
  29. extern int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeInfoType type,
  30. const int argc, const lo_arg* const* const argv, const char* const types);
  31. #endif
  32. // -----------------------------------------------------------------------
  33. CarlaEngineOsc::CarlaEngineOsc(CarlaEngine* const engine) noexcept
  34. : fEngine(engine),
  35. fServerTCP(nullptr),
  36. fServerUDP(nullptr)
  37. {
  38. CARLA_SAFE_ASSERT(engine != nullptr);
  39. carla_debug("CarlaEngineOsc::CarlaEngineOsc(%p)", engine);
  40. }
  41. CarlaEngineOsc::~CarlaEngineOsc() noexcept
  42. {
  43. CARLA_SAFE_ASSERT(fName.isEmpty());
  44. CARLA_SAFE_ASSERT(fServerPathTCP.isEmpty());
  45. CARLA_SAFE_ASSERT(fServerPathUDP.isEmpty());
  46. CARLA_SAFE_ASSERT(fServerTCP == nullptr);
  47. CARLA_SAFE_ASSERT(fServerUDP == nullptr);
  48. carla_debug("CarlaEngineOsc::~CarlaEngineOsc()");
  49. }
  50. // -----------------------------------------------------------------------
  51. void CarlaEngineOsc::init(const char* const name) noexcept
  52. {
  53. CARLA_SAFE_ASSERT_RETURN(fName.isEmpty(),);
  54. CARLA_SAFE_ASSERT_RETURN(fServerPathTCP.isEmpty(),);
  55. CARLA_SAFE_ASSERT_RETURN(fServerPathUDP.isEmpty(),);
  56. CARLA_SAFE_ASSERT_RETURN(fServerTCP == nullptr,);
  57. CARLA_SAFE_ASSERT_RETURN(fServerUDP == nullptr,);
  58. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  59. carla_debug("CarlaEngineOsc::init(\"%s\")", name);
  60. fName = name;
  61. fName.toBasic();
  62. fServerTCP = lo_server_new_with_proto(nullptr, LO_TCP, osc_error_handler_TCP);
  63. if (fServerTCP != nullptr)
  64. {
  65. if (char* const tmpServerPathTCP = lo_server_get_url(fServerTCP))
  66. {
  67. fServerPathTCP = tmpServerPathTCP;
  68. fServerPathTCP += fName;
  69. std::free(tmpServerPathTCP);
  70. }
  71. lo_server_add_method(fServerTCP, nullptr, nullptr, osc_message_handler_TCP, this);
  72. }
  73. fServerUDP = lo_server_new_with_proto(nullptr, LO_UDP, osc_error_handler_UDP);
  74. if (fServerUDP != nullptr)
  75. {
  76. if (char* const tmpServerPathUDP = lo_server_get_url(fServerUDP))
  77. {
  78. fServerPathUDP = tmpServerPathUDP;
  79. fServerPathUDP += fName;
  80. std::free(tmpServerPathUDP);
  81. }
  82. lo_server_add_method(fServerUDP, nullptr, nullptr, osc_message_handler_UDP, this);
  83. }
  84. CARLA_SAFE_ASSERT(fName.isNotEmpty());
  85. CARLA_SAFE_ASSERT(fServerPathTCP.isNotEmpty());
  86. CARLA_SAFE_ASSERT(fServerPathUDP.isNotEmpty());
  87. CARLA_SAFE_ASSERT(fServerTCP != nullptr);
  88. CARLA_SAFE_ASSERT(fServerUDP != nullptr);
  89. }
  90. void CarlaEngineOsc::idle() const noexcept
  91. {
  92. if (fServerTCP != nullptr)
  93. {
  94. for (;;)
  95. {
  96. try {
  97. if (lo_server_recv_noblock(fServerTCP, 0) == 0)
  98. break;
  99. } CARLA_SAFE_EXCEPTION_CONTINUE("OSC idle TCP")
  100. }
  101. }
  102. if (fServerUDP != nullptr)
  103. {
  104. for (;;)
  105. {
  106. try {
  107. if (lo_server_recv_noblock(fServerUDP, 0) == 0)
  108. break;
  109. } CARLA_SAFE_EXCEPTION_CONTINUE("OSC idle UDP")
  110. }
  111. }
  112. }
  113. void CarlaEngineOsc::close() noexcept
  114. {
  115. CARLA_SAFE_ASSERT(fName.isNotEmpty());
  116. CARLA_SAFE_ASSERT(fServerPathTCP.isNotEmpty());
  117. CARLA_SAFE_ASSERT(fServerPathUDP.isNotEmpty());
  118. CARLA_SAFE_ASSERT(fServerTCP != nullptr);
  119. CARLA_SAFE_ASSERT(fServerUDP != nullptr);
  120. carla_debug("CarlaEngineOsc::close()");
  121. fName.clear();
  122. if (fServerTCP != nullptr)
  123. {
  124. lo_server_del_method(fServerTCP, nullptr, nullptr);
  125. lo_server_free(fServerTCP);
  126. fServerTCP = nullptr;
  127. }
  128. if (fServerUDP != nullptr)
  129. {
  130. lo_server_del_method(fServerUDP, nullptr, nullptr);
  131. lo_server_free(fServerUDP);
  132. fServerUDP = nullptr;
  133. }
  134. fServerPathTCP.clear();
  135. fServerPathUDP.clear();
  136. #ifndef BUILD_BRIDGE
  137. fControlData.free();
  138. #endif
  139. }
  140. // -----------------------------------------------------------------------
  141. int CarlaEngineOsc::handleMessage(const bool isTCP, const char* const path, const int argc, const lo_arg* const* const argv, const char* const types, const lo_message msg)
  142. {
  143. CARLA_SAFE_ASSERT_RETURN(fName.isNotEmpty(), 1);
  144. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', 1);
  145. #ifdef DEBUG
  146. if (std::strstr(path, "/bridge_pong") == nullptr) {
  147. carla_debug("CarlaEngineOsc::handleMessage(%s, \"%s\", %i, %p, \"%s\", %p)", bool2str(isTCP), path, argc, argv, types, msg);
  148. }
  149. #endif
  150. //if (isTCP)
  151. {
  152. CARLA_SAFE_ASSERT_RETURN(fServerPathTCP.isNotEmpty(), 1);
  153. CARLA_SAFE_ASSERT_RETURN(fServerTCP != nullptr, 1);
  154. }
  155. //else
  156. {
  157. CARLA_SAFE_ASSERT_RETURN(fServerPathUDP.isNotEmpty(), 1);
  158. CARLA_SAFE_ASSERT_RETURN(fServerUDP != nullptr, 1);
  159. }
  160. #ifndef BUILD_BRIDGE
  161. // Initial path check
  162. if (std::strcmp(path, "/register") == 0)
  163. {
  164. const lo_address source(lo_message_get_source(msg));
  165. return handleMsgRegister(isTCP, argc, argv, types, source);
  166. }
  167. if (std::strcmp(path, "/unregister") == 0)
  168. {
  169. return handleMsgUnregister();
  170. }
  171. #endif
  172. const size_t nameSize(fName.length());
  173. // Check if message is for this client
  174. if (std::strlen(path) <= nameSize || std::strncmp(path+1, fName, nameSize) != 0)
  175. {
  176. carla_stderr("CarlaEngineOsc::handleMessage() - message not for this client -> '%s' != '/%s/'", path, fName.buffer());
  177. return 1;
  178. }
  179. // Get plugin id from path, "/carla/23/method" -> 23
  180. uint pluginId = 0;
  181. size_t offset;
  182. if (std::isdigit(path[nameSize+2]))
  183. {
  184. if (std::isdigit(path[nameSize+3]))
  185. {
  186. if (std::isdigit(path[nameSize+5]))
  187. {
  188. carla_stderr2("CarlaEngineOsc::handleMessage() - invalid plugin id, over 999? (value: \"%s\")", path+(nameSize+1));
  189. return 1;
  190. }
  191. else if (std::isdigit(path[nameSize+4]))
  192. {
  193. // 3 digits, /xyz/method
  194. offset = 6;
  195. pluginId += uint(path[nameSize+2]-'0')*100;
  196. pluginId += uint(path[nameSize+3]-'0')*10;
  197. pluginId += uint(path[nameSize+4]-'0');
  198. }
  199. else
  200. {
  201. // 2 digits, /xy/method
  202. offset = 5;
  203. pluginId += uint(path[nameSize+2]-'0')*10;
  204. pluginId += uint(path[nameSize+3]-'0');
  205. }
  206. }
  207. else
  208. {
  209. // single digit, /x/method
  210. offset = 4;
  211. pluginId += uint(path[nameSize+2]-'0');
  212. }
  213. }
  214. else
  215. {
  216. carla_stderr("CarlaEngineOsc::handleMessage() - invalid message '%s'", path);
  217. return 1;
  218. }
  219. if (pluginId > fEngine->getCurrentPluginCount())
  220. {
  221. carla_stderr("CarlaEngineOsc::handleMessage() - failed to get plugin, wrong id '%i'", pluginId);
  222. return 0;
  223. }
  224. // Get plugin
  225. CarlaPlugin* const plugin(fEngine->getPluginUnchecked(pluginId));
  226. if (plugin == nullptr || plugin->getId() != pluginId)
  227. {
  228. carla_stderr("CarlaEngineOsc::handleMessage() - invalid plugin id '%i', probably has been removed", pluginId);
  229. return 0;
  230. }
  231. // Get method from path, "/Carla/i/method" -> "method"
  232. char method[32+1];
  233. method[32] = '\0';
  234. std::strncpy(method, path + (nameSize + offset), 32);
  235. if (method[0] == '\0')
  236. {
  237. carla_stderr("CarlaEngineOsc::handleMessage(%s, \"%s\", ...) - received message without method", bool2str(isTCP), path);
  238. return 0;
  239. }
  240. // Common OSC methods (DSSI and bridge UIs)
  241. if (std::strcmp(method, "update") == 0)
  242. {
  243. const lo_address source(lo_message_get_source(msg));
  244. return handleMsgUpdate(plugin, argc, argv, types, source);
  245. }
  246. if (std::strcmp(method, "configure") == 0)
  247. return handleMsgConfigure(plugin, argc, argv, types);
  248. if (std::strcmp(method, "control") == 0)
  249. return handleMsgControl(plugin, argc, argv, types);
  250. if (std::strcmp(method, "program") == 0)
  251. return handleMsgProgram(plugin, argc, argv, types);
  252. if (std::strcmp(method, "midi") == 0)
  253. return handleMsgMidi(plugin, argc, argv, types);
  254. if (std::strcmp(method, "exiting") == 0)
  255. return handleMsgExiting(plugin);
  256. #ifndef BUILD_BRIDGE
  257. // Internal methods
  258. if (std::strcmp(method, "set_active") == 0)
  259. return handleMsgSetActive(plugin, argc, argv, types);
  260. if (std::strcmp(method, "set_drywet") == 0)
  261. return handleMsgSetDryWet(plugin, argc, argv, types);
  262. if (std::strcmp(method, "set_volume") == 0)
  263. return handleMsgSetVolume(plugin, argc, argv, types);
  264. if (std::strcmp(method, "set_balance_left") == 0)
  265. return handleMsgSetBalanceLeft(plugin, argc, argv, types);
  266. if (std::strcmp(method, "set_balance_right") == 0)
  267. return handleMsgSetBalanceRight(plugin, argc, argv, types);
  268. if (std::strcmp(method, "set_panning") == 0)
  269. return handleMsgSetPanning(plugin, argc, argv, types);
  270. if (std::strcmp(method, "set_parameter_value") == 0)
  271. return handleMsgSetParameterValue(plugin, argc, argv, types);
  272. if (std::strcmp(method, "set_parameter_midi_cc") == 0)
  273. return handleMsgSetParameterMidiCC(plugin, argc, argv, types);
  274. if (std::strcmp(method, "set_parameter_midi_channel") == 0)
  275. return handleMsgSetParameterMidiChannel(plugin, argc, argv, types);
  276. if (std::strcmp(method, "set_program") == 0)
  277. return handleMsgSetProgram(plugin, argc, argv, types);
  278. if (std::strcmp(method, "set_midi_program") == 0)
  279. return handleMsgSetMidiProgram(plugin, argc, argv, types);
  280. if (std::strcmp(method, "note_on") == 0)
  281. return handleMsgNoteOn(plugin, argc, argv, types);
  282. if (std::strcmp(method, "note_off") == 0)
  283. return handleMsgNoteOff(plugin, argc, argv, types);
  284. // Plugin Bridges
  285. if ((plugin->getHints() & PLUGIN_IS_BRIDGE) != 0 && std::strlen(method) >= 11 && std::strncmp(method, "bridge_", 7) == 0)
  286. {
  287. const char* const bmethod(method+7);
  288. if (std::strcmp(bmethod, "pong") == 0)
  289. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgePong, argc, argv, types);
  290. if (std::strcmp(bmethod, "plugin_info1") == 0)
  291. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgePluginInfo1, argc, argv, types);
  292. if (std::strcmp(bmethod, "plugin_info2") == 0)
  293. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgePluginInfo2, argc, argv, types);
  294. if (std::strcmp(bmethod, "audio_count") == 0)
  295. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeAudioCount, argc, argv, types);
  296. if (std::strcmp(bmethod, "midi_count") == 0)
  297. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeMidiCount, argc, argv, types);
  298. if (std::strcmp(bmethod, "parameter_count") == 0)
  299. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeParameterCount, argc, argv, types);
  300. if (std::strcmp(bmethod, "program_count") == 0)
  301. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeProgramCount, argc, argv, types);
  302. if (std::strcmp(bmethod, "midi_program_count") == 0)
  303. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeMidiProgramCount, argc, argv, types);
  304. if (std::strcmp(bmethod, "parameter_data") == 0)
  305. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeParameterData, argc, argv, types);
  306. if (std::strcmp(bmethod, "parameter_ranges1") == 0)
  307. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeParameterRanges1, argc, argv, types);
  308. if (std::strcmp(bmethod, "parameter_ranges2") == 0)
  309. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeParameterRanges2, argc, argv, types);
  310. if (std::strcmp(bmethod, "parameter_midi_cc") == 0)
  311. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeParameterMidiCC, argc, argv, types);
  312. if (std::strcmp(bmethod, "parameter_midi_channel") == 0)
  313. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeParameterMidiChannel, argc, argv, types);
  314. if (std::strcmp(bmethod, "parameter_value") == 0)
  315. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeParameterValue, argc, argv, types);
  316. if (std::strcmp(bmethod, "default_value") == 0)
  317. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeDefaultValue, argc, argv, types);
  318. if (std::strcmp(bmethod, "current_program") == 0)
  319. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeCurrentProgram, argc, argv, types);
  320. if (std::strcmp(bmethod, "current_midi_program") == 0)
  321. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeCurrentMidiProgram, argc, argv, types);
  322. if (std::strcmp(bmethod, "program_name") == 0)
  323. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeProgramName, argc, argv, types);
  324. if (std::strcmp(bmethod, "midi_program_data") == 0)
  325. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeMidiProgramData, argc, argv, types);
  326. if (std::strcmp(bmethod, "configure") == 0)
  327. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeConfigure, argc, argv, types);
  328. if (std::strcmp(bmethod, "set_custom_data") == 0)
  329. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeSetCustomData, argc, argv, types);
  330. if (std::strcmp(bmethod, "set_chunk_data") == 0)
  331. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeSetChunkData, argc, argv, types);
  332. if (std::strcmp(bmethod, "update_now") == 0)
  333. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeUpdateNow, argc, argv, types);
  334. if (std::strcmp(bmethod, "error") == 0)
  335. return CarlaPluginSetOscBridgeInfo(plugin, kPluginBridgeError, argc, argv, types);
  336. }
  337. #endif
  338. // Plugin-specific methods
  339. #ifdef WANT_LV2
  340. if (std::strcmp(method, "lv2_atom_transfer") == 0)
  341. return handleMsgLv2AtomTransfer(plugin, argc, argv, types);
  342. if (std::strcmp(method, "lv2_urid_map") == 0)
  343. return handleMsgLv2UridMap(plugin, argc, argv, types);
  344. #endif
  345. carla_stderr("CarlaEngineOsc::handleMessage() - unsupported OSC method '%s'", method);
  346. return 1;
  347. }
  348. // -----------------------------------------------------------------------
  349. #ifndef BUILD_BRIDGE
  350. int CarlaEngineOsc::handleMsgRegister(const bool isTCP, const int argc, const lo_arg* const* const argv, const char* const types, const lo_address source)
  351. {
  352. carla_debug("CarlaEngineOsc::handleMsgRegister()");
  353. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "s");
  354. if (fControlData.path != nullptr)
  355. {
  356. carla_stderr("CarlaEngineOsc::handleMsgRegister() - OSC backend already registered to %s", fControlData.path);
  357. return 1;
  358. }
  359. const char* const url = &argv[0]->s;
  360. carla_debug("CarlaEngineOsc::handleMsgRegister() - OSC backend registered to %s", url);
  361. {
  362. const char* host = lo_address_get_hostname(source);
  363. const char* port = lo_address_get_port(source);
  364. fControlData.source = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  365. }
  366. {
  367. char* host = lo_url_get_hostname(url);
  368. char* port = lo_url_get_port(url);
  369. fControlData.path = carla_strdup_free(lo_url_get_path(url));
  370. fControlData.target = lo_address_new_with_proto(isTCP ? LO_TCP : LO_UDP, host, port);
  371. std::free(host);
  372. std::free(port);
  373. }
  374. for (uint i=0, count=fEngine->getCurrentPluginCount(); i < count; ++i)
  375. {
  376. CarlaPlugin* const plugin(fEngine->getPluginUnchecked(i));
  377. if (plugin != nullptr && plugin->isEnabled())
  378. plugin->registerToOscClient();
  379. }
  380. return 0;
  381. }
  382. int CarlaEngineOsc::handleMsgUnregister()
  383. {
  384. carla_debug("CarlaEngineOsc::handleMsgUnregister()");
  385. if (fControlData.path == nullptr)
  386. {
  387. carla_stderr("CarlaEngineOsc::handleMsgUnregister() - OSC backend is not registered yet");
  388. return 1;
  389. }
  390. fControlData.free();
  391. return 0;
  392. }
  393. #endif
  394. // -----------------------------------------------------------------------
  395. int CarlaEngineOsc::handleMsgUpdate(CARLA_ENGINE_OSC_HANDLE_ARGS2, const lo_address source)
  396. {
  397. carla_debug("CarlaEngineOsc::handleMsgUpdate()");
  398. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "s");
  399. const char* const url = (const char*)&argv[0]->s;
  400. plugin->updateOscData(source, url);
  401. return 0;
  402. }
  403. int CarlaEngineOsc::handleMsgConfigure(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  404. {
  405. carla_debug("CarlaEngineOsc::handleMsgConfigure()");
  406. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ss");
  407. const char* const key = (const char*)&argv[0]->s;
  408. const char* const value = (const char*)&argv[1]->s;
  409. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  410. return 0;
  411. }
  412. int CarlaEngineOsc::handleMsgControl(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  413. {
  414. carla_debug("CarlaEngineOsc::handleMsgControl()");
  415. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "if");
  416. const int32_t rindex = argv[0]->i;
  417. const float value = argv[1]->f;
  418. plugin->setParameterValueByRealIndex(rindex, value, false, true, true);
  419. return 0;
  420. }
  421. int CarlaEngineOsc::handleMsgProgram(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  422. {
  423. carla_debug("CarlaEngineOsc::handleMsgProgram()");
  424. if (argc == 2)
  425. {
  426. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  427. const int32_t bank = argv[0]->i;
  428. const int32_t program = argv[1]->i;
  429. CARLA_SAFE_ASSERT_RETURN(bank >= 0, 0);
  430. CARLA_SAFE_ASSERT_RETURN(program >= 0, 0);
  431. plugin->setMidiProgramById(static_cast<uint32_t>(bank), static_cast<uint32_t>(program), false, true, true);
  432. return 0;
  433. }
  434. else
  435. {
  436. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  437. const int32_t program = argv[0]->i;
  438. CARLA_SAFE_ASSERT_RETURN(program >= 0, 0);
  439. CARLA_SAFE_ASSERT_RETURN(program < static_cast<int32_t>(plugin->getProgramCount()), 0);
  440. plugin->setProgram(program, false, true, true);
  441. return 0;
  442. }
  443. }
  444. int CarlaEngineOsc::handleMsgMidi(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  445. {
  446. carla_debug("CarlaEngineOsc::handleMsgMidi()");
  447. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "m");
  448. #ifdef BUILD_BRIDGE
  449. CARLA_ASSERT(false); // this should never happen
  450. return 0;
  451. // unused
  452. (void)plugin;
  453. (void)argv;
  454. #else
  455. if (plugin->getMidiInCount() == 0)
  456. {
  457. carla_stderr("CarlaEngineOsc::handleMsgMidi() - received midi when plugin has no midi inputs");
  458. return 0;
  459. }
  460. const uint8_t* const data = argv[0]->m;
  461. uint8_t status = data[1];
  462. uint8_t channel = status & 0x0F;
  463. // Fix bad note-off
  464. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  465. status = MIDI_STATUS_NOTE_OFF;
  466. if (MIDI_IS_STATUS_NOTE_OFF(status))
  467. {
  468. const uint8_t note = data[2];
  469. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, 0);
  470. plugin->sendMidiSingleNote(channel, note, 0, false, true, true);
  471. }
  472. else if (MIDI_IS_STATUS_NOTE_ON(status))
  473. {
  474. const uint8_t note = data[2];
  475. const uint8_t velo = data[3];
  476. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE, 0);
  477. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE, 0);
  478. plugin->sendMidiSingleNote(channel, note, velo, false, true, true);
  479. }
  480. return 0;
  481. #endif
  482. }
  483. int CarlaEngineOsc::handleMsgExiting(CARLA_ENGINE_OSC_HANDLE_ARGS1)
  484. {
  485. carla_debug("CarlaEngineOsc::handleMsgExiting()");
  486. // TODO - check for non-UIs (dssi-vst) and set to -1 instead
  487. fEngine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, plugin->getId(), 0, 0, 0.0f, nullptr);
  488. plugin->showCustomUI(false);
  489. return 0;
  490. }
  491. // -----------------------------------------------------------------------
  492. #ifndef BUILD_BRIDGE
  493. int CarlaEngineOsc::handleMsgSetActive(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  494. {
  495. carla_debug("CarlaEngineOsc::handleMsgSetActive()");
  496. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  497. const bool active = (argv[0]->i != 0);
  498. plugin->setActive(active, false, true);
  499. return 0;
  500. }
  501. int CarlaEngineOsc::handleMsgSetDryWet(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  502. {
  503. carla_debug("CarlaEngineOsc::handleMsgSetDryWet()");
  504. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  505. const float value = argv[0]->f;
  506. plugin->setDryWet(value, false, true);
  507. return 0;
  508. }
  509. int CarlaEngineOsc::handleMsgSetVolume(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  510. {
  511. carla_debug("CarlaEngineOsc::handleMsgSetVolume()");
  512. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  513. const float value = argv[0]->f;
  514. plugin->setVolume(value, false, true);
  515. return 0;
  516. }
  517. int CarlaEngineOsc::handleMsgSetBalanceLeft(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  518. {
  519. carla_debug("CarlaEngineOsc::handleMsgSetBalanceLeft()");
  520. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  521. const float value = argv[0]->f;
  522. plugin->setBalanceLeft(value, false, true);
  523. return 0;
  524. }
  525. int CarlaEngineOsc::handleMsgSetBalanceRight(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  526. {
  527. carla_debug("CarlaEngineOsc::handleMsgSetBalanceRight()");
  528. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  529. const float value = argv[0]->f;
  530. plugin->setBalanceRight(value, false, true);
  531. return 0;
  532. }
  533. int CarlaEngineOsc::handleMsgSetPanning(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  534. {
  535. carla_debug("CarlaEngineOsc::handleMsgSetPanning()");
  536. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "f");
  537. const float value = argv[0]->f;
  538. plugin->setPanning(value, false, true);
  539. return 0;
  540. }
  541. int CarlaEngineOsc::handleMsgSetParameterValue(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  542. {
  543. carla_debug("CarlaEngineOsc::handleMsgSetParameterValue()");
  544. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "if");
  545. const int32_t index = argv[0]->i;
  546. const float value = argv[1]->f;
  547. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  548. plugin->setParameterValue(static_cast<uint32_t>(index), value, true, false, true);
  549. return 0;
  550. }
  551. int CarlaEngineOsc::handleMsgSetParameterMidiCC(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  552. {
  553. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiCC()");
  554. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  555. const int32_t index = argv[0]->i;
  556. const int32_t cc = argv[1]->i;
  557. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  558. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc <= 0x5F, 0);
  559. plugin->setParameterMidiCC(static_cast<uint32_t>(index), static_cast<int16_t>(cc), false, true);
  560. return 0;
  561. }
  562. int CarlaEngineOsc::handleMsgSetParameterMidiChannel(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  563. {
  564. carla_debug("CarlaEngineOsc::handleMsgSetParameterMidiChannel()");
  565. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  566. const int32_t index = argv[0]->i;
  567. const int32_t channel = argv[1]->i;
  568. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  569. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  570. plugin->setParameterMidiChannel(static_cast<uint32_t>(index), static_cast<uint8_t>(channel), false, true);
  571. return 0;
  572. }
  573. int CarlaEngineOsc::handleMsgSetProgram(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  574. {
  575. carla_debug("CarlaEngineOsc::handleMsgSetProgram()");
  576. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  577. const int32_t index = argv[0]->i;
  578. CARLA_SAFE_ASSERT_RETURN(index >= -1, 0);
  579. plugin->setProgram(index, true, false, true);
  580. return 0;
  581. }
  582. int CarlaEngineOsc::handleMsgSetMidiProgram(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  583. {
  584. carla_debug("CarlaEngineOsc::handleMsgSetMidiProgram()");
  585. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(1, "i");
  586. const int32_t index = argv[0]->i;
  587. CARLA_SAFE_ASSERT_RETURN(index >= -1, 0);
  588. plugin->setMidiProgram(index, true, false, true);
  589. return 0;
  590. }
  591. int CarlaEngineOsc::handleMsgNoteOn(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  592. {
  593. carla_debug("CarlaEngineOsc::handleMsgNoteOn()");
  594. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iii");
  595. const int32_t channel = argv[0]->i;
  596. const int32_t note = argv[1]->i;
  597. const int32_t velo = argv[2]->i;
  598. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  599. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE, 0);
  600. CARLA_SAFE_ASSERT_RETURN(velo >= 0 && velo < MAX_MIDI_VALUE, 0);
  601. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), static_cast<uint8_t>(velo), true, false, true);
  602. return 0;
  603. }
  604. int CarlaEngineOsc::handleMsgNoteOff(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  605. {
  606. carla_debug("CarlaEngineOsc::handleMsgNoteOff()");
  607. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "ii");
  608. const int32_t channel = argv[0]->i;
  609. const int32_t note = argv[1]->i;
  610. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS, 0);
  611. CARLA_SAFE_ASSERT_RETURN(note >= 0 && note < MAX_MIDI_NOTE, 0);
  612. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), 0, true, false, true);
  613. return 0;
  614. }
  615. #endif
  616. // -----------------------------------------------------------------------
  617. CARLA_BACKEND_END_NAMESPACE