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.

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