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.

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