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.

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