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.

804 lines
26KB

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