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.

840 lines
27KB

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