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.

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