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.

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